Passed
Push — 16.1 ( 6e101a...277864 )
by Ralf
18:52
created
api/src/Storage/Base.php 2 patches
Spacing   +220 added lines, -220 removed lines patch added patch discarded remove patch
@@ -179,7 +179,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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))
@@ -851,7 +851,7 @@  discard block
 block discarded – undo
851 851
 		else
852 852
 		{
853 853
 			$criteria = $this->data2db($criteria);
854
-			foreach($criteria as $col => $val)
854
+			foreach ($criteria as $col => $val)
855 855
 			{
856 856
 				if (is_int($col))
857 857
 				{
@@ -859,7 +859,7 @@  discard block
 block discarded – undo
859 859
 				}
860 860
 				elseif ($empty || $val != '')
861 861
 				{
862
-					if (!($db_col = array_search($col,$this->db_cols)))
862
+					if (!($db_col = array_search($col, $this->db_cols)))
863 863
 					{
864 864
 						$db_col = $col;
865 865
 					}
@@ -870,7 +870,7 @@  discard block
 block discarded – undo
870 870
 							$this->table_def['fd'][$db_col]['nullable'] !== false)
871 871
 						{
872 872
 							unset($criteria[$col]);
873
-							$query[] =  '(' . $db_col . ' IS NULL OR ' . $db_col . " = '')";
873
+							$query[] = '('.$db_col.' IS NULL OR '.$db_col." = '')";
874 874
 						}
875 875
 						else
876 876
 						{
@@ -878,10 +878,10 @@  discard block
 block discarded – undo
878 878
 						}
879 879
 					}
880 880
 					elseif ($wildcard || $criteria[$col][0] == '!' ||
881
-						is_string($criteria[$col]) && (strpos($criteria[$col],'*')!==false || strpos($criteria[$col],'?')!==false))
881
+						is_string($criteria[$col]) && (strpos($criteria[$col], '*') !== false || strpos($criteria[$col], '?') !== false))
882 882
 					{
883 883
 						// if search pattern alread contains a wildcard, do NOT add further ones automatic
884
-						if (is_string($criteria[$col]) && (strpos($criteria[$col],'*')!==false || strpos($criteria[$col],'?')!==false))
884
+						if (is_string($criteria[$col]) && (strpos($criteria[$col], '*') !== false || strpos($criteria[$col], '?') !== false))
885 885
 						{
886 886
 							$wildcard = '';
887 887
 						}
@@ -890,19 +890,19 @@  discard block
 block discarded – undo
890 890
 						if ($criteria[$col][0] == '!')
891 891
 						{
892 892
 							$cmp_op = ' NOT'.$cmp_op;
893
-							$criteria[$col] = substr($criteria[$col],1);
893
+							$criteria[$col] = substr($criteria[$col], 1);
894 894
 							$negate = true;
895 895
 						}
896
-						foreach(explode(' ',$criteria[$col]) as $crit)
896
+						foreach (explode(' ', $criteria[$col]) as $crit)
897 897
 						{
898 898
 							$query[] = ($negate ? ' ('.$db_col.' IS NULL OR ' : '').$db_col.$cmp_op.
899
-								$this->db->quote($wildcard.str_replace(array('%','_','*','?'),array('\\%','\\_','%','_'),$crit).$wildcard).
899
+								$this->db->quote($wildcard.str_replace(array('%', '_', '*', '?'), array('\\%', '\\_', '%', '_'), $crit).$wildcard).
900 900
 								($negate ? ') ' : '');
901 901
 						}
902 902
 					}
903
-					elseif (strpos($db_col,'.') !== false)	// we have a table-name specified
903
+					elseif (strpos($db_col, '.') !== false)	// we have a table-name specified
904 904
 					{
905
-						list($table,$only_col) = explode('.',$db_col);
905
+						list($table, $only_col) = explode('.', $db_col);
906 906
 						$type = $this->db->get_column_attribute($only_col, $table, true, 'type');
907 907
 						if (empty($type))
908 908
 						{
@@ -910,15 +910,15 @@  discard block
 block discarded – undo
910 910
 						}
911 911
 						if (is_array($val) && count($val) > 1)
912 912
 						{
913
-							foreach($val as &$v)
913
+							foreach ($val as &$v)
914 914
 							{
915 915
 								$v = $this->db->quote($v, $type);
916 916
 							}
917
-							$query[] = $sql = $db_col.' IN (' .implode(',',$val).')';
917
+							$query[] = $sql = $db_col.' IN ('.implode(',', $val).')';
918 918
 						}
919 919
 						else
920 920
 						{
921
-							$query[] = $db_col.'='.$this->db->quote(is_array($val)?array_shift($val):$val,$type);
921
+							$query[] = $db_col.'='.$this->db->quote(is_array($val) ?array_shift($val) : $val, $type);
922 922
 						}
923 923
 					}
924 924
 					else
@@ -927,7 +927,7 @@  discard block
 block discarded – undo
927 927
 					}
928 928
 				}
929 929
 			}
930
-			if (is_array($query) && $op != 'AND') $query = $this->db->column_data_implode(' '.$op.' ',$query);
930
+			if (is_array($query) && $op != 'AND') $query = $this->db->column_data_implode(' '.$op.' ', $query);
931 931
 		}
932 932
 		if (is_array($filter))
933 933
 		{
@@ -935,19 +935,19 @@  discard block
 block discarded – undo
935 935
 			$data2db_filter = $this->data2db($filter);
936 936
 			if (!is_array($data2db_filter)) {
937 937
 				echo function_backtrace()."<br/>\n";
938
-				echo "filter=";_debug_array($filter);
939
-				echo "data2db(filter)=";_debug_array($data2db_filter);
938
+				echo "filter="; _debug_array($filter);
939
+				echo "data2db(filter)="; _debug_array($data2db_filter);
940 940
 			}
941
-			foreach($data2db_filter as $col => $val)
941
+			foreach ($data2db_filter as $col => $val)
942 942
 			{
943 943
 				if ($val !== '')
944 944
 				{
945 945
 					// check if a db-internal name conversation necessary
946
-					if (!is_int($col) && ($c = array_search($col,$this->db_cols)))
946
+					if (!is_int($col) && ($c = array_search($col, $this->db_cols)))
947 947
 					{
948
-						$col = $this->table_name . '.' . $c;
948
+						$col = $this->table_name.'.'.$c;
949 949
 					}
950
-					if(is_int($col))
950
+					if (is_int($col))
951 951
 					{
952 952
 						$db_filter[] = $val;
953 953
 					}
@@ -965,35 +965,35 @@  discard block
 block discarded – undo
965 965
 			{
966 966
 				if ($op != 'AND')
967 967
 				{
968
-					$db_filter[] = '('.$this->db->column_data_implode(' '.$op.' ',$query).')';
968
+					$db_filter[] = '('.$this->db->column_data_implode(' '.$op.' ', $query).')';
969 969
 				}
970 970
 				else
971 971
 				{
972
-					$db_filter = array_merge($db_filter,$query);
972
+					$db_filter = array_merge($db_filter, $query);
973 973
 				}
974 974
 			}
975 975
 			$query = $db_filter;
976 976
 		}
977
-		if ((int) $this->debug >= 4)
977
+		if ((int)$this->debug >= 4)
978 978
 		{
979
-			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";
979
+			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";
980 980
 			echo "<br>criteria = "; _debug_array($criteria);
981 981
 		}
982 982
 		if ($only_keys === true)
983 983
 		{
984 984
 			$colums = array_keys($this->db_key_cols);
985
-			foreach($colums as &$column)
985
+			foreach ($colums as &$column)
986 986
 			{
987
-				$column = $this->table_name . '.' . $column;
987
+				$column = $this->table_name.'.'.$column;
988 988
 			}
989 989
 		}
990 990
 		elseif (is_array($only_keys))
991 991
 		{
992 992
 			$colums = array();
993
-			foreach($only_keys as $key => $col)
993
+			foreach ($only_keys as $key => $col)
994 994
 			{
995 995
 				//Convert ambiguous columns to prefixed tablename.column name
996
-				$colums[] = ($db_col = array_search($col,$this->db_cols)) ?  $this->table_name .'.'.$db_col.' AS '.$col :$col;
996
+				$colums[] = ($db_col = array_search($col, $this->db_cols)) ? $this->table_name.'.'.$db_col.' AS '.$col : $col;
997 997
 			}
998 998
 		}
999 999
 		elseif (!$only_keys)
@@ -1023,20 +1023,20 @@  discard block
 block discarded – undo
1023 1023
 			{
1024 1024
 				$colums[$key] = $this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id;
1025 1025
 			}
1026
-			elseif (!is_array($colums) && strpos($colums,$this->autoinc_id) !== false)
1026
+			elseif (!is_array($colums) && strpos($colums, $this->autoinc_id) !== false)
1027 1027
 			{
1028
-				$colums = preg_replace('/(?<! AS)([ ,]+)'.preg_quote($this->autoinc_id).'([ ,]+)/','\\1'.$this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id.'\\2',$colums);
1028
+				$colums = preg_replace('/(?<! AS)([ ,]+)'.preg_quote($this->autoinc_id).'([ ,]+)/', '\\1'.$this->table_name.'.'.$this->autoinc_id.' AS '.$this->autoinc_id.'\\2', $colums);
1029 1029
 			}
1030 1030
 		}
1031
-		$num_rows = 0;	// as spec. in max_matches in the user-prefs
1032
-		if (is_array($start)) list($start,$num_rows) = $start;
1031
+		$num_rows = 0; // as spec. in max_matches in the user-prefs
1032
+		if (is_array($start)) list($start, $num_rows) = $start;
1033 1033
 
1034 1034
 		// fix GROUP BY clause to contain all non-aggregate selected columns
1035
-		if ($order_by && stripos($order_by,'GROUP BY') !== false)
1035
+		if ($order_by && stripos($order_by, 'GROUP BY') !== false)
1036 1036
 		{
1037 1037
 			$order_by = $this->fix_group_by_columns($order_by, $colums, $this->table_name, $this->autoinc_id);
1038 1038
 		}
1039
-		elseif ($order_by && stripos($order_by,'ORDER BY')===false && stripos($order_by,'GROUP BY')===false && stripos($order_by,'HAVING')===false)
1039
+		elseif ($order_by && stripos($order_by, 'ORDER BY') === false && stripos($order_by, 'GROUP BY') === false && stripos($order_by, 'HAVING') === false)
1040 1040
 		{
1041 1041
 			$order_by = 'ORDER BY '.$order_by;
1042 1042
 		}
@@ -1059,9 +1059,9 @@  discard block
 block discarded – undo
1059 1059
 				);
1060 1060
 				if (!$union_cols)	// union used the colum-names of the first query
1061 1061
 				{
1062
-					$union_cols = $this->_get_columns($only_keys,$extra_cols);
1062
+					$union_cols = $this->_get_columns($only_keys, $extra_cols);
1063 1063
 				}
1064
-				return true;	// waiting for further calls, before running the union-query
1064
+				return true; // waiting for further calls, before running the union-query
1065 1065
 			}
1066 1066
 			// running the union query now
1067 1067
 			if ($start !== false)	// need to get the total too, saved in $this->total
@@ -1072,10 +1072,10 @@  discard block
 block discarded – undo
1072 1072
 				}
1073 1073
 				else	// cant do a count, have to run the query without limit
1074 1074
 				{
1075
-					$this->total = $this->db->union($union,__LINE__,__FILE__)->NumRows();
1075
+					$this->total = $this->db->union($union, __LINE__, __FILE__)->NumRows();
1076 1076
 				}
1077 1077
 			}
1078
-			$rs = $this->db->union($union,__LINE__,__FILE__,$order_by,$start,$num_rows);
1078
+			$rs = $this->db->union($union, __LINE__, __FILE__, $order_by, $start, $num_rows);
1079 1079
 			if ($this->debug) error_log(__METHOD__."() ".$this->db->Query_ID->sql);
1080 1080
 
1081 1081
 			$cols = $union_cols;
@@ -1089,21 +1089,21 @@  discard block
 block discarded – undo
1089 1089
 				{
1090 1090
 					$mysql_calc_rows = 'SQL_CALC_FOUND_ROWS ';
1091 1091
 				}
1092
-				elseif (!$need_full_no_count && (!$join || stripos($join,'LEFT JOIN')!==false))
1092
+				elseif (!$need_full_no_count && (!$join || stripos($join, 'LEFT JOIN') !== false))
1093 1093
 				{
1094
-					$this->total = $this->db->select($this->table_name,'COUNT(*)',$query,__LINE__,__FILE__,false,'',$this->app,0,$join)->fetchColumn();
1094
+					$this->total = $this->db->select($this->table_name, 'COUNT(*)', $query, __LINE__, __FILE__, false, '', $this->app, 0, $join)->fetchColumn();
1095 1095
 				}
1096 1096
 				else	// cant do a count, have to run the query without limit
1097 1097
 				{
1098
-					$this->total = $this->db->select($this->table_name,$colums,$query,__LINE__,__FILE__,false,$order_by,false,0,$join)->NumRows();
1098
+					$this->total = $this->db->select($this->table_name, $colums, $query, __LINE__, __FILE__, false, $order_by, false, 0, $join)->NumRows();
1099 1099
 				}
1100 1100
 			}
1101
-			$rs = $this->db->select($this->table_name,$mysql_calc_rows.$colums,$query,__LINE__,__FILE__,
1102
-				$start,$order_by,$this->app,$num_rows,$join);
1101
+			$rs = $this->db->select($this->table_name, $mysql_calc_rows.$colums, $query, __LINE__, __FILE__,
1102
+				$start, $order_by, $this->app, $num_rows, $join);
1103 1103
 			if ($this->debug) error_log(__METHOD__."() ".$this->db->Query_ID->sql);
1104
-			$cols = $this->_get_columns($only_keys,$extra_cols);
1104
+			$cols = $this->_get_columns($only_keys, $extra_cols);
1105 1105
 		}
1106
-		if ((int) $this->debug >= 4) echo "<p>sql='{$this->db->Query_ID->sql}'</p>\n";
1106
+		if ((int)$this->debug >= 4) echo "<p>sql='{$this->db->Query_ID->sql}'</p>\n";
1107 1107
 
1108 1108
 		if ($mysql_calc_rows)
1109 1109
 		{
@@ -1112,14 +1112,14 @@  discard block
 block discarded – undo
1112 1112
 		// ToDo: Implement that as an iterator, as $rs is also an interator and we could return one instead of an array
1113 1113
 		if ($this->search_return_iterator)
1114 1114
 		{
1115
-			return new Db2DataIterator($this,$rs);
1115
+			return new Db2DataIterator($this, $rs);
1116 1116
 		}
1117 1117
 		$arr = array();
1118 1118
 		$n = 0;
1119
-		if ($rs) foreach($rs as $row)
1119
+		if ($rs) foreach ($rs as $row)
1120 1120
 		{
1121 1121
 			$data = array();
1122
-			foreach($cols as $db_col => $col)
1122
+			foreach ($cols as $db_col => $col)
1123 1123
 			{
1124 1124
 				$data[$col] = (isset($row[$db_col]) ? $row[$db_col] : $row[$col]);
1125 1125
 			}
@@ -1149,7 +1149,7 @@  discard block
 block discarded – undo
1149 1149
 		$matches = null;
1150 1150
 		if (substr($GLOBALS['egw']->db->Type, 0, 5) == 'mysql' || !preg_match('/(GROUP BY .*)(HAVING.*|ORDER BY.*)?$/iU', $group_by, $matches))
1151 1151
 		{
1152
-			return $group_by;	// nothing to do
1152
+			return $group_by; // nothing to do
1153 1153
 		}
1154 1154
 		$changes = 0;
1155 1155
 		$group_by_cols = preg_split('/, */', trim(substr($matches[1], 9)));
@@ -1159,9 +1159,9 @@  discard block
 block discarded – undo
1159 1159
 			$columns = preg_split('/, */', $columns);
1160 1160
 
1161 1161
 			// fix columns containing commas as part of function calls
1162
-			for($n = 0; $n < count($columns); ++$n)
1162
+			for ($n = 0; $n < count($columns); ++$n)
1163 1163
 			{
1164
-				$col =& $columns[$n];
1164
+				$col = & $columns[$n];
1165 1165
 				while (substr_count($col, '(') > substr_count($col, ')') && ++$n < count($columns))
1166 1166
 				{
1167 1167
 					$col .= ','.$columns[$n];
@@ -1170,7 +1170,7 @@  discard block
 block discarded – undo
1170 1170
 			}
1171 1171
 			unset($col);
1172 1172
 		}
1173
-		foreach($columns as $n => $col)
1173
+		foreach ($columns as $n => $col)
1174 1174
 		{
1175 1175
 			if ($col == '*')
1176 1176
 			{
@@ -1183,7 +1183,7 @@  discard block
 block discarded – undo
1183 1183
 			{
1184 1184
 				if (($pos = stripos($col, 'DISTINCT ')) !== false)
1185 1185
 				{
1186
-					$col = substr($col, $pos+9);
1186
+					$col = substr($col, $pos + 9);
1187 1187
 				}
1188 1188
 				$alias = $col;
1189 1189
 				if (stripos($col, ' AS ')) list($col, $alias) = preg_split('/ +AS +/i', $col);
@@ -1201,7 +1201,7 @@  discard block
 block discarded – undo
1201 1201
 		$ret = $group_by;
1202 1202
 		if ($changes)
1203 1203
 		{
1204
-			$ret = str_replace($matches[1], 'GROUP BY '.implode(',', $group_by_cols).' ',  $group_by);
1204
+			$ret = str_replace($matches[1], 'GROUP BY '.implode(',', $group_by_cols).' ', $group_by);
1205 1205
 			//error_log(__METHOD__."('$group_by', ".array2string($columns).") group_by_cols=".array2string($group_by_cols)." changed to $ret");
1206 1206
 		}
1207 1207
 		return $ret;
@@ -1221,12 +1221,12 @@  discard block
 block discarded – undo
1221 1221
 	 * @param array $search_cols =array() List of columns to search.  If not provided, all columns in $this->db_cols will be considered
1222 1222
 	 * @return array or column => value pairs
1223 1223
 	 */
1224
-	public function search2criteria($_pattern,&$wildcard='',&$op='AND',$extra_col=null, $search_cols = array())
1224
+	public function search2criteria($_pattern, &$wildcard = '', &$op = 'AND', $extra_col = null, $search_cols = array())
1225 1225
 	{
1226 1226
 		$pattern = trim($_pattern);
1227 1227
 		// This function can get called multiple times.  Make sure it doesn't re-process.
1228 1228
 		if (empty($pattern) || is_array($pattern)) return $pattern;
1229
-		if(strpos($pattern, 'CAST(COALESCE(') !== false)
1229
+		if (strpos($pattern, 'CAST(COALESCE(') !== false)
1230 1230
 		{
1231 1231
 			return $pattern;
1232 1232
 		}
@@ -1243,24 +1243,24 @@  discard block
 block discarded – undo
1243 1243
 		$numeric_columns = array();
1244 1244
 
1245 1245
 		// Special handling for an ID search, #<int>
1246
-		if(strpos($_pattern, '#') === 0 && is_numeric(substr($_pattern, 1)))
1246
+		if (strpos($_pattern, '#') === 0 && is_numeric(substr($_pattern, 1)))
1247 1247
 		{
1248
-			return array('(' . $this->table_name.'.'. $this->autoinc_id . '=' . (int)substr($_pattern,1) . ')');
1248
+			return array('('.$this->table_name.'.'.$this->autoinc_id.'='.(int)substr($_pattern, 1).')');
1249 1249
 		}
1250
-		if(!$search_cols)
1250
+		if (!$search_cols)
1251 1251
 		{
1252 1252
 			$search_cols = $this->get_default_search_columns();
1253 1253
 		}
1254 1254
 		// Concat all fields to be searched together, so the conditions operate across the whole record
1255
-		foreach($search_cols as $col)
1255
+		foreach ($search_cols as $col)
1256 1256
 		{
1257 1257
 			$col_name = $col;
1258 1258
 			$table = $this->table_name;
1259
-			if (strpos($col,'.') !== false)
1259
+			if (strpos($col, '.') !== false)
1260 1260
 			{
1261
-				list($table,$col_name) = explode('.',$col);
1261
+				list($table, $col_name) = explode('.', $col);
1262 1262
 			}
1263
-			$table_def = $table == $this->table_name ? $this->table_def : $this->db->get_table_definitions(true,$table);
1263
+			$table_def = $table == $this->table_name ? $this->table_def : $this->db->get_table_definitions(true, $table);
1264 1264
 			if ($table_def['fd'][$col_name] && in_array($table_def['fd'][$col_name]['type'], $numeric_types))
1265 1265
 			{
1266 1266
 				$numeric_columns[] = $col;
@@ -1268,11 +1268,11 @@  discard block
 block discarded – undo
1268 1268
 			}
1269 1269
 			if ($this->db->Type == 'mysql' && $table_def['fd'][$col_name]['type'] === 'ascii' && preg_match('/[\x80-\xFF]/', $_pattern))
1270 1270
 			{
1271
-				continue;	// will only give sql error
1271
+				continue; // will only give sql error
1272 1272
 			}
1273
-			$columns[] = sprintf($this->db->capabilities[Api\Db::CAPABILITY_CAST_AS_VARCHAR],"COALESCE($col,'')");
1273
+			$columns[] = sprintf($this->db->capabilities[Api\Db::CAPABILITY_CAST_AS_VARCHAR], "COALESCE($col,'')");
1274 1274
 		}
1275
-		if(!$columns)
1275
+		if (!$columns)
1276 1276
 		{
1277 1277
 			return array();
1278 1278
 		}
@@ -1281,9 +1281,9 @@  discard block
 block discarded – undo
1281 1281
 		$break = ' ';
1282 1282
 		$token = strtok($pattern, $break);
1283 1283
 
1284
-		while($token)
1284
+		while ($token)
1285 1285
 		{
1286
-			if($token == strtoupper(lang('AND')) || $token == 'AND')
1286
+			if ($token == strtoupper(lang('AND')) || $token == 'AND')
1287 1287
 			{
1288 1288
 				$token = '+'.strtok($break);
1289 1289
 			}
@@ -1296,10 +1296,10 @@  discard block
 block discarded – undo
1296 1296
 			{
1297 1297
 				$token = '-'.strtok($break);
1298 1298
 			}
1299
-			if ($token[0]=='"')
1299
+			if ($token[0] == '"')
1300 1300
 			{
1301
-				$token = substr($token, 1,strlen($token));
1302
- 				if(substr($token, -1) != '"')
1301
+				$token = substr($token, 1, strlen($token));
1302
+ 				if (substr($token, -1) != '"')
1303 1303
 				{
1304 1304
 					$token .= ' '.strtok('"');
1305 1305
 				}
@@ -1310,16 +1310,16 @@  discard block
 block discarded – undo
1310 1310
 			}
1311 1311
 
1312 1312
 			// prepend and append extra wildcard %, if pattern does NOT already contain wildcards
1313
-			if (strpos($token,'*') === false && strpos($token,'?') === false)
1313
+			if (strpos($token, '*') === false && strpos($token, '?') === false)
1314 1314
 			{
1315
-				$wildcard = '%';	// if pattern contains no wildcards, add them before AND after the pattern
1315
+				$wildcard = '%'; // if pattern contains no wildcards, add them before AND after the pattern
1316 1316
 			}
1317 1317
 			else
1318 1318
 			{
1319
-				$wildcard = '';		// no extra wildcard, if pattern already contains some
1319
+				$wildcard = ''; // no extra wildcard, if pattern already contains some
1320 1320
 			}
1321 1321
 
1322
-			switch($token[0])
1322
+			switch ($token[0])
1323 1323
 			{
1324 1324
 				case '+':
1325 1325
 					$op = 'AND';
@@ -1334,32 +1334,32 @@  discard block
 block discarded – undo
1334 1334
 					$op = 'OR';
1335 1335
 					break;
1336 1336
 			}
1337
-			$token_filter = ' '.call_user_func_array(array($GLOBALS['egw']->db,'concat'),$columns).' '.
1338
-				$this->db->capabilities['case_insensitive_like'] . ' ' .
1339
-				$GLOBALS['egw']->db->quote($wildcard.str_replace(array('%','_','*','?'),array('\\%','\\_','%','_'),$token).$wildcard);
1337
+			$token_filter = ' '.call_user_func_array(array($GLOBALS['egw']->db, 'concat'), $columns).' '.
1338
+				$this->db->capabilities['case_insensitive_like'].' '.
1339
+				$GLOBALS['egw']->db->quote($wildcard.str_replace(array('%', '_', '*', '?'), array('\\%', '\\_', '%', '_'), $token).$wildcard);
1340 1340
 
1341 1341
 			// Compare numeric token as equality for numeric columns
1342 1342
 			// skip user-wildcards (*,?) in is_numeric test, but not SQL wildcards, which get escaped and give sql-error
1343
-			if (is_numeric(str_replace(array('*','?'), '', $token)))
1343
+			if (is_numeric(str_replace(array('*', '?'), '', $token)))
1344 1344
 			{
1345 1345
 				$numeric_filter = array();
1346
-				foreach($numeric_columns as $col)
1346
+				foreach ($numeric_columns as $col)
1347 1347
 				{
1348
-					if($wildcard == '')
1348
+					if ($wildcard == '')
1349 1349
 					{
1350 1350
 						// Token has a wildcard from user, use LIKE
1351
-						$numeric_filter[] = "($col IS NOT NULL AND CAST($col AS CHAR) " .
1352
-							$this->db->capabilities['case_insensitive_like'] . ' ' .
1353
-							$GLOBALS['egw']->db->quote(str_replace(array('*','?'), array('%','_'), $token)) . ')';
1351
+						$numeric_filter[] = "($col IS NOT NULL AND CAST($col AS CHAR) ".
1352
+							$this->db->capabilities['case_insensitive_like'].' '.
1353
+							$GLOBALS['egw']->db->quote(str_replace(array('*', '?'), array('%', '_'), $token)).')';
1354 1354
 					}
1355 1355
 					else
1356 1356
 					{
1357 1357
 						$numeric_filter[] = "($col IS NOT NULL AND $col = $token)";
1358 1358
 					}
1359 1359
 				}
1360
-				if(count($numeric_filter) > 0)
1360
+				if (count($numeric_filter) > 0)
1361 1361
 				{
1362
-					$token_filter = '(' . $token_filter . ' OR ' . implode(' OR ', $numeric_filter) . ')';
1362
+					$token_filter = '('.$token_filter.' OR '.implode(' OR ', $numeric_filter).')';
1363 1363
 				}
1364 1364
 			}
1365 1365
 			$criteria[$op][] = $token_filter;
@@ -1367,32 +1367,32 @@  discard block
 block discarded – undo
1367 1367
 			$token = strtok($break);
1368 1368
 		}
1369 1369
 
1370
-		if($criteria['NOT'])
1370
+		if ($criteria['NOT'])
1371 1371
 		{
1372
-			$filter[] = 'NOT (' . implode(' OR ', $criteria['NOT']) . ') ';
1372
+			$filter[] = 'NOT ('.implode(' OR ', $criteria['NOT']).') ';
1373 1373
 		}
1374
-		if($criteria['AND'])
1374
+		if ($criteria['AND'])
1375 1375
 		{
1376
-			$filter[] = implode(' AND ', $criteria['AND']) . ' ';
1376
+			$filter[] = implode(' AND ', $criteria['AND']).' ';
1377 1377
 		}
1378
-		if($criteria['OR'])
1378
+		if ($criteria['OR'])
1379 1379
 		{
1380
-			$filter[] = '(' . implode(' OR ', $criteria['OR']) . ') ';
1380
+			$filter[] = '('.implode(' OR ', $criteria['OR']).') ';
1381 1381
 		}
1382 1382
 
1383
-		if(count($filter))
1383
+		if (count($filter))
1384 1384
 		{
1385
-			$result = '(' . implode(' AND ', $filter) . ')';
1385
+			$result = '('.implode(' AND ', $filter).')';
1386 1386
 		}
1387 1387
 
1388 1388
 		// OR extra column on the end so a null or blank won't block a hit in the main columns
1389 1389
 		if ($extra_col)
1390 1390
 		{
1391
-			$result .= (strlen($result) ? ' OR ' : ' ') . "$extra_col = " . $GLOBALS['egw']->db->quote($pattern);
1391
+			$result .= (strlen($result) ? ' OR ' : ' ')."$extra_col = ".$GLOBALS['egw']->db->quote($pattern);
1392 1392
 		}
1393 1393
 
1394 1394
 		$op = 'OR';
1395
-		return array('(' . $result . ')');
1395
+		return array('('.$result.')');
1396 1396
 	}
1397 1397
 
1398 1398
 	/**
@@ -1411,21 +1411,21 @@  discard block
 block discarded – undo
1411 1411
 		$numeric_types = array('auto', 'int', 'float', 'double');
1412 1412
 
1413 1413
 		// Skip some numeric columns that don't make sense to search if we have to default to all columns
1414
-		if(is_null($this->columns_to_search))
1414
+		if (is_null($this->columns_to_search))
1415 1415
 		{
1416
-			foreach($search_cols as $key => &$col)
1416
+			foreach ($search_cols as $key => &$col)
1417 1417
 			{
1418 1418
 				// If the name as given isn't a real column name, and adding the prefix doesn't help, skip it
1419
-				if(!$this->table_def['fd'][$col] && !($col = $this->prefix.array_search($col, $search_cols))) {
1419
+				if (!$this->table_def['fd'][$col] && !($col = $this->prefix.array_search($col, $search_cols))) {
1420 1420
 					// Can't search this column
1421 1421
 					unset($search_cols[$key]);
1422 1422
 					continue;
1423 1423
 				}
1424
-				if(in_array($this->table_def['fd'][$col]['type'], $numeric_types))
1424
+				if (in_array($this->table_def['fd'][$col]['type'], $numeric_types))
1425 1425
 				{
1426
-					foreach($skip_columns_with as $bad)
1426
+					foreach ($skip_columns_with as $bad)
1427 1427
 					{
1428
-						if(strpos($col, $bad) !== false)
1428
+						if (strpos($col, $bad) !== false)
1429 1429
 						{
1430 1430
 							unset($search_cols[$key]);
1431 1431
 							continue 2;
@@ -1447,7 +1447,7 @@  discard block
 block discarded – undo
1447 1447
 	 * @param string|array $extra_cols ='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
1448 1448
 	 * @return array with columns as db-name => internal-name pairs
1449 1449
 	 */
1450
-	function _get_columns($only_keys,$extra_cols)
1450
+	function _get_columns($only_keys, $extra_cols)
1451 1451
 	{
1452 1452
 		//echo "_get_columns() only_keys="; _debug_array($only_keys); echo "extra_cols="; _debug_array($extra_cols);
1453 1453
 		if ($only_keys === true)	// only primary key
@@ -1458,7 +1458,7 @@  discard block
 block discarded – undo
1458 1458
 		{
1459 1459
 			$cols = array();
1460 1460
 			$distinct_checked = false;
1461
-			foreach(is_array($only_keys) ? $only_keys : explode(',', $only_keys) as $col)
1461
+			foreach (is_array($only_keys) ? $only_keys : explode(',', $only_keys) as $col)
1462 1462
 			{
1463 1463
 				if (!$distinct_checked)
1464 1464
 				{
@@ -1467,17 +1467,17 @@  discard block
 block discarded – undo
1467 1467
 				}
1468 1468
 				if (!$col || $col == '*' || $col == $this->table_name.'.*')	// all columns
1469 1469
 				{
1470
-					$cols = array_merge($cols,$this->db_cols);
1470
+					$cols = array_merge($cols, $this->db_cols);
1471 1471
 				}
1472 1472
 				else	// only the specified columns
1473 1473
 				{
1474
-					if (stripos($col,'as'))	// if there's already an explicit naming of the column, just use it
1474
+					if (stripos($col, 'as'))	// if there's already an explicit naming of the column, just use it
1475 1475
 					{
1476
-						$col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col);
1476
+						$col = preg_replace('/^.*as +([a-z0-9_]+) *$/i', '\\1', $col);
1477 1477
 						$cols[$col] = $col;
1478 1478
 						continue;
1479 1479
 					}
1480
-					if (($db_col = array_search($col,$this->db_cols)) !== false)
1480
+					if (($db_col = array_search($col, $this->db_cols)) !== false)
1481 1481
 					{
1482 1482
 						$cols[$db_col] = $col;
1483 1483
 					}
@@ -1490,10 +1490,10 @@  discard block
 block discarded – undo
1490 1490
 		}
1491 1491
 		if ($extra_cols)	// extra columns to report
1492 1492
 		{
1493
-			foreach(is_array($extra_cols) ? $extra_cols : explode(',',$extra_cols) as $col)
1493
+			foreach (is_array($extra_cols) ? $extra_cols : explode(',', $extra_cols) as $col)
1494 1494
 			{
1495
-				if (stripos($col,'as ')!==false) $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col);
1496
-				if (($db_col = array_search($col,$this->db_cols)) !== false)
1495
+				if (stripos($col, 'as ') !== false) $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i', '\\1', $col);
1496
+				if (($db_col = array_search($col, $this->db_cols)) !== false)
1497 1497
 				{
1498 1498
 					$cols[$db_col] = $col;
1499 1499
 				}
@@ -1520,12 +1520,12 @@  discard block
 block discarded – undo
1520 1520
 	 * @param string|array $extra_cols =array()
1521 1521
 	 * @return int total number of rows
1522 1522
 	 */
1523
-	function get_rows($query,&$rows,&$readonlys,$join='',$need_full_no_count=false,$only_keys=false,$extra_cols=array())
1523
+	function get_rows($query, &$rows, &$readonlys, $join = '', $need_full_no_count = false, $only_keys = false, $extra_cols = array())
1524 1524
 	{
1525
-		unset($readonlys);	// required by function signature, but not used in this default implementation
1526
-		if ((int) $this->debug >= 4)
1525
+		unset($readonlys); // required by function signature, but not used in this default implementation
1526
+		if ((int)$this->debug >= 4)
1527 1527
 		{
1528
-			echo "<p>so_sql::get_rows(".print_r($query,true).",,)</p>\n";
1528
+			echo "<p>so_sql::get_rows(".print_r($query, true).",,)</p>\n";
1529 1529
 		}
1530 1530
 		$criteria = array();
1531 1531
 		$op = 'AND';
@@ -1533,11 +1533,11 @@  discard block
 block discarded – undo
1533 1533
 		{
1534 1534
 			$criteria = $query['search'];
1535 1535
 		}
1536
-		$rows = $this->search($criteria,$only_keys,$query['order']?$query['order'].' '.$query['sort']:'',$extra_cols,
1537
-			'',false,$op,$query['num_rows']?array((int)$query['start'],$query['num_rows']):(int)$query['start'],
1538
-			$query['col_filter'],$join,$need_full_no_count);
1536
+		$rows = $this->search($criteria, $only_keys, $query['order'] ? $query['order'].' '.$query['sort'] : '', $extra_cols,
1537
+			'', false, $op, $query['num_rows'] ? array((int)$query['start'], $query['num_rows']) : (int)$query['start'],
1538
+			$query['col_filter'], $join, $need_full_no_count);
1539 1539
 
1540
-		if (!$rows) $rows = array();	// otherwise false returned from search would be returned as array(false)
1540
+		if (!$rows) $rows = array(); // otherwise false returned from search would be returned as array(false)
1541 1541
 
1542 1542
 		return $this->total;
1543 1543
 	}
@@ -1548,7 +1548,7 @@  discard block
 block discarded – undo
1548 1548
 	 * @param array $data =null data-set to check, defaults to $this->data
1549 1549
 	 * @return int 0: all keys are unique, 1: first key not unique, 2: ...
1550 1550
 	 */
1551
-	function not_unique($data=null)
1551
+	function not_unique($data = null)
1552 1552
 	{
1553 1553
 		if (!is_array($data))
1554 1554
 		{
@@ -1561,12 +1561,12 @@  discard block
 block discarded – undo
1561 1561
 		{
1562 1562
 			$uni_keys[] = $this->db_key_cols;
1563 1563
 		}
1564
-		foreach($uni_keys as $db_col => $col)
1564
+		foreach ($uni_keys as $db_col => $col)
1565 1565
 		{
1566 1566
 			if (is_array($col))
1567 1567
 			{
1568 1568
 				$query = array();
1569
-				foreach($col as $db_c => $c)
1569
+				foreach ($col as $db_c => $c)
1570 1570
 				{
1571 1571
 					$query[$db_c] = $data[$c];
1572 1572
 				}
@@ -1575,17 +1575,17 @@  discard block
 block discarded – undo
1575 1575
 			{
1576 1576
 				$query = array($db_col => $data[$col]);
1577 1577
 			}
1578
-			foreach($this->db->select($this->table_name,$this->db_key_cols,$query,__LINE__,__FILE__,false,'',$this->app) as $other)
1578
+			foreach ($this->db->select($this->table_name, $this->db_key_cols, $query, __LINE__, __FILE__, false, '', $this->app) as $other)
1579 1579
 			{
1580
-				foreach($this->db_key_cols as $key_col)
1580
+				foreach ($this->db_key_cols as $key_col)
1581 1581
 				{
1582 1582
 					if ($data[$key_col] != $other[$key_col])
1583 1583
 					{
1584
-						if ((int) $this->debug >= 4)
1584
+						if ((int)$this->debug >= 4)
1585 1585
 						{
1586 1586
 							echo "<p>not_unique in ".array2string($col)." as for '$key_col': '${data[$key_col]}' != '${other[$key_col]}'</p>\n";
1587 1587
 						}
1588
-						return $n;	// different entry => $n not unique
1588
+						return $n; // different entry => $n not unique
1589 1589
 					}
1590 1590
 				}
1591 1591
 			}
@@ -1606,7 +1606,7 @@  discard block
 block discarded – undo
1606 1606
 	 * @param string $order ='' order, default '' = same as (first) $value_col
1607 1607
 	 * @return array with key_col => value_col pairs or array if more then one value_col given (keys as in value_col)
1608 1608
 	 */
1609
-	function query_list($value_col,$key_col='',$filter=array(),$order='')
1609
+	function query_list($value_col, $key_col = '', $filter = array(), $order = '')
1610 1610
 	{
1611 1611
 		static $cache = array();
1612 1612
 
@@ -1619,16 +1619,16 @@  discard block
 block discarded – undo
1619 1619
 		if (!is_array($value_col)) $value_col = array($value_col);
1620 1620
 
1621 1621
 		$cols = $ret = array();
1622
-		foreach($value_col as $key => $col)
1622
+		foreach ($value_col as $key => $col)
1623 1623
 		{
1624 1624
 			$matches = null;
1625
-			$cols[$key] = preg_match('/AS ([a-z_0-9]+)$/i',$col,$matches) ? $matches[1] : $col;
1625
+			$cols[$key] = preg_match('/AS ([a-z_0-9]+)$/i', $col, $matches) ? $matches[1] : $col;
1626 1626
 		}
1627 1627
 		if (!$order) $order = current($cols);
1628 1628
 
1629
-		if (($search =& $this->search(array(),($key_col ? $key_col.',' : 'DISTINCT ').implode(',',$value_col),$order,'','',false,'AND',false,$filter)))
1629
+		if (($search = & $this->search(array(), ($key_col ? $key_col.',' : 'DISTINCT ').implode(',', $value_col), $order, '', '', false, 'AND', false, $filter)))
1630 1630
 		{
1631
-			if (preg_match('/AS ([a-z_0-9]+)$/i',$key_col,$matches))
1631
+			if (preg_match('/AS ([a-z_0-9]+)$/i', $key_col, $matches))
1632 1632
 			{
1633 1633
 				$key_col = $matches[1];
1634 1634
 			}
@@ -1636,12 +1636,12 @@  discard block
 block discarded – undo
1636 1636
 			{
1637 1637
 				$key_col = current($cols);
1638 1638
 			}
1639
-			foreach($search as $row)
1639
+			foreach ($search as $row)
1640 1640
 			{
1641 1641
 				if (count($cols) > 1)
1642 1642
 				{
1643 1643
 					$data = array();
1644
-					foreach($cols as $key => $col)
1644
+					foreach ($cols as $key => $col)
1645 1645
 					{
1646 1646
 						$data[$key] = $row[$col];
1647 1647
 					}
@@ -1653,7 +1653,7 @@  discard block
 block discarded – undo
1653 1653
 				if ($data) $ret[$row[$key_col]] = $data;
1654 1654
 			}
1655 1655
 		}
1656
-		return $cache[$cache_key] =& $ret;
1656
+		return $cache[$cache_key] = & $ret;
1657 1657
 	}
1658 1658
 
1659 1659
 	/**
@@ -1662,13 +1662,13 @@  discard block
 block discarded – undo
1662 1662
 	 * @param string $column =null name of column or null for all (default)
1663 1663
 	 * @return array|string array with internal-name => comment pairs, or string with comment, if $column given
1664 1664
 	 */
1665
-	public function get_comments($column=null)
1665
+	public function get_comments($column = null)
1666 1666
 	{
1667
-		static $comments=null;
1667
+		static $comments = null;
1668 1668
 
1669 1669
 		if (is_null($comments))
1670 1670
 		{
1671
-			foreach($this->db_cols as $db_col => $col)
1671
+			foreach ($this->db_cols as $db_col => $col)
1672 1672
 			{
1673 1673
 				$comments[$col] = $this->table_def['fd'][$db_col]['comment'];
1674 1674
 			}
Please login to merge, or discard this patch.
Braces   +193 added lines, -51 removed lines patch added patch discarded remove patch
@@ -182,7 +182,10 @@  discard block
 block discarded – undo
182 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
-		if ($app == 'phpgwapi') $app = 'api';
185
+		if ($app == 'phpgwapi')
186
+		{
187
+			$app = 'api';
188
+		}
186 189
 
187 190
 		if ($no_clone)
188 191
 		{
@@ -198,9 +201,15 @@  discard block
 block discarded – undo
198 201
 		{
199 202
 			$this->app = $app;
200 203
 
201
-			if (!$no_clone) $this->db->set_app($app);
204
+			if (!$no_clone)
205
+			{
206
+				$this->db->set_app($app);
207
+			}
202 208
 
203
-			if ($table) $this->setup_table($app,$table,$column_prefix);
209
+			if ($table)
210
+			{
211
+				$this->setup_table($app,$table,$column_prefix);
212
+			}
204 213
 		}
205 214
 		$this->init();
206 215
 
@@ -221,7 +230,10 @@  discard block
 block discarded – undo
221 230
 	 */
222 231
 	public function set_times($timestamp_type=false)
223 232
 	{
224
-		if ($timestamp_type !== false) $this->timestamp_type = $timestamp_type;
233
+		if ($timestamp_type !== false)
234
+		{
235
+			$this->timestamp_type = $timestamp_type;
236
+		}
225 237
 
226 238
 		// set current time
227 239
 		switch($this->timestamp_type)
@@ -279,7 +291,10 @@  discard block
 block discarded – undo
279 291
 			{
280 292
 				$this->autoinc_id = $col;
281 293
 			}
282
-			if ($def['type'] == 'bool') $this->has_bools = true;
294
+			if ($def['type'] == 'bool')
295
+			{
296
+				$this->has_bools = true;
297
+			}
283 298
 
284 299
 			foreach($this->table_def['uc'] as $k => $uni_index)
285 300
 			{
@@ -318,7 +333,10 @@  discard block
 block discarded – undo
318 333
 	 */
319 334
 	function data_merge($new)
320 335
 	{
321
-		if ((int) $this->debug >= 4) echo "<p>so_sql::data_merge(".print_r($new,true).")</p>\n";
336
+		if ((int) $this->debug >= 4)
337
+		{
338
+			echo "<p>so_sql::data_merge(".print_r($new,true).")</p>\n";
339
+		}
322 340
 
323 341
 		if (!is_array($new) || !count($new))
324 342
 		{
@@ -342,7 +360,10 @@  discard block
 block discarded – undo
342 360
 		{
343 361
 			$this->data[self::USER_TIMEZONE_READ] = $new[self::USER_TIMEZONE_READ];
344 362
 		}
345
-		if ((int) $this->debug >= 4) _debug_array($this->data);
363
+		if ((int) $this->debug >= 4)
364
+		{
365
+			_debug_array($this->data);
366
+		}
346 367
 	}
347 368
 
348 369
 	/**
@@ -486,7 +507,10 @@  discard block
 block discarded – undo
486 507
 		if (!is_array($keys))
487 508
 		{
488 509
 			$pk = array_values($this->db_key_cols);
489
-			if ($pk) $keys = array($pk[0] => $keys);
510
+			if ($pk)
511
+			{
512
+				$keys = array($pk[0] => $keys);
513
+			}
490 514
 		}
491 515
 
492 516
 		$this->init($keys);
@@ -500,13 +524,16 @@  discard block
 block discarded – undo
500 524
 				$query[$db_col] = $this->data[$col];
501 525
 			}
502 526
 		}
503
-		if (!$query)	// no primary key in keys, lets try the data_cols for a unique key
527
+		if (!$query)
528
+		{
529
+			// no primary key in keys, lets try the data_cols for a unique key
504 530
 		{
505 531
 			foreach($this->db_uni_cols as $db_col => $col)
506 532
 			{
507 533
 				if (!is_array($col) && $this->data[$col] != '')
508 534
 				{
509 535
 					$query[$db_col] = $this->data[$col];
536
+		}
510 537
 				}
511 538
 				elseif(is_array($col))
512 539
 				{
@@ -520,31 +547,43 @@  discard block
 block discarded – undo
520 547
 						}
521 548
 						$q[$db_c] = $this->data[$c];
522 549
 					}
523
-					if ($q) $query += $q;
550
+					if ($q)
551
+					{
552
+						$query += $q;
553
+					}
524 554
 				}
525 555
 			}
526 556
 		}
527
-		if (!$query)	// no unique key in keys, lets try everything else
557
+		if (!$query)
558
+		{
559
+			// no unique key in keys, lets try everything else
528 560
 		{
529 561
 			foreach($this->db_data_cols as $db_col => $col)
530 562
 			{
531 563
 				if ($this->data[$col] != '')
532 564
 				{
533 565
 					$query[$db_col] = $this->data[$col];
566
+		}
534 567
 				}
535 568
 			}
536 569
 		}
537
-		if (!$query)	// keys has no cols
570
+		if (!$query)
571
+		{
572
+			// keys has no cols
538 573
 		{
539 574
 			$this->db2data();
575
+		}
540 576
 
541 577
 			return False;
542 578
 		}
543
-		if ($join)	// Prefix the columns with the table-name, as they might exist in the join
579
+		if ($join)
580
+		{
581
+			// Prefix the columns with the table-name, as they might exist in the join
544 582
 		{
545 583
 			foreach($query as $col => $val)
546 584
 			{
547 585
 				if (is_int($col) || strpos($join,$col) === false) continue;
586
+		}
548 587
 				$query[] = $this->db->expression($this->table_name,$this->table_name.'.',array($col=>$val));
549 588
 				unset($query[$col]);
550 589
 			}
@@ -558,11 +597,14 @@  discard block
 block discarded – undo
558 597
 			$query,__LINE__,__FILE__,False,'',$this->app,0,$join) as $row)
559 598
 		{
560 599
 			$cols = $this->db_cols;
561
-			if ($extra_cols)	// extra columns to report
600
+			if ($extra_cols)
601
+			{
602
+				// extra columns to report
562 603
 			{
563 604
 				foreach(is_array($extra_cols) ? $extra_cols : array($extra_cols) as $col)
564 605
 				{
565 606
 					if (FALSE!==stripos($col,' as ')) $col = preg_replace('/^.* as *([a-z0-9_]+) *$/i','\\1',$col);
607
+			}
566 608
 					$cols[$col] = $col;
567 609
 				}
568 610
 			}
@@ -585,7 +627,10 @@  discard block
 block discarded – undo
585 627
 		{
586 628
 			unset($this->data[$this->db_key_cols[$this->autoinc_id]]);
587 629
 		}
588
-		if ((int) $this->debug >= 4) echo "nothing found !!!</p>\n";
630
+		if ((int) $this->debug >= 4)
631
+		{
632
+			echo "nothing found !!!</p>\n";
633
+		}
589 634
 
590 635
 		$this->db2data();
591 636
 
@@ -601,7 +646,10 @@  discard block
 block discarded – undo
601 646
 	 */
602 647
 	function save($keys=null,$extra_where=null)
603 648
 	{
604
-		if (is_array($keys) && count($keys)) $this->data_merge($keys);
649
+		if (is_array($keys) && count($keys))
650
+		{
651
+			$this->data_merge($keys);
652
+		}
605 653
 
606 654
 		// check if data contains user timezone during read AND user changed timezone since then
607 655
 		// --> load old timezone for the rest of this request
@@ -618,9 +666,13 @@  discard block
 block discarded – undo
618 666
 		}
619 667
 		$this->data2db();
620 668
 
621
-		if ((int) $this->debug >= 4) { echo "so_sql::save(".print_r($keys,true).") autoinc_id='$this->autoinc_id', data="; _debug_array($this->data); }
669
+		if ((int) $this->debug >= 4)
670
+		{
671
+echo "so_sql::save(".print_r($keys,true).") autoinc_id='$this->autoinc_id', data="; _debug_array($this->data); }
622 672
 
623
-		if ($this->autoinc_id && !$this->data[$this->db_key_cols[$this->autoinc_id]])	// insert with auto id
673
+		if ($this->autoinc_id && !$this->data[$this->db_key_cols[$this->autoinc_id]])
674
+		{
675
+			// insert with auto id
624 676
 		{
625 677
 			foreach($this->db_cols as $db_col => $col)
626 678
 			{
@@ -630,7 +682,9 @@  discard block
 block discarded – undo
630 682
 						(isset($this->table_def['fd'][$db_col]['default']) ||	// we have a default value
631 683
 						 !isset($this->table_def['fd'][$db_col]['nullable']) || $this->table_def['fd'][$db_col]['nullable']))	// column is nullable
632 684
 					{
633
-						continue;	// no need to write that (unset) column
685
+						continue;
686
+		}
687
+		// no need to write that (unset) column
634 688
 					}
635 689
 					if ($this->table_def['fd'][$db_col]['type'] == 'varchar' &&
636 690
 						strlen($this->data[$col]) > $this->table_def['fd'][$db_col]['precision'])
@@ -661,9 +715,13 @@  discard block
 block discarded – undo
661 715
 				if (!array_key_exists($col,$this->data) &&	// handling of unset columns in $this->data
662 716
 					($this->autoinc_id ||			// update of table with auto id or
663 717
 					 isset($this->table_def['fd'][$db_col]['default']) ||	// we have a default value or
664
-					 !isset($this->table_def['fd'][$db_col]['nullable']) || $this->table_def['fd'][$db_col]['nullable']))	// column is nullable
718
+					 !isset($this->table_def['fd'][$db_col]['nullable']) || $this->table_def['fd'][$db_col]['nullable']))
665 719
 				{
666
-					continue;	// no need to write that (unset) column
720
+					// column is nullable
721
+				{
722
+					continue;
723
+				}
724
+				// no need to write that (unset) column
667 725
 				}
668 726
 				$data[$db_col] = !is_object($this->data[$col]) && (string) $this->data[$col] === '' && $this->empty_on_write == 'NULL' ? null : $this->data[$col];
669 727
 			}
@@ -680,9 +738,12 @@  discard block
 block discarded – undo
680 738
 			{
681 739
 				$keys[$db_col] = $this->data[$col];
682 740
 			}
683
-			if (!$data && !$this->autoinc_id)	// happens if all columns are in the primary key
741
+			if (!$data && !$this->autoinc_id)
742
+			{
743
+				// happens if all columns are in the primary key
684 744
 			{
685 745
 				$data = $keys;
746
+			}
686 747
 				$keys = False;
687 748
 			}
688 749
 			if ($this->autoinc_id)
@@ -713,7 +774,10 @@  discard block
 block discarded – undo
713 774
 	 */
714 775
 	function update($_fields,$merge=true)
715 776
 	{
716
-		if ($merge) $this->data_merge($_fields);
777
+		if ($merge)
778
+		{
779
+			$this->data_merge($_fields);
780
+		}
717 781
 
718 782
 		$fields = $this->data2db($_fields);
719 783
 
@@ -766,9 +830,12 @@  discard block
 block discarded – undo
766 830
 		{
767 831
 			$keys = array($this->autoinc_id => $keys);
768 832
 		}
769
-		if (!is_array($keys) || !count($keys))	// use internal data
833
+		if (!is_array($keys) || !count($keys))
834
+		{
835
+			// use internal data
770 836
 		{
771 837
 			$data = $this->data;
838
+		}
772 839
 			$keys = $this->db_key_cols;
773 840
 		}
774 841
 		else	// data and keys are supplied in $keys
@@ -803,7 +870,10 @@  discard block
 block discarded – undo
803 870
 				$query[$db_col] = $data[$col];
804 871
 			}
805 872
 		}
806
-		if ($only_return_query) return $query;
873
+		if ($only_return_query)
874
+		{
875
+			return $query;
876
+		}
807 877
 
808 878
 		$this->db->delete($this->table_name,$query,__LINE__,__FILE__,$this->app);
809 879
 
@@ -836,7 +906,10 @@  discard block
 block discarded – undo
836 906
 	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 907
 	{
838 908
 		//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";
909
+		if ((int) $this->debug >= 4)
910
+		{
911
+			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";
912
+		}
840 913
 
841 914
 		// if extending class or instanciator set columns to search, convert string criteria to array
842 915
 		if ($criteria && !is_array($criteria))
@@ -900,9 +973,12 @@  discard block
 block discarded – undo
900 973
 								($negate ? ') ' : '');
901 974
 						}
902 975
 					}
903
-					elseif (strpos($db_col,'.') !== false)	// we have a table-name specified
976
+					elseif (strpos($db_col,'.') !== false)
977
+					{
978
+						// we have a table-name specified
904 979
 					{
905 980
 						list($table,$only_col) = explode('.',$db_col);
981
+					}
906 982
 						$type = $this->db->get_column_attribute($only_col, $table, true, 'type');
907 983
 						if (empty($type))
908 984
 						{
@@ -927,13 +1003,17 @@  discard block
 block discarded – undo
927 1003
 					}
928 1004
 				}
929 1005
 			}
930
-			if (is_array($query) && $op != 'AND') $query = $this->db->column_data_implode(' '.$op.' ',$query);
1006
+			if (is_array($query) && $op != 'AND')
1007
+			{
1008
+				$query = $this->db->column_data_implode(' '.$op.' ',$query);
1009
+			}
931 1010
 		}
932 1011
 		if (is_array($filter))
933 1012
 		{
934 1013
 			$db_filter = array();
935 1014
 			$data2db_filter = $this->data2db($filter);
936
-			if (!is_array($data2db_filter)) {
1015
+			if (!is_array($data2db_filter))
1016
+			{
937 1017
 				echo function_backtrace()."<br/>\n";
938 1018
 				echo "filter=";_debug_array($filter);
939 1019
 				echo "data2db(filter)=";_debug_array($data2db_filter);
@@ -1029,7 +1109,10 @@  discard block
 block discarded – undo
1029 1109
 			}
1030 1110
 		}
1031 1111
 		$num_rows = 0;	// as spec. in max_matches in the user-prefs
1032
-		if (is_array($start)) list($start,$num_rows) = $start;
1112
+		if (is_array($start))
1113
+		{
1114
+			list($start,$num_rows) = $start;
1115
+		}
1033 1116
 
1034 1117
 		// fix GROUP BY clause to contain all non-aggregate selected columns
1035 1118
 		if ($order_by && stripos($order_by,'GROUP BY') !== false)
@@ -1057,18 +1140,24 @@  discard block
 block discarded – undo
1057 1140
 					'append' => $order_by,
1058 1141
 					'join'   => $join,
1059 1142
 				);
1060
-				if (!$union_cols)	// union used the colum-names of the first query
1143
+				if (!$union_cols)
1144
+				{
1145
+					// union used the colum-names of the first query
1061 1146
 				{
1062 1147
 					$union_cols = $this->_get_columns($only_keys,$extra_cols);
1063 1148
 				}
1149
+				}
1064 1150
 				return true;	// waiting for further calls, before running the union-query
1065 1151
 			}
1066 1152
 			// running the union query now
1067
-			if ($start !== false)	// need to get the total too, saved in $this->total
1153
+			if ($start !== false)
1154
+			{
1155
+				// need to get the total too, saved in $this->total
1068 1156
 			{
1069 1157
 				if ($this->db->Type == 'mysql' && $this->db->ServerInfo['version'] >= 4.0)
1070 1158
 				{
1071 1159
 					$union[0]['cols'] = ($mysql_calc_rows = 'SQL_CALC_FOUND_ROWS ').$union[0]['cols'];
1160
+			}
1072 1161
 				}
1073 1162
 				else	// cant do a count, have to run the query without limit
1074 1163
 				{
@@ -1076,18 +1165,24 @@  discard block
 block discarded – undo
1076 1165
 				}
1077 1166
 			}
1078 1167
 			$rs = $this->db->union($union,__LINE__,__FILE__,$order_by,$start,$num_rows);
1079
-			if ($this->debug) error_log(__METHOD__."() ".$this->db->Query_ID->sql);
1168
+			if ($this->debug)
1169
+			{
1170
+				error_log(__METHOD__."() ".$this->db->Query_ID->sql);
1171
+			}
1080 1172
 
1081 1173
 			$cols = $union_cols;
1082 1174
 			$union = $union_cols = array();
1083 1175
 		}
1084 1176
 		else	// no UNION
1085 1177
 		{
1086
-			if ($start !== false)	// need to get the total too, saved in $this->total
1178
+			if ($start !== false)
1179
+			{
1180
+				// need to get the total too, saved in $this->total
1087 1181
 			{
1088 1182
 				if ($this->db->Type == 'mysql' && $this->db->ServerInfo['version'] >= 4.0)
1089 1183
 				{
1090 1184
 					$mysql_calc_rows = 'SQL_CALC_FOUND_ROWS ';
1185
+			}
1091 1186
 				}
1092 1187
 				elseif (!$need_full_no_count && (!$join || stripos($join,'LEFT JOIN')!==false))
1093 1188
 				{
@@ -1100,10 +1195,16 @@  discard block
 block discarded – undo
1100 1195
 			}
1101 1196
 			$rs = $this->db->select($this->table_name,$mysql_calc_rows.$colums,$query,__LINE__,__FILE__,
1102 1197
 				$start,$order_by,$this->app,$num_rows,$join);
1103
-			if ($this->debug) error_log(__METHOD__."() ".$this->db->Query_ID->sql);
1198
+			if ($this->debug)
1199
+			{
1200
+				error_log(__METHOD__."() ".$this->db->Query_ID->sql);
1201
+			}
1104 1202
 			$cols = $this->_get_columns($only_keys,$extra_cols);
1105 1203
 		}
1106
-		if ((int) $this->debug >= 4) echo "<p>sql='{$this->db->Query_ID->sql}'</p>\n";
1204
+		if ((int) $this->debug >= 4)
1205
+		{
1206
+			echo "<p>sql='{$this->db->Query_ID->sql}'</p>\n";
1207
+		}
1107 1208
 
1108 1209
 		if ($mysql_calc_rows)
1109 1210
 		{
@@ -1116,9 +1217,12 @@  discard block
 block discarded – undo
1116 1217
 		}
1117 1218
 		$arr = array();
1118 1219
 		$n = 0;
1119
-		if ($rs) foreach($rs as $row)
1220
+		if ($rs)
1221
+		{
1222
+			foreach($rs as $row)
1120 1223
 		{
1121 1224
 			$data = array();
1225
+		}
1122 1226
 			foreach($cols as $db_col => $col)
1123 1227
 			{
1124 1228
 				$data[$col] = (isset($row[$db_col]) ? $row[$db_col] : $row[$col]);
@@ -1186,9 +1290,15 @@  discard block
 block discarded – undo
1186 1290
 					$col = substr($col, $pos+9);
1187 1291
 				}
1188 1292
 				$alias = $col;
1189
-				if (stripos($col, ' AS ')) list($col, $alias) = preg_split('/ +AS +/i', $col);
1293
+				if (stripos($col, ' AS '))
1294
+				{
1295
+					list($col, $alias) = preg_split('/ +AS +/i', $col);
1296
+				}
1190 1297
 				// do NOT group by constant expressions
1191
-				if (preg_match('/^ *(-?[0-9]+|".*"|\'.*\'|NULL) *$/i', $col)) continue;
1298
+				if (preg_match('/^ *(-?[0-9]+|".*"|\'.*\'|NULL) *$/i', $col))
1299
+				{
1300
+					continue;
1301
+				}
1192 1302
 				if (!in_array($col, $group_by_cols) && !in_array($alias, $group_by_cols))
1193 1303
 				{
1194 1304
 					// instead of aliased primary key, we have to use original column incl. table-name as alias is ambigues
@@ -1225,7 +1335,10 @@  discard block
 block discarded – undo
1225 1335
 	{
1226 1336
 		$pattern = trim($_pattern);
1227 1337
 		// This function can get called multiple times.  Make sure it doesn't re-process.
1228
-		if (empty($pattern) || is_array($pattern)) return $pattern;
1338
+		if (empty($pattern) || is_array($pattern))
1339
+		{
1340
+			return $pattern;
1341
+		}
1229 1342
 		if(strpos($pattern, 'CAST(COALESCE(') !== false)
1230 1343
 		{
1231 1344
 			return $pattern;
@@ -1300,7 +1413,7 @@  discard block
 block discarded – undo
1300 1413
 			{
1301 1414
 				$token = substr($token, 1,strlen($token));
1302 1415
  				if(substr($token, -1) != '"')
1303
-				{
1416
+ 				{
1304 1417
 					$token .= ' '.strtok('"');
1305 1418
 				}
1306 1419
 				else
@@ -1416,7 +1529,8 @@  discard block
 block discarded – undo
1416 1529
 			foreach($search_cols as $key => &$col)
1417 1530
 			{
1418 1531
 				// If the name as given isn't a real column name, and adding the prefix doesn't help, skip it
1419
-				if(!$this->table_def['fd'][$col] && !($col = $this->prefix.array_search($col, $search_cols))) {
1532
+				if(!$this->table_def['fd'][$col] && !($col = $this->prefix.array_search($col, $search_cols)))
1533
+				{
1420 1534
 					// Can't search this column
1421 1535
 					unset($search_cols[$key]);
1422 1536
 					continue;
@@ -1450,10 +1564,13 @@  discard block
 block discarded – undo
1450 1564
 	function _get_columns($only_keys,$extra_cols)
1451 1565
 	{
1452 1566
 		//echo "_get_columns() only_keys="; _debug_array($only_keys); echo "extra_cols="; _debug_array($extra_cols);
1453
-		if ($only_keys === true)	// only primary key
1567
+		if ($only_keys === true)
1568
+		{
1569
+			// only primary key
1454 1570
 		{
1455 1571
 			$cols = $this->db_key_cols;
1456 1572
 		}
1573
+		}
1457 1574
 		else
1458 1575
 		{
1459 1576
 			$cols = array();
@@ -1462,18 +1579,27 @@  discard block
 block discarded – undo
1462 1579
 			{
1463 1580
 				if (!$distinct_checked)
1464 1581
 				{
1465
-					if (stripos($col, 'DISTINCT ') === 0) $col = substr($col, 9);
1582
+					if (stripos($col, 'DISTINCT ') === 0)
1583
+					{
1584
+						$col = substr($col, 9);
1585
+					}
1466 1586
 					$distinct_checked = true;
1467 1587
 				}
1468
-				if (!$col || $col == '*' || $col == $this->table_name.'.*')	// all columns
1588
+				if (!$col || $col == '*' || $col == $this->table_name.'.*')
1589
+				{
1590
+					// all columns
1469 1591
 				{
1470 1592
 					$cols = array_merge($cols,$this->db_cols);
1471 1593
 				}
1594
+				}
1472 1595
 				else	// only the specified columns
1473 1596
 				{
1474
-					if (stripos($col,'as'))	// if there's already an explicit naming of the column, just use it
1597
+					if (stripos($col,'as'))
1598
+					{
1599
+						// if there's already an explicit naming of the column, just use it
1475 1600
 					{
1476 1601
 						$col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col);
1602
+					}
1477 1603
 						$cols[$col] = $col;
1478 1604
 						continue;
1479 1605
 					}
@@ -1488,11 +1614,14 @@  discard block
 block discarded – undo
1488 1614
 				}
1489 1615
 			}
1490 1616
 		}
1491
-		if ($extra_cols)	// extra columns to report
1617
+		if ($extra_cols)
1618
+		{
1619
+			// extra columns to report
1492 1620
 		{
1493 1621
 			foreach(is_array($extra_cols) ? $extra_cols : explode(',',$extra_cols) as $col)
1494 1622
 			{
1495 1623
 				if (stripos($col,'as ')!==false) $col = preg_replace('/^.*as +([a-z0-9_]+) *$/i','\\1',$col);
1624
+		}
1496 1625
 				if (($db_col = array_search($col,$this->db_cols)) !== false)
1497 1626
 				{
1498 1627
 					$cols[$db_col] = $col;
@@ -1537,7 +1666,11 @@  discard block
 block discarded – undo
1537 1666
 			'',false,$op,$query['num_rows']?array((int)$query['start'],$query['num_rows']):(int)$query['start'],
1538 1667
 			$query['col_filter'],$join,$need_full_no_count);
1539 1668
 
1540
-		if (!$rows) $rows = array();	// otherwise false returned from search would be returned as array(false)
1669
+		if (!$rows)
1670
+		{
1671
+			$rows = array();
1672
+		}
1673
+		// otherwise false returned from search would be returned as array(false)
1541 1674
 
1542 1675
 		return $this->total;
1543 1676
 	}
@@ -1616,7 +1749,10 @@  discard block
 block discarded – undo
1616 1749
 		{
1617 1750
 			return $cache[$cache_key];
1618 1751
 		}
1619
-		if (!is_array($value_col)) $value_col = array($value_col);
1752
+		if (!is_array($value_col))
1753
+		{
1754
+			$value_col = array($value_col);
1755
+		}
1620 1756
 
1621 1757
 		$cols = $ret = array();
1622 1758
 		foreach($value_col as $key => $col)
@@ -1624,7 +1760,10 @@  discard block
 block discarded – undo
1624 1760
 			$matches = null;
1625 1761
 			$cols[$key] = preg_match('/AS ([a-z_0-9]+)$/i',$col,$matches) ? $matches[1] : $col;
1626 1762
 		}
1627
-		if (!$order) $order = current($cols);
1763
+		if (!$order)
1764
+		{
1765
+			$order = current($cols);
1766
+		}
1628 1767
 
1629 1768
 		if (($search =& $this->search(array(),($key_col ? $key_col.',' : 'DISTINCT ').implode(',',$value_col),$order,'','',false,'AND',false,$filter)))
1630 1769
 		{
@@ -1650,7 +1789,10 @@  discard block
 block discarded – undo
1650 1789
 				{
1651 1790
 					$data = $row[current($cols)];
1652 1791
 				}
1653
-				if ($data) $ret[$row[$key_col]] = $data;
1792
+				if ($data)
1793
+				{
1794
+					$ret[$row[$key_col]] = $data;
1795
+				}
1654 1796
 			}
1655 1797
 		}
1656 1798
 		return $cache[$cache_key] =& $ret;
Please login to merge, or discard this patch.