[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Administrator 5 * @subpackage com_banners 6 * 7 * @copyright (C) 2008 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Component\Banners\Administrator\Model; 12 13 use Joomla\CMS\Factory; 14 use Joomla\CMS\Form\Form; 15 use Joomla\CMS\Language\Text; 16 use Joomla\CMS\MVC\Model\AdminModel; 17 use Joomla\CMS\Table\Table; 18 use Joomla\CMS\Versioning\VersionableModelTrait; 19 use Joomla\Component\Categories\Administrator\Helper\CategoriesHelper; 20 21 // phpcs:disable PSR1.Files.SideEffects 22 \defined('_JEXEC') or die; 23 // phpcs:enable PSR1.Files.SideEffects 24 25 /** 26 * Banner model. 27 * 28 * @since 1.6 29 */ 30 class BannerModel extends AdminModel 31 { 32 use VersionableModelTrait; 33 34 /** 35 * The prefix to use with controller messages. 36 * 37 * @var string 38 * @since 1.6 39 */ 40 protected $text_prefix = 'COM_BANNERS_BANNER'; 41 42 /** 43 * The type alias for this content type. 44 * 45 * @var string 46 * @since 3.2 47 */ 48 public $typeAlias = 'com_banners.banner'; 49 50 /** 51 * Batch copy/move command. If set to false, the batch copy/move command is not supported 52 * 53 * @var string 54 */ 55 protected $batch_copymove = 'category_id'; 56 57 /** 58 * Allowed batch commands 59 * 60 * @var array 61 */ 62 protected $batch_commands = array( 63 'client_id' => 'batchClient', 64 'language_id' => 'batchLanguage' 65 ); 66 67 /** 68 * Batch client changes for a group of banners. 69 * 70 * @param string $value The new value matching a client. 71 * @param array $pks An array of row IDs. 72 * @param array $contexts An array of item contexts. 73 * 74 * @return boolean True if successful, false otherwise and internal error is set. 75 * 76 * @since 2.5 77 */ 78 protected function batchClient($value, $pks, $contexts) 79 { 80 // Set the variables 81 $user = Factory::getUser(); 82 83 /** @var \Joomla\Component\Banners\Administrator\Table\BannerTable $table */ 84 $table = $this->getTable(); 85 86 foreach ($pks as $pk) { 87 if (!$user->authorise('core.edit', $contexts[$pk])) { 88 $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); 89 90 return false; 91 } 92 93 $table->reset(); 94 $table->load($pk); 95 $table->cid = (int) $value; 96 97 if (!$table->store()) { 98 $this->setError($table->getError()); 99 100 return false; 101 } 102 } 103 104 // Clean the cache 105 $this->cleanCache(); 106 107 return true; 108 } 109 110 /** 111 * Method to test whether a record can be deleted. 112 * 113 * @param object $record A record object. 114 * 115 * @return boolean True if allowed to delete the record. Defaults to the permission set in the component. 116 * 117 * @since 1.6 118 */ 119 protected function canDelete($record) 120 { 121 if (empty($record->id) || $record->state != -2) { 122 return false; 123 } 124 125 if (!empty($record->catid)) { 126 return Factory::getUser()->authorise('core.delete', 'com_banners.category.' . (int) $record->catid); 127 } 128 129 return parent::canDelete($record); 130 } 131 132 /** 133 * A method to preprocess generating a new title in order to allow tables with alternative names 134 * for alias and title to use the batch move and copy methods 135 * 136 * @param integer $categoryId The target category id 137 * @param Table $table The JTable within which move or copy is taking place 138 * 139 * @return void 140 * 141 * @since 3.8.12 142 */ 143 public function generateTitle($categoryId, $table) 144 { 145 // Alter the title & alias 146 $data = $this->generateNewTitle($categoryId, $table->alias, $table->name); 147 $table->name = $data['0']; 148 $table->alias = $data['1']; 149 } 150 151 /** 152 * Method to test whether a record can have its state changed. 153 * 154 * @param object $record A record object. 155 * 156 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 157 * 158 * @since 1.6 159 */ 160 protected function canEditState($record) 161 { 162 // Check against the category. 163 if (!empty($record->catid)) { 164 return Factory::getUser()->authorise('core.edit.state', 'com_banners.category.' . (int) $record->catid); 165 } 166 167 // Default to component settings if category not known. 168 return parent::canEditState($record); 169 } 170 171 /** 172 * Method to get the record form. 173 * 174 * @param array $data Data for the form. [optional] 175 * @param boolean $loadData True if the form is to load its own data (default case), false if not. [optional] 176 * 177 * @return Form|boolean A Form object on success, false on failure 178 * 179 * @since 1.6 180 */ 181 public function getForm($data = array(), $loadData = true) 182 { 183 // Get the form. 184 $form = $this->loadForm('com_banners.banner', 'banner', array('control' => 'jform', 'load_data' => $loadData)); 185 186 if (empty($form)) { 187 return false; 188 } 189 190 // Modify the form based on access controls. 191 if (!$this->canEditState((object) $data)) { 192 // Disable fields for display. 193 $form->setFieldAttribute('ordering', 'disabled', 'true'); 194 $form->setFieldAttribute('publish_up', 'disabled', 'true'); 195 $form->setFieldAttribute('publish_down', 'disabled', 'true'); 196 $form->setFieldAttribute('state', 'disabled', 'true'); 197 $form->setFieldAttribute('sticky', 'disabled', 'true'); 198 199 // Disable fields while saving. 200 // The controller has already verified this is a record you can edit. 201 $form->setFieldAttribute('ordering', 'filter', 'unset'); 202 $form->setFieldAttribute('publish_up', 'filter', 'unset'); 203 $form->setFieldAttribute('publish_down', 'filter', 'unset'); 204 $form->setFieldAttribute('state', 'filter', 'unset'); 205 $form->setFieldAttribute('sticky', 'filter', 'unset'); 206 } 207 208 // Don't allow to change the created_by user if not allowed to access com_users. 209 if (!Factory::getUser()->authorise('core.manage', 'com_users')) { 210 $form->setFieldAttribute('created_by', 'filter', 'unset'); 211 } 212 213 return $form; 214 } 215 216 /** 217 * Method to get the data that should be injected in the form. 218 * 219 * @return mixed The data for the form. 220 * 221 * @since 1.6 222 */ 223 protected function loadFormData() 224 { 225 // Check the session for previously entered form data. 226 $app = Factory::getApplication(); 227 $data = $app->getUserState('com_banners.edit.banner.data', array()); 228 229 if (empty($data)) { 230 $data = $this->getItem(); 231 232 // Prime some default values. 233 if ($this->getState('banner.id') == 0) { 234 $filters = (array) $app->getUserState('com_banners.banners.filter'); 235 $filterCatId = $filters['category_id'] ?? null; 236 237 $data->set('catid', $app->input->getInt('catid', $filterCatId)); 238 } 239 } 240 241 $this->preprocessData('com_banners.banner', $data); 242 243 return $data; 244 } 245 246 /** 247 * Method to stick records. 248 * 249 * @param array $pks The ids of the items to publish. 250 * @param integer $value The value of the published state 251 * 252 * @return boolean True on success. 253 * 254 * @since 1.6 255 */ 256 public function stick(&$pks, $value = 1) 257 { 258 /** @var \Joomla\Component\Banners\Administrator\Table\BannerTable $table */ 259 $table = $this->getTable(); 260 $pks = (array) $pks; 261 262 // Access checks. 263 foreach ($pks as $i => $pk) { 264 if ($table->load($pk)) { 265 if (!$this->canEditState($table)) { 266 // Prune items that you can't change. 267 unset($pks[$i]); 268 Factory::getApplication()->enqueueMessage(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), 'error'); 269 } 270 } 271 } 272 273 // Attempt to change the state of the records. 274 if (!$table->stick($pks, $value, Factory::getUser()->id)) { 275 $this->setError($table->getError()); 276 277 return false; 278 } 279 280 return true; 281 } 282 283 /** 284 * A protected method to get a set of ordering conditions. 285 * 286 * @param Table $table A record object. 287 * 288 * @return array An array of conditions to add to ordering queries. 289 * 290 * @since 1.6 291 */ 292 protected function getReorderConditions($table) 293 { 294 $db = $this->getDatabase(); 295 296 return [ 297 $db->quoteName('catid') . ' = ' . (int) $table->catid, 298 $db->quoteName('state') . ' >= 0', 299 ]; 300 } 301 302 /** 303 * Prepare and sanitise the table prior to saving. 304 * 305 * @param Table $table A Table object. 306 * 307 * @return void 308 * 309 * @since 1.6 310 */ 311 protected function prepareTable($table) 312 { 313 $date = Factory::getDate(); 314 $user = Factory::getUser(); 315 316 if (empty($table->id)) { 317 // Set the values 318 $table->created = $date->toSql(); 319 $table->created_by = $user->id; 320 321 // Set ordering to the last item if not set 322 if (empty($table->ordering)) { 323 $db = $this->getDatabase(); 324 $query = $db->getQuery(true) 325 ->select('MAX(' . $db->quoteName('ordering') . ')') 326 ->from($db->quoteName('#__banners')); 327 328 $db->setQuery($query); 329 $max = $db->loadResult(); 330 331 $table->ordering = $max + 1; 332 } 333 } else { 334 // Set the values 335 $table->modified = $date->toSql(); 336 $table->modified_by = $user->id; 337 } 338 339 // Increment the content version number. 340 $table->version++; 341 } 342 343 /** 344 * Allows preprocessing of the Form object. 345 * 346 * @param Form $form The form object 347 * @param array $data The data to be merged into the form object 348 * @param string $group The plugin group to be executed 349 * 350 * @return void 351 * 352 * @since 3.6.1 353 */ 354 protected function preprocessForm(Form $form, $data, $group = 'content') 355 { 356 if ($this->canCreateCategory()) { 357 $form->setFieldAttribute('catid', 'allowAdd', 'true'); 358 359 // Add a prefix for categories created on the fly. 360 $form->setFieldAttribute('catid', 'customPrefix', '#new#'); 361 } 362 363 parent::preprocessForm($form, $data, $group); 364 } 365 366 /** 367 * Method to save the form data. 368 * 369 * @param array $data The form data. 370 * 371 * @return boolean True on success. 372 * 373 * @since 1.6 374 */ 375 public function save($data) 376 { 377 $input = Factory::getApplication()->input; 378 379 // Create new category, if needed. 380 $createCategory = true; 381 382 // If category ID is provided, check if it's valid. 383 if (is_numeric($data['catid']) && $data['catid']) { 384 $createCategory = !CategoriesHelper::validateCategoryId($data['catid'], 'com_banners'); 385 } 386 387 // Save New Category 388 if ($createCategory && $this->canCreateCategory()) { 389 $category = [ 390 // Remove #new# prefix, if exists. 391 'title' => strpos($data['catid'], '#new#') === 0 ? substr($data['catid'], 5) : $data['catid'], 392 'parent_id' => 1, 393 'extension' => 'com_banners', 394 'language' => $data['language'], 395 'published' => 1, 396 ]; 397 398 /** @var \Joomla\Component\Categories\Administrator\Model\CategoryModel $categoryModel */ 399 $categoryModel = Factory::getApplication()->bootComponent('com_categories') 400 ->getMVCFactory()->createModel('Category', 'Administrator', ['ignore_request' => true]); 401 402 // Create new category. 403 if (!$categoryModel->save($category)) { 404 $this->setError($categoryModel->getError()); 405 406 return false; 407 } 408 409 // Get the new category ID. 410 $data['catid'] = $categoryModel->getState('category.id'); 411 } 412 413 // Alter the name for save as copy 414 if ($input->get('task') == 'save2copy') { 415 /** @var \Joomla\Component\Banners\Administrator\Table\BannerTable $origTable */ 416 $origTable = clone $this->getTable(); 417 $origTable->load($input->getInt('id')); 418 419 if ($data['name'] == $origTable->name) { 420 list($name, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['name']); 421 $data['name'] = $name; 422 $data['alias'] = $alias; 423 } else { 424 if ($data['alias'] == $origTable->alias) { 425 $data['alias'] = ''; 426 } 427 } 428 429 $data['state'] = 0; 430 } 431 432 return parent::save($data); 433 } 434 435 /** 436 * Is the user allowed to create an on the fly category? 437 * 438 * @return boolean 439 * 440 * @since 3.6.1 441 */ 442 private function canCreateCategory() 443 { 444 return Factory::getUser()->authorise('core.create', 'com_banners'); 445 } 446 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |