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
00064 include_once( "lib/ezdb/classes/ezdb.php" );
00065 include_once( "lib/ezutils/classes/eztexttool.php" );
00066 include_once( "lib/ezutils/classes/ezdebug.php" );
00067
eZPersistentObject
00069 {
eZPersistentObject( $row )
00077 {
00078 $this->PersistentDataDirty = false;
00079 if ( is_numeric( $row ) )
00080 $row =& $this->fetch( $row, false );
00081 $this->fill( $row );
00082 }
00083
fill( &$row )
00091 {
00092 if ( $row == false )
00093 return;
00094 $def =& $this->definition();
00095 $fields =& $def["fields"];
00096 reset( $fields );
00097 while ( ($key = key( $fields ) ) !== null )
00098 {
00099 $item =& $fields[$key];
00100 $this->$item =& $row[$key];
00101 next( $fields );
00102 }
00103 }
00104
fetchObject(
00111 &$def,
00113 $field_filters,
00115 $conds,
00116 $asObject = true,
00118 $grouping = null,
00120 $custom_fields = null )
00121 {
00122 $rows =& eZPersistentObject::fetchObjectList( $def, $field_filters, $conds,
00123 array(), null, $asObject,
00124 $grouping, $custom_fields );
00125 return $rows[0];
00126 }
00127
remove( $conditions = null, $extraConditions = null )
00136 {
00137 $def =& $this->definition();
00138 $keys =& $def["keys"];
00139 if ( !is_array( $conditions ) )
00140 {
00141 $conditions = array();
00142 foreach ( $keys as $key )
00143 {
00144 $value =& $this->attribute( $key );
00145 $conditions[$key] =& $value;
00146 }
00147 }
00148 eZPersistentObject::removeObject( $def, $conditions, $extraConditions );
00149 }
00150
removeObject( &$def, $conditions = null, $extraConditions = null )
00158 {
00159 $db =& eZDB::instance();
00160
00161 $table =& $def["name"];
00162 if ( is_array( $extraConditions ) )
00163 {
00164 foreach ( $extraConditions as $key => $cond )
00165 {
00166 $conditions[$key] = $cond;
00167 }
00168 }
00169 $cond_text = eZPersistentObject::conditionText( $conditions );
00170
00171 $db->query( "DELETE FROM $table $cond_text" );
00172 }
00173
store( $fieldFilters = null )
00179 {
00180 eZPersistentObject::storeObject( $this, $fieldFilters );
00181 }
00182
sync( $fieldFilters = null )
00188 {
00189 if ( $this->hasDirtyData() )
00190 $this->store( $fieldFilters );
00191 }
00192
storeObject( &$obj, $fieldFilters = null )
00199 {
00200 $db =& eZDB::instance();
00201
00202 $def =& $obj->definition();
00203 $fields =& $def["fields"];
00204 $keys =& $def["keys"];
00205 $table =& $def["name"];
00206 $relations =& $def["relations"];
00207 $insert_object = false;
00208 $exclude_fields = array();
00209 foreach ( $keys as $key )
00210 {
00211 $value =& $obj->attribute( $key );
00212 if ( is_null( $value ) )
00213 {
00214 $insert_object = true;
00215 $exclude_fields[] = $key;
00216 }
00217 }
00218 $key_conds = array();
00219 foreach ( $keys as $key )
00220 {
00221 $value =& $obj->attribute( $key );
00222 $key_conds[$key] = $value;
00223 }
00224 $important_keys = $keys;
00225 if ( is_array( $relations ) )
00226 {
00227 $important_keys = array();
00228 foreach( $relations as $relation => $relation_data )
00229 {
00230 if ( !in_array( $relation, $keys ) )
00231 $important_keys[] = $relation;
00232 }
00233 }
00234 if ( !$insert_object and count( $important_keys ) != 1 )
00235 {
00236 $rows =& eZPersistentObject::fetchObjectList( $def, $keys, $key_conds,
00237 array(), null, false,
00238 null, null );
00239 if ( count( $rows ) == 0 )
00240 $insert_object = true;
00241 }
00242 if ( $insert_object )
00243 {
00244 $use_fields = array_diff( array_keys( $fields ), $exclude_fields );
00245 $field_text = implode( ", ", $use_fields );
00246 $use_values = array();
00247 foreach ( $use_fields as $key )
00248 {
00249 $value =& $obj->attribute( $key );
00250 $use_values[] = "'" . $db->escapeString( $value ) . "'";
00251 }
00252 $value_text = implode( ", ", $use_values );
00253 $sql = "INSERT INTO $table ($field_text) VALUES($value_text)";
00254
00255 $db->query( $sql );
00256
00257 if ( isset( $def["increment_key"] ) && !($obj->attribute( $def["increment_key"]) > 0) )
00258 {
00259 $inc =& $def["increment_key"];
00260 $id = $db->lastSerialID( $table, $inc );
00261 if ( $id !== false )
00262 $obj->setAttribute( $inc, $id );
00263 }
00264 }
00265 else
00266 {
00267 $use_fields = array_diff( array_keys( $fields ), $keys );
00268 $field_text = "";
00269 $field_text_len = 0;
00270 $i = 0;
00271 foreach ( $use_fields as $key )
00272 {
00273 $value =& $obj->attribute( $key );
00274 $field_text_entry = $key . "='" . $db->escapeString( $value ) . "'";
00275 $field_text_len += strlen( $field_text_entry );
00276 $needNewline = false;
00277 if ( $field_text_len > 60 )
00278 {
00279 $needNewline = true;
00280 $field_text_len = 0;
00281 }
00282 if ( $i > 0 )
00283 $field_text .= "," . ($needNewline ? "\n " : ' ');
00284 $field_text .= $field_text_entry;
00285 ++$i;
00286 }
00287 $cond_text = eZPersistentObject::conditionText( $key_conds );
00288 $sql = "UPDATE $table\nSET $field_text$cond_text";
00289
00290 $db->query( $sql );
00291 }
00292 $obj->setHasDirtyData( false );
00293 }
00294
conditionText( &$conditions )
00299 {
00300 $row = null;
00301 return eZPersistentObject::conditionTextByRow( $conditions, $row );
00302 }
00303
conditionTextByRow( &$conditions, &$row )
00309 {
00310 $db =& eZDB::instance();
00311
00312 $where_text = "";
00313 if ( is_array( $conditions ) and
00314 count( $conditions ) > 0 )
00315 {
00316 $where_text = "\nWHERE ";
00317 $i = 0;
00318 foreach ( $conditions as $id => $cond )
00319 {
00320 if ( $i > 0 )
00321 $where_text .= " AND ";
00322 if ( is_array( $row ) )
00323 {
00324 $where_text .= $cond . "='" . $db->escapeString( $row[$cond] ) . "'";
00325 }
00326 else
00327 {
00328 if ( is_array( $cond ) )
00329 {
00330 if ( is_array( $cond[0] ) )
00331 {
00332 $where_text .= $id . ' IN ( ';
00333 $j = 0;
00334 foreach ( $cond[0] as $value )
00335 {
00336 if ( $j > 0 )
00337 $where_text .= ", ";
00338 $where_text .= "'" . $db->escapeString( $value ) . "'";
00339 ++$j;
00340 }
00341 $where_text .= ' ) ';
00342 }
00343 else if ( is_array( $cond[1] ) )
00344 {
00345 $range = $cond[1];
00346 $where_text .= "$id BETWEEN '" . $range[0] . "' AND '" . $range[1] . "'";
00347 }
00348 else
00349 $where_text .= $id . $cond[0] . "'" . $db->escapeString( $cond[1] ) . "'";
00350 }
00351 else
00352 $where_text .= $id . "='" . $db->escapeString( $cond ) . "'";
00353 }
00354 ++$i;
00355 }
00356 }
00357 return $where_text;
00358 }
00359
00364 function &fetchObjectList(
00365 &$def,
00367 $field_filters = null,
00369 $conds = null,
00371 $sorts = null,
00373 $limit = null,
00374 $asObject = true,
00376 $grouping = false,
00378 $custom_fields = null )
00379 {
00380 $db =& eZDB::instance();
00381 $fields =& $def["fields"];
00382 $table =& $def["name"];
00383 $class_name =& $def["class_name"];
00384 if ( is_array( $field_filters ) )
00385 $field_array = array_unique( array_intersect(
00386 $field_filters, array_keys( $fields ) ) );
00387 else
00388 $field_array = array_keys( $fields );
00389 if ( $custom_fields !== null and is_array( $custom_fields ) )
00390 {
00391 foreach( $custom_fields as $custom_field )
00392 {
00393 $custom_text = $custom_field["operation"];
00394 if ( isset( $custom_field["name"] ) )
00395 {
00396 $field_name =& $custom_field["name"];
00397 $custom_text .= " AS $field_name";
00398 }
00399 $field_array[] = $custom_text;
00400 }
00401 }
00402 $field_text = '';
00403 $i = 0;
00404 foreach ( $field_array as $field_item )
00405 {
00406 if ( ( $i % 7 ) == 0 and
00407 $i > 0 )
00408 $field_text .= ",\n ";
00409 else if ( $i > 0 )
00410 $field_text .= ', ';
00411 $field_text .= $field_item;
00412 ++$i;
00413 }
00414
00415 $where_text = eZPersistentObject::conditionText( $conds );
00416 $sort_text = "";
00417 if ( isset( $def["sort"] ) or is_array( $sorts ) )
00418 {
00419 $sort_list =& $def["sort"];
00420 if ( is_array( $sorts ) )
00421 $sort_list =& $sorts;
00422 if ( count( $sort_list ) > 0 )
00423 {
00424 $sort_text = "\nORDER BY ";
00425 $i = 0;
00426 foreach ( $sort_list as $sort_id => $sort_type )
00427 {
00428 if ( $i > 0 )
00429 $sort_text .= ", ";
00430 if ( $sort_type == "desc" )
00431 $sort_text .= "$sort_id DESC";
00432 else
00433 $sort_text .= "$sort_id ASC";
00434 ++$i;
00435 }
00436 }
00437 }
00438
00439 $grouping_text = "";
00440 if ( isset( $def["grouping"] ) or ( is_array( $grouping ) and count( $grouping) > 0 ) )
00441 {
00442 $grouping_list =& $def["grouping"];
00443 if ( is_array( $grouping ) )
00444 $grouping_list =& $grouping;
00445 if ( count( $grouping_list ) > 0 )
00446 {
00447 $grouping_text = "\nGROUP BY ";
00448 $i = 0;
00449 foreach ( $grouping_list as $grouping_id )
00450 {
00451 if ( $i > 0 )
00452 $grouping_text .= ", ";
00453 $grouping_text .= "$grouping_id";
00454 ++$i;
00455 }
00456 }
00457 }
00458
00459 $db_params = array();
00460 if ( is_array( $limit ) )
00461 {
00462 if ( isset( $limit["offset"] ) )
00463 {
00464 $db_params["offset"] = $limit["offset"];
00465 $db_params["limit"] = $limit["length"];
00466 }
00467 else
00468 {
00469 $db_params["limit"] = $limit["length"];
00470 }
00471 }
00472
00473 $sqlText = "SELECT $field_text\nFROM $table" . $where_text . $grouping_text . $sort_text;
00474 $rows =& $db->arrayQuery( $sqlText,
00475 $db_params );
00476
00477 return eZPersistentObject::handleRows( $rows, $class_name, $asObject );
00478 }
00479
00480 function &handleRows( &$rows, $class_name, $asObject )
00481 {
00482 if ( $asObject )
00483 {
00484 $objects = array();
00485 foreach ( $rows as $row )
00486 {
00487 $objects[] = new $class_name( $row );
00488 }
00489 return $objects;
00490 }
00491 else
00492 return $rows;
00493 }
00494
00498 function swapRow( $table, &$keys, &$order_id, &$rows, $id1, $id2 )
00499 {
00500 $db =& eZDB::instance();
00501 $text = $order_id . "='" . $db->escapeString( $rows[$id1][$order_id] ) . "' WHERE ";
00502 $i = 0;
00503 foreach ( $keys as $key )
00504 {
00505 if ( $i > 0 )
00506 $text .= " AND ";
00507 $text .= $key . "='" . $db->escapeString( $rows[$id2][$key] ) . "'";
00508 ++$i;
00509 }
00510 return "UPDATE $table SET $text";
00511 }
00512
00518 function newObjectOrder( &$def, $orderField, $conditions )
00519 {
00520 $db =& eZDB::instance();
00521 $table =& $def["name"];
00522 $keys =& $def["keys"];
00523 $cond_text = eZPersistentObject::conditionText( $conditions );
00524 $rows =& $db->arrayQuery( "SELECT MAX($orderField) AS $orderField FROM $table $cond_text" );
00525 if ( count( $rows ) > 0 and isset( $rows[0][$orderField] ) )
00526 return $rows[0][$orderField] + 1;
00527 else
00528 return 1;
00529 }
00530
00541 function reorderObject( &$def,
00543 $orderField,
00544 $conditions,
00545 $down = true )
00546 {
00547 $db =& eZDB::instance();
00548 $table =& $def["name"];
00549 $keys =& $def["keys"];
00550
00551 reset( $orderField );
00552 $order_id = key( $orderField );
00553 $order_val = $orderField[$order_id];
00554 if ( $down )
00555 {
00556 $order_operator = ">=";
00557 $order_type = "asc";
00558 $order_add = -1;
00559 }
00560 else
00561 {
00562 $order_operator = "<=";
00563 $order_type = "desc";
00564 $order_add = 1;
00565 }
00566 $fields = array_merge( $keys, array( $order_id ) );
00567 $rows =& eZPersistentObject::fetchObjectList( $def,
00568 $fields,
00569 array_merge( $conditions,
00570 array( $order_id => array( $order_operator,
00571 $order_val ) ) ),
00572 array( $order_id => $order_type ),
00573 array( "length" => 2 ),
00574 false );
00575 if ( count( $rows ) == 2 )
00576 {
00577 $swapSQL1 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 1, 0 );
00578 $swapSQL2 = eZPersistentObject::swapRow( $table, $keys, $order_id, $rows, 0, 1 );
00579
00580
00581 $db->query( $swapSQL1 );
00582 $db->query( $swapSQL2 );
00583 }
00584 else
00585 {
00586 $tmp =& eZPersistentObject::fetchObjectList( $def,
00587 $fields,
00588 $conditions,
00589 array( $order_id => $order_type ),
00590 array( "length" => 1 ),
00591 false );
00592 $where_text = eZPersistentObject::conditionTextByRow( $keys, $rows[0] );
00593 $db->query( "UPDATE $table SET $order_id='" . ( $tmp[0][$order_id] + $order_add ) .
00594 "'$where_text" );
00595 }
00596 }
00597
00633 function &definition()
00634 {
00635 return array();
00636 }
00637
00638 function &escapeArray( &$array )
00639 {
00640 $db =& eZDB::instance();
00641 $out = array();
00642 foreach( $array as $key => $value )
00643 {
00644 if ( is_array( $value ) )
00645 {
00646 $tmp = array();
00647 foreach( $value as $valueItem )
00648 {
00649 $tmp[] = $db->escapeString( $valueItem );
00650 }
00651 $out[$key] = $tmp;
00652 unset( $tmp );
00653 }
00654 else
00655 $out[$key] = $db->escapeString( $value );
00656 }
00657 return $out;
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 function updateObjectList( $parameters )
00701 {
00702 $db =& eZDB::instance();
00703 $def =& $parameters['definition'];
00704 $table =& $def['name'];
00705 $fields =& $def['fields'];
00706 $keys =& $def['keys'];
00707
00708 $updateFields =& $parameters['update_fields'];
00709 $conditions =& $parameters['conditions'];
00710
00711 $query = "UPDATE $table SET ";
00712 $i = 0;
00713 foreach( $updateFields as $field => $value )
00714 {
00715 if ( $i > 0 )
00716 $query .= ', ';
00717 $query .= $field . "='" . $db->escapeString( $value ) . "'";
00718 ++$i;
00719 }
00720 $query .= "\n" . 'WHERE ';
00721 $i = 0;
00722 foreach( $conditions as $conditionKey => $condition )
00723 {
00724 if ( $i > 0 )
00725 $query .= ' AND ';
00726 if ( is_array( $condition ) )
00727 {
00728 $query .= $conditionKey . ' IN (';
00729 $j = 0;
00730 foreach( $condition as $conditionValue )
00731 {
00732 if ( $j > 0 )
00733 $query .= ', ';
00734 $query .= "'" . $db->escapeString( $conditionValue ) . "'";
00735 ++$j;
00736 }
00737 $query .= ')';
00738 }
00739 else
00740 $query .= $conditionKey . "='" . $db->escapeString( $condition ) . "'";
00741 ++$i;
00742 }
00743 $db->query( $query );
00744
00745
00746
00747 }
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00816 function &attributes()
00817 {
00818 $def =& $this->definition();
00819 $attrs = array_keys( $def["fields"] );
00820 if ( isset( $def["function_attributes"] ) )
00821 $attrs = array_merge( $attrs, array_keys( $def["function_attributes"] ) );
00822 return $attrs;
00823 }
00824
00828 function hasAttribute( $attr )
00829 {
00830 $def =& $this->definition();
00831 $has_attr = isset( $def["fields"][$attr] );
00832 if ( !$has_attr and isset( $def["function_attributes"] ) )
00833 $has_attr = isset( $def["function_attributes"][$attr] );
00834 return $has_attr;
00835 }
00836
00841 function &attribute( $attr )
00842 {
00843 $def =& $this->definition();
00844 $fields =& $def["fields"];
00845 $functions =& $def["functions"];
00846 $attr_functions = null;
00847 if ( isset( $def["function_attributes"] ) )
00848 $attr_functions = $def["function_attributes"];
00849 if ( !isset( $fields[$attr] ) )
00850 {
00851 if ( !is_null( $attr_functions ) or
00852 !isset( $attr_functions[$attr] ) )
00853 {
00854 eZDebug::writeError( "Undefined attribute '$attr', cannot get",
00855 $def['class_name'] );
00856 return null;
00857 }
00858 }
00859 if ( !is_null( $attr_functions ) and isset( $attr_functions[$attr] ) )
00860 {
00861 $function_name = $attr_functions[$attr];
00862 return $this->$function_name();
00863 }
00864 else if ( isset( $functions[$attr] ) )
00865 {
00866 $function_name = $functions[$attr];
00867 return $this->$function_name();
00868 }
00869 else
00870 {
00871 $attr_name = $fields[$attr];
00872 return $this->$attr_name;
00873 }
00874 }
00875
00880 function setAttribute( $attr, $val )
00881 {
00882 $def =& $this->definition();
00883 $fields =& $def["fields"];
00884 $functions =& $def["set_functions"];
00885 if ( !isset( $fields[$attr] ) )
00886 {
00887 eZDebug::writeError( "Undefined attribute '$attr', cannot set",
00888 $def['class_name'] );
00889 return;
00890 }
00891 if ( isset( $functions[$attr] ) )
00892 {
00893 $function_name = $functions[$attr];
00894 $this->$function_name( $val );
00895 }
00896 else
00897 {
00898 $attr_name = $fields[$attr];
00899 $this->$attr_name = $val;
00900 $this->setHasDirtyData( true );
00901 }
00902 }
00903
00908 function hasDirtyData()
00909 {
00910 return $this->PersistentDataDirty;
00911 }
00912
00917 function setHasDirtyData( $hasDirtyData )
00918 {
00919 $this->PersistentDataDirty = $hasDirtyData;
00920 }
00921
00924 var $PersistentDataDirty;
00925 }
00926
00927 ?>