PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/com_joomleague/administrator/components/com_joomleague/models/teamstaffs.php

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