public function save($key = null, $urlVar = null) { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $app = JFactory::getApplication(); $model = $this->getModel(); $table = $model->getTable(); $data = $app->input->post->get('jform', array(), 'array'); $context = "{$this->option}.edit.{$this->context}"; $task = $this->getTask(); $checkin = property_exists($table, 'checked_out'); // Determine the name of the primary key for the data. if (empty($key)) { $key = $table->getKeyName(); } // To avoid data collisions the urlVar may be different from the primary key. if (empty($urlVar)) { $urlVar = $key; } $recordId = $app->input->getInt($urlVar); // Populate the row id from the session. $data[$key] = $recordId; // Access check. if (!$this->allowSave($data, $key)) { $this->setError(JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED')); return false; } // Validate the posted data. // Sometimes the form needs some posted data, such as for plugins and modules. $form = $model->getForm($data, true); if (!$form) { $this->setError($model->getError()); return false; } // Test whether the data is valid. $validData = $model->validate($form, $data); // Check for validation errors. if ($validData === false) { // Get the validation messages. $errors = $model->getErrors(); $messages = array(); // Push up to three validation messages out to the user. for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) { if ($errors[$i] instanceof Exception) { $this->setError($errors[$i]->getMessage()); } else { $this->setError($errors[$i]); } } // Save the data in the session. $app->setUserState($context . '.data', $data); return false; } // Attempt to save the data. if (!$model->save($validData)) { // Save the data in the session. $app->setUserState($context . '.data', $validData); // Redirect back to the edit screen. $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError())); return false; } // Save succeeded, so check-in the record. if ($checkin && $model->checkin($validData[$key]) === false) { // Save the data in the session. $app->setUserState($context . '.data', $validData); // Check-in failed, so go back to the record and display a notice. $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); return false; } $this->setMessage(JText::_('COM_DJREVIEWS_REVIEW_SUCCESSFULLY_ADDED')); $this->releaseEditId($context, $recordId); $app->setUserState($context . '.data', null); // Invoke the postSave method to allow for the child class to access the model. $this->postSaveHook($model, $validData); require_once JPath::clean(JPATH_ROOT . '/components/com_djreviews/lib/api.php'); DJReviewsAPI::recalculate(); return true; }
public function delete(&$cid) { if (count($cid)) { // mark other reviews to be recalculated first $query = ' UPDATE #__djrevs_reviews AS r ' . ' INNER JOIN (SELECT DISTINCT object_id FROM #__djrevs_reviews WHERE id IN (' . implode(',', $cid) . ')) as r2 ON r2.object_id = r.object_id' . ' SET r.recalculate = 2 '; $this->_db->setQuery($query); $this->_db->query(); $this->_db->setQuery('DELETE FROM #__djrevs_reviews_items WHERE review_id IN (' . implode(',', $cid) . ')'); if ($this->_db->query() == false) { $this->setError($this->_db->getErrorMsg()); return false; } } require_once JPath::clean(JPATH_ROOT . '/components/com_djreviews/lib/api.php'); DJReviewsAPI::recalculate(); $deleted = parent::delete($cid); return $deleted; }