/com_tracks/administrator/components/com_tracks/models/projectindividual.php

https://github.com/mermandamus/Joomla-Tracks · PHP · 271 lines · 156 code · 28 blank · 87 comment · 18 complexity · 9530694e448b34dd1258312e7680b54d MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: projectindividual.php 23 2008-02-15 08:36:46Z julienv $
  4. * @package JoomlaTracks
  5. * @copyright Copyright (C) 2008 Julien Vonthron. All rights reserved.
  6. * @license GNU/GPL, see LICENSE.php
  7. * Joomla Tracks is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. // Check to ensure this file is included in Joomla!
  14. defined('_JEXEC') or die();
  15. jimport('joomla.application.component.model');
  16. require_once (JPATH_COMPONENT.DS.'models'.DS.'item.php');
  17. /**
  18. * Joomla Tracks Component Projectindividual Model
  19. *
  20. * @author Julien Vonthron <julien.vonthron@gmail.com>
  21. * @package Tracks
  22. * @since 0.1
  23. */
  24. class TracksModelProjectindividual extends TracksModelItem
  25. {
  26. /**
  27. * Method to remove a projectindividual
  28. *
  29. * @access public
  30. * @return boolean|int number of deleted rows
  31. * @since 0.1
  32. */
  33. function delete($cid = array())
  34. {
  35. // TODO: delete the subobjects too ?
  36. $result = false;
  37. if (count( $cid ))
  38. {
  39. JArrayHelper::toInteger($cid);
  40. $cids = implode( ',', $cid );
  41. // delete results first
  42. $query = 'DELETE rr FROM #__tracks_projects_individuals AS pi, #__tracks_rounds_results AS rr '
  43. . ' WHERE pi.id IN ( '.$cids.' ) AND pi.individual_id = rr.individual_id ';
  44. $this->_db->setQuery( $query );
  45. if(!$this->_db->query())
  46. {
  47. $this->setError($this->_db->getErrorMsg());
  48. return false;
  49. }
  50. else
  51. {
  52. // now delete the individuals from the project
  53. $query = 'DELETE FROM #__tracks_projects_individuals '
  54. . ' WHERE id IN ( '.$cids.' ) ';
  55. $this->_db->setQuery( $query );
  56. if(!$this->_db->query())
  57. {
  58. $this->setError($this->_db->getErrorMsg());
  59. return false;
  60. }
  61. }
  62. }
  63. return $this->_db->getAffectedRows();
  64. }
  65. /**
  66. * Method to (un)publish a projectindividual
  67. *
  68. * @access public
  69. * @return boolean True on success
  70. * @since 0.1
  71. */
  72. function publish($cid = array(), $publish = 1)
  73. {
  74. $user =& JFactory::getUser();
  75. if (count( $cid ))
  76. {
  77. JArrayHelper::toInteger($cid);
  78. $cids = implode( ',', $cid );
  79. $query = 'UPDATE #__tracks_projects_individuals'
  80. . ' SET published = '.(int) $publish
  81. . ' WHERE id IN ( '.$cids.' )'
  82. . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $user->get('id').' ) )'
  83. ;
  84. $this->_db->setQuery( $query );
  85. if (!$this->_db->query()) {
  86. $this->setError($this->_db->getErrorMsg());
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. /**
  93. * Method to load content projectindividual data
  94. *
  95. * @access private
  96. * @return boolean True on success
  97. * @since 1.5
  98. */
  99. function _loadData()
  100. {
  101. // Lets load the content if it doesn't already exist
  102. if (empty($this->_data))
  103. {
  104. $query = ' SELECT obj.*, p.first_name, p.last_name, t.name as team_name '
  105. . ' FROM #__tracks_projects_individuals AS obj '
  106. . ' INNER JOIN #__tracks_individuals AS p ON (p.id = obj.individual_id) '
  107. . ' LEFT JOIN #__tracks_teams AS t ON (t.id = obj.team_id) '
  108. . ' WHERE obj.id = '.(int) $this->_id;
  109. $this->_db->setQuery($query);
  110. $this->_data = $this->_db->loadObject();
  111. return (boolean) $this->_data;
  112. }
  113. return true;
  114. }
  115. /**
  116. * Method to return a individuals array (id, name)
  117. *
  118. * @access public
  119. * @return array seasons
  120. * @since 1.5
  121. */
  122. function getIndividuals()
  123. {
  124. $query = 'SELECT id, CONCAT( last_name, ", ", first_name ) as name '
  125. . ' FROM #__tracks_individuals '
  126. . ' ORDER BY name ASC ';
  127. $this->_db->setQuery( $query );
  128. if ( !$result = $this->_db->loadObjectList() ) {
  129. $this->setError($this->_db->getErrorMsg());
  130. return false;
  131. }
  132. else return $result;
  133. }
  134. /**
  135. * Method to return a teams array (id, name)
  136. *
  137. * @access public
  138. * @return array seasons
  139. * @since 1.5
  140. */
  141. function getTeams()
  142. {
  143. $query = 'SELECT id, name'
  144. . ' FROM #__tracks_teams '
  145. . ' ORDER BY name ASC ';
  146. $this->_db->setQuery( $query );
  147. if ( !$result = $this->_db->loadObjectList() ) {
  148. $this->setError($this->_db->getErrorMsg());
  149. return false;
  150. }
  151. else return $result;
  152. }
  153. /**
  154. * Method to store the item
  155. *
  156. * @access public
  157. * @return boolean True on success
  158. * @since 1.5
  159. */
  160. function store($data)
  161. {
  162. $row =& $this->getTable();
  163. // Bind the form fields to the items table
  164. if (!$row->bind($data)) {
  165. $this->setError($this->_db->getErrorMsg());
  166. return false;
  167. }
  168. // Create the timestamp for the date
  169. $row->checked_out_time = gmdate('Y-m-d H:i:s');
  170. // Make sure the item is valid
  171. if (!$row->check()) {
  172. $this->setError($this->_db->getErrorMsg());
  173. return false;
  174. }
  175. // Store the item to the database
  176. if (!$row->store()) {
  177. $this->setError($this->_db->getErrorMsg());
  178. return false;
  179. }
  180. return $row->id;
  181. }
  182. /**
  183. * Method to store mutliple rows
  184. *
  185. * @access public
  186. * @param array rows to be inserted
  187. * @return false|int count of inserted rows
  188. */
  189. function assign($rows)
  190. {
  191. $i = 0;
  192. // count inserted
  193. $rec = 0;
  194. for ($i=0, $n=count( $rows ); $i < $n; $i++)
  195. {
  196. $row =& $rows[$i];
  197. $query = 'SELECT id '
  198. . ' FROM #__tracks_projects_individuals '
  199. . ' WHERE individual_id = ' . intval($row->individual_id)
  200. . ' AND project_id = ' . intval($row->project_id);
  201. $this->_db->setQuery( $query );
  202. if ( count( $this->_db->loadObjectList()) ) {
  203. // already assigned
  204. continue;
  205. }
  206. $new =& $this->getTable();
  207. $new->bind($row);
  208. // Store the item to the database
  209. if (!$new->store()) {
  210. $this->setError($this->_db->getErrorMsg());
  211. return false;
  212. }
  213. $rec++;
  214. }
  215. return $rec;
  216. }
  217. /**
  218. * Method to initialise the projectindividual data
  219. *
  220. * @access private
  221. * @return boolean True on success
  222. * @since 1.5
  223. */
  224. function _initData()
  225. {
  226. // Lets load the content if it doesn't already exist
  227. if (empty($this->_data))
  228. {
  229. $projectindividual = new stdClass();
  230. $projectindividual->id = 0;
  231. $projectindividual->individual_id = 0;
  232. $projectindividual->project_id = 0;
  233. $projectindividual->team_id = 0;
  234. $projectindividual->number = 0;
  235. $projectindividual->checked_out = 0;
  236. $projectindividual->checked_out_time = 0;
  237. $this->_data = $projectindividual;
  238. return (boolean) $this->_data;
  239. }
  240. return true;
  241. }
  242. }
  243. ?>