Project.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. require_once(PATH::ENTITY . "Entity.php");
  3. require_once(PATH::ENTITY . "License.php");
  4. require_once(PATH::ENTITY . "Project_Type.php");
  5. require_once(PATH::ENTITY . "Project_Image.php");
  6. require_once(PATH::ENTITY . "Project_Url.php");
  7. /**
  8. * Project.
  9. *
  10. * Represents an object from the table 'project'.
  11. */
  12. class Project extends Entity{
  13. /**
  14. * @var int Project identifier.
  15. */
  16. private $id;
  17. /**
  18. * @var string Permalink for linking the project (relative).
  19. */
  20. private $permalink;
  21. /**
  22. * @var int Project index for sorting.
  23. */
  24. private $index;
  25. /**
  26. * @var int Project type identifier.
  27. */
  28. private $type_id;
  29. /**
  30. * @var Project_Type Type of project.
  31. */
  32. private $type;
  33. /**
  34. * @var string Project title in the selected language.
  35. */
  36. private $title;
  37. /**
  38. * @var string Project logo filename.
  39. */
  40. private $logo;
  41. /**
  42. * @var string Project summary in the selected language.
  43. */
  44. private $header;
  45. /**
  46. * @var String Project description in the selected language.
  47. */
  48. private $text;
  49. /**
  50. * @var int License identifier.
  51. */
  52. private $license_id;
  53. /**
  54. * @var License Project license.
  55. */
  56. private $license;
  57. /**
  58. * @var Project_Image[] List of project images.
  59. */
  60. private $images = [];
  61. /**
  62. * @var Project_Tag[] List of project tags.
  63. */
  64. private $tags = [];
  65. /**
  66. * @var Project_URL[] List of the project URLs
  67. */
  68. private $urls = [];
  69. /**
  70. * @var bool[] Control flags to check loading status.
  71. */
  72. private $load_flags = [
  73. "type" => false,
  74. "images" => false,
  75. "tags" => false,
  76. "urls" => false
  77. ];
  78. /**
  79. * Constructor.
  80. *
  81. * Searches the database and retrieves the information about the
  82. * project, populating it and it's items.
  83. *
  84. * @param string $id Identifier or permalink of the project.
  85. */
  86. public function __construct($id){
  87. Log::debug("Loading project with ID: " . $id);
  88. $statement = get_context()->get_db()->prepare("
  89. SELECT
  90. id,
  91. permalink,
  92. idx,
  93. type,
  94. title,
  95. logo,
  96. header,
  97. text,
  98. license
  99. FROM project
  100. WHERE
  101. id = :id OR
  102. permalink = :permalink
  103. LIMIT 1
  104. ");
  105. $statement->bindValue(':id', $id, PDO::PARAM_INT);
  106. $statement->bindValue(':permalink', $id, PDO::PARAM_STR);
  107. $statement->execute();
  108. $r_project = $statement->fetch(PDO::FETCH_ASSOC);
  109. if ($r_project !== false){
  110. $this->id = $r_project["id"];
  111. $this->permalink = $r_project["permalink"];
  112. $this->idx = $r_project["idx"];
  113. $this->title = Text::get($r_project["title"]);
  114. $this->logo = $r_project["logo"];
  115. $this->header = Text::get($r_project["header"]);
  116. $this->text = Text::get($r_project["text"]);
  117. $this->type_id = $r_project["type"];
  118. $this->license_id = $r_project["license"];
  119. $this->mark_as_loaded(true);
  120. }
  121. else{
  122. Log::debug("Project with id '$id' doesn't exist");
  123. }
  124. }
  125. /**
  126. * Retrieves the project identifier
  127. *
  128. * @return int Project ID.
  129. */
  130. public function get_id(){
  131. return $this->id;
  132. }
  133. /**
  134. * Retrieves the project permalink
  135. *
  136. * @return string Project permalink
  137. */
  138. public function get_permalink(){
  139. return $this->permalink;
  140. }
  141. /**
  142. * Retrieves the project index for sorting purpuses.
  143. *
  144. * @return int Project index
  145. */
  146. public function get_index(){
  147. return $this->index;
  148. }
  149. /**
  150. * Loads the project type information.
  151. *
  152. * Not exposed, must be called before accessing the project type.
  153. */
  154. private function load_type(){
  155. if ($this->get_flag("type") === false){
  156. $this->type = new Project_Type($this->type_id);
  157. $this->set_flag("type", true);
  158. }
  159. }
  160. /**
  161. * Retrieves the project type.
  162. *
  163. * @return Project_Type The project type.
  164. */
  165. public function get_type(){
  166. if ($this->get_flag("type") === false){
  167. $this->load_type();
  168. }
  169. return $this->type;
  170. }
  171. /**
  172. * Retrieves the project title.
  173. *
  174. * @return string Title.
  175. */
  176. public function get_title(){
  177. return $this->title;
  178. }
  179. /**
  180. * Retrieves the project logo.
  181. *
  182. * @return string The logo filename.
  183. */
  184. public function get_logo(){
  185. return $this->logo;
  186. }
  187. /**
  188. * Retrieves a short project description.
  189. *
  190. * @return string Project header.
  191. */
  192. public function get_header(){
  193. return $this->header;
  194. }
  195. /**
  196. * Retrieves the project description.
  197. *
  198. * @return string Project description text.
  199. */
  200. public function get_text(){
  201. return $this->text;
  202. }
  203. /**
  204. * Loads the project license information.
  205. *
  206. * Not exposed, must be called before accessing the project license.
  207. */
  208. private function load_license(){
  209. if ($this->get_flag("license") === false){
  210. $this->type = new License($this->licenses_id);
  211. $this->set_flag("license", true);
  212. }
  213. }
  214. /**
  215. * Retrieves the project license.
  216. *
  217. * @return License Project license.
  218. */
  219. public function get_license(){
  220. if ($this->get_flag("license") === false){
  221. $this->load_license();
  222. }
  223. return $this->license;
  224. }
  225. /**
  226. * Loads the project images.
  227. *
  228. * Not exposed, must be called before accessing the project images.
  229. */
  230. private function load_images(){
  231. if ($this->get_flag("images") === false){
  232. $statement = get_context()->get_db()->prepare("
  233. SELECT id
  234. FROM project_image
  235. WHERE project = :id
  236. ORDER BY idx
  237. ");
  238. $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
  239. $statement->execute();
  240. while ($r_image = $statement->fetch(PDO::FETCH_ASSOC)){
  241. array_push($this->images, new Project_Image($r_image["id"]));
  242. }
  243. $this->set_flag("images", true);
  244. }
  245. }
  246. /**
  247. * Retrieves the projcet images
  248. *
  249. * @return Project_Image[] List of the project images.
  250. */
  251. public function get_images(){
  252. if ($this->get_flag("images") === false){
  253. $this->load_images();
  254. }
  255. return $this->images;
  256. }
  257. /**
  258. * Loads the project tags.
  259. *
  260. * Not exposed, must be called before accessing the project tags.
  261. */
  262. private function load_tags(){
  263. if ($this->get_flag("tags") === false){
  264. $statement = get_context()->get_db()->prepare("
  265. SELECT id
  266. FROM project_tag
  267. WHERE project = :id
  268. ");
  269. $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
  270. $statement->execute();
  271. while ($r_tag = $statement->fetch(PDO::FETCH_ASSOC)){
  272. array_push($this->tags, Text::get($r_tag["id"]));
  273. }
  274. $this->set_flag("tags", true);
  275. }
  276. }
  277. /**
  278. * Retrieves the project tags.
  279. *
  280. * @return Project_Tag[] Project tags.
  281. */
  282. public function get_tags(){
  283. if ($this->get_flag("tags") === false){
  284. $this->load_tags();
  285. }
  286. return $this->tags;
  287. }
  288. /**
  289. * Loads the project URLs.
  290. *
  291. * Not exposed, must be called before accessing the project URLs.
  292. */
  293. private function load_urls(){
  294. if ($this->get_flag("urls") === false){
  295. $statement = get_context()->get_db()->prepare("
  296. SELECT id
  297. FROM project_url
  298. WHERE project = :id
  299. ");
  300. $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
  301. $statement->execute();
  302. while ($r_url = $statement->fetch(PDO::FETCH_ASSOC)){
  303. array_push($this->urls, new Project_Url($r_url["id"]));
  304. }
  305. $this->set_flag("urls", true);
  306. }
  307. }
  308. /**
  309. * Retrieves the project URLs
  310. *
  311. * @return Project_URL[] List of the project URLs.
  312. */
  313. public function get_urls(){
  314. if ($this->get_flag("urls") === false){
  315. $this->load_urls();
  316. }
  317. return $this->urls;
  318. }
  319. /**
  320. * Retrieves a state flag
  321. *
  322. * @return boolean Flag value, false if it doesn't exist.
  323. */
  324. private function get_flag($flag){
  325. $value = false;
  326. if (array_key_exists($flag , $this->load_flags)){
  327. $value = $this->load_flags[$flag];
  328. }
  329. return $value;
  330. }
  331. /**
  332. * Sets a load status flag.
  333. *
  334. * If, once set, all flags are set to true, mark_as_complete(true)
  335. * will be automatically called.
  336. *
  337. * @param string $flag Flag name.
  338. * @param boolean $value Flag name.
  339. */
  340. private function set_flag($flag, $value){
  341. $key_found = false;
  342. $keys = array_keys($this->load_flags);
  343. foreach($keys as $key){
  344. if ($key === $flag){
  345. $key_found = true;
  346. break;
  347. }
  348. }
  349. if ($key_found == false){
  350. Log::warn("Trying to set non-existing flag '$flag' in project");
  351. return false;
  352. }
  353. else{
  354. if ($value !== true && $value !== false){
  355. Log::warn("Trying to set project flag '$flag' to an invalid value: " . $value);
  356. return false;
  357. }
  358. else{
  359. $this->load_flags[$flag] = $value;
  360. // Once set, loop all keys to check if they are all true
  361. $all_loaded = true;
  362. foreach($this->load_flags as $value){
  363. if ($value !== true){
  364. $all_loaded = false;
  365. break;
  366. }
  367. }
  368. $this->mark_as_complete($all_loaded);
  369. return true;
  370. }
  371. }
  372. }
  373. }