PageRenderTime 74ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/src/components/com_coupon/models/coupon.php

http://kak.googlecode.com/
PHP | 171 lines | 102 code | 21 blank | 48 comment | 14 complexity | 9cea379e6651abe66d38c52ae79b3a0c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * @version $Id: archive.php 19343 2010-11-03 18:12:02Z ian $
  4. * @package Joomla
  5. * @subpackage Content
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. // Check to ensure this file is included in Joomla!
  15. defined('_JEXEC') or die( 'Restricted access' );
  16. jimport('joomla.application.component.model');
  17. /**
  18. * Content Component Archive Model
  19. *
  20. * @package Joomla
  21. * @subpackage Content
  22. * @since 1.5
  23. */
  24. class CouponModelCoupon extends JModel
  25. {
  26. /**
  27. * Article list array
  28. *
  29. * @var array
  30. */
  31. var $_data = array();
  32. /**
  33. * Article total
  34. *
  35. * @var integer
  36. */
  37. var $_total = array();
  38. /**
  39. * Method to get the archived article list
  40. *
  41. * @access public
  42. * @return array
  43. */
  44. function getData()
  45. {
  46. global $mainframe;
  47. // Lets load the content if it doesn't already exist
  48. if (empty($this->_data))
  49. {
  50. $query = $this->_buildQuery();
  51. $this->_data = $this->_getList($query);
  52. }
  53. return $this->_data;
  54. }
  55. /**
  56. * Method to get the total number of content items for the frontpage
  57. *
  58. * @access public
  59. * @return integer
  60. */
  61. function getTotal()
  62. {
  63. // Lets load the content if it doesn't already exist
  64. if (empty($this->_total))
  65. {
  66. $query = $this->_buildQuery();
  67. $this->_total = $this->_getListCount($query);
  68. }
  69. return $this->_total;
  70. }
  71. // JModel override to add alternating value for $odd
  72. function &_getList( $query, $limitstart=0, $limit=0 )
  73. {
  74. $result =& parent::_getList($query, $limitstart, $limit);
  75. if ($result == null) {
  76. $result = array();
  77. }
  78. $odd = 1;
  79. foreach ($result as $k => $row) {
  80. $result[$k]->odd = $odd;
  81. $odd = 1 - $odd;
  82. }
  83. return $result;
  84. }
  85. function _buildQuery()
  86. {
  87. global $mainframe;
  88. $db =& JFactory::getDBO();
  89. $params = &$mainframe->getParams();
  90. $session = JFactory::getSession();
  91. $catid = $db->getEscaped(JRequest::getInt("coupon_cat", 0));
  92. if ($catid == 0) {
  93. $catid = $this->getGroupCatFromCookie();
  94. if ($catid == null || !CouponHelperCoupon::isCouponCatEnable($catid)) {
  95. jimport("site.constants");
  96. $catid = JSiteConstants::LOCATE_CAT_DANANG;
  97. }
  98. }
  99. $industryid = JRequest::getInt('industryid', 0);
  100. // Get the WHERE and ORDER BY clauses for the query
  101. $where = 'HAVING (cat_id = ' . $catid . ' or cat_id = 0) AND deal.status = 1 ';
  102. if($industryid != 0) {
  103. $where = $where.'AND industry_id = '.$industryid;
  104. }
  105. $orderby = " ORDER BY deal.priority desc ";
  106. $query = 'SELECT DISTINCT deal.*, DATE_FORMAT(MIN(satisfy_time), "%e %m %Y %H %i %s") AS satisfy_time,DATE_FORMAT(dead_line, "%e %m %Y %H %i %s") AS dead_line, dead_line - now() < 0 as time_out, SUM(deal_option.counter) AS counter, deal.min_order AS min_order, SUM(deal_option.max_order) AS max_order, deal_option.deal_id' .
  107. ', CASE WHEN CHAR_LENGTH(deal.alias) THEN CONCAT_WS(":", deal.id, deal.alias) ELSE deal.id END as slug' .
  108. ' FROM pm_deal deal LEFT JOIN pm_deal_option deal_option ON deal.id = deal_option.deal_id
  109. GROUP BY deal_option.deal_id '.
  110. $where.
  111. $orderby;
  112. return $query;
  113. }
  114. function getOptionsOfDeal($dealId) {
  115. $query = $this->_buildOptionsOfDealQuery($dealId);
  116. $result =& parent::_getList($query);
  117. return $result;
  118. }
  119. function _buildOptionsOfDealQuery($dealId) {
  120. global $mainframe;
  121. $params = &$mainframe->getParams();
  122. $industryid = JRequest::getInt('industryid', 0);
  123. // Get the WHERE and ORDER BY clauses for the query
  124. $where = ' WHERE deal_id='.$dealId;
  125. if($industryid != 0) {
  126. $where = $where.'AND industry_id = '.$industryid;
  127. }
  128. $orderby = " ORDER BY ordering ";
  129. $limit = "";
  130. $query = 'SELECT * FROM #__deal_option '.
  131. $where.
  132. $orderby.
  133. $limit;
  134. return $query;
  135. }
  136. function getGroupCatFromCookie() {
  137. $result = null;
  138. jimport('joomla.utilities.utility');
  139. $hash = JUtility::getHash('JCOUPON_CATEGORY_ID');
  140. if ($str = JRequest::getString($hash, '', 'cookie', JREQUEST_ALLOWRAW | JREQUEST_NOTRIM))
  141. {
  142. jimport('joomla.utilities.simplecrypt');
  143. //Create the encryption key, apply extra hardening using the user agent string
  144. $key = JUtility::getHash('JCOUPON_CATEGORY_ID');
  145. $crypt = new JSimpleCrypt($key);
  146. $result = $crypt->decrypt($str);
  147. }
  148. return $result;
  149. }
  150. }