Current File : /home/pacjaorg/public_html/km/administrator/components/com_djclassifieds/model.php |
<?php
/**
* @package DJ-Classifieds
* @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
* @license http://www.gnu.org/licenses GNU/GPL
* @author url: http://dj-extensions.com
* @author email contact@dj-extensions.com
*/
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Database\ParameterType;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
defined('_JEXEC') or die('Restricted access');
abstract class DJClassifiedsAdminModel extends JModelAdmin
{
public function getItem($pk = null)
{
$pk = (!empty($pk)) ? $pk : (int) $this->getState($this->getName() . '.id');
$table = $this->getTable();
if ($pk) { // negative IDs support
// Attempt to load the row.
$return = $table->load($pk);
// Check for a table object error.
if ($return === false) {
// If there was no underlying error, then the false means there simply was not a row in the db for this $pk.
if (!$table->getError()) {
$this->setError(Text::_('JLIB_APPLICATION_ERROR_NOT_EXIST'));
} else {
$this->setError($table->getError());
}
return false;
}
}
// Convert to the CMSObject before adding other data.
$properties = $table->getProperties(1);
$item = ArrayHelper::toObject($properties, CMSObject::class);
if (property_exists($item, 'params')) {
$registry = new Registry($item->params);
$item->params = $registry->toArray();
}
return $item;
}
public function save($data)
{
$table = $this->getTable();
$context = $this->option . '.' . $this->name;
$app = Factory::getApplication();
if (\array_key_exists('tags', $data) && \is_array($data['tags'])) {
$table->newTags = $data['tags'];
}
$key = $table->getKeyName();
$pk = (isset($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
$isNew = true;
// Include the plugins for the save events.
PluginHelper::importPlugin($this->events_map['save']);
// Allow an exception to be thrown.
try {
// Load the row if saving an existing record.
if ($pk) { // negative IDs support
$table->load($pk);
$isNew = false;
}
// Bind the data.
if (!$table->bind($data)) {
$this->setError($table->getError());
return false;
}
// Prepare the row for saving
$this->prepareTable($table);
// Check the data.
if (!$table->check()) {
$this->setError($table->getError());
return false;
}
// Trigger the before save event.
$result = $app->triggerEvent($this->event_before_save, [$context, $table, $isNew, $data]);
if (\in_array(false, $result, true)) {
$this->setError($table->getError());
return false;
}
// Store the data.
if (!$table->store()) {
$this->setError($table->getError());
return false;
}
// Clean the cache.
$this->cleanCache();
// Trigger the after save event.
$app->triggerEvent($this->event_after_save, [$context, $table, $isNew, $data]);
} catch (\Exception $e) {
$this->setError($e->getMessage());
return false;
}
if (isset($table->$key)) {
$this->setState($this->getName() . '.id', $table->$key);
}
$this->setState($this->getName() . '.new', $isNew);
if ($this->associationsContext && Associations::isEnabled() && !empty($data['associations'])) {
$associations = $data['associations'];
// Unset any invalid associations
$associations = ArrayHelper::toInteger($associations);
// Unset any invalid associations
foreach ($associations as $tag => $id) {
if (!$id) {
unset($associations[$tag]);
}
}
// Show a warning if the item isn't assigned to a language but we have associations.
if ($associations && $table->language === '*') {
$app->enqueueMessage(
Text::_(strtoupper($this->option) . '_ERROR_ALL_LANGUAGE_ASSOCIATED'),
'warning'
);
}
// Get associationskey for edited item
$db = $this->getDbo();
$id = (int) $table->$key;
$query = $db->getQuery(true)
->select($db->quoteName('key'))
->from($db->quoteName('#__associations'))
->where($db->quoteName('context') . ' = :context')
->where($db->quoteName('id') . ' = :id')
->bind(':context', $this->associationsContext)
->bind(':id', $id, ParameterType::INTEGER);
$db->setQuery($query);
$oldKey = $db->loadResult();
if ($associations || $oldKey !== null) {
// Deleting old associations for the associated items
$query = $db->getQuery(true)
->delete($db->quoteName('#__associations'))
->where($db->quoteName('context') . ' = :context')
->bind(':context', $this->associationsContext);
$where = [];
if ($associations) {
$where[] = $db->quoteName('id') . ' IN (' . implode(',', $query->bindArray(array_values($associations))) . ')';
}
if ($oldKey !== null) {
$where[] = $db->quoteName('key') . ' = :oldKey';
$query->bind(':oldKey', $oldKey);
}
$query->extendWhere('AND', $where, 'OR');
$db->setQuery($query);
$db->execute();
}
// Adding self to the association
if ($table->language !== '*') {
$associations[$table->language] = (int) $table->$key;
}
if (\count($associations) > 1) {
// Adding new association for these items
$key = md5(json_encode($associations));
$query = $db->getQuery(true)
->insert($db->quoteName('#__associations'))
->columns(
[
$db->quoteName('id'),
$db->quoteName('context'),
$db->quoteName('key'),
]
);
foreach ($associations as $id) {
$query->values(
implode(
',',
$query->bindArray(
[$id, $this->associationsContext, $key],
[ParameterType::INTEGER, ParameterType::STRING, ParameterType::STRING]
)
)
);
}
$db->setQuery($query);
$db->execute();
}
}
if ($app->input->get('task') == 'editAssociations') {
return $this->redirectToAssociations($data);
}
return true;
}
}