some module work, thanks rheinig
[eventlist:version1-0.git] / modules / mod_eventlist_wide / helper.php
1 <?php
2 /**
3  * @version 1.0 $Id$
4  * @package Joomla
5  * @subpackage EventList Wide Module
6  * @copyright (C) 2005 - 2008 Christoph Lukes
7  * @license GNU/GPL, see LICENCE.php
8  * EventList is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License 2
10  * as published by the Free Software Foundation.
11
12  * EventList is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16
17  * You should have received a copy of the GNU General Public License
18  * along with EventList; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 // no direct access
23 defined('_JEXEC') or die('Restricted access');
24
25 /**
26  * EventList Modulewide helper
27  *
28  * @package Joomla
29  * @subpackage EventList Wide Module
30  * @since               1.0
31  */
32 class modEventListwideHelper
33 {
34
35         /**
36          * Method to get the events
37          *
38          * @access public
39          * @return array
40          */
41         function getList(&$params)
42         {
43                 global $mainframe;
44
45                 $db                     =& JFactory::getDBO();
46                 $user           =& JFactory::getUser();
47                 $user_gid       = (int) $user->get('aid');
48
49                 //all upcoming events
50                 if ($params->get( 'type' ) == 1) {
51                         $where = ' WHERE a.dates > CURDATE()';
52                 //      $where = ' WHERE a.published = 1';
53                         $order = ' ORDER BY a.dates, a.times';
54                 }
55                 
56                 //archived events only
57                 if ($params->get( 'type' ) == 2) {
58                         $where = ' WHERE a.published = -1';
59                         $order = ' ORDER BY a.dates DESC, a.times DESC';
60                 }
61                 
62                 //currently running events only
63                 if ($params->get( 'type' ) == 3) {
64                         $where = ' WHERE a.published = 1';                      
65                         $where .= ' AND ( a.dates = CURDATE()';
66                         $where .= ' OR ( a.enddates >= CURDATE() AND a.dates <= CURDATE() ))';
67                         $order = ' ORDER BY a.dates, a.times';
68                 }
69
70                 //clean parameter data
71                 $catid  = trim( $params->get('catid') );
72                 $venid  = trim( $params->get('venid') );
73                 $state  = JString::strtolower(trim( $params->get('stateloc') ) );
74
75                 //Build category selection query statement
76                 if ($catid)
77                 {
78                         $ids = explode( ',', $catid );
79                         JArrayHelper::toInteger( $ids );
80                         $categories = ' AND (c.id=' . implode( ' OR c.id=', $ids ) . ')';
81                 }
82                 
83                 //Build venue selection query statement
84                 if ($venid)
85                 {
86                         $ids = explode( ',', $venid );
87                         JArrayHelper::toInteger( $ids );
88                         $venues = ' AND (l.id=' . implode( ' OR l.id=', $ids ) . ')';
89                 }
90                 
91                 //Build state selection query statement
92                 if ($state)
93                 {
94                         $rawstate = explode( ',', $state );
95                         
96                         foreach ($rawstate as $val)
97                         {
98                                 if ($val) {
99                                         $states[] = '"'.trim($db->getEscaped($val)).'"';
100                                 }
101                         }
102         
103                         JArrayHelper::toString( $states );
104                         $stat = ' AND (LOWER(l.state)='.implode(' OR LOWER(l.state)=',$states).')';
105                 }
106                 
107                 //perform select query
108                 $query = 'SELECT a.title, a.dates, a.enddates, a.datdescription, a.times, a.endtimes, a.datimage, l.venue, l.state, l.locimage, l.city, c.catname,'
109                                 .' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug,'
110                                 .' CASE WHEN CHAR_LENGTH(l.alias) THEN CONCAT_WS(\':\', l.id, l.alias) ELSE l.id END as venueslug,'
111                                 .' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END as categoryslug'
112                                 .' FROM #__eventlist_events AS a'
113                                 .' LEFT JOIN #__eventlist_venues AS l ON l.id = a.locid'
114                                 .' LEFT JOIN #__eventlist_categories AS c ON c.id = a.catsid'
115                                 . $where
116                                 .' AND c.access <= '.$user_gid
117                                 .($catid ? $categories : '')
118                                 .($venid ? $venues : '')
119                                 .($state ? $stat : '')
120                                 . $order
121                                 .' LIMIT '.(int)$params->get( 'count', '2' )
122                                 ;
123
124                 $db->setQuery($query);
125                 $rows = $db->loadObjectList();
126                 
127                 //assign datemethod value to jview
128                 jimport('joomla.application.component.view');
129                 JView::assignRef('datemethod', $params->get('datemethod', 1));
130                 JView::assignRef('use_modal', $params->get('use_modal', 0));
131                 
132                 if ($params->get('use_modal', 0)) {
133                         JHTML::_('behavior.modal');
134                 }
135
136                 //Loop through the result rows and prepare data
137                 $i              = 0;
138                 $lists  = array();
139                 foreach ( $rows as $row )
140                 {
141                         //create thumbnails if needed and receive imagedata
142                         $dimage = ELImage::flyercreator($row->datimage, 'event');
143                         $limage = ELImage::flyercreator($row->locimage);
144                                                 
145                         //cut titel
146                         $length = strlen(htmlspecialchars( $row->title ));
147                         
148                         if ($length > $params->get('cuttitle', '25')) {
149                                 $row->title = substr($row->title, 0, $params->get('cuttitle', '18'));
150                                 $row->title = $row->title.'...';
151                         }
152                         
153                         $lists[$i]->title                       = htmlspecialchars( $row->title, ENT_COMPAT, 'UTF-8' );
154                         $lists[$i]->venue                       = htmlspecialchars( $row->venue, ENT_COMPAT, 'UTF-8' );
155                         $lists[$i]->catname                     = htmlspecialchars( $row->catname, ENT_COMPAT, 'UTF-8' );
156                         $lists[$i]->state                       = htmlspecialchars( $row->state, ENT_COMPAT, 'UTF-8' );                 
157                         $lists[$i]->eventlink           = $params->get('linkevent', 1) ? JRoute::_( EventListHelperRoute::getRoute($row->slug) ) : '';
158                         $lists[$i]->venuelink           = $params->get('linkvenue', 1) ? JRoute::_( EventListHelperRoute::getRoute($row->venueslug, 'venueevents') ) : '';
159                         $lists[$i]->categorylink        = $params->get('linkcategory', 1) ? JRoute::_( EventListHelperRoute::getRoute($row->categoryslug, 'categoryevents') ) : '';
160                         $lists[$i]->date                        = modEventListwideHelper::_format_date($row, $params);
161                         $lists[$i]->time                        = $row->times ? modEventListwideHelper::_format_time($row->dates, $row->times, $params) : '' ;
162                         $lists[$i]->eventimage          = JURI::base(true).'/'.$dimage['thumb'];
163                         $lists[$i]->eventimageorig      = JURI::base(true).'/'.$dimage['original'];
164                         $lists[$i]->venueimage          = JURI::base(true).'/'.$limage['thumb'];
165                         $lists[$i]->venueimageorig      = JURI::base(true).'/'.$limage['original'];
166                         
167                         /* Hint: Thanks for checking the code. If you want to display the event description in the module use the following command in your layout:
168                          * <?php echo $item->eventdescription; ?>
169                          * Note that all html elements will be removed
170                          */
171                         $length = 50;
172                         $etc = '...';
173                         $description = strip_tags($row->datdescription);
174                         if (strlen($description) > $length) {
175                         $length -= strlen($etc);
176                         $description = preg_replace('/\s+?(\S+)?$/', '', substr($description, 0, $length+1));
177                                 $lists[$i]->eventdescription = substr($description, 0, $length).$etc;
178                         } else {
179                                 $lists[$i]->eventdescription    = $description;
180                         }
181                         
182                         $i++;
183                 }
184                 
185                 return $lists;
186         }
187
188         /**
189          * Method to format date information
190          *
191          * @access public
192          * @return string
193          */
194         function _format_date($row, &$params)
195         {
196                 //Get needed timestamps and format
197                 $yesterday_stamp        = mktime(0, 0, 0, date("m") , date("d")-1, date("Y"));
198                 $yesterday                      = strftime("%Y-%m-%d", $yesterday_stamp);
199                 $today_stamp            = mktime(0, 0, 0, date("m") , date("d"), date("Y"));
200                 $today                          = date('Y-m-d');
201                 $tomorrow_stamp         = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
202                 $tomorrow                       = strftime("%Y-%m-%d", $tomorrow_stamp);
203                 
204                 $dates_stamp            = strtotime($row->dates);
205                 $enddates_stamp         = $row->enddates ? strtotime($row->enddates) : null;
206
207                 //if datemethod show day difference
208                 if($params->get('datemethod', 1) == 2) {
209                         //check if today or tomorrow
210                         if($row->dates == $today) {
211                                 $result = JText::_( 'TODAY' );
212                         } elseif($row->dates == $tomorrow) {
213                                 $result = JText::_( 'TOMORROW' );
214                         } elseif($row->dates == $yesterday) {
215                                 $result = JText::_( 'YESTERDAY' );
216                         
217                         //This one isn't very different from the DAYS AGO output but it seems 
218                         //adequate to use a different language string here.
219                         //
220                         //the event has an enddate and it's earlier than yesterday
221                         } elseif($row->enddates && $enddates_stamp < $yesterday_stamp) {
222                                 $days = round( ($today_stamp - $enddates_stamp) / 86400 );
223                                 $result = JText::sprintf( 'ENDED DAYS AGO', $days );
224                                 
225                         //the event has an enddate and it's later than today but the startdate is earlier than today
226                         //means a currently running event 
227                         } elseif($row->enddates && $enddates_stamp > $today_stamp && $dates_stamp < $today_stamp) {
228                                 $days = round( ($today_stamp - $dates_stamp) / 86400 );
229                                 $result = JText::sprintf( 'STARTED DAYS AGO', $days );
230                                 
231                         //the events date is earlier than yesterday
232                         } elseif($dates_stamp < $yesterday_stamp) {
233                                 $days = round( ($today_stamp - $dates_stamp) / 86400 );
234                                 $result = JText::sprintf( 'DAYS AGO', $days );
235                                 
236                         //the events date is later than tomorrow
237                         } elseif($dates_stamp > $tomorrow_stamp) {
238                                 $days = round( ($dates_stamp - $today_stamp) / 86400 );
239                                 $result = JText::sprintf( 'DAYS AHEAD', $days );
240                         }
241                 } else {
242                         //single day event
243                         $date = strftime($params->get('formatdate', '%d.%m.%Y'), strtotime( $row->dates.' '.$row->times ));
244                         $result = JText::sprintf('ON DATE', $date);
245                         
246                         //Upcoming multidayevent (From 16.10.2008 Until 18.08.2008)
247                         if($dates_stamp > $tomorrow_stamp && $enddates_stamp) {
248                                 $startdate = strftime($params->get('formatdate', '%d.%m.%Y'), strtotime( $row->dates.' '.$row->times ));
249                                 $enddate = strftime($params->get('formatdate', '%d.%m.%Y'), strtotime( $row->enddates.' '.$row->endtimes ));
250                                 $result = JText::sprintf('FROM UNTIL', $startdate, $enddate);
251                         }
252                         
253                         //current multidayevent (Until 18.08.2008)
254                         if( $row->enddates && $enddates_stamp > $today_stamp && $dates_stamp < $today_stamp ) {
255                                 //format date
256                                 $result = strftime($params->get('formatdate', '%d.%m.%Y'), strtotime( $row->enddates.' '.$row->endtimes ));
257                                 $result = JText::sprintf('UNTIL', $result);
258                         }
259                 }
260                                 
261                 return $result;
262         }
263         /**
264          * Method to format time information
265          *
266          * @access public
267          * @return string
268          */
269         function _format_time($date, $time, &$params)
270         {
271                 $time = strftime( $params->get('formattime', '%H:%M'), strtotime( $date.' '.$time ));
272
273                 return $time;
274         }
275 }