PageRenderTime 64ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_redirect/models/link.php

https://github.com/joebushi/joomla
PHP | 336 lines | 171 code | 48 blank | 117 comment | 19 complexity | 3dfafe7fb68172c230932f3b591c4f31 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access.
  8. defined('_JEXEC') or die;
  9. jimport('joomla.application.component.modelform');
  10. /**
  11. * Redirect link model.
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_redirect
  15. * @since 1.6
  16. */
  17. class RedirectModelLink extends JModelForm
  18. {
  19. /**
  20. * Method to auto-populate the model state.
  21. */
  22. protected function _populateState()
  23. {
  24. // Get the application object.
  25. $app = & JFactory::getApplication();
  26. // Load the User state.
  27. if (!$pk = (int) $app->getUserState('com_redirect.edit.link.id')) {
  28. $pk = (int) JRequest::getInt('id');
  29. }
  30. $this->setState('link.id', $pk);
  31. // Load the parameters.
  32. $params = JComponentHelper::getParams('com_redirect');
  33. $this->setState('params', $params);
  34. }
  35. /**
  36. * Returns a reference to the a Table object, always creating it.
  37. *
  38. * @param type $type The table type to instantiate
  39. * @param string $prefix A prefix for the table class name. Optional.
  40. * @param array $options Configuration array for model. Optional.
  41. * @return JTable A database object
  42. */
  43. public function getTable($type = 'Link', $prefix = 'RedirectTable', $config = array())
  44. {
  45. return JTable::getInstance($type, $prefix, $config);
  46. }
  47. /**
  48. * Method to override check-out a row for editing.
  49. *
  50. * @param int The ID of the primary key.
  51. * @return boolean
  52. */
  53. public function checkout($pk = null)
  54. {
  55. // Initialise variables.
  56. $pk = (!empty($pk)) ? $pk : (int) $this->getState('link.id');
  57. return parent::checkout($pk);
  58. }
  59. /**
  60. * Method to checkin a row.
  61. *
  62. * @param integer The ID of the primary key.
  63. *
  64. * @return boolean
  65. */
  66. public function checkin($pk = null)
  67. {
  68. // Initialise variables.
  69. $pk = (!empty($pk)) ? $pk : (int) $this->getState('link.id');
  70. return parent::checkin($pk);
  71. }
  72. /**
  73. * Method to get a single record.
  74. *
  75. * @param integer The id of the primary key.
  76. *
  77. * @return mixed Object on success, false on failure.
  78. */
  79. public function &getItem($pk = null)
  80. {
  81. // Initialise variables.
  82. $pk = (!empty($pk)) ? $pk : (int) $this->getState('link.id');
  83. $false = false;
  84. // Get a row instance.
  85. $table = &$this->getTable();
  86. // Attempt to load the row.
  87. $return = $table->load($pk);
  88. // Check for a table object error.
  89. if ($return === false && $table->getError()) {
  90. $this->setError($table->getError());
  91. return $false;
  92. }
  93. // Prime required properties.
  94. if (empty($table->id))
  95. {
  96. // Prepare data for a new record.
  97. }
  98. $value = JArrayHelper::toObject($table->getProperties(1), 'JObject');
  99. return $value;
  100. }
  101. /**
  102. * Method to get the record form.
  103. *
  104. * @return mixed JForm object on success, false on failure.
  105. */
  106. public function getForm()
  107. {
  108. // Initialise variables.
  109. $app = JFactory::getApplication();
  110. // Get the form.
  111. $form = parent::getForm('link', 'com_redirect.link', array('array' => 'jform', 'event' => 'onPrepareForm'));
  112. // Check for an error.
  113. if (JError::isError($form)) {
  114. $this->setError($form->getMessage());
  115. return false;
  116. }
  117. // Check the session for previously entered form data.
  118. $data = $app->getUserState('com_redirect.edit.link.data', array());
  119. // Bind the form data if present.
  120. if (!empty($data)) {
  121. $form->bind($data);
  122. }
  123. return $form;
  124. }
  125. /**
  126. * Method to save the form data.
  127. *
  128. * @param array The form data.
  129. * @return boolean True on success.
  130. */
  131. public function save($data)
  132. {
  133. // Initialise variables.
  134. $table = $this->getTable();
  135. $pk = (!empty($data['id'])) ? $data['id'] : (int) $this->getState('link.id');
  136. $isNew = true;
  137. // Load the row if saving an existing record.
  138. if ($pk > 0) {
  139. $table->load($pk);
  140. $isNew = false;
  141. }
  142. // Bind the data.
  143. if (!$table->bind($data)) {
  144. $this->setError(JText::sprintf('JTable_Error_Bind_failed', $table->getError()));
  145. return false;
  146. }
  147. // Prepare the row for saving
  148. $this->_prepareTable($table);
  149. // Check the data.
  150. if (!$table->check()) {
  151. $this->setError($table->getError());
  152. return false;
  153. }
  154. // Store the data.
  155. if (!$table->store()) {
  156. $this->setError($table->getError());
  157. return false;
  158. }
  159. $this->setState('link.id', $table->id);
  160. return true;
  161. }
  162. /**
  163. * Prepare and sanitise the table prior to saving.
  164. */
  165. protected function _prepareTable(&$table)
  166. {
  167. }
  168. /**
  169. * Method to delete links.
  170. *
  171. * @param array An array of link ids.
  172. * @return boolean Returns true on success, false on failure.
  173. */
  174. public function delete(&$pks)
  175. {
  176. // Typecast variable.
  177. $pks = (array) $pks;
  178. // Get a row instance.
  179. $table = &$this->getTable();
  180. // Iterate the items to delete each one.
  181. foreach ($pks as $i => $pk)
  182. {
  183. if ($table->load($pk))
  184. {
  185. // Access checks.
  186. $allow = $user->authorise('core.edit.state', 'com_redirect');
  187. if ($allow)
  188. {
  189. if (!$table->delete($pk))
  190. {
  191. $this->setError($table->getError());
  192. return false;
  193. }
  194. }
  195. else
  196. {
  197. // Prune items that you can't change.
  198. unset($pks[$i]);
  199. JError::raiseWarning(403, JText::_('JError_Core_Edit_State_not_permitted'));
  200. }
  201. }
  202. else
  203. {
  204. $this->setError($table->getError());
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. /**
  211. * Method to publish links.
  212. *
  213. * @param array The ids of the items to publish.
  214. * @param int The value of the published state
  215. *
  216. * @return boolean True on success.
  217. */
  218. function publish(&$pks, $value = 1)
  219. {
  220. // Initialise variables.
  221. $user = JFactory::getUser();
  222. $table = $this->getTable();
  223. $pks = (array) $pks;
  224. // Access checks.
  225. foreach ($pks as $i => $pk)
  226. {
  227. if ($table->load($pk))
  228. {
  229. $allow = $user->authorise('core.edit.state', 'com_redirect');
  230. if (!$allow)
  231. {
  232. // Prune items that you can't change.
  233. unset($pks[$i]);
  234. JError::raiseWarning(403, JText::_('JError_Core_Edit_State_not_permitted'));
  235. }
  236. }
  237. }
  238. // Attempt to change the state of the records.
  239. if (!$table->publish($pks, $value, $user->get('id'))) {
  240. $this->setError($table->getError());
  241. return false;
  242. }
  243. return true;
  244. }
  245. /**
  246. * Method to activate links.
  247. *
  248. * @param array An array of link ids.
  249. * @param string The new URL to set for the redirect.
  250. * @param string A comment for the redirect links.
  251. * @return boolean Returns true on success, false on failure.
  252. */
  253. public function activate(&$pks, $url, $comment = null)
  254. {
  255. // Initialise variables.
  256. $user = JFactory::getUser();
  257. $db = $this->getDbo();
  258. // Sanitize the ids.
  259. $pks = (array) $pks;
  260. JArrayHelper::toInteger($pks);
  261. // Populate default comment if necessary.
  262. $comment = (!empty($comment)) ? $comment : JText::sprintf('Redir_Redirected_On', JHtml::date(time()));
  263. // Access checks.
  264. if (!$user->authorise('core.edit', 'com_redirect'))
  265. {
  266. $pks = array();
  267. $this->setError(JText::_('JError_Core_Edit_not_permitted'));
  268. return false;
  269. }
  270. if (!empty($pks))
  271. {
  272. // Update the link rows.
  273. $db->setQuery(
  274. 'UPDATE `#__redirect_links`' .
  275. ' SET `new_url` = '.$db->Quote($url).', `published` = 1, `comment` = '.$db->Quote($comment) .
  276. ' WHERE `id` IN ('.implode(',', $pks).')'
  277. );
  278. $db->query();
  279. // Check for a database error.
  280. if ($error = $this->_db->getErrorMsg())
  281. {
  282. $this->setError($error);
  283. return false;
  284. }
  285. }
  286. return true;
  287. }
  288. }