PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_newsfeeds/models/newsfeed.php

https://gitlab.com/lankerd/paGO---Testing-Site
PHP | 546 lines | 286 code | 72 blank | 188 comment | 39 complexity | d4cac65a2ba729b68f064f1339c7a15d MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_newsfeeds
  5. *
  6. * @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. use Joomla\Registry\Registry;
  11. JLoader::register('NewsfeedsHelper', JPATH_ADMINISTRATOR . '/components/com_newsfeeds/helpers/newsfeeds.php');
  12. /**
  13. * Newsfeed model.
  14. *
  15. * @since 1.6
  16. */
  17. class NewsfeedsModelNewsfeed extends JModelAdmin
  18. {
  19. /**
  20. * The type alias for this content type.
  21. *
  22. * @var string
  23. * @since 3.2
  24. */
  25. public $typeAlias = 'com_newsfeeds.newsfeed';
  26. /**
  27. * The context used for the associations table
  28. *
  29. * @var string
  30. * @since 3.4.4
  31. */
  32. protected $associationsContext = 'com_newsfeeds.item';
  33. /**
  34. * @var string The prefix to use with controller messages.
  35. * @since 1.6
  36. */
  37. protected $text_prefix = 'COM_NEWSFEEDS';
  38. /**
  39. * Batch copy items to a new category or current.
  40. *
  41. * @param integer $value The new category.
  42. * @param array $pks An array of row IDs.
  43. * @param array $contexts An array of item contexts.
  44. *
  45. * @return mixed An array of new IDs on success, boolean false on failure.
  46. *
  47. * @since 11.1
  48. */
  49. protected function batchCopy($value, $pks, $contexts)
  50. {
  51. $categoryId = (int) $value;
  52. $newIds = array();
  53. if (!parent::checkCategoryId($categoryId))
  54. {
  55. return false;
  56. }
  57. // Parent exists so we let's proceed
  58. while (!empty($pks))
  59. {
  60. // Pop the first ID off the stack
  61. $pk = array_shift($pks);
  62. $this->table->reset();
  63. // Check that the row actually exists
  64. if (!$this->table->load($pk))
  65. {
  66. if ($error = $this->table->getError())
  67. {
  68. // Fatal error
  69. $this->setError($error);
  70. return false;
  71. }
  72. else
  73. {
  74. // Not fatal error
  75. $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
  76. continue;
  77. }
  78. }
  79. // Alter the title & alias
  80. $data = $this->generateNewTitle($categoryId, $this->table->alias, $this->table->name);
  81. $this->table->name = $data['0'];
  82. $this->table->alias = $data['1'];
  83. // Reset the ID because we are making a copy
  84. $this->table->id = 0;
  85. // Unpublish because we are making a copy
  86. $this->table->published = 0;
  87. // New category ID
  88. $this->table->catid = $categoryId;
  89. // TODO: Deal with ordering?
  90. // $this->table->ordering = 1;
  91. // Check the row.
  92. if (!$this->table->check())
  93. {
  94. $this->setError($this->table->getError());
  95. return false;
  96. }
  97. parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
  98. // Store the row.
  99. if (!$this->table->store())
  100. {
  101. $this->setError($this->table->getError());
  102. return false;
  103. }
  104. // Get the new item ID
  105. $newId = $this->table->get('id');
  106. // Add the new ID to the array
  107. $newIds[$pk] = $newId;
  108. }
  109. // Clean the cache
  110. $this->cleanCache();
  111. return $newIds;
  112. }
  113. /**
  114. * Method to test whether a record can be deleted.
  115. *
  116. * @param object $record A record object.
  117. *
  118. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  119. *
  120. * @since 1.6
  121. */
  122. protected function canDelete($record)
  123. {
  124. if (!empty($record->id))
  125. {
  126. if ($record->published != -2)
  127. {
  128. return false;
  129. }
  130. $user = JFactory::getUser();
  131. if (!empty($record->catid))
  132. {
  133. return $user->authorise('core.delete', 'com_newsfeed.category.' . (int) $record->catid);
  134. }
  135. else
  136. {
  137. return parent::canDelete($record);
  138. }
  139. }
  140. return false;
  141. }
  142. /**
  143. * Method to test whether a record can have its state changed.
  144. *
  145. * @param object $record A record object.
  146. *
  147. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  148. *
  149. * @since 1.6
  150. */
  151. protected function canEditState($record)
  152. {
  153. $user = JFactory::getUser();
  154. if (!empty($record->catid))
  155. {
  156. return $user->authorise('core.edit.state', 'com_newsfeeds.category.' . (int) $record->catid);
  157. }
  158. else
  159. {
  160. return parent::canEditState($record);
  161. }
  162. }
  163. /**
  164. * Returns a Table object, always creating it.
  165. *
  166. * @param string $type The table type to instantiate
  167. * @param string $prefix A prefix for the table class name. Optional.
  168. * @param array $config Configuration array for model. Optional.
  169. *
  170. * @return JTable A database object
  171. *
  172. * @since 1.6
  173. */
  174. public function getTable($type = 'Newsfeed', $prefix = 'NewsfeedsTable', $config = array())
  175. {
  176. return JTable::getInstance($type, $prefix, $config);
  177. }
  178. /**
  179. * Method to get the record form.
  180. *
  181. * @param array $data Data for the form.
  182. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  183. *
  184. * @return JForm A JForm object on success, false on failure
  185. *
  186. * @since 1.6
  187. */
  188. public function getForm($data = array(), $loadData = true)
  189. {
  190. // Get the form.
  191. $form = $this->loadForm('com_newsfeeds.newsfeed', 'newsfeed', array('control' => 'jform', 'load_data' => $loadData));
  192. if (empty($form))
  193. {
  194. return false;
  195. }
  196. // Determine correct permissions to check.
  197. if ($this->getState('newsfeed.id'))
  198. {
  199. // Existing record. Can only edit in selected categories.
  200. $form->setFieldAttribute('catid', 'action', 'core.edit');
  201. }
  202. else
  203. {
  204. // New record. Can only create in selected categories.
  205. $form->setFieldAttribute('catid', 'action', 'core.create');
  206. }
  207. // Modify the form based on access controls.
  208. if (!$this->canEditState((object) $data))
  209. {
  210. // Disable fields for display.
  211. $form->setFieldAttribute('ordering', 'disabled', 'true');
  212. $form->setFieldAttribute('published', 'disabled', 'true');
  213. $form->setFieldAttribute('publish_up', 'disabled', 'true');
  214. $form->setFieldAttribute('publish_down', 'disabled', 'true');
  215. // Disable fields while saving.
  216. // The controller has already verified this is a record you can edit.
  217. $form->setFieldAttribute('ordering', 'filter', 'unset');
  218. $form->setFieldAttribute('published', 'filter', 'unset');
  219. $form->setFieldAttribute('publish_up', 'filter', 'unset');
  220. $form->setFieldAttribute('publish_down', 'filter', 'unset');
  221. }
  222. return $form;
  223. }
  224. /**
  225. * Method to get the data that should be injected in the form.
  226. *
  227. * @return mixed The data for the form.
  228. *
  229. * @since 1.6
  230. */
  231. protected function loadFormData()
  232. {
  233. // Check the session for previously entered form data.
  234. $data = JFactory::getApplication()->getUserState('com_newsfeeds.edit.newsfeed.data', array());
  235. if (empty($data))
  236. {
  237. $data = $this->getItem();
  238. // Prime some default values.
  239. if ($this->getState('newsfeed.id') == 0)
  240. {
  241. $app = JFactory::getApplication();
  242. $data->set('catid', $app->input->get('catid', $app->getUserState('com_newsfeeds.newsfeeds.filter.category_id'), 'int'));
  243. }
  244. }
  245. $this->preprocessData('com_newsfeeds.newsfeed', $data);
  246. return $data;
  247. }
  248. /**
  249. * Method to save the form data.
  250. *
  251. * @param array $data The form data.
  252. *
  253. * @return boolean True on success.
  254. *
  255. * @since 3.0
  256. */
  257. public function save($data)
  258. {
  259. $input = JFactory::getApplication()->input;
  260. // Alter the name for save as copy
  261. if ($input->get('task') == 'save2copy')
  262. {
  263. $origTable = clone $this->getTable();
  264. $origTable->load($input->getInt('id'));
  265. if ($data['name'] == $origTable->name)
  266. {
  267. list($name, $alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['name']);
  268. $data['name'] = $name;
  269. $data['alias'] = $alias;
  270. }
  271. else
  272. {
  273. if ($data['alias'] == $origTable->alias)
  274. {
  275. $data['alias'] = '';
  276. }
  277. }
  278. $data['published'] = 0;
  279. }
  280. return parent::save($data);
  281. }
  282. /**
  283. * Method to get a single record.
  284. *
  285. * @param integer $pk The id of the primary key.
  286. *
  287. * @return mixed Object on success, false on failure.
  288. *
  289. * @since 1.6
  290. */
  291. public function getItem($pk = null)
  292. {
  293. if ($item = parent::getItem($pk))
  294. {
  295. // Convert the params field to an array.
  296. $registry = new Registry;
  297. $registry->loadString($item->metadata);
  298. $item->metadata = $registry->toArray();
  299. // Convert the images field to an array.
  300. $registry = new Registry;
  301. $registry->loadString($item->images);
  302. $item->images = $registry->toArray();
  303. }
  304. // Load associated newsfeeds items
  305. $app = JFactory::getApplication();
  306. $assoc = JLanguageAssociations::isEnabled();
  307. if ($assoc)
  308. {
  309. $item->associations = array();
  310. if ($item->id != null)
  311. {
  312. $associations = JLanguageAssociations::getAssociations('com_newsfeeds', '#__newsfeeds', 'com_newsfeeds.item', $item->id);
  313. foreach ($associations as $tag => $association)
  314. {
  315. $item->associations[$tag] = $association->id;
  316. }
  317. }
  318. }
  319. if (!empty($item->id))
  320. {
  321. $item->tags = new JHelperTags;
  322. $item->tags->getTagIds($item->id, 'com_newsfeeds.newsfeed');
  323. $item->metadata['tags'] = $item->tags;
  324. }
  325. return $item;
  326. }
  327. /**
  328. * Prepare and sanitise the table prior to saving.
  329. *
  330. * @param JTable $table The table object
  331. *
  332. * @return void
  333. */
  334. protected function prepareTable($table)
  335. {
  336. $date = JFactory::getDate();
  337. $user = JFactory::getUser();
  338. $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
  339. $table->alias = JApplicationHelper::stringURLSafe($table->alias);
  340. if (empty($table->alias))
  341. {
  342. $table->alias = JApplicationHelper::stringURLSafe($table->name);
  343. }
  344. if (empty($table->id))
  345. {
  346. // Set the values
  347. $table->created = $date->toSql();
  348. // Set ordering to the last item if not set
  349. if (empty($table->ordering))
  350. {
  351. $db = $this->getDbo();
  352. $query = $db->getQuery(true)
  353. ->select('MAX(ordering)')
  354. ->from($db->quoteName('#__newsfeeds'));
  355. $db->setQuery($query);
  356. $max = $db->loadResult();
  357. $table->ordering = $max + 1;
  358. }
  359. }
  360. else
  361. {
  362. // Set the values
  363. $table->modified = $date->toSql();
  364. $table->modified_by = $user->get('id');
  365. }
  366. // Increment the content version number.
  367. $table->version++;
  368. }
  369. /**
  370. * Method to change the published state of one or more records.
  371. *
  372. * @param array &$pks A list of the primary keys to change.
  373. * @param integer $value The value of the published state.
  374. *
  375. * @return boolean True on success.
  376. *
  377. * @since 1.6
  378. */
  379. public function publish(&$pks, $value = 1)
  380. {
  381. $result = parent::publish($pks, $value);
  382. // Clean extra cache for newsfeeds
  383. $this->cleanCache('feed_parser');
  384. return $result;
  385. }
  386. /**
  387. * A protected method to get a set of ordering conditions.
  388. *
  389. * @param object $table A record object.
  390. *
  391. * @return array An array of conditions to add to add to ordering queries.
  392. *
  393. * @since 1.6
  394. */
  395. protected function getReorderConditions($table)
  396. {
  397. $condition = array();
  398. $condition[] = 'catid = ' . (int) $table->catid;
  399. return $condition;
  400. }
  401. /**
  402. * A protected method to get a set of ordering conditions.
  403. *
  404. * @param JForm $form The form object.
  405. * @param array $data The data to be injected into the form
  406. * @param string $group The plugin group to process
  407. *
  408. * @return array An array of conditions to add to add to ordering queries.
  409. *
  410. * @since 1.6
  411. */
  412. protected function preprocessForm(JForm $form, $data, $group = 'content')
  413. {
  414. // Association newsfeeds items
  415. $app = JFactory::getApplication();
  416. $assoc = JLanguageAssociations::isEnabled();
  417. if ($assoc)
  418. {
  419. $languages = JLanguageHelper::getLanguages('lang_code');
  420. $addform = new SimpleXMLElement('<form />');
  421. $fields = $addform->addChild('fields');
  422. $fields->addAttribute('name', 'associations');
  423. $fieldset = $fields->addChild('fieldset');
  424. $fieldset->addAttribute('name', 'item_associations');
  425. $fieldset->addAttribute('description', 'COM_NEWSFEEDS_ITEM_ASSOCIATIONS_FIELDSET_DESC');
  426. $add = false;
  427. foreach ($languages as $tag => $language)
  428. {
  429. if (empty($data->language) || $tag != $data->language)
  430. {
  431. $add = true;
  432. $field = $fieldset->addChild('field');
  433. $field->addAttribute('name', $tag);
  434. $field->addAttribute('type', 'modal_newsfeed');
  435. $field->addAttribute('language', $tag);
  436. $field->addAttribute('label', $language->title);
  437. $field->addAttribute('translate_label', 'false');
  438. $field->addAttribute('edit', 'true');
  439. $field->addAttribute('clear', 'true');
  440. }
  441. }
  442. if ($add)
  443. {
  444. $form->load($addform, false);
  445. }
  446. }
  447. parent::preprocessForm($form, $data, $group);
  448. }
  449. /**
  450. * Method to change the title & alias.
  451. *
  452. * @param integer $category_id The id of the parent.
  453. * @param string $alias The alias.
  454. * @param string $name The title.
  455. *
  456. * @return array Contains the modified title and alias.
  457. *
  458. * @since 3.1
  459. */
  460. protected function generateNewTitle($category_id, $alias, $name)
  461. {
  462. // Alter the title & alias
  463. $table = $this->getTable();
  464. while ($table->load(array('alias' => $alias, 'catid' => $category_id)))
  465. {
  466. if ($name == $table->name)
  467. {
  468. $name = JString::increment($name);
  469. }
  470. $alias = JString::increment($alias, 'dash');
  471. }
  472. return array($name, $alias);
  473. }
  474. }