400 lines
10 KiB
PHP
400 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* @package Joomla.Component.Builder
|
|
*
|
|
* @created 4th September 2022
|
|
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
|
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
|
* @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 JCB template file (EVER)
|
|
defined('_JCB_TEMPLATE') or die;
|
|
?>
|
|
###BOM###
|
|
namespace ###NAMESPACEPREFIX###\Component\###ComponentNamespace###\Administrator\Model;
|
|
|
|
###ADMIN_VIEW_MODEL_HEADER###
|
|
|
|
// No direct access to this file
|
|
\defined('_JEXEC') or die;
|
|
|
|
/**
|
|
* ###Component### ###View### Admin Model
|
|
*
|
|
* @since 1.6
|
|
*/
|
|
class ###View###Model extends AdminModel
|
|
{
|
|
use VersionableModelTrait;
|
|
|
|
/**
|
|
* The tab layout fields array.
|
|
*
|
|
* @var array
|
|
* @since 3.0.0
|
|
*/
|
|
protected $tabLayoutFields = ###TABLAYOUTFIELDSARRAY###;
|
|
|
|
/**
|
|
* The styles array.
|
|
*
|
|
* @var array
|
|
* @since 4.3
|
|
*/
|
|
protected array $styles = [
|
|
'administrator/components/com_###component###/assets/css/admin.css',
|
|
'administrator/components/com_###component###/assets/css/###view###.css'
|
|
];
|
|
|
|
/**
|
|
* The scripts array.
|
|
*
|
|
* @var array
|
|
* @since 4.3
|
|
*/
|
|
protected array $scripts = [
|
|
'administrator/components/com_###component###/assets/js/admin.js',
|
|
'media/com_###component###/js/###view###.js'
|
|
];
|
|
|
|
/**
|
|
* @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.
|
|
*
|
|
* @return Table A database object
|
|
* @since 3.0
|
|
* @throws \Exception
|
|
*/
|
|
public function getTable($type = '###view###', $prefix = 'Administrator', $config = [])
|
|
{
|
|
// get instance of the table
|
|
return parent::getTable($type, $prefix, $config);
|
|
}###ADMIN_CUSTOM_BUTTONS_METHOD###
|
|
|
|
/**
|
|
* 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 (property_exists($item, 'metadata') && !is_array($item->metadata))
|
|
{
|
|
// Convert the metadata field to an array.
|
|
$metadata = new Joomla___a87c432d_b5b4_428e_b7ff_14b51664c624___Power($item->metadata);
|
|
$item->metadata = $metadata->toArray();
|
|
}
|
|
|
|
// check edit access permissions
|
|
if (!empty($item->id) && !$this->allowEdit((array) $item))
|
|
{
|
|
$app = Joomla___39403062_84fb_46e0_bac4_0023f766e827___Power::getApplication();
|
|
$app->enqueueMessage(Joomla___ba6326ef_cb79_4348_80f4_ab086082e3c5___Power::_('Not authorised!'), 'error');
|
|
$app->redirect('index.php?option=com_###component###');
|
|
return false;
|
|
}###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 Form|boolean A Form object on success, false on failure
|
|
* @since 1.6
|
|
*/
|
|
public function getForm($data = [], $loadData = true, $options = ['control' => 'jform'])
|
|
{
|
|
// set load data option
|
|
$options['load_data'] = $loadData;###JMODELADMIN_GETFORM###
|
|
}
|
|
|
|
/**
|
|
* Method to get the styles that have to be included on the view
|
|
*
|
|
* @return array styles files
|
|
* @since 4.3
|
|
*/
|
|
public function getStyles(): array
|
|
{
|
|
return $this->styles;
|
|
}
|
|
|
|
/**
|
|
* Method to set the styles that have to be included on the view
|
|
*
|
|
* @return void
|
|
* @since 4.3
|
|
*/
|
|
public function setStyles(string $path): void
|
|
{
|
|
$this->styles[] = $path;
|
|
}
|
|
|
|
/**
|
|
* Method to get the script that have to be included on the view
|
|
*
|
|
* @return array script files
|
|
* @since 4.3
|
|
*/
|
|
public function getScripts(): array
|
|
{
|
|
return $this->scripts;
|
|
}
|
|
|
|
/**
|
|
* Method to set the script that have to be included on the view
|
|
*
|
|
* @return void
|
|
* @since 4.3
|
|
*/
|
|
public function setScript(string $path): void
|
|
{
|
|
$this->scripts[] = $path;
|
|
}
|
|
|
|
/**
|
|
* 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###
|
|
}
|
|
|
|
/**
|
|
* Method to check if you can edit an existing record.
|
|
* We know this is a double access check (Controller already does an allowEdit check)
|
|
* But when the item is directly accessed the controller is skipped (2025_).
|
|
*
|
|
* @param array $data An array of input data.
|
|
* @param string $key The name of the key for the primary key.
|
|
*
|
|
* @return boolean True if allowed to edit the record. Defaults to the permission set in the component.
|
|
* @since 2.5
|
|
*/
|
|
protected function allowEdit(array $data = [], string $key = 'id'): bool
|
|
{###JMODELADMIN_ALLOWEDIT###
|
|
}
|
|
|
|
/**
|
|
* Prepare and sanitise the table data prior to saving.
|
|
*
|
|
* @param Table $table A Table object.
|
|
*
|
|
* @return void
|
|
* @since 1.6
|
|
*/
|
|
protected function prepareTable($table)
|
|
{###LICENSE_TABLE_LOCKED_CHECK###
|
|
$date = Joomla___39403062_84fb_46e0_bac4_0023f766e827___Power::getDate();
|
|
$user = $this->getCurrentUser();
|
|
|
|
if (isset($table->name))
|
|
{
|
|
$table->name = \htmlspecialchars_decode($table->name, ENT_QUOTES);
|
|
}
|
|
|
|
if (isset($table->alias) && empty($table->alias))
|
|
{
|
|
$table->generateAlias();
|
|
}
|
|
|
|
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))
|
|
{
|
|
$db = $this->getDatabase();
|
|
$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;
|
|
}
|
|
|
|
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
|
|
*/
|
|
protected function loadFormData()
|
|
{
|
|
// Check the session for previously entered form data.
|
|
$data = Joomla___39403062_84fb_46e0_bac4_0023f766e827___Power::getApplication()->getUserState('com_###component###.edit.###view###.data', []);
|
|
|
|
if (empty($data))
|
|
{
|
|
$data = $this->getItem();
|
|
}
|
|
|
|
// run the per process of the data
|
|
$this->preprocessData('com_###component###.###view###', $data);
|
|
|
|
return $data;
|
|
}###VALIDATIONFIX######UNIQUEFIELDS###
|
|
|
|
/**
|
|
* 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###
|
|
|
|
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###
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Method to save the form data.
|
|
*
|
|
* @param array $data The form data.
|
|
*
|
|
* @return boolean True on success.
|
|
* @since 1.6
|
|
*/
|
|
public function save($data)
|
|
{
|
|
$input = Joomla___39403062_84fb_46e0_bac4_0023f766e827___Power::getApplication()->getInput();
|
|
$filter = Joomla___193deb3e_0c3e_4610_8e55_450e463095b4___Power::getInstance();
|
|
|
|
// set the metadata to the Item Data
|
|
if (isset($data['metadata']) && isset($data['metadata']['author']))
|
|
{
|
|
$data['metadata']['author'] = $filter->clean($data['metadata']['author'], 'TRIM');
|
|
|
|
$metadata = new Joomla___a87c432d_b5b4_428e_b7ff_14b51664c624___Power;
|
|
$metadata->loadArray($data['metadata']);
|
|
$data['metadata'] = (string) $metadata;
|
|
}###CHECKBOX_SAVE######METHOD_ITEM_SAVE###
|
|
|
|
// Set the Params Items to data
|
|
if (isset($data['params']) && is_array($data['params']))
|
|
{
|
|
$params = new Joomla___a87c432d_b5b4_428e_b7ff_14b51664c624___Power;
|
|
$params->loadArray($data['params']);
|
|
$data['params'] = (string) $params;
|
|
}###TITLEALIASFIX###
|
|
|
|
if (parent::save($data))
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 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([$field => $value]))
|
|
{
|
|
$value = Joomla___46ba6c12_361e_4f7d_b6d9_70450e7cd7c2___Power::increment($value);
|
|
}
|
|
|
|
return $value;
|
|
}###GENERATENEWTITLE###
|
|
}
|