@@ -118,41 +118,41 @@ discard block |
||
| 118 | 118 | * @param boolean $allow_multiple_values =false should we allow AND store multiple values (1:N relations) |
| 119 | 119 | * @param string $timestamp_type =null default null=leave them as is, 'ts'|'integer' use integer unix timestamps, 'object' use DateTime objects |
| 120 | 120 | */ |
| 121 | - function __construct($app,$table,$extra_table,$column_prefix='', |
|
| 122 | - $extra_key='_name',$extra_value='_value',$extra_id='_id', |
|
| 123 | - Db $db=null,$no_clone=true,$allow_multiple_values=false,$timestamp_type=null) |
|
| 121 | + function __construct($app, $table, $extra_table, $column_prefix = '', |
|
| 122 | + $extra_key = '_name', $extra_value = '_value', $extra_id = '_id', |
|
| 123 | + Db $db = null, $no_clone = true, $allow_multiple_values = false, $timestamp_type = null) |
|
| 124 | 124 | { |
| 125 | 125 | // calling the Storage\Base constructor |
| 126 | - parent::__construct($app,$table,$db,$column_prefix,$no_clone,$timestamp_type); |
|
| 126 | + parent::__construct($app, $table, $db, $column_prefix, $no_clone, $timestamp_type); |
|
| 127 | 127 | |
| 128 | 128 | $this->allow_multiple_values = $allow_multiple_values; |
| 129 | 129 | $this->extra_table = $extra_table; |
| 130 | - if (!$this->extra_id) $this->extra_id = $this->autoinc_id; // default to auto id of regular table |
|
| 130 | + if (!$this->extra_id) $this->extra_id = $this->autoinc_id; // default to auto id of regular table |
|
| 131 | 131 | |
| 132 | 132 | // if names from columns of extra table are only postfixes (starting with _), prepend column prefix |
| 133 | - if (!($prefix=$column_prefix)) |
|
| 133 | + if (!($prefix = $column_prefix)) |
|
| 134 | 134 | { |
| 135 | - list($prefix) = explode('_',$this->autoinc_id); |
|
| 135 | + list($prefix) = explode('_', $this->autoinc_id); |
|
| 136 | 136 | } |
| 137 | - elseif(substr($prefix,-1) == '_') |
|
| 137 | + elseif (substr($prefix, -1) == '_') |
|
| 138 | 138 | { |
| 139 | - $prefix = substr($prefix,0,-1); // remove trailing underscore from column prefix parameter |
|
| 139 | + $prefix = substr($prefix, 0, -1); // remove trailing underscore from column prefix parameter |
|
| 140 | 140 | } |
| 141 | - foreach(array( |
|
| 141 | + foreach (array( |
|
| 142 | 142 | 'extra_id' => $extra_id, |
| 143 | 143 | 'extra_key' => $extra_key, |
| 144 | 144 | 'extra_value' => $extra_value |
| 145 | 145 | ) as $col => $val) |
| 146 | 146 | { |
| 147 | 147 | $this->$col = $col_name = $val; |
| 148 | - if ($col_name[0] == '_') $this->$col = $prefix . $val; |
|
| 148 | + if ($col_name[0] == '_') $this->$col = $prefix.$val; |
|
| 149 | 149 | } |
| 150 | 150 | // some sanity checks, maybe they should be active only for development |
| 151 | - if (!($extra_defs = $this->db->get_table_definitions($app,$extra_table))) |
|
| 151 | + if (!($extra_defs = $this->db->get_table_definitions($app, $extra_table))) |
|
| 152 | 152 | { |
| 153 | 153 | throw new Exception\WrongParameter("extra table $extra_table is NOT defined!"); |
| 154 | 154 | } |
| 155 | - foreach(array('extra_id','extra_key','extra_value') as $col) |
|
| 155 | + foreach (array('extra_id', 'extra_key', 'extra_value') as $col) |
|
| 156 | 156 | { |
| 157 | 157 | if (!$this->$col || !isset($extra_defs['fd'][$this->$col])) |
| 158 | 158 | { |
@@ -177,23 +177,23 @@ discard block |
||
| 177 | 177 | * @param array $field_names =null custom fields to read, default all |
| 178 | 178 | * @return array id => $this->cf_field(name) => value |
| 179 | 179 | */ |
| 180 | - function read_customfields($ids,$field_names=null) |
|
| 180 | + function read_customfields($ids, $field_names = null) |
|
| 181 | 181 | { |
| 182 | 182 | if (is_null($field_names)) $field_names = array_keys($this->customfields); |
| 183 | 183 | |
| 184 | - foreach((array)$ids as $key => $id) |
|
| 184 | + foreach ((array)$ids as $key => $id) |
|
| 185 | 185 | { |
| 186 | 186 | if (!(int)$id && is_array($ids)) unset($ids[$key]); |
| 187 | 187 | } |
| 188 | - if (!$ids || !$field_names) return array(); // nothing to do |
|
| 188 | + if (!$ids || !$field_names) return array(); // nothing to do |
|
| 189 | 189 | |
| 190 | 190 | $entries = array(); |
| 191 | - foreach($this->db->select($this->extra_table,'*',array( |
|
| 191 | + foreach ($this->db->select($this->extra_table, '*', array( |
|
| 192 | 192 | $this->extra_id => $ids, |
| 193 | 193 | $this->extra_key => $field_names, |
| 194 | - ),__LINE__,__FILE__,false,'',$this->app) as $row) |
|
| 194 | + ), __LINE__, __FILE__, false, '', $this->app) as $row) |
|
| 195 | 195 | { |
| 196 | - $entry =& $entries[$row[$this->extra_id]]; |
|
| 196 | + $entry = & $entries[$row[$this->extra_id]]; |
|
| 197 | 197 | if (!is_array($entry)) $entry = array(); |
| 198 | 198 | $field = $this->get_cf_field($row[$this->extra_key]); |
| 199 | 199 | |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | * @param array $extra_cols =array() extra-data to be saved |
| 217 | 217 | * @return bool false on success, errornumber on failure |
| 218 | 218 | */ |
| 219 | - function save_customfields(&$data, array $extra_cols=array()) |
|
| 219 | + function save_customfields(&$data, array $extra_cols = array()) |
|
| 220 | 220 | { |
| 221 | 221 | $id = isset($data[$this->autoinc_id]) ? $data[$this->autoinc_id] : $data[$this->db_key_cols[$this->autoinc_id]]; |
| 222 | 222 | |
@@ -233,22 +233,21 @@ discard block |
||
| 233 | 233 | $is_multiple = $this->is_multiple($name); |
| 234 | 234 | |
| 235 | 235 | // we explicitly need to delete fields, if value is empty or field allows multiple values or we have no unique index |
| 236 | - if(empty($data[$field]) || $is_multiple || !$this->extra_has_unique_index) |
|
| 236 | + if (empty($data[$field]) || $is_multiple || !$this->extra_has_unique_index) |
|
| 237 | 237 | { |
| 238 | - $this->db->delete($this->extra_table,$where,__LINE__,__FILE__,$this->app); |
|
| 239 | - if (empty($data[$field])) continue; // nothing else to do for empty values |
|
| 238 | + $this->db->delete($this->extra_table, $where, __LINE__, __FILE__, $this->app); |
|
| 239 | + if (empty($data[$field])) continue; // nothing else to do for empty values |
|
| 240 | 240 | } |
| 241 | - foreach($is_multiple && !is_array($data[$field]) ? explode(',',$data[$field]) : |
|
| 242 | - // regular custom fields (!$is_multiple) eg. addressbook store multiple values comma-separated |
|
| 241 | + foreach ($is_multiple && !is_array($data[$field]) ? explode(',', $data[$field]) : // regular custom fields (!$is_multiple) eg. addressbook store multiple values comma-separated |
|
| 243 | 242 | (array)(!$is_multiple && is_array($data[$field]) ? implode(',', $data[$field]) : $data[$field]) as $value) |
| 244 | 243 | { |
| 245 | - if (!$this->db->insert($this->extra_table,array($this->extra_value => $value)+$extra_cols,$where,__LINE__,__FILE__,$this->app)) |
|
| 244 | + if (!$this->db->insert($this->extra_table, array($this->extra_value => $value) + $extra_cols, $where, __LINE__, __FILE__, $this->app)) |
|
| 246 | 245 | { |
| 247 | 246 | return $this->db->Errno; |
| 248 | 247 | } |
| 249 | 248 | } |
| 250 | 249 | } |
| 251 | - return false; // no error |
|
| 250 | + return false; // no error |
|
| 252 | 251 | } |
| 253 | 252 | |
| 254 | 253 | /** |
@@ -264,7 +263,7 @@ discard block |
||
| 264 | 263 | |
| 265 | 264 | if ($this->customfields) |
| 266 | 265 | { |
| 267 | - foreach(array_keys($this->customfields) as $name) |
|
| 266 | + foreach (array_keys($this->customfields) as $name) |
|
| 268 | 267 | { |
| 269 | 268 | if (isset($new[$field = $this->get_cf_field($name)])) |
| 270 | 269 | { |
@@ -284,16 +283,16 @@ discard block |
||
| 284 | 283 | * @param string $join sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or |
| 285 | 284 | * @return array|boolean data if row could be retrived else False |
| 286 | 285 | */ |
| 287 | - function read($keys,$extra_cols='',$join='') |
|
| 286 | + function read($keys, $extra_cols = '', $join = '') |
|
| 288 | 287 | { |
| 289 | - if (!parent::read($keys,$extra_cols,$join)) |
|
| 288 | + if (!parent::read($keys, $extra_cols, $join)) |
|
| 290 | 289 | { |
| 291 | 290 | return false; |
| 292 | 291 | } |
| 293 | 292 | if (($id = (int)$this->data[$this->db_key_cols[$this->autoinc_id]]) && $this->customfields && |
| 294 | 293 | ($cfs = $this->read_customfields($id))) |
| 295 | 294 | { |
| 296 | - $this->data = array_merge($this->data,$cfs[$id]); |
|
| 295 | + $this->data = array_merge($this->data, $cfs[$id]); |
|
| 297 | 296 | } |
| 298 | 297 | return $this->data; |
| 299 | 298 | } |
@@ -307,14 +306,14 @@ discard block |
||
| 307 | 306 | * @param string|array $extra_where =null extra where clause, eg. to check an etag, returns true if no affected rows! |
| 308 | 307 | * @return int|boolean 0 on success, or errno != 0 on error, or true if $extra_where is given and no rows affected |
| 309 | 308 | */ |
| 310 | - function save($keys=null,$extra_where=null) |
|
| 309 | + function save($keys = null, $extra_where = null) |
|
| 311 | 310 | { |
| 312 | 311 | if (is_array($keys) && count($keys) && !isset($keys[0])) // allow to use an etag, eg array('etag=etag+1') |
| 313 | 312 | { |
| 314 | 313 | $this->data_merge($keys); |
| 315 | 314 | $keys = null; |
| 316 | 315 | } |
| 317 | - $ret = parent::save($keys,$extra_where); |
|
| 316 | + $ret = parent::save($keys, $extra_where); |
|
| 318 | 317 | |
| 319 | 318 | if ($ret == 0 && $this->customfields) |
| 320 | 319 | { |
@@ -332,26 +331,26 @@ discard block |
||
| 332 | 331 | * @param boolean $only_return_ids =false return $ids of delete call to db object, but not run it (can be used by extending classes!) |
| 333 | 332 | * @return int|array affected rows, should be 1 if ok, 0 if an error or array with id's if $only_return_ids |
| 334 | 333 | */ |
| 335 | - function delete($keys=null,$only_return_ids=false) |
|
| 334 | + function delete($keys = null, $only_return_ids = false) |
|
| 336 | 335 | { |
| 337 | 336 | if ($this->customfields || $only_return_ids) |
| 338 | 337 | { |
| 339 | - $query = parent::delete($keys,true); |
|
| 338 | + $query = parent::delete($keys, true); |
|
| 340 | 339 | // check if query contains more then the id's |
| 341 | 340 | if (!isset($query[$this->autoinc_id]) || count($query) != 1) |
| 342 | 341 | { |
| 343 | - foreach($this->db->select($this->table_name,$this->autoinc_id,$query,__LINE__,__FILE__,false,'',$this->app) as $row) |
|
| 342 | + foreach ($this->db->select($this->table_name, $this->autoinc_id, $query, __LINE__, __FILE__, false, '', $this->app) as $row) |
|
| 344 | 343 | { |
| 345 | 344 | $ids[] = $row[$this->autoinc_id]; |
| 346 | 345 | } |
| 347 | - if (!$ids) return 0; // no rows affected |
|
| 346 | + if (!$ids) return 0; // no rows affected |
|
| 348 | 347 | } |
| 349 | 348 | else |
| 350 | 349 | { |
| 351 | 350 | $ids = (array)$query[$this->autoinc_id]; |
| 352 | 351 | } |
| 353 | 352 | if ($only_return_ids) return $ids; |
| 354 | - $this->db->delete($this->extra_table,array($this->extra_id => $ids),__LINE__,__FILE__); |
|
| 353 | + $this->db->delete($this->extra_table, array($this->extra_id => $ids), __LINE__, __FILE__); |
|
| 355 | 354 | } |
| 356 | 355 | return parent::delete($keys); |
| 357 | 356 | } |
@@ -374,32 +373,32 @@ discard block |
||
| 374 | 373 | * @param string|array $extra_cols =array() |
| 375 | 374 | * @return int total number of rows |
| 376 | 375 | */ |
| 377 | - function get_rows($query,&$rows,&$readonlys,$join='',$need_full_no_count=false,$only_keys=false,$extra_cols=array()) |
|
| 376 | + function get_rows($query, &$rows, &$readonlys, $join = '', $need_full_no_count = false, $only_keys = false, $extra_cols = array()) |
|
| 378 | 377 | { |
| 379 | - parent::get_rows($query,$rows,$readonlys,$join,$need_full_no_count,$only_keys,$extra_cols); |
|
| 378 | + parent::get_rows($query, $rows, $readonlys, $join, $need_full_no_count, $only_keys, $extra_cols); |
|
| 380 | 379 | |
| 381 | - $selectcols = $query['selectcols'] ? (is_string($query['selectcols']) ? explode(',',$query['selectcols']) :$query['selectcols']): array(); |
|
| 380 | + $selectcols = $query['selectcols'] ? (is_string($query['selectcols']) ? explode(',', $query['selectcols']) : $query['selectcols']) : array(); |
|
| 382 | 381 | |
| 383 | - if ($rows && $this->customfields && (!$selectcols || in_array('customfields',$selectcols))) |
|
| 382 | + if ($rows && $this->customfields && (!$selectcols || in_array('customfields', $selectcols))) |
|
| 384 | 383 | { |
| 385 | 384 | $id2keys = array(); |
| 386 | - foreach($rows as $key => $row) |
|
| 385 | + foreach ($rows as $key => $row) |
|
| 387 | 386 | { |
| 388 | 387 | $id2keys[$row[$this->db_key_cols[$this->autoinc_id]]] = $key; |
| 389 | 388 | } |
| 390 | 389 | // check if only certain cf's to show |
| 391 | 390 | if (!in_array('customfields', $selectcols)) |
| 392 | 391 | { |
| 393 | - foreach($selectcols as $col) |
|
| 392 | + foreach ($selectcols as $col) |
|
| 394 | 393 | { |
| 395 | 394 | if ($this->is_cf($col)) $fields[] = $this->get_cf_name($col); |
| 396 | 395 | } |
| 397 | 396 | } |
| 398 | - if (($cfs = $this->read_customfields(array_keys($id2keys),$fields))) |
|
| 397 | + if (($cfs = $this->read_customfields(array_keys($id2keys), $fields))) |
|
| 399 | 398 | { |
| 400 | - foreach($cfs as $id => $data) |
|
| 399 | + foreach ($cfs as $id => $data) |
|
| 401 | 400 | { |
| 402 | - $rows[$id2keys[$id]] = array_merge($rows[$id2keys[$id]],$data); |
|
| 401 | + $rows[$id2keys[$id]] = array_merge($rows[$id2keys[$id]], $data); |
|
| 403 | 402 | } |
| 404 | 403 | } |
| 405 | 404 | } |
@@ -416,9 +415,9 @@ discard block |
||
| 416 | 415 | */ |
| 417 | 416 | public function cf_match($_pattern) |
| 418 | 417 | { |
| 419 | - static $private_cfs=null; |
|
| 418 | + static $private_cfs = null; |
|
| 420 | 419 | |
| 421 | - if (!$this->customfields) return ''; // no custom-fields --> no search |
|
| 420 | + if (!$this->customfields) return ''; // no custom-fields --> no search |
|
| 422 | 421 | |
| 423 | 422 | $sql = ' OR '.$this->table_name.'.'.$this->autoinc_id.' IN (SELECT '.$this->autoinc_id. |
| 424 | 423 | ' FROM '.$this->extra_table.' WHERE '.$this->extra_value.' '. |
@@ -429,7 +428,7 @@ discard block |
||
| 429 | 428 | if (!isset($private_cfs)) |
| 430 | 429 | { |
| 431 | 430 | $private_cfs = array_diff_key( |
| 432 | - Storage\Customfields::get($this->app, true, null, $this->db), // true: get private cfs too |
|
| 431 | + Storage\Customfields::get($this->app, true, null, $this->db), // true: get private cfs too |
|
| 433 | 432 | $this->customfields); |
| 434 | 433 | //error_log(__METHOD__."() private_cfs=".array2string($private_cfs)); |
| 435 | 434 | } |
@@ -460,7 +459,7 @@ discard block |
||
| 460 | 459 | * @param boolean $need_full_no_count =false If true an unlimited query is run to determine the total number of rows, default false |
| 461 | 460 | * @return array|NULL array of matching rows (the row is an array of the cols) or NULL |
| 462 | 461 | */ |
| 463 | - function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false) |
|
| 462 | + function &search($criteria, $only_keys = True, $order_by = '', $extra_cols = '', $wildcard = '', $empty = False, $op = 'AND', $start = false, $filter = null, $join = '', $need_full_no_count = false) |
|
| 464 | 463 | { |
| 465 | 464 | //error_log(__METHOD__.'('.array2string(array_combine(array_slice(array('criteria','only_keys','order_by','extra_cols','wildcard','empty','op','start','filter','join','need_full_no_count'), 0, count(func_get_args())), func_get_args())).')'); |
| 466 | 465 | |
@@ -468,7 +467,7 @@ discard block |
||
| 468 | 467 | if (!$this->customfields && strpos($order_by, self::CF_PREFIX) === false && |
| 469 | 468 | strpos(implode(',', array_keys($filter ? $filter : [])), self::CF_PREFIX) === false) |
| 470 | 469 | { |
| 471 | - return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count); |
|
| 470 | + return parent::search($criteria, $only_keys, $order_by, $extra_cols, $wildcard, $empty, $op, $start, $filter, $join, $need_full_no_count); |
|
| 472 | 471 | } |
| 473 | 472 | if ($only_keys === false) |
| 474 | 473 | { |
@@ -477,7 +476,7 @@ discard block |
||
| 477 | 476 | $extra_join_added = $join && strpos($join, $this->extra_join) !== false; |
| 478 | 477 | if ($criteria && is_string($criteria)) |
| 479 | 478 | { |
| 480 | - $extra_join_added = true; // we have NOT added the join, as we use a sub-query and therefore not need it |
|
| 479 | + $extra_join_added = true; // we have NOT added the join, as we use a sub-query and therefore not need it |
|
| 481 | 480 | |
| 482 | 481 | $criteria = $this->search2criteria($criteria, $wildcard, $op); |
| 483 | 482 | } |
@@ -489,10 +488,10 @@ discard block |
||
| 489 | 488 | // we should check if the CF is (still) available, but that makes the slow search even slower :( |
| 490 | 489 | if (($negate = $criteria[$this->extra_value][0] === '!')) |
| 491 | 490 | { |
| 492 | - $criteria[$this->extra_value] = substr($criteria[$this->extra_value],1); |
|
| 491 | + $criteria[$this->extra_value] = substr($criteria[$this->extra_value], 1); |
|
| 493 | 492 | } |
| 494 | - $criteria[] = $this->extra_table.'.'.$this->extra_value . ' ' .($negate ? 'NOT ' : ''). |
|
| 495 | - $this->db->capabilities[Db::CAPABILITY_CASE_INSENSITIV_LIKE]. ' ' . |
|
| 493 | + $criteria[] = $this->extra_table.'.'.$this->extra_value.' '.($negate ? 'NOT ' : ''). |
|
| 494 | + $this->db->capabilities[Db::CAPABILITY_CASE_INSENSITIV_LIKE].' '. |
|
| 496 | 495 | $this->db->quote($wildcard.$criteria[$this->extra_value].$wildcard); |
| 497 | 496 | unset($criteria[$this->extra_value]); |
| 498 | 497 | } |
@@ -501,13 +500,13 @@ discard block |
||
| 501 | 500 | { |
| 502 | 501 | if ($criteria[$this->autoinc_id]) |
| 503 | 502 | { |
| 504 | - $criteria[] = $this->db->expression($this->table_name,$this->table_name.'.', |
|
| 503 | + $criteria[] = $this->db->expression($this->table_name, $this->table_name.'.', |
|
| 505 | 504 | array($this->autoinc_id => $criteria[$this->autoinc_id])); |
| 506 | 505 | } |
| 507 | 506 | unset($criteria[$this->autoinc_id]); |
| 508 | 507 | } |
| 509 | 508 | // replace ambiguous column with (an exact match of) table_name.column |
| 510 | - foreach($criteria as $name => $val) |
|
| 509 | + foreach ($criteria as $name => $val) |
|
| 511 | 510 | { |
| 512 | 511 | // only add extra_join, if we really need it |
| 513 | 512 | if (!$extra_join_added && ( |
@@ -519,9 +518,9 @@ discard block |
||
| 519 | 518 | $extra_join_added = true; |
| 520 | 519 | } |
| 521 | 520 | $extra_columns = $this->db->get_table_definitions($this->app, $this->extra_table); |
| 522 | - if(is_string($name) && $extra_columns['fd'][array_search($name, $this->db_cols)]) |
|
| 521 | + if (is_string($name) && $extra_columns['fd'][array_search($name, $this->db_cols)]) |
|
| 523 | 522 | { |
| 524 | - $criteria[] = $this->db->expression($this->table_name,$this->table_name.'.',array( |
|
| 523 | + $criteria[] = $this->db->expression($this->table_name, $this->table_name.'.', array( |
|
| 525 | 524 | array_search($name, $this->db_cols) => $val, |
| 526 | 525 | )); |
| 527 | 526 | unset($criteria[$name]); |
@@ -533,12 +532,12 @@ discard block |
||
| 533 | 532 | $name = substr($name, 1); |
| 534 | 533 | if (($negate = $criteria[$name][0] === '!')) |
| 535 | 534 | { |
| 536 | - $val = substr($val,1); |
|
| 535 | + $val = substr($val, 1); |
|
| 537 | 536 | } |
| 538 | - $cfcriteria[] = '(' . $this->extra_table.'.'.$this->extra_value . ' ' .($negate ? 'NOT ' : ''). |
|
| 539 | - $this->db->capabilities[Db::CAPABILITY_CASE_INSENSITIV_LIKE]. ' ' . |
|
| 540 | - $this->db->quote($wildcard.$val.$wildcard) . ' AND ' . |
|
| 541 | - $this->extra_table.'.'.$this->extra_key . ' = ' . $this->db->quote($name) . |
|
| 537 | + $cfcriteria[] = '('.$this->extra_table.'.'.$this->extra_value.' '.($negate ? 'NOT ' : ''). |
|
| 538 | + $this->db->capabilities[Db::CAPABILITY_CASE_INSENSITIV_LIKE].' '. |
|
| 539 | + $this->db->quote($wildcard.$val.$wildcard).' AND '. |
|
| 540 | + $this->extra_table.'.'.$this->extra_key.' = '.$this->db->quote($name). |
|
| 542 | 541 | ')'; |
| 543 | 542 | unset($criteria[self::CF_PREFIX.$name]); |
| 544 | 543 | } |
@@ -550,49 +549,49 @@ discard block |
||
| 550 | 549 | } |
| 551 | 550 | } |
| 552 | 551 | } |
| 553 | - if ($cfcriteria && $op =='OR') $criteria[] = implode(' OR ',$cfcriteria); |
|
| 552 | + if ($cfcriteria && $op == 'OR') $criteria[] = implode(' OR ', $cfcriteria); |
|
| 554 | 553 | } |
| 555 | - if($only_keys === true) |
|
| 554 | + if ($only_keys === true) |
|
| 556 | 555 | { |
| 557 | 556 | // Expand to keys here, so table_name can be prepended below |
| 558 | 557 | $only_keys = array_values($this->db_key_cols); |
| 559 | 558 | } |
| 560 | 559 | // replace ambiguous column with (an exact match of) table_name.column |
| 561 | - if(is_array($only_keys)) |
|
| 560 | + if (is_array($only_keys)) |
|
| 562 | 561 | { |
| 563 | - foreach($only_keys as $key => &$col) |
|
| 562 | + foreach ($only_keys as $key => &$col) |
|
| 564 | 563 | { |
| 565 | - if(is_numeric($key) && in_array($col, $this->db_cols, true)) |
|
| 564 | + if (is_numeric($key) && in_array($col, $this->db_cols, true)) |
|
| 566 | 565 | { |
| 567 | - $col = $this->table_name .'.'.array_search($col, $this->db_cols).' AS '.$col; |
|
| 566 | + $col = $this->table_name.'.'.array_search($col, $this->db_cols).' AS '.$col; |
|
| 568 | 567 | } |
| 569 | 568 | } |
| 570 | 569 | } |
| 571 | 570 | // check if we order by a custom field --> join cf table for given cf and order by it's value |
| 572 | - if (strpos($order_by,self::CF_PREFIX) !== false) |
|
| 571 | + if (strpos($order_by, self::CF_PREFIX) !== false) |
|
| 573 | 572 | { |
| 574 | 573 | // if $order_by contains more then order by columns (eg. group by) split it off before |
| 575 | 574 | if (($pos = stripos($order_by, 'order by')) !== false) |
| 576 | 575 | { |
| 577 | - $group_by = substr($order_by, 0, $pos+9); |
|
| 578 | - $order_by = substr($order_by, $pos+9); |
|
| 576 | + $group_by = substr($order_by, 0, $pos + 9); |
|
| 577 | + $order_by = substr($order_by, $pos + 9); |
|
| 579 | 578 | } |
| 580 | 579 | // fields to order by, as cutomfields may have names with spaces, we examine each order by criteria |
| 581 | - $fields2order = explode(',',$order_by); |
|
| 582 | - foreach($fields2order as $v) |
|
| 580 | + $fields2order = explode(',', $order_by); |
|
| 581 | + foreach ($fields2order as $v) |
|
| 583 | 582 | { |
| 584 | - if (strpos($v,self::CF_PREFIX) !== false) |
|
| 583 | + if (strpos($v, self::CF_PREFIX) !== false) |
|
| 585 | 584 | { |
| 586 | 585 | // we found a customfield, so we split that part by space char in order to get Sorting Direction and Fieldname |
| 587 | - $buff = explode(' ',trim($v)); |
|
| 586 | + $buff = explode(' ', trim($v)); |
|
| 588 | 587 | $orderDir = array_pop($buff); |
| 589 | - $key = substr(trim(implode(' ',$buff)), 1); |
|
| 588 | + $key = substr(trim(implode(' ', $buff)), 1); |
|
| 590 | 589 | if (!isset($this->customfields[$key])) |
| 591 | 590 | { |
| 592 | 591 | $order_by = preg_replace('/'.preg_quote($v, '/').',?/', '', $order_by); |
| 593 | - continue; // ignore unavaiable CF |
|
| 592 | + continue; // ignore unavaiable CF |
|
| 594 | 593 | } |
| 595 | - switch($this->customfields[$key]['type']) |
|
| 594 | + switch ($this->customfields[$key]['type']) |
|
| 596 | 595 | { |
| 597 | 596 | case 'int': |
| 598 | 597 | $order_by = str_replace($v, 'extra_order.'.$this->extra_value.' IS NULL,'. |
@@ -630,26 +629,26 @@ discard block |
||
| 630 | 629 | { |
| 631 | 630 | $_cfnames = array_keys($this->customfields); |
| 632 | 631 | $extra_filter = null; |
| 633 | - foreach($filter as $name => $val) |
|
| 632 | + foreach ($filter as $name => $val) |
|
| 634 | 633 | { |
| 635 | 634 | // replace ambiguous auto-id with (an exact match of) table_name.autoid |
| 636 | 635 | if (is_string($name) && $name == $this->autoinc_id) |
| 637 | 636 | { |
| 638 | 637 | if ((int)$filter[$this->autoinc_id]) |
| 639 | 638 | { |
| 640 | - $filter[] = $this->db->expression($this->table_name,$this->table_name.'.',array( |
|
| 639 | + $filter[] = $this->db->expression($this->table_name, $this->table_name.'.', array( |
|
| 641 | 640 | $this->autoinc_id => $filter[$this->autoinc_id], |
| 642 | 641 | )); |
| 643 | 642 | } |
| 644 | 643 | unset($filter[$this->autoinc_id]); |
| 645 | 644 | } |
| 646 | 645 | // replace ambiguous column with (an exact match of) table_name.column |
| 647 | - elseif (is_string($name) && $val!=null && in_array($name, $this->db_cols)) |
|
| 646 | + elseif (is_string($name) && $val != null && in_array($name, $this->db_cols)) |
|
| 648 | 647 | { |
| 649 | 648 | $extra_columns = $this->db->get_table_definitions($this->app, $this->extra_table); |
| 650 | 649 | if ($extra_columns['fd'][array_search($name, $this->db_cols)]) |
| 651 | 650 | { |
| 652 | - $filter[] = $this->db->expression($this->table_name,$this->table_name.'.',array( |
|
| 651 | + $filter[] = $this->db->expression($this->table_name, $this->table_name.'.', array( |
|
| 653 | 652 | array_search($name, $this->db_cols) => $val, |
| 654 | 653 | )); |
| 655 | 654 | unset($filter[$name]); |
@@ -661,59 +660,59 @@ discard block |
||
| 661 | 660 | if (!isset($this->customfields[$cf_name])) |
| 662 | 661 | { |
| 663 | 662 | unset($filter[$name]); |
| 664 | - continue; // ignore unavailable CF |
|
| 663 | + continue; // ignore unavailable CF |
|
| 665 | 664 | } |
| 666 | 665 | if (!empty($val)) // empty -> dont filter |
| 667 | 666 | { |
| 668 | 667 | if ($val[0] === '!') // negative filter |
| 669 | 668 | { |
| 670 | - $sql_filter = 'extra_filter.'.$this->extra_value.'!='.$this->db->quote(substr($val,1)); |
|
| 669 | + $sql_filter = 'extra_filter.'.$this->extra_value.'!='.$this->db->quote(substr($val, 1)); |
|
| 671 | 670 | } |
| 672 | 671 | else // using Db::expression to allow to use array() with possible values or NULL |
| 673 | 672 | { |
| 674 | - if($this->customfields[$cf_name]['type'] == 'select' && |
|
| 673 | + if ($this->customfields[$cf_name]['type'] == 'select' && |
|
| 675 | 674 | $this->customfields[$cf_name]['rows'] > 1) |
| 676 | 675 | { |
| 677 | 676 | // Multi-select - any entry with the filter value selected matches |
| 678 | - $sql_filter = str_replace($this->extra_value,'extra_filter.'. |
|
| 679 | - $this->extra_value,$this->db->expression($this->extra_table,array( |
|
| 680 | - $this->db->concat("','",$this->extra_value,"','").' '.$this->db->capabilities[Db::CAPABILITY_CASE_INSENSITIV_LIKE].' '.$this->db->quote('%,'.$val.',%') |
|
| 677 | + $sql_filter = str_replace($this->extra_value, 'extra_filter.'. |
|
| 678 | + $this->extra_value, $this->db->expression($this->extra_table, array( |
|
| 679 | + $this->db->concat("','", $this->extra_value, "','").' '.$this->db->capabilities[Db::CAPABILITY_CASE_INSENSITIV_LIKE].' '.$this->db->quote('%,'.$val.',%') |
|
| 681 | 680 | )) |
| 682 | 681 | ); |
| 683 | 682 | } |
| 684 | 683 | elseif ($this->customfields[$cf_name]['type'] == 'text') |
| 685 | 684 | { |
| 686 | - $sql_filter = str_replace($this->extra_value,'extra_filter.'.$this->extra_value, |
|
| 687 | - $this->db->expression($this->extra_table,array( |
|
| 685 | + $sql_filter = str_replace($this->extra_value, 'extra_filter.'.$this->extra_value, |
|
| 686 | + $this->db->expression($this->extra_table, array( |
|
| 688 | 687 | $this->extra_value.' '.$this->db->capabilities[Db::CAPABILITY_CASE_INSENSITIV_LIKE].' '.$this->db->quote($wildcard.$val.$wildcard) |
| 689 | 688 | )) |
| 690 | 689 | ); |
| 691 | 690 | } |
| 692 | 691 | else |
| 693 | 692 | { |
| 694 | - $sql_filter = str_replace($this->extra_value,'extra_filter.'. |
|
| 695 | - $this->extra_value,$this->db->expression($this->extra_table,array($this->extra_value => $val))); |
|
| 693 | + $sql_filter = str_replace($this->extra_value, 'extra_filter.'. |
|
| 694 | + $this->extra_value, $this->db->expression($this->extra_table, array($this->extra_value => $val))); |
|
| 696 | 695 | } |
| 697 | 696 | } |
| 698 | 697 | // need to use a LEFT JOIN for negative search or to allow NULL values |
| 699 | - $need_left_join = $val[0] === '!' || strpos($sql_filter,'IS NULL') !== false ? ' LEFT ' : ''; |
|
| 700 | - $join .= str_replace('extra_filter','extra_filter'.$extra_filter,$need_left_join.$this->extra_join_filter. |
|
| 698 | + $need_left_join = $val[0] === '!' || strpos($sql_filter, 'IS NULL') !== false ? ' LEFT ' : ''; |
|
| 699 | + $join .= str_replace('extra_filter', 'extra_filter'.$extra_filter, $need_left_join.$this->extra_join_filter. |
|
| 701 | 700 | ' AND extra_filter.'.$this->extra_key.'='.$this->db->quote($cf_name). |
| 702 | 701 | ' AND '.$sql_filter); |
| 703 | 702 | ++$extra_filter; |
| 704 | 703 | } |
| 705 | 704 | unset($filter[$name]); |
| 706 | 705 | } |
| 707 | - elseif(is_int($name) && $this->is_cf($val)) // lettersearch: #cfname LIKE 's%' |
|
| 706 | + elseif (is_int($name) && $this->is_cf($val)) // lettersearch: #cfname LIKE 's%' |
|
| 708 | 707 | { |
| 709 | - $_cf = explode(' ',$val); |
|
| 710 | - foreach($_cf as $cf_np) |
|
| 708 | + $_cf = explode(' ', $val); |
|
| 709 | + foreach ($_cf as $cf_np) |
|
| 711 | 710 | { |
| 712 | 711 | // building cf_name by glueing parts together (, in case someone used whitespace in their custom field names) |
| 713 | - $tcf_name = ($tcf_name?$tcf_name.' ':'').$cf_np; |
|
| 712 | + $tcf_name = ($tcf_name ? $tcf_name.' ' : '').$cf_np; |
|
| 714 | 713 | // reacts on the first one found that matches an existing customfield, should be better then the old behavior of |
| 715 | 714 | // simply splitting by " " and using the first part |
| 716 | - if ($this->is_cf($tcf_name) && ($cfn = $this->get_cf_name($tcf_name)) && array_search($cfn,(array)$_cfnames,true)!==false ) |
|
| 715 | + if ($this->is_cf($tcf_name) && ($cfn = $this->get_cf_name($tcf_name)) && array_search($cfn, (array)$_cfnames, true) !== false) |
|
| 717 | 716 | { |
| 718 | 717 | $cf = $tcf_name; |
| 719 | 718 | break; |
@@ -721,10 +720,10 @@ discard block |
||
| 721 | 720 | } |
| 722 | 721 | unset($filter[$name]); |
| 723 | 722 | $cf_name = $this->get_cf_name($cf); |
| 724 | - if (!isset($this->customfields[$cf_name])) continue; // ignore unavailable CF |
|
| 725 | - $join .= str_replace('extra_filter','extra_filter'.$extra_filter,$this->extra_join_filter. |
|
| 723 | + if (!isset($this->customfields[$cf_name])) continue; // ignore unavailable CF |
|
| 724 | + $join .= str_replace('extra_filter', 'extra_filter'.$extra_filter, $this->extra_join_filter. |
|
| 726 | 725 | ' AND extra_filter.'.$this->extra_key.'='.$this->db->quote($cf_name). |
| 727 | - ' AND '.str_replace($cf,'extra_filter.'.$this->extra_value,$val)); |
|
| 726 | + ' AND '.str_replace($cf, 'extra_filter.'.$this->extra_value, $val)); |
|
| 728 | 727 | ++$extra_filter; |
| 729 | 728 | } |
| 730 | 729 | } |
@@ -735,15 +734,14 @@ discard block |
||
| 735 | 734 | if (is_array($only_keys)) |
| 736 | 735 | { |
| 737 | 736 | $only_keys = array_values($only_keys); |
| 738 | - $only_keys[0] = 'DISTINCT '.($only_keys[0] != $this->autoinc_id ? $only_keys[0] : |
|
| 739 | - $this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id); |
|
| 737 | + $only_keys[0] = 'DISTINCT '.($only_keys[0] != $this->autoinc_id ? $only_keys[0] : $this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id); |
|
| 740 | 738 | } |
| 741 | 739 | else |
| 742 | 740 | { |
| 743 | 741 | $only_keys = 'DISTINCT '.$only_keys; |
| 744 | 742 | } |
| 745 | 743 | } |
| 746 | - return parent::search($criteria,$only_keys,$order_by,$extra_cols,$wildcard,$empty,$op,$start,$filter,$join,$need_full_no_count); |
|
| 744 | + return parent::search($criteria, $only_keys, $order_by, $extra_cols, $wildcard, $empty, $op, $start, $filter, $join, $need_full_no_count); |
|
| 747 | 745 | } |
| 748 | 746 | |
| 749 | 747 | /** |
@@ -765,7 +763,7 @@ discard block |
||
| 765 | 763 | */ |
| 766 | 764 | function get_cf_name($field) |
| 767 | 765 | { |
| 768 | - return substr($field,1); |
|
| 766 | + return substr($field, 1); |
|
| 769 | 767 | } |
| 770 | 768 | |
| 771 | 769 | /** |
@@ -787,7 +785,7 @@ discard block |
||
| 787 | 785 | */ |
| 788 | 786 | function is_multiple($name) |
| 789 | 787 | { |
| 790 | - return $this->allow_multiple_values && in_array($this->customfields[$name]['type'],array('select','select-account')) && |
|
| 788 | + return $this->allow_multiple_values && in_array($this->customfields[$name]['type'], array('select', 'select-account')) && |
|
| 791 | 789 | $this->customfields[$name]['rows'] > 1; |
| 792 | 790 | } |
| 793 | 791 | } |
@@ -127,7 +127,11 @@ discard block |
||
| 127 | 127 | |
| 128 | 128 | $this->allow_multiple_values = $allow_multiple_values; |
| 129 | 129 | $this->extra_table = $extra_table; |
| 130 | - if (!$this->extra_id) $this->extra_id = $this->autoinc_id; // default to auto id of regular table |
|
| 130 | + if (!$this->extra_id) |
|
| 131 | + { |
|
| 132 | + $this->extra_id = $this->autoinc_id; |
|
| 133 | + } |
|
| 134 | + // default to auto id of regular table |
|
| 131 | 135 | |
| 132 | 136 | // if names from columns of extra table are only postfixes (starting with _), prepend column prefix |
| 133 | 137 | if (!($prefix=$column_prefix)) |
@@ -145,7 +149,10 @@ discard block |
||
| 145 | 149 | ) as $col => $val) |
| 146 | 150 | { |
| 147 | 151 | $this->$col = $col_name = $val; |
| 148 | - if ($col_name[0] == '_') $this->$col = $prefix . $val; |
|
| 152 | + if ($col_name[0] == '_') |
|
| 153 | + { |
|
| 154 | + $this->$col = $prefix . $val; |
|
| 155 | + } |
|
| 149 | 156 | } |
| 150 | 157 | // some sanity checks, maybe they should be active only for development |
| 151 | 158 | if (!($extra_defs = $this->db->get_table_definitions($app,$extra_table))) |
@@ -179,13 +186,23 @@ discard block |
||
| 179 | 186 | */ |
| 180 | 187 | function read_customfields($ids,$field_names=null) |
| 181 | 188 | { |
| 182 | - if (is_null($field_names)) $field_names = array_keys($this->customfields); |
|
| 189 | + if (is_null($field_names)) |
|
| 190 | + { |
|
| 191 | + $field_names = array_keys($this->customfields); |
|
| 192 | + } |
|
| 183 | 193 | |
| 184 | 194 | foreach((array)$ids as $key => $id) |
| 185 | 195 | { |
| 186 | - if (!(int)$id && is_array($ids)) unset($ids[$key]); |
|
| 196 | + if (!(int)$id && is_array($ids)) |
|
| 197 | + { |
|
| 198 | + unset($ids[$key]); |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + if (!$ids || !$field_names) |
|
| 202 | + { |
|
| 203 | + return array(); |
|
| 187 | 204 | } |
| 188 | - if (!$ids || !$field_names) return array(); // nothing to do |
|
| 205 | + // nothing to do |
|
| 189 | 206 | |
| 190 | 207 | $entries = array(); |
| 191 | 208 | foreach($this->db->select($this->extra_table,'*',array( |
@@ -194,7 +211,10 @@ discard block |
||
| 194 | 211 | ),__LINE__,__FILE__,false,'',$this->app) as $row) |
| 195 | 212 | { |
| 196 | 213 | $entry =& $entries[$row[$this->extra_id]]; |
| 197 | - if (!is_array($entry)) $entry = array(); |
|
| 214 | + if (!is_array($entry)) |
|
| 215 | + { |
|
| 216 | + $entry = array(); |
|
| 217 | + } |
|
| 198 | 218 | $field = $this->get_cf_field($row[$this->extra_key]); |
| 199 | 219 | |
| 200 | 220 | if ($this->allow_multiple_values && $this->is_multiple($row[$this->extra_key])) |
@@ -224,7 +244,10 @@ discard block |
||
| 224 | 244 | |
| 225 | 245 | foreach (array_keys((array)$this->customfields) as $name) |
| 226 | 246 | { |
| 227 | - if (!isset($data[$field = $this->get_cf_field($name)])) continue; |
|
| 247 | + if (!isset($data[$field = $this->get_cf_field($name)])) |
|
| 248 | + { |
|
| 249 | + continue; |
|
| 250 | + } |
|
| 228 | 251 | |
| 229 | 252 | $where = array( |
| 230 | 253 | $this->extra_id => $id, |
@@ -236,7 +259,11 @@ discard block |
||
| 236 | 259 | if(empty($data[$field]) || $is_multiple || !$this->extra_has_unique_index) |
| 237 | 260 | { |
| 238 | 261 | $this->db->delete($this->extra_table,$where,__LINE__,__FILE__,$this->app); |
| 239 | - if (empty($data[$field])) continue; // nothing else to do for empty values |
|
| 262 | + if (empty($data[$field])) |
|
| 263 | + { |
|
| 264 | + continue; |
|
| 265 | + } |
|
| 266 | + // nothing else to do for empty values |
|
| 240 | 267 | } |
| 241 | 268 | foreach($is_multiple && !is_array($data[$field]) ? explode(',',$data[$field]) : |
| 242 | 269 | // regular custom fields (!$is_multiple) eg. addressbook store multiple values comma-separated |
@@ -309,9 +336,12 @@ discard block |
||
| 309 | 336 | */ |
| 310 | 337 | function save($keys=null,$extra_where=null) |
| 311 | 338 | { |
| 312 | - if (is_array($keys) && count($keys) && !isset($keys[0])) // allow to use an etag, eg array('etag=etag+1') |
|
| 339 | + if (is_array($keys) && count($keys) && !isset($keys[0])) |
|
| 340 | + { |
|
| 341 | + // allow to use an etag, eg array('etag=etag+1') |
|
| 313 | 342 | { |
| 314 | 343 | $this->data_merge($keys); |
| 344 | + } |
|
| 315 | 345 | $keys = null; |
| 316 | 346 | } |
| 317 | 347 | $ret = parent::save($keys,$extra_where); |
@@ -344,13 +374,20 @@ discard block |
||
| 344 | 374 | { |
| 345 | 375 | $ids[] = $row[$this->autoinc_id]; |
| 346 | 376 | } |
| 347 | - if (!$ids) return 0; // no rows affected |
|
| 377 | + if (!$ids) |
|
| 378 | + { |
|
| 379 | + return 0; |
|
| 380 | + } |
|
| 381 | + // no rows affected |
|
| 348 | 382 | } |
| 349 | 383 | else |
| 350 | 384 | { |
| 351 | 385 | $ids = (array)$query[$this->autoinc_id]; |
| 352 | 386 | } |
| 353 | - if ($only_return_ids) return $ids; |
|
| 387 | + if ($only_return_ids) |
|
| 388 | + { |
|
| 389 | + return $ids; |
|
| 390 | + } |
|
| 354 | 391 | $this->db->delete($this->extra_table,array($this->extra_id => $ids),__LINE__,__FILE__); |
| 355 | 392 | } |
| 356 | 393 | return parent::delete($keys); |
@@ -392,7 +429,10 @@ discard block |
||
| 392 | 429 | { |
| 393 | 430 | foreach($selectcols as $col) |
| 394 | 431 | { |
| 395 | - if ($this->is_cf($col)) $fields[] = $this->get_cf_name($col); |
|
| 432 | + if ($this->is_cf($col)) |
|
| 433 | + { |
|
| 434 | + $fields[] = $this->get_cf_name($col); |
|
| 435 | + } |
|
| 396 | 436 | } |
| 397 | 437 | } |
| 398 | 438 | if (($cfs = $this->read_customfields(array_keys($id2keys),$fields))) |
@@ -418,7 +458,11 @@ discard block |
||
| 418 | 458 | { |
| 419 | 459 | static $private_cfs=null; |
| 420 | 460 | |
| 421 | - if (!$this->customfields) return ''; // no custom-fields --> no search |
|
| 461 | + if (!$this->customfields) |
|
| 462 | + { |
|
| 463 | + return ''; |
|
| 464 | + } |
|
| 465 | + // no custom-fields --> no search |
|
| 422 | 466 | |
| 423 | 467 | $sql = ' OR '.$this->table_name.'.'.$this->autoinc_id.' IN (SELECT '.$this->autoinc_id. |
| 424 | 468 | ' FROM '.$this->extra_table.' WHERE '.$this->extra_value.' '. |
@@ -550,7 +594,10 @@ discard block |
||
| 550 | 594 | } |
| 551 | 595 | } |
| 552 | 596 | } |
| 553 | - if ($cfcriteria && $op =='OR') $criteria[] = implode(' OR ',$cfcriteria); |
|
| 597 | + if ($cfcriteria && $op =='OR') |
|
| 598 | + { |
|
| 599 | + $criteria[] = implode(' OR ',$cfcriteria); |
|
| 600 | + } |
|
| 554 | 601 | } |
| 555 | 602 | if($only_keys === true) |
| 556 | 603 | { |
@@ -663,11 +710,14 @@ discard block |
||
| 663 | 710 | unset($filter[$name]); |
| 664 | 711 | continue; // ignore unavailable CF |
| 665 | 712 | } |
| 666 | - if (!empty($val)) // empty -> dont filter |
|
| 713 | + if (!empty($val)) |
|
| 714 | + { |
|
| 715 | + // empty -> dont filter |
|
| 667 | 716 | { |
| 668 | 717 | if ($val[0] === '!') // negative filter |
| 669 | 718 | { |
| 670 | 719 | $sql_filter = 'extra_filter.'.$this->extra_value.'!='.$this->db->quote(substr($val,1)); |
| 720 | + } |
|
| 671 | 721 | } |
| 672 | 722 | else // using Db::expression to allow to use array() with possible values or NULL |
| 673 | 723 | { |
@@ -704,9 +754,12 @@ discard block |
||
| 704 | 754 | } |
| 705 | 755 | unset($filter[$name]); |
| 706 | 756 | } |
| 707 | - elseif(is_int($name) && $this->is_cf($val)) // lettersearch: #cfname LIKE 's%' |
|
| 757 | + elseif(is_int($name) && $this->is_cf($val)) |
|
| 758 | + { |
|
| 759 | + // lettersearch: #cfname LIKE 's%' |
|
| 708 | 760 | { |
| 709 | 761 | $_cf = explode(' ',$val); |
| 762 | + } |
|
| 710 | 763 | foreach($_cf as $cf_np) |
| 711 | 764 | { |
| 712 | 765 | // building cf_name by glueing parts together (, in case someone used whitespace in their custom field names) |
@@ -721,7 +774,11 @@ discard block |
||
| 721 | 774 | } |
| 722 | 775 | unset($filter[$name]); |
| 723 | 776 | $cf_name = $this->get_cf_name($cf); |
| 724 | - if (!isset($this->customfields[$cf_name])) continue; // ignore unavailable CF |
|
| 777 | + if (!isset($this->customfields[$cf_name])) |
|
| 778 | + { |
|
| 779 | + continue; |
|
| 780 | + } |
|
| 781 | + // ignore unavailable CF |
|
| 725 | 782 | $join .= str_replace('extra_filter','extra_filter'.$extra_filter,$this->extra_join_filter. |
| 726 | 783 | ' AND extra_filter.'.$this->extra_key.'='.$this->db->quote($cf_name). |
| 727 | 784 | ' AND '.str_replace($cf,'extra_filter.'.$this->extra_value,$val)); |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | * @param string $timestamp_type =null default null=leave them as is, 'ts'|'integer' use integer unix timestamps, |
| 180 | 180 | * 'object' use Api\DateTime objects or 'string' use DB timestamp (Y-m-d H:i:s) string |
| 181 | 181 | */ |
| 182 | - function __construct($app='',$table='',Api\Db $db=null,$column_prefix='',$no_clone=false,$timestamp_type=null) |
|
| 182 | + function __construct($app = '', $table = '', Api\Db $db = null, $column_prefix = '', $no_clone = false, $timestamp_type = null) |
|
| 183 | 183 | { |
| 184 | 184 | // ease the transition to api |
| 185 | 185 | if ($app == 'phpgwapi') $app = 'api'; |
@@ -200,11 +200,11 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | if (!$no_clone) $this->db->set_app($app); |
| 202 | 202 | |
| 203 | - if ($table) $this->setup_table($app,$table,$column_prefix); |
|
| 203 | + if ($table) $this->setup_table($app, $table, $column_prefix); |
|
| 204 | 204 | } |
| 205 | 205 | $this->init(); |
| 206 | 206 | |
| 207 | - if ((int) $this->debug >= 4) |
|
| 207 | + if ((int)$this->debug >= 4) |
|
| 208 | 208 | { |
| 209 | 209 | echo "<p>".__METHOD__."('$app','$table')</p>\n"; |
| 210 | 210 | _debug_array($this); |
@@ -219,21 +219,21 @@ discard block |
||
| 219 | 219 | * null=leave them as is, 'ts'|'integer' use integer unix timestamps, 'object' use Api\DateTime objects, |
| 220 | 220 | * 'string' use DB timestamp (Y-m-d H:i:s) string |
| 221 | 221 | */ |
| 222 | - public function set_times($timestamp_type=false) |
|
| 222 | + public function set_times($timestamp_type = false) |
|
| 223 | 223 | { |
| 224 | 224 | if ($timestamp_type !== false) $this->timestamp_type = $timestamp_type; |
| 225 | 225 | |
| 226 | 226 | // set current time |
| 227 | - switch($this->timestamp_type) |
|
| 227 | + switch ($this->timestamp_type) |
|
| 228 | 228 | { |
| 229 | 229 | case 'object': |
| 230 | 230 | $this->now = new Api\DateTime('now'); |
| 231 | 231 | break; |
| 232 | 232 | case 'string': |
| 233 | - $this->now = Api\DateTime::to('now',Api\DateTime::DATABASE); |
|
| 233 | + $this->now = Api\DateTime::to('now', Api\DateTime::DATABASE); |
|
| 234 | 234 | break; |
| 235 | 235 | default: |
| 236 | - $this->now = Api\DateTime::to('now','ts'); |
|
| 236 | + $this->now = Api\DateTime::to('now', 'ts'); |
|
| 237 | 237 | } |
| 238 | 238 | $this->tz_offset_s = Api\DateTime::tz_offset_s(); |
| 239 | 239 | } |
@@ -247,10 +247,10 @@ discard block |
||
| 247 | 247 | * @param string $table table-name |
| 248 | 248 | * @param string $colum_prefix ='' column prefix to automatic remove from the column-name, if the column name starts with it |
| 249 | 249 | */ |
| 250 | - function setup_table($app,$table,$colum_prefix='') |
|
| 250 | + function setup_table($app, $table, $colum_prefix = '') |
|
| 251 | 251 | { |
| 252 | 252 | $this->table_name = $table; |
| 253 | - $this->table_def = $this->db->get_table_definitions($app,$table); |
|
| 253 | + $this->table_def = $this->db->get_table_definitions($app, $table); |
|
| 254 | 254 | if (!$this->table_def || !is_array($this->table_def['fd'])) |
| 255 | 255 | { |
| 256 | 256 | throw new Api\Exception\WrongParameter(__METHOD__."('$app','$table'): No table definition for '$table' found !!!"); |
@@ -258,14 +258,14 @@ discard block |
||
| 258 | 258 | $this->db_key_cols = $this->db_data_cols = $this->db_cols = array(); |
| 259 | 259 | $this->autoinc_id = ''; |
| 260 | 260 | $len_prefix = strlen($colum_prefix); |
| 261 | - foreach($this->table_def['fd'] as $col => $def) |
|
| 261 | + foreach ($this->table_def['fd'] as $col => $def) |
|
| 262 | 262 | { |
| 263 | 263 | $name = $col; |
| 264 | - if ($len_prefix && substr($name,0,$len_prefix) == $colum_prefix) |
|
| 264 | + if ($len_prefix && substr($name, 0, $len_prefix) == $colum_prefix) |
|
| 265 | 265 | { |
| 266 | - $name = substr($col,$len_prefix); |
|
| 266 | + $name = substr($col, $len_prefix); |
|
| 267 | 267 | } |
| 268 | - if (in_array($col,$this->table_def['pk'])) |
|
| 268 | + if (in_array($col, $this->table_def['pk'])) |
|
| 269 | 269 | { |
| 270 | 270 | $this->db_key_cols[$col] = $name; |
| 271 | 271 | } |
@@ -281,13 +281,13 @@ discard block |
||
| 281 | 281 | } |
| 282 | 282 | if ($def['type'] == 'bool') $this->has_bools = true; |
| 283 | 283 | |
| 284 | - foreach($this->table_def['uc'] as $k => $uni_index) |
|
| 284 | + foreach ($this->table_def['uc'] as $k => $uni_index) |
|
| 285 | 285 | { |
| 286 | - if (is_array($uni_index) && in_array($name,$uni_index)) |
|
| 286 | + if (is_array($uni_index) && in_array($name, $uni_index)) |
|
| 287 | 287 | { |
| 288 | 288 | $this->db_uni_cols[$k][$col] = $name; |
| 289 | 289 | } |
| 290 | - elseif($name === $uni_index) |
|
| 290 | + elseif ($name === $uni_index) |
|
| 291 | 291 | { |
| 292 | 292 | $this->db_uni_cols[$col] = $name; |
| 293 | 293 | } |
@@ -302,9 +302,9 @@ discard block |
||
| 302 | 302 | function convert_all_timestamps() |
| 303 | 303 | { |
| 304 | 304 | $check_already_included = !empty($this->timestamps); |
| 305 | - foreach($this->table_def['fd'] as $name => $data) |
|
| 305 | + foreach ($this->table_def['fd'] as $name => $data) |
|
| 306 | 306 | { |
| 307 | - if ($data['type'] == 'timestamp' && (!$check_already_included || !in_array($name,$this->timestamps))) |
|
| 307 | + if ($data['type'] == 'timestamp' && (!$check_already_included || !in_array($name, $this->timestamps))) |
|
| 308 | 308 | { |
| 309 | 309 | $this->timestamps[] = $name; |
| 310 | 310 | } |
@@ -318,22 +318,22 @@ discard block |
||
| 318 | 318 | */ |
| 319 | 319 | function data_merge($new) |
| 320 | 320 | { |
| 321 | - if ((int) $this->debug >= 4) echo "<p>so_sql::data_merge(".print_r($new,true).")</p>\n"; |
|
| 321 | + if ((int)$this->debug >= 4) echo "<p>so_sql::data_merge(".print_r($new, true).")</p>\n"; |
|
| 322 | 322 | |
| 323 | 323 | if (!is_array($new) || !count($new)) |
| 324 | 324 | { |
| 325 | 325 | return; |
| 326 | 326 | } |
| 327 | - foreach($this->db_cols as $db_col => $col) |
|
| 327 | + foreach ($this->db_cols as $db_col => $col) |
|
| 328 | 328 | { |
| 329 | - if (array_key_exists($col,$new)) |
|
| 329 | + if (array_key_exists($col, $new)) |
|
| 330 | 330 | { |
| 331 | 331 | $this->data[$col] = $new[$col]; |
| 332 | 332 | } |
| 333 | 333 | } |
| 334 | - foreach($this->non_db_cols as $db_col => $col) |
|
| 334 | + foreach ($this->non_db_cols as $db_col => $col) |
|
| 335 | 335 | { |
| 336 | - if (array_key_exists($col,$new)) |
|
| 336 | + if (array_key_exists($col, $new)) |
|
| 337 | 337 | { |
| 338 | 338 | $this->data[$col] = $new[$col]; |
| 339 | 339 | } |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | { |
| 343 | 343 | $this->data[self::USER_TIMEZONE_READ] = $new[self::USER_TIMEZONE_READ]; |
| 344 | 344 | } |
| 345 | - if ((int) $this->debug >= 4) _debug_array($this->data); |
|
| 345 | + if ((int)$this->debug >= 4) _debug_array($this->data); |
|
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | /** |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | * @param array $data =null if given works on that array and returns result, else works on internal data-array |
| 367 | 367 | * @return array |
| 368 | 368 | */ |
| 369 | - function db2data($data=null) |
|
| 369 | + function db2data($data = null) |
|
| 370 | 370 | { |
| 371 | 371 | if (!is_array($data)) |
| 372 | 372 | { |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | } |
| 375 | 375 | if ($this->timestamps) |
| 376 | 376 | { |
| 377 | - foreach($this->timestamps as $name) |
|
| 377 | + foreach ($this->timestamps as $name) |
|
| 378 | 378 | { |
| 379 | 379 | if (isset($data[$name]) && $data[$name]) |
| 380 | 380 | { |
@@ -384,7 +384,7 @@ discard block |
||
| 384 | 384 | } |
| 385 | 385 | else |
| 386 | 386 | { |
| 387 | - $data[$name] = Api\DateTime::server2user($data[$name],$this->timestamp_type); |
|
| 387 | + $data[$name] = Api\DateTime::server2user($data[$name], $this->timestamp_type); |
|
| 388 | 388 | } |
| 389 | 389 | } |
| 390 | 390 | } |
@@ -400,7 +400,7 @@ discard block |
||
| 400 | 400 | throw new Api\Exception\WrongParameter(__METHOD__."(): No table definition for '$this->table' found !!!"); |
| 401 | 401 | } |
| 402 | 402 | } |
| 403 | - foreach($this->table_def['fd'] as $col => $def) |
|
| 403 | + foreach ($this->table_def['fd'] as $col => $def) |
|
| 404 | 404 | { |
| 405 | 405 | if ($def['type'] == 'bool' && isset($data[$col])) |
| 406 | 406 | { |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | * @param array $data =null if given works on that array and returns result, else works on internal data-array |
| 433 | 433 | * @return array |
| 434 | 434 | */ |
| 435 | - function data2db($data=null) |
|
| 435 | + function data2db($data = null) |
|
| 436 | 436 | { |
| 437 | 437 | if (!is_array($data)) |
| 438 | 438 | { |
@@ -440,11 +440,11 @@ discard block |
||
| 440 | 440 | } |
| 441 | 441 | if ($this->timestamps) |
| 442 | 442 | { |
| 443 | - foreach($this->timestamps as $name) |
|
| 443 | + foreach ($this->timestamps as $name) |
|
| 444 | 444 | { |
| 445 | 445 | if (isset($data[$name]) && $data[$name]) |
| 446 | 446 | { |
| 447 | - $data[$name] = Api\DateTime::user2server($data[$name],$this->timestamp_type); |
|
| 447 | + $data[$name] = Api\DateTime::user2server($data[$name], $this->timestamp_type); |
|
| 448 | 448 | } |
| 449 | 449 | } |
| 450 | 450 | } |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | * @param array $keys =array() array with keys in form internalName => value |
| 458 | 458 | * @return array internal data after init |
| 459 | 459 | */ |
| 460 | - function init($keys=array()) |
|
| 460 | + function init($keys = array()) |
|
| 461 | 461 | { |
| 462 | 462 | $this->data = array(); |
| 463 | 463 | |
@@ -481,7 +481,7 @@ discard block |
||
| 481 | 481 | * @param string $join ='' sql to do a join, added as is after the table-name, eg. ", table2 WHERE x=y" or |
| 482 | 482 | * @return array|boolean data if row could be retrived else False |
| 483 | 483 | */ |
| 484 | - function read($keys,$extra_cols='',$join='') |
|
| 484 | + function read($keys, $extra_cols = '', $join = '') |
|
| 485 | 485 | { |
| 486 | 486 | if (!is_array($keys)) |
| 487 | 487 | { |
@@ -502,16 +502,16 @@ discard block |
||
| 502 | 502 | } |
| 503 | 503 | if (!$query) // no primary key in keys, lets try the data_cols for a unique key |
| 504 | 504 | { |
| 505 | - foreach($this->db_uni_cols as $db_col => $col) |
|
| 505 | + foreach ($this->db_uni_cols as $db_col => $col) |
|
| 506 | 506 | { |
| 507 | 507 | if (!is_array($col) && $this->data[$col] != '') |
| 508 | 508 | { |
| 509 | 509 | $query[$db_col] = $this->data[$col]; |
| 510 | 510 | } |
| 511 | - elseif(is_array($col)) |
|
| 511 | + elseif (is_array($col)) |
|
| 512 | 512 | { |
| 513 | 513 | $q = array(); |
| 514 | - foreach($col as $db_c => $c) |
|
| 514 | + foreach ($col as $db_c => $c) |
|
| 515 | 515 | { |
| 516 | 516 | if ($this->data[$col] == '') |
| 517 | 517 | { |
@@ -526,7 +526,7 @@ discard block |
||
| 526 | 526 | } |
| 527 | 527 | if (!$query) // no unique key in keys, lets try everything else |
| 528 | 528 | { |
| 529 | - foreach($this->db_data_cols as $db_col => $col) |
|
| 529 | + foreach ($this->db_data_cols as $db_col => $col) |
|
| 530 | 530 | { |
| 531 | 531 | if ($this->data[$col] != '') |
| 532 | 532 | { |
@@ -542,27 +542,27 @@ discard block |
||
| 542 | 542 | } |
| 543 | 543 | if ($join) // Prefix the columns with the table-name, as they might exist in the join |
| 544 | 544 | { |
| 545 | - foreach($query as $col => $val) |
|
| 545 | + foreach ($query as $col => $val) |
|
| 546 | 546 | { |
| 547 | - if (is_int($col) || strpos($join,$col) === false) continue; |
|
| 548 | - $query[] = $this->db->expression($this->table_name,$this->table_name.'.',array($col=>$val)); |
|
| 547 | + if (is_int($col) || strpos($join, $col) === false) continue; |
|
| 548 | + $query[] = $this->db->expression($this->table_name, $this->table_name.'.', array($col=>$val)); |
|
| 549 | 549 | unset($query[$col]); |
| 550 | 550 | } |
| 551 | 551 | } |
| 552 | 552 | // copy direct SQL parts from $keys |
| 553 | - for($i = 0; is_array($keys) && isset($keys[$i]); ++$i) |
|
| 553 | + for ($i = 0; is_array($keys) && isset($keys[$i]); ++$i) |
|
| 554 | 554 | { |
| 555 | 555 | $query[] = $keys[$i]; |
| 556 | 556 | } |
| 557 | - foreach($this->db->select($this->table_name,'*'.($extra_cols?','.(is_array($extra_cols)?implode(',',$extra_cols):$extra_cols):''), |
|
| 558 | - $query,__LINE__,__FILE__,False,'',$this->app,0,$join) as $row) |
|
| 557 | + foreach ($this->db->select($this->table_name, '*'.($extra_cols ? ','.(is_array($extra_cols) ?implode(',', $extra_cols) : $extra_cols) : ''), |
|
| 558 | + $query, __LINE__, __FILE__, False, '', $this->app, 0, $join) as $row) |
|
| 559 | 559 | { |
| 560 | 560 | $cols = $this->db_cols; |
| 561 | 561 | if ($extra_cols) // extra columns to report |
| 562 | 562 | { |
| 563 | - foreach(is_array($extra_cols) ? $extra_cols : array($extra_cols) as $col) |
|
| 563 | + foreach (is_array($extra_cols) ? $extra_cols : array($extra_cols) as $col) |
|
| 564 | 564 | { |
| 565 | - if (FALSE!==stripos($col,' as ')) $col = preg_replace('/^.* as *([a-z0-9_]+) *$/i','\\1',$col); |
|
| 565 | + if (FALSE !== stripos($col, ' as ')) $col = preg_replace('/^.* as *([a-z0-9_]+) *$/i', '\\1', $col); |
|
| 566 | 566 | $cols[$col] = $col; |
| 567 | 567 | } |
| 568 | 568 | } |
@@ -575,7 +575,7 @@ discard block |
||
| 575 | 575 | // store user timezone used for reading |
| 576 | 576 | $this->data[self::USER_TIMEZONE_READ] = Api\DateTime::$user_timezone->getName(); |
| 577 | 577 | |
| 578 | - if ((int) $this->debug >= 4) |
|
| 578 | + if ((int)$this->debug >= 4) |
|
| 579 | 579 | { |
| 580 | 580 | echo "data =\n"; _debug_array($this->data); |
| 581 | 581 | } |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | { |
| 586 | 586 | unset($this->data[$this->db_key_cols[$this->autoinc_id]]); |
| 587 | 587 | } |
| 588 | - if ((int) $this->debug >= 4) echo "nothing found !!!</p>\n"; |
|
| 588 | + if ((int)$this->debug >= 4) echo "nothing found !!!</p>\n"; |
|
| 589 | 589 | |
| 590 | 590 | $this->db2data(); |
| 591 | 591 | |
@@ -599,7 +599,7 @@ discard block |
||
| 599 | 599 | * @param string|array $extra_where =null extra where clause, eg. to check an etag, returns true if no affected rows! |
| 600 | 600 | * @return int|boolean 0 on success, or errno != 0 on error, or true if $extra_where is given and no rows affected |
| 601 | 601 | */ |
| 602 | - function save($keys=null,$extra_where=null) |
|
| 602 | + function save($keys = null, $extra_where = null) |
|
| 603 | 603 | { |
| 604 | 604 | if (is_array($keys) && count($keys)) $this->data_merge($keys); |
| 605 | 605 | |
@@ -618,65 +618,65 @@ discard block |
||
| 618 | 618 | } |
| 619 | 619 | $this->data2db(); |
| 620 | 620 | |
| 621 | - if ((int) $this->debug >= 4) { echo "so_sql::save(".print_r($keys,true).") autoinc_id='$this->autoinc_id', data="; _debug_array($this->data); } |
|
| 621 | + if ((int)$this->debug >= 4) { echo "so_sql::save(".print_r($keys, true).") autoinc_id='$this->autoinc_id', data="; _debug_array($this->data); } |
|
| 622 | 622 | |
| 623 | 623 | if ($this->autoinc_id && !$this->data[$this->db_key_cols[$this->autoinc_id]]) // insert with auto id |
| 624 | 624 | { |
| 625 | - foreach($this->db_cols as $db_col => $col) |
|
| 625 | + foreach ($this->db_cols as $db_col => $col) |
|
| 626 | 626 | { |
| 627 | 627 | if (!$this->autoinc_id || $db_col != $this->autoinc_id) // not write auto-inc-id |
| 628 | 628 | { |
| 629 | - if (!array_key_exists($col,$this->data) && // handling of unset columns in $this->data |
|
| 630 | - (isset($this->table_def['fd'][$db_col]['default']) || // we have a default value |
|
| 629 | + if (!array_key_exists($col, $this->data) && // handling of unset columns in $this->data |
|
| 630 | + (isset($this->table_def['fd'][$db_col]['default']) || // we have a default value |
|
| 631 | 631 | !isset($this->table_def['fd'][$db_col]['nullable']) || $this->table_def['fd'][$db_col]['nullable'])) // column is nullable |
| 632 | 632 | { |
| 633 | - continue; // no need to write that (unset) column |
|
| 633 | + continue; // no need to write that (unset) column |
|
| 634 | 634 | } |
| 635 | 635 | if ($this->table_def['fd'][$db_col]['type'] == 'varchar' && |
| 636 | 636 | strlen($this->data[$col]) > $this->table_def['fd'][$db_col]['precision']) |
| 637 | 637 | { |
| 638 | 638 | // truncate the field to mamimum length, if upper layers didn't care |
| 639 | - $data[$db_col] = substr($this->data[$col],0,$this->table_def['fd'][$db_col]['precision']); |
|
| 639 | + $data[$db_col] = substr($this->data[$col], 0, $this->table_def['fd'][$db_col]['precision']); |
|
| 640 | 640 | } |
| 641 | 641 | else |
| 642 | 642 | { |
| 643 | - $data[$db_col] = (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; |
|
| 643 | + $data[$db_col] = (string)$this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; |
|
| 644 | 644 | } |
| 645 | 645 | } |
| 646 | 646 | } |
| 647 | - $this->db->insert($this->table_name,$data,false,__LINE__,__FILE__,$this->app); |
|
| 647 | + $this->db->insert($this->table_name, $data, false, __LINE__, __FILE__, $this->app); |
|
| 648 | 648 | |
| 649 | 649 | if ($this->autoinc_id) |
| 650 | 650 | { |
| 651 | - $this->data[$this->db_key_cols[$this->autoinc_id]] = $this->db->get_last_insert_id($this->table_name,$this->autoinc_id); |
|
| 651 | + $this->data[$this->db_key_cols[$this->autoinc_id]] = $this->db->get_last_insert_id($this->table_name, $this->autoinc_id); |
|
| 652 | 652 | } |
| 653 | 653 | } |
| 654 | 654 | else // insert in table without auto id or update of existing row, dont write colums unset in $this->data |
| 655 | 655 | { |
| 656 | - foreach($this->db_data_cols as $db_col => $col) |
|
| 656 | + foreach ($this->db_data_cols as $db_col => $col) |
|
| 657 | 657 | { |
| 658 | 658 | // we need to update columns set to null: after a $this->data[$col]=null: |
| 659 | 659 | // - array_key_exits($col,$this->data) === true |
| 660 | 660 | // - isset($this->data[$col]) === false |
| 661 | - if (!array_key_exists($col,$this->data) && // handling of unset columns in $this->data |
|
| 662 | - ($this->autoinc_id || // update of table with auto id or |
|
| 663 | - isset($this->table_def['fd'][$db_col]['default']) || // we have a default value or |
|
| 661 | + if (!array_key_exists($col, $this->data) && // handling of unset columns in $this->data |
|
| 662 | + ($this->autoinc_id || // update of table with auto id or |
|
| 663 | + isset($this->table_def['fd'][$db_col]['default']) || // we have a default value or |
|
| 664 | 664 | !isset($this->table_def['fd'][$db_col]['nullable']) || $this->table_def['fd'][$db_col]['nullable'])) // column is nullable |
| 665 | 665 | { |
| 666 | - continue; // no need to write that (unset) column |
|
| 666 | + continue; // no need to write that (unset) column |
|
| 667 | 667 | } |
| 668 | - $data[$db_col] = !is_object($this->data[$col]) && (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; |
|
| 668 | + $data[$db_col] = !is_object($this->data[$col]) && (string)$this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col]; |
|
| 669 | 669 | } |
| 670 | 670 | // allow to add direct sql updates, eg. "etag=etag+1" with int keys |
| 671 | 671 | if (is_array($keys) && isset($keys[0])) |
| 672 | 672 | { |
| 673 | - for($n=0; isset($keys[$n]); ++$n) |
|
| 673 | + for ($n = 0; isset($keys[$n]); ++$n) |
|
| 674 | 674 | { |
| 675 | 675 | $data[] = $keys[$n]; |
| 676 | 676 | } |
| 677 | 677 | } |
| 678 | 678 | $keys = $extra_where; |
| 679 | - foreach($this->db_key_cols as $db_col => $col) |
|
| 679 | + foreach ($this->db_key_cols as $db_col => $col) |
|
| 680 | 680 | { |
| 681 | 681 | $keys[$db_col] = $this->data[$col]; |
| 682 | 682 | } |
@@ -687,16 +687,16 @@ discard block |
||
| 687 | 687 | } |
| 688 | 688 | if ($this->autoinc_id) |
| 689 | 689 | { |
| 690 | - $this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__,$this->app); |
|
| 690 | + $this->db->update($this->table_name, $data, $keys, __LINE__, __FILE__, $this->app); |
|
| 691 | 691 | if (($nothing_affected = !$this->db->Errno && !$this->db->affected_rows()) && $extra_where) |
| 692 | 692 | { |
| 693 | - return true; // extra_where not met, eg. etag wrong |
|
| 693 | + return true; // extra_where not met, eg. etag wrong |
|
| 694 | 694 | } |
| 695 | 695 | } |
| 696 | 696 | // always try an insert if we have no autoinc_id, as we dont know if the data exists |
| 697 | 697 | if (!$this->autoinc_id || $nothing_affected) |
| 698 | 698 | { |
| 699 | - $this->db->insert($this->table_name,$data,$keys,__LINE__,__FILE__,$this->app); |
|
| 699 | + $this->db->insert($this->table_name, $data, $keys, __LINE__, __FILE__, $this->app); |
|
| 700 | 700 | } |
| 701 | 701 | } |
| 702 | 702 | $this->db2data(); |
@@ -711,7 +711,7 @@ discard block |
||
| 711 | 711 | * @param boolean $merge =true if true $fields will be merged with $this->data (after update!), otherwise $this->data will be just $fields |
| 712 | 712 | * @return int|boolean 0 on success, or errno != 0 on error, or true if $extra_where is given and no rows affected |
| 713 | 713 | */ |
| 714 | - function update($_fields,$merge=true) |
|
| 714 | + function update($_fields, $merge = true) |
|
| 715 | 715 | { |
| 716 | 716 | if ($merge) $this->data_merge($_fields); |
| 717 | 717 | |
@@ -719,23 +719,23 @@ discard block |
||
| 719 | 719 | |
| 720 | 720 | // extract the keys from $fields or - if not set there - from $this->data |
| 721 | 721 | $keys = array(); |
| 722 | - foreach($this->db_key_cols as $col => $name) |
|
| 722 | + foreach ($this->db_key_cols as $col => $name) |
|
| 723 | 723 | { |
| 724 | 724 | $keys[$col] = isset($fields[$name]) ? $fields[$name] : $this->data[$name]; |
| 725 | 725 | unset($fields[$name]); |
| 726 | 726 | } |
| 727 | 727 | // extract the data from $fields |
| 728 | 728 | $data = array(); |
| 729 | - foreach($this->db_data_cols as $col => $name) |
|
| 729 | + foreach ($this->db_data_cols as $col => $name) |
|
| 730 | 730 | { |
| 731 | - if (array_key_exists($name,$fields)) |
|
| 731 | + if (array_key_exists($name, $fields)) |
|
| 732 | 732 | { |
| 733 | 733 | $data[$col] = $fields[$name]; |
| 734 | 734 | unset($fields[$name]); |
| 735 | 735 | } |
| 736 | 736 | } |
| 737 | 737 | // add direct sql like 'etag=etag+1' (it has integer keys) |
| 738 | - foreach($fields as $key => $value) |
|
| 738 | + foreach ($fields as $key => $value) |
|
| 739 | 739 | { |
| 740 | 740 | if (is_int($key)) |
| 741 | 741 | { |
@@ -744,9 +744,9 @@ discard block |
||
| 744 | 744 | } |
| 745 | 745 | if (!$data) |
| 746 | 746 | { |
| 747 | - return 0; // nothing to update |
|
| 747 | + return 0; // nothing to update |
|
| 748 | 748 | } |
| 749 | - if (!$this->db->update($this->table_name,$data,$keys,__LINE__,__FILE__,$this->app)) |
|
| 749 | + if (!$this->db->update($this->table_name, $data, $keys, __LINE__, __FILE__, $this->app)) |
|
| 750 | 750 | { |
| 751 | 751 | return $this->db->Errno; |
| 752 | 752 | } |
@@ -760,7 +760,7 @@ discard block |
||
| 760 | 760 | * @param boolean $only_return_query =false return $query of delete call to db object, but not run it (used by so_sql_cf!) |
| 761 | 761 | * @return int|array affected rows, should be 1 if ok, 0 if an error or array with id's if $only_return_ids |
| 762 | 762 | */ |
| 763 | - function delete($keys=null,$only_return_query=false) |
|
| 763 | + function delete($keys = null, $only_return_query = false) |
|
| 764 | 764 | { |
| 765 | 765 | if ($this->autoinc_id && $keys && !is_array($keys)) |
| 766 | 766 | { |
@@ -774,7 +774,7 @@ discard block |
||
| 774 | 774 | else // data and keys are supplied in $keys |
| 775 | 775 | { |
| 776 | 776 | $data = $keys; $keys = array(); |
| 777 | - foreach($this->db_cols as $db_col => $col) |
|
| 777 | + foreach ($this->db_cols as $db_col => $col) |
|
| 778 | 778 | { |
| 779 | 779 | if (isset($data[$col])) |
| 780 | 780 | { |
@@ -782,7 +782,7 @@ discard block |
||
| 782 | 782 | } |
| 783 | 783 | } |
| 784 | 784 | // keep sql fragments (with integer key) |
| 785 | - foreach($data as $key => $val) |
|
| 785 | + foreach ($data as $key => $val) |
|
| 786 | 786 | { |
| 787 | 787 | if (is_int($key)) |
| 788 | 788 | { |
@@ -792,7 +792,7 @@ discard block |
||
| 792 | 792 | } |
| 793 | 793 | $data = $this->data2db($data); |
| 794 | 794 | |
| 795 | - foreach($keys as $db_col => $col) |
|
| 795 | + foreach ($keys as $db_col => $col) |
|
| 796 | 796 | { |
| 797 | 797 | if (is_int($db_col)) |
| 798 | 798 | { |
@@ -805,7 +805,7 @@ discard block |
||
| 805 | 805 | } |
| 806 | 806 | if ($only_return_query) return $query; |
| 807 | 807 | |
| 808 | - $this->db->delete($this->table_name,$query,__LINE__,__FILE__,$this->app); |
|
| 808 | + $this->db->delete($this->table_name, $query, __LINE__, __FILE__, $this->app); |
|
| 809 | 809 | |
| 810 | 810 | return $this->db->affected_rows(); |
| 811 | 811 | } |
@@ -833,15 +833,15 @@ discard block |
||
| 833 | 833 | * @todo return an interator instead of an array |
| 834 | 834 | * @return array|NULL array of matching rows (the row is an array of the cols) or NULL |
| 835 | 835 | */ |
| 836 | - function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='',$need_full_no_count=false) |
|
| 836 | + function &search($criteria, $only_keys = True, $order_by = '', $extra_cols = '', $wildcard = '', $empty = False, $op = 'AND', $start = false, $filter = null, $join = '', $need_full_no_count = false) |
|
| 837 | 837 | { |
| 838 | 838 | //error_log(__METHOD__.'('.array2string(array_combine(array_slice(array('criteria','only_keys','order_by','extra_cols','wildcard','empty','op','start','filter','join','need_full_no_count'), 0, count(func_get_args())), func_get_args())).')'); |
| 839 | - if ((int) $this->debug >= 4) echo "<p>so_sql::search(".print_r($criteria,true).",'$only_keys','$order_by',".print_r($extra_cols,true).",'$wildcard','$empty','$op','$start',".print_r($filter,true).",'$join')</p>\n"; |
|
| 839 | + if ((int)$this->debug >= 4) echo "<p>so_sql::search(".print_r($criteria, true).",'$only_keys','$order_by',".print_r($extra_cols, true).",'$wildcard','$empty','$op','$start',".print_r($filter, true).",'$join')</p>\n"; |
|
| 840 | 840 | |
| 841 | 841 | // if extending class or instanciator set columns to search, convert string criteria to array |
| 842 | 842 | if ($criteria && !is_array($criteria)) |
| 843 | 843 | { |
| 844 | - $search = $this->search2criteria($criteria,$wildcard,$op); |
|
| 844 | + $search = $this->search2criteria($criteria, $wildcard, $op); |
|
| 845 | 845 | $criteria = array($search); |
| 846 | 846 | } |
| 847 | 847 | if (!is_array($criteria)) |
@@ -858,19 +858,19 @@ discard block |
||
| 858 | 858 | $data2db_filter = $this->data2db($filter); |
| 859 | 859 | if (!is_array($data2db_filter)) { |
| 860 | 860 | echo function_backtrace()."<br/>\n"; |
| 861 | - echo "filter=";_debug_array($filter); |
|
| 862 | - echo "data2db(filter)=";_debug_array($data2db_filter); |
|
| 861 | + echo "filter="; _debug_array($filter); |
|
| 862 | + echo "data2db(filter)="; _debug_array($data2db_filter); |
|
| 863 | 863 | } |
| 864 | - foreach($data2db_filter as $col => $val) |
|
| 864 | + foreach ($data2db_filter as $col => $val) |
|
| 865 | 865 | { |
| 866 | 866 | if ($val !== '') |
| 867 | 867 | { |
| 868 | 868 | // check if a db-internal name conversation necessary |
| 869 | - if (!is_int($col) && ($c = array_search($col,$this->db_cols))) |
|
| 869 | + if (!is_int($col) && ($c = array_search($col, $this->db_cols))) |
|
| 870 | 870 | { |
| 871 | - $col = $this->table_name . '.' . $c; |
|
| 871 | + $col = $this->table_name.'.'.$c; |
|
| 872 | 872 | } |
| 873 | - if(is_int($col)) |
|
| 873 | + if (is_int($col)) |
|
| 874 | 874 | { |
| 875 | 875 | $db_filter[] = $val; |
| 876 | 876 | } |
@@ -888,35 +888,35 @@ discard block |
||
| 888 | 888 | { |
| 889 | 889 | if ($op != 'AND') |
| 890 | 890 | { |
| 891 | - $db_filter[] = '('.$this->db->column_data_implode(' '.$op.' ',$query).')'; |
|
| 891 | + $db_filter[] = '('.$this->db->column_data_implode(' '.$op.' ', $query).')'; |
|
| 892 | 892 | } |
| 893 | 893 | else |
| 894 | 894 | { |
| 895 | - $db_filter = array_merge($db_filter,$query); |
|
| 895 | + $db_filter = array_merge($db_filter, $query); |
|
| 896 | 896 | } |
| 897 | 897 | } |
| 898 | 898 | $query = $db_filter; |
| 899 | 899 | } |
| 900 | - if ((int) $this->debug >= 4) |
|
| 900 | + if ((int)$this->debug >= 4) |
|
| 901 | 901 | { |
| 902 | - echo "<p>so_sql::search(,only_keys=$only_keys,order_by='$order_by',wildcard='$wildcard',empty=$empty,$op,start='$start',".print_r($filter,true).") query=".print_r($query,true).", total='$this->total'</p>\n"; |
|
| 902 | + echo "<p>so_sql::search(,only_keys=$only_keys,order_by='$order_by',wildcard='$wildcard',empty=$empty,$op,start='$start',".print_r($filter, true).") query=".print_r($query, true).", total='$this->total'</p>\n"; |
|
| 903 | 903 | echo "<br>criteria = "; _debug_array($criteria); |
| 904 | 904 | } |
| 905 | 905 | if ($only_keys === true) |
| 906 | 906 | { |
| 907 | 907 | $colums = array_keys($this->db_key_cols); |
| 908 | - foreach($colums as &$column) |
|
| 908 | + foreach ($colums as &$column) |
|
| 909 | 909 | { |
| 910 | - $column = $this->table_name . '.' . $column; |
|
| 910 | + $column = $this->table_name.'.'.$column; |
|
| 911 | 911 | } |
| 912 | 912 | } |
| 913 | 913 | elseif (is_array($only_keys)) |
| 914 | 914 | { |
| 915 | 915 | $colums = array(); |
| 916 | - foreach($only_keys as $key => $col) |
|
| 916 | + foreach ($only_keys as $key => $col) |
|
| 917 | 917 | { |
| 918 | 918 | //Convert ambiguous columns to prefixed tablename.column name |
| 919 | - $colums[] = ($db_col = array_search($col,$this->db_cols)) ? $this->table_name .'.'.$db_col.' AS '.$col :$col; |
|
| 919 | + $colums[] = ($db_col = array_search($col, $this->db_cols)) ? $this->table_name.'.'.$db_col.' AS '.$col : $col; |
|
| 920 | 920 | } |
| 921 | 921 | } |
| 922 | 922 | elseif (!$only_keys) |
@@ -946,20 +946,20 @@ discard block |
||
| 946 | 946 | { |
| 947 | 947 | $colums[$key] = $this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id; |
| 948 | 948 | } |
| 949 | - elseif (!is_array($colums) && strpos($colums,$this->autoinc_id) !== false) |
|
| 949 | + elseif (!is_array($colums) && strpos($colums, $this->autoinc_id) !== false) |
|
| 950 | 950 | { |
| 951 | - $colums = preg_replace('/(?<! AS)([ ,]+)'.preg_quote($this->autoinc_id).'([ ,]+)/','\\1'.$this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id.'\\2',$colums); |
|
| 951 | + $colums = preg_replace('/(?<! AS)([ ,]+)'.preg_quote($this->autoinc_id).'([ ,]+)/', '\\1'.$this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id.'\\2', $colums); |
|
| 952 | 952 | } |
| 953 | 953 | } |
| 954 | - $num_rows = 0; // as spec. in max_matches in the user-prefs |
|
| 955 | - if (is_array($start)) list($start,$num_rows) = $start; |
|
| 954 | + $num_rows = 0; // as spec. in max_matches in the user-prefs |
|
| 955 | + if (is_array($start)) list($start, $num_rows) = $start; |
|
| 956 | 956 | |
| 957 | 957 | // fix GROUP BY clause to contain all non-aggregate selected columns |
| 958 | - if ($order_by && stripos($order_by,'GROUP BY') !== false) |
|
| 958 | + if ($order_by && stripos($order_by, 'GROUP BY') !== false) |
|
| 959 | 959 | { |
| 960 | 960 | $order_by = $this->fix_group_by_columns($order_by, $colums, $this->table_name, $this->autoinc_id); |
| 961 | 961 | } |
| 962 | - elseif ($order_by && stripos($order_by,'ORDER BY')===false && stripos($order_by,'GROUP BY')===false && stripos($order_by,'HAVING')===false) |
|
| 962 | + elseif ($order_by && stripos($order_by, 'ORDER BY') === false && stripos($order_by, 'GROUP BY') === false && stripos($order_by, 'HAVING') === false) |
|
| 963 | 963 | { |
| 964 | 964 | $order_by = 'ORDER BY '.$order_by; |
| 965 | 965 | } |
@@ -982,9 +982,9 @@ discard block |
||
| 982 | 982 | ); |
| 983 | 983 | if (!$union_cols) // union used the colum-names of the first query |
| 984 | 984 | { |
| 985 | - $union_cols = $this->_get_columns($only_keys,$extra_cols); |
|
| 985 | + $union_cols = $this->_get_columns($only_keys, $extra_cols); |
|
| 986 | 986 | } |
| 987 | - return true; // waiting for further calls, before running the union-query |
|
| 987 | + return true; // waiting for further calls, before running the union-query |
|
| 988 | 988 | } |
| 989 | 989 | // running the union query now |
| 990 | 990 | if ($start !== false) // need to get the total too, saved in $this->total |
@@ -995,10 +995,10 @@ discard block |
||
| 995 | 995 | } |
| 996 | 996 | else // cant do a count, have to run the query without limit |
| 997 | 997 | { |
| 998 | - $this->total = $this->db->union($union,__LINE__,__FILE__)->NumRows(); |
|
| 998 | + $this->total = $this->db->union($union, __LINE__, __FILE__)->NumRows(); |
|
| 999 | 999 | } |
| 1000 | 1000 | } |
| 1001 | - $rs = $this->db->union($union,__LINE__,__FILE__,$order_by,$start,$num_rows); |
|
| 1001 | + $rs = $this->db->union($union, __LINE__, __FILE__, $order_by, $start, $num_rows); |
|
| 1002 | 1002 | if ($this->debug) error_log(__METHOD__."() ".$this->db->Query_ID->sql); |
| 1003 | 1003 | |
| 1004 | 1004 | $cols = $union_cols; |
@@ -1012,21 +1012,21 @@ discard block |
||
| 1012 | 1012 | { |
| 1013 | 1013 | $mysql_calc_rows = 'SQL_CALC_FOUND_ROWS '; |
| 1014 | 1014 | } |
| 1015 | - elseif (!$need_full_no_count && (!$join || stripos($join,'LEFT JOIN')!==false)) |
|
| 1015 | + elseif (!$need_full_no_count && (!$join || stripos($join, 'LEFT JOIN') !== false)) |
|
| 1016 | 1016 | { |
| 1017 | - $this->total = $this->db->select($this->table_name,'COUNT(*)',$query,__LINE__,__FILE__,false,'',$this->app,0,$join)->fetchColumn(); |
|
| 1017 | + $this->total = $this->db->select($this->table_name, 'COUNT(*)', $query, __LINE__, __FILE__, false, '', $this->app, 0, $join)->fetchColumn(); |
|
| 1018 | 1018 | } |
| 1019 | 1019 | else // cant do a count, have to run the query without limit |
| 1020 | 1020 | { |
| 1021 | - $this->total = $this->db->select($this->table_name,$colums,$query,__LINE__,__FILE__,false,$order_by,false,0,$join)->NumRows(); |
|
| 1021 | + $this->total = $this->db->select($this->table_name, $colums, $query, __LINE__, __FILE__, false, $order_by, false, 0, $join)->NumRows(); |
|
| 1022 | 1022 | } |
| 1023 | 1023 | } |
| 1024 | - $rs = $this->db->select($this->table_name,$mysql_calc_rows.$colums,$query,__LINE__,__FILE__, |
|
| 1025 | - $start,$order_by,$this->app,$num_rows,$join); |
|
| 1024 | + $rs = $this->db->select($this->table_name, $mysql_calc_rows.$colums, $query, __LINE__, __FILE__, |
|
| 1025 | + $start, $order_by, $this->app, $num_rows, $join); |
|
| 1026 | 1026 | if ($this->debug) error_log(__METHOD__."() ".$this->db->Query_ID->sql); |
| 1027 | - $cols = $this->_get_columns($only_keys,$extra_cols); |
|
| 1027 | + $cols = $this->_get_columns($only_keys, $extra_cols); |
|
| 1028 | 1028 | } |
| 1029 | - if ((int) $this->debug >= 4) echo "<p>sql='{$this->db->Query_ID->sql}'</p>\n"; |
|
| 1029 | + if ((int)$this->debug >= 4) echo "<p>sql='{$this->db->Query_ID->sql}'</p>\n"; |
|
| 1030 | 1030 | |
| 1031 | 1031 | if ($mysql_calc_rows) |
| 1032 | 1032 | { |
@@ -1035,14 +1035,14 @@ discard block |
||
| 1035 | 1035 | // ToDo: Implement that as an iterator, as $rs is also an interator and we could return one instead of an array |
| 1036 | 1036 | if ($this->search_return_iterator) |
| 1037 | 1037 | { |
| 1038 | - return new Db2DataIterator($this,$rs); |
|
| 1038 | + return new Db2DataIterator($this, $rs); |
|
| 1039 | 1039 | } |
| 1040 | 1040 | $arr = array(); |
| 1041 | 1041 | $n = 0; |
| 1042 | - if ($rs) foreach($rs as $row) |
|
| 1042 | + if ($rs) foreach ($rs as $row) |
|
| 1043 | 1043 | { |
| 1044 | 1044 | $data = array(); |
| 1045 | - foreach($cols as $db_col => $col) |
|
| 1045 | + foreach ($cols as $db_col => $col) |
|
| 1046 | 1046 | { |
| 1047 | 1047 | $data[$col] = (isset($row[$db_col]) ? $row[$db_col] : $row[$col]); |
| 1048 | 1048 | } |
@@ -1066,7 +1066,7 @@ discard block |
||
| 1066 | 1066 | protected function parse_search(Array $_criteria, $wildcard, $empty, $op) |
| 1067 | 1067 | { |
| 1068 | 1068 | $criteria = $this->data2db($_criteria); |
| 1069 | - foreach($criteria as $col => $val) |
|
| 1069 | + foreach ($criteria as $col => $val) |
|
| 1070 | 1070 | { |
| 1071 | 1071 | if (is_int($col)) |
| 1072 | 1072 | { |
@@ -1074,7 +1074,7 @@ discard block |
||
| 1074 | 1074 | } |
| 1075 | 1075 | elseif ($empty || $val != '') |
| 1076 | 1076 | { |
| 1077 | - if (!($db_col = array_search($col,$this->db_cols))) |
|
| 1077 | + if (!($db_col = array_search($col, $this->db_cols))) |
|
| 1078 | 1078 | { |
| 1079 | 1079 | $db_col = $col; |
| 1080 | 1080 | } |
@@ -1085,7 +1085,7 @@ discard block |
||
| 1085 | 1085 | $this->table_def['fd'][$db_col]['nullable'] !== false) |
| 1086 | 1086 | { |
| 1087 | 1087 | unset($criteria[$col]); |
| 1088 | - $query[] = '(' . $db_col . ' IS NULL OR ' . $db_col . " = '')"; |
|
| 1088 | + $query[] = '('.$db_col.' IS NULL OR '.$db_col." = '')"; |
|
| 1089 | 1089 | } |
| 1090 | 1090 | else |
| 1091 | 1091 | { |
@@ -1093,10 +1093,10 @@ discard block |
||
| 1093 | 1093 | } |
| 1094 | 1094 | } |
| 1095 | 1095 | elseif ($wildcard || $criteria[$col][0] == '!' || |
| 1096 | - is_string($criteria[$col]) && (strpos($criteria[$col],'*')!==false || strpos($criteria[$col],'?')!==false)) |
|
| 1096 | + is_string($criteria[$col]) && (strpos($criteria[$col], '*') !== false || strpos($criteria[$col], '?') !== false)) |
|
| 1097 | 1097 | { |
| 1098 | 1098 | // if search pattern alread contains a wildcard, do NOT add further ones automatic |
| 1099 | - if (is_string($criteria[$col]) && (strpos($criteria[$col],'*')!==false || strpos($criteria[$col],'?')!==false)) |
|
| 1099 | + if (is_string($criteria[$col]) && (strpos($criteria[$col], '*') !== false || strpos($criteria[$col], '?') !== false)) |
|
| 1100 | 1100 | { |
| 1101 | 1101 | $wildcard = ''; |
| 1102 | 1102 | } |
@@ -1105,19 +1105,19 @@ discard block |
||
| 1105 | 1105 | if ($criteria[$col][0] == '!') |
| 1106 | 1106 | { |
| 1107 | 1107 | $cmp_op = ' NOT'.$cmp_op; |
| 1108 | - $criteria[$col] = substr($criteria[$col],1); |
|
| 1108 | + $criteria[$col] = substr($criteria[$col], 1); |
|
| 1109 | 1109 | $negate = true; |
| 1110 | 1110 | } |
| 1111 | - foreach(explode(' ',$criteria[$col]) as $crit) |
|
| 1111 | + foreach (explode(' ', $criteria[$col]) as $crit) |
|
| 1112 | 1112 | { |
| 1113 | 1113 | $query[] = ($negate ? ' ('.$db_col.' IS NULL OR ' : '').$db_col.$cmp_op. |
| 1114 | - $this->db->quote($wildcard.str_replace(array('%','_','*','?'),array('\\%','\\_','%','_'),$crit).$wildcard). |
|
| 1114 | + $this->db->quote($wildcard.str_replace(array('%', '_', '*', '?'), array('\\%', '\\_', '%', '_'), $crit).$wildcard). |
|
| 1115 | 1115 | ($negate ? ') ' : ''); |
| 1116 | 1116 | } |
| 1117 | 1117 | } |
| 1118 | - elseif (strpos($db_col,'.') !== false) // we have a table-name specified |
|
| 1118 | + elseif (strpos($db_col, '.') !== false) // we have a table-name specified |
|
| 1119 | 1119 | { |
| 1120 | - list($table,$only_col) = explode('.',$db_col); |
|
| 1120 | + list($table, $only_col) = explode('.', $db_col); |
|
| 1121 | 1121 | $type = $this->db->get_column_attribute($only_col, $table, true, 'type'); |
| 1122 | 1122 | if (empty($type)) |
| 1123 | 1123 | { |
@@ -1125,15 +1125,15 @@ discard block |
||
| 1125 | 1125 | } |
| 1126 | 1126 | if (is_array($val) && count($val) > 1) |
| 1127 | 1127 | { |
| 1128 | - foreach($val as &$v) |
|
| 1128 | + foreach ($val as &$v) |
|
| 1129 | 1129 | { |
| 1130 | 1130 | $v = $this->db->quote($v, $type); |
| 1131 | 1131 | } |
| 1132 | - $query[] = $sql = $db_col.' IN (' .implode(',',$val).')'; |
|
| 1132 | + $query[] = $sql = $db_col.' IN ('.implode(',', $val).')'; |
|
| 1133 | 1133 | } |
| 1134 | 1134 | else |
| 1135 | 1135 | { |
| 1136 | - $query[] = $db_col.'='.$this->db->quote(is_array($val)?array_shift($val):$val,$type); |
|
| 1136 | + $query[] = $db_col.'='.$this->db->quote(is_array($val) ?array_shift($val) : $val, $type); |
|
| 1137 | 1137 | } |
| 1138 | 1138 | } |
| 1139 | 1139 | else |
@@ -1142,7 +1142,7 @@ discard block |
||
| 1142 | 1142 | } |
| 1143 | 1143 | } |
| 1144 | 1144 | } |
| 1145 | - if (is_array($query) && $op != 'AND') $query = $this->db->column_data_implode(' '.$op.' ',$query); |
|
| 1145 | + if (is_array($query) && $op != 'AND') $query = $this->db->column_data_implode(' '.$op.' ', $query); |
|
| 1146 | 1146 | return $query; |
| 1147 | 1147 | } |
| 1148 | 1148 | |
@@ -1166,7 +1166,7 @@ discard block |
||
| 1166 | 1166 | $matches = null; |
| 1167 | 1167 | if (substr($GLOBALS['egw']->db->Type, 0, 5) == 'mysql' || !preg_match('/(GROUP BY .*)(HAVING.*|ORDER BY.*)?$/iU', $group_by, $matches)) |
| 1168 | 1168 | { |
| 1169 | - return $group_by; // nothing to do |
|
| 1169 | + return $group_by; // nothing to do |
|
| 1170 | 1170 | } |
| 1171 | 1171 | $changes = 0; |
| 1172 | 1172 | $group_by_cols = preg_split('/, */', trim(substr($matches[1], 9))); |
@@ -1176,9 +1176,9 @@ discard block |
||
| 1176 | 1176 | $columns = preg_split('/, */', $columns); |
| 1177 | 1177 | |
| 1178 | 1178 | // fix columns containing commas as part of function calls |
| 1179 | - for($n = 0; $n < count($columns); ++$n) |
|
| 1179 | + for ($n = 0; $n < count($columns); ++$n) |
|
| 1180 | 1180 | { |
| 1181 | - $col =& $columns[$n]; |
|
| 1181 | + $col = & $columns[$n]; |
|
| 1182 | 1182 | while (substr_count($col, '(') > substr_count($col, ')') && ++$n < count($columns)) |
| 1183 | 1183 | { |
| 1184 | 1184 | $col .= ','.$columns[$n]; |
@@ -1187,7 +1187,7 @@ discard block |
||
| 1187 | 1187 | } |
| 1188 | 1188 | unset($col); |
| 1189 | 1189 | } |
| 1190 | - foreach($columns as $n => $col) |
|
| 1190 | + foreach ($columns as $n => $col) |
|
| 1191 | 1191 | { |
| 1192 | 1192 | if ($col == '*') |
| 1193 | 1193 | { |
@@ -1200,7 +1200,7 @@ discard block |
||
| 1200 | 1200 | { |
| 1201 | 1201 | if (($pos = stripos($col, 'DISTINCT ')) !== false) |
| 1202 | 1202 | { |
| 1203 | - $col = substr($col, $pos+9); |
|
| 1203 | + $col = substr($col, $pos + 9); |
|
| 1204 | 1204 | } |
| 1205 | 1205 | $alias = $col; |
| 1206 | 1206 | if (stripos($col, ' AS ')) list($col, $alias) = preg_split('/ +AS +/i', $col); |
@@ -1218,7 +1218,7 @@ discard block |
||
| 1218 | 1218 | $ret = $group_by; |
| 1219 | 1219 | if ($changes) |
| 1220 | 1220 | { |
| 1221 | - $ret = str_replace($matches[1], 'GROUP BY '.implode(',', $group_by_cols).' ', $group_by); |
|
| 1221 | + $ret = str_replace($matches[1], 'GROUP BY '.implode(',', $group_by_cols).' ', $group_by); |
|
| 1222 | 1222 | //error_log(__METHOD__."('$group_by', ".array2string($columns).") group_by_cols=".array2string($group_by_cols)." changed to $ret"); |
| 1223 | 1223 | } |
| 1224 | 1224 | return $ret; |
@@ -1238,12 +1238,12 @@ discard block |
||
| 1238 | 1238 | * @param array $search_cols =array() List of columns to search. If not provided, all columns in $this->db_cols will be considered |
| 1239 | 1239 | * @return array or column => value pairs |
| 1240 | 1240 | */ |
| 1241 | - public function search2criteria($_pattern,&$wildcard='',&$op='AND',$extra_col=null, $search_cols = array()) |
|
| 1241 | + public function search2criteria($_pattern, &$wildcard = '', &$op = 'AND', $extra_col = null, $search_cols = array()) |
|
| 1242 | 1242 | { |
| 1243 | 1243 | $pattern = trim($_pattern); |
| 1244 | 1244 | // This function can get called multiple times. Make sure it doesn't re-process. |
| 1245 | 1245 | if (empty($pattern) || is_array($pattern)) return $pattern; |
| 1246 | - if(strpos($pattern, 'CAST(COALESCE(') !== false) |
|
| 1246 | + if (strpos($pattern, 'CAST(COALESCE(') !== false) |
|
| 1247 | 1247 | { |
| 1248 | 1248 | return $pattern; |
| 1249 | 1249 | } |
@@ -1260,24 +1260,24 @@ discard block |
||
| 1260 | 1260 | $numeric_columns = array(); |
| 1261 | 1261 | |
| 1262 | 1262 | // Special handling for an ID search, #<int> |
| 1263 | - if(strpos($_pattern, '#') === 0 && is_numeric(substr($_pattern, 1))) |
|
| 1263 | + if (strpos($_pattern, '#') === 0 && is_numeric(substr($_pattern, 1))) |
|
| 1264 | 1264 | { |
| 1265 | - return array('(' . $this->table_name.'.'. $this->autoinc_id . '=' . (int)substr($_pattern,1) . ')'); |
|
| 1265 | + return array('('.$this->table_name.'.'.$this->autoinc_id.'='.(int)substr($_pattern, 1).')'); |
|
| 1266 | 1266 | } |
| 1267 | - if(!$search_cols) |
|
| 1267 | + if (!$search_cols) |
|
| 1268 | 1268 | { |
| 1269 | 1269 | $search_cols = $this->get_default_search_columns(); |
| 1270 | 1270 | } |
| 1271 | 1271 | // Concat all fields to be searched together, so the conditions operate across the whole record |
| 1272 | - foreach($search_cols as $col) |
|
| 1272 | + foreach ($search_cols as $col) |
|
| 1273 | 1273 | { |
| 1274 | 1274 | $col_name = $col; |
| 1275 | 1275 | $table = $this->table_name; |
| 1276 | - if (strpos($col,'.') !== false) |
|
| 1276 | + if (strpos($col, '.') !== false) |
|
| 1277 | 1277 | { |
| 1278 | - list($table,$col_name) = explode('.',$col); |
|
| 1278 | + list($table, $col_name) = explode('.', $col); |
|
| 1279 | 1279 | } |
| 1280 | - $table_def = $table == $this->table_name ? $this->table_def : $this->db->get_table_definitions(true,$table); |
|
| 1280 | + $table_def = $table == $this->table_name ? $this->table_def : $this->db->get_table_definitions(true, $table); |
|
| 1281 | 1281 | if ($table_def['fd'][$col_name] && in_array($table_def['fd'][$col_name]['type'], $numeric_types)) |
| 1282 | 1282 | { |
| 1283 | 1283 | $numeric_columns[] = $col; |
@@ -1285,11 +1285,11 @@ discard block |
||
| 1285 | 1285 | } |
| 1286 | 1286 | if ($this->db->Type == 'mysql' && $table_def['fd'][$col_name]['type'] === 'ascii' && preg_match('/[\x80-\xFF]/', $_pattern)) |
| 1287 | 1287 | { |
| 1288 | - continue; // will only give sql error |
|
| 1288 | + continue; // will only give sql error |
|
| 1289 | 1289 | } |
| 1290 | - $columns[] = sprintf($this->db->capabilities[Api\Db::CAPABILITY_CAST_AS_VARCHAR],"COALESCE($col,'')"); |
|
| 1290 | + $columns[] = sprintf($this->db->capabilities[Api\Db::CAPABILITY_CAST_AS_VARCHAR], "COALESCE($col,'')"); |
|
| 1291 | 1291 | } |
| 1292 | - if(!$columns) |
|
| 1292 | + if (!$columns) |
|
| 1293 | 1293 | { |
| 1294 | 1294 | return array(); |
| 1295 | 1295 | } |
@@ -1298,9 +1298,9 @@ discard block |
||
| 1298 | 1298 | $break = ' '; |
| 1299 | 1299 | $token = strtok($pattern, $break); |
| 1300 | 1300 | |
| 1301 | - while($token) |
|
| 1301 | + while ($token) |
|
| 1302 | 1302 | { |
| 1303 | - if($token == strtoupper(lang('AND')) || $token == 'AND') |
|
| 1303 | + if ($token == strtoupper(lang('AND')) || $token == 'AND') |
|
| 1304 | 1304 | { |
| 1305 | 1305 | $token = '+'.strtok($break); |
| 1306 | 1306 | } |
@@ -1313,10 +1313,10 @@ discard block |
||
| 1313 | 1313 | { |
| 1314 | 1314 | $token = '-'.strtok($break); |
| 1315 | 1315 | } |
| 1316 | - if ($token[0]=='"') |
|
| 1316 | + if ($token[0] == '"') |
|
| 1317 | 1317 | { |
| 1318 | - $token = substr($token, 1,strlen($token)); |
|
| 1319 | - if(substr($token, -1) != '"') |
|
| 1318 | + $token = substr($token, 1, strlen($token)); |
|
| 1319 | + if (substr($token, -1) != '"') |
|
| 1320 | 1320 | { |
| 1321 | 1321 | $token .= ' '.strtok('"'); |
| 1322 | 1322 | } |
@@ -1327,16 +1327,16 @@ discard block |
||
| 1327 | 1327 | } |
| 1328 | 1328 | |
| 1329 | 1329 | // prepend and append extra wildcard %, if pattern does NOT already contain wildcards |
| 1330 | - if (strpos($token,'*') === false && strpos($token,'?') === false) |
|
| 1330 | + if (strpos($token, '*') === false && strpos($token, '?') === false) |
|
| 1331 | 1331 | { |
| 1332 | - $wildcard = '%'; // if pattern contains no wildcards, add them before AND after the pattern |
|
| 1332 | + $wildcard = '%'; // if pattern contains no wildcards, add them before AND after the pattern |
|
| 1333 | 1333 | } |
| 1334 | 1334 | else |
| 1335 | 1335 | { |
| 1336 | - $wildcard = ''; // no extra wildcard, if pattern already contains some |
|
| 1336 | + $wildcard = ''; // no extra wildcard, if pattern already contains some |
|
| 1337 | 1337 | } |
| 1338 | 1338 | |
| 1339 | - switch($token[0]) |
|
| 1339 | + switch ($token[0]) |
|
| 1340 | 1340 | { |
| 1341 | 1341 | case '+': |
| 1342 | 1342 | $op = 'AND'; |
@@ -1351,9 +1351,9 @@ discard block |
||
| 1351 | 1351 | $op = 'OR'; |
| 1352 | 1352 | break; |
| 1353 | 1353 | } |
| 1354 | - $search_token = $wildcard.str_replace(array('%','_','*','?'),array('\\%','\\_','%','_'),$token).$wildcard; |
|
| 1355 | - $token_filter = ' '.call_user_func_array(array($GLOBALS['egw']->db,'concat'),$columns).' '. |
|
| 1356 | - $this->db->capabilities['case_insensitive_like'] . ' ' . |
|
| 1354 | + $search_token = $wildcard.str_replace(array('%', '_', '*', '?'), array('\\%', '\\_', '%', '_'), $token).$wildcard; |
|
| 1355 | + $token_filter = ' '.call_user_func_array(array($GLOBALS['egw']->db, 'concat'), $columns).' '. |
|
| 1356 | + $this->db->capabilities['case_insensitive_like'].' '. |
|
| 1357 | 1357 | $GLOBALS['egw']->db->quote($search_token); |
| 1358 | 1358 | |
| 1359 | 1359 | // if we have customfields and this is Api\Storage (not Api\Storage\Base) |
@@ -1365,26 +1365,26 @@ discard block |
||
| 1365 | 1365 | |
| 1366 | 1366 | // Compare numeric token as equality for numeric columns |
| 1367 | 1367 | // skip user-wildcards (*,?) in is_numeric test, but not SQL wildcards, which get escaped and give sql-error |
| 1368 | - if (is_numeric(str_replace(array('*','?'), '', $token))) |
|
| 1368 | + if (is_numeric(str_replace(array('*', '?'), '', $token))) |
|
| 1369 | 1369 | { |
| 1370 | 1370 | $numeric_filter = array(); |
| 1371 | - foreach($numeric_columns as $col) |
|
| 1371 | + foreach ($numeric_columns as $col) |
|
| 1372 | 1372 | { |
| 1373 | - if($wildcard == '') |
|
| 1373 | + if ($wildcard == '') |
|
| 1374 | 1374 | { |
| 1375 | 1375 | // Token has a wildcard from user, use LIKE |
| 1376 | - $numeric_filter[] = "($col IS NOT NULL AND CAST($col AS CHAR) " . |
|
| 1377 | - $this->db->capabilities['case_insensitive_like'] . ' ' . |
|
| 1378 | - $GLOBALS['egw']->db->quote(str_replace(array('*','?'), array('%','_'), $token)) . ')'; |
|
| 1376 | + $numeric_filter[] = "($col IS NOT NULL AND CAST($col AS CHAR) ". |
|
| 1377 | + $this->db->capabilities['case_insensitive_like'].' '. |
|
| 1378 | + $GLOBALS['egw']->db->quote(str_replace(array('*', '?'), array('%', '_'), $token)).')'; |
|
| 1379 | 1379 | } |
| 1380 | 1380 | else |
| 1381 | 1381 | { |
| 1382 | 1382 | $numeric_filter[] = "($col IS NOT NULL AND $col = $token)"; |
| 1383 | 1383 | } |
| 1384 | 1384 | } |
| 1385 | - if(count($numeric_filter) > 0) |
|
| 1385 | + if (count($numeric_filter) > 0) |
|
| 1386 | 1386 | { |
| 1387 | - $token_filter .= ' OR ' . implode(' OR ', $numeric_filter); |
|
| 1387 | + $token_filter .= ' OR '.implode(' OR ', $numeric_filter); |
|
| 1388 | 1388 | } |
| 1389 | 1389 | } |
| 1390 | 1390 | $criteria[$op][] = '('.$token_filter.')'; |
@@ -1392,32 +1392,32 @@ discard block |
||
| 1392 | 1392 | $token = strtok($break); |
| 1393 | 1393 | } |
| 1394 | 1394 | |
| 1395 | - if($criteria['NOT']) |
|
| 1395 | + if ($criteria['NOT']) |
|
| 1396 | 1396 | { |
| 1397 | - $filter[] = 'NOT (' . implode(' OR ', $criteria['NOT']) . ') '; |
|
| 1397 | + $filter[] = 'NOT ('.implode(' OR ', $criteria['NOT']).') '; |
|
| 1398 | 1398 | } |
| 1399 | - if($criteria['AND']) |
|
| 1399 | + if ($criteria['AND']) |
|
| 1400 | 1400 | { |
| 1401 | - $filter[] = implode(' AND ', $criteria['AND']) . ' '; |
|
| 1401 | + $filter[] = implode(' AND ', $criteria['AND']).' '; |
|
| 1402 | 1402 | } |
| 1403 | - if($criteria['OR']) |
|
| 1403 | + if ($criteria['OR']) |
|
| 1404 | 1404 | { |
| 1405 | - $filter[] = '(' . implode(' OR ', $criteria['OR']) . ') '; |
|
| 1405 | + $filter[] = '('.implode(' OR ', $criteria['OR']).') '; |
|
| 1406 | 1406 | } |
| 1407 | 1407 | |
| 1408 | - if(count($filter)) |
|
| 1408 | + if (count($filter)) |
|
| 1409 | 1409 | { |
| 1410 | - $result = '(' . implode(' AND ', $filter) . ')'; |
|
| 1410 | + $result = '('.implode(' AND ', $filter).')'; |
|
| 1411 | 1411 | } |
| 1412 | 1412 | |
| 1413 | 1413 | // OR extra column on the end so a null or blank won't block a hit in the main columns |
| 1414 | 1414 | if ($extra_col) |
| 1415 | 1415 | { |
| 1416 | - $result .= (strlen($result) ? ' OR ' : ' ') . "$extra_col = " . $GLOBALS['egw']->db->quote($pattern); |
|
| 1416 | + $result .= (strlen($result) ? ' OR ' : ' ')."$extra_col = ".$GLOBALS['egw']->db->quote($pattern); |
|
| 1417 | 1417 | } |
| 1418 | 1418 | |
| 1419 | 1419 | $op = 'OR'; |
| 1420 | - return array('(' . $result . ')'); |
|
| 1420 | + return array('('.$result.')'); |
|
| 1421 | 1421 | } |
| 1422 | 1422 | |
| 1423 | 1423 | /** |
@@ -1436,21 +1436,21 @@ discard block |
||
| 1436 | 1436 | $numeric_types = array('auto', 'int', 'float', 'double'); |
| 1437 | 1437 | |
| 1438 | 1438 | // Skip some numeric columns that don't make sense to search if we have to default to all columns |
| 1439 | - if(is_null($this->columns_to_search)) |
|
| 1439 | + if (is_null($this->columns_to_search)) |
|
| 1440 | 1440 | { |
| 1441 | - foreach($search_cols as $key => &$col) |
|
| 1441 | + foreach ($search_cols as $key => &$col) |
|
| 1442 | 1442 | { |
| 1443 | 1443 | // If the name as given isn't a real column name, and adding the prefix doesn't help, skip it |
| 1444 | - if(!$this->table_def['fd'][$col] && !($col = $this->prefix.array_search($col, $search_cols))) { |
|
| 1444 | + if (!$this->table_def['fd'][$col] && !($col = $this->prefix.array_search($col, $search_cols))) { |
|
| 1445 | 1445 | // Can't search this column |
| 1446 | 1446 | unset($search_cols[$key]); |
| 1447 | 1447 | continue; |
| 1448 | 1448 | } |
| 1449 | - if(in_array($this->table_def['fd'][$col]['type'], $numeric_types)) |
|
| 1449 | + if (in_array($this->table_def['fd'][$col]['type'], $numeric_types)) |
|
| 1450 | 1450 | { |
| 1451 | - foreach($skip_columns_with as $bad) |
|
| 1451 | + foreach ($skip_columns_with as $bad) |
|
| 1452 | 1452 | { |
| 1453 | - if(strpos($col, $bad) !== false) |
|
| 1453 | + if (strpos($col, $bad) !== false) |
|
| 1454 | 1454 | { |
| 1455 | 1455 | unset($search_cols[$key]); |
| 1456 | 1456 | continue 2; |
@@ -1472,7 +1472,7 @@ discard block |
||
| 1472 | 1472 | * @param string|array $extra_cols ='' string or array of strings to be added to the SELECT, eg. "count(*) as num" |
| 1473 | 1473 | * @return array with columns as db-name => internal-name pairs |
| 1474 | 1474 | */ |
| 1475 | - function _get_columns($only_keys,$extra_cols) |
|
| 1475 | + function _get_columns($only_keys, $extra_cols) |
|
| 1476 | 1476 | { |
| 1477 | 1477 | //echo "_get_columns() only_keys="; _debug_array($only_keys); echo "extra_cols="; _debug_array($extra_cols); |
| 1478 | 1478 | if ($only_keys === true) // only primary key |
@@ -1483,7 +1483,7 @@ discard block |
||
| 1483 | 1483 | { |
| 1484 | 1484 | $cols = array(); |
| 1485 | 1485 | $distinct_checked = false; |
| 1486 | - foreach(is_array($only_keys) ? $only_keys : explode(',', $only_keys) as $col) |
|
| 1486 | + foreach (is_array($only_keys) ? $only_keys : explode(',', $only_keys) as $col) |
|
| 1487 | 1487 | { |
| 1488 | 1488 | if (!$distinct_checked) |
| 1489 | 1489 | { |
@@ -1492,17 +1492,17 @@ discard block |
||
| 1492 | 1492 | } |
| 1493 | 1493 | if (!$col || $col == '*' || $col == $this->table_name.'.*') // all columns |
| 1494 | 1494 | { |
| 1495 | - $cols = array_merge($cols,$this->db_cols); |
|
| 1495 | + $cols = array_merge($cols, $this->db_cols); |
|
| 1496 | 1496 | } |
| 1497 | 1497 | else // only the specified columns |
| 1498 | 1498 | { |
| 1499 | - if (stripos($col,'as')) // if there's already an explicit naming of the column, just use it |
|
| 1499 | + if (stripos($col, 'as')) // if there's already an explicit naming of the column, just use it |
|
| 1500 | 1500 | { |
| 1501 | - $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col); |
|
| 1501 | + $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i', '\\1', $col); |
|
| 1502 | 1502 | $cols[$col] = $col; |
| 1503 | 1503 | continue; |
| 1504 | 1504 | } |
| 1505 | - if (($db_col = array_search($col,$this->db_cols)) !== false) |
|
| 1505 | + if (($db_col = array_search($col, $this->db_cols)) !== false) |
|
| 1506 | 1506 | { |
| 1507 | 1507 | $cols[$db_col] = $col; |
| 1508 | 1508 | } |
@@ -1515,10 +1515,10 @@ discard block |
||
| 1515 | 1515 | } |
| 1516 | 1516 | if ($extra_cols) // extra columns to report |
| 1517 | 1517 | { |
| 1518 | - foreach(is_array($extra_cols) ? $extra_cols : explode(',',$extra_cols) as $col) |
|
| 1518 | + foreach (is_array($extra_cols) ? $extra_cols : explode(',', $extra_cols) as $col) |
|
| 1519 | 1519 | { |
| 1520 | - if (stripos($col,'as ')!==false) $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col); |
|
| 1521 | - if (($db_col = array_search($col,$this->db_cols)) !== false) |
|
| 1520 | + if (stripos($col, 'as ') !== false) $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i', '\\1', $col); |
|
| 1521 | + if (($db_col = array_search($col, $this->db_cols)) !== false) |
|
| 1522 | 1522 | { |
| 1523 | 1523 | $cols[$db_col] = $col; |
| 1524 | 1524 | } |
@@ -1545,12 +1545,12 @@ discard block |
||
| 1545 | 1545 | * @param string|array $extra_cols =array() |
| 1546 | 1546 | * @return int total number of rows |
| 1547 | 1547 | */ |
| 1548 | - function get_rows($query,&$rows,&$readonlys,$join='',$need_full_no_count=false,$only_keys=false,$extra_cols=array()) |
|
| 1548 | + function get_rows($query, &$rows, &$readonlys, $join = '', $need_full_no_count = false, $only_keys = false, $extra_cols = array()) |
|
| 1549 | 1549 | { |
| 1550 | - unset($readonlys); // required by function signature, but not used in this default implementation |
|
| 1551 | - if ((int) $this->debug >= 4) |
|
| 1550 | + unset($readonlys); // required by function signature, but not used in this default implementation |
|
| 1551 | + if ((int)$this->debug >= 4) |
|
| 1552 | 1552 | { |
| 1553 | - echo "<p>so_sql::get_rows(".print_r($query,true).",,)</p>\n"; |
|
| 1553 | + echo "<p>so_sql::get_rows(".print_r($query, true).",,)</p>\n"; |
|
| 1554 | 1554 | } |
| 1555 | 1555 | $criteria = array(); |
| 1556 | 1556 | $op = 'AND'; |
@@ -1558,11 +1558,11 @@ discard block |
||
| 1558 | 1558 | { |
| 1559 | 1559 | $criteria = $query['search']; |
| 1560 | 1560 | } |
| 1561 | - $rows = $this->search($criteria,$only_keys,$query['order']?$query['order'].' '.$query['sort']:'',$extra_cols, |
|
| 1562 | - '',false,$op,$query['num_rows']?array((int)$query['start'],$query['num_rows']):(int)$query['start'], |
|
| 1563 | - $query['col_filter'],$join,$need_full_no_count); |
|
| 1561 | + $rows = $this->search($criteria, $only_keys, $query['order'] ? $query['order'].' '.$query['sort'] : '', $extra_cols, |
|
| 1562 | + '', false, $op, $query['num_rows'] ? array((int)$query['start'], $query['num_rows']) : (int)$query['start'], |
|
| 1563 | + $query['col_filter'], $join, $need_full_no_count); |
|
| 1564 | 1564 | |
| 1565 | - if (!$rows) $rows = array(); // otherwise false returned from search would be returned as array(false) |
|
| 1565 | + if (!$rows) $rows = array(); // otherwise false returned from search would be returned as array(false) |
|
| 1566 | 1566 | |
| 1567 | 1567 | return $this->total; |
| 1568 | 1568 | } |
@@ -1573,7 +1573,7 @@ discard block |
||
| 1573 | 1573 | * @param array $data =null data-set to check, defaults to $this->data |
| 1574 | 1574 | * @return int 0: all keys are unique, 1: first key not unique, 2: ... |
| 1575 | 1575 | */ |
| 1576 | - function not_unique($data=null) |
|
| 1576 | + function not_unique($data = null) |
|
| 1577 | 1577 | { |
| 1578 | 1578 | if (!is_array($data)) |
| 1579 | 1579 | { |
@@ -1586,12 +1586,12 @@ discard block |
||
| 1586 | 1586 | { |
| 1587 | 1587 | $uni_keys[] = $this->db_key_cols; |
| 1588 | 1588 | } |
| 1589 | - foreach($uni_keys as $db_col => $col) |
|
| 1589 | + foreach ($uni_keys as $db_col => $col) |
|
| 1590 | 1590 | { |
| 1591 | 1591 | if (is_array($col)) |
| 1592 | 1592 | { |
| 1593 | 1593 | $query = array(); |
| 1594 | - foreach($col as $db_c => $c) |
|
| 1594 | + foreach ($col as $db_c => $c) |
|
| 1595 | 1595 | { |
| 1596 | 1596 | $query[$db_c] = $data[$c]; |
| 1597 | 1597 | } |
@@ -1600,17 +1600,17 @@ discard block |
||
| 1600 | 1600 | { |
| 1601 | 1601 | $query = array($db_col => $data[$col]); |
| 1602 | 1602 | } |
| 1603 | - foreach($this->db->select($this->table_name,$this->db_key_cols,$query,__LINE__,__FILE__,false,'',$this->app) as $other) |
|
| 1603 | + foreach ($this->db->select($this->table_name, $this->db_key_cols, $query, __LINE__, __FILE__, false, '', $this->app) as $other) |
|
| 1604 | 1604 | { |
| 1605 | - foreach($this->db_key_cols as $key_col) |
|
| 1605 | + foreach ($this->db_key_cols as $key_col) |
|
| 1606 | 1606 | { |
| 1607 | 1607 | if ($data[$key_col] != $other[$key_col]) |
| 1608 | 1608 | { |
| 1609 | - if ((int) $this->debug >= 4) |
|
| 1609 | + if ((int)$this->debug >= 4) |
|
| 1610 | 1610 | { |
| 1611 | 1611 | echo "<p>not_unique in ".array2string($col)." as for '$key_col': '${data[$key_col]}' != '${other[$key_col]}'</p>\n"; |
| 1612 | 1612 | } |
| 1613 | - return $n; // different entry => $n not unique |
|
| 1613 | + return $n; // different entry => $n not unique |
|
| 1614 | 1614 | } |
| 1615 | 1615 | } |
| 1616 | 1616 | } |
@@ -1631,7 +1631,7 @@ discard block |
||
| 1631 | 1631 | * @param string $order ='' order, default '' = same as (first) $value_col |
| 1632 | 1632 | * @return array with key_col => value_col pairs or array if more then one value_col given (keys as in value_col) |
| 1633 | 1633 | */ |
| 1634 | - function query_list($value_col,$key_col='',$filter=array(),$order='') |
|
| 1634 | + function query_list($value_col, $key_col = '', $filter = array(), $order = '') |
|
| 1635 | 1635 | { |
| 1636 | 1636 | static $cache = array(); |
| 1637 | 1637 | |
@@ -1644,16 +1644,16 @@ discard block |
||
| 1644 | 1644 | if (!is_array($value_col)) $value_col = array($value_col); |
| 1645 | 1645 | |
| 1646 | 1646 | $cols = $ret = array(); |
| 1647 | - foreach($value_col as $key => $col) |
|
| 1647 | + foreach ($value_col as $key => $col) |
|
| 1648 | 1648 | { |
| 1649 | 1649 | $matches = null; |
| 1650 | - $cols[$key] = preg_match('/AS ([a-z_0-9]+)$/i',$col,$matches) ? $matches[1] : $col; |
|
| 1650 | + $cols[$key] = preg_match('/AS ([a-z_0-9]+)$/i', $col, $matches) ? $matches[1] : $col; |
|
| 1651 | 1651 | } |
| 1652 | 1652 | if (!$order) $order = current($cols); |
| 1653 | 1653 | |
| 1654 | - if (($search =& $this->search(array(),($key_col ? $key_col.',' : 'DISTINCT ').implode(',',$value_col),$order,'','',false,'AND',false,$filter))) |
|
| 1654 | + if (($search = & $this->search(array(), ($key_col ? $key_col.',' : 'DISTINCT ').implode(',', $value_col), $order, '', '', false, 'AND', false, $filter))) |
|
| 1655 | 1655 | { |
| 1656 | - if (preg_match('/AS ([a-z_0-9]+)$/i',$key_col,$matches)) |
|
| 1656 | + if (preg_match('/AS ([a-z_0-9]+)$/i', $key_col, $matches)) |
|
| 1657 | 1657 | { |
| 1658 | 1658 | $key_col = $matches[1]; |
| 1659 | 1659 | } |
@@ -1661,12 +1661,12 @@ discard block |
||
| 1661 | 1661 | { |
| 1662 | 1662 | $key_col = current($cols); |
| 1663 | 1663 | } |
| 1664 | - foreach($search as $row) |
|
| 1664 | + foreach ($search as $row) |
|
| 1665 | 1665 | { |
| 1666 | 1666 | if (count($cols) > 1) |
| 1667 | 1667 | { |
| 1668 | 1668 | $data = array(); |
| 1669 | - foreach($cols as $key => $col) |
|
| 1669 | + foreach ($cols as $key => $col) |
|
| 1670 | 1670 | { |
| 1671 | 1671 | $data[$key] = $row[$col]; |
| 1672 | 1672 | } |
@@ -1678,7 +1678,7 @@ discard block |
||
| 1678 | 1678 | if ($data) $ret[$row[$key_col]] = $data; |
| 1679 | 1679 | } |
| 1680 | 1680 | } |
| 1681 | - return $cache[$cache_key] =& $ret; |
|
| 1681 | + return $cache[$cache_key] = & $ret; |
|
| 1682 | 1682 | } |
| 1683 | 1683 | |
| 1684 | 1684 | /** |
@@ -1687,13 +1687,13 @@ discard block |
||
| 1687 | 1687 | * @param string $column =null name of column or null for all (default) |
| 1688 | 1688 | * @return array|string array with internal-name => comment pairs, or string with comment, if $column given |
| 1689 | 1689 | */ |
| 1690 | - public function get_comments($column=null) |
|
| 1690 | + public function get_comments($column = null) |
|
| 1691 | 1691 | { |
| 1692 | - static $comments=null; |
|
| 1692 | + static $comments = null; |
|
| 1693 | 1693 | |
| 1694 | 1694 | if (is_null($comments)) |
| 1695 | 1695 | { |
| 1696 | - foreach($this->db_cols as $db_col => $col) |
|
| 1696 | + foreach ($this->db_cols as $db_col => $col) |
|
| 1697 | 1697 | { |
| 1698 | 1698 | $comments[$col] = $this->table_def['fd'][$db_col]['comment']; |
| 1699 | 1699 | } |
@@ -1063,7 +1063,7 @@ |
||
| 1063 | 1063 | * @return Array |
| 1064 | 1064 | * @throws Api\Db\Exception |
| 1065 | 1065 | */ |
| 1066 | - protected function parse_search(Array $_criteria, $wildcard, $empty, $op) |
|
| 1066 | + protected function parse_search(array $_criteria, $wildcard, $empty, $op) |
|
| 1067 | 1067 | { |
| 1068 | 1068 | $criteria = $this->data2db($_criteria); |
| 1069 | 1069 | foreach($criteria as $col => $val) |