PageRenderTime 44ms CodeModel.GetById 14ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/com_joomleague/components/com_joomleague/modules/mod_joomleague_calendar/helper.php

https://gitlab.com/volleyuisp/joomleague
PHP | 406 lines | 330 code | 57 blank | 19 comment | 51 complexity | 91616aad0a1a63aeda629fe97a8702c0 MD5 | raw file
  1<?php
  2/**
  3 * @version		1.2.2.1
  4 * @package		Blog Calendar
  5 * @author		Justo Gonzalez de Rivera
  6 * @license		GNU/GPL
  7 * modified by johncage for use with joomleague
  8 * @version		1.5.0.1
  9 */
 10
 11
 12// no direct access
 13defined('_JEXEC') or die('Restricted access');
 14
 15//require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
 16require_once (dirname(__FILE__).DS.'calendarClass.php');
 17
 18
 19class modJLCalendarHelper
 20{
 21
 22
 23
 24	function showCal(&$params,$year,$month,$ajax=0,$modid) //this function returns the html of the calendar for a given month
 25	{
 26		global $mainframe;
 27		$offset = 0; //$mainframe->getCfg('offset');
 28		$language= JFactory::getLanguage(); //get the current language
 29		$language->load( 'mod_joomleague_calendar' ); //load the language ini file of the module
 30		$article= $language->_('MOD_JOOMLEAGUE_CALENDAR_VALUEMATCH');
 31		$articles= $language->_('MOD_JOOMLEAGUE_CALENDAR_VALUEMATCHES'); //this strings are used for the titles of the links
 32		$article2= $language->_('MOD_JOOMLEAGUE_CALENDAR_MATCHTHISDAY');
 33
 34		$cal = new JLCalendar; //this object creates the html for the calendar
 35		$dayNamLen= $params->get('cal_length_days');
 36
 37		$cal->dayNames = array(
 38		substr(JText::_( 'SUN' ),0,$dayNamLen),
 39		substr(JText::_( 'MON' ),0,$dayNamLen),
 40		substr(JText::_( 'TUE' ),0,$dayNamLen),
 41		substr(JText::_( 'WED' ),0,$dayNamLen),
 42		substr(JText::_( 'THU' ),0,$dayNamLen),
 43		substr(JText::_( 'FRI' ),0,$dayNamLen),
 44		substr(JText::_( 'SAT' ),0,$dayNamLen)
 45		);
 46
 47		$cal->monthNames = array(
 48		JText::_( 'JANUARY' ),
 49		JText::_( 'FEBRUARY' ),
 50		JText::_( 'MARCH' ),
 51		JText::_( 'APRIL' ),
 52		JText::_( 'MAY' ),
 53		JText::_( 'JUNE' ),
 54		JText::_( 'JULY' ),
 55		JText::_( 'AUGUST' ),
 56		JText::_( 'SEPTEMBER' ),
 57		JText::_( 'OCTOBER' ),
 58		JText::_( 'NOVEMBER' ),
 59		JText::_( 'DECEMBER' )
 60		);
 61
 62		$cal->startDay = $params->get('cal_start_day'); //set the startday (this is the day that appears in the first column). Sunday = 0
 63		//it is loaded from the language ini because it may vary from one country to another, in Spain
 64		//for example, the startday is Monday (1)
 65		$cal->lightbox = $params->get('lightbox');
 66		$cal->lightbox_on_pageload = $params->get('lightbox_on_pageload');
 67		$cal->prefix = $params->get('custom_prefix');
 68		$cal->usedteams = $params->get('usedteams');
 69		$cal->usedclubs = $params->get('usedclubs');
 70		$cal->params = $params;
 71		//set the link for the month, this will be the link for the calendar header (ex. December 2007)
 72		$cal->monthLink=JRoute::_('index.php?option=com_joomleague_calendar' . '&year=' . $year .
 73					'&month=' . $month . '&modid=' . $modid);
 74		$cal->modid= $modid;
 75		$cal->ajax = $ajax;
 76		$cal->getMatches($month,$year);
 77		$counter= Array();
 78		jimport('joomla.utilities.date');
 79		foreach ( $cal->matches as $row )
 80		{
 81			$created= new JDate($row['date'], -$offset);
 82			$createdYear=$created->toFormat('%Y');
 83			$createdMonth=$created->toFormat('%m');
 84			$createdDay=$created->toFormat('%d'); //have to use %d because %e doesn't works on windows
 85			$createdDate=$createdYear . $createdMonth . $createdDay; //this makes an unique variable for every day
 86			$counter[$createdDate]['createdYear'] = $createdYear;
 87			$counter[$createdDate]['createdMonth'] = $createdMonth;
 88			$counter[$createdDate]['createdDay'] = $createdDay;
 89			$counter[$createdDate]['tiptitle'] = $created->toFormat('%A, %d.%m.%Y');
 90			if (!isset($counter[$createdDate]['count'])) $counter[$createdDate]['count'] = 1;
 91			else $counter[$createdDate]['count'] += 1; //$counter[$date] counts the number of articles in each day, to display it as a title in the link of the day
 92		}
 93		foreach ($counter AS $createdDate => $val) {
 94			$title =  $counter[$createdDate]['tiptitle'].' :: ' .$counter[$createdDate]['count'] . ' ';
 95			$title .= ($counter[$createdDate]['count'] > 1)? $articles : $article;
 96			$title .= ' ' . $article2;
 97			$inject = $params->get('inject', 0);
 98			$update_module = $params->get('update_module', 0);
 99			$cal->linklist[$createdDate]['click']=	'jlCalmod_showhide(\'jlCalList-'.$modid.'\', \'jlcal_'
100			.$counter[$createdDate]['createdYear'].'-'.$counter[$createdDate]['createdMonth'].'-'.$counter[$createdDate]['createdDay'].'-'.$modid
101			.'\', \''.str_replace(' :: ', ': ',$title).'\', '.$inject.', '.$modid.');';
102			//$cal->linklist[$createdDate]['click']=	'jlcnewDate('.$counter[$createdDate]['createdMonth'].",". $counter[$createdDate]['createdYear'].",".$modid."," .$day.');';
103			$cal->linklist[$createdDate]['link']=	'javascript:void(0)';
104			$cal->linklist[$createdDate]['link'].="\" title=\""; //the calendar class sets the links this way: <a href=" . THE LINK STRING . ">
105			//so, the easiest way to add a title to that link is by setting THE LINK STRING = the link" title="the title
106			//the result link would be <a href="the link" title="the title">
107			$cal->linklist[$createdDate]['link'].= $title;
108
109			//the above 3 lines output something like: 3 articles on this day. Or: 1 article on this day
110		}
111
112
113		return $cal->getMonthView($month,$year);
114
115	}
116
117	function getDate_byId($id)
118	{
119		global $mainframe;
120		$offset = 0; // $mainframe->getCfg('offset');
121		$prefix = $params->get('custom_prefix');
122		$query=	' SELECT match_date' .
123			' FROM #__joomleague_matches'.
124			' WHERE match_id=\'' . $id . '\'';
125		$query = ($prefix != '') ? str_replace('#__', $prefix, $query) : $query;
126		$db = JFactory::getDbo();
127		$db->setQuery($query);
128		$row=& $db->loadObjectList();
129
130
131		jimport('joomla.utilities.date');
132		$created=new JDate($row[0]->match_date, -$offset);
133
134
135		$createdYear=$created->toFormat('%Y');
136		$createdMonth=$created->toFormat('%m');
137		$createdDay=$created->toFormat('%d');
138
139		$createdDate=Array($createdYear,$createdMonth,$createdDay);
140
141		return $createdDate;
142	}
143
144	function showDropDown($params,$year,$month,$day,$ajax=0){
145
146		$results= $this->setTheQuery($params,$year,$month,$day,$ajax,1);
147
148		foreach($results as $key => $result){
149			$created=new JDate($results[$key]->match_date);
150			$createdYear= $created->toFormat('%Y');
151			$createdMonth= $created->toFormat('%m');
152
153
154			$results[$key]->year = $createdYear;
155			$results[$key]->month = $createdMonth;
156
157			$createdYear == $year ? $articleCounter[$createdYear]['now']= true : '';
158			$createdMonth== $month ? $articleCounter[$createdYear][$createdMonth]['now']= true : '';
159			if (!isset($articleCounter[$createdYear][$createdMonth]['total'])) $articleCounter[$createdYear][$createdMonth]['total'] = 0;
160			if (!isset($articleCounter[$createdYear]['total'])) $articleCounter[$createdYear]['total'] = 0;
161			$articleCounter[$createdYear][$createdMonth]['total']++;
162			$articleCounter[$createdYear]['total']++;
163
164		}
165
166		return array($results,$articleCounter);
167	}
168
169}
170
171
172class JLCalendar extends PHPCalendar
173{
174
175	var $linklist; //this variable will be an array that contains all the links of the month
176	var $prefix;
177	var $params;
178	var $matches = array();
179	var $teams = array();
180	var $teamslist = array();
181
182	function addTeam($id, $name='', $pic='')
183	{
184		if (! array_key_exists($id, $this->teams)&& $id > 0){
185			$this->teams[$id] 			= new stdclass;
186			$this->teams[$id]->value 	= $id;
187			$this->teams[$id]->name 	= $name;
188			$this->teams[$id]->picture 	= $pic;
189			$this->teamslist[] 			= $this->teams[$id];
190		}
191	}
192
193	function getDateLink($day, $month, $year) //this function is called from getMonthView(month,year) to get the link of the given day
194	{										  //if this function returns nothing (""), then getMonthView wont put a link on that day
195		$link = "";
196		if(strlen($month)<2)
197		$month = '0'.$month;
198		if(strlen($day)<2)
199		$day = '0'.$day;
200
201		$date= $year . $month . $day;
202		if(isset($this->linklist[$date]['link'])){
203			$link=$this->linklist[$date]['link'];  //$this->linklist[$date] was set for every date in the foreach bucle at lines 50-83
204		}
205
206		return $link;
207	}
208	function getDateClick($day, $month, $year) //this function is called from getMonthView(month,year) to get the link of the given day
209	{										  //if this function returns nothing (""), then getMonthView wont put a link on that day
210		$link = "";
211		if(strlen($month)<2)
212		$month = '0'.$month;
213		if(strlen($day)<2)
214		$day = '0'.$day;
215
216		$date= $year . $month . $day;
217		if(isset($this->linklist[$date]['click'])){
218			$link=$this->linklist[$date]['click'];  //$this->linklist[$date] was set for every date in the foreach bucle at lines 50-83
219		}
220
221		return $link;
222	}
223
224
225	//Return the URL to link to in order to display a calendar for a given month/year.
226	//this function is called to get the links of the two arrows in the header.
227	function getCalendarLink($month, $year)
228	{
229		$getquery = JRequest::get('GET'); //get the GET query
230		$calendarLink= JUri::current().'?'; //get the current url, without the GET query; and add "?", to set the GET vars
231
232		foreach($getquery as $key => $value){  /*this bucle goes through every GET variable that was in the url*/
233			if($key!='month' AND $key!='year' AND $key!='day' AND $value){ /*the month,year, and day Variables must be diferent of the current ones, because this is a link for a diferent month */
234				$calendarLink.= $key . '=' . $value . '&amp;';
235			}
236		}
237		$calendarLink.='month='.$month.'&amp;year='.$year; //add the month and the year that was passed to the function to the GET string
238		return $calendarLink;
239	}
240
241
242	function jl_utf8_convert ($text, $fromenc='iso-8859-1', $toenc='UTF-8' )
243	{
244		if (strtolower($fromenc)==strtolower($toenc) || $this->params->get('convert', 0)==0) return $text;
245
246		elseif (function_exists('iconv')) {
247			return iconv($fromenc, $toenc, $text);
248		}
249		elseif (strtolower($fromenc) == 'iso-8859-1' && strtolower($toenc) == 'utf-8') {
250			return utf8_encode($text);
251		}
252		elseif (strtolower($fromenc) == 'utf-8' && strtolower($toenc) == 'iso-8859-1') {
253			return utf8_decode($text);
254		}
255		else return $text;
256	}
257	function sortObject($array, $comparefunction, $property = '')
258	{
259		$zcount=count($array);
260		for($i=1;$i<$zcount;$i++){
261			for ($a=$zcount-1;$a>=$i;$a--) {
262				if($this->$comparefunction($array[$a-1]->$property,$array[$a]->$property)>0){
263					$tempzal=$array[$a-1];
264					$array[$a-1]=$array[$a];
265					$array[$a]=$tempzal;
266				}
267			}
268		}
269		return $array;
270	}
271	function sortArray($array, $comparefunction, $property = ''){
272		$zcount=count($array);
273		for($i=1;$i<$zcount;$i++){
274			for ($a=$zcount-1;$a>=$i;$a--) {
275				if($this->$comparefunction($array[$a-1][$property],$array[$a][$property])>0){
276					$tempzal=$array[$a-1];
277					$array[$a-1]=$array[$a];
278					$array[$a]=$tempzal;
279				}
280			}
281		}
282		return $array;
283	}
284
285
286	function asc($a,$b){
287		if($a<$b)return -1;
288		if($a==$b)return 0;
289		return 1;
290	}
291
292	function desc($a,$b){
293		if($a>$b)return -1;
294		if($a==$b)return 0;
295		return 1;
296	}
297
298	function getMatches($month, $year) {
299		$livescore = $this->params->get('livescore', '');
300		$joomleague = $this->params->get('joomleague', '');
301		$caldates = array();
302		$caldates['start'] = date("$year-$month-01 00:00:00");
303		$caldates['end'] = date("$year-$month-31 23:59:59");
304		$jlrows = array();
305		$lsrows = array();
306		$usejevents = $this->params->get('jevents', 0);
307		if ( $usejevents == 1 ) {
308			$day = 0;
309			require_once (dirname(__FILE__).DS.'connectors'.DS.'jevents.php');
310			JEventsConnector::getEntries($caldates, $this->params, $this->matches);
311		}
312		require_once (dirname(__FILE__).DS.'connectors'.DS.$joomleague.'.php');
313		$this->params->prefix = $this->prefix;
314		JoomleagueConnector::getEntries ( $caldates, $this->params, $this->matches );
315		if ($livescore != ''){
316			require_once (dirname(__FILE__).DS.'connectors'.DS.'livescore.php');
317			$this->params->prefix = $this->params->get('prefix_livescore', '');
318			LivescoreConnector::getMatches ( $caldates, $this->params, $this->matches );
319		}
320		$matches = $this->sortArray($this->matches, 'asc', 'date');
321		$this->matches = $matches;
322		return $matches;
323	}
324
325	function matches_output($month, $year) {
326		global $mainframe;
327		$language= JFactory::getLanguage(); //get the current language
328		$language->load( 'mod_joomleague_calendar' ); //load the language ini file of the module
329		$article= $language->_('MOD_JOOMLEAGUE_CALENDAR_VALUEMATCH');
330		$articles= $language->_('MOD_JOOMLEAGUE_CALENDAR_VALUEMATCHES'); //this strings are used for the titles of the links
331		$article2= $language->_('MOD_JOOMLEAGUE_CALENDAR_MATCHTHISDAY');
332		$noarticle= $language->_('MOD_JOOMLEAGUE_CALENDAR_NOMATCHES');
333		$outstring = '';
334		$todaystring = '';
335		$matches = $this->matches;
336		$div = '';
337		$now = new JDate();
338		$today = $now->toFormat('%Y-%m-%d');
339		$todaytitle = '';
340		$pm='';
341		$offset = 0; // $mainframe->getCfg('offset');
342		$update_module = $this->params->get('update_module', 0);
343		$totalgamesstring = (count($matches)>0) ? count($matches) : $noarticle;
344		$totalgamesstring .= ' ';
345		$totalgamesstring .= (count($matches) > 1) ? $articles : $article;
346		$totalgamesstring .=  ' ';
347		$totalgamesstring .= $language->_('MOD_JOOMLEAGUE_CALENDAR_VALUEMATCHESMONTH') . ' ' .$this->monthNames[$month - 1] . ' ' . $year;
348		$thistitle = ($todaytitle != '') ? $todaytitle : $totalgamesstring;
349		$thistitle = $totalgamesstring;
350		$format = array();
351		$format[] = array('tag' => 'span', 'divid' => 'oldjlCalListTitle-'.$this->modid, 'class' => 'jlcal_hiddenmatches', 'text' => $totalgamesstring);
352		$format[] = array('tag' => 'span', 'divid' => 'jlCalListTitle-'.$this->modid, 'class' => 'jlCalListTitle', 'text' => $thistitle);
353		$format[] = array('tag' => 'span', 'divid' => 'jlCalListDayTitle-'.$this->modid, 'class' => 'jlCalListTitle', 'text' => '');
354		for ($x=0;$x<count($matches);$x++) {
355			$sclass = ($x%2) ? 'sectiontableentry1' : 'sectiontableentry2';
356			$row = $matches[$x];
357			$thispm = $row['project_id'].'_'.$row['matchcode'].'_'.$row['type'];
358			$da= new JDate($row['date'], -$offset);
359			if ($div !=$da->toFormat('%Y-%m-%d')) {
360				$counter = 0;
361				$div = $da->toFormat('%Y-%m-%d');
362				$format[] = array('tag' => 'div', 'divid' => 'jlcal_'.$div."-".$this->modid, 'class' => 'jlcal_hiddenmatches');
363				$format[] = array('tag' => 'table', 'divid' => 'jlcal_'.$div."-".$this->modid, 'class' => 'jlcal_result_table');
364			}
365			if($pm != $thispm) {
366				$format[] = array('tag' => 'headingrow', 'text' => $row['headingtitle']);
367				$roundname = $row['headingtitle'];
368			}
369
370			$pm = $thispm;
371			$format[] = $row;
372			$counter++;
373			if (isset($matches[$x+1])) $nd= new JDate($matches[$x+1]['date'], -$offset);
374			else $nd = false;
375			if (!$nd || $nd->toFormat('%Y-%m-%d') != $da->toFormat('%Y-%m-%d')) {
376
377				$pm = '';
378				$format[] = array('tag' => 'tableend');
379				$format[] = array('tag' => 'divend');
380				$titletext = $counter;
381				$titletext .= ' ';
382				$titletext .= ($counter > 1)? $articles : $article;
383				$titletext .= ' ';
384				$titletext .= ($today == $da->toFormat('%Y-%m-%d')) ? $language->_('MOD_JOOMLEAGUE_CALENDAR_TODAY'): $language->_('MOD_JOOMLEAGUE_CALENDAR_AT');
385				$titletext .= ' ' .$da->toFormat('%d').'. '.$this->monthNames[$month - 1] . ' ' . $year;
386				$format[] = array('tag' => 'span', 'divid' => 'jlcaltitte_'.$div."-".$this->modid, 'class' => 'jlcal_hiddenmatches', 'text' => $titletext);
387
388			}
389		}
390
391
392		return $format;
393	}
394	function output_teamlist() {
395		$teamslist = array();
396		if(count($this->teams) > 0 && $this->params->get('show_teamslist', 0) == 1) {
397			$teams = $this->sortObject($this->teamslist, 'asc', 'name');
398			$teamslist[] = JHtml::_('select.option', 0, JText::_($this->params->get('teamslist_option')));
399			foreach ($teams AS $id => $obj) {
400				$teamslist[] = JHtml::_('select.option', $obj->value, JText::_($obj->name));
401			}
402		}
403		return $teamslist;
404	}
405}
406?>