1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4/* 5 * All fields are stored in object as strings. 6 * NULLs are converted to empty string. 7 * If any attribute in object === NULL -> it means that it was not initialized 8 * But we always inserts NULLs instead of empty strings. 9 * https://stackoverflow.com/questions/1267999/mysql-better-to-insert-null-or-empty-string 10 **/ 11 12use dokuwiki\plugin\bez\meta\PermissionDeniedException; 13use dokuwiki\plugin\bez\meta\ValidationException; 14 15abstract class Entity { 16 17 /** @var Model */ 18 protected $model; 19 20 /** @var Validator */ 21 protected $validator; 22 23 /** @var Acl */ 24 protected $acl; 25 26 abstract public static function get_columns(); 27 28 public static function get_select_columns() { 29 $class = get_called_class(); 30 return $class::get_columns(); 31 } 32 33 public static function get_acl_columns() { 34 $class = get_called_class(); 35 return $class::get_select_columns(); 36 } 37 38 public function get_assoc($filter=NULL) { 39 $assoc = array(); 40 41 $columns = $this->get_select_columns(); 42 if ($filter !== NULL) { 43 $columns = array_intersect($columns, $filter); 44 } 45 46 foreach ($columns as $col) { 47 $assoc[$col] = $this->$col; 48 } 49 return $assoc; 50 } 51 52 public function get_table_name() { 53 $class = (new \ReflectionClass($this))->getShortName(); 54 return lcfirst($class); 55 } 56 57 public function __get($property) { 58 if (!property_exists($this, $property) || !in_array($property, $this->get_columns())) { 59 throw new \Exception('there is no column: "'.$property. '"" in table: "' . $this->get_table_name() . '"'); 60 } 61 62 if ($this->acl_of($property) < BEZ_PERMISSION_VIEW) { 63 throw new PermissionDeniedException(); 64 } 65 66 return $this->$property; 67 68 } 69 70 protected function set_property($property, $value) { 71 if ($this->acl_of($property) < BEZ_PERMISSION_CHANGE) { 72 throw new PermissionDeniedException("cannot change field $property"); 73 } 74 $this->$property = $value; 75 } 76 77 protected function set_property_array($array) { 78 foreach ($array as $k => $v) { 79 $this->set_property($k, $v); 80 } 81 } 82 83 public function set_data($post) { 84 $val_data = $this->validator->validate($post); 85 if ($val_data === false) { 86 throw new ValidationException($this->get_table_name(), $this->validator->get_errors()); 87 } 88 89 $this->set_property_array($val_data); 90 } 91 92 public function changable_fields($filter=NULL) { 93 $fields = $this->acl->get_list(); 94 95 if ($filter !== NULL) { 96 $fields = array_filter($fields, function ($k) use ($filter) { 97 return in_array($k, $filter); 98 }, ARRAY_FILTER_USE_KEY); 99 } 100 101 return array_keys(array_filter($fields, function ($var) { 102 return $var >= BEZ_PERMISSION_CHANGE; 103 })); 104 } 105 106 public function can_be_null($field) { 107 $rule = $this->validator->get_rule($field); 108 $null = $rule[1]; 109 if (strtolower($null) == 'null') { 110 return true; 111 } 112 113 return false; 114 } 115 116 public function __construct($model) { 117 $this->model = $model; 118 $this->validator = new Validator($this->model); 119 120 $this->acl = new Acl($this->model->get_level(), $this->get_acl_columns()); 121 } 122 123 public function acl_of($field) { 124 return $this->acl->acl_of($field); 125 } 126 127} 128