xref: /plugin/bez/mdl/Entity.php (revision e09b232ffd94bc38ec04930c9ddc745874b0ba41)
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 function get_assoc($filter=NULL) {
34		$assoc = array();
35
36        $columns = $this->get_select_columns();
37        if ($filter !== NULL) {
38            $columns = array_intersect($columns, $filter);
39        }
40
41		foreach ($columns as $col) {
42			$assoc[$col] = $this->$col;
43		}
44		return $assoc;
45	}
46
47    public function get_table_name() {
48        $class = (new \ReflectionClass($this))->getShortName();
49		return lcfirst($class);
50	}
51
52	public function __get($property) {
53        if (!property_exists($this, $property) || !in_array($property, $this->get_columns())) {
54            throw new \Exception('there is no column: "'.$property. '"" in table: "' . $this->get_table_name() . '"');
55        }
56
57        if ($this->acl_of($property) < BEZ_PERMISSION_VIEW) {
58            throw new PermissionDeniedException();
59        }
60
61        return $this->$property;
62
63	}
64
65    protected function set_property($property, $value) {
66        if ($this->acl_of($property) < BEZ_PERMISSION_CHANGE) {
67            throw new PermissionDeniedException("cannot change field $property");
68        }
69        $this->$property = $value;
70    }
71
72    protected function set_property_array($array) {
73        foreach ($array as $k => $v) {
74            $this->set_property($k, $v);
75        }
76    }
77
78    public function set_data($post) {
79        $val_data = $this->validator->validate($post);
80		if ($val_data === false) {
81			throw new ValidationException($this->get_table_name(), $this->validator->get_errors());
82		}
83
84		$this->set_property_array($val_data);
85    }
86
87    public function changable_fields($filter=NULL) {
88       $fields = $this->acl->get_list();
89
90       if ($filter !== NULL) {
91           $fields = array_filter($fields, function ($k) use ($filter) {
92                return in_array($k, $filter);
93           }, ARRAY_FILTER_USE_KEY);
94       }
95
96       return array_keys(array_filter($fields, function ($var) {
97           return $var >= BEZ_PERMISSION_CHANGE;
98       }));
99    }
100
101    public function can_be_null($field) {
102	    $rule = $this->validator->get_rule($field);
103	    $null = $rule[1];
104	    if (strtolower($null) == 'null') {
105	        return true;
106        }
107
108        return false;
109    }
110
111    public function __construct($model) {
112        $this->model = $model;
113        $this->validator = new Validator($this->model);
114
115        $this->acl = new Acl($this->model->get_level(), $this->get_select_columns());
116    }
117
118    public function acl_of($field) {
119        return $this->acl->acl_of($field);
120    }
121
122}
123