PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_banners/tables/banner.php

https://bitbucket.org/asosso/joomla25
PHP | 327 lines | 196 code | 34 blank | 97 comment | 37 complexity | 4da9913d61c177e4f9a1d20bf9b2ea30 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE.txt
  5. */
  6. // No direct access.
  7. defined('_JEXEC') or die;
  8. /**
  9. * Banner table
  10. *
  11. * @package Joomla.Administrator
  12. * @subpackage com_banners
  13. * @since 1.5
  14. */
  15. class BannersTableBanner extends JTable
  16. {
  17. /**
  18. * Constructor
  19. *
  20. * @since 1.5
  21. */
  22. function __construct(&$_db)
  23. {
  24. parent::__construct('#__banners', 'id', $_db);
  25. $date = JFactory::getDate();
  26. $this->created = $date->toSql();
  27. }
  28. function clicks()
  29. {
  30. $query = 'UPDATE #__banners'
  31. . ' SET clicks = (clicks + 1)'
  32. . ' WHERE id = ' . (int) $this->id
  33. ;
  34. $this->_db->setQuery($query);
  35. $this->_db->query();
  36. }
  37. /**
  38. * Overloaded check function
  39. *
  40. * @return boolean
  41. * @see JTable::check
  42. * @since 1.5
  43. */
  44. function check()
  45. {
  46. // Set name
  47. $this->name = htmlspecialchars_decode($this->name, ENT_QUOTES);
  48. // Set alias
  49. $this->alias = JApplication::stringURLSafe($this->alias);
  50. if (empty($this->alias)) {
  51. $this->alias = JApplication::stringURLSafe($this->name);
  52. }
  53. // Check the publish down date is not earlier than publish up.
  54. if ($this->publish_down > $this->_db->getNullDate() && $this->publish_down < $this->publish_up) {
  55. $this->setError(JText::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));
  56. return false;
  57. }
  58. // Set ordering
  59. if ($this->state < 0) {
  60. // Set ordering to 0 if state is archived or trashed
  61. $this->ordering = 0;
  62. } elseif (empty($this->ordering)) {
  63. // Set ordering to last if ordering was 0
  64. $this->ordering = self::getNextOrder($this->_db->quoteName('catid').'=' . $this->_db->Quote($this->catid).' AND state>=0');
  65. }
  66. return true;
  67. }
  68. /**
  69. * Overloaded bind function
  70. *
  71. * @param array $hash named array
  72. * @return null|string null is operation was satisfactory, otherwise returns an error
  73. * @see JTable:bind
  74. * @since 1.5
  75. */
  76. public function bind($array, $ignore = array())
  77. {
  78. if (isset($array['params']) && is_array($array['params'])) {
  79. $registry = new JRegistry();
  80. $registry->loadArray($array['params']);
  81. if((int) $registry->get('width', 0) < 0){
  82. $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_NEGATIVE_NOT_PERMITTED', JText::_('COM_BANNERS_FIELD_WIDTH_LABEL')));
  83. return false;
  84. }
  85. if((int) $registry->get('height', 0) < 0){
  86. $this->setError(JText::sprintf('JLIB_DATABASE_ERROR_NEGATIVE_NOT_PERMITTED', JText::_('COM_BANNERS_FIELD_HEIGHT_LABEL')));
  87. return false;
  88. }
  89. // Converts the width and height to an absolute numeric value:
  90. $width = abs((int) $registry->get('width', 0));
  91. $height = abs((int) $registry->get('height', 0));
  92. // Sets the width and height to an empty string if = 0
  93. $registry->set('width', ($width ? $width : ''));
  94. $registry->set('height', ($height ? $height : ''));
  95. $array['params'] = (string)$registry;
  96. }
  97. if (isset($array['imptotal'])) {
  98. $array['imptotal'] = abs((int) $array['imptotal']);
  99. }
  100. return parent::bind($array, $ignore);
  101. }
  102. /**
  103. * method to store a row
  104. *
  105. * @param boolean $updateNulls True to update fields even if they are null.
  106. */
  107. function store($updateNulls = false)
  108. {
  109. if (empty($this->id))
  110. {
  111. $purchase_type = $this->purchase_type;
  112. if ($purchase_type < 0 && $this->cid)
  113. {
  114. $client = JTable::getInstance('Client', 'BannersTable');
  115. $client->load($this->cid);
  116. $purchase_type = $client->purchase_type;
  117. }
  118. if ($purchase_type < 0)
  119. {
  120. $params = JComponentHelper::getParams('com_banners');
  121. $purchase_type = $params->get('purchase_type');
  122. }
  123. switch($purchase_type)
  124. {
  125. case 1:
  126. $this->reset=$this->_db->getNullDate();
  127. break;
  128. case 2:
  129. $date = JFactory::getDate('+1 year '.date('Y-m-d', strtotime('now')));
  130. $reset = $this->_db->Quote($date->toSql());
  131. break;
  132. case 3:
  133. $date = JFactory::getDate('+1 month '.date('Y-m-d', strtotime('now')));
  134. $reset = $this->_db->Quote($date->toSql());
  135. break;
  136. case 4:
  137. $date = JFactory::getDate('+7 day '.date('Y-m-d', strtotime('now')));
  138. $reset = $this->_db->Quote($date->toSql());
  139. break;
  140. case 5:
  141. $date = JFactory::getDate('+1 day '.date('Y-m-d', strtotime('now')));
  142. $reset = $this->_db->Quote($date->toSql());
  143. break;
  144. }
  145. // Store the row
  146. parent::store($updateNulls);
  147. }
  148. else
  149. {
  150. // Get the old row
  151. $oldrow = JTable::getInstance('Banner', 'BannersTable');
  152. if (!$oldrow->load($this->id) && $oldrow->getError())
  153. {
  154. $this->setError($oldrow->getError());
  155. }
  156. // Verify that the alias is unique
  157. $table = JTable::getInstance('Banner', 'BannersTable');
  158. if ($table->load(array('alias'=>$this->alias, 'catid'=>$this->catid)) && ($table->id != $this->id || $this->id==0)) {
  159. $this->setError(JText::_('COM_BANNERS_ERROR_UNIQUE_ALIAS'));
  160. return false;
  161. }
  162. // Store the new row
  163. parent::store($updateNulls);
  164. // Need to reorder ?
  165. if ($oldrow->state>=0 && ($this->state < 0 || $oldrow->catid != $this->catid))
  166. {
  167. // Reorder the oldrow
  168. $this->reorder($this->_db->quoteName('catid').'=' . $this->_db->Quote($oldrow->catid).' AND state>=0');
  169. }
  170. }
  171. return count($this->getErrors())==0;
  172. }
  173. /**
  174. * Method to set the publishing state for a row or list of rows in the database
  175. * table. The method respects checked out rows by other users and will attempt
  176. * to checkin rows that it can after adjustments are made.
  177. *
  178. * @param mixed An optional array of primary key values to update. If not
  179. * set the instance property value is used.
  180. * @param integer The publishing state. eg. [0 = unpublished, 1 = published, 2=archived, -2=trashed]
  181. * @param integer The user id of the user performing the operation.
  182. * @return boolean True on success.
  183. * @since 1.6
  184. */
  185. public function publish($pks = null, $state = 1, $userId = 0)
  186. {
  187. // Initialise variables.
  188. $k = $this->_tbl_key;
  189. // Sanitize input.
  190. JArrayHelper::toInteger($pks);
  191. $userId = (int) $userId;
  192. $state = (int) $state;
  193. // If there are no primary keys set check to see if the instance key is set.
  194. if (empty($pks))
  195. {
  196. if ($this->$k) {
  197. $pks = array($this->$k);
  198. }
  199. // Nothing to set publishing state on, return false.
  200. else {
  201. $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  202. return false;
  203. }
  204. }
  205. // Get an instance of the table
  206. $table = JTable::getInstance('Banner', 'BannersTable');
  207. // For all keys
  208. foreach ($pks as $pk)
  209. {
  210. // Load the banner
  211. if(!$table->load($pk))
  212. {
  213. $this->setError($table->getError());
  214. }
  215. // Verify checkout
  216. if($table->checked_out==0 || $table->checked_out==$userId)
  217. {
  218. // Change the state
  219. $table->state = $state;
  220. $table->checked_out=0;
  221. $table->checked_out_time=$this->_db->getNullDate();
  222. // Check the row
  223. $table->check();
  224. // Store the row
  225. if (!$table->store())
  226. {
  227. $this->setError($table->getError());
  228. }
  229. }
  230. }
  231. return count($this->getErrors())==0;
  232. }
  233. /**
  234. * Method to set the sticky state for a row or list of rows in the database
  235. * table. The method respects checked out rows by other users and will attempt
  236. * to checkin rows that it can after adjustments are made.
  237. *
  238. * @param mixed An optional array of primary key values to update. If not
  239. * set the instance property value is used.
  240. * @param integer The sticky state. eg. [0 = unsticked, 1 = sticked]
  241. * @param integer The user id of the user performing the operation.
  242. * @return boolean True on success.
  243. * @since 1.6
  244. */
  245. public function stick($pks = null, $state = 1, $userId = 0)
  246. {
  247. // Initialise variables.
  248. $k = $this->_tbl_key;
  249. // Sanitize input.
  250. JArrayHelper::toInteger($pks);
  251. $userId = (int) $userId;
  252. $state = (int) $state;
  253. // If there are no primary keys set check to see if the instance key is set.
  254. if (empty($pks))
  255. {
  256. if ($this->$k) {
  257. $pks = array($this->$k);
  258. }
  259. // Nothing to set publishing state on, return false.
  260. else {
  261. $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  262. return false;
  263. }
  264. }
  265. // Get an instance of the table
  266. $table = JTable::getInstance('Banner', 'BannersTable');
  267. // For all keys
  268. foreach ($pks as $pk)
  269. {
  270. // Load the banner
  271. if(!$table->load($pk))
  272. {
  273. $this->setError($table->getError());
  274. }
  275. // Verify checkout
  276. if($table->checked_out==0 || $table->checked_out==$userId)
  277. {
  278. // Change the state
  279. $table->sticky = $state;
  280. $table->checked_out=0;
  281. $table->checked_out_time=$this->_db->getNullDate();
  282. // Check the row
  283. $table->check();
  284. // Store the row
  285. if (!$table->store())
  286. {
  287. $this->setError($table->getError());
  288. }
  289. }
  290. }
  291. return count($this->getErrors())==0;
  292. }
  293. }