PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_banners/models/banner.php

https://bitbucket.org/asosso/joomla25
PHP | 487 lines | 273 code | 58 blank | 156 comment | 39 complexity | f1879ea74a0d063e23dfcb2afd3b6b70 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_banners
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access.
  10. defined('_JEXEC') or die;
  11. jimport('joomla.application.component.modeladmin');
  12. /**
  13. * Banner model.
  14. *
  15. * @package Joomla.Administrator
  16. * @subpackage com_banners
  17. * @since 1.6
  18. */
  19. class BannersModelBanner extends JModelAdmin
  20. {
  21. /**
  22. * @var string The prefix to use with controller messages.
  23. * @since 1.6
  24. */
  25. protected $text_prefix = 'COM_BANNERS_BANNER';
  26. /**
  27. * Method to perform batch operations on an item or a set of items.
  28. *
  29. * @param array $commands An array of commands to perform.
  30. * @param array $pks An array of item ids.
  31. * @param array $contexts An array of item contexts.
  32. *
  33. * @return boolean Returns true on success, false on failure.
  34. *
  35. * @since 2.5
  36. */
  37. public function batch($commands, $pks, $contexts)
  38. {
  39. // Sanitize user ids.
  40. $pks = array_unique($pks);
  41. JArrayHelper::toInteger($pks);
  42. // Remove any values of zero.
  43. if (array_search(0, $pks, true))
  44. {
  45. unset($pks[array_search(0, $pks, true)]);
  46. }
  47. if (empty($pks))
  48. {
  49. $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
  50. return false;
  51. }
  52. $done = false;
  53. if (!empty($commands['category_id']))
  54. {
  55. $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c');
  56. if ($cmd == 'c')
  57. {
  58. $result = $this->batchCopy($commands['category_id'], $pks, $contexts);
  59. if (is_array($result))
  60. {
  61. $pks = $result;
  62. }
  63. else
  64. {
  65. return false;
  66. }
  67. }
  68. elseif ($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts))
  69. {
  70. return false;
  71. }
  72. $done = true;
  73. }
  74. if (strlen($commands['client_id']) > 0)
  75. {
  76. if (!$this->batchClient($commands['client_id'], $pks, $contexts))
  77. {
  78. return false;
  79. }
  80. $done = true;
  81. }
  82. if (!empty($commands['language_id']))
  83. {
  84. if (!$this->batchLanguage($commands['language_id'], $pks, $contexts))
  85. {
  86. return false;
  87. }
  88. $done = true;
  89. }
  90. if (!$done)
  91. {
  92. $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
  93. return false;
  94. }
  95. // Clear the cache
  96. $this->cleanCache();
  97. return true;
  98. }
  99. /**
  100. * Batch client changes for a group of banners.
  101. *
  102. * @param string $value The new value matching a client.
  103. * @param array $pks An array of row IDs.
  104. * @param array $contexts An array of item contexts.
  105. *
  106. * @return boolean True if successful, false otherwise and internal error is set.
  107. *
  108. * @since 2.5
  109. */
  110. protected function batchClient($value, $pks, $contexts)
  111. {
  112. // Set the variables
  113. $user = JFactory::getUser();
  114. $table = $this->getTable();
  115. foreach ($pks as $pk)
  116. {
  117. if ($user->authorise('core.edit', $contexts[$pk]))
  118. {
  119. $table->reset();
  120. $table->load($pk);
  121. $table->cid = (int) $value;
  122. if (!$table->store())
  123. {
  124. $this->setError($table->getError());
  125. return false;
  126. }
  127. }
  128. else
  129. {
  130. $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT'));
  131. return false;
  132. }
  133. }
  134. // Clean the cache
  135. $this->cleanCache();
  136. return true;
  137. }
  138. /**
  139. * Batch copy items to a new category or current.
  140. *
  141. * @param integer $value The new category.
  142. * @param array $pks An array of row IDs.
  143. * @param array $contexts An array of item contexts.
  144. *
  145. * @return mixed An array of new IDs on success, boolean false on failure.
  146. *
  147. * @since 2.5
  148. */
  149. protected function batchCopy($value, $pks, $contexts)
  150. {
  151. $categoryId = (int) $value;
  152. $table = $this->getTable();
  153. $i = 0;
  154. // Check that the category exists
  155. if ($categoryId)
  156. {
  157. $categoryTable = JTable::getInstance('Category');
  158. if (!$categoryTable->load($categoryId))
  159. {
  160. if ($error = $categoryTable->getError())
  161. {
  162. // Fatal error
  163. $this->setError($error);
  164. return false;
  165. }
  166. else
  167. {
  168. $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
  169. return false;
  170. }
  171. }
  172. }
  173. if (empty($categoryId))
  174. {
  175. $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND'));
  176. return false;
  177. }
  178. // Check that the user has create permission for the component
  179. $user = JFactory::getUser();
  180. if (!$user->authorise('core.create', 'com_banners.category.' . $categoryId))
  181. {
  182. $this->setError(JText::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE'));
  183. return false;
  184. }
  185. // Parent exists so we let's proceed
  186. while (!empty($pks))
  187. {
  188. // Pop the first ID off the stack
  189. $pk = array_shift($pks);
  190. $table->reset();
  191. // Check that the row actually exists
  192. if (!$table->load($pk))
  193. {
  194. if ($error = $table->getError())
  195. {
  196. // Fatal error
  197. $this->setError($error);
  198. return false;
  199. }
  200. else
  201. {
  202. // Not fatal error
  203. $this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk));
  204. continue;
  205. }
  206. }
  207. // Alter the title & alias
  208. $data = $this->generateNewTitle($categoryId, $table->alias, $table->name);
  209. $table->name = $data['0'];
  210. $table->alias = $data['1'];
  211. // Reset the ID because we are making a copy
  212. $table->id = 0;
  213. // New category ID
  214. $table->catid = $categoryId;
  215. // TODO: Deal with ordering?
  216. //$table->ordering = 1;
  217. // Check the row.
  218. if (!$table->check())
  219. {
  220. $this->setError($table->getError());
  221. return false;
  222. }
  223. // Store the row.
  224. if (!$table->store())
  225. {
  226. $this->setError($table->getError());
  227. return false;
  228. }
  229. // Get the new item ID
  230. $newId = $table->get('id');
  231. // Add the new ID to the array
  232. $newIds[$i] = $newId;
  233. $i++;
  234. }
  235. // Clean the cache
  236. $this->cleanCache();
  237. return $newIds;
  238. }
  239. /**
  240. * Method to test whether a record can be deleted.
  241. *
  242. * @param object $record A record object.
  243. *
  244. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  245. *
  246. * @since 1.6
  247. */
  248. protected function canDelete($record)
  249. {
  250. if (!empty($record->id))
  251. {
  252. if ($record->state != -2)
  253. {
  254. return;
  255. }
  256. $user = JFactory::getUser();
  257. if (!empty($record->catid))
  258. {
  259. return $user->authorise('core.delete', 'com_banners.category.' . (int) $record->catid);
  260. }
  261. else
  262. {
  263. return parent::canDelete($record);
  264. }
  265. }
  266. }
  267. /**
  268. * Method to test whether a record can have its state changed.
  269. *
  270. * @param object $record A record object.
  271. *
  272. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  273. *
  274. * @since 1.6
  275. */
  276. protected function canEditState($record)
  277. {
  278. $user = JFactory::getUser();
  279. // Check against the category.
  280. if (!empty($record->catid))
  281. {
  282. return $user->authorise('core.edit.state', 'com_banners.category.' . (int) $record->catid);
  283. }
  284. // Default to component settings if category not known.
  285. else
  286. {
  287. return parent::canEditState($record);
  288. }
  289. }
  290. /**
  291. * Returns a JTable object, always creating it.
  292. *
  293. * @param string $type The table type to instantiate. [optional]
  294. * @param string $prefix A prefix for the table class name. [optional]
  295. * @param array $config Configuration array for model. [optional]
  296. *
  297. * @return JTable A database object
  298. *
  299. * @since 1.6
  300. */
  301. public function getTable($type = 'Banner', $prefix = 'BannersTable', $config = array())
  302. {
  303. return JTable::getInstance($type, $prefix, $config);
  304. }
  305. /**
  306. * Method to get the record form.
  307. *
  308. * @param array $data Data for the form. [optional]
  309. * @param boolean $loadData True if the form is to load its own data (default case), false if not. [optional]
  310. *
  311. * @return mixed A JForm object on success, false on failure
  312. *
  313. * @since 1.6
  314. */
  315. public function getForm($data = array(), $loadData = true)
  316. {
  317. // Get the form.
  318. $form = $this->loadForm('com_banners.banner', 'banner', array('control' => 'jform', 'load_data' => $loadData));
  319. if (empty($form))
  320. {
  321. return false;
  322. }
  323. // Determine correct permissions to check.
  324. if ($this->getState('banner.id'))
  325. {
  326. // Existing record. Can only edit in selected categories.
  327. $form->setFieldAttribute('catid', 'action', 'core.edit');
  328. }
  329. else
  330. {
  331. // New record. Can only create in selected categories.
  332. $form->setFieldAttribute('catid', 'action', 'core.create');
  333. }
  334. // Modify the form based on access controls.
  335. if (!$this->canEditState((object) $data))
  336. {
  337. // Disable fields for display.
  338. $form->setFieldAttribute('ordering', 'disabled', 'true');
  339. $form->setFieldAttribute('publish_up', 'disabled', 'true');
  340. $form->setFieldAttribute('publish_down', 'disabled', 'true');
  341. $form->setFieldAttribute('state', 'disabled', 'true');
  342. $form->setFieldAttribute('sticky', 'disabled', 'true');
  343. // Disable fields while saving.
  344. // The controller has already verified this is a record you can edit.
  345. $form->setFieldAttribute('ordering', 'filter', 'unset');
  346. $form->setFieldAttribute('publish_up', 'filter', 'unset');
  347. $form->setFieldAttribute('publish_down', 'filter', 'unset');
  348. $form->setFieldAttribute('state', 'filter', 'unset');
  349. $form->setFieldAttribute('sticky', 'filter', 'unset');
  350. }
  351. return $form;
  352. }
  353. /**
  354. * Method to get the data that should be injected in the form.
  355. *
  356. * @return mixed The data for the form.
  357. *
  358. * @since 1.6
  359. */
  360. protected function loadFormData()
  361. {
  362. // Check the session for previously entered form data.
  363. $data = JFactory::getApplication()->getUserState('com_banners.edit.banner.data', array());
  364. if (empty($data))
  365. {
  366. $data = $this->getItem();
  367. // Prime some default values.
  368. if ($this->getState('banner.id') == 0)
  369. {
  370. $app = JFactory::getApplication();
  371. $data->set('catid', JRequest::getInt('catid', $app->getUserState('com_banners.banners.filter.category_id')));
  372. }
  373. }
  374. return $data;
  375. }
  376. /**
  377. * Method to stick records.
  378. *
  379. * @param array &$pks The ids of the items to publish.
  380. * @param integer $value The value of the published state
  381. *
  382. * @return boolean True on success.
  383. *
  384. * @since 1.6
  385. */
  386. function stick(&$pks, $value = 1)
  387. {
  388. // Initialise variables.
  389. $user = JFactory::getUser();
  390. $table = $this->getTable();
  391. $pks = (array) $pks;
  392. // Access checks.
  393. foreach ($pks as $i => $pk)
  394. {
  395. if ($table->load($pk))
  396. {
  397. if (!$this->canEditState($table))
  398. {
  399. // Prune items that you can't change.
  400. unset($pks[$i]);
  401. JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
  402. }
  403. }
  404. }
  405. // Attempt to change the state of the records.
  406. if (!$table->stick($pks, $value, $user->get('id')))
  407. {
  408. $this->setError($table->getError());
  409. return false;
  410. }
  411. return true;
  412. }
  413. /**
  414. * A protected method to get a set of ordering conditions.
  415. *
  416. * @param JTable $table A record object.
  417. *
  418. * @return array An array of conditions to add to add to ordering queries.
  419. *
  420. * @since 1.6
  421. */
  422. protected function getReorderConditions($table)
  423. {
  424. $condition = array();
  425. $condition[] = 'catid = '. (int) $table->catid;
  426. $condition[] = 'state >= 0';
  427. return $condition;
  428. }
  429. }