Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezpersistentobject.php
Go to the documentation of this file.
1 <?php
33 class eZPersistentObject
34 {
42 public $PersistentDataDirty;
43
53 public function __construct( $row = null )
54 {
55 $this->PersistentDataDirty = false;
56 if ( is_numeric( $row ) )
57 $row = $this->fetch( $row, false );
58 $this->fill( $row );
59 }
60
65 public function eZPersistentObject( $row = null )
66 {
67 self::__construct( $row );
68 }
69
81 public function fill( $row )
82 {
83 if ( !is_array( $row ) )
84 return;
85 $def = $this->definition();
86
87 foreach ( $def["fields"] as $key => $item )
88 {
89 if ( isset( $item['name'] ) )
90 {
91 $item = $item['name'];
92 }
93 $this->$item = isset( $row[$key] ) ? $row[$key] : null;
94 }
95
96 return true;
97 }
98
111 public static function replaceFieldsWithShortNames( eZDBInterface $db, array $fieldDefs, &$fields )
112 {
113 if ( !$db->useShortNames() || !$fields )
114 return;
115
116 $short_fields_names = array();
117 foreach ( $fields as $key => $val )
118 {
119 if( is_numeric( $key ) ) // $fields is not an associative array
120 {
121 if ( array_key_exists( $val, $fieldDefs ) &&
122 array_key_exists( 'short_name', $fieldDefs[$val] ) )
123 {
124 $short_fields_names[$key] = $fieldDefs[$val]['short_name'];
125 }
126 else
127 $short_fields_names[$key] = $val;
128 }
129 else // $fields is an associative array
130 {
131 if ( array_key_exists( $key, $fieldDefs ) &&
132 array_key_exists( 'short_name', $fieldDefs[$key] ) )
133 {
134 $newkey = $fieldDefs[$key]['short_name'];
135 }
136 else
137 $newkey = $key;
138 $short_fields_names[$newkey] = $val;
139 }
140
141 }
142 $fields = $short_fields_names;
143 }
144
155 protected static function replaceFieldsWithLongNames( eZDBInterface $db, array $fieldDefs, &$fields )
156 {
157 if ( !$db->useShortNames() || !$fields )
158 return;
159
160 foreach ( $fieldDefs as $fieldName => $fieldDefinition )
161 {
162 if ( !isset( $fieldDefinition['short_name'] ) )
163 {
164 continue;
165 }
166 $shortName = $fieldDefinition['short_name'];
167 if ( !isset( $fields[$shortName] ) )
168 {
169 continue;
170 }
171 $fields[$fieldName] = $fields[$shortName];
172 unset( $fields[$shortName] );
173 }
174 }
175
188 public static function count( $def, $conds = null, $field = null )
189 {
190 if ( !isset( $field ) )
191 {
192 $field = '*';
193 }
194 $customFields = array( array( 'operation' => 'COUNT( ' . $field . ' )', 'name' => 'row_count' ) );
195 $rows = eZPersistentObject::fetchObjectList( $def, array(), $conds, array(), null, false, false, $customFields );
196 return $rows[0]['row_count'];
197 }
198
214 public static function fetchObject( $def, $field_filters, $conds, $asObject = true, $grouping = null, $custom_fields = null )
215 {
216 $rows = eZPersistentObject::fetchObjectList( $def, $field_filters, $conds,
217 array(), null, $asObject,
218 $grouping, $custom_fields );
219 if ( $rows )
220 return $rows[0];
221 return null;
222 }
223
240 public function remove( $conditions = null, $extraConditions = null )
241 {
242 $def = $this->definition();
243 $keys = $def["keys"];
244 if ( !is_array( $conditions ) )
245 {
246 $conditions = array();
247 foreach ( $keys as $key )
248 {
249 $value = $this->attribute( $key );
250 $conditions[$key] = $value;
251 }
252 }
253 eZPersistentObject::removeObject( $def, $conditions, $extraConditions );
254 }
255
272 public static function removeObject( $def, $conditions = null, $extraConditions = null )
273 {
274 $db = eZDB::instance();
275
276 $table = $def["name"];
277 if ( is_array( $extraConditions ) )
278 {
279 foreach ( $extraConditions as $key => $cond )
280 {
281 $conditions[$key] = $cond;
282 }
283 }
284
285 // substitute fields mentioned the conditions whith their short names (if any)
286 $fields = $def['fields'];
287 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $conditions );
288
289 $cond_text = eZPersistentObject::conditionText( $conditions );
290
291 $db->query( "DELETE FROM $table $cond_text" );
292 }
293
305 public function store( $fieldFilters = null )
306 {
307 eZPersistentObject::storeObject( $this, $fieldFilters );
308 }
309
320 public function sync( $fieldFilters = null )
321 {
322 if ( $this->hasDirtyData() )
323 $this->store( $fieldFilters );
324 }
325
339 public static function storeObject( $obj, $fieldFilters = null )
340 {
341 $db = eZDB::instance();
342 $useFieldFilters = ( isset( $fieldFilters ) && is_array( $fieldFilters ) && $fieldFilters );
343
344 $def = $obj->definition();
345 $fields = $def["fields"];
346 $keys = $def["keys"];
347 $table = $def["name"];
348 $relations = isset( $def["relations"] ) ? $def["relations"] : null;
349 $insert_object = false;
350 $exclude_fields = array();
351 foreach ( $keys as $key )
352 {
353 $value = $obj->attribute( $key );
354 if ( $value === null )
355 {
356 $insert_object = true;
357 $exclude_fields[] = $key;
358 }
359 }
360
361 if ( $useFieldFilters )
362 $insert_object = false;
363
364 $use_fields = array_diff( array_keys( $fields ), $exclude_fields );
365 // If we filter out some of the fields we need to intersect it with $use_fields
366 if ( is_array( $fieldFilters ) )
367 $use_fields = array_intersect( $use_fields, $fieldFilters );
368 $doNotEscapeFields = array();
369 $changedValueFields = array();
370 $numericDataTypes = array( 'integer', 'float', 'double' );
371
372 foreach ( $use_fields as $field_name )
373 {
374 $field_def = $fields[$field_name];
375 $value = $obj->attribute( $field_name );
376
377 if ( $value === null )
378 {
379 if ( ! is_array( $field_def ) )
380 {
381 $exclude_fields[] = $field_name;
382 }
383 else
384 {
385 if ( array_key_exists( 'default', $field_def ) &&
386 ( $field_def['default'] !== null ||
387 ( $field_name == 'data_int' &&
388 array_key_exists( 'required', $field_def ) &&
389 $field_def[ 'required' ] == false ) ) )
390 {
391 $obj->setAttribute( $field_name, $field_def[ 'default' ] );
392 }
393 else
394 {
395 //if ( in_array( $field_def['datatype'], $numericDataTypes )
396 $exclude_fields[] = $field_name;
397 }
398 }
399 }
400
401 if ( strlen( (string) $value ) == 0 &&
402 is_array( $field_def ) &&
403 in_array( $field_def['datatype'], $numericDataTypes ) &&
404 array_key_exists( 'default', $field_def ) &&
405 ( $field_def[ 'default' ] === null || is_numeric( $field_def[ 'default' ] ) ) )
406 {
407 $obj->setAttribute( $field_name, $field_def[ 'default' ] );
408 }
409
410 if ( $value !== null &&
411 $field_def['datatype'] === 'string' &&
412 array_key_exists( 'max_length', $field_def ) &&
413 $field_def['max_length'] > 0 )
414 {
415 $obj->setAttribute( $field_name, $db->truncateString( $value, $field_def['max_length'], $field_name ) );
416 }
417 $bindDataTypes = array( 'text' );
418 if ( $db->bindingType() != eZDBInterface::BINDING_NO &&
419 $db->countStringSize( $value ) > 2000 &&
420 is_array( $field_def ) &&
421 in_array( $field_def['datatype'], $bindDataTypes )
422 )
423 {
424 $boundValue = $db->bindVariable( $value, $field_def );
425 // $obj->setAttribute( $field_name, $value );
426 $doNotEscapeFields[] = $field_name;
427 $changedValueFields[$field_name] = $boundValue;
428 }
429
430 }
431 $key_conds = array();
432 foreach ( $keys as $key )
433 {
434 $value = $obj->attribute( $key );
435 $key_conds[$key] = $value;
436 }
437 unset( $value );
438
439 $important_keys = $keys;
440 if ( is_array( $relations ) )
441 {
442 // $important_keys = array();
443 foreach( $relations as $relation => $relation_data )
444 {
445 if ( !in_array( $relation, $keys ) )
446 $important_keys[] = $relation;
447 }
448 }
449 if ( count( $important_keys ) == 0 && !$useFieldFilters )
450 {
451 $insert_object = true;
452 }
453 else if ( !$insert_object )
454 {
455 $rows = eZPersistentObject::fetchObjectList( $def, $keys, $key_conds,
456 array(), null, false,
457 null, null );
458 if ( count( $rows ) == 0 )
459 {
460 /* If we only want to update some fields in a record
461 * and that records does not exist, then we should do nothing, only return.
462 */
463 if ( $useFieldFilters )
464 return;
465
466 $insert_object = true;
467 }
468 }
469
470 if ( $insert_object )
471 {
472 // Note: When inserting we cannot hone the $fieldFilters parameters
473
474 $use_fields = array_diff( array_keys( $fields ), $exclude_fields );
475 $use_field_names = $use_fields;
476 if ( $db->useShortNames() )
477 {
478 $use_short_field_names = $use_field_names;
479 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $use_short_field_names );
480 $field_text = implode( ', ', $use_short_field_names );
481 unset( $use_short_field_names );
482 }
483 else
484 $field_text = implode( ', ', $use_field_names );
485
486 $use_values_hash = array();
487 $escapeFields = array_diff( $use_fields, $doNotEscapeFields );
488
489 foreach ( $escapeFields as $key )
490 {
491 $value = $obj->attribute( $key );
492 $field_def = $fields[$key];
493
494 if ( $field_def['datatype'] == 'float' || $field_def['datatype'] == 'double' )
495 {
496 if ( $value === null )
497 {
498 $use_values_hash[$key] = 'NULL';
499 }
500 else
501 {
502 $use_values_hash[$key] = sprintf( '%F', $value );
503 }
504 }
505 else if ( $field_def['datatype'] == 'int' || $field_def['datatype'] == 'integer' )
506 {
507 if ( $value === null )
508 {
509 $use_values_hash[$key] = 'NULL';
510 }
511 else
512 {
513 $use_values_hash[$key] = sprintf( '%d', $value );
514 }
515 }
516 else
517 {
518 // Note: for more colherence, we might use NULL for sql strings if the php value is NULL and not an empty sring
519 // but to keep compatibility with ez db, where most string columns are "not null default ''",
520 // and code feeding us a php null value without meaning it, we do not.
521 $use_values_hash[$key] = "'" . $db->escapeString( $value ) . "'";
522 }
523 }
524 foreach ( $doNotEscapeFields as $key )
525 {
526 $use_values_hash[$key] = $changedValueFields[$key];
527 }
528 $use_values = array();
529 foreach ( $use_field_names as $field )
530 $use_values[] = $use_values_hash[$field];
531 unset( $use_values_hash );
532 $value_text = implode( ", ", $use_values );
533
534 if( $db->DatabaseName() == 'sqlite' )
535 {
536 $sql = "INSERT OR REPLACE INTO $table ($field_text) VALUES($value_text)";
537 }
538 else
539 {
540 $sql = "INSERT INTO $table ($field_text) VALUES($value_text)";
541 }
542 $db->query( $sql );
543
544 if ( isset( $def["increment_key"] ) &&
545 is_string( $def["increment_key"] ) &&
546 !( $obj->attribute( $def["increment_key"] ) > 0 ) )
547 {
548 $inc = $def["increment_key"];
549 $id = $db->lastSerialID( $table, $inc );
550 if ( $id !== false )
551 $obj->setAttribute( $inc, $id );
552 }
553 }
554 else
555 {
556 $use_fields = array_diff( array_keys( $fields ), array_merge( $keys, $exclude_fields ) );
557 if ( count( $use_fields ) > 0 )
558 {
559 // If we filter out some of the fields we need to intersect it with $use_fields
560 if ( is_array( $fieldFilters ) )
561 $use_fields = array_intersect( $use_fields, $fieldFilters );
562 $use_field_names = array();
563 foreach ( $use_fields as $key )
564 {
565 if ( $db->useShortNames() && is_array( $fields[$key] ) && array_key_exists( 'short_name', $fields[$key] ) && strlen( $fields[$key]['short_name'] ) > 0 )
566 $use_field_names[$key] = $fields[$key]['short_name'];
567 else
568 $use_field_names[$key] = $key;
569 }
570
571 $field_text = "";
572 $field_text_len = 0;
573 $i = 0;
574
575
576 foreach ( $use_fields as $key )
577 {
578 $value = $obj->attribute( $key );
579
580 if ( $fields[$key]['datatype'] == 'float' || $fields[$key]['datatype'] == 'double' )
581 {
582 if ( $value === null )
583 $field_text_entry = $use_field_names[$key] . '=NULL';
584 else
585 $field_text_entry = $use_field_names[$key] . "=" . sprintf( '%F', $value );
586 }
587 else if ($fields[$key]['datatype'] == 'int' || $fields[$key]['datatype'] == 'integer' )
588 {
589 if ( $value === null )
590 $field_text_entry = $use_field_names[$key] . '=NULL';
591 else
592 $field_text_entry = $use_field_names[$key] . "=" . sprintf( '%d', $value );
593 }
594 else if ( in_array( $use_field_names[$key], $doNotEscapeFields ) )
595 {
596 $field_text_entry = $use_field_names[$key] . "=" . $changedValueFields[$key];
597 }
598 else
599 {
600 $field_text_entry = $use_field_names[$key] . "='" . $db->escapeString( $value ) . "'";
601 }
602
603 $field_text_len += strlen( $field_text_entry );
604 $needNewline = false;
605 if ( $field_text_len > 60 )
606 {
607 $needNewline = true;
608 $field_text_len = 0;
609 }
610 if ( $i > 0 )
611 $field_text .= "," . ($needNewline ? "\n " : ' ');
612 $field_text .= $field_text_entry;
613 ++$i;
614 }
615 $cond_text = eZPersistentObject::conditionText( $key_conds );
616 $sql = "UPDATE $table SET $field_text$cond_text";
617 $db->query( $sql );
618 }
619 }
620 $obj->setHasDirtyData( false );
621 }
622
629 public static function conditionText( $conditions )
630 {
631 return eZPersistentObject::conditionTextByRow( $conditions, null );
632 }
633
644 public static function conditionTextByRow( $conditions, $row )
645 {
646 $db = eZDB::instance();
647
648 $where_text = "";
649 if ( is_array( $conditions ) and
650 count( $conditions ) > 0 )
651 {
652 $where_text = " WHERE ";
653 $i = 0;
654 foreach ( $conditions as $id => $cond )
655 {
656 if ( $i > 0 )
657 $where_text .= " AND ";
658 if ( is_array( $row ) )
659 {
660 $where_text .= $cond . "='" . $db->escapeString( $row[$cond] ) . "'";
661 }
662 else
663 {
664 if ( is_array( $cond ) )
665 {
666 if ( is_array( $cond[0] ) )
667 {
668 $where_text .= $id . ' IN ( ';
669 $j = 0;
670 foreach ( $cond[0] as $value )
671 {
672 if ( $j > 0 )
673 $where_text .= ", ";
674 $where_text .= "'" . $db->escapeString( $value ) . "'";
675 ++$j;
676 }
677 $where_text .= ' ) ';
678 }
679 else if ( is_array( $cond[1] ) )
680 {
681 $range = $cond[1];
682 $where_text .= "$id BETWEEN '" . $db->escapeString( $range[0] ) . "' AND '" . $db->escapeString( $range[1] ) . "'";
683 }
684 else
685 {
686 switch ( $cond[0] )
687 {
688 case '>=':
689 case '<=':
690 case '<':
691 case '>':
692 case '=':
693 case '<>':
694 case '!=':
695 case 'like':
696 {
697 $where_text .= $db->escapeString( $id ) . " " . $cond[0] . " '" . $db->escapeString( $cond[1] ) . "'";
698 } break;
699 default:
700 {
701 eZDebug::writeError( "Conditional operator '$cond[0]' is not supported.", __METHOD__ );
702 } break;
703 }
704
705 }
706 }
707 else
708 $where_text .= $db->escapeString( $id ) . "='" . $db->escapeString( $cond ) . "'";
709 }
710 ++$i;
711 }
712 }
713 return $where_text;
714 }
715
807 public static function fetchObjectList( $def,
808 $field_filters = null,
809 $conds = null,
810 $sorts = null,
811 $limit = null,
812 $asObject = true,
813 $grouping = false,
814 $custom_fields = null,
815 $custom_tables = null,
816 $custom_conds = null )
817 {
818 $db = eZDB::instance();
819 $fields = $def["fields"];
820 $tables = $def["name"];
821 $class_name = $def["class_name"];
822 if ( is_array( $custom_tables ) )
823 {
824 foreach( $custom_tables as $custom_table )
825 $tables .= ', ' . $db->escapeString( $custom_table );
826 }
827 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $conds );
828 if ( is_array( $field_filters ) )
829 $field_array = array_unique( array_intersect(
830 $field_filters, array_keys( $fields ) ) );
831 else
832 $field_array = array_keys( $fields );
833 if ( $custom_fields !== null and is_array( $custom_fields ) )
834 {
835 foreach( $custom_fields as $custom_field )
836 {
837 if ( is_array( $custom_field ) )
838 {
839 $custom_text = $custom_field["operation"];
840 if ( isset( $custom_field["name"] ) )
841 {
842 $field_name = $custom_field["name"];
843 $custom_text .= " AS $field_name";
844 }
845 }
846 else
847 {
848 $custom_text = $custom_field;
849 }
850 $field_array[] = $custom_text;
851 }
852 }
853 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $field_array );
854 $field_text = '';
855 $i = 0;
856 foreach ( $field_array as $field_item )
857 {
858 if ( ( $i % 7 ) == 0 and
859 $i > 0 )
860 $field_text .= ", ";
861 else if ( $i > 0 )
862 $field_text .= ', ';
863 $field_text .= $field_item;
864 ++$i;
865 }
866
867 $where_text = eZPersistentObject::conditionText( $conds );
868 if ( $custom_conds )
869 $where_text .= $custom_conds;
870
871 $sort_text = "";
872 if ( $sorts !== false and ( isset( $def["sort"] ) or is_array( $sorts ) ) )
873 {
874 $sort_list = array();
875 if ( is_array( $sorts ) )
876 {
877 $sort_list = $sorts;
878 }
879 else if ( isset( $def['sort'] ) )
880 {
881 $sort_list = $def["sort"];
882 }
883 if ( count( $sort_list ) > 0 )
884 {
885 $sort_text = " ORDER BY ";
886 $i = 0;
887 foreach ( $sort_list as $sort_id => $sort_type )
888 {
889 if ( $i > 0 )
890 $sort_text .= ", ";
891 if ( $sort_type == "desc" )
892 $sort_text .= "$sort_id DESC";
893 else
894 $sort_text .= "$sort_id ASC";
895 ++$i;
896 }
897 }
898 }
899
900 $grouping_text = "";
901 if ( isset( $def["grouping"] ) or ( is_array( $grouping ) and count( $grouping ) > 0 ) )
902 {
903 $grouping_list = isset( $def["grouping"] ) ? $def["grouping"] : array();
904 if ( is_array( $grouping ) )
905 $grouping_list = $grouping;
906 if ( count( $grouping_list ) > 0 )
907 {
908 $grouping_text = " GROUP BY ";
909 $i = 0;
910 foreach ( $grouping_list as $grouping_id )
911 {
912 if ( $i > 0 )
913 $grouping_text .= ", ";
914 $grouping_text .= "$grouping_id";
915 ++$i;
916 }
917 }
918 }
919
920 $db_params = array();
921 if ( is_array( $limit ) )
922 {
923 if ( isset( $limit["offset"] ) )
924 {
925 $db_params["offset"] = $limit["offset"];
926 }
927 if ( isset( $limit['limit'] ) )
928 {
929 $db_params["limit"] = $limit["limit"];
930 }
931 else
932 {
933 $db_params["limit"] = $limit["length"];
934 }
935 }
936
937 $sqlText = "SELECT $field_text
938 FROM $tables" .
939 $where_text .
940 $grouping_text .
941 $sort_text;
942 $rows = $db->arrayQuery( $sqlText,
943 $db_params );
944
945 // Indicate that a DB error occured.
946 if ( $rows === false )
947 return null;
948
949 $objectList = eZPersistentObject::handleRows( $rows, $class_name, $asObject );
950 return $objectList;
951 }
952
964 public static function handleRows( $rows, $class_name, $asObject )
965 {
966 $objectDefinition = $class_name::definition();
967
968 if ( is_array( $rows ) )
969 {
970 $db = eZDB::instance();
971 foreach( $rows as &$row )
972 {
973 self::replaceFieldsWithLongNames( $db, $objectDefinition['fields'], $row );
974 }
975 }
976
977 if ( $asObject )
978 {
979 $objects = array();
980 if ( is_array( $rows ) )
981 {
982 foreach ( $rows as &$row )
983 {
984 $objects[] = new $class_name( $row );
985 }
986 }
987 return $objects;
988 }
989 else
990 return $rows;
991 }
992
1008 public static function swapRow( $table, $keys, $order_id, $rows, $id1, $id2 )
1009 {
1010 $db = eZDB::instance();
1011 $text = $order_id . "='" . $db->escapeString( $rows[$id1][$order_id] ) . "' WHERE ";
1012 $i = 0;
1013 foreach ( $keys as $key )
1014 {
1015 if ( $i > 0 )
1016 $text .= " AND ";
1017 $text .= $key . "='" . $db->escapeString( $rows[$id2][$key] ) . "'";
1018 ++$i;
1019 }
1020 return "UPDATE $table SET $text";
1021 }
1022
1035 public static function newObjectOrder( $def, $orderField, $conditions )
1036 {
1037 $db = eZDB::instance();
1038 $table = $def["name"];
1039 $keys = $def["keys"];
1040
1041 $cond_text = eZPersistentObject::conditionText( $conditions );
1042 $rows = $db->arrayQuery( "SELECT MAX($orderField) AS $orderField FROM $table $cond_text" );
1043 if ( count( $rows ) > 0 and isset( $rows[0][$orderField] ) )
1044 return $rows[0][$orderField] + 1;
1045 else
1046 return 1;
1047 }
1048
1070 public static function reorderObject( $def, $orderField, $conditions, $down = true )
1071 {
1072 $db = eZDB::instance();
1073 $table = $def["name"];
1074 $keys = $def["keys"];
1075
1076 reset( $orderField );
1077 $order_id = key( $orderField );
1078 $order_val = $orderField[$order_id];
1079 if ( $down )
1080 {
1081 $order_operator = ">=";
1082 $order_type = "asc";
1083 $order_add = -1;
1084 }
1085 else
1086 {
1087 $order_operator = "<=";
1088 $order_type = "desc";
1089 $order_add = 1;
1090 }
1091 $fields = array_merge( $keys, array( $order_id ) );
1092 $rows = eZPersistentObject::fetchObjectList( $def,
1093 $fields,
1094 array_merge( $conditions,
1095 array( $order_id => array( $order_operator,
1096 $order_val ) ) ),
1097 array( $order_id => $order_type ),
1098 array( "length" => 2 ),
1099 false );
1100 if ( count( $rows ) == 2 )
1101 {
1102 $swapSQL1 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 1, 0 );
1103 $swapSQL2 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 0, 1 );
1104 $db->begin();
1105 $db->query( $swapSQL1 );
1106 $db->query( $swapSQL2 );
1107 $db->commit();
1108 }
1109 else
1110 {
1111 $tmp = eZPersistentObject::fetchObjectList( $def,
1112 $fields,
1113 $conditions,
1114 array( $order_id => $order_type ),
1115 array( "length" => 1 ),
1116 false );
1117 $where_text = eZPersistentObject::conditionTextByRow( $keys, $rows[0] );
1118 $db->query( "UPDATE $table SET $order_id='" . ( $tmp[0][$order_id] + $order_add ) .
1119 "'$where_text" );
1120 }
1121 }
1122
1167 public static function definition()
1168 {
1169 return array();
1170 }
1171
1178 public static function escapeArray( $array )
1179 {
1180 $db = eZDB::instance();
1181 $out = array();
1182 foreach( $array as $key => $value )
1183 {
1184 if ( is_array( $value ) )
1185 {
1186 $tmp = array();
1187 foreach( $value as $valueItem )
1188 {
1189 $tmp[] = $db->escapeString( $valueItem );
1190 }
1191 $out[$key] = $tmp;
1192 unset( $tmp );
1193 }
1194 else
1195 $out[$key] = $db->escapeString( $value );
1196 }
1197 return $out;
1198 }
1199
1210 public static function updateObjectList( $parameters )
1211 {
1212 $db = eZDB::instance();
1213 $def = $parameters['definition'];
1214 $table = $def['name'];
1215 $fields = $def['fields'];
1216 $keys = $def['keys'];
1217
1218 $updateFields = $parameters['update_fields'];
1219 $conditions = $parameters['conditions'];
1220
1221 $query = "UPDATE $table SET ";
1222 $i = 0;
1223 $valueBound = false;
1224
1225 foreach( $updateFields as $field => $value )
1226 {
1227 $fieldDef = $fields[ $field ];
1228 $numericDataTypes = array( 'integer', 'float', 'double' );
1229 if ( strlen( $value ) == 0 &&
1230 is_array( $fieldDef ) &&
1231 in_array( $fieldDef['datatype'], $numericDataTypes ) &&
1232 array_key_exists( 'default', $fieldDef ) &&
1233 $fieldDef[ 'default' ] !== null )
1234 {
1235 $value = $fieldDef[ 'default' ];
1236 }
1237
1238 $bindDataTypes = array( 'text' );
1239 if ( $db->bindingType() != eZDBInterface::BINDING_NO &&
1240 $db->countStringSize( $value ) > 2000 &&
1241 is_array( $fieldDef ) &&
1242 in_array( $fieldDef['datatype'], $bindDataTypes )
1243 )
1244 {
1245 $value = $db->bindVariable( $value, $fieldDef );
1246 $valueBound = true;
1247 }
1248 else
1249 $valueBound = false;
1250
1251 if ( $i > 0 )
1252 $query .= ', ';
1253 if ( $valueBound )
1254 $query .= $field . "=" . $value;
1255 else
1256 $query .= $field . "='" . $db->escapeString( $value ) . "'";
1257 ++$i;
1258 }
1259 $query .= ' WHERE ';
1260 $i = 0;
1261 foreach( $conditions as $conditionKey => $condition )
1262 {
1263 if ( $i > 0 )
1264 $query .= ' AND ';
1265 if ( is_array( $condition ) )
1266 {
1267 $query .= $conditionKey . ' IN (';
1268 $j = 0;
1269 foreach( $condition as $conditionValue )
1270 {
1271 if ( $j > 0 )
1272 $query .= ', ';
1273 $query .= "'" . $db->escapeString( $conditionValue ) . "'";
1274 ++$j;
1275 }
1276 $query .= ')';
1277 }
1278 else
1279 $query .= $conditionKey . "='" . $db->escapeString( $condition ) . "'";
1280 ++$i;
1281 }
1282 $db->query( $query );
1283 }
1284
1293 public function attributes()
1294 {
1295 $def = $this->definition();
1296 $attrs = array_keys( $def["fields"] );
1297 if ( isset( $def["function_attributes"] ) )
1298 $attrs = array_unique( array_merge( $attrs, array_keys( $def["function_attributes"] ) ) );
1299 if ( isset( $def["functions"] ) )
1300 $attrs = array_unique( array_merge( $attrs, array_keys( $def["functions"] ) ) );
1301 return $attrs;
1302 }
1303
1310 public function hasAttribute( $attr )
1311 {
1312 $def = $this->definition();
1313 $has_attr = isset( $def["fields"][$attr] );
1314 if ( !$has_attr and isset( $def["function_attributes"] ) )
1315 $has_attr = isset( $def["function_attributes"][$attr] );
1316 if ( !$has_attr and isset( $def["functions"] ) )
1317 $has_attr = isset( $def["functions"][$attr] );
1318 return $has_attr;
1319 }
1320
1330 public function attribute( $attr, $noFunction = false )
1331 {
1332 $def = $this->definition();
1333 $attrFunctions = isset( $def["function_attributes"] ) ? $def["function_attributes"] : null;
1334 if ( $noFunction === false && isset( $attrFunctions[$attr] ) )
1335 {
1336 $functionName = $attrFunctions[$attr];
1337 if ( method_exists( $this, $functionName ) )
1338 {
1339 return $this->$functionName();
1340 }
1341
1342 eZDebug::writeError( 'Could not find function : "' . get_class( $this ) . '::' . $functionName . '()".', __METHOD__ );
1343 return null;
1344 }
1345
1346 $fields = $def["fields"];
1347 if ( isset( $fields[$attr] ) )
1348 {
1349 $attrName = $fields[$attr];
1350 if ( isset( $attrName['name'] ) )
1351 {
1352 $attrName = $attrName['name'];
1353 }
1354 return $this->$attrName;
1355 }
1356
1357 if ( isset( $def["functions"][$attr] ) )
1358 {
1359 return $this->{$def["functions"][$attr]}();
1360 }
1361
1362 eZDebug::writeError( "Attribute '$attr' does not exist", $def['class_name'] . '::attribute' );
1363 return null;
1364 }
1365
1375 public function setAttribute( $attr, $val )
1376 {
1377 $def = $this->definition();
1378 $fields = $def["fields"];
1379 $functions = isset( $def["set_functions"] ) ? $def["set_functions"] : null;
1380 if ( isset( $fields[$attr] ) )
1381 {
1382 $attrName = $fields[$attr];
1383 if ( is_array( $attrName ) )
1384 {
1385 $attrName = $attrName['name'];
1386 }
1387
1388 $oldValue = null;
1389 if ( isset( $this->$attrName ) )
1390 $oldValue = $this->$attrName;
1391 $this->$attrName = $val;
1392 if ( $oldValue === null ||
1393 $oldValue !== $val )
1394 $this->setHasDirtyData( true );
1395 }
1396 else if ( isset( $functions[$attr] ) )
1397 {
1398 $functionName = $functions[$attr];
1399 $oldValue = $this->$functionName( $val );
1400 if ( $oldValue === null or $oldValue !== $val )
1401 $this->setHasDirtyData( true );
1402 }
1403 else
1404 {
1405 eZDebug::writeError( "Undefined attribute '$attr', cannot set",
1406 $def['class_name'] );
1407 }
1408 }
1409
1415 public function hasDirtyData()
1416 {
1417 return $this->PersistentDataDirty;
1418 }
1419
1426 public function setHasDirtyData( $hasDirtyData )
1427 {
1428 $this->PersistentDataDirty = $hasDirtyData;
1429 }
1430
1439 public static function getShortAttributeName( $db, $def, $attrName )
1440 {
1441 $fields = $def['fields'];
1442
1443 if ( $db->useShortNames() && isset( $fields[$attrName] ) && array_key_exists( 'short_name', $fields[$attrName] ) && $fields[$attrName]['short_name'] )
1444 return $fields[$attrName]['short_name'];
1445
1446 return $attrName;
1447 }
1448
1449 public $ID;
1450 public $ContentObjectID;
1451 public $SectionID;
1452 public $OwnerID;
1453 public $ClassID;
1454 public $Published;
1455 public $Modified;
1456 public $CurrentVersion;
1457 public $Status;
1458 public $RemoteID;
1459 public $LanguageMask;
1460 public $InitialLanguageID;
1461 public $NodeID;
1462 public $ParentNodeID;
1463 public $MainNodeID;
1464 public $ContentObjectVersion;
1465 public $ContentObjectIsPublished;
1466 public $Depth;
1467 public $SortField;
1468 public $SortOrder;
1469 public $Priority;
1470 public $ModifiedSubNode;
1471 public $PathString;
1472 public $PathIdentificationString;
1473 public $IsHidden;
1474 public $IsInvisible;
1475 public $UserID;
1476 public $Name;
1477 public $Locale;
1478 public $Identifier;
1479 public $NavigationPartIdentifier;
1480 public $SerializedDescriptionList;
1481 public $URLAliasName;
1482 public $AlwaysAvailable;
1483 public $CanTranslate;
1484 public $DataInt1;
1485 public $DataInt2;
1486 public $DataInt3;
1487 public $DataInt4;
1488 public $DataFloat1;
1489 public $DataFloat2;
1490 public $DataFloat3;
1491 public $DataFloat4;
1492 public $DataText1;
1493 public $DataText2;
1494 public $DataText3;
1495 public $DataText4;
1496 public $DataText5;
1497 public $SerializedDataText;
1498 public $Category;
1499 public $PolicyID;
1500 public $Values;
1501 public $Created;
1502 public $CurrencyCode;
1503 public $ProductCollectionID;
1504 public $ItemCount;
1505 public $Price;
1506 public $IsVATIncluded;
1507 public $VATValue;
1508 public $DiscountValue;
1509 public $Email;
1510 public $IsTemporary;
1511 public $IsArchived;
1512 public $IsActive;
1513 public $OrderID;
1514 public $ModifierID;
1515 public $Code;
1516 public $Symbol;
1517 public $AutoRateValue;
1518 public $CustomRateValue;
1519 public $RateFactor;
1520 public $Main;
1521 public $MementoKey;
1522 public $MainKey;
1523 public $WorkflowEventPos;
1524 public $LanguageID;
1525 public $ContentObjectStateGroupID;
1526 public $contentobject_attribute_id;
1527 public $contentobject_id;
1528 public $rating;
1529 public $rating_count;
1530 public $rating_average;
1531 }
1532
1533 ?>
$i
$i
Definition: admin/adlist.php:91
$tmp
$tmp
Definition: bugview.php:99
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:546
eZPersistentObject
Definition: ezpersistentobject.php:34
eZPersistentObject\$Email
$Email
Definition: ezpersistentobject.php:1509
eZPersistentObject\$NodeID
$NodeID
Definition: ezpersistentobject.php:1461
eZPersistentObject\$PersistentDataDirty
$PersistentDataDirty
Definition: ezpersistentobject.php:42
eZPersistentObject\conditionTextByRow
static conditionTextByRow( $conditions, $row)
Definition: ezpersistentobject.php:644
eZPersistentObject\$DataInt2
$DataInt2
Definition: ezpersistentobject.php:1485
eZPersistentObject\newObjectOrder
static newObjectOrder( $def, $orderField, $conditions)
Definition: ezpersistentobject.php:1035
eZPersistentObject\updateObjectList
static updateObjectList( $parameters)
Definition: ezpersistentobject.php:1210
eZPersistentObject\$MainNodeID
$MainNodeID
Definition: ezpersistentobject.php:1463
eZPersistentObject\$LanguageMask
$LanguageMask
Definition: ezpersistentobject.php:1459
eZPersistentObject\$Values
$Values
Definition: ezpersistentobject.php:1500
eZPersistentObject\$ModifierID
$ModifierID
Definition: ezpersistentobject.php:1514
eZPersistentObject\$DataText4
$DataText4
Definition: ezpersistentobject.php:1495
eZPersistentObject\$DiscountValue
$DiscountValue
Definition: ezpersistentobject.php:1508
eZPersistentObject\$PathIdentificationString
$PathIdentificationString
Definition: ezpersistentobject.php:1472
eZPersistentObject\$LanguageID
$LanguageID
Definition: ezpersistentobject.php:1524
eZPersistentObject\$IsInvisible
$IsInvisible
Definition: ezpersistentobject.php:1474
eZPersistentObject\$DataFloat1
$DataFloat1
Definition: ezpersistentobject.php:1488
eZPersistentObject\hasAttribute
hasAttribute( $attr)
Definition: ezpersistentobject.php:1310
eZPersistentObject\fetchObject
static fetchObject( $def, $field_filters, $conds, $asObject=true, $grouping=null, $custom_fields=null)
Definition: ezpersistentobject.php:214
eZPersistentObject\$DataText2
$DataText2
Definition: ezpersistentobject.php:1493
eZPersistentObject\$CurrencyCode
$CurrencyCode
Definition: ezpersistentobject.php:1502
eZPersistentObject\$Symbol
$Symbol
Definition: ezpersistentobject.php:1516
eZPersistentObject\$ParentNodeID
$ParentNodeID
Definition: ezpersistentobject.php:1462
eZPersistentObject\$Depth
$Depth
Definition: ezpersistentobject.php:1466
eZPersistentObject\swapRow
static swapRow( $table, $keys, $order_id, $rows, $id1, $id2)
Definition: ezpersistentobject.php:1008
eZPersistentObject\$IsTemporary
$IsTemporary
Definition: ezpersistentobject.php:1510
eZPersistentObject\$AlwaysAvailable
$AlwaysAvailable
Definition: ezpersistentobject.php:1482
eZPersistentObject\__construct
__construct( $row=null)
Definition: ezpersistentobject.php:53
eZPersistentObject\$VATValue
$VATValue
Definition: ezpersistentobject.php:1507
eZPersistentObject\$Status
$Status
Definition: ezpersistentobject.php:1457
eZPersistentObject\$DataFloat2
$DataFloat2
Definition: ezpersistentobject.php:1489
eZPersistentObject\$IsArchived
$IsArchived
Definition: ezpersistentobject.php:1511
eZPersistentObject\$DataText3
$DataText3
Definition: ezpersistentobject.php:1494
eZPersistentObject\$MainKey
$MainKey
Definition: ezpersistentobject.php:1522
eZPersistentObject\$DataInt4
$DataInt4
Definition: ezpersistentobject.php:1487
eZPersistentObject\definition
static definition()
Definition: ezpersistentobject.php:1167
eZPersistentObject\$rating
$rating
Definition: ezpersistentobject.php:1528
eZPersistentObject\$ID
$ID
Definition: ezpersistentobject.php:1449
eZPersistentObject\store
store( $fieldFilters=null)
Definition: ezpersistentobject.php:305
eZPersistentObject\$OwnerID
$OwnerID
Definition: ezpersistentobject.php:1452
eZPersistentObject\escapeArray
static escapeArray( $array)
Definition: ezpersistentobject.php:1178
eZPersistentObject\$SortField
$SortField
Definition: ezpersistentobject.php:1467
eZPersistentObject\sync
sync( $fieldFilters=null)
Definition: ezpersistentobject.php:320
eZPersistentObject\$Priority
$Priority
Definition: ezpersistentobject.php:1469
eZPersistentObject\setHasDirtyData
setHasDirtyData( $hasDirtyData)
Definition: ezpersistentobject.php:1426
eZPersistentObject\$DataFloat4
$DataFloat4
Definition: ezpersistentobject.php:1491
eZPersistentObject\getShortAttributeName
static getShortAttributeName( $db, $def, $attrName)
Definition: ezpersistentobject.php:1439
eZPersistentObject\$InitialLanguageID
$InitialLanguageID
Definition: ezpersistentobject.php:1460
eZPersistentObject\$Published
$Published
Definition: ezpersistentobject.php:1454
eZPersistentObject\$PathString
$PathString
Definition: ezpersistentobject.php:1471
eZPersistentObject\reorderObject
static reorderObject( $def, $orderField, $conditions, $down=true)
Definition: ezpersistentobject.php:1070
eZPersistentObject\$AutoRateValue
$AutoRateValue
Definition: ezpersistentobject.php:1517
eZPersistentObject\$Identifier
$Identifier
Definition: ezpersistentobject.php:1478
eZPersistentObject\removeObject
static removeObject( $def, $conditions=null, $extraConditions=null)
Definition: ezpersistentobject.php:272
eZPersistentObject\$PolicyID
$PolicyID
Definition: ezpersistentobject.php:1499
eZPersistentObject\$UserID
$UserID
Definition: ezpersistentobject.php:1475
eZPersistentObject\conditionText
static conditionText( $conditions)
Definition: ezpersistentobject.php:629
eZPersistentObject\$CanTranslate
$CanTranslate
Definition: ezpersistentobject.php:1483
eZPersistentObject\$Locale
$Locale
Definition: ezpersistentobject.php:1477
eZPersistentObject\$DataText1
$DataText1
Definition: ezpersistentobject.php:1492
eZPersistentObject\$NavigationPartIdentifier
$NavigationPartIdentifier
Definition: ezpersistentobject.php:1479
eZPersistentObject\$ContentObjectStateGroupID
$ContentObjectStateGroupID
Definition: ezpersistentobject.php:1525
eZPersistentObject\$Code
$Code
Definition: ezpersistentobject.php:1515
eZPersistentObject\storeObject
static storeObject( $obj, $fieldFilters=null)
Definition: ezpersistentobject.php:339
eZPersistentObject\$IsVATIncluded
$IsVATIncluded
Definition: ezpersistentobject.php:1506
eZPersistentObject\$WorkflowEventPos
$WorkflowEventPos
Definition: ezpersistentobject.php:1523
eZPersistentObject\$rating_count
$rating_count
Definition: ezpersistentobject.php:1529
eZPersistentObject\$MementoKey
$MementoKey
Definition: ezpersistentobject.php:1521
eZPersistentObject\$ClassID
$ClassID
Definition: ezpersistentobject.php:1453
eZPersistentObject\$IsHidden
$IsHidden
Definition: ezpersistentobject.php:1473
eZPersistentObject\setAttribute
setAttribute( $attr, $val)
Definition: ezpersistentobject.php:1375
eZPersistentObject\$ItemCount
$ItemCount
Definition: ezpersistentobject.php:1504
eZPersistentObject\$DataInt1
$DataInt1
Definition: ezpersistentobject.php:1484
eZPersistentObject\fill
fill( $row)
Definition: ezpersistentobject.php:81
eZPersistentObject\$Main
$Main
Definition: ezpersistentobject.php:1520
eZPersistentObject\$OrderID
$OrderID
Definition: ezpersistentobject.php:1513
eZPersistentObject\handleRows
static handleRows( $rows, $class_name, $asObject)
Definition: ezpersistentobject.php:964
eZPersistentObject\$Created
$Created
Definition: ezpersistentobject.php:1501
eZPersistentObject\$DataInt3
$DataInt3
Definition: ezpersistentobject.php:1486
eZPersistentObject\$RemoteID
$RemoteID
Definition: ezpersistentobject.php:1458
eZPersistentObject\$ContentObjectVersion
$ContentObjectVersion
Definition: ezpersistentobject.php:1464
eZPersistentObject\$SortOrder
$SortOrder
Definition: ezpersistentobject.php:1468
eZPersistentObject\$Modified
$Modified
Definition: ezpersistentobject.php:1455
eZPersistentObject\$CurrentVersion
$CurrentVersion
Definition: ezpersistentobject.php:1456
eZPersistentObject\attributes
attributes()
Definition: ezpersistentobject.php:1293
eZPersistentObject\$contentobject_attribute_id
$contentobject_attribute_id
Definition: ezpersistentobject.php:1526
eZPersistentObject\$DataText5
$DataText5
Definition: ezpersistentobject.php:1496
eZPersistentObject\$CustomRateValue
$CustomRateValue
Definition: ezpersistentobject.php:1518
eZPersistentObject\$DataFloat3
$DataFloat3
Definition: ezpersistentobject.php:1490
eZPersistentObject\$RateFactor
$RateFactor
Definition: ezpersistentobject.php:1519
eZPersistentObject\$rating_average
$rating_average
Definition: ezpersistentobject.php:1530
eZPersistentObject\hasDirtyData
hasDirtyData()
Definition: ezpersistentobject.php:1415
eZPersistentObject\count
static count( $def, $conds=null, $field=null)
Definition: ezpersistentobject.php:188
eZPersistentObject\$ProductCollectionID
$ProductCollectionID
Definition: ezpersistentobject.php:1503
eZPersistentObject\$ContentObjectIsPublished
$ContentObjectIsPublished
Definition: ezpersistentobject.php:1465
eZPersistentObject\$Price
$Price
Definition: ezpersistentobject.php:1505
eZPersistentObject\attribute
attribute( $attr, $noFunction=false)
Definition: ezpersistentobject.php:1330
eZPersistentObject\$ModifiedSubNode
$ModifiedSubNode
Definition: ezpersistentobject.php:1470
eZPersistentObject\$SectionID
$SectionID
Definition: ezpersistentobject.php:1451
eZPersistentObject\replaceFieldsWithShortNames
static replaceFieldsWithShortNames(eZDBInterface $db, array $fieldDefs, &$fields)
Definition: ezpersistentobject.php:111
eZPersistentObject\$SerializedDataText
$SerializedDataText
Definition: ezpersistentobject.php:1497
eZPersistentObject\$URLAliasName
$URLAliasName
Definition: ezpersistentobject.php:1481
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:807
eZPersistentObject\$contentobject_id
$contentobject_id
Definition: ezpersistentobject.php:1527
eZPersistentObject\$SerializedDescriptionList
$SerializedDescriptionList
Definition: ezpersistentobject.php:1480
eZPersistentObject\$Category
$Category
Definition: ezpersistentobject.php:1498
eZPersistentObject\$IsActive
$IsActive
Definition: ezpersistentobject.php:1512
eZPersistentObject\$ContentObjectID
$ContentObjectID
Definition: ezpersistentobject.php:1450
eZPersistentObject\replaceFieldsWithLongNames
static replaceFieldsWithLongNames(eZDBInterface $db, array $fieldDefs, &$fields)
Definition: ezpersistentobject.php:155
eZPersistentObject\eZPersistentObject
eZPersistentObject( $row=null)
Definition: ezpersistentobject.php:65
eZPersistentObject\$Name
$Name
Definition: ezpersistentobject.php:1476
$item
$item
Definition: classes/admin/linklist.php:228
$id
$id
Definition: companytypelist.php:193
$query
if(isset( $Year) &&!is_numeric( $Year)||isset( $Month) &&!is_numeric( $Month)) $query
Definition: entryexitpages.php:65
$db
$db
Definition: ezarticle/user/authorlist.php:53
$row
$row
Definition: ezcalendar/user/dayview.php:403
$j
$j
Definition: ezimagecatalogue/admin/imagelist.php:345
$limit
$limit
Definition: ezimagecatalogue/admin/imagelist.php:312
$def
$def
Definition: ezprestauthorizedclient.php:12
$rows
$rows
Definition: frontpage.php:128
$field
$field
Definition: listtable.php:41
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.