/joomla_components/com_subsys/models/orders.php

https://github.com/rkshakya/RKSProjects · PHP · 191 lines · 110 code · 30 blank · 51 comment · 13 complexity · b126f53e1543377d4c4659327cd8f83d MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * @package Joomla.Tutorials
  5. * @subpackage Components
  6. * @license GNU/GPL
  7. * @author Ravi Shakya
  8. */
  9. // No direct access
  10. defined( '_JEXEC' ) or die( 'Restricted access' );
  11. jimport( 'joomla.application.component.model' );
  12. /**
  13. *
  14. * @package Joomla.Tutorials
  15. * @subpackage Components
  16. */
  17. class SubsysModelOrders extends JModel
  18. {
  19. /**
  20. *
  21. * @var array
  22. */
  23. var $_data;
  24. var $_subscribers;
  25. /**
  26. * Items total
  27. * @var integer
  28. */
  29. var $_total = null;
  30. /**
  31. * Pagination object
  32. * @var object
  33. */
  34. var $_pagination = null;
  35. var $_count = null;
  36. var $_list = null;
  37. var $_subcode = null;
  38. var $_timeperiod = null;
  39. function __construct()
  40. {
  41. parent::__construct();
  42. $db =& JFactory::getDBO();
  43. $mainframe = JFactory::getApplication();
  44. global $option;
  45. $this->_subcode = JRequest::getVar('sub_code', 0, '', 'int');
  46. $this->_timeperiod = JRequest::getVar('time_period', 0, '', 'int');
  47. //dump($this->_subcode, "SUBCODE");
  48. // Get pagination request variables
  49. $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
  50. $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
  51. // Get the user state
  52. $filter_order = $mainframe->getUserStateFromRequest($option.'filter_order','filter_order', 'order_date');
  53. $filter_order_Dir = $mainframe->getUserStateFromRequest($option.'filter_order_Dir','filter_order_Dir', 'DESC');
  54. // In case limit has been changed, adjust it
  55. $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
  56. $this->setState('limit', $limit);
  57. $this->setState('limitstart', $limitstart);
  58. // Build the list array for use in the layout
  59. $lists['order'] = $filter_order;
  60. $lists['order_Dir'] = $filter_order_Dir;
  61. $lists['subcode']= $this->_subcode;
  62. $lists['timeperiod']= $this->_timeperiod;
  63. $this->_lists = $lists;
  64. }
  65. /**
  66. * Returns the query
  67. * @return string The query to be used to retrieve the rows from the database
  68. */
  69. function _buildQuery()
  70. {
  71. $query = ' select O.order_id, O.order_code, O.order_date, O.order_title, O.order_invno, O.order_invamt, O.order_paid, S.num_issues, S.num_copies,'
  72. . ' S.pub_code, P.pub_code, P.pub_name, S.subscription_title, S.issue_from, S.issue_to, S.start_date, S.exp_date, O.cdate, O.mdate, SU.sub_name, O.sub_code '
  73. . ' from sms_orders O, sms_subscriptions S, sms_publications P, sms_subscribers SU'
  74. . ' where O.order_code = S.order_code'
  75. . ' and O.order_id = S.order_id'
  76. . ' and O.sub_code = S.sub_code'
  77. . ' and P.pub_code = S.pub_code'
  78. . ' and SU.sub_code = O.sub_code';
  79. if($this->_subcode){ $query .= ' AND O.sub_code ='. $this->_subcode; }
  80. if($this->_timeperiod){ $query .= ' AND S.exp_date BETWEEN CURDATE() AND DATE_ADD( CURDATE( ) , INTERVAL '. $this->_timeperiod . ' DAY )' ; }
  81. $query .= $this-> _buildQueryOrderBy();
  82. //print $query;
  83. //dump($this->_subcode, "SUB");
  84. //dump($query, "QUERY");
  85. return $query;
  86. }
  87. function _buildQueryOrderBy()
  88. {
  89. global $mainframe, $option;
  90. // Array of allowable order fields
  91. $orders = array('order_code', 'order_date', 'sub_name', 'order_invno', 'order_invamt', 'pub_name', 'num_copies', 'num_issues', 'start_date', 'exp_date');
  92. // Get the order field and direction, default order field
  93. // is 'ordering', default direction is ascending
  94. $filter_order = $mainframe->getUserStateFromRequest($option.'filter_order', 'filter_order', 'order_date');
  95. $filter_order_Dir = strtoupper($mainframe->getUserStateFromRequest($option.'filter_order_Dir', 'filter_order_Dir', 'DESC'));
  96. // Validate the order direction, must be ASC or DESC
  97. if ($filter_order_Dir != 'ASC' && $filter_order_Dir != 'DESC')
  98. {
  99. $filter_order_Dir = 'ASC';
  100. }
  101. // If order column is unknown use the default
  102. if (!in_array($filter_order, $orders))
  103. {
  104. $filter_order = 'order_date';
  105. }
  106. $orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
  107. if ($filter_order != 'order_date')
  108. {
  109. $orderby .= ' , order_date ';
  110. }
  111. // Return the ORDER BY clause
  112. return $orderby;
  113. }
  114. /**
  115. * Retrieves the data
  116. * @return array Array of objects containing the data from the database
  117. */
  118. function getData()
  119. {
  120. // Lets load the data if it doesn't already exist
  121. if (empty( $this->_data ))
  122. {
  123. $query = $this->_buildQuery();
  124. $this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
  125. $this->_lists['subcount'] = $this->_getListCount($query);
  126. }
  127. //dump($this->_data, "RESULTS");
  128. return $this->_data;
  129. }
  130. function getLists(){
  131. return $this->_lists;
  132. }
  133. function getSubscribers(){
  134. $querySub = ' SELECT sub_code, sub_name FROM sms_subscribers order by sub_name';
  135. $this->_subscribers = $this->_getList($querySub);
  136. return $this->_subscribers;
  137. }
  138. function getSummary(){
  139. $querySummary = ' SELECT distinct order_code FROM sms_orders WHERE sub_code = '. $this->_subcode;
  140. $this->_count = $this->_getListCount($querySummary);
  141. return $this->_count;
  142. }
  143. function getTotal()
  144. {
  145. // Load the content if it doesn't already exist
  146. if (empty($this->_total)) {
  147. $query = $this->_buildQuery();
  148. $this->_total = $this->_getListCount($query);
  149. }
  150. return $this->_total;
  151. }
  152. function getPagination()
  153. {
  154. // Load the content if it doesn't already exist
  155. if (empty($this->_pagination)) {
  156. jimport('joomla.html.pagination');
  157. $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
  158. }
  159. return $this->_pagination;
  160. }
  161. }