/core/classes/TBGEdition.class.php

https://github.com/disassembler/thebuggenie · PHP · 414 lines · 223 code · 41 blank · 150 comment · 24 complexity · 52e98567c5f282167205792de1d0f57c MD5 · raw file

  1. <?php
  2. /**
  3. * Edition class
  4. *
  5. * @author Daniel Andre Eikeland <zegenie@zegeniestudios.net>
  6. * @version 3.1
  7. * @license http://www.opensource.org/licenses/mozilla1.1.php Mozilla Public License 1.1 (MPL 1.1)
  8. * @package thebuggenie
  9. * @subpackage main
  10. */
  11. /**
  12. * Edition class
  13. *
  14. * @package thebuggenie
  15. * @subpackage main
  16. */
  17. class TBGEdition extends TBGOwnableItem
  18. {
  19. protected static $_b2dbtablename = 'TBGEditionsTable';
  20. /**
  21. * The project
  22. *
  23. * @var TBGProject
  24. * @Class TBGProject
  25. */
  26. protected $_project = null;
  27. /**
  28. * Editions components
  29. *
  30. * @var array|TBGComponent
  31. */
  32. protected $_components = null;
  33. /**
  34. * Edition builds
  35. *
  36. * @var array|TBGBuild
  37. */
  38. protected $_builds = null;
  39. protected $_description = '';
  40. protected $_assignees = null;
  41. /**
  42. * The editions documentation URL
  43. *
  44. * @var string
  45. */
  46. protected $_doc_url = '';
  47. static protected $_editions = null;
  48. public function _postSave($is_new)
  49. {
  50. if ($is_new)
  51. {
  52. TBGContext::setPermission("canseeedition", $this->getID(), "core", 0, TBGContext::getUser()->getGroup()->getID(), 0, true);
  53. TBGEvent::createNew('core', 'TBGEdition::createNew', $this)->trigger();
  54. }
  55. }
  56. /**
  57. * Retrieve all editions for a specific project
  58. *
  59. * @param integer $project_id
  60. *
  61. * @return array
  62. */
  63. public static function getAllByProjectID($project_id)
  64. {
  65. if (self::$_editions === null)
  66. {
  67. self::$_editions = array();
  68. }
  69. if (!array_key_exists($project_id, self::$_editions))
  70. {
  71. self::$_editions[$project_id] = array();
  72. if ($res = B2DB::getTable('TBGEditionsTable')->getByProjectID($project_id))
  73. {
  74. while ($row = $res->getNextRow())
  75. {
  76. $edition = TBGContext::factory()->TBGEdition($row->get(TBGEditionsTable::ID), $row);
  77. self::$_editions[$project_id][$edition->getID()] = $edition;
  78. }
  79. }
  80. }
  81. return self::$_editions[$project_id];
  82. }
  83. /**
  84. * Constructor function
  85. *
  86. * @param B2DBRow $row
  87. */
  88. public function _construct(B2DBRow $row, $foreign_key = null)
  89. {
  90. TBGEvent::createNew('core', 'TBGEdition::__construct', $this)->trigger();
  91. }
  92. /**
  93. * Populates components inside the edition
  94. *
  95. * @return void
  96. */
  97. protected function _populateComponents()
  98. {
  99. if ($this->_components === null)
  100. {
  101. $this->_components = array();
  102. if ($res = B2DB::getTable('TBGEditionComponentsTable')->getByEditionID($this->getID()))
  103. {
  104. while ($row = $res->getNextRow())
  105. {
  106. $this->_components[$row->get(TBGEditionComponentsTable::COMPONENT)] = TBGContext::factory()->TBGComponent($row->get(TBGEditionComponentsTable::COMPONENT));
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * Returns an array with all components
  113. *
  114. * @return array|TBGComponent
  115. */
  116. public function getComponents()
  117. {
  118. $this->_populateComponents();
  119. return $this->_components;
  120. }
  121. /**
  122. * Whether or not this edition has a component enabled
  123. *
  124. * @param TBGComponent|integer $component The component to check for
  125. *
  126. * @return boolean
  127. */
  128. public function hasComponent($c_id)
  129. {
  130. if ($c_id instanceof TBGComponent)
  131. {
  132. $c_id = $c_id->getID();
  133. }
  134. return array_key_exists($c_id, $this->getComponents());
  135. }
  136. /**
  137. * Whether this edition has a description set
  138. *
  139. * @return string
  140. */
  141. public function hasDescription()
  142. {
  143. return (bool) $this->getDescription();
  144. }
  145. /**
  146. * Adds an existing component to the edition
  147. *
  148. * @param TBGComponent|integer $component
  149. */
  150. public function addComponent($c_id)
  151. {
  152. if ($c_id instanceof TBGComponent)
  153. {
  154. $c_id = $c_id->getID();
  155. }
  156. return B2DB::getTable('TBGEditionComponentsTable')->addEditionComponent($this->getID(), $c_id);
  157. }
  158. /**
  159. * Removes an existing component from the edition
  160. *
  161. * @param TBGComponent|integer $c_id
  162. */
  163. public function removeComponent($c_id)
  164. {
  165. if ($c_id instanceof TBGComponent)
  166. {
  167. $c_id = $c_id->getID();
  168. }
  169. B2DB::getTable('TBGEditionComponentsTable')->removeEditionComponent($this->getID(), $c_id);
  170. }
  171. /**
  172. * Returns the description
  173. *
  174. * @return string
  175. */
  176. public function getDescription()
  177. {
  178. return $this->_description;
  179. }
  180. /**
  181. * Returns the documentation url
  182. *
  183. * @return string
  184. */
  185. public function getDocumentationURL()
  186. {
  187. return $this->_doc_url;
  188. }
  189. /**
  190. * Returns the component specified
  191. *
  192. * @param integer $c_id
  193. *
  194. * @return TBGComponent
  195. */
  196. public function getComponent($c_id)
  197. {
  198. $this->_populateComponents();
  199. if (array_key_exists($c_id, $this->_components))
  200. {
  201. return $this->_components[$c_id];
  202. }
  203. return null;
  204. }
  205. /**
  206. * Populates builds inside the edition
  207. *
  208. * @return void
  209. */
  210. protected function _populateBuilds()
  211. {
  212. if ($this->_builds === null)
  213. {
  214. $this->_builds = array();
  215. if ($res = B2DB::getTable('TBGBuildsTable')->getByEditionID($this->getID()))
  216. {
  217. while ($row = $res->getNextRow())
  218. {
  219. $this->_builds[$row->get(TBGBuildsTable::ID)] = TBGContext::factory()->TBGBuild($row->get(TBGBuildsTable::ID), $row);
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * Returns an array with all builds
  226. *
  227. * @return array|TBGBuild
  228. */
  229. public function getBuilds()
  230. {
  231. $this->_populateBuilds();
  232. return $this->_builds;
  233. }
  234. /**
  235. * Returns the default build
  236. *
  237. * @return TBGBuild
  238. */
  239. public function getDefaultBuild()
  240. {
  241. $this->_populateBuilds();
  242. if (count($this->_builds) > 0)
  243. {
  244. foreach ($this->_builds as $build)
  245. {
  246. if ($build->isDefault() && $build->isLocked() == false)
  247. {
  248. return $build;
  249. }
  250. }
  251. return array_slice($this->_builds, 0, 1);
  252. }
  253. return 0;
  254. }
  255. public function _sortBuildsByReleaseDate(TBGBuild $build1, TBGBuild $build2)
  256. {
  257. if ($build1->getReleaseDate() == $build2->getReleaseDate())
  258. {
  259. return 0;
  260. }
  261. return ($build1->getReleaseDate() < $build2->getReleaseDate()) ? -1 : 1;
  262. }
  263. /**
  264. * Returns the latest build
  265. *
  266. * @return TBGBuild
  267. */
  268. public function getLatestBuild()
  269. {
  270. $this->_populateBuilds();
  271. if (count($this->getBuilds()) > 0)
  272. {
  273. $builds = usort($this->getBuilds(), array($this, '_sortBuildsByReleaseDate'));
  274. return array_slice($builds, 0, 1);
  275. }
  276. return null;
  277. }
  278. /**
  279. * Returns the parent project
  280. *
  281. * @return TBGProject
  282. */
  283. public function getProject()
  284. {
  285. return $this->_getPopulatedObjectFromProperty('_project');
  286. }
  287. public function setProject($project)
  288. {
  289. $this->_project = $project;
  290. }
  291. /**
  292. * Add an assignee to the edition
  293. *
  294. * @param TBGIdentifiableClass $assignee
  295. * @param integer $role
  296. *
  297. * @return boolean
  298. */
  299. public function addAssignee(TBGIdentifiableClass $assignee, $role)
  300. {
  301. $retval = TBGEditionAssigneesTable::getTable()->addAssigneeToEdition($this->getID(), $assignee, $role);
  302. $this->applyInitialPermissionSet($assignee, $role);
  303. return $retval;
  304. }
  305. protected function _populateAssignees()
  306. {
  307. if ($this->_assignees === null)
  308. {
  309. $this->_assignees = TBGEditionAssigneesTable::getTable()->getByEditionID($this->getID());
  310. }
  311. }
  312. /**
  313. * Get assignees for this edition
  314. *
  315. * @return array
  316. */
  317. public function getAssignees()
  318. {
  319. $this->_populateAssignees();
  320. return $this->_assignees;
  321. }
  322. public function getAssignedUsers()
  323. {
  324. $this->_populateAssignees();
  325. $users = array();
  326. foreach (array_keys($this->_assignees['users']) as $user_id)
  327. {
  328. $users[$user_id] = TBGContext::factory()->TBGUser($user_id);
  329. }
  330. return $users;
  331. }
  332. public function getAssignedTeams()
  333. {
  334. $this->_populateAssignees();
  335. $teams = array();
  336. foreach (array_keys($this->_assignees['teams']) as $team_id)
  337. {
  338. $teams[$team_id] = TBGContext::factory()->TBGTeam($team_id);
  339. }
  340. return $teams;
  341. }
  342. /**
  343. * Set the edition description
  344. *
  345. * @param string $description
  346. */
  347. public function setDescription($description)
  348. {
  349. $this->_description = $description;
  350. }
  351. /**
  352. * Set the editions documentation url
  353. *
  354. * @param string $doc_url
  355. */
  356. public function setDocumentationURL($doc_url)
  357. {
  358. $this->_doc_url = $doc_url;
  359. }
  360. public function _preDelete()
  361. {
  362. B2DB::getTable('TBGEditionAssigneesTable')->deleteByEditionID($this->getID());
  363. }
  364. /**
  365. * Whether or not the current user can access the edition
  366. *
  367. * @return boolean
  368. */
  369. public function hasAccess()
  370. {
  371. return ($this->getProject()->canSeeAllEditions() || TBGContext::getUser()->hasPermission('canseeedition', $this->getID()));
  372. }
  373. }