PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/administrator/components/com_joomleague/models/projectteams.php

https://gitlab.com/julienv/joomleague
PHP | 444 lines | 322 code | 53 blank | 69 comment | 25 complexity | 549dac859ee26a7237f54e7f8e4b090c MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2006-2014 joomleague.at. All rights reserved.
  4. * @license GNU/GPL, see LICENSE.php
  5. * Joomla! is free software. This version may have been modified pursuant
  6. * to the GNU General Public License, and as distributed it includes or
  7. * is derivative of works licensed under the GNU General Public License or
  8. * other free or open source software licenses.
  9. * See COPYRIGHT.php for copyright notices and details.
  10. */
  11. // Check to ensure this file is included in Joomla!
  12. defined( '_JEXEC' ) or die( 'Restricted access' );
  13. jimport( 'joomla.application.component.model' );
  14. require_once( JPATH_COMPONENT . DS . 'models' . DS . 'list.php' );
  15. /**
  16. * Joomleague Component projectteam Model
  17. *
  18. * @package JoomLeague
  19. * @since 0.1
  20. */
  21. class JoomleagueModelProjectteams extends JoomleagueModelList
  22. {
  23. var $_identifier = "pteams";
  24. function _buildQuery()
  25. {
  26. // Get the WHERE and ORDER BY clauses for the query
  27. $where = $this->_buildContentWhere();
  28. $orderby = $this->_buildContentOrderBy();
  29. $query = ' SELECT pt.id AS projectteamid,
  30. pt.*,
  31. t.name,
  32. t.club_id,
  33. c.email AS club_email,
  34. (SELECT count(id)
  35. FROM #__joomleague_team_player tp
  36. WHERE projectteam_id = projectteamid and tp.published=1) playercount,
  37. (SELECT count(id)
  38. FROM #__joomleague_team_staff ts
  39. WHERE projectteam_id = projectteamid and ts.published=1) staffcount,
  40. pt.info
  41. FROM #__joomleague_project_team AS pt
  42. LEFT JOIN #__joomleague_team t on pt.team_id = t.id
  43. LEFT JOIN #__joomleague_club c on t.club_id = c.id
  44. LEFT JOIN #__joomleague_division d on d.id = pt.division_id
  45. LEFT JOIN #__joomleague_playground plg on plg.id = pt.standard_playground ' .
  46. $where . $orderby;
  47. return $query;
  48. }
  49. function _buildContentOrderBy()
  50. {
  51. $option = JRequest::getCmd('option');
  52. $mainframe = JFactory::getApplication();
  53. $filter_order = $mainframe->getUserStateFromRequest( $option . 'tl_filter_order', 'filter_order', 't.name', 'cmd' );
  54. $filter_order_Dir = $mainframe->getUserStateFromRequest( $option . 'tl_filter_order_Dir', 'filter_order_Dir', '', 'word' );
  55. if ( $filter_order == 't.name' )
  56. {
  57. $orderby = ' ORDER BY t.name ' . $filter_order_Dir;
  58. }
  59. else
  60. {
  61. $orderby = ' ORDER BY ' . $filter_order . ' ' . $filter_order_Dir . ' , t.name ';
  62. }
  63. return $orderby;
  64. }
  65. function _buildContentWhere()
  66. {
  67. $option = JRequest::getCmd('option');
  68. $mainframe = JFactory::getApplication();
  69. $where=array();
  70. $project_id = $mainframe->getUserState( $option.'project' );
  71. $division = (int) $mainframe->getUserStateFromRequest($option.'tl_division', 'division', 0);
  72. $where[] = ' pt.project_id = ' . $project_id;
  73. $division=JString::strtolower($division);
  74. if ($division>0)
  75. {
  76. $where[]=' d.id = '.$this->_db->Quote($division);
  77. }
  78. $where=(count($where) ? ' WHERE '.implode(' AND ',$where) : '');
  79. return $where;
  80. }
  81. /**
  82. * Method to update project teams list
  83. *
  84. * @access public
  85. * @return boolean True on success
  86. *
  87. */
  88. function store( $data )
  89. {
  90. $result = true;
  91. $peid = $data['project_teamslist'];
  92. if ( $peid == null )
  93. {
  94. $query = " DELETE
  95. FROM #__joomleague_project_team
  96. WHERE project_id = '" . $data['id'] . "'";
  97. $this->_db->setQuery( $query );
  98. if ( !$this->_db->query() )
  99. {
  100. $this->setError( $this->_db->getErrorMsg() );
  101. $result = false;
  102. }
  103. }
  104. else
  105. {
  106. JArrayHelper::toInteger( $peid );
  107. $peids = implode( ',', $peid );
  108. $query = " DELETE
  109. FROM #__joomleague_project_team
  110. WHERE project_id = '" . $data['id'] . "' AND team_id NOT IN (" . $peids . ")";
  111. $this->_db->setQuery( $query );
  112. if ( !$this->_db->query() )
  113. {
  114. $this->setError( $this->_db->getErrorMsg() );
  115. $result = false;
  116. }
  117. $query = " UPDATE #__joomleague_match
  118. SET projectteam1_id = NULL
  119. WHERE projectteam1_id in (select id from #__joomleague_project_team
  120. where project_id = '" . $data['id'] . "'
  121. AND team_id NOT IN (" . $peids . "))";
  122. $this->_db->setQuery( $query );
  123. if ( !$this->_db->query() )
  124. {
  125. $this->setError( $this->_db->getErrorMsg() );
  126. $result = false;
  127. }
  128. $query = " UPDATE #__joomleague_match
  129. SET projectteam2_id = NULL
  130. WHERE projectteam2_id in (select id from #__joomleague_project_team
  131. where project_id = '" . $data['id'] . "'
  132. AND team_id NOT IN (" . $peids . "))";
  133. $this->_db->setQuery( $query );
  134. if ( !$this->_db->query() )
  135. {
  136. $this->setError( $this->_db->getErrorMsg() );
  137. $result = false;
  138. }
  139. }
  140. $ordering = "1";
  141. for ( $x = 0; $x < count( $data['project_teamslist'] ); $x++ )
  142. {
  143. $query = " INSERT IGNORE
  144. INTO #__joomleague_project_team
  145. (project_id, team_id, ordering)
  146. VALUES ( '" . $data['id'] . "', '".$data['project_teamslist'][$x] . "', '".$ordering++."')";
  147. $this->_db->setQuery( $query );
  148. if ( !$this->_db->query() )
  149. {
  150. $this->setError( $this->_db->getErrorMsg() );
  151. $result = false;
  152. }
  153. }
  154. return $result;
  155. }
  156. /**
  157. * Method to update checked project teams
  158. *
  159. * @access public
  160. * @return boolean True on success
  161. *
  162. */
  163. function storeshort( $cid, $data )
  164. {
  165. $result = true;
  166. for ( $x = 0; $x < count( $cid ); $x++ )
  167. {
  168. $tblProjectteam = JTable::getInstance('Projectteam','Table');
  169. $tblProjectteam->id = $cid[$x];
  170. $tblProjectteam->division_id = $data['division_id' . $cid[$x]];
  171. $tblProjectteam->start_points = $data['start_points' .$cid[$x]];
  172. $tblProjectteam->points_finally = $data['points_finally' .$cid[$x]];
  173. $tblProjectteam->neg_points_finally = $data['neg_points_finally' . $cid[$x]];
  174. $tblProjectteam->matches_finally = $data['matches_finally' . $cid[$x]];
  175. $tblProjectteam->won_finally = $data['won_finally' . $cid[$x]];
  176. $tblProjectteam->draws_finally = $data['draws_finally' . $cid[$x]];
  177. $tblProjectteam->lost_finally = $data['lost_finally' . $cid[$x]];
  178. $tblProjectteam->homegoals_finally = $data['homegoals_finally' .$cid[$x]];
  179. $tblProjectteam->guestgoals_finally = $data['guestgoals_finally' . $cid[$x]];
  180. $tblProjectteam->diffgoals_finally = $data['diffgoals_finally' . $cid[$x]];
  181. if (!$tblProjectteam->check())
  182. {
  183. $this->setError($tblProjectteam->getError());
  184. $result = false;
  185. }
  186. if (!$tblProjectteam->store())
  187. {
  188. $this->setError($tblProjectteam->getError());
  189. $result = false;
  190. }
  191. }
  192. return $result;
  193. }
  194. /**
  195. * Method to return the teams array (id, name)
  196. *
  197. * @access public
  198. * @return array
  199. * @since 0.1
  200. */
  201. function getTeams()
  202. {
  203. $query = ' SELECT id AS value,
  204. name AS text,
  205. info
  206. FROM #__joomleague_team
  207. ORDER BY text ASC ';
  208. $this->_db->setQuery( $query );
  209. if ( !$result = $this->_db->loadObjectList() )
  210. {
  211. $this->setError( $this->_db->getErrorMsg() );
  212. return false;
  213. }
  214. else
  215. {
  216. return $result;
  217. }
  218. }
  219. public function changeTeamId($arrOldTeamIds, $arrNewTeamIds, &$app)
  220. {
  221. $result = true;
  222. for ($t=0; $t < sizeof($arrOldTeamIds); $t++ )
  223. {
  224. $project_team_id = $arrOldTeamIds[$t];
  225. $team_id_new = $arrNewTeamIds[$project_team_id];
  226. $tblProjectTeam = JTable::getInstance('projectteam', 'Table');
  227. $tblProjectTeam->load($project_team_id);
  228. $tblOldTeam = JTable::getInstance('team', 'Table');
  229. $tblOldTeam->load($tblProjectTeam->team_id);
  230. $old_team_name = $tblOldTeam->name;
  231. $tblNewTeam = JTable::getInstance('team', 'Table');
  232. $tblNewTeam->load($team_id_new);
  233. $new_team_name = $tblNewTeam->name;
  234. $app->enqueueMessage(JText::sprintf('COM_JOOMLEAGUE_ADMIN_PROJECTTEAM_MODEL_ASSIGNED_OLD_TEAMNAME', $old_team_name, $new_team_name),'Notice');
  235. $tblProjectTeam->id = $project_team_id;
  236. $tblProjectTeam->team_id = $team_id_new;
  237. if (!$tblProjectTeam->store())
  238. {
  239. $this->setError($tblProjectTeam->getError());
  240. $result = false;
  241. break;
  242. }
  243. }
  244. return result;
  245. }
  246. /**
  247. * Method to return a Teams array (id,name)
  248. *
  249. * @access public
  250. * @return array seasons
  251. * @since 1.5.0a
  252. */
  253. function getAllTeams($pid)
  254. {
  255. $db = JFactory::getDbo();
  256. if ( $pid )
  257. {
  258. // jetzt brauchen wir noch das land der liga !
  259. $querycountry = "SELECT l.country
  260. from #__joomleague_league as l
  261. inner join #__joomleague_project as p
  262. on p.league_id = l.id
  263. where p.id = '$pid'
  264. ";
  265. $db->setQuery( $querycountry );
  266. $country = $db->loadResult();
  267. $query="SELECT t.id as value, t.name as text
  268. FROM #__joomleague_team as t
  269. INNER JOIN #__joomleague_club as c
  270. ON c.id = t.club_id
  271. WHERE c.country = '$country'
  272. ORDER BY t.name ASC
  273. ";
  274. }
  275. else
  276. {
  277. $query='SELECT id as value, name as text
  278. FROM #__joomleague_team
  279. ORDER BY name ASC ';
  280. }
  281. $db->setQuery($query);
  282. if (!$result=$db->loadObjectList())
  283. {
  284. $this->setError($db->getErrorMsg());
  285. return false;
  286. }
  287. foreach ($result as $teams){
  288. $teams->name = $teams->text;
  289. }
  290. return $result;
  291. }
  292. /**
  293. * Method to return the project teams array (id, name)
  294. *
  295. * @param $project_id
  296. * @access public
  297. * @return array
  298. * @since 0.1
  299. */
  300. function getProjectTeams($project_id=0)
  301. {
  302. $query = ' SELECT t.id AS value,
  303. t.name AS text,
  304. t.notes, pt.info
  305. FROM #__joomleague_team AS t
  306. LEFT JOIN #__joomleague_project_team AS pt ON pt.team_id = t.id
  307. WHERE pt.project_id = ' . $project_id . '
  308. ORDER BY text ASC ';
  309. $this->_db->setQuery( $query );
  310. if ( !$result = $this->_db->loadObjectList() )
  311. {
  312. $this->setError( $this->_db->getErrorMsg() );
  313. return false;
  314. }
  315. else
  316. {
  317. return $result;
  318. }
  319. }
  320. /**
  321. * copy teams to other projects
  322. *
  323. * @param int $dest destination project id
  324. * @param array $ptids teams to transfer
  325. */
  326. function copy($dest, $ptids)
  327. {
  328. if (!$dest)
  329. {
  330. $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_PROJECTTEAMS_Destination_project_required'));
  331. return false;
  332. }
  333. if (!is_array($ptids) || !count($ptids))
  334. {
  335. $this->setError(JText::_('COM_JOOMLEAGUE_ADMIN_PROJECTTEAMS_no_teams_to_copy'));
  336. return false;
  337. }
  338. // first copy the teams
  339. $query = ' INSERT INTO #__joomleague_project_team (team_id, project_id, info, picture, standard_playground, extended)'
  340. . ' SELECT team_id, '.$dest.', info, picture, standard_playground, extended '
  341. . ' FROM #__joomleague_project_team '
  342. . ' WHERE id IN (' . implode(',', $ptids).')';
  343. $this->_db->setQuery($query);
  344. $res = $this->_db->query();
  345. if (!$res)
  346. {
  347. $this->setError($this->_db->getErrorMsg());
  348. return false;
  349. }
  350. // now copy the players
  351. $query = ' INSERT INTO #__joomleague_team_player (projectteam_id, person_id, jerseynumber, picture, extended, published) '
  352. . ' SELECT dest.id AS projectteam_id, tp.person_id, tp.jerseynumber, tp.picture, tp.extended,tp.published '
  353. . ' FROM #__joomleague_team_player AS tp '
  354. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  355. . ' INNER JOIN #__joomleague_project_team AS dest ON pt.team_id = dest.team_id AND dest.project_id = '.$dest
  356. . ' WHERE pt.id IN (' . implode(',', $ptids).')';
  357. $this->_db->setQuery($query);
  358. $res = $this->_db->query();
  359. // and finally the staff
  360. $query = ' INSERT INTO #__joomleague_team_staff (projectteam_id, person_id, picture, extended, published) '
  361. . ' SELECT dest.id AS projectteam_id, tp.person_id, tp.picture, tp.extended,tp.published '
  362. . ' FROM #__joomleague_team_staff AS tp '
  363. . ' INNER JOIN #__joomleague_project_team AS pt ON pt.id = tp.projectteam_id '
  364. . ' INNER JOIN #__joomleague_project_team AS dest ON pt.team_id = dest.team_id AND dest.project_id = '.$dest
  365. . ' WHERE pt.id IN (' . implode(',', $ptids).')';
  366. $this->_db->setQuery($query);
  367. $res = $this->_db->query();
  368. if (!$res)
  369. {
  370. $this->setError($this->_db->getErrorMsg());
  371. return false;
  372. }
  373. return true;
  374. }
  375. /**
  376. * return count of projectteams
  377. *
  378. * @param int project_id
  379. * @return int
  380. */
  381. function getProjectTeamsCount($project_id)
  382. {
  383. $query='SELECT count(*) AS count
  384. FROM #__joomleague_project_team AS pt
  385. JOIN #__joomleague_project AS p on p.id = pt.project_id
  386. WHERE p.id='.$project_id;
  387. $this->_db->setQuery($query);
  388. return $this->_db->loadResult();
  389. }
  390. }
  391. ?>