Commit 02da191b authored by Janur J.'s avatar Janur J.
Browse files

Location counters reviewed

parent ed7eb4a0
......@@ -107,47 +107,32 @@ SQL;
}
protected function _insert(array $entryData)
{
$this->getHelper()->insert($entryData);
return (0 === $this->_iaDb->getErrorNumber()) ? $entryData['id'] : false;
}
protected function _entryAdd(array $entryData)
{
$entryData['id'] = $this->_getNewEntryId();
$entryData['date_added'] = date(iaDb::DATETIME_FORMAT);
$entryData['date_modified'] = date(iaDb::DATETIME_FORMAT);
return parent::_entryAdd($entryData);
$this->getHelper()->insert($entryData);
return (0 === $this->_iaDb->getErrorNumber()) ? $entryData['id'] : false;
}
protected function _entryUpdate(array $entryData, $entryId)
protected function _update(array $entryData, $entryId)
{
$entryData['date_modified'] = date(iaDb::DATETIME_FORMAT);
return parent::_entryUpdate($entryData, $entryId);
return parent::_update($entryData, $entryId);
}
public function updateCounters($entryId, array $entryData, $action, $previousData = null)
{
switch ($action) {
case iaCore::ACTION_ADD:
$this->_iaLocation->updateCounters($entryData, ['status' => iaEstate::STATUS_HIDDEN], $action);
break;
case iaCore::ACTION_EDIT:
$this->_iaLocation->updateCounters(array_merge(['location_id' => $previousData['location_id']],
$entryData), $previousData, $action);
break;
case iaCore::ACTION_DELETE:
$this->_iaLocation->updateCounters(['status' => iaEstate::STATUS_HIDDEN], $entryData, $action);
if (isset($_SESSION[iaEstate::SESSION_RECENT_KEY][$entryId]))// remove from the Recently Viewed list
{
unset($_SESSION[iaEstate::SESSION_RECENT_KEY][$entryId]);
}
$this->_iaLocation->editCounters($action, $entryData, $previousData);
if (iaCore::ACTION_DELETE) {
// remove from the Recently Viewed list
if (isset($_SESSION[iaEstate::SESSION_RECENT_KEY][$entryId])) {
unset($_SESSION[iaEstate::SESSION_RECENT_KEY][$entryId]);
}
}
}
......
......@@ -499,7 +499,7 @@ SQL;
//
protected function _editCounter($entryId, $action)
protected function _editCounter($entryId, $action = self::COUNTER_INCREMENT)
{
$parentsList = $this->getParentsFlat($entryId);
......@@ -514,20 +514,38 @@ SQL;
);
}
public function updateCounters($itemId, array $itemData, $action, $previousData = null)
public function editCounters($action, array $itemData, $previousData = null)
{
if ($itemData[self::COLUMN_STATUS] == self::STATUS_AVAILABLE && $previousData[self::COLUMN_STATUS] != self::STATUS_AVAILABLE) // status of the listing has been changed to Active
{
$this->_editCounter($itemData[self::COLUMN_LOCATION], self::COUNTER_INCREMENT);
} elseif ($previousData[self::COLUMN_STATUS] == self::STATUS_AVAILABLE && $itemData[self::COLUMN_STATUS] != self::STATUS_AVAILABLE) // listing has been deactivated
{
$this->_editCounter($previousData[self::COLUMN_LOCATION], self::COUNTER_DECREMENT);
} elseif ($itemData[self::COLUMN_STATUS] == self::STATUS_AVAILABLE && $previousData[self::COLUMN_STATUS] == self::STATUS_AVAILABLE) // listing has only been moved to another category
{
if (isset($itemData[self::COLUMN_LOCATION]) && isset($previousData[self::COLUMN_LOCATION]) && $itemData[self::COLUMN_LOCATION] != $previousData[self::COLUMN_LOCATION]) {
$this->_editCounter($itemData[self::COLUMN_LOCATION], self::COUNTER_INCREMENT);
$this->_editCounter($previousData[self::COLUMN_LOCATION], self::COUNTER_DECREMENT);
}
switch ($action) {
case iaCore::ACTION_EDIT:
if (!isset($itemData['location_id'])) {
if (iaEstate::STATUS_AVAILABLE == $previousData['status'] && iaEstate::STATUS_AVAILABLE != $itemData['status']) {
$this->_editCounter($previousData['location_id'], self::COUNTER_DECREMENT);
} elseif (iaEstate::STATUS_AVAILABLE != $previousData['status'] && iaEstate::STATUS_AVAILABLE == $itemData['status']) {
$this->_editCounter($previousData['location_id']);
}
} else {
if ($itemData['location_id'] == $previousData['location_id']) {
if (iaEstate::STATUS_AVAILABLE == $previousData['status'] && iaEstate::STATUS_AVAILABLE != $itemData['status']) {
$this->_editCounter($itemData['location_id'], self::COUNTER_DECREMENT);
} elseif (iaEstate::STATUS_AVAILABLE != $previousData['status'] && iaEstate::STATUS_AVAILABLE == $itemData['status']) {
$this->_editCounter($itemData['location_id']);
}
} else { // category changed
iaEstate::STATUS_AVAILABLE == $itemData['status']
&& $this->_editCounter($itemData['location_id']);
iaEstate::STATUS_AVAILABLE == $previousData['status']
&& $this->_editCounter($previousData['location_id'], self::COUNTER_DECREMENT);
}
}
break;
case iaCore::ACTION_ADD:
iaEstate::STATUS_AVAILABLE == $itemData['status']
&& $this->_editCounter($itemData['location_id']);
break;
case iaCore::ACTION_DELETE:
iaEstate::STATUS_AVAILABLE == $itemData['status']
&& $this->_editCounter($itemData['location_id'], self::COUNTER_DECREMENT);
}
}
......
......@@ -263,7 +263,6 @@ class iaEstate extends abstractRealestateModuleFront
{
$rows = $this->get('e.`id` IN (' . implode(',', $ids) . ')', 0, 100,
['field' => 'date_modified', 'direction' => iaDb::ORDER_DESC]);
_v($rows);
if ($rows) {
foreach ($rows as &$row) {
......@@ -369,8 +368,17 @@ SQL;
$result = (0 == $this->iaDb->getErrorNumber()) ? $entryData[self::COLUMN_ID] : false;
if (!iaUsers::hasIdentity() && $result) {
$this->_rememberUsersListing($entryData);
if ($result) {
$this->updateCounters($entryData[self::COLUMN_ID], $entryData, iaCore::ACTION_ADD);
// finally, notify plugins
$this->iaCore->startHook('phpListingAdded', [
'itemId' => $entryData[self::COLUMN_ID],
'itemName' => $this->getItemName(),
'itemData' => $entryData
]);
iaUsers::hasIdentity() || $this->_rememberUsersListing($entryData);
}
return $result;
......@@ -383,30 +391,23 @@ SQL;
return parent::update($entryData, $id);
}
public function delete($listingId)
public function updateCounters($itemId, array $itemData, $action, $previousData = null)
{
$listingData = $this->getById($listingId);
$result = parent::delete($listingId);
if ($result) {
// update the listings counter for the location listing was in
$iaLocation = $this->iaCore->factoryModule('location', $this->getModuleName(), 'common');
$iaLocation->updateCounters([iaLocation::COLUMN_STATUS => self::STATUS_HIDDEN], $listingData);
$this->iaCore->factoryModule('location', $this->getModuleName(), 'common')
->editCounters($action, $itemData, $previousData);
if (iaCore::ACTION_DELETE == $action) {
// remove from the Recently Viewed list
if (isset($_SESSION[self::SESSION_RECENT_KEY][$listingData[self::COLUMN_ID]])) {
unset($_SESSION[self::SESSION_RECENT_KEY][$listingData[self::COLUMN_ID]]);
if (isset($_SESSION[self::SESSION_RECENT_KEY][$itemId])) {
unset($_SESSION[self::SESSION_RECENT_KEY][$itemId]);
}
//
// finally, remove from comparing list if exists
if (isset($_SESSION[self::SESSION_COMPARISON_KEY][$listingData[self::COLUMN_ID]])) {
unset($_SESSION[self::SESSION_COMPARISON_KEY][$listingData[self::COLUMN_ID]]);
if (isset($_SESSION[self::SESSION_COMPARISON_KEY][$itemId])) {
unset($_SESSION[self::SESSION_COMPARISON_KEY][$itemId]);
}
};
return $result;
}
}
public function sortingParams(array $keys, array $defaults)
......
......@@ -22,7 +22,7 @@ if (iaView::REQUEST_HTML != $iaView->getRequestType()) {
}
$iaCore->factory('util');
$iaProperty = $iaCore->factoryModule('estate', IA_CURRENT_MODULE);
$iaEstate = $iaCore->factoryModule('estate', IA_CURRENT_MODULE);
$listingId = 0;
......@@ -40,7 +40,7 @@ switch ($pageAction) {
if (1 == count($_GET)) {
$key = key($_GET);
if (is_numeric($key) && !$_GET[$key]) {
if ($row = $iaProperty->getById($key, true)) {
if ($row = $iaEstate->getById($key, true)) {
$listing = $row;
unset($listing[iaDb::ID_COLUMN_SELECTION]);
}
......@@ -56,7 +56,7 @@ switch ($pageAction) {
}
$listingId = end($iaCore->requestPath);
$listing = $iaProperty->getById($listingId, true);
$listing = $iaEstate->getById($listingId, true);
if (empty($listing)) {
return iaView::errorPage(iaView::ERROR_NOT_FOUND);
......@@ -67,11 +67,11 @@ switch ($pageAction) {
}
if (iaCore::ACTION_DELETE == $pageAction) {
$result = $iaProperty->delete($listingId);
$result = $iaEstate->delete($listingId);
iaUtil::redirect(
iaLanguage::get($result ? 'thanks' : 'error'),
iaLanguage::get($result ? 'listing_successfully_removed' : 'db_error'),
$result ? $iaProperty->url('account', []) : $iaProperty->url('view', $listing)
$result ? $iaEstate->url('account', []) : $iaEstate->url('view', $listing)
);
}
......@@ -82,17 +82,17 @@ switch ($pageAction) {
}
$iaField = $iaCore->factory('field');
$iaLocation = $iaCore->factoryModule('location', $iaProperty->getModuleName(), 'common');
$iaLocation = $iaCore->factoryModule('location', $iaEstate->getModuleName(), 'common');
$iaPlan = $iaCore->factory('plan');
// get available plans
$plans = $iaPlan->getPlans($iaProperty->getItemName());
$plans = $iaPlan->getPlans($iaEstate->getItemName());
if (isset($_POST['data-property'])) {
$error = false;
$messages = [];
list($item, $error, $messages) = $iaField->parsePost($iaProperty->getItemName(), $listing);
list($item, $error, $messages) = $iaField->parsePost($iaEstate->getItemName(), $listing);
if (!iaUsers::hasIdentity() && !iaValidate::isCaptchaValid()) {
$error = true;
......@@ -122,7 +122,7 @@ if (isset($_POST['data-property'])) {
if (iaCore::ACTION_ADD == $pageAction) {
$item[iaEstate::COLUMN_STATUS] = $autoApprovalEnabled ? iaEstate::STATUS_AVAILABLE : iaCore::STATUS_APPROVAL;
$listingId = $iaProperty->insert($item);
$listingId = $iaEstate->insert($item);
$result = (bool)$listingId;
$resultMessage = iaLanguage::get('listing_successfully_submitted');
......@@ -135,7 +135,7 @@ if (isset($_POST['data-property'])) {
$item[iaEstate::COLUMN_STATUS] = iaCore::STATUS_APPROVAL;
}
$result = $iaProperty->update($item, $listingId);
$result = $iaEstate->update($item, $listingId);
$resultMessage = iaLanguage::get('listing_successfully_updated');
}
......@@ -143,7 +143,7 @@ if (isset($_POST['data-property'])) {
$iaCore->startHook('phpAddItemAfterAll', [
'type' => iaCore::FRONT,
'listing' => $listingId,
'item' => $iaProperty->getItemName(),
'item' => $iaEstate->getItemName(),
'data' => $item,
'old' => $listing
]);
......@@ -151,14 +151,14 @@ if (isset($_POST['data-property'])) {
if ($result) {
$iaLocation->updateCounters($item, $listing);
$item = $iaProperty->getById($listingId, true);
$item = $iaEstate->getById($listingId, true);
$url = $iaProperty->url('view', $item);
$url = $iaEstate->url('view', $item);
if (isset($_POST['plan_id']) && $_POST['plan_id']) {
$plan = $iaPlan->getById((int)$_POST['plan_id']);
if ($plan['cost'] > 0) {
$url = $iaPlan->prePayment($iaProperty->getItemName(), $item, $plan['id']);
$url = $iaPlan->prePayment($iaEstate->getItemName(), $item, $plan['id']);
}
}
......@@ -170,7 +170,7 @@ if (isset($_POST['data-property'])) {
}
}
$sections = $iaField->getTabs($iaProperty->getItemName(), $listing);
$sections = $iaField->getTabs($iaEstate->getItemName(), $listing);
if (iaCore::ACTION_EDIT == $pageAction) {
if ($listing['status'] != iaCore::STATUS_APPROVAL && $iaCore->get('realestate_auto_approval')) {
......@@ -200,7 +200,7 @@ if (iaCore::ACTION_EDIT == $pageAction) {
}
}
$listing['item'] = $iaProperty->getItemName();
$listing['item'] = $iaEstate->getItemName();
$listing['parents'] = $iaLocation->getParentsFlat($listing[iaEstate::COLUMN_LOCATION]);
$iaView->assign('item', $listing);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment