Project.php 10 KB

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