xref: /plugin/bez/mdl/Entity.php (revision ffe3109b3ffb461f7393087200b87a2995522fbf)
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
93    public function purge() {
94	    if (property_exists($this, 'content') && property_exists($this, 'content_html')) {
95            $rule = $this->validator->get_rule('content');
96
97            $html = p_render('xhtml',p_get_instructions($this->content), $ignore);
98
99            //probably content contains only white spaces
100            if (empty($html) && $rule[1] == 'NOT NULL') {
101                $html = '<p></p>';
102            }
103            $this->content_html = $html;
104        }
105
106    }
107
108    public function changable_fields($filter=NULL) {
109       $fields = $this->acl->get_list();
110
111       if ($filter !== NULL) {
112           $fields = array_filter($fields, function ($k) use ($filter) {
113                return in_array($k, $filter);
114           }, ARRAY_FILTER_USE_KEY);
115       }
116
117       return array_keys(array_filter($fields, function ($var) {
118           return $var >= BEZ_PERMISSION_CHANGE;
119       }));
120    }
121
122    public function can_be_null($field) {
123	    $rule = $this->validator->get_rule($field);
124	    $null = $rule[1];
125	    if (strtolower($null) == 'null') {
126	        return true;
127        }
128
129        return false;
130    }
131
132    public function __construct($model) {
133        $this->model = $model;
134        $this->validator = new Validator($this->model);
135
136        $this->acl = new Acl($this->model->get_level(), $this->get_acl_columns());
137    }
138
139    public function acl_of($field) {
140        return $this->acl->acl_of($field);
141    }
142
143    protected function html_link_url() {
144	    return '#';
145    }
146
147    protected function html_link_content() {
148	    echo $this->id;
149    }
150
151    public function html_link($pre='', $post='', $print=true) {
152        $ret = '<a href="'.$this->html_link_url().'">';
153        $ret .= $pre . $this->html_link_content() . $post;
154        $ret .= '</a>';
155
156        if ($print) {
157            echo $ret;
158        }
159        return $ret;
160	}
161
162}
163