/admin/models/currencies.php

https://bitbucket.org/nathanphan/joomla_zjdonation_custom · PHP · 284 lines · 143 code · 51 blank · 90 comment · 24 complexity · 1d576c9e5573a602115bd17147cf5b62 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author Nguyen Dinh Luan
  5. * @package Joomla!
  6. * @subpackage ZJ_Donation
  7. * @copyright Copyright (C) 2008 - 2011 by Joomseller Solutions. All rights reserved.
  8. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL version 3
  9. */
  10. // no direct access
  11. defined('_JEXEC') or die('Restricted access');
  12. jimport('joomla.application.component.model');
  13. /**
  14. * ZJ_Donation Component - Currencies Model.
  15. * @package ZJ_Donation
  16. * @subpackage Model
  17. */
  18. class ZJ_DonationModelCurrencies extends JModel {
  19. /** @var array Currency array */
  20. var $_data = null;
  21. /** @var int Currency total */
  22. var $_total = null;
  23. /** @var object Pagination object */
  24. var $_pagination = null;
  25. /**
  26. * Constructor.
  27. */
  28. function __construct() {
  29. global $mainframe, $option;
  30. parent::__construct();
  31. // get the pagination request variables
  32. $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
  33. $limitstart = $mainframe->getUserStateFromRequest($option . '.currencies.limitstart', 'limitstart', 0, 'int');
  34. // in case limit has been changed, adjust limitstart accordingly
  35. $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
  36. $this->setState('limit', $limit);
  37. $this->setState('limitstart', $limitstart);
  38. }
  39. /****************************************************************
  40. * Get data for presentation.
  41. ***************************************************************/
  42. /**
  43. * Method to get currencies item data.
  44. * @access public
  45. * @return array
  46. */
  47. function getData() {
  48. // Lets load the data if it doesn't already exist
  49. if (empty($this->_data)) {
  50. $query = $this->_buildQuery();
  51. $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
  52. }
  53. return $this->_data;
  54. }
  55. /**
  56. * Method to get the total number of currency items.
  57. * @access public
  58. * @return integer
  59. */
  60. function getTotal() {
  61. // Lets load the total if it doesn't already exist
  62. if (empty($this->_total)) {
  63. $query = $this->_buildQuery();
  64. $this->_total = $this->_getListCount($query);
  65. }
  66. return $this->_total;
  67. }
  68. /**
  69. * Method to get a pagination object for the currencies.
  70. * @access public
  71. * @return integer
  72. */
  73. function getPagination() {
  74. // Lets load the pagination if it doesn't already exist
  75. if (empty($this->_pagination)) {
  76. jimport('joomla.html.pagination');
  77. $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
  78. }
  79. return $this->_pagination;
  80. }
  81. /**
  82. * Method to get query string.
  83. * @access private
  84. * @return string
  85. */
  86. function _buildQuery() {
  87. // Get the WHERE and ORDER BY clauses for the query
  88. $where = $this->_buildQueryWhere();
  89. $orderby = $this->_buildQueryOrderBy();
  90. $query = 'SELECT a.* '
  91. . ' FROM #__zj_donation_currencies AS a'
  92. . $where
  93. . $orderby
  94. ;
  95. return $query;
  96. }
  97. /**
  98. * Method to build WHERE clause for the query.
  99. * @access private
  100. * @return string
  101. */
  102. function _buildQueryWhere() {
  103. global $mainframe, $option;
  104. $db = $this->_db;
  105. $filter_state = $mainframe->getUserStateFromRequest($option . '.currencies.filter_state', 'filter_state', '', 'word');
  106. $search = $mainframe->getUserStateFromRequest($option . '.currencies.search', 'search', '', 'string');
  107. $search = JString::strtolower($search);
  108. $where = array();
  109. // filter by state of currencies
  110. if ($filter_state) {
  111. if ($filter_state == 'P') {
  112. $where[] = 'a.published = 1';
  113. } else if ($filter_state == 'U') {
  114. $where[] = 'a.published = 0';
  115. }
  116. }
  117. // filter by title of currencies
  118. if ($search) {
  119. $where[] = 'LOWER(a.title) LIKE ' . $db->Quote('%' . $db->getEscaped($search, true) . '%', false);
  120. }
  121. // build the WHERE string
  122. $where = (count($where) ? ' WHERE ' . implode(' AND ', $where) : '');
  123. return $where;
  124. }
  125. /**
  126. * Method to get ORDER BY clause for the query.
  127. * @access private
  128. * @return string
  129. */
  130. function _buildQueryOrderBy() {
  131. global $mainframe, $option;
  132. $filter_order = $mainframe->getUserStateFromRequest($option . 'currencies.filter_order', 'filter_order', 'a.ordering', 'cmd');
  133. $filter_order_Dir = $mainframe->getUserStateFromRequest($option . 'currencies.filter_order_Dir', 'filter_order_Dir', '', 'word');
  134. if ($filter_order == 'a.ordering') {
  135. $orderby = ' ORDER BY a.ordering ' . $filter_order_Dir;
  136. } else {
  137. $orderby = ' ORDER BY ' . $filter_order . ' ' . $filter_order_Dir . ', a.ordering ';
  138. }
  139. return $orderby;
  140. }
  141. /****************************************************************
  142. * Manipulate data.
  143. ***************************************************************/
  144. /**
  145. * Overwrite getTable method.
  146. */
  147. function getTable($name = 'currency', $prefix = 'Table', $options = array()) {
  148. return parent::getTable($name, $prefix, $options);
  149. }
  150. /**
  151. * Method to (un)publish a currency.
  152. * @access public
  153. * @return boolean True on success
  154. */
  155. function publish($cid = array(), $publish = 1) {
  156. $user = &JFactory::getUser();
  157. if (count($cid)) {
  158. JArrayHelper::toInteger($cid);
  159. $cids = implode(', ', $cid);
  160. $query = 'UPDATE #__zj_donation_currencies'
  161. . ' SET published = ' . (int) $publish
  162. . ' WHERE id IN (' . $cids . ')'
  163. . ' AND (checked_out = 0 OR (checked_out = ' . (int) $user->get('id') . ' ))'
  164. ;
  165. $this->_db->setQuery($query);
  166. if (!$this->_db->query()) {
  167. $this->setError($this->_db->getErrorMsg());
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. /**
  174. * Method to remove a currency.
  175. * @access public
  176. * @param $cid
  177. * @return boolean True on success
  178. */
  179. function delete($cid = array()) {
  180. if (count($cid)) {
  181. JArrayHelper::toInteger($cid);
  182. $cids = implode(', ', $cid);
  183. $query = 'DELETE FROM #__zj_donation_currencies'
  184. . ' WHERE id IN (' . $cids . ')';
  185. ;
  186. $this->_db->setQuery($query);
  187. if (!$this->_db->query()) {
  188. $this->setError($this->_db->getErrorMsg());
  189. return false;
  190. }
  191. }
  192. return true;
  193. }
  194. /**
  195. * Method to move a currency.
  196. * @access public
  197. * @return boolean True on success
  198. */
  199. function move($direction) {
  200. $cid = JRequest::getVar('cid', array(0), 'post', 'array');
  201. $id = $cid[0];
  202. $row = &$this->getTable();
  203. if (!$row->load($id)) {
  204. $this->setError($this->_db->getErrorMsg());
  205. return false;
  206. }
  207. if (!$row->move($direction)) {
  208. $this->setError($this->_db->getErrorMsg());
  209. return false;
  210. }
  211. return true;
  212. }
  213. /**
  214. * Method to move a currency.
  215. * @access public
  216. * @return boolean True on success
  217. */
  218. function saveorder($cid = array(), $order = array()) {
  219. $row = &$this->getTable();
  220. // update ordering values
  221. for ($i = 0, $n = count($cid); $i < $n; $i++) {
  222. $row->load((int) $cid[$i]);
  223. if ($row->ordering != $order[$i]) {
  224. $row->ordering = $order[$i];
  225. if (!$row->store()) {
  226. $this->setError($this->_db->getErrorMsg());
  227. return false;
  228. }
  229. }
  230. }
  231. return true;
  232. }
  233. }