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='', $desc=true, $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 $fields = call_user_func(array($this->get_object_class_name(), 'get_columns')); 135 if (!in_array($orderby, $fields)) { 136 throw new \Exception('unknown field '.$orderby); 137 } 138 $q .= " ORDER BY $orderby"; 139 if ($desc) { 140 $q .= " DESC"; 141 } 142 } 143 144 if (is_int($limit)) { 145 $q .= " LIMIT $limit"; 146 } 147 148 $sth = $this->model->db->prepare($q); 149 150 $sth->setFetchMode(\PDO::FETCH_CLASS, $this->get_object_class_name(), 151 array($this->model, $defaults)); 152 153 $sth->execute($execute); 154 155 return $sth; 156 } 157 158 public function count($filters=array()) { 159 $table = $this->get_table_view(); 160 if (!$table) { 161 $table = $this->get_table_name(); 162 } 163 164 list($where_q, $execute) = $this->build_where($filters); 165 166 $q = "SELECT COUNT(*) FROM $table " . $where_q; 167 $sth = $this->model->db->prepare($q); 168 $sth->execute($execute); 169 170 $count = $sth->fetchColumn(); 171 return $count; 172 } 173 174 public function get_one($id, $defaults=array()) { 175 $table = $this->get_table_view(); 176 if (!$table) { 177 $table = $this->get_table_name(); 178 } 179 180 $q = $this->select_query()." WHERE $table.id = ?"; 181 182 $sth = $this->model->db->prepare($q); 183 $sth->execute(array($id)); 184 185 $obj = $sth->fetchObject($this->get_object_class_name(), 186 array($this->model, $defaults)); 187 188 if ($obj === false) { 189 throw new \Exception('there is no '.$this->get_table_name().' with id: '.$id); 190 } 191 192 return $obj; 193 } 194 195 public function get_table_name() { 196 $class = (new \ReflectionClass($this))->getShortName(); 197 return lcfirst(str_replace('Factory', '', $class)); 198 } 199 200 public function get_object_class_name() { 201 $class = (new \ReflectionClass($this))->getName(); 202 return str_replace('Factory', '', $class); 203 } 204 205 public function create_object($defaults=array()) { 206 $object_name = $this->get_object_class_name(); 207 208 $obj = new $object_name($this->model, $defaults); 209 return $obj; 210 } 211 212 protected function beginTransaction() { 213 $this->model->sqlite->query('BEGIN TRANSACTION'); 214 } 215 216 protected function commitTransaction() { 217 $this->model->sqlite->query('COMMIT TRANSACTION'); 218 } 219 220 protected function rollbackTransaction() { 221 $this->model->sqlite->query('ROLLBACK'); 222 } 223 224 protected function insert(Entity $obj) { 225 if ($obj->id != NULL) { 226 throw new \Exception('row already saved'); 227 } 228 229 $set = array(); 230 $execute = array(); 231 $columns = array(); 232 foreach ($obj->get_columns() as $column) { 233 if ($obj->$column === null) continue; 234 $set[] = ":$column"; 235 $columns[] = $column; 236 $value = $obj->$column; 237 if ($value === '') { 238 $execute[':'.$column] = null; 239 } else { 240 $execute[':'.$column] = $value; 241 } 242 } 243 244 $query = 'INSERT INTO '.$this->get_table_name().' 245 ('.implode(',', $columns).') 246 VALUES ('.implode(',', $set).')'; 247 248 $sth = $this->model->db->prepare($query); 249 $sth->execute($execute); 250 251 $reflectionClass = new \ReflectionClass($obj); 252 $reflectionProperty = $reflectionClass->getProperty('id'); 253 $reflectionProperty->setAccessible(true); 254 $reflectionProperty->setValue($obj, $this->model->db->lastInsertId()); 255 256 } 257 258 protected function update(Entity $obj) { 259 if ($obj->id == NULL) { 260 throw new \Exception('row not inserted'); 261 } 262 263 $set = array(); 264 $execute = array(':id' => $obj->id); 265 foreach ($obj->get_columns() as $column) { 266 if ($column == 'id') continue; 267 $set[] = "$column=:$column"; 268 $value = $obj->$column; 269 if ($value === '') { 270 $execute[':'.$column] = null; 271 } else { 272 $execute[':'.$column] = $value; 273 } 274 } 275 276 $query = 'UPDATE ' . $this->get_table_name() . 277 ' SET ' . implode(',', $set) . 278 ' WHERE id=:id'; 279 280 $sth = $this->model->db->prepare($query); 281 $sth->execute($execute); 282 } 283 284 public function initial_save(Entity $obj, $data) { 285 $obj->set_data($data); 286 $this->insert($obj); 287 } 288 289 public function update_save(Entity $obj, $data) { 290 $obj->set_data($data); 291 $this->update($obj); 292 } 293 294 public function save(Entity $obj) { 295 if ($obj->id == NULL) { 296 $this->insert($obj); 297 } else { 298 $this->update($obj); 299 } 300 } 301 302 protected function delete_from_db($id) { 303 $q = 'DELETE FROM '.$this->get_table_name().' WHERE id = ?'; 304 $sth = $this->model->db->prepare($q); 305 $sth->execute(array($id)); 306 } 307 308 public function delete(Entity $obj) { 309 if ($obj->acl_of('id') < BEZ_PERMISSION_DELETE) { 310 throw new PermissionDeniedException('cannot delete'); 311 } 312 $this->delete_from_db($obj->id); 313 } 314} 315