public static function optimizeListListingQuery(&$query, $massSelect = false) { $user = JFactory::getUser(); $query->select('cmain.id AS cat_id'); $query->join('', '#__judirectory_listings_xref AS listingxmain ON listing.id = listingxmain.listing_id AND listingxmain.main = 1'); $query->join('', '#__judirectory_categories AS cmain ON cmain.id = listingxmain.cat_id'); $categoryIdArrayCanAccess = JUDirectoryFrontHelperPermission::getAccessibleCategoryIds(); if (is_array($categoryIdArrayCanAccess) && count($categoryIdArrayCanAccess) > 0) { $query->where('cmain.id IN(' . implode(",", $categoryIdArrayCanAccess) . ')'); } else { $query->where('cmain.id IN("")'); } if ($massSelect) { $commentsField = new JUDirectoryFieldCore_comments(); if ($commentsField->canView(array("view" => "list"))) { $isModerator = JUDirectoryFrontHelperModerator::isModerator(); if (!$isModerator && !$user->authorise('core.admin', 'com_judirectory')) { $params = JUDirectoryHelper::getParams(); $negative_vote_comment = $params->get('negative_vote_comment'); if (is_numeric($negative_vote_comment) && $negative_vote_comment > 0) { $query->select('(SELECT COUNT(*) FROM #__judirectory_comments AS cm WHERE cm.listing_id = listing.id AND cm.approved = 1 AND cm.published = 1 AND cm.level = 1 AND (cm.total_votes - cm.helpful_votes) < ' . $negative_vote_comment . ' ) AS total_comments'); } else { $query->select('(SELECT COUNT(*) FROM #__judirectory_comments AS cm WHERE cm.listing_id = listing.id AND cm.approved = 1 AND cm.published = 1 AND cm.level = 1) AS total_comments'); } } } $subscriptionsField = new JUDirectoryFieldCore_subscriptions(); if ($subscriptionsField->canView(array("view" => "list"))) { $query->select('(SELECT COUNT(*) FROM #__judirectory_subscriptions AS sub WHERE sub.item_id = listing.id AND sub.type = "listing" AND sub.published = 1) AS total_subscriptions'); } $reportsField = new JUDirectoryFieldCore_reports(); if ($reportsField->canView(array("view" => "list"))) { $query->select('(SELECT COUNT(*) FROM #__judirectory_reports AS r WHERE r.item_id = listing.id AND r.type = "listing") AS total_reports'); } $categoriesField = new JUDirectoryFieldCore_categories(); if ($categoriesField->canView(array("view" => "list"))) { $query->select('(SELECT GROUP_CONCAT(catids.id ORDER BY listingx_catids.main DESC, listingx_catids.ordering ASC SEPARATOR ",") FROM (#__judirectory_categories AS catids JOIN #__judirectory_listings_xref AS listingx_catids ON catids.id = listingx_catids.cat_id) WHERE listing.id = listingx_catids.listing_id GROUP BY listing.id) AS cat_ids'); $query->select('(SELECT GROUP_CONCAT(cattitles.title ORDER BY listingx_cattitles.main DESC, listingx_cattitles.ordering ASC SEPARATOR "|||") FROM (#__judirectory_categories AS cattitles JOIN #__judirectory_listings_xref AS listingx_cattitles ON cattitles.id = listingx_cattitles.cat_id) WHERE listing.id = listingx_cattitles.listing_id GROUP BY listing.id) AS cat_titles'); } $tagsField = new JUDirectoryFieldCore_tags(); if ($tagsField->canView(array("view" => "list"))) { $query->select('IFNULL ((SELECT GROUP_CONCAT(tagids.id ORDER BY tx_tagids.ordering ASC SEPARATOR ",") FROM (#__judirectory_tags AS tagids JOIN #__judirectory_tags_xref AS tx_tagids ON tagids.id = tx_tagids.tag_id) WHERE listing.id = tx_tagids.listing_id GROUP BY listing.id), "") AS tag_ids'); $query->select('IFNULL ((SELECT GROUP_CONCAT(tagtitles.title ORDER BY tx_tagtitles.ordering ASC SEPARATOR "|||") FROM (#__judirectory_tags AS tagtitles JOIN #__judirectory_tags_xref AS tx_tagtitles ON tagtitles.id = tx_tagtitles.tag_id) WHERE listing.id = tx_tagtitles.listing_id GROUP BY listing.id), "") AS tag_titles'); } $app = JFactory::getApplication(); $accessLevel = implode(',', $user->getAuthorisedViewLevels()); $db = JFactory::getDbo(); $date = JFactory::getDate(); $nullDate = $db->quote($db->getNullDate()); $nowDate = $db->quote($date->toSql()); $fieldQuery = $db->getQuery(true); $fieldQuery->select('field.id'); $fieldQuery->from('#__judirectory_fields AS field'); $fieldQuery->where('field.field_name = ""'); $fieldQuery->where('field.list_view = 1'); $fieldQuery->where('field.published = 1'); $fieldQuery->where('field.publish_up <= ' . $nowDate); $fieldQuery->where('(field.publish_down = ' . $nullDate . ' OR field.publish_down > ' . $nowDate . ')'); $fieldQuery->where('field.access IN (' . $accessLevel . ')'); $view = $app->input->get('view', ''); if ($view == 'category' || $view == 'tree') { $cat_id = $app->input->getInt('id', 0); $category = JUDirectoryHelper::getCategoryById($cat_id); if (is_object($category)) { $fieldQuery->where('field.group_id = ' . $category->fieldgroup_id); } } else { $fieldQuery->join('', '#__judirectory_categories AS c ON (field.group_id = c.fieldgroup_id OR field.group_id = 1)'); if (is_array($categoryIdArrayCanAccess) && count($categoryIdArrayCanAccess) > 0) { $fieldQuery->where('c.id IN(' . implode(",", $categoryIdArrayCanAccess) . ')'); } else { $fieldQuery->where('c.id IN("")'); } } $fieldQuery->join('', '#__judirectory_fields_groups AS field_group ON field.group_id = field_group.id'); $fieldQuery->where('field_group.published = 1'); $fieldQuery->where('field_group.access IN (' . $accessLevel . ')'); $fieldQuery->group('field.id'); $db->setQuery($fieldQuery); $fields = $db->loadObjectList(); foreach ($fields as $field) { $query->select('IFNULL (fields_values_' . $field->id . '.value, "") AS field_values_' . $field->id); $query->join('LEFT', '#__judirectory_fields_values AS fields_values_' . $field->id . ' ON fields_values_' . $field->id . '.listing_id = listing.id AND fields_values_' . $field->id . '.field_id = ' . $field->id); } } }
public function save($key = null, $urlVar = null) { JSession::checkToken() or die(JText::_('JINVALID_TOKEN')); $app = JFactory::getApplication(); $lang = JFactory::getLanguage(); $model = $this->getModel(); $table = $model->getTable(); $checkin = property_exists($table, 'checked_out'); $context = "{$this->option}.edit.{$this->context}"; $task = $this->getTask(); $data = $app->input->post->get('jform', array(), 'array'); $fieldsData = $app->input->post->get('fields', array(), 'array'); $related_listings = array_values($app->input->post->get("related_listings", array(), 'array')); if (empty($key)) { $key = $table->getKeyName(); } if (empty($urlVar)) { $urlVar = $key; } $recordId = $app->input->getInt($urlVar, 0); if (!$this->checkEditId($context, $recordId)) { $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $recordId)); $this->setMessage($this->getError(), 'error'); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false)); return false; } $data[$key] = $recordId; if ($task == 'save2copy') { if ($checkin && $model->checkin($data[$key]) === false) { $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); $this->setMessage($this->getError(), 'error'); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false)); return false; } $listingArr = array($data[$key]); $currentListingObject = JUDirectoryHelper::getListingById($data[$key]); $catArr = array($currentListingObject->cat_id); $copyOptionsArr = array('copy_rates', 'copy_hits', 'copy_permission', 'copy_extra_fields', 'copy_related_listings', 'copy_comments', 'copy_reports', 'copy_subscriptions', 'copy_logs'); $listingCopyMappedId = $model->copyAndMap($listingArr, $catArr, $copyOptionsArr, 'save2copy', $fieldsData); $data[$key] = $listingCopyMappedId; $save2copy = true; $task = 'apply'; } if (!$this->allowSave($data, $key)) { $this->setError(JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED')); $this->setMessage($this->getError(), 'error'); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false)); return false; } $form = $model->getForm($data, false); if (!$form) { $app->enqueueMessage($model->getError(), 'error'); return false; } $validData = $model->validate($form, $data); $validFieldsData = $model->validateFields($fieldsData, $data[$key]); if ($validData === false || $validFieldsData === false) { $errors = $model->getErrors(); for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) { if ($errors[$i] instanceof Exception) { $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); } else { $app->enqueueMessage($errors[$i], 'warning'); } } $app->setUserState($context . '.data', $data); $app->setUserState($context . '.fieldsdata', $fieldsData); $app->setUserState($context . '.related_listings', $related_listings); if (isset($save2copy) && $save2copy) { $model->delete($data[$key]); } $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false)); return false; } $data['data'] = $validData; $data['data'][$key] = $data[$key]; $data['fieldsData'] = $validFieldsData; $data['related_listings'] = $related_listings; $categoriesField = new JUDirectoryFieldCore_categories(); if ($model->getListingSubmitType($data['data'][$key]) == 'submit' && !$categoriesField->canSubmit() || $model->getListingSubmitType($data['data'][$key]) == 'edit' && !$categoriesField->canEdit()) { $listingObjectDb = JUDirectoryHelper::getListingById($data['data'][$key]); if ($listingObjectDb) { $data['fieldsData'][$categoriesField->id]['main'] = $listingObjectDb->cat_id; } else { $this->setError(JText::_('COM_JUDIRECTORY_INVALID_LISTING')); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false)); return false; } } if (!$model->save($data)) { $app->setUserState($context . '.data', $validData); $app->setUserState($context . '.fieldsdata', $validFieldsData); $app->setUserState($context . '.related_listings', $related_listings); $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError())); $this->setMessage($this->getError(), 'error'); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false)); return false; } if ($table->load($recordId)) { if ($table->id > 0) { if ($checkin && $model->checkin($recordId) === false) { $app->setUserState($context . '.data', $validData); $app->setUserState($context . '.fieldsdata', $validFieldsData); $app->setUserState($context . '.related_listings', $related_listings); $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); $this->setMessage($this->getError(), 'error'); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false)); return false; } } } $this->setMessage(JText::_(($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS') ? $this->text_prefix : 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')); switch ($task) { case 'apply': $recordId = $model->getState($this->context . '.id'); $this->holdEditId($context, $recordId); $app->setUserState($context . '.data', null); $app->setUserState($context . '.fieldsdata', null); $app->setUserState($context . '.related_listings', null); $model->checkout($recordId); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false)); break; case 'save2new': $this->releaseEditId($context, $recordId); $app->setUserState($context . '.data', null); $app->setUserState($context . '.fieldsdata', null); $app->setUserState($context . '.related_listings', null); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend(null, $urlVar), false)); break; default: $this->releaseEditId($context, $recordId); $app->setUserState($context . '.data', null); $app->setUserState($context . '.fieldsdata', null); $app->setUserState($context . '.related_listings', null); $this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false)); break; } $this->postSaveHook($model, $validData); return true; }
public function saveListingStoreCategoryField($isNew, $pk, $fieldsData, $newMainCatId) { if (!$isNew) { $categoriesField = new JUDirectoryFieldCore_categories(null, $pk); if ($this->getListingSubmitType($pk) == 'submit' && $categoriesField->canSubmit() || $this->getListingSubmitType($pk) == 'edit' && $categoriesField->canEdit()) { $categoriesField->is_new = $isNew; $categoriesFieldValue = $fieldsData[$categoriesField->id]; $categoriesFieldValue = $categoriesField->onSaveListing($categoriesFieldValue); $saveFieldCategory = $categoriesField->storeValue($categoriesFieldValue); if ($saveFieldCategory) { $listingObject = JUDirectoryHelper::getListingById($pk); $mainCatIdDB = $listingObject->cat_id; if ($mainCatIdDB != $newMainCatId) { $fieldGroupIdDB = JUDirectoryHelper::getCategoryById($mainCatIdDB)->fieldgroup_id; $fieldGroupId = JUDirectoryHelper::getCategoryById($newMainCatId)->fieldgroup_id; if ($fieldGroupId != $fieldGroupIdDB) { JUDirectoryHelper::deleteFieldValuesOfListing($pk); } } } else { $this->setError('COM_JUDIRECTORY_FAIL_TO_SAVE_CATEGORY_FIELD'); return false; } } } return true; }