/src/lib/Database/Entities/Article.php

https://gitlab.com/gothcon/cthulhu · PHP · 293 lines · 199 code · 36 blank · 58 comment · 57 complexity · 103c500ccb2fef721d6d121bb1040f04 MD5 · raw file

  1. <?php
  2. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "ReadWriteEntity.php");
  3. /**
  4. * @property string $name
  5. * @property int $l
  6. * @property int $r
  7. * @property int $p
  8. * @property int $image_id
  9. * @property decimal $price
  10. * @property string $available_from
  11. * @property string $available_to
  12. * @property string $type
  13. * @property string $description
  14. * @property int $image_id
  15. */
  16. class Article extends ReadWriteEntity{
  17. /**
  18. *
  19. * @return ArticleRepository
  20. */
  21. public static function getRepository() {
  22. return parent::getRepository();
  23. }
  24. public function __construct(){
  25. parent::__construct();
  26. $this->persistedProperties["name"] = null;
  27. $this->persistedProperties["l"] = null;
  28. $this->persistedProperties["r"] = null;
  29. $this->persistedProperties["p"] = null;
  30. $this->persistedProperties["price"] = 0.00;
  31. $this->persistedProperties["available_from"] = null;
  32. $this->persistedProperties["available_to"] = null;
  33. $this->persistedProperties["type"] = "category";
  34. $this->persistedProperties["description"] = "";
  35. $this->persistedProperties["image_id"] = null;
  36. }
  37. private $availableFromDate = null;
  38. private $availableFromTime =null;
  39. private $availableToTime = null;
  40. private $availableToDate = null;
  41. /** @var Image */
  42. private $_image;
  43. private function isNullOrEmpty($value){
  44. return is_null($value) || strlen(trim($value)) == 0;
  45. }
  46. private function isDate($date){
  47. if($this->isNullOrEmpty($date))
  48. return false;
  49. if(strtotime("{$date} 00:00:00") <= 0)
  50. return false;
  51. return true;
  52. }
  53. private function isTime($time){
  54. if($this->isNullOrEmpty($time))
  55. return false;
  56. $time = static::createTime($time);
  57. if($time !== false && strtotime("2000-01-01 {$time}") <= 0)
  58. return false;
  59. return true;
  60. }
  61. private function updateAvailableFrom(){
  62. if(!is_null($this->availableFromDate) && !is_null($this->availableFromTime)){
  63. parent::__set("available_from","{$this->availableFromDate} {$this->availableFromTime}");
  64. }
  65. else{
  66. parent::__set("available_from",null);
  67. }
  68. }
  69. private function updateAvailableTo(){
  70. if(!is_null($this->availableToDate) && !is_null($this->availableToTime)){
  71. parent::__set("available_to","{$this->availableToDate} {$this->availableToTime}");
  72. }
  73. else{
  74. parent::__set("available_to",null);
  75. }
  76. }
  77. public function __set($name,$value){
  78. if($name == "available_from"){
  79. if(!is_null($value) && strtotime($value) > 0){
  80. $this->availableFromDate = substr($value,0,10);
  81. $this->availableFromTime = substr($value,11,8);
  82. parent::__set("available_from",$value);
  83. }else{
  84. $this->availableFromDate = null;
  85. $this->availableFromTime = null;
  86. }
  87. }
  88. else if($name == "available_to"){
  89. if(!is_null($value) && strtotime($value) > 0){
  90. $this->availableToDate = substr($value,0,10);
  91. $this->availableToTime = substr($value,11,8);
  92. parent::__set("available_to",$value);
  93. }else{
  94. $this->availableToDate = null;
  95. $this->availableToTime = null;
  96. parent::__set("available_to",null);
  97. }
  98. }
  99. else if($name == "available_to_time"){
  100. $value = !$value ? "23:59:59" : static::createTime($value);
  101. $this->availableToTime = $this->isTime($value) ? $value : null;
  102. $this->updateAvailableTo();
  103. }
  104. else if($name == "available_to_date"){
  105. $this->availableToDate = $this->isDate($value) ? $value : null;
  106. $this->updateAvailableTo();
  107. }
  108. else if($name == "available_from_time"){
  109. $value = !$value ? "00:00:00" : static::createTime($value);
  110. $this->availableFromTime = $this->isTime($value) ? $value : null;
  111. $this->updateAvailableFrom();
  112. }
  113. else if($name == "available_from_date"){
  114. $this->availableFromDate = $this->isDate($value) ? $value : null;
  115. $this->updateAvailableFrom();
  116. }
  117. else{
  118. parent::__set($name,$value);
  119. }
  120. }
  121. public function __get($name){
  122. if($name == "available_to_time"){
  123. return $this->availableToTime;
  124. }
  125. else if($name == "available_to_date"){
  126. return $this->availableToDate;
  127. }
  128. else if($name == "available_from_time"){
  129. return $this->availableFromTime;
  130. }
  131. else if($name == "available_from_date"){
  132. return $this->availableFromDate;
  133. }
  134. else{
  135. return parent::__get($name);
  136. }
  137. }
  138. public function isAvailable(){
  139. $now = time();
  140. return ((is_null($this->available_from) || $now >= strtotime($this->available_from)) && (is_null($this->available_to) || $now <= strtotime($this->available_to)));
  141. }
  142. private static function createTime($value){
  143. $value = str_replace(array(":"," "),"",$value);
  144. if(!is_numeric($value))
  145. return false;
  146. $value = strlen($value) == 1 ? "0".$value : $value;
  147. $value = str_pad($value,6,"0");
  148. $value = str_split($value,2);
  149. return implode(":",$value);
  150. }
  151. /**
  152. * @return Article
  153. */
  154. public static function loadRootNode(){
  155. return static::getRepository()->getRootNode();
  156. }
  157. /**
  158. * @return array
  159. */
  160. public function loadPath(){
  161. return static::getRepository()->loadPath($this);
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function loadSiblings(){
  167. return static::getRepository()->loadSiblings($this);
  168. }
  169. /**
  170. * @return array
  171. */
  172. public function loadChildren(){
  173. return static::getRepository()->getChildren($this);
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function loadAllChildren($type = null){
  179. return Article::getRepository()->getAllChildren($this,$type);
  180. }
  181. public function loadPublicChildren(){
  182. return static::getRepository()->loadPublicChildren($this);
  183. }
  184. /**
  185. * @return array
  186. */
  187. public static function loadChildrenByParent($parentId){
  188. return static::getRepository()->getChildren($parentId);
  189. }
  190. /**
  191. * @return array
  192. */
  193. public function loadPublicSiblings(){
  194. return static::loadPublicSiblings($this);
  195. }
  196. /**
  197. * @param int $id
  198. * @return Article
  199. */
  200. public static function loadById($id) {
  201. return parent::loadById($id);
  202. }
  203. /**
  204. * @return Article[]
  205. */
  206. public static function loadList() {
  207. return parent::loadList();
  208. }
  209. public function __toString() {
  210. return "Article #{$this->id}: {$this->name}";
  211. }
  212. /**
  213. *
  214. * @param Image $image
  215. */
  216. public function setImage($image){
  217. $this->_image = $image;
  218. if($image->id > 0)
  219. $this->persistedProperties["image_id"] = $image->id;
  220. }
  221. /**
  222. *
  223. * @return Image
  224. */
  225. public function getImage(){
  226. return $this->_image;
  227. }
  228. /**
  229. *
  230. * @return bool
  231. */
  232. public function save($overrideValidation = false) {
  233. static::getRepository()->begin();
  234. if($this->type != "signup_slot_fee")
  235. $this->signup_slot_id = null;
  236. if($this->type != "entrance_fee")
  237. $this->entrance_id = null;
  238. if($this->_image != null && strlen($this->_image->content)){
  239. $this->_image->save();
  240. $this->persistedProperties["image_id"] = $this->_image->id;
  241. }
  242. if(parent::save($overrideValidation)){
  243. static::getRepository()->commit();
  244. return true;
  245. }else{
  246. static::getRepository()->rollback();
  247. return false;
  248. }
  249. }
  250. }
  251. define("ARTICLE_TYPE_CATEGORY","category");
  252. define("ARTICLE_TYPE_ARTICLE","article");
  253. define("ARTICLE_TYPE_COMPOSITE_CATEGORY","category");
  254. define("ARTICLE_TYPE_ENTRANCE_FEE","entrance_fee");
  255. define("ARTICLE_TYPE_SLOT_FEE","slot_fee");
  256. define("ARTICLE_TYPE_PACKAGE","package");
  257. ?>