eZ Publish  [4.0]
ezhttppersistence.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZHTTPPersistence class
00004 //
00005 // Created on: <19-Apr-2002 16:14:47 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZHTTPPersistence ezhttppersistence.php
00033   \ingroup eZHTTP
00034   \brief Object persistence using HTTP post variables
00035 
00036   This class allows objects or data to exist between page views.
00037   It can read HTTP post variables and set them in existing objects
00038   to override data. This is useful if you want to keep changes in a
00039   page but don't want to store the changes in a DB. It also makes it
00040   easier to fetch the changes by the user before an object is stored.
00041 
00042 */
00043 
00044 //include_once( "lib/ezutils/classes/ezhttptool.php" );
00045 
00046 class eZHTTPPersistence
00047 {
00048     /*!
00049      Initializes the class.
00050     */
00051     function eZHTTPPersistence()
00052     {
00053     }
00054 
00055     /*!
00056      Fetches the HTTP post variables using the base name $base_name and stores
00057      them in the object $objects, if $is_array is true then $objects is assumed
00058      to be an array and all objects are updated.
00059     */
00060     static function fetch( $base_name,
00061                     /*! The definition of the objects, uses the same syntax as eZPersistentObject */
00062                     $def,
00063                     $objects,
00064                     /*! The eZHTTPTool object */
00065                     $http,
00066                     $is_array )
00067     {
00068         if ( $is_array )
00069         {
00070             for ( $i = 0; $i < count( $objects ); ++$i )
00071             {
00072                 $object = $objects[$i];
00073                 eZHTTPPersistence::fetchElement( $base_name, $def, $object, $http, $i );
00074             }
00075         }
00076         else
00077             eZHTTPPersistence::fetchElement( $base_name, $def, $objects, $http, false );
00078     }
00079 
00080     /*!
00081      \private
00082      Helper function for fetch().
00083     */
00084     static function fetchElement( $base_name, $def,
00085                                   $object, $http, $index )
00086     {
00087         $fields = $def["fields"];
00088         $keys = $def["keys"];
00089         foreach ( $fields as $field_name => $field_member )
00090         {
00091             if ( !in_array( $field_name, $keys ) )
00092             {
00093                 $post_var = $base_name . "_" . $field_name;
00094                 if ( $http->hasPostVariable( $post_var ) )
00095                 {
00096                     $post_value = $http->postVariable( $post_var );
00097                     if ( $index === false )
00098                         $object->setAttribute( $field_name, $post_value );
00099                     else
00100                         $object->setAttribute( $field_name, $post_value[$index] );
00101                 }
00102             }
00103         }
00104     }
00105 
00106     /*!
00107      \deprecated This function has some serious flaws and will be removed in a future release
00108      Goes trough all fields defined in \a $def and tries to find a post variable
00109      which is named \a $base_name, field name and "checked" with _ between items.
00110      If the post variable is an array the id of the current object is matched against
00111      that array, if one is found the matched field is set to be true otherwise false.
00112      If no post variable was found with that signature the field is ignored.
00113      Example of name:
00114      \code
00115        In the HTML code use:<br/>
00116        <input type="checkbox" name="ContentClassAttribute_is_searchable_checked[]" value="some_id" />
00117      \endcode
00118     */
00119     static function handleChecked( $base_name,
00120                             /*! The definition of the objects, uses the same syntax as eZPersistentObject */
00121                             $def,
00122                             $objects,
00123                             $http,
00124                             $is_array = true )
00125     {
00126         if ( $is_array )
00127         {
00128             foreach( $objects as $object )
00129             {
00130                 eZHTTPPersistence::handleCheckedElement( $base_name, $def, $object, $http );
00131             }
00132         }
00133         else
00134             eZHTTPPersistence::handleCheckedElement( $base_name, $def, $objects, $http );
00135     }
00136 
00137     /*!
00138      \private
00139      Helper function for handleChecked().
00140      \deprecated This function has some serious flaws and will be removed in a future release
00141     */
00142     static function handleCheckedElement( $base_name, $def,
00143                                    $object, $http )
00144     {
00145         $fields = $def["fields"];
00146         $keys = $def["keys"];
00147         $id = $object->attribute( "id" );
00148         foreach ( $fields as $field_name => $field_member )
00149         {
00150             if ( !in_array( $field_name, $keys ) )
00151             {
00152                 $post_var = $base_name . "_" . $field_name . "_checked";
00153                 if ( $http->hasPostVariable( $post_var ) or $field_name == "is_searchable" or $field_name == "is_required"   )
00154                 {
00155                     $value = false;
00156                     $post_value = $http->postVariable( $post_var );
00157                     if ( is_array( $post_value ) &&
00158                          in_array( $id, $post_value ) )
00159                     {
00160                         $value = true;
00161                     }
00162                     else
00163                     {
00164                          $value = false;
00165                     }
00166                     $object->setAttribute( $field_name, $value );
00167                 }
00168             }
00169         }
00170     }
00171 
00172     /*!
00173      Loops over the HTTP post variables with $base_name as the base.
00174      It examines the HTTP post variable $base_name "_" $cond "_checked"
00175      which should contain an array of ids. The ids are then matched against
00176      the objects attribute $cond. If they match the object is moved to the
00177      $rejects array otherwise the $keepers array.
00178     */
00179     static function splitSelected( $base_name,
00180                             $objects, /*! The eZHTTPTool object */ $http, $cond,
00181                             &$keepers, &$rejects )
00182     {
00183         $keepers = array();
00184         $rejects = array();
00185         $post_var = $base_name . "_" . $cond . "_checked";
00186         if ( $http->hasPostVariable( $post_var ) )
00187         {
00188             $checks = $http->postVariable( $post_var );
00189         }
00190         else
00191         {
00192             return false;
00193         }
00194         foreach( $objects as $object )
00195         {
00196             if ( $object->hasAttribute( $cond ) )
00197             {
00198                 $val = $object->attribute( $cond );
00199                 if ( in_array( $val, $checks ) )
00200                 {
00201                     $rejects[] = $object;
00202                 }
00203                 else
00204                 {
00205                     $keepers[] = $object;
00206                 }
00207             }
00208             else
00209             {
00210                 $keepers[] = $object;
00211             }
00212         }
00213         return true;
00214     }
00215 
00216 }
00217 
00218 ?>