[ 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_newsfeeds 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\Newsfeeds\Administrator\Model; 12 13 use Joomla\CMS\Application\ApplicationHelper; 14 use Joomla\CMS\Factory; 15 use Joomla\CMS\Form\Form; 16 use Joomla\CMS\Helper\TagsHelper; 17 use Joomla\CMS\Language\Associations; 18 use Joomla\CMS\Language\LanguageHelper; 19 use Joomla\CMS\MVC\Model\AdminModel; 20 use Joomla\CMS\Versioning\VersionableModelTrait; 21 use Joomla\Component\Categories\Administrator\Helper\CategoriesHelper; 22 use Joomla\Registry\Registry; 23 24 // phpcs:disable PSR1.Files.SideEffects 25 \defined('_JEXEC') or die; 26 // phpcs:enable PSR1.Files.SideEffects 27 28 /** 29 * Newsfeed model. 30 * 31 * @since 1.6 32 */ 33 class NewsfeedModel extends AdminModel 34 { 35 use VersionableModelTrait; 36 37 /** 38 * The type alias for this content type. 39 * 40 * @var string 41 * @since 3.2 42 */ 43 public $typeAlias = 'com_newsfeeds.newsfeed'; 44 45 /** 46 * The context used for the associations table 47 * 48 * @var string 49 * @since 3.4.4 50 */ 51 protected $associationsContext = 'com_newsfeeds.item'; 52 53 /** 54 * @var string The prefix to use with controller messages. 55 * @since 1.6 56 */ 57 protected $text_prefix = 'COM_NEWSFEEDS'; 58 59 /** 60 * Method to test whether a record can be deleted. 61 * 62 * @param object $record A record object. 63 * 64 * @return boolean True if allowed to delete the record. Defaults to the permission set in the component. 65 * 66 * @since 1.6 67 */ 68 protected function canDelete($record) 69 { 70 if (empty($record->id) || $record->published != -2) { 71 return false; 72 } 73 74 if (!empty($record->catid)) { 75 return Factory::getUser()->authorise('core.delete', 'com_newsfeed.category.' . (int) $record->catid); 76 } 77 78 return parent::canDelete($record); 79 } 80 81 /** 82 * Method to test whether a record can have its state changed. 83 * 84 * @param object $record A record object. 85 * 86 * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component. 87 * 88 * @since 1.6 89 */ 90 protected function canEditState($record) 91 { 92 if (!empty($record->catid)) { 93 return Factory::getUser()->authorise('core.edit.state', 'com_newsfeeds.category.' . (int) $record->catid); 94 } 95 96 return parent::canEditState($record); 97 } 98 99 /** 100 * Method to get the record form. 101 * 102 * @param array $data Data for the form. 103 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 104 * 105 * @return Form|bool A Form object on success, false on failure 106 * 107 * @since 1.6 108 */ 109 public function getForm($data = array(), $loadData = true) 110 { 111 // Get the form. 112 $form = $this->loadForm('com_newsfeeds.newsfeed', 'newsfeed', array('control' => 'jform', 'load_data' => $loadData)); 113 114 if (empty($form)) { 115 return false; 116 } 117 118 // Modify the form based on access controls. 119 if (!$this->canEditState((object) $data)) { 120 // Disable fields for display. 121 $form->setFieldAttribute('ordering', 'disabled', 'true'); 122 $form->setFieldAttribute('published', 'disabled', 'true'); 123 $form->setFieldAttribute('publish_up', 'disabled', 'true'); 124 $form->setFieldAttribute('publish_down', 'disabled', 'true'); 125 126 // Disable fields while saving. 127 // The controller has already verified this is a record you can edit. 128 $form->setFieldAttribute('ordering', 'filter', 'unset'); 129 $form->setFieldAttribute('published', 'filter', 'unset'); 130 $form->setFieldAttribute('publish_up', 'filter', 'unset'); 131 $form->setFieldAttribute('publish_down', 'filter', 'unset'); 132 } 133 134 // Don't allow to change the created_by user if not allowed to access com_users. 135 if (!Factory::getUser()->authorise('core.manage', 'com_users')) { 136 $form->setFieldAttribute('created_by', 'filter', 'unset'); 137 } 138 139 return $form; 140 } 141 142 /** 143 * Method to get the data that should be injected in the form. 144 * 145 * @return mixed The data for the form. 146 * 147 * @since 1.6 148 */ 149 protected function loadFormData() 150 { 151 // Check the session for previously entered form data. 152 $data = Factory::getApplication()->getUserState('com_newsfeeds.edit.newsfeed.data', array()); 153 154 if (empty($data)) { 155 $data = $this->getItem(); 156 157 // Prime some default values. 158 if ($this->getState('newsfeed.id') == 0) { 159 $app = Factory::getApplication(); 160 $data->set('catid', $app->input->get('catid', $app->getUserState('com_newsfeeds.newsfeeds.filter.category_id'), 'int')); 161 } 162 } 163 164 $this->preprocessData('com_newsfeeds.newsfeed', $data); 165 166 return $data; 167 } 168 169 /** 170 * Method to save the form data. 171 * 172 * @param array $data The form data. 173 * 174 * @return boolean True on success. 175 * 176 * @since 3.0 177 */ 178 public function save($data) 179 { 180 $input = Factory::getApplication()->input; 181 182 // Create new category, if needed. 183 $createCategory = true; 184 185 // If category ID is provided, check if it's valid. 186 if (is_numeric($data['catid']) && $data['catid']) { 187 $createCategory = !CategoriesHelper::validateCategoryId($data['catid'], 'com_newsfeeds'); 188 } 189 190 // Save New Category 191 if ($createCategory && $this->canCreateCategory()) { 192 $category = [ 193 // Remove #new# prefix, if exists. 194 'title' => strpos($data['catid'], '#new#') === 0 ? substr($data['catid'], 5) : $data['catid'], 195 'parent_id' => 1, 196 'extension' => 'com_newsfeeds', 197 'language' => $data['language'], 198 'published' => 1, 199 ]; 200 201 /** @var \Joomla\Component\Categories\Administrator\Model\CategoryModel $categoryModel */ 202 $categoryModel = Factory::getApplication()->bootComponent('com_categories') 203 ->getMVCFactory()->createModel('Category', 'Administrator', ['ignore_request' => true]); 204 205 // Create new category. 206 if (!$categoryModel->save($category)) { 207 $this->setError($categoryModel->getError()); 208 209 return false; 210 } 211 212 // Get the Category ID. 213 $data['catid'] = $categoryModel->getState('category.id'); 214 } 215 216 // Alter the name for save as copy 217 if ($input->get('task') == 'save2copy') { 218 $origTable = clone $this->getTable(); 219 $origTable->load($input->getInt('id')); 220 221 if ($data['name'] == $origTable->name) { 222 list($name, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['name']); 223 $data['name'] = $name; 224 $data['alias'] = $alias; 225 } else { 226 if ($data['alias'] == $origTable->alias) { 227 $data['alias'] = ''; 228 } 229 } 230 231 $data['published'] = 0; 232 } 233 234 return parent::save($data); 235 } 236 237 /** 238 * Method to get a single record. 239 * 240 * @param integer $pk The id of the primary key. 241 * 242 * @return mixed Object on success, false on failure. 243 * 244 * @since 1.6 245 */ 246 public function getItem($pk = null) 247 { 248 if ($item = parent::getItem($pk)) { 249 // Convert the params field to an array. 250 $registry = new Registry($item->metadata); 251 $item->metadata = $registry->toArray(); 252 253 // Convert the images field to an array. 254 $registry = new Registry($item->images); 255 $item->images = $registry->toArray(); 256 } 257 258 // Load associated newsfeeds items 259 $assoc = Associations::isEnabled(); 260 261 if ($assoc) { 262 $item->associations = array(); 263 264 if ($item->id != null) { 265 $associations = Associations::getAssociations('com_newsfeeds', '#__newsfeeds', 'com_newsfeeds.item', $item->id); 266 267 foreach ($associations as $tag => $association) { 268 $item->associations[$tag] = $association->id; 269 } 270 } 271 } 272 273 if (!empty($item->id)) { 274 $item->tags = new TagsHelper(); 275 $item->tags->getTagIds($item->id, 'com_newsfeeds.newsfeed'); 276 277 // @todo: We probably don't need this in any client - but needs careful validation 278 if (!Factory::getApplication()->isClient('api')) { 279 $item->metadata['tags'] = $item->tags; 280 } 281 } 282 283 return $item; 284 } 285 286 /** 287 * Prepare and sanitise the table prior to saving. 288 * 289 * @param \Joomla\CMS\Table\Table $table The table object 290 * 291 * @return void 292 */ 293 protected function prepareTable($table) 294 { 295 $date = Factory::getDate(); 296 $user = Factory::getUser(); 297 298 $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES); 299 $table->alias = ApplicationHelper::stringURLSafe($table->alias, $table->language); 300 301 if (empty($table->alias)) { 302 $table->alias = ApplicationHelper::stringURLSafe($table->name, $table->language); 303 } 304 305 if (empty($table->id)) { 306 // Set the values 307 $table->created = $date->toSql(); 308 309 // Set ordering to the last item if not set 310 if (empty($table->ordering)) { 311 $db = $this->getDatabase(); 312 $query = $db->getQuery(true) 313 ->select('MAX(' . $db->quoteName('ordering') . ')') 314 ->from($db->quoteName('#__newsfeeds')); 315 $db->setQuery($query); 316 $max = $db->loadResult(); 317 318 $table->ordering = $max + 1; 319 } 320 } else { 321 // Set the values 322 $table->modified = $date->toSql(); 323 $table->modified_by = $user->get('id'); 324 } 325 326 // Increment the content version number. 327 $table->version++; 328 } 329 330 /** 331 * Method to change the published state of one or more records. 332 * 333 * @param array &$pks A list of the primary keys to change. 334 * @param integer $value The value of the published state. 335 * 336 * @return boolean True on success. 337 * 338 * @since 1.6 339 */ 340 public function publish(&$pks, $value = 1) 341 { 342 $result = parent::publish($pks, $value); 343 344 // Clean extra cache for newsfeeds 345 $this->cleanCache('feed_parser'); 346 347 return $result; 348 } 349 350 /** 351 * A protected method to get a set of ordering conditions. 352 * 353 * @param object $table A record object. 354 * 355 * @return array An array of conditions to add to ordering queries. 356 * 357 * @since 1.6 358 */ 359 protected function getReorderConditions($table) 360 { 361 return [ 362 $this->getDatabase()->quoteName('catid') . ' = ' . (int) $table->catid, 363 ]; 364 } 365 366 /** 367 * A protected method to get a set of ordering conditions. 368 * 369 * @param Form $form The form object. 370 * @param array $data The data to be injected into the form 371 * @param string $group The plugin group to process 372 * 373 * @return array An array of conditions to add to ordering queries. 374 * 375 * @since 1.6 376 */ 377 protected function preprocessForm(Form $form, $data, $group = 'content') 378 { 379 if ($this->canCreateCategory()) { 380 $form->setFieldAttribute('catid', 'allowAdd', 'true'); 381 382 // Add a prefix for categories created on the fly. 383 $form->setFieldAttribute('catid', 'customPrefix', '#new#'); 384 } 385 386 // Association newsfeeds items 387 if (Associations::isEnabled()) { 388 $languages = LanguageHelper::getContentLanguages(false, false, null, 'ordering', 'asc'); 389 390 if (count($languages) > 1) { 391 $addform = new \SimpleXMLElement('<form />'); 392 $fields = $addform->addChild('fields'); 393 $fields->addAttribute('name', 'associations'); 394 $fieldset = $fields->addChild('fieldset'); 395 $fieldset->addAttribute('name', 'item_associations'); 396 397 foreach ($languages as $language) { 398 $field = $fieldset->addChild('field'); 399 $field->addAttribute('name', $language->lang_code); 400 $field->addAttribute('type', 'modal_newsfeed'); 401 $field->addAttribute('language', $language->lang_code); 402 $field->addAttribute('label', $language->title); 403 $field->addAttribute('translate_label', 'false'); 404 $field->addAttribute('select', 'true'); 405 $field->addAttribute('new', 'true'); 406 $field->addAttribute('edit', 'true'); 407 $field->addAttribute('clear', 'true'); 408 $field->addAttribute('propagate', 'true'); 409 } 410 411 $form->load($addform, false); 412 } 413 } 414 415 parent::preprocessForm($form, $data, $group); 416 } 417 418 /** 419 * Is the user allowed to create an on the fly category? 420 * 421 * @return boolean 422 * 423 * @since 3.6.1 424 */ 425 private function canCreateCategory() 426 { 427 return Factory::getUser()->authorise('core.create', 'com_newsfeeds'); 428 } 429 }
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 |