00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 include_once( "lib/ezdb/classes/ezdb.php" );
00059 include_once( "lib/ezutils/classes/ezdebug.php" );
00060
00061 class eZPersistentObject
00062 {
00063
00064
00065
00066
00067
00068
00069 function eZPersistentObject( $row )
00070 {
00071 $this->PersistentDataDirty = false;
00072 if ( is_numeric( $row ) )
00073 $row = $this->fetch( $row, false );
00074 $this->fill( $row );
00075 }
00076
00077
00078
00079
00080
00081
00082
00083 function fill( &$row )
00084 {
00085 if ( $row == false )
00086 return;
00087 $def = $this->definition();
00088 $fields =& $def["fields"];
00089
00090 foreach ( $fields as $key => $value )
00091 {
00092 $item = $fields[$key];
00093 if ( is_array( $item ) )
00094 {
00095 $item = $item['name'];
00096 }
00097 $this->$item =& $row[$key];
00098 }
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108 function replaceFieldsWithShortNames( &$db, &$fieldDefs, &$fields )
00109 {
00110 if ( !$db->useShortNames() || !$fields )
00111 return;
00112
00113 $short_fields_names = array();
00114 foreach ( $fields as $key => $val )
00115 {
00116 if( is_numeric( $key ) )
00117 {
00118 if ( array_key_exists( $val, $fieldDefs ) &&
00119 array_key_exists( 'short_name', $fieldDefs[$val] ) )
00120 {
00121 $short_fields_names[$key] = $fieldDefs[$val]['short_name'];
00122 }
00123 else
00124 $short_fields_names[$key] = $val;
00125 }
00126 else
00127 {
00128 if ( array_key_exists( $key, $fieldDefs ) &&
00129 array_key_exists( 'short_name', $fieldDefs[$key] ) )
00130 {
00131 $newkey = $fieldDefs[$key]['short_name'];
00132 }
00133 else
00134 $newkey = $key;
00135 $short_fields_names[$newkey] = $val;
00136 }
00137
00138 }
00139 $fields = $short_fields_names;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 function fetchObject(
00150 &$def,
00151
00152 $field_filters,
00153
00154 $conds,
00155 $asObject = true,
00156
00157 $grouping = null,
00158
00159 $custom_fields = null )
00160 {
00161 $rows = eZPersistentObject::fetchObjectList( $def, $field_filters, $conds,
00162 array(), null, $asObject,
00163 $grouping, $custom_fields );
00164 if ( $rows )
00165 return $rows[0];
00166 return null;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 function remove( $conditions = null, $extraConditions = null )
00179 {
00180 $def = $this->definition();
00181 $keys =& $def["keys"];
00182 if ( !is_array( $conditions ) )
00183 {
00184 $conditions = array();
00185 foreach ( $keys as $key )
00186 {
00187 $value =& $this->attribute( $key );
00188 $conditions[$key] =& $value;
00189 }
00190 }
00191 eZPersistentObject::removeObject( $def, $conditions, $extraConditions );
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 function removeObject( &$def, $conditions = null, $extraConditions = null )
00203 {
00204 $db =& eZDB::instance();
00205
00206 $table =& $def["name"];
00207 if ( is_array( $extraConditions ) )
00208 {
00209 foreach ( $extraConditions as $key => $cond )
00210 {
00211 $conditions[$key] = $cond;
00212 }
00213 }
00214
00215
00216
00217
00218 $fields =& $def['fields'];
00219 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $conditions );
00220
00221 $cond_text = eZPersistentObject::conditionText( $conditions );
00222
00223 $db->query( "DELETE FROM $table $cond_text" );
00224 }
00225
00226
00227
00228
00229
00230
00231
00232 function store( $fieldFilters = null )
00233 {
00234 eZPersistentObject::storeObject( $this, $fieldFilters );
00235 }
00236
00237
00238
00239
00240
00241
00242
00243 function sync( $fieldFilters = null )
00244 {
00245 if ( $this->hasDirtyData() )
00246 $this->store( $fieldFilters );
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256 function storeObject( &$obj, $fieldFilters = null )
00257 {
00258 $db =& eZDB::instance();
00259 $useFieldFilters = ( isset( $fieldFilters ) && is_array( $fieldFilters ) && $fieldFilters );
00260
00261 $def = $obj->definition();
00262 $fields =& $def["fields"];
00263 $keys =& $def["keys"];
00264 $table =& $def["name"];
00265 $relations =& $def["relations"];
00266 $insert_object = false;
00267 $exclude_fields = array();
00268 foreach ( $keys as $key )
00269 {
00270 $value =& $obj->attribute( $key );
00271 if ( is_null( $value ) )
00272 {
00273 $insert_object = true;
00274 $exclude_fields[] = $key;
00275 }
00276 }
00277
00278 if ( $useFieldFilters )
00279 $insert_object = false;
00280
00281 $use_fields = array_diff( array_keys( $fields ), $exclude_fields );
00282
00283 if ( is_array( $fieldFilters ) )
00284 $use_fields = array_intersect( $use_fields, $fieldFilters );
00285 $doNotEscapeFields = array();
00286 $changedValueFields = array();
00287 $numericDataTypes = array( 'integer', 'float', 'double' );
00288
00289 foreach ( $use_fields as $field_name )
00290 {
00291 $field_def = $fields[$field_name];
00292 $value =& $obj->attribute( $field_name );
00293
00294 if ( is_null( $value ) )
00295 {
00296 if ( ! is_array( $field_def ) )
00297 {
00298 $exclude_fields[] = $field_name;
00299 }
00300 else
00301 {
00302 if ( array_key_exists( 'default', $field_def ) &&
00303 (! is_null( $field_def['default'] ) ||
00304 ( $field_name == 'data_int' &&
00305 array_key_exists( 'required', $field_def ) &&
00306 $field_def[ 'required' ] == false ) ) )
00307 {
00308 $obj->setAttribute( $field_name, $field_def[ 'default' ] );
00309 }
00310 else
00311 {
00312
00313 $exclude_fields[] = $field_name;
00314 }
00315 }
00316 }
00317
00318 if ( strlen( $value ) == 0 &&
00319 is_array( $field_def ) &&
00320 in_array( $field_def['datatype'], $numericDataTypes ) &&
00321 array_key_exists( 'default', $field_def ) &&
00322 ( is_null( $field_def[ 'default' ] ) || is_numeric( $field_def[ 'default' ] ) ) )
00323 {
00324 $obj->setAttribute( $field_name, $field_def[ 'default' ] );
00325 }
00326
00327 if ( !is_null( $value ) &&
00328 $field_def['datatype'] === 'string' &&
00329 array_key_exists( 'max_length', $field_def ) &&
00330 $field_def['max_length'] > 0 &&
00331 strlen( $value ) > $field_def['max_length'] )
00332 {
00333 $obj->setAttribute( $field_name, substr( $value, 0, $field_def['max_length'] ) );
00334 eZDebug::writeDebug( $value, "truncation of $field_name to max_length=". $field_def['max_length'] );
00335 }
00336 $bindDataTypes = array( 'text' );
00337 if ( $db->bindingType() != EZ_DB_BINDING_NO &&
00338 strlen( $value ) > 2000 &&
00339 is_array( $field_def ) &&
00340 in_array( $field_def['datatype'], $bindDataTypes )
00341 )
00342 {
00343 $boundValue = $db->bindVariable( $value, $field_def );
00344
00345 $doNotEscapeFields[] = $field_name;
00346 $changedValueFields[$field_name] = $boundValue;
00347 }
00348
00349 }
00350 $key_conds = array();
00351 foreach ( $keys as $key )
00352 {
00353 $value =& $obj->attribute( $key );
00354 $key_conds[$key] = $value;
00355 }
00356 unset( $value );
00357
00358 $important_keys = $keys;
00359 if ( is_array( $relations ) )
00360 {
00361
00362 foreach( $relations as $relation => $relation_data )
00363 {
00364 if ( !in_array( $relation, $keys ) )
00365 $important_keys[] = $relation;
00366 }
00367 }
00368 if ( count( $important_keys ) == 0 && !$useFieldFilters )
00369 {
00370 $insert_object = true;
00371 }
00372 else if ( !$insert_object )
00373 {
00374 $rows = eZPersistentObject::fetchObjectList( $def, $keys, $key_conds,
00375 array(), null, false,
00376 null, null );
00377 if ( count( $rows ) == 0 )
00378 {
00379
00380
00381
00382 if ( $useFieldFilters )
00383 return;
00384
00385 $insert_object = true;
00386 }
00387 }
00388
00389 if ( $insert_object )
00390 {
00391
00392 require_once( 'lib/compat.php' );
00393
00394
00395
00396 $use_fields = array_diff( array_keys( $fields ), $exclude_fields );
00397 $use_field_names = $use_fields;
00398 if ( $db->useShortNames() )
00399 {
00400 $use_short_field_names = $use_field_names;
00401 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $use_short_field_names );
00402 $field_text = implode( ', ', $use_short_field_names );
00403 unset( $use_short_field_names );
00404 }
00405 else
00406 $field_text = implode( ', ', $use_field_names );
00407
00408 $use_values_hash = array();
00409 $escapeFields = array_diff( $use_fields, $doNotEscapeFields );
00410
00411 foreach ( $escapeFields as $key )
00412 {
00413 $value = $obj->attribute( $key );
00414 $field_def = $fields[$key];
00415
00416 if ( $field_def['datatype'] == 'float' || $field_def['datatype'] == 'double' )
00417 {
00418 if ( is_null( $value ) )
00419 {
00420 $use_values_hash[$key] = 'NULL';
00421 }
00422 else
00423 {
00424 $use_values_hash[$key] = ezsprintf( '%F', $value );
00425 }
00426 }
00427 else if ( $field_def['datatype'] == 'int' || $field_def['datatype'] == 'integer' )
00428 {
00429 if ( is_null( $value ) )
00430 {
00431 $use_values_hash[$key] = 'NULL';
00432 }
00433 else
00434 {
00435 $use_values_hash[$key] = ezsprintf( '%d', $value );
00436 }
00437 }
00438 else
00439 {
00440
00441
00442
00443 $use_values_hash[$key] = "'" . $db->escapeString( $value ) . "'";
00444 }
00445 }
00446 foreach ( $doNotEscapeFields as $key )
00447 {
00448 $value =& $changedValueFields[$key];
00449 $use_values_hash[$key] = $value;
00450 }
00451 $use_values = array();
00452 foreach ( $use_field_names as $field )
00453 $use_values[] = $use_values_hash[$field];
00454 unset( $use_values_hash );
00455 $value_text = implode( ", ", $use_values );
00456
00457 $sql = "INSERT INTO $table ($field_text) VALUES($value_text)";
00458 $db->query( $sql );
00459
00460 if ( isset( $def["increment_key"] ) &&
00461 is_string( $def["increment_key"] ) &&
00462 !( $obj->attribute( $def["increment_key"] ) > 0 ) )
00463 {
00464 $inc =& $def["increment_key"];
00465 $id = $db->lastSerialID( $table, $inc );
00466 if ( $id !== false )
00467 $obj->setAttribute( $inc, $id );
00468 }
00469 }
00470 else
00471 {
00472 $use_fields = array_diff( array_keys( $fields ), array_merge( $keys, $exclude_fields ) );
00473 if ( count( $use_fields ) > 0 )
00474 {
00475
00476 require_once( 'lib/compat.php' );
00477
00478
00479 if ( is_array( $fieldFilters ) )
00480 $use_fields = array_intersect( $use_fields, $fieldFilters );
00481 $use_field_names = array();
00482 foreach ( $use_fields as $key )
00483 {
00484 if ( $db->useShortNames() && is_array( $fields[$key] ) && array_key_exists( 'short_name', $fields[$key] ) && strlen( $fields[$key]['short_name'] ) > 0 )
00485 $use_field_names[$key] = $fields[$key]['short_name'];
00486 else
00487 $use_field_names[$key] = $key;
00488 }
00489
00490 $field_text = "";
00491 $field_text_len = 0;
00492 $i = 0;
00493
00494
00495 foreach ( $use_fields as $key )
00496 {
00497 $value = $obj->attribute( $key );
00498
00499 if ( $fields[$key]['datatype'] == 'float' || $fields[$key]['datatype'] == 'double' )
00500 {
00501 if (is_null($value))
00502 $field_text_entry = $use_field_names[$key] . '=NULL';
00503 else
00504 $field_text_entry = $use_field_names[$key] . "=" . ezsprintf( '%F', $value );
00505 }
00506 else if ($fields[$key]['datatype'] == 'int' || $fields[$key]['datatype'] == 'integer' )
00507 {
00508 if (is_null($value))
00509 $field_text_entry = $use_field_names[$key] . '=NULL';
00510 else
00511 $field_text_entry = $use_field_names[$key] . "=" . ezsprintf( '%d', $value );
00512 }
00513 else if ( in_array( $use_field_names[$key], $doNotEscapeFields ) )
00514 {
00515 $field_text_entry = $use_field_names[$key] . "=" . $changedValueFields[$key];
00516 }
00517 else
00518 {
00519 $field_text_entry = $use_field_names[$key] . "='" . $db->escapeString( $value ) . "'";
00520 }
00521
00522 $field_text_len += strlen( $field_text_entry );
00523 $needNewline = false;
00524 if ( $field_text_len > 60 )
00525 {
00526 $needNewline = true;
00527 $field_text_len = 0;
00528 }
00529 if ( $i > 0 )
00530 $field_text .= "," . ($needNewline ? "\n " : ' ');
00531 $field_text .= $field_text_entry;
00532 ++$i;
00533 }
00534 $cond_text = eZPersistentObject::conditionText( $key_conds );
00535 $sql = "UPDATE $table\nSET $field_text$cond_text";
00536 $db->query( $sql );
00537 }
00538 }
00539 $obj->setHasDirtyData( false );
00540 }
00541
00542
00543
00544
00545 function conditionText( &$conditions )
00546 {
00547 $row = null;
00548 return eZPersistentObject::conditionTextByRow( $conditions, $row );
00549 }
00550
00551
00552
00553
00554
00555 function &conditionTextByRow( &$conditions, &$row )
00556 {
00557 $db =& eZDB::instance();
00558
00559 $where_text = "";
00560 if ( is_array( $conditions ) and
00561 count( $conditions ) > 0 )
00562 {
00563 $where_text = "\nWHERE ";
00564 $i = 0;
00565 foreach ( $conditions as $id => $cond )
00566 {
00567 if ( $i > 0 )
00568 $where_text .= " AND ";
00569 if ( is_array( $row ) )
00570 {
00571 $where_text .= $cond . "='" . $db->escapeString( $row[$cond] ) . "'";
00572 }
00573 else
00574 {
00575 if ( is_array( $cond ) )
00576 {
00577 if ( is_array( $cond[0] ) )
00578 {
00579 $where_text .= $id . ' IN ( ';
00580 $j = 0;
00581 foreach ( $cond[0] as $value )
00582 {
00583 if ( $j > 0 )
00584 $where_text .= ", ";
00585 $where_text .= "'" . $db->escapeString( $value ) . "'";
00586 ++$j;
00587 }
00588 $where_text .= ' ) ';
00589 }
00590 else if ( is_array( $cond[1] ) )
00591 {
00592 $range = $cond[1];
00593 $where_text .= "$id BETWEEN '" . $db->escapeString( $range[0] ) . "' AND '" . $db->escapeString( $range[1] ) . "'";
00594 }
00595 else
00596 {
00597 switch ( $cond[0] )
00598 {
00599 case '>=':
00600 case '<=':
00601 case '<':
00602 case '>':
00603 case '=':
00604 case '<>':
00605 case '!=':
00606 case 'like':
00607 {
00608 $where_text .= $db->escapeString( $id ) . " " . $cond[0] . " '" . $db->escapeString( $cond[1] ) . "'";
00609 } break;
00610 default:
00611 {
00612 eZDebug::writeError( "Conditional operator '$cond[0]' is not supported.",'eZPersistentObject::conditionTextByRow()' );
00613 } break;
00614 }
00615
00616 }
00617 }
00618 else
00619 $where_text .= $db->escapeString( $id ) . "='" . $db->escapeString( $cond ) . "'";
00620 }
00621 ++$i;
00622 }
00623 }
00624 return $where_text;
00625 }
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728 function fetchObjectList( &$def,
00729 $field_filters = null,
00730 $conds = null,
00731 $sorts = null,
00732 $limit = null,
00733 $asObject = true,
00734 $grouping = false,
00735 $custom_fields = null,
00736 $custom_tables = null,
00737 $custom_conds = null )
00738 {
00739 $db =& eZDB::instance();
00740 $fields =& $def["fields"];
00741 $tables =& $def["name"];
00742 $class_name =& $def["class_name"];
00743 if ( is_array( $custom_tables ) )
00744 {
00745 foreach( $custom_tables as $custom_table )
00746 $tables .= ', ' . $db->escapeString( $custom_table );
00747 }
00748 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $conds );
00749 if ( is_array( $field_filters ) )
00750 $field_array = array_unique( array_intersect(
00751 $field_filters, array_keys( $fields ) ) );
00752 else
00753 $field_array = array_keys( $fields );
00754 if ( $custom_fields !== null and is_array( $custom_fields ) )
00755 {
00756 foreach( $custom_fields as $custom_field )
00757 {
00758 if ( is_array( $custom_field ) )
00759 {
00760 $custom_text = $custom_field["operation"];
00761 if ( isset( $custom_field["name"] ) )
00762 {
00763 $field_name =& $custom_field["name"];
00764 $custom_text .= " AS $field_name";
00765 }
00766 }
00767 else
00768 {
00769 $custom_text = $custom_field;
00770 }
00771 $field_array[] = $custom_text;
00772 }
00773 }
00774 eZPersistentObject::replaceFieldsWithShortNames( $db, $fields, $field_array );
00775 $field_text = '';
00776 $i = 0;
00777 foreach ( $field_array as $field_item )
00778 {
00779 if ( ( $i % 7 ) == 0 and
00780 $i > 0 )
00781 $field_text .= ",\n ";
00782 else if ( $i > 0 )
00783 $field_text .= ', ';
00784 $field_text .= $field_item;
00785 ++$i;
00786 }
00787
00788 $where_text = eZPersistentObject::conditionText( $conds );
00789 if ( $custom_conds )
00790 $where_text .= $custom_conds;
00791
00792 $sort_text = "";
00793 if ( $sorts !== false and ( isset( $def["sort"] ) or is_array( $sorts ) ) )
00794 {
00795 $sort_list =& $def["sort"];
00796 if ( is_array( $sorts ) )
00797 $sort_list =& $sorts;
00798 if ( count( $sort_list ) > 0 )
00799 {
00800 $sort_text = "\nORDER BY ";
00801 $i = 0;
00802 foreach ( $sort_list as $sort_id => $sort_type )
00803 {
00804 if ( $i > 0 )
00805 $sort_text .= ", ";
00806 if ( $sort_type == "desc" )
00807 $sort_text .= "$sort_id DESC";
00808 else
00809 $sort_text .= "$sort_id ASC";
00810 ++$i;
00811 }
00812 }
00813 }
00814
00815 $grouping_text = "";
00816 if ( isset( $def["grouping"] ) or ( is_array( $grouping ) and count( $grouping ) > 0 ) )
00817 {
00818 $grouping_list =& $def["grouping"];
00819 if ( is_array( $grouping ) )
00820 $grouping_list =& $grouping;
00821 if ( count( $grouping_list ) > 0 )
00822 {
00823 $grouping_text = "\nGROUP BY ";
00824 $i = 0;
00825 foreach ( $grouping_list as $grouping_id )
00826 {
00827 if ( $i > 0 )
00828 $grouping_text .= ", ";
00829 $grouping_text .= "$grouping_id";
00830 ++$i;
00831 }
00832 }
00833 }
00834
00835 $db_params = array();
00836 if ( is_array( $limit ) )
00837 {
00838 if ( isset( $limit["offset"] ) )
00839 {
00840 $db_params["offset"] = $limit["offset"];
00841 }
00842 if ( isset( $limit['limit'] ) )
00843 {
00844 $db_params["limit"] = $limit["limit"];
00845 }
00846 else
00847 {
00848 $db_params["limit"] = $limit["length"];
00849 }
00850 }
00851
00852 $sqlText = "SELECT $field_text
00853 FROM $tables" .
00854 $where_text .
00855 $grouping_text .
00856 $sort_text;
00857 $rows = $db->arrayQuery( $sqlText,
00858 $db_params );
00859
00860
00861 if ( $rows === false )
00862 return null;
00863
00864 $objectList = eZPersistentObject::handleRows( $rows, $class_name, $asObject );
00865 return $objectList;
00866 }
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876 function handleRows( &$rows, $class_name, $asObject )
00877 {
00878 if ( $asObject )
00879 {
00880 $objects = array();
00881 if ( is_array( $rows ) )
00882 {
00883 foreach ( $rows as $row )
00884 {
00885 $objects[] = new $class_name( $row );
00886 }
00887 }
00888 return $objects;
00889 }
00890 else
00891 return $rows;
00892 }
00893
00894
00895
00896
00897
00898
00899 function swapRow( $table, &$keys, &$order_id, &$rows, $id1, $id2 )
00900 {
00901 $db =& eZDB::instance();
00902 $text = $order_id . "='" . $db->escapeString( $rows[$id1][$order_id] ) . "' WHERE ";
00903 $i = 0;
00904 foreach ( $keys as $key )
00905 {
00906 if ( $i > 0 )
00907 $text .= " AND ";
00908 $text .= $key . "='" . $db->escapeString( $rows[$id2][$key] ) . "'";
00909 ++$i;
00910 }
00911 return "UPDATE $table SET $text";
00912 }
00913
00914
00915
00916
00917
00918
00919 function newObjectOrder( &$def, $orderField, $conditions )
00920 {
00921 $db =& eZDB::instance();
00922 $table =& $def["name"];
00923 $keys =& $def["keys"];
00924 $cond_text = eZPersistentObject::conditionText( $conditions );
00925 $rows = $db->arrayQuery( "SELECT MAX($orderField) AS $orderField FROM $table $cond_text" );
00926 if ( count( $rows ) > 0 and isset( $rows[0][$orderField] ) )
00927 return $rows[0][$orderField] + 1;
00928 else
00929 return 1;
00930 }
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944 function reorderObject( &$def,
00945
00946 $orderField,
00947 $conditions,
00948 $down = true )
00949 {
00950 $db =& eZDB::instance();
00951 $table =& $def["name"];
00952 $keys =& $def["keys"];
00953
00954 reset( $orderField );
00955 $order_id = key( $orderField );
00956 $order_val = $orderField[$order_id];
00957 if ( $down )
00958 {
00959 $order_operator = ">=";
00960 $order_type = "asc";
00961 $order_add = -1;
00962 }
00963 else
00964 {
00965 $order_operator = "<=";
00966 $order_type = "desc";
00967 $order_add = 1;
00968 }
00969 $fields = array_merge( $keys, array( $order_id ) );
00970 $rows = eZPersistentObject::fetchObjectList( $def,
00971 $fields,
00972 array_merge( $conditions,
00973 array( $order_id => array( $order_operator,
00974 $order_val ) ) ),
00975 array( $order_id => $order_type ),
00976 array( "length" => 2 ),
00977 false );
00978 if ( count( $rows ) == 2 )
00979 {
00980 $swapSQL1 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 1, 0 );
00981 $swapSQL2 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 0, 1 );
00982 $db->begin();
00983 $db->query( $swapSQL1 );
00984 $db->query( $swapSQL2 );
00985 $db->commit();
00986 }
00987 else
00988 {
00989 $tmp = eZPersistentObject::fetchObjectList( $def,
00990 $fields,
00991 $conditions,
00992 array( $order_id => $order_type ),
00993 array( "length" => 1 ),
00994 false );
00995 $where_text = eZPersistentObject::conditionTextByRow( $keys, $rows[0] );
00996 $db->query( "UPDATE $table SET $order_id='" . ( $tmp[0][$order_id] + $order_add ) .
00997 "'$where_text" );
00998 }
00999 }
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036 function definition()
01037 {
01038 return array();
01039 }
01040
01041 function &escapeArray( &$array )
01042 {
01043 $db =& eZDB::instance();
01044 $out = array();
01045 foreach( $array as $key => $value )
01046 {
01047 if ( is_array( $value ) )
01048 {
01049 $tmp = array();
01050 foreach( $value as $valueItem )
01051 {
01052 $tmp[] = $db->escapeString( $valueItem );
01053 }
01054 $out[$key] = $tmp;
01055 unset( $tmp );
01056 }
01057 else
01058 $out[$key] = $db->escapeString( $value );
01059 }
01060 return $out;
01061 }
01062
01063
01064
01065
01066
01067 function updateObjectList( $parameters )
01068 {
01069 $db =& eZDB::instance();
01070 $def =& $parameters['definition'];
01071 $table =& $def['name'];
01072 $fields =& $def['fields'];
01073 $keys =& $def['keys'];
01074
01075 $updateFields =& $parameters['update_fields'];
01076 $conditions =& $parameters['conditions'];
01077
01078 $query = "UPDATE $table SET ";
01079 $i = 0;
01080 $valueBound = false;
01081
01082 foreach( $updateFields as $field => $value )
01083 {
01084 $fieldDef =& $fields[ $field ];
01085 $numericDataTypes = array( 'integer', 'float', 'double' );
01086 if ( strlen( $value ) == 0 &&
01087 is_array( $fieldDef ) &&
01088 in_array( $fieldDef['datatype'], $numericDataTypes ) &&
01089 array_key_exists( 'default', $fieldDef ) &&
01090 !is_null( $fieldDef[ 'default' ] ) )
01091 {
01092 $value=$fieldDef[ 'default' ];
01093 }
01094
01095 $bindDataTypes = array( 'text' );
01096 if ( $db->bindingType() != EZ_DB_BINDING_NO &&
01097 strlen( $value ) > 2000 &&
01098 is_array( $fieldDef ) &&
01099 in_array( $fieldDef['datatype'], $bindDataTypes )
01100 )
01101 {
01102 $value = $db->bindVariable( $value, $fieldDef );
01103 $valueBound = true;
01104 }
01105 else
01106 $valueBound = false;
01107
01108 if ( $i > 0 )
01109 $query .= ', ';
01110 if ( $valueBound )
01111 $query .= $field . "=" . $value;
01112 else
01113 $query .= $field . "='" . $db->escapeString( $value ) . "'";
01114 ++$i;
01115 }
01116 $query .= "\n" . 'WHERE ';
01117 $i = 0;
01118 foreach( $conditions as $conditionKey => $condition )
01119 {
01120 if ( $i > 0 )
01121 $query .= ' AND ';
01122 if ( is_array( $condition ) )
01123 {
01124 $query .= $conditionKey . ' IN (';
01125 $j = 0;
01126 foreach( $condition as $conditionValue )
01127 {
01128 if ( $j > 0 )
01129 $query .= ', ';
01130 $query .= "'" . $db->escapeString( $conditionValue ) . "'";
01131 ++$j;
01132 }
01133 $query .= ')';
01134 }
01135 else
01136 $query .= $conditionKey . "='" . $db->escapeString( $condition ) . "'";
01137 ++$i;
01138 }
01139 $db->query( $query );
01140 }
01141
01142
01143
01144
01145
01146 function attributes()
01147 {
01148 $def = $this->definition();
01149 $attrs = array_keys( $def["fields"] );
01150 if ( isset( $def["function_attributes"] ) )
01151 $attrs = array_unique( array_merge( $attrs, array_keys( $def["function_attributes"] ) ) );
01152 if ( isset( $def["functions"] ) )
01153 $attrs = array_unique( array_merge( $attrs, array_keys( $def["functions"] ) ) );
01154 return $attrs;
01155 }
01156
01157
01158
01159
01160 function hasAttribute( $attr )
01161 {
01162 $def = $this->definition();
01163 $has_attr = isset( $def["fields"][$attr] );
01164 if ( !$has_attr and isset( $def["function_attributes"] ) )
01165 $has_attr = isset( $def["function_attributes"][$attr] );
01166 if ( !$has_attr and isset( $def["functions"] ) )
01167 $has_attr = isset( $def["functions"][$attr] );
01168 return $has_attr;
01169 }
01170
01171
01172
01173
01174
01175 function &attribute( $attr, $noFunction = false )
01176 {
01177 $def = $this->definition();
01178 $fields =& $def["fields"];
01179 $functions =& $def["functions"];
01180 $attrFunctions =& $def["function_attributes"];
01181 if ( $noFunction === false and isset( $attrFunctions[$attr] ) )
01182 {
01183 $functionName = $attrFunctions[$attr];
01184 $retVal = null;
01185 if ( method_exists( $this, $functionName ) )
01186 {
01187 $retVal =& $this->$functionName();
01188 }
01189 else
01190 {
01191 eZDebug::writeError( 'Could not find function : "' . get_class( $this ) . '::' . $functionName . '()".',
01192 'eZPersistentObject::attribute()' );
01193 }
01194 return $retVal;
01195 }
01196 else if ( isset( $fields[$attr] ) )
01197 {
01198 $attrName = $fields[$attr];
01199 if ( is_array( $attrName ) )
01200 {
01201 $attrName =& $attrName['name'];
01202 }
01203 return $this->$attrName;
01204 }
01205 else if ( isset( $functions[$attr] ) )
01206 {
01207 $functionName = $functions[$attr];
01208 return $this->$functionName();
01209 }
01210 else
01211 {
01212 eZDebug::writeError( "Attribute '$attr' does not exist", $def['class_name'] . '::attribute' );
01213 $attrValue = null;
01214 return $attrValue;
01215 }
01216 }
01217
01218
01219
01220
01221
01222 function setAttribute( $attr, $val )
01223 {
01224 $def = $this->definition();
01225 $fields =& $def["fields"];
01226 $functions =& $def["set_functions"];
01227 if ( isset( $fields[$attr] ) )
01228 {
01229 $attrName = $fields[$attr];
01230 if ( is_array( $attrName ) )
01231 {
01232 $attrName =& $attrName['name'];
01233 }
01234
01235 $oldValue = null;
01236 if ( isset( $this->$attrName ) )
01237 $oldValue = $this->$attrName;
01238 $this->$attrName = $val;
01239 if ( $oldValue === null or $oldValue !== $val )
01240 $this->setHasDirtyData( true );
01241 }
01242 else if ( isset( $functions[$attr] ) )
01243 {
01244 $functionName = $functions[$attr];
01245 $oldValue = $this->$functionName( $val );
01246 if ( $oldValue === null or $oldValue !== $val )
01247 $this->setHasDirtyData( true );
01248 }
01249 else
01250 {
01251 eZDebug::writeError( "Undefined attribute '$attr', cannot set",
01252 $def['class_name'] );
01253 }
01254 }
01255
01256
01257
01258
01259
01260 function hasDirtyData()
01261 {
01262 return $this->PersistentDataDirty;
01263 }
01264
01265
01266
01267
01268
01269 function setHasDirtyData( $hasDirtyData )
01270 {
01271 $this->PersistentDataDirty = $hasDirtyData;
01272 }
01273
01274
01275
01276
01277 function getShortAttributeName( &$db, &$def, $attrName )
01278 {
01279 $fields =& $def['fields'];
01280
01281 if ( $db->useShortNames() && isset( $fields[$attrName] ) && array_key_exists( 'short_name', $fields[$attrName] ) && $fields[$attrName]['short_name'] )
01282 return $fields[$attrName]['short_name'];
01283
01284 return $attrName;
01285 }
01286
01287
01288
01289 var $PersistentDataDirty;
01290 }
01291
01292 ?>