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; }