PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/components/com_joomleague/modules/mod_joomleague_calendar/connectors/joomleague.php

https://gitlab.com/julienv/joomleague
PHP | 431 lines | 332 code | 85 blank | 14 comment | 67 complexity | cf0c1e03d8c6649f3e478946ec1f2a4b MD5 | raw file
  1. <?php
  2. class JoomleagueConnector extends JLCalendar{
  3. //var $database = JFactory::getDbo();
  4. var $xparams;
  5. var $prefix;
  6. function getEntries ( &$caldates, &$params, &$matches )
  7. {
  8. $m = array();
  9. $b = array();
  10. $this->xparams = $params;
  11. $this->prefix = $params->prefix;
  12. if($this->xparams->get('joomleague_use_favteams', 0) == 1)
  13. {
  14. $this->favteams = JoomleagueConnector::getFavs();
  15. }
  16. if ($this->xparams->get('jlmatches', 0) == 1)
  17. {
  18. $rows = JoomleagueConnector::getMatches($caldates);
  19. $m = JoomleagueConnector::formatMatches($rows, $matches);
  20. }
  21. if ($this->xparams->get('jlbirthdays', 1) == 1)
  22. {
  23. $birthdays = JoomleagueConnector::getBirthdays ( $caldates, $this->params, $this->matches );
  24. $b = JoomleagueConnector::formatBirthdays($birthdays, $matches, $caldates);
  25. }
  26. return array_merge($m, $b);
  27. }
  28. function getFavs()
  29. {
  30. $query = "SELECT id, fav_team FROM #__joomleague_project
  31. where fav_team != '' ";
  32. $projectid = $this->xparams->get('project_ids') ;
  33. if ($projectid)
  34. {
  35. $projectids = (is_array($projectid)) ? implode(",", $projectid) : $projectid;
  36. $query .= " AND id IN(".$projectids.")";
  37. }
  38. $query = ($this->prefix != '') ? str_replace('#__', $this->prefix, $query) : $query;
  39. $database = JFactory::getDbo();
  40. $database->setQuery($query);
  41. $fav=$database->loadObjectList();
  42. // echo '<pre>';
  43. // print_r($fav);
  44. // echo '</pre>';
  45. // exit(0);
  46. return $fav;
  47. // return implode(',', $fav);
  48. }
  49. function getBirthdays ( $caldates, $ordering='ASC' )
  50. {
  51. $teamCondition = '';
  52. $clubCondition = '';
  53. $favCondition = '';
  54. $limitingcondition = '';
  55. $limitingconditions = array();
  56. $database = JFactory::getDbo();
  57. $customteam = JRequest::getVar('jlcteam',0,'default','POST');
  58. $teamid = $this->xparams->get('team_ids') ;
  59. if($customteam != 0)
  60. {
  61. $limitingconditions[] = "( m.projectteam1_id = ".$customteam." OR m.projectteam2_id = ".$customteam.")";
  62. }
  63. if ($teamid && $customteam == 0)
  64. {
  65. $teamids = (is_array($teamid)) ? implode(",", $teamid) : $teamid;
  66. if($teamids > 0) {
  67. $limitingconditions[] = "pt.team_id IN (".$teamids.")";
  68. }
  69. }
  70. if($this->xparams->get('joomleague_use_favteams', 0) == 1 && $customteam == 0)
  71. {
  72. foreach ($this->favteams as $projectfavs)
  73. {
  74. $favConds[] = "(pt.team_id IN (". $projectfavs->fav_team.") AND p.id =".$projectfavs->id.")";
  75. }
  76. $limitingconditions[] = implode(' OR ', $favConds);
  77. }
  78. // new insert for user select a club
  79. $clubid = $this->xparams->get('club_ids') ;
  80. if ($clubid && $customteam == 0)
  81. {
  82. $clubids = (is_array($clubid)) ? implode(",", $clubid) : $clubid;
  83. if($clubids > 0) $limitingconditions[] = "team.club_id IN (".$clubids.")";
  84. }
  85. if (count($limitingconditions) > 0)
  86. {
  87. $limitingcondition .=' AND (';
  88. $limitingcondition .= implode(' OR ', $limitingconditions);
  89. $limitingcondition .=')';
  90. }
  91. $query="SELECT p.id, p.firstname, p.lastname, p.picture, p.country,
  92. DATE_FORMAT(p.birthday, '%m-%d') AS month_day,
  93. YEAR( CURRENT_DATE( ) ) as year,
  94. DATE_FORMAT('".$caldates['start']."', '%Y') - YEAR( p.birthday ) AS age,
  95. DATE_FORMAT(p.birthday,'%Y-%m-%d') AS date_of_birth,
  96. pt.project_id as project_id,
  97. 'showPlayer' AS func_to_call,
  98. 'pid' AS id_to_append,
  99. team.short_name, team.id as teamid
  100. FROM #__joomleague_person AS p
  101. inner JOIN #__joomleague_team_player AS tp ON (p.id = tp.person_id)
  102. inner JOIN #__joomleague_project_team AS pt ON (pt.id = tp.projectteam_id)
  103. inner JOIN #__joomleague_team AS team ON (team.id = pt.team_id )
  104. inner JOIN #__joomleague_club AS club ON (club.id = team.club_id )
  105. WHERE p.published = 1 AND p.birthday != '0000-00-00' and DATE_FORMAT(p.birthday, '%m') = DATE_FORMAT('".$caldates['start']."', '%m')";
  106. $projectid = $this->xparams->get('project_ids') ;
  107. if ($projectid)
  108. {
  109. $projectids = (is_array($projectid)) ? implode(",", $projectid) : $projectid;
  110. if($projectids > 0) $query .= " AND (pt.project_id IN (".$projectids.") )";
  111. }
  112. $query .= $limitingcondition;
  113. $query .= " GROUP BY p.id ORDER BY p.birthday";
  114. $query = ($this->prefix != '') ? str_replace('#__', $this->prefix, $query) : $query;
  115. $database->setQuery($query);
  116. //echo($database->getQuery());
  117. $players=$database->loadObjectList();
  118. return $players;
  119. }
  120. function formatBirthdays( $rows, &$matches, $dates )
  121. {
  122. $newrows = array();
  123. $year = substr($dates['start'], 0, 4);
  124. foreach ($rows AS $key => $row)
  125. {
  126. $newrows[$key]['type'] = 'jlb';
  127. $newrows[$key]['homepic'] = '';
  128. $newrows[$key]['awaypic'] = '';
  129. $newrows[$key]['date'] = $year.'-'.$row->month_day;
  130. $newrows[$key]['age'] = '('.$row->age.')';
  131. $newrows[$key]['headingtitle'] = $this->xparams->get('birthday_text', 'Birthday');
  132. $newrows[$key]['name'] = '';
  133. if ($row->picture != '' AND file_exists(JPATH_BASE.DS.$row->picture))
  134. {
  135. $linkit = 1;
  136. $newrows[$key]['name'] = '<img src="'.JUri::root(true).'/'.$row->picture.'" alt="Picture" style="height:40px; vertical-align:middle;margin:0 5px;" />';
  137. //echo $newrows[$key]['name'].'<br />';
  138. }
  139. $newrows[$key]['name'] .= parent::jl_utf8_convert ($row->firstname, 'iso-8859-1', 'utf-8').' ';
  140. $newrows[$key]['name'] .= parent::jl_utf8_convert ($row->lastname, 'iso-8859-1', 'utf-8').' - '.parent::jl_utf8_convert ($row->short_name, 'iso-8859-1', 'utf-8');
  141. //$newrows[$key]['name'] .= ' ('..')';
  142. $newrows[$key]['matchcode'] = 0;
  143. $newrows[$key]['project_id'] = $row->project_id;
  144. // new insert for link to player profile
  145. //$newrows[$key]['link'] = 'index.php?option=com_joomleague&view=player&p='.$row->project_id.'&pid='.$row->id;
  146. $newrows[$key]['link'] = JoomleagueHelperRoute::getPlayerRoute( $row->project_id, $row->teamid, $row->id);
  147. $matches[] = $newrows[$key];
  148. }
  149. return $newrows;
  150. }
  151. function formatMatches( $rows, &$matches )
  152. {
  153. $newrows = array();
  154. $teamnames = $this->xparams->get('team_names', 'short_name');
  155. $teams = JoomleagueConnector::getTeamsFromMatches( $rows );
  156. $teams[0] = new stdclass;
  157. $teams[0]->name = $teams[0]->$teamnames = $teams[0]->logo_small = $teams[0]->logo_middle = $teams[0]->logo_big = '';
  158. foreach ($rows AS $key => $row) {
  159. $newrows[$key]['type'] = 'jlm';
  160. $newrows[$key]['homepic'] = JoomleagueConnector::buildImage($teams[$row->projectteam1_id]);
  161. $newrows[$key]['awaypic'] = JoomleagueConnector::buildImage($teams[$row->projectteam2_id]);
  162. $newrows[$key]['date'] = JoomleagueHelper::getMatchStartTimestamp($row);
  163. //$newrows[$key]['result'] = (!is_null($row->matchpart1_result)) ? $row->matchpart1_result . ':' . $row->matchpart2_result : '-:-';
  164. $newrows[$key]['result'] = (!is_null($row->team1_result)) ? $row->team1_result . ':' . $row->team2_result : '-:-';
  165. $newrows[$key]['headingtitle'] = parent::jl_utf8_convert ($row->name.'-'.$row->roundname, 'iso-8859-1', 'utf-8');
  166. $newrows[$key]['homename'] = JoomleagueConnector::formatTeamName($teams[$row->projectteam1_id]);
  167. $newrows[$key]['awayname'] = JoomleagueConnector::formatTeamName($teams[$row->projectteam2_id]);
  168. $newrows[$key]['matchcode'] = $row->matchcode;
  169. $newrows[$key]['project_id'] = $row->project_id;
  170. // insert matchdetaillinks
  171. $newrows[$key]['link'] = JoomleagueHelperRoute::getNextMatchRoute( $row->project_id, $row->matchcode);
  172. $matches[] = $newrows[$key];
  173. parent::addTeam($row->projectteam1_id, parent::jl_utf8_convert ($teams[$row->projectteam1_id]->name, 'iso-8859-1', 'utf-8'), $newrows[$key]['homepic']);
  174. parent::addTeam($row->projectteam2_id, parent::jl_utf8_convert ($teams[$row->projectteam2_id]->name, 'iso-8859-1', 'utf-8'), $newrows[$key]['awaypic']);
  175. }
  176. return $newrows;
  177. }
  178. function formatTeamName($team)
  179. {
  180. $teamnames = $this->xparams->get('team_names', 'short_name');
  181. switch ($teamnames)
  182. {
  183. case '-':
  184. return '';
  185. break;
  186. case 'short_name':
  187. $teamname = '<acronym title="'.parent::jl_utf8_convert ($team->name, 'iso-8859-1', 'utf-8').'">'
  188. .parent::jl_utf8_convert ($team->short_name, 'iso-8859-1', 'utf-8')
  189. .'</acronym>';
  190. break;
  191. default:
  192. if (!isset($team->$teamnames) OR (is_null($team->$teamnames) OR trim($team->$teamnames)=='')) {
  193. $teamname = parent::jl_utf8_convert ($team->name, 'iso-8859-1', 'utf-8');
  194. }
  195. else {
  196. $teamname = parent::jl_utf8_convert ($team->$teamnames, 'iso-8859-1', 'utf-8');
  197. }
  198. break;
  199. }
  200. return $teamname;
  201. }
  202. function buildImage($team)
  203. {
  204. $image = $this->xparams->get('team_logos', 'logo_small');
  205. if ($image == '-') { return ''; }
  206. $logo = '';
  207. if ($team->$image != '' && file_exists(JPATH_BASE.'/'.$team->$image))
  208. {
  209. $h = $this->xparams->get('logo_height', 20);
  210. $logo = '<img src="'.JUri::root(true).'/'.$team->$image.'" alt="'
  211. .parent::jl_utf8_convert ($team->short_name, 'iso-8859-1', 'utf-8').'" title="'
  212. .parent::jl_utf8_convert ($team->name, 'iso-8859-1', 'utf-8').'"';
  213. if ($h > 0) {
  214. $logo .= ' style="height:'.$h.'px;"';
  215. }
  216. $logo .= ' />';
  217. }
  218. return $logo;
  219. }
  220. function getMatches($caldates, $ordering='ASC')
  221. {
  222. $database = JFactory::getDbo();
  223. $teamCondition = '';
  224. $clubCondition = '';
  225. $favCondition = '';
  226. $limitingcondition = '';
  227. $limitingconditions = array();
  228. $favConds = array();
  229. $customteam = JRequest::getVar('jlcteam',0,'default','POST');
  230. $teamid = $this->xparams->get('team_ids') ;
  231. if($customteam != 0)
  232. {
  233. $limitingconditions[] = "( m.projectteam1_id = ".$customteam." OR m.projectteam2_id = ".$customteam.")";
  234. }
  235. if ($teamid && $customteam == 0)
  236. {
  237. $teamids = (is_array($teamid)) ? implode(",", $teamid) : $teamid;
  238. if($teamids > 0)
  239. {
  240. $limitingconditions[] = "pt.team_id IN (".$teamids.")";
  241. }
  242. }
  243. $clubid = $this->xparams->get('club_ids') ;
  244. if ($clubid && $customteam == 0)
  245. {
  246. $clubids = (is_array($clubid)) ? implode(",", $clubid) : $clubid;
  247. if($clubids > 0)
  248. {
  249. $limitingconditions[] = "team.club_id IN (".$clubids.")";
  250. }
  251. }
  252. if($this->xparams->get('joomleague_use_favteams', 0) == 1 && $customteam == 0)
  253. {
  254. foreach ($this->favteams as $projectfavs)
  255. {
  256. $favConds[] = "(pt.team_id IN (". $projectfavs->fav_team.") AND p.id =".$projectfavs->id.")";
  257. }
  258. if(!empty($favConds))
  259. {
  260. $limitingconditions[] = implode(' OR ', $favConds);
  261. }
  262. }
  263. if (count($limitingconditions) > 0)
  264. {
  265. $limitingcondition .=' AND (';
  266. $limitingcondition .= implode(' OR ', $limitingconditions);
  267. $limitingcondition .=')';
  268. }
  269. $limit = (isset($caldates['limitstart'])&&isset($caldates['limitend'])) ? ' LIMIT '.$caldates['limitstart'].', '.$caldates['limitend'] :'';
  270. $query = "SELECT m.*,p.*,
  271. match_date AS caldate,
  272. r.roundcode, r.name AS roundname, r.round_date_first, r.round_date_last,
  273. m.id as matchcode, p.id as project_id
  274. FROM #__joomleague_match m
  275. inner JOIN #__joomleague_round r ON r.id = m.round_id
  276. inner JOIN #__joomleague_project p ON p.id = r.project_id
  277. inner JOIN #__joomleague_project_team pt ON (pt.id = m.projectteam1_id OR pt.id = m.projectteam2_id)
  278. inner JOIN #__joomleague_team team ON team.id = pt.team_id
  279. inner JOIN #__joomleague_club club ON club.id = team.club_id
  280. ";
  281. $where = " WHERE m.published = 1
  282. AND p.published = 1 ";
  283. if (isset($caldates['start'])) $where .= " AND m.match_date >= '".$caldates['start']."'";
  284. if (isset($caldates['end'])) $where .= " AND m.match_date <= '".$caldates['end']."'";
  285. if (isset($caldates['matchcode'])) $where .= " AND r.matchcode = '".$caldates['matchcode']."'";
  286. $projectid = $this->xparams->get('project_ids') ;
  287. if ($projectid)
  288. {
  289. $projectids = (is_array($projectid)) ? implode(",", $projectid) : $projectid;
  290. if($projectids > 0) $where .= " AND p.id IN (".$projectids.")";
  291. }
  292. if(isset($caldates['resultsonly']) && $caldates['resultsonly']== 1) $where .= " AND m.team1_result IS NOT NULL";
  293. $where .= $limitingcondition;
  294. $where .= " GROUP BY m.id";
  295. $where .=" ORDER BY m.match_date ".$ordering;
  296. $query = ($this->prefix != '') ? str_replace('#__', $this->prefix, $query) : $query;
  297. $database->setQuery($query.$where.$limit);
  298. $result = $database->loadObjectList();
  299. if ($result)
  300. {
  301. foreach ($result as $match)
  302. {
  303. JoomleagueHelper::convertMatchDateToTimezone($match);
  304. }
  305. }
  306. return $result;
  307. }
  308. function getTeamsFromMatches( &$games )
  309. {
  310. $database = JFactory::getDbo();
  311. if ( !count ($games) ) return Array();
  312. foreach ( $games as $m )
  313. {
  314. $teamsId[] = $m->projectteam1_id;
  315. $teamsId[] = $m->projectteam2_id;
  316. }
  317. $listTeamId = implode( ",", array_unique($teamsId) );
  318. $query = "SELECT tl.id AS teamtoolid, tl.division_id, tl.standard_playground, tl.start_points,
  319. tl.info, tl.team_id, tl.checked_out, tl.checked_out_time, tl.picture, tl.project_id,
  320. t.id, t.name, t.short_name, t.middle_name, t.info, t.club_id,
  321. c.logo_small, c.logo_middle, c.logo_big, c.country,
  322. p.name AS project_name
  323. FROM #__joomleague_team t
  324. INNER JOIN #__joomleague_project_team tl on tl.team_id = t.id
  325. INNER JOIN #__joomleague_project p on p.id = tl.project_id
  326. LEFT JOIN #__joomleague_club c on t.club_id = c.id
  327. WHERE tl.id IN (".$listTeamId.") AND tl.project_id = p.id";
  328. $query = ($this->prefix != '') ? str_replace('#__', $this->prefix, $query) : $query;
  329. $database->setQuery($query);
  330. if ( !$result = $database->loadObjectList('teamtoolid') ) $result = Array();
  331. return $result;
  332. }
  333. function build_url( &$row )
  334. {
  335. }
  336. function getGlobalTeams ()
  337. {
  338. $teamnames = $this->xparams->get('team_names', 'short_name');
  339. $database = JFactory::getDbo();
  340. $query = "SELECT t.".$teamnames." AS name, t.id AS value
  341. FROM #__joomleague_teams t, #__joomleague p
  342. WHERE t.id IN(p.fav_team)";
  343. $database->setQuery($query);
  344. $result = $database->loadObjectList();
  345. }
  346. }