preparations for sef
[eventlist:eventlist.git] / models / eventlist.php
1 <?php
2 /**
3  * @version 0.9 $Id$
4  * @package Joomla
5  * @subpackage EventList
6  * @copyright (C) 2005 - 2007 Christoph Lukes
7  * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
8  */
9
10 // no direct access
11 defined('_JEXEC') or die('Restricted access');
12
13 jimport('joomla.application.component.model');
14
15 /**
16  * EventList Component EventList Model
17  *
18  * @package Joomla
19  * @subpackage EventList
20  * @since               0.9
21  */
22 class EventListModelEventList extends JModel
23 {
24         /**
25          * Events data array
26          *
27          * @var array
28          */
29         var $_data = null;
30
31         /**
32          * Events total
33          *
34          * @var integer
35          */
36         var $_total = null;
37
38         /**
39          * Pagination object
40          *
41          * @var object
42          */
43         var $_pagination = null;
44
45         /**
46          * Constructor
47          *
48          * @since 0.9
49          */
50         function __construct()
51         {
52                 parent::__construct();
53
54                 global $mainframe, $option;
55
56                 // Get the paramaters of the active menu item
57                 $menu           =& JMenu::getInstance();
58                 $item           = $menu->getActive();
59                 $params         =& $menu->getParams($item->id);
60
61                 //get the number of events from database
62                 $limit                  = JRequest::getVar('limit', $params->get('display_num'), '', 'int');
63                 $limitstart             = JRequest::getVar('limitstart', 0, '', 'int');
64
65                 $this->setState('limit', $limit);
66                 $this->setState('limitstart', $limitstart);
67
68                 // Get the filter request variables
69                 $this->setState('filter_order', JRequest::getVar('filter_order', 'a.dates'));
70                 $this->setState('filter_order_dir', JRequest::getVar('filter_order_Dir', 'ASC'));
71         }
72
73         /**
74          * Method to get the Events
75          *
76          * @access public
77          * @return array
78          */
79         function &getData( )
80         {
81                 $pop    = JRequest::getVar('pop', 0, '', 'int');
82
83                 // Lets load the content if it doesn't already exist
84                 if (empty($this->_data))
85                 {
86                         $query = $this->_buildQuery();
87
88                         if ($pop) {
89                                 $this->_data = $this->_getList( $query );
90                         } else {
91                                 $this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
92                         }
93                 }
94
95                 return $this->_data;
96         }
97
98         /**
99          * Total nr of events
100          *
101          * @access public
102          * @return integer
103          */
104         function getTotal()
105         {
106                 // Lets load the total nr if it doesn't already exist
107                 if (empty($this->_total))
108                 {
109                         $query = $this->_buildQuery();
110                         $this->_total = $this->_getListCount($query);
111                 }
112
113                 return $this->_total;
114         }
115
116         /**
117          * Method to get a pagination object for the events
118          *
119          * @access public
120          * @return integer
121          */
122         function getPagination()
123         {
124                 // Lets load the content if it doesn't already exist
125                 if (empty($this->_pagination))
126                 {
127                         jimport('joomla.html.pagination');
128                         $this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
129                 }
130
131                 return $this->_pagination;
132         }
133
134         /**
135          * Build the query
136          *
137          * @access private
138          * @return string
139          */
140         function _buildQuery()
141         {
142                 // Get the WHERE and ORDER BY clauses for the query
143                 $where          = $this->_buildContentWhere();
144                 $orderby        = $this->_buildContentOrderBy();
145
146                 //Get Events from Database
147                 $query = 'SELECT a.id, a.dates, a.enddates, a.times, a.endtimes, a.title, a.locid, a.datdescription, l.club, l.city, l.state, l.url, c.catname, c.id AS catid,'
148                                 . ' CASE WHEN CHAR_LENGTH(a.title) THEN CONCAT_WS(":", a.id, a.title) ELSE a.id END as slug'
149                                 . ' FROM #__eventlist_events AS a'
150                                 . ' LEFT JOIN #__eventlist_venues AS l ON l.id = a.locid'
151                                 . ' LEFT JOIN #__eventlist_categories AS c ON c.id = a.catsid'
152                                 . $where
153                                 . $orderby
154                                 ;
155
156                 return $query;
157         }
158
159         /**
160          * Build the order clause
161          *
162          * @access private
163          * @return string
164          */
165         function _buildContentOrderBy()
166         {
167                 global $mainframe, $option;
168
169                 $filter_order           = $this->getState('filter_order');
170                 $filter_order_dir       = $this->getState('filter_order_dir');
171
172                 $orderby        = ' ORDER BY '.$filter_order.' '.$filter_order_Dir.', a.dates, a.times';
173
174                 return $orderby;
175         }
176
177         /**
178          * Build the where clause
179          *
180          * @access private
181          * @return string
182          */
183         function _buildContentWhere()
184         {
185                 $user           =& JFactory::getUser();
186                 $gid            = (int) $user->get('aid');
187
188                 // Get the paramaters of the active menu item           
189                 $menu           =& JMenu::getInstance();
190                 $item           = $menu->getActive();
191                 $params         =& $menu->getParams($item->id);
192
193                 // First thing we need to do is to select only published events
194                 $where = ' WHERE a.published = 1';
195
196                 // Second is to only select events assigned to category the user has access to
197                 $where .= ' AND c.access <= '.$gid;
198
199                 /*
200                  * If we have a filter, and this is enabled... lets tack the AND clause
201                  * for the filter onto the WHERE clause of the content item query.
202                  */
203                 if ($params->get('filter'))
204                 {
205                         $filter                 = JRequest::getVar('filter', '', 'request');
206                         $filter_type    = JRequest::getVar('filter_type', '', 'request');
207
208                         if ($filter)
209                         {
210                                 // clean filter variables
211                                 $filter                 = JString::strtolower($filter);
212                                 $filter_type    = JString::strtolower($filter_type);
213
214                                 switch ($filter_type)
215                                 {
216                                         case 'title' :
217                                                 $where .= ' AND LOWER( a.title ) LIKE "%'.$filter.'%"';
218                                                 break;
219
220                                         case 'venue' :
221                                                 $where .= ' AND LOWER( l.club ) LIKE "%'.$filter.'%"';
222                                                 break;
223
224                                         case 'city' :
225                                                 $where .= ' AND LOWER( l.city ) LIKE "%'.$filter.'%"';
226                                                 break;
227                                 }
228                         }
229                 }
230                 return $where;
231         }
232 }
233 ?>