1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4 5use dokuwiki\plugin\bez\meta\PermissionDeniedException; 6 7define('BEZ_TABLE_PERMISSION_UNKNOWN', -1); 8define('BEZ_TABLE_PERMISSION_SELECT', 0); 9define('BEZ_TABLE_PERMISSION_INSERT', 1); 10 11abstract class Factory { 12 /** @var Model */ 13 protected $model; 14 15 public function permission() { 16 return BEZ_TABLE_PERMISSION_INSERT; 17 } 18 19 protected function filter_field_map($field) { 20 $table = $this->get_table_view(); 21 if (!$table) { 22 $table = $this->get_table_name(); 23 } 24 25 return "$table.$field"; 26 } 27 28 protected function select_query() { 29 $table = $this->get_table_view(); 30 if (!$table) { 31 $table = $this->get_table_name(); 32 } 33 return "SELECT * FROM $table"; 34 } 35 36 public function get_table_view() { 37 return false; 38 } 39 40 protected function build_where($filters=array()) { 41 $execute = array(); 42 $where_q = array(); 43 foreach ($filters as $filter => $value) { 44 $field = $this->filter_field_map($filter); 45 46 //parser 47 $operator = '='; 48 $function = ''; 49 $function_args = array(); 50 if (is_array($value)) { 51 $operators = array('!=', '<', '>', '<=', '>=', 'LIKE', 'BETWEEN', 'OR'); 52 $functions = array('', 'date'); 53 54 $operator = $value[0]; 55 $function = isset($value[2]) ? $value[2] : ''; 56 57 if (is_array($function)) { 58 $function = $function[0]; 59 $function_args = array_slice($value[2], 1); 60 } 61 62 $value = $value[1]; 63 64 if (!in_array($operator, $operators)) { 65 throw new \Exception('unknown operator: '.$operator); 66 } 67 68 if (!in_array($function, $functions)) { 69 throw new \Exception('unknown function: '.$function); 70 } 71 } 72 73 //builder 74 if ($operator === 'BETWEEN') { 75 if (count($value) < 2) { 76 throw new \Exception('wrong BETWEEN argument. provide two values'); 77 } 78 if ($function !== '') { 79 array_unshift($function_args, $field); 80 $where_q[] = "$function(".implode(',', $function_args).") BETWEEN :${filter}_start AND :${filter}_end"; 81 } else { 82 $where_q[] = "$field BETWEEN :${filter}_start AND :${filter}_end"; 83 } 84 $execute[":${filter}_start"] = $value[0]; 85 $execute[":${filter}_end"] = $value[1]; 86 } elseif ($operator === 'OR') { 87 if (!is_array($value)) { 88 throw new \Exception('$data should be an array'); 89 } 90 91 $where_array = array(); 92 93 foreach ($value as $k => $v) { 94 $exec = ":${filter}_$k"; 95 $where_array[] = "$field = $exec"; 96 $execute[$exec] = $v; 97 } 98 $where_q[] = '('.implode(' OR ', $where_array).')'; 99 100 101 } else { 102 if ($function !== '') { 103 array_unshift($function_args, $field); 104 $where_q[] = "$function(".implode(',', $function_args).") $operator :$filter"; 105 $execute[":$filter"] = $value; 106 } elseif (empty($value)) { 107 $where_q[] = "($field IS NULL OR $field = '')"; 108 } else { 109 $where_q[] = "$field $operator :$filter"; 110 $execute[":$filter"] = $value; 111 } 112 113 } 114 } 115 116 $where = ''; 117 if (count($where_q) > 0) { 118 $where = ' WHERE '.implode(' AND ', $where_q); 119 } 120 return array($where, $execute); 121 } 122 123 public function __construct(Model $model) { 124 $this->model = $model; 125 } 126 127 public function get_all($filters=array(), $orderby='', $defaults=array(), $limit=false) { 128 129 list($where_q, $execute) = $this->build_where($filters); 130 131 $q = $this->select_query() . $where_q; 132 133 if ($orderby != '') { 134 if (is_array($orderby)) $orderby = implode(', ', $orderby); 135 $q .= " ORDER BY $orderby"; 136 } 137 138 if (is_int($limit)) { 139 $q .= " LIMIT $limit"; 140 } 141 142 $sth = $this->model->db->prepare($q); 143 144 $sth->setFetchMode(\PDO::FETCH_CLASS, $this->get_object_class_name(), 145 array($this->model, $defaults)); 146 147 $sth->execute($execute); 148 149 return $sth; 150 } 151 152 public function count($filters=array()) { 153 $table = $this->get_table_view(); 154 if (!$table) { 155 $table = $this->get_table_name(); 156 } 157 158 list($where_q, $execute) = $this->build_where($filters); 159 160 $q = "SELECT COUNT(*) FROM $table " . $where_q; 161 $sth = $this->model->db->prepare($q); 162 $sth->execute($execute); 163 164 $count = $sth->fetchColumn(); 165 return $count; 166 } 167 168 public function get_one($id, $defaults=array()) { 169 $table = $this->get_table_view(); 170 if (!$table) { 171 $table = $this->get_table_name(); 172 } 173 174 $q = $this->select_query()." WHERE $table.id = ?"; 175 176 $sth = $this->model->db->prepare($q); 177 $sth->execute(array($id)); 178 179 $obj = $sth->fetchObject($this->get_object_class_name(), 180 array($this->model, $defaults)); 181 182 if ($obj === false) { 183 throw new \Exception('there is no '.$this->get_table_name().' with id: '.$id); 184 } 185 186 return $obj; 187 } 188 189 public function get_table_name() { 190 $class = (new \ReflectionClass($this))->getShortName(); 191 return lcfirst(str_replace('Factory', '', $class)); 192 } 193 194 public function get_object_class_name() { 195 $class = (new \ReflectionClass($this))->getName(); 196 return str_replace('Factory', '', $class); 197 } 198 199 public function create_object($defaults=array()) { 200 $object_name = $this->get_object_class_name(); 201 202 $obj = new $object_name($this->model, $defaults); 203 return $obj; 204 } 205 206 protected function beginTransaction() { 207 $this->model->sqlite->query('BEGIN TRANSACTION'); 208 } 209 210 protected function commitTransaction() { 211 $this->model->sqlite->query('COMMIT TRANSACTION'); 212 } 213 214 protected function rollbackTransaction() { 215 $this->model->sqlite->query('ROLLBACK'); 216 } 217 218 protected function insert(Entity $obj) { 219 if ($obj->id != NULL) { 220 throw new \Exception('row already saved'); 221 } 222 223 $set = array(); 224 $execute = array(); 225 $columns = array(); 226 foreach ($obj->get_columns() as $column) { 227 if ($obj->$column === null) continue; 228 $set[] = ":$column"; 229 $columns[] = $column; 230 $value = $obj->$column; 231 if ($value === '') { 232 $execute[':'.$column] = null; 233 } else { 234 $execute[':'.$column] = $value; 235 } 236 } 237 238 $query = 'INSERT INTO '.$this->get_table_name().' 239 ('.implode(',', $columns).') 240 VALUES ('.implode(',', $set).')'; 241 242 $sth = $this->model->db->prepare($query); 243 $sth->execute($execute); 244 245 $reflectionClass = new \ReflectionClass($obj); 246 $reflectionProperty = $reflectionClass->getProperty('id'); 247 $reflectionProperty->setAccessible(true); 248 $reflectionProperty->setValue($obj, $this->model->db->lastInsertId()); 249 250 } 251 252 protected function update(Entity $obj) { 253 if ($obj->id == NULL) { 254 throw new \Exception('row not inserted'); 255 } 256 257 $set = array(); 258 $execute = array(':id' => $obj->id); 259 foreach ($obj->get_columns() as $column) { 260 if ($column == 'id') continue; 261 $set[] = "$column=:$column"; 262 $value = $obj->$column; 263 if ($value === '') { 264 $execute[':'.$column] = null; 265 } else { 266 $execute[':'.$column] = $value; 267 } 268 } 269 270 $query = 'UPDATE ' . $this->get_table_name() . 271 ' SET ' . implode(',', $set) . 272 ' WHERE id=:id'; 273 274 $sth = $this->model->db->prepare($query); 275 $sth->execute($execute); 276 } 277 278 public function initial_save(Entity $obj, $data) { 279 $obj->set_data($data); 280 $this->insert($obj); 281 } 282 283 public function update_save(Entity $obj, $data) { 284 $obj->set_data($data); 285 $this->update($obj); 286 } 287 288 public function save(Entity $obj) { 289 if ($obj->id == NULL) { 290 $this->insert($obj); 291 } else { 292 $this->update($obj); 293 } 294 } 295 296 protected function delete_from_db($id) { 297 $q = 'DELETE FROM '.$this->get_table_name().' WHERE id = ?'; 298 $sth = $this->model->db->prepare($q); 299 $sth->execute(array($id)); 300 } 301 302 public function delete(Entity $obj) { 303 if ($obj->acl_of('id') < BEZ_PERMISSION_DELETE) { 304 throw new PermissionDeniedException('cannot delete'); 305 } 306 $this->delete_from_db($obj->id); 307 } 308} 309