PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/redirect/code/trunk/administrator/components/com_redirect/models/link.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 357 lines | 165 code | 57 blank | 135 comment | 19 complexity | 262132fc5c4e70b1ed79869fda7367a1 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: link.php 390 2010-11-05 11:35:33Z eddieajau $
  4. * @package NewLifeInIT
  5. * @subpackage com_redirect
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://www.theartofjoomla.com
  9. */
  10. defined('_JEXEC') or die('Invalid Request.');
  11. juimport('joomla.application.component.model16');
  12. juimport('joomla.database.databasequery');
  13. /**
  14. * The Redirect Link Model
  15. *
  16. * @package NewLifeInIT
  17. * @subpackage com_redirect
  18. * @version 1.0
  19. */
  20. class RedirectModelLink extends JModel16
  21. {
  22. /**
  23. * Array of items for memory caching.
  24. *
  25. * @access protected
  26. * @var array
  27. */
  28. var $_items = array();
  29. /**
  30. * Auto-populate the model state.
  31. *
  32. * @return void
  33. */
  34. protected function populateState()
  35. {
  36. // Get the application object.
  37. $app = &JFactory::getApplication();
  38. // Attempt to auto-load the link id.
  39. $linkId = (int) $app->getUserState('redirect.edit.link.id');
  40. if (!$linkId) {
  41. $linkId = (int)JRequest::getInt('l_id');
  42. }
  43. // Only set the link id if there is a value.
  44. if ($linkId) {
  45. $this->setState('link.id', $linkId);
  46. }
  47. }
  48. /**
  49. * Method to get a link item.
  50. *
  51. * @param integer The id of the link to get.
  52. * @return mixed Link data object on success, false on failure.
  53. * @since 1.0
  54. */
  55. public function &getItem($linkId = null)
  56. {
  57. // Initialize variables.
  58. $linkId = (!empty($linkId)) ? $linkId : (int)$this->getState('link.id');
  59. $false = false;
  60. // Get a link row instance.
  61. $table = &$this->getTable('Link', 'RedirectTable');
  62. // Attempt to load the row.
  63. $return = $table->load($linkId);
  64. // Check for a table object error.
  65. if ($return === false && $table->getError()) {
  66. $this->serError($table->getError());
  67. return $false;
  68. }
  69. // Check for a database error.
  70. if ($this->_db->getErrorNum()) {
  71. $this->setError($this->_db->getErrorMsg());
  72. return $false;
  73. }
  74. $value = JArrayHelper::toObject($table->getProperties(1), 'JObject');
  75. return $value;
  76. }
  77. /**
  78. * Method to get the form object.
  79. *
  80. * @param string The type of form to load (view, model).
  81. *
  82. * @return mixed JXForm object on success, false on failure.
  83. */
  84. public function &getForm($type = 'view')
  85. {
  86. // Initialize variables.
  87. $app = JFactory::getApplication();
  88. $false = false;
  89. // Get the form.
  90. juimport('jxtended.form.form');
  91. JForm::addFormPath(JPATH_COMPONENT.'/models/forms');
  92. JForm::addFieldPath(JPATH_COMPONENT.'/models/fields');
  93. $form = JForm::getInstance('link', 'jxform', true, array('array' => 'jxform'));
  94. // Check for an error.
  95. if (JError::isError($form)) {
  96. $this->setError($form->getMessage());
  97. return $false;
  98. }
  99. // Check the session for previously entered form data.
  100. $data = $app->getUserState('redirect.edit.link.data', array());
  101. // Bind the form data if present.
  102. if (!empty($data)) {
  103. $form->bind($data);
  104. }
  105. return $form;
  106. }
  107. /**
  108. * Method to publish links.
  109. *
  110. * @param array The ids of the items to publish.
  111. * @return boolean True on success.
  112. * @since 1.0
  113. */
  114. public function publish($linkId)
  115. {
  116. // Sanitize the ids.
  117. $linkId = (array) $linkId;
  118. JArrayHelper::toInteger($linkId);
  119. // Get the current user object.
  120. $user = &JFactory::getUser();
  121. // Get a link row instance.
  122. $table = &$this->getTable('Link', 'RedirectTable');
  123. // Attempt to publish the items.
  124. if (!$table->publish($linkId, 1, $user->get('id'))) {
  125. $this->setError($table->getError());
  126. return false;
  127. }
  128. return true;
  129. }
  130. /**
  131. * Method to unpublish links.
  132. *
  133. * @param array The ids of the items to unpublish.
  134. * @return boolean True on success.
  135. * @since 1.0
  136. */
  137. public function unpublish($linkId)
  138. {
  139. // Sanitize the ids.
  140. $linkId = (array) $linkId;
  141. JArrayHelper::toInteger($linkId);
  142. // Get the current user object.
  143. $user = &JFactory::getUser();
  144. // Get a link row instance.
  145. $table = &$this->getTable('Link', 'RedirectTable');
  146. // Attempt to unpublish the items.
  147. if (!$table->publish($linkId, 0, $user->get('id'))) {
  148. $this->setError($table->getError());
  149. return false;
  150. }
  151. return true;
  152. }
  153. /**
  154. * Method to archive links.
  155. *
  156. * @param array The ids of the items to unpublish.
  157. * @return boolean True on success.
  158. * @since 1.0
  159. */
  160. public function archive($linkId)
  161. {
  162. // Sanitize the ids.
  163. $linkId = (array) $linkId;
  164. JArrayHelper::toInteger($linkId);
  165. // Get the current user object.
  166. $user = &JFactory::getUser();
  167. // Get a link row instance.
  168. $table = &$this->getTable('Link', 'RedirectTable');
  169. // Attempt to unpublish the items.
  170. if (!$table->publish($linkId, 2, $user->get('id'))) {
  171. $this->setError($table->getError());
  172. return false;
  173. }
  174. return true;
  175. }
  176. /**
  177. * Method to delete links.
  178. *
  179. * @param array An array of link ids.
  180. * @return boolean Returns true on success, false on failure.
  181. * @since 1.0
  182. */
  183. public function delete($linkId)
  184. {
  185. // Sanitize the ids.
  186. $linkId = (array) $linkId;
  187. JArrayHelper::toInteger($linkId);
  188. // Get a link row instance.
  189. $table = &$this->getTable('Link', 'RedirectTable');
  190. // Iterate the links to delete each one.
  191. foreach ($linkId as $id)
  192. {
  193. $table->delete($id);
  194. }
  195. return true;
  196. }
  197. /**
  198. * Method to activate links.
  199. *
  200. * @param array An array of link ids.
  201. * @param string The new URL to set for the redirect.
  202. * @param string A comment for the redirect links.
  203. * @return boolean Returns true on success, false on failure.
  204. * @since 1.0
  205. */
  206. public function activate($linkId, $url, $comment=null)
  207. {
  208. // Sanitize the ids.
  209. $linkId = (array) $linkId;
  210. JArrayHelper::toInteger($linkId);
  211. // Populate default comment if necessary.
  212. $comment = (!empty($comment)) ? $comment : JText::sprintf('REDIRECTED_ON', JHtml::date(time()));
  213. if (!empty($linkId)) {
  214. // Implode the link ids.
  215. $linkId = implode(' OR `id` = ', $linkId);
  216. // Update the link rows.
  217. $this->_db->setQuery(
  218. 'UPDATE `#__redirect_links`' .
  219. ' SET `new_url` = '.$this->_db->Quote($url).', `published` = 1, `comment` = '.$this->_db->Quote($comment) .
  220. ' WHERE `id` ='.$linkId
  221. );
  222. $this->_db->query();
  223. // Check for a database error.
  224. if ($this->_db->getErrorNum()) {
  225. $this->setError($this->_db->getErrorMsg());
  226. return false;
  227. }
  228. }
  229. return true;
  230. }
  231. /**
  232. * Method to validate the form data.
  233. *
  234. * @param array The form data.
  235. * @return mixed Array of filtered data if valid, false otherwise.
  236. * @since 1.0
  237. */
  238. public function validate($data)
  239. {
  240. // Get the form.
  241. $form = &$this->getForm('model');
  242. // Check for an error.
  243. if ($form === false) {
  244. return false;
  245. }
  246. // Filter and validate the form data.
  247. $form->filter($data);
  248. $return = $form->validate($data);
  249. // Check for an error.
  250. if (JError::isError($return)) {
  251. $this->setError($return->getMessage());
  252. return false;
  253. }
  254. // Check the validation results.
  255. if ($return === false)
  256. {
  257. // Get the validation messages from the form.
  258. foreach ($form->getErrors() as $message) {
  259. $this->setError($message);
  260. }
  261. return false;
  262. }
  263. return $data;
  264. }
  265. /**
  266. * Method to save the form data.
  267. *
  268. * @param array The form data.
  269. * @return boolean True on success.
  270. * @since 1.0
  271. */
  272. public function save($data)
  273. {
  274. $linkId = (!empty($data['id'])) ? $data['id'] : (int)$this->getState('link.id');
  275. $isNew = true;
  276. // Get a link row instance.
  277. $table = &$this->getTable('Link', 'RedirectTable');
  278. // Load the row if saving an existing item.
  279. if ($linkId > 0) {
  280. $table->load($linkId);
  281. $isNew = false;
  282. }
  283. // Bind the data.
  284. if (!$table->bind($data)) {
  285. $this->setError(JText::sprintf('REDIRECT_LINK_BIND_FAILED', $table->getError()));
  286. return false;
  287. }
  288. // Check the data.
  289. if (!$table->check()) {
  290. $this->setError($table->getError());
  291. return false;
  292. }
  293. // Store the data.
  294. if (!$table->store()) {
  295. $this->setError($this->_db->getErrorMsg());
  296. return false;
  297. }
  298. return $table->id;
  299. }
  300. }