2021-03-05 03:08:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.Component.Builder
|
|
|
|
*
|
|
|
|
* @created 30th April, 2015
|
2024-03-02 20:10:30 +00:00
|
|
|
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
|
|
|
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
2021-03-05 03:08:47 +00:00
|
|
|
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
// No direct access to this file
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
?>
|
|
|
|
###BOM###
|
|
|
|
|
|
|
|
// No direct access to this file
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
|
|
|
###ADMIN_VIEW_MODEL_HEADER###
|
|
|
|
|
|
|
|
/**
|
2022-05-25 08:30:55 +00:00
|
|
|
* ###Component### ###View### Admin Model
|
2021-03-05 03:08:47 +00:00
|
|
|
*/
|
2022-05-25 08:30:55 +00:00
|
|
|
class ###Component###Model###View### extends AdminModel
|
2021-03-05 03:08:47 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The tab layout fields array.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $tabLayoutFields = ###TABLAYOUTFIELDSARRAY###;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string The prefix to use with controller messages.
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected $text_prefix = 'COM_###COMPONENT###';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type alias for this content type.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @since 3.2
|
|
|
|
*/
|
|
|
|
public $typeAlias = 'com_###component###.###view###';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a Table object, always creating it
|
|
|
|
*
|
|
|
|
* @param type $type The table type to instantiate
|
|
|
|
* @param string $prefix A prefix for the table class name. Optional.
|
|
|
|
* @param array $config Configuration array for model. Optional.
|
|
|
|
*
|
2024-03-02 20:10:30 +00:00
|
|
|
* @return Table A database object
|
2021-03-05 03:08:47 +00:00
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
2024-03-02 20:10:30 +00:00
|
|
|
public function getTable($type = '###view###', $prefix = '###Component###Table', $config = [])
|
2021-03-05 03:08:47 +00:00
|
|
|
{
|
|
|
|
// add table path for when model gets used from other component
|
|
|
|
$this->addTablePath(JPATH_ADMINISTRATOR . '/components/com_###component###/tables');
|
|
|
|
// get instance of the table
|
2024-03-02 20:10:30 +00:00
|
|
|
return Table::getInstance($type, $prefix, $config);
|
2021-03-05 03:08:47 +00:00
|
|
|
}###ADMIN_CUSTOM_BUTTONS_METHOD###
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Method to get a single record.
|
|
|
|
*
|
|
|
|
* @param integer $pk The id of the primary key.
|
|
|
|
*
|
|
|
|
* @return mixed Object on success, false on failure.
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
public function getItem($pk = null)
|
|
|
|
{###LICENSE_LOCKED_CHECK###
|
|
|
|
if ($item = parent::getItem($pk))
|
|
|
|
{
|
|
|
|
if (!empty($item->params) && !is_array($item->params))
|
|
|
|
{
|
|
|
|
// Convert the params field to an array.
|
|
|
|
$registry = new Registry;
|
|
|
|
$registry->loadString($item->params);
|
|
|
|
$item->params = $registry->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($item->metadata))
|
|
|
|
{
|
|
|
|
// Convert the metadata field to an array.
|
|
|
|
$registry = new Registry;
|
|
|
|
$registry->loadString($item->metadata);
|
|
|
|
$item->metadata = $registry->toArray();
|
|
|
|
}###METHOD_GET_ITEM###
|
|
|
|
}###LINKEDVIEWGLOBAL###
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
}###LINKEDVIEWMETHODS######LICENSE_LOCKED_SET_BOOL###
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get the record form.
|
|
|
|
*
|
|
|
|
* @param array $data Data for the form.
|
|
|
|
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
|
|
|
|
* @param array $options Optional array of options for the form creation.
|
|
|
|
*
|
|
|
|
* @return mixed A JForm object on success, false on failure
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
2024-03-02 20:10:30 +00:00
|
|
|
public function getForm($data = [], $loadData = true, $options = array('control' => 'jform'))
|
2021-03-05 03:08:47 +00:00
|
|
|
{
|
|
|
|
// set load data option
|
|
|
|
$options['load_data'] = $loadData;###JMODELADMIN_GETFORM###
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get the script that have to be included on the form
|
|
|
|
*
|
2024-03-02 20:10:30 +00:00
|
|
|
* @return string script files
|
2021-03-05 03:08:47 +00:00
|
|
|
*/
|
|
|
|
public function getScript()
|
|
|
|
{
|
2021-03-16 23:14:54 +00:00
|
|
|
return 'media/com_###component###/js/###view###.js';
|
2021-03-05 03:08:47 +00:00
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Method to test whether a record can be deleted.
|
|
|
|
*
|
|
|
|
* @param object $record A record object.
|
|
|
|
*
|
|
|
|
* @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function canDelete($record)
|
|
|
|
{###JMODELADMIN_CANDELETE###
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to test whether a record can have its state edited.
|
|
|
|
*
|
|
|
|
* @param object $record A record object.
|
|
|
|
*
|
|
|
|
* @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function canEditState($record)
|
|
|
|
{###JMODELADMIN_CANEDITSTATE###
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Method override to check if you can edit an existing record.
|
|
|
|
*
|
2024-03-02 20:10:30 +00:00
|
|
|
* @param array $data An array of input data.
|
|
|
|
* @param string $key The name of the key for the primary key.
|
2021-03-05 03:08:47 +00:00
|
|
|
*
|
2024-03-02 20:10:30 +00:00
|
|
|
* @return boolean
|
|
|
|
* @since 2.5
|
2021-03-05 03:08:47 +00:00
|
|
|
*/
|
2024-03-02 20:10:30 +00:00
|
|
|
protected function allowEdit($data = [], $key = 'id')
|
2021-03-05 03:08:47 +00:00
|
|
|
{###JMODELADMIN_ALLOWEDIT###
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Prepare and sanitise the table data prior to saving.
|
|
|
|
*
|
2024-03-02 20:10:30 +00:00
|
|
|
* @param Table $table A Table object.
|
2021-03-05 03:08:47 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
protected function prepareTable($table)
|
|
|
|
{###LICENSE_TABLE_LOCKED_CHECK###
|
2024-03-02 20:10:30 +00:00
|
|
|
$date = Factory::getDate();
|
|
|
|
$user = Factory::getUser();
|
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
if (isset($table->name))
|
|
|
|
{
|
|
|
|
$table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
if (isset($table->alias) && empty($table->alias))
|
|
|
|
{
|
|
|
|
$table->generateAlias();
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
if (empty($table->id))
|
|
|
|
{
|
|
|
|
$table->created = $date->toSql();
|
|
|
|
// set the user
|
|
|
|
if ($table->created_by == 0 || empty($table->created_by))
|
|
|
|
{
|
|
|
|
$table->created_by = $user->id;
|
|
|
|
}
|
|
|
|
// Set ordering to the last item if not set
|
|
|
|
if (empty($table->ordering))
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$db = Factory::getDbo();
|
2021-03-05 03:08:47 +00:00
|
|
|
$query = $db->getQuery(true)
|
|
|
|
->select('MAX(ordering)')
|
|
|
|
->from($db->quoteName('#__###component###_###view###'));
|
|
|
|
$db->setQuery($query);
|
|
|
|
$max = $db->loadResult();
|
|
|
|
|
|
|
|
$table->ordering = $max + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$table->modified = $date->toSql();
|
|
|
|
$table->modified_by = $user->id;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
if (!empty($table->id))
|
|
|
|
{
|
|
|
|
// Increment the items version number.
|
|
|
|
$table->version++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to get the data that should be injected in the form.
|
|
|
|
*
|
|
|
|
* @return mixed The data for the form.
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
2024-03-02 20:10:30 +00:00
|
|
|
protected function loadFormData()
|
2021-03-05 03:08:47 +00:00
|
|
|
{
|
|
|
|
// Check the session for previously entered form data.
|
2024-03-02 20:10:30 +00:00
|
|
|
$data = Factory::getApplication()->getUserState('com_###component###.edit.###view###.data', []);
|
2021-03-05 03:08:47 +00:00
|
|
|
|
|
|
|
if (empty($data))
|
|
|
|
{
|
|
|
|
$data = $this->getItem();
|
|
|
|
// run the perprocess of the data
|
|
|
|
$this->preprocessData('com_###component###.###view###', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}###VALIDATIONFIX######UNIQUEFIELDS###
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Method to delete one or more records.
|
|
|
|
*
|
|
|
|
* @param array &$pks An array of record primary keys.
|
|
|
|
*
|
|
|
|
* @return boolean True if successful, false if an error occurs.
|
|
|
|
*
|
|
|
|
* @since 12.2
|
|
|
|
*/
|
|
|
|
public function delete(&$pks)
|
|
|
|
{###JMODELADMIN_BEFORE_DELETE###
|
|
|
|
if (!parent::delete($pks))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}###JMODELADMIN_AFTER_DELETE###
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to change the published state of one or more records.
|
|
|
|
*
|
|
|
|
* @param array &$pks A list of the primary keys to change.
|
|
|
|
* @param integer $value The value of the published state.
|
|
|
|
*
|
|
|
|
* @return boolean True on success.
|
|
|
|
*
|
|
|
|
* @since 12.2
|
|
|
|
*/
|
|
|
|
public function publish(&$pks, $value = 1)
|
|
|
|
{###JMODELADMIN_BEFORE_PUBLISH###
|
|
|
|
if (!parent::publish($pks, $value))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}###JMODELADMIN_AFTER_PUBLISH###
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
return true;
|
2024-03-02 20:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Method to perform batch operations on an item or a set of items.
|
|
|
|
*
|
|
|
|
* @param array $commands An array of commands to perform.
|
|
|
|
* @param array $pks An array of item ids.
|
|
|
|
* @param array $contexts An array of item contexts.
|
|
|
|
*
|
|
|
|
* @return boolean Returns true on success, false on failure.
|
|
|
|
*
|
|
|
|
* @since 12.2
|
|
|
|
*/
|
|
|
|
public function batch($commands, $pks, $contexts)
|
|
|
|
{
|
|
|
|
// Sanitize ids.
|
|
|
|
$pks = array_unique($pks);
|
|
|
|
ArrayHelper::toInteger($pks);
|
|
|
|
|
|
|
|
// Remove any values of zero.
|
|
|
|
if (array_search(0, $pks, true))
|
|
|
|
{
|
|
|
|
unset($pks[array_search(0, $pks, true)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($pks))
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$this->setError(Text::_('JGLOBAL_NO_ITEM_SELECTED'));
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$done = false;
|
|
|
|
|
|
|
|
// Set some needed variables.
|
2024-03-02 20:10:30 +00:00
|
|
|
$this->user = Factory::getUser();
|
|
|
|
$this->table = $this->getTable();
|
|
|
|
$this->tableClassName = get_class($this->table);
|
|
|
|
$this->contentType = new UCMType;
|
|
|
|
$this->type = $this->contentType->getTypeByTable($this->tableClassName);
|
|
|
|
$this->canDo = ###Component###Helper::getActions('###view###');
|
|
|
|
$this->batchSet = true;
|
2021-03-05 03:08:47 +00:00
|
|
|
|
|
|
|
if (!$this->canDo->get('core.batch'))
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$this->setError(Text::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
if ($this->type == false)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$type = new UCMType;
|
2021-03-05 03:08:47 +00:00
|
|
|
$this->type = $type->getTypeByAlias($this->typeAlias);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags');
|
|
|
|
|
|
|
|
if (!empty($commands['move_copy']))
|
|
|
|
{
|
|
|
|
$cmd = ArrayHelper::getValue($commands, 'move_copy', 'c');
|
|
|
|
|
|
|
|
if ($cmd == 'c')
|
|
|
|
{
|
|
|
|
$result = $this->batchCopy($commands, $pks, $contexts);
|
|
|
|
|
|
|
|
if (is_array($result))
|
|
|
|
{
|
|
|
|
foreach ($result as $old => $new)
|
|
|
|
{
|
|
|
|
$contexts[$new] = $contexts[$old];
|
|
|
|
}
|
|
|
|
$pks = array_values($result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif ($cmd == 'm' && !$this->batchMove($commands, $pks, $contexts))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$done)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$this->setError(Text::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
|
2021-03-05 03:08:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the cache
|
|
|
|
$this->cleanCache();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}###MODEL_BATCH_COPY######MODEL_BATCH_MOVE###
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Method to save the form data.
|
|
|
|
*
|
|
|
|
* @param array $data The form data.
|
|
|
|
*
|
|
|
|
* @return boolean True on success.
|
|
|
|
*
|
|
|
|
* @since 1.6
|
|
|
|
*/
|
|
|
|
public function save($data)
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$input = Factory::getApplication()->input;
|
|
|
|
$filter = InputFilter::getInstance();
|
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
// set the metadata to the Item Data
|
|
|
|
if (isset($data['metadata']) && isset($data['metadata']['author']))
|
|
|
|
{
|
|
|
|
$data['metadata']['author'] = $filter->clean($data['metadata']['author'], 'TRIM');
|
2024-03-02 20:10:30 +00:00
|
|
|
|
|
|
|
$metadata = new Registry;
|
2021-03-05 03:08:47 +00:00
|
|
|
$metadata->loadArray($data['metadata']);
|
|
|
|
$data['metadata'] = (string) $metadata;
|
|
|
|
}###CHECKBOX_SAVE######METHOD_ITEM_SAVE###
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
// Set the Params Items to data
|
|
|
|
if (isset($data['params']) && is_array($data['params']))
|
|
|
|
{
|
2024-03-02 20:10:30 +00:00
|
|
|
$params = new Registry;
|
2021-03-05 03:08:47 +00:00
|
|
|
$params->loadArray($data['params']);
|
|
|
|
$data['params'] = (string) $params;
|
|
|
|
}###TITLEALIASFIX###
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
if (parent::save($data))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-02 20:10:30 +00:00
|
|
|
|
2021-03-05 03:08:47 +00:00
|
|
|
/**
|
|
|
|
* Method to generate a unique value.
|
|
|
|
*
|
|
|
|
* @param string $field name.
|
|
|
|
* @param string $value data.
|
|
|
|
*
|
|
|
|
* @return string New value.
|
|
|
|
*
|
|
|
|
* @since 3.0
|
|
|
|
*/
|
|
|
|
protected function generateUnique($field,$value)
|
|
|
|
{
|
|
|
|
// set field value unique
|
|
|
|
$table = $this->getTable();
|
|
|
|
|
|
|
|
while ($table->load(array($field => $value)))
|
|
|
|
{
|
|
|
|
$value = StringHelper::increment($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}###GENERATENEWTITLE###
|
|
|
|
}
|