PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/administrator/components/com_joomleague/models/teamplayers.php

https://gitlab.com/julienv/joomleague
PHP | 340 lines | 252 code | 25 blank | 63 comment | 21 complexity | 9337728f64409ce4d8840289042b8808 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 TeamPlayers Model
  17. *
  18. * @package JoomLeague
  19. * @since 0.1
  20. */
  21. class JoomleagueModelTeamPlayers extends JoomleagueModelList
  22. {
  23. var $_identifier = "teamplayers";
  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 ppl.firstname,
  30. ppl.lastname,
  31. ppl.nickname,
  32. ppl.height,
  33. ppl.weight, ppl.id, tp.id as tpid,
  34. ppl.id AS person_id,
  35. tp.*,
  36. tp.project_position_id,
  37. u.name AS editor
  38. FROM #__joomleague_person AS ppl
  39. INNER JOIN #__joomleague_team_player AS tp ON tp.person_id=ppl.id
  40. LEFT JOIN #__users AS u ON u.id=tp.checked_out '
  41. . $where
  42. . $orderby;
  43. return $query;
  44. }
  45. function _buildContentOrderBy()
  46. {
  47. $option = JRequest::getCmd('option');
  48. $mainframe = JFactory::getApplication();
  49. $filter_order = $mainframe->getUserStateFromRequest($option.'tp_filter_order', 'filter_order', 'ppl.ordering', 'cmd');
  50. $filter_order_Dir = $mainframe->getUserStateFromRequest($option.'tp_filter_order_Dir', 'filter_order_Dir', '', 'word');
  51. if ($filter_order=='ppl.lastname')
  52. {
  53. $orderby=' ORDER BY ppl.lastname '.$filter_order_Dir;
  54. }
  55. else
  56. {
  57. $orderby=' ORDER BY '.$filter_order.' '.$filter_order_Dir.',ppl.lastname ';
  58. }
  59. return $orderby;
  60. }
  61. function _buildContentWhere()
  62. {
  63. $option = JRequest::getCmd('option');
  64. $mainframe = JFactory::getApplication();
  65. $project_id=$mainframe->getUserState($option.'project');
  66. $team_id=$mainframe->getUserState($option.'project_team_id');
  67. $filter_state = $mainframe->getUserStateFromRequest( $option . 'tp_filter_state', 'filter_state', '', 'word' );
  68. $search = $mainframe->getUserStateFromRequest($option.'tp_search', 'search', '', 'string');
  69. $search_mode = $mainframe->getUserStateFromRequest($option.'tp_search_mode','search_mode', '', 'string');
  70. $search=JString::strtolower($search);
  71. $where=array();
  72. $where[]='tp.projectteam_id= '.$team_id;
  73. $where[]="ppl.published = '1'";
  74. if ($search)
  75. {
  76. if ($search_mode)
  77. {
  78. $where[]='(LOWER(ppl.lastname) LIKE '.$this->_db->Quote($search.'%') .
  79. 'OR LOWER(ppl.firstname) LIKE '.$this->_db->Quote($search.'%') .
  80. 'OR LOWER(ppl.nickname) LIKE '.$this->_db->Quote($search.'%').')';
  81. }
  82. else
  83. {
  84. $where[]='(LOWER(ppl.lastname) LIKE '.$this->_db->Quote('%'.$search.'%').
  85. 'OR LOWER(ppl.firstname) LIKE '.$this->_db->Quote('%'.$search.'%') .
  86. 'OR LOWER(ppl.nickname) LIKE '.$this->_db->Quote('%'.$search.'%').')';
  87. }
  88. }
  89. if ( $filter_state )
  90. {
  91. if ( $filter_state == 'P' )
  92. {
  93. $where[] = 'tp.published = 1';
  94. }
  95. elseif ($filter_state == 'U' )
  96. {
  97. $where[] = 'tp.published = 0';
  98. }
  99. }
  100. $where=(count($where) ? ' WHERE '.implode(' AND ',$where) : '');
  101. return $where;
  102. }
  103. /**
  104. * Method to update checked project teams
  105. *
  106. * @access public
  107. * @return boolean True on success
  108. *
  109. */
  110. function storeshort($cid,$data)
  111. {
  112. $result=true;
  113. for ($x=0; $x < count($cid); $x++)
  114. {
  115. $query=" UPDATE #__joomleague_team_player
  116. SET project_position_id='" . $data['project_position_id'.$cid[$x]] . "',
  117. jerseynumber='" . $data['jerseynumber'.$cid[$x]] . "',
  118. checked_out=0,
  119. checked_out_time=0
  120. WHERE id=" . $cid[$x];
  121. $this->_db->setQuery($query);
  122. if(!$this->_db->query())
  123. {
  124. $this->setError($this->_db->getErrorMsg());
  125. $result= false;
  126. }
  127. }
  128. return $result;
  129. }
  130. /**
  131. * Method to return the players array (projectid,teamid)
  132. *
  133. * @access public
  134. * @return array
  135. * @since 0.1
  136. */
  137. function getPersons()
  138. {
  139. $query=" SELECT id AS value,
  140. lastname,
  141. firstname,
  142. info,
  143. weight,
  144. height,
  145. picture,
  146. birthday,
  147. notes,
  148. nickname,
  149. knvbnr,
  150. country
  151. FROM #__joomleague_person
  152. WHERE published = '1'
  153. ORDER BY firstname ASC ";
  154. $this->_db->setQuery($query);
  155. if (!$result=$this->_db->loadObjectList())
  156. {
  157. $this->setError($this->_db->getErrorMsg());
  158. return false;
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Method to return a divisions array (id,name)
  164. *
  165. * @access public
  166. * @return array
  167. * @since 0.1
  168. */
  169. function getDivisions()
  170. {
  171. $option = JRequest::getCmd('option');
  172. $mainframe = JFactory::getApplication();
  173. $project_id=$mainframe->getUserState($option.'project');
  174. $query=" SELECT id AS value, name AS text
  175. FROM #__joomleague_division
  176. WHERE project_id=$project_id ORDER BY name ASC ";
  177. $this->_db->setQuery($query);
  178. if (!$result=$this->_db->loadObjectList())
  179. {
  180. $this->setError($this->_db->getErrorMsg());
  181. return false;
  182. }
  183. return $result;
  184. }
  185. /**
  186. * Method to return a positions array (id,position)
  187. *
  188. * @access public
  189. * @return array
  190. * @since 0.1
  191. */
  192. function getPositions()
  193. {
  194. $option = JRequest::getCmd('option');
  195. $mainframe = JFactory::getApplication();
  196. $project_id=$mainframe->getUserState($option.'project');
  197. $query=" SELECT pp.id AS value,name AS text
  198. FROM #__joomleague_position AS p
  199. LEFT JOIN #__joomleague_project_position AS pp ON pp.position_id=p.id
  200. WHERE pp.project_id=$project_id AND p.persontype=1
  201. ORDER BY ordering ";
  202. $this->_db->setQuery($query);
  203. if (!$result=$this->_db->loadObjectList())
  204. {
  205. $this->setError($this->_db->getErrorMsg());
  206. return false;
  207. }
  208. foreach ($result as $position){$position->text=JText::_($position->text);}
  209. return $result;
  210. }
  211. /**
  212. * return list of project teams for select options
  213. *
  214. * @return array
  215. */
  216. function getProjectTeamList()
  217. {
  218. $query=' SELECT t.id AS value,
  219. t.name AS text
  220. FROM #__joomleague_team AS t
  221. INNER JOIN #__joomleague_project_team AS tt ON tt.team_id=t.id
  222. WHERE tt.project_id='.$this->_project_id;
  223. $this->_db->setQuery($query);
  224. return $this->_db->loadObjectList();
  225. }
  226. /**
  227. * add the specified persons to team
  228. *
  229. * @param array int player ids
  230. * @param int team id
  231. * @return int number of row inserted
  232. */
  233. function storeAssigned($cid, $projectteam_id)
  234. {
  235. if (!count($cid) || !$projectteam_id){return 0;}
  236. $query=" SELECT p.id
  237. FROM #__joomleague_person AS p
  238. INNER JOIN #__joomleague_team_player AS tp ON tp.person_id=p.id
  239. WHERE tp.projectteam_id=".$this->_db->Quote($projectteam_id)." AND p.published = '1'";
  240. $this->_db->setQuery($query);
  241. $current=$this->_db->loadColumn();
  242. $added=0;
  243. foreach ($cid AS $pid)
  244. {
  245. if (!in_array($pid,$current))
  246. {
  247. $tblTeamplayer = JTable::getInstance( 'Teamplayer', 'Table' );
  248. $tblTeamplayer->person_id = $pid;
  249. $tblTeamplayer->projectteam_id = $projectteam_id;
  250. $tblTeamplayer->published = 1;
  251. $tblProjectTeam = JTable::getInstance( 'Projectteam', 'Table' );
  252. $tblProjectTeam->load($projectteam_id);
  253. if ( !$tblTeamplayer->check() )
  254. {
  255. $this->setError( $tblTeamplayer->getError() );
  256. continue;
  257. }
  258. // Get data from player
  259. $query = " SELECT picture, position_id
  260. FROM #__joomleague_person AS pl
  261. WHERE pl.id=". $this->_db->Quote($pid);
  262. $this->_db->setQuery( $query );
  263. $person = $this->_db->loadObject();
  264. if ( $person )
  265. {
  266. $query = "SELECT id FROM #__joomleague_project_position ";
  267. $query.= " WHERE position_id = " . $this->_db->Quote($person->position_id);
  268. $query.= " AND project_id = " . $this->_db->Quote($tblProjectTeam->project_id);
  269. $this->_db->setQuery($query);
  270. if ($resPrjPosition = $this->_db->loadObject())
  271. {
  272. $tblTeamplayer->project_position_id = $resPrjPosition->id;
  273. }
  274. $tblTeamplayer->picture = $person->picture;
  275. $tblTeamplayer->projectteam_id = $projectteam_id;
  276. }
  277. $query = " SELECT max(ordering) count
  278. FROM #__joomleague_team_player";
  279. $this->_db->setQuery( $query );
  280. $tp = $this->_db->loadObject();
  281. $tblTeamplayer->ordering = (int) $tp->count + 1;
  282. if ( !$tblTeamplayer->store() )
  283. {
  284. $this->setError( $tblTeamplayer->getError() );
  285. continue;
  286. }
  287. $added++;
  288. }
  289. }
  290. return $added;
  291. }
  292. /**
  293. * remove specified players from team
  294. * @param $cids player ids
  295. * @return int count of removed
  296. */
  297. function remove($cids)
  298. {
  299. $count=0;
  300. foreach($cids as $cid)
  301. {
  302. $object=&$this->getTable('teamplayer');
  303. if ($object->canDelete($cid) && $object->delete($cid))
  304. {
  305. $count++;
  306. }
  307. else
  308. {
  309. $this->setError(JText::sprintf('COM_JOOMLEAGUE_ADMIN_TEAMSTAFFS_MODEL_ERROR_REMOVE_TEAMPLAYER',$object->getError()));
  310. }
  311. }
  312. return $count;
  313. }
  314. }
  315. ?>