[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezmultioption/ -> ezmultioptiontype.php (source)

   1  <?php
   2  //
   3  // Definition of eZOptionType class
   4  //
   5  // Created on: <29-Jul-2004 15:52:24 gv>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZMultiOptionType ezmultioptiontype.php
  31    \ingroup eZDatatype
  32    \brief A datatype which works with multiple options.
  33  
  34    This allows the user to add several option choices almost as if he
  35    was adding attributes with option datatypes.
  36  
  37    This class implements the interface for a datatype but passes
  38    most of the work over to the eZMultiOption class which handles
  39    parsing, storing and manipulation of multioptions and options.
  40  
  41    This datatype supports:
  42    - fetch and validation of HTTP data
  43    - search indexing
  44    - product option information
  45    - class title
  46    - class serialization
  47  
  48  */
  49  
  50  include_once ( "kernel/classes/ezdatatype.php" );
  51  include_once ( "kernel/classes/datatypes/ezmultioption/ezmultioption.php" );
  52  include_once ( 'lib/ezutils/classes/ezstringutils.php' );
  53  define( "EZ_MULTIOPTION_DEFAULT_NAME_VARIABLE", "_ezmultioption_default_name_" );
  54  define( "EZ_DATATYPESTRING_MULTIOPTION", "ezmultioption" );
  55  
  56  class eZMultiOptionType extends eZDataType
  57  {
  58      /*!
  59       Constructor to initialize the datatype.
  60      */
  61      function eZMultiOptionType()
  62      {
  63          $this->eZDataType( EZ_DATATYPESTRING_MULTIOPTION, ezi18n( 'kernel/classes/datatypes', "Multi-option", 'Datatype name' ),
  64                             array( 'serialize_supported' => true ) );
  65      }
  66  
  67      /*!
  68       Validates the input for this datatype.
  69       \return True if input is valid.
  70      */
  71      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
  72      {
  73          $count = 0;
  74          $classAttribute =& $contentObjectAttribute->contentClassAttribute();
  75          if ( $http->hasPostVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) ) )
  76          {
  77              $classAttribute =& $contentObjectAttribute->contentClassAttribute();
  78              $multioptionIDArray = $http->postVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) );
  79  
  80              foreach ( $multioptionIDArray as $id )
  81              {
  82                  $multioptionName = $http->postVariable( $base . "_data_multioption_name_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
  83                  $optionIDArray = $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  84                                   ? $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  85                                   : array();
  86                  $optionCountArray = $http->hasPostVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  87                                      ? $http->postVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  88                                      : array();
  89                  $optionValueArray = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  90                                      ? $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  91                                      : array();
  92                  $optionAdditionalPriceArray = $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  93                                                ? $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
  94                                                : array();
  95                  for ( $i = 0; $i < count( $optionIDArray ); $i++ )
  96                  {
  97                      if ( $contentObjectAttribute->validateIsRequired() and !$classAttribute->attribute( 'is_information_collector' ) )
  98                      {
  99                          if ( trim( $optionValueArray[$i] ) == "" )
 100                          {
 101                              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 102                                                                                   'The option value must be provided.' ) );
 103                              return EZ_INPUT_VALIDATOR_STATE_INVALID;
 104                          }
 105                          else
 106                              ++$count;
 107                      }
 108  
 109                      if ( trim( $optionValueArray[$i] ) != "" )
 110                      {
 111                          if ( strlen( $optionAdditionalPriceArray[$i] ) && !preg_match( "#^[-|+]?[0-9]+(\.){0,1}[0-9]{0,2}$#", $optionAdditionalPriceArray[$i] ) )
 112                          {
 113                              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 114                                                                                   'The additional price for the multioption value is not valid.' ) );
 115                              return EZ_INPUT_VALIDATOR_STATE_INVALID;
 116                          }
 117                      }
 118  
 119                  }
 120              }
 121          }
 122          if ( $contentObjectAttribute->validateIsRequired() and
 123                   !$classAttribute->attribute( 'is_information_collector' ) )
 124          {
 125              if ( $count == 0 )
 126              {
 127                  $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 128                                                                       'At least one option is required.' ) );
 129                  return EZ_INPUT_VALIDATOR_STATE_INVALID;
 130              }
 131  
 132          }
 133  
 134          $optionSetName = $http->hasPostVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) )
 135                           ? $http->postVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) )
 136                           : '';
 137          if ( trim( $optionSetName ) == '' )
 138          {
 139              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 140                                                                   'Option set name is required.' ) );
 141              return EZ_INPUT_VALIDATOR_STATE_INVALID;
 142          }
 143  
 144          return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 145      }
 146  
 147      /*!
 148       This function calles xmlString function to create xml string and then store the content.
 149      */
 150      function storeObjectAttribute( &$contentObjectAttribute )
 151      {
 152          $multioption =& $contentObjectAttribute->content();
 153          $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
 154      }
 155  
 156      /*!
 157       \return An eZMultiOption object which contains all the option data
 158      */
 159      function &objectAttributeContent( &$contentObjectAttribute )
 160      {
 161          $multioption = new eZMultiOption( "" );
 162          $multioption->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
 163          return $multioption;
 164      }
 165  
 166      /*!
 167       \reimp
 168      */
 169      function isIndexable()
 170      {
 171          return true;
 172      }
 173  
 174      /*!
 175       \return The internal XML text.
 176      */
 177      function metaData( $contentObjectAttribute )
 178      {
 179          return $contentObjectAttribute->attribute( "data_text" );
 180      }
 181  
 182      /*!
 183       Fetches the http post var integer input and stores it in the data instance.
 184      */
 185      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 186      {
 187          $multioptionIDArray = $http->hasPostVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) )
 188                                ? $http->postVariable( $base . "_data_multioption_id_" . $contentObjectAttribute->attribute( "id" ) )
 189                                : array();
 190          $optionSetName = $http->postVariable( $base . "_data_optionset_name_" . $contentObjectAttribute->attribute( "id" ) );
 191          $multioption = new eZMultiOption( $optionSetName );
 192          foreach ( $multioptionIDArray as $id )
 193          {
 194              $multioptionName = $http->postVariable( $base . "_data_multioption_name_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
 195              $optionIDArray = $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 196                               ? $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 197                               : array();
 198  
 199              $optionPriority = $http->postVariable( $base . "_data_multioption_priority_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id );
 200              // check to prevent PHP warning if the default choice is specified (no radio button selected)
 201              if ( $http->hasPostVariable( $base . "_data_radio_checked_" . $contentObjectAttribute->attribute("id") . '_' . $id ) )
 202                  $optionDefaultValue = $http->postVariable( $base . "_data_radio_checked_" . $contentObjectAttribute->attribute("id") . '_' . $id );
 203              else
 204                  $optionDefaultValue = '';
 205              $newID = $multioption->addMultiOption( $multioptionName,$optionPriority, $optionDefaultValue );
 206  
 207              $optionCountArray = $http->hasPostVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 208                                  ? $http->postVariable( $base . "_data_option_option_id_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 209                                  : array();
 210              $optionValueArray = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 211                                  ? $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 212                                  : array();
 213              $optionAdditionalPriceArray = $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 214                                            ? $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) . '_' . $id )
 215                                            : array();
 216  
 217              for ( $i = 0; $i < count( $optionIDArray ); $i++ )
 218                  $multioption->addOption( $newID, $optionCountArray[$i], $optionValueArray[$i], $optionAdditionalPriceArray[$i] );
 219          }
 220  
 221          $multioption->sortMultiOptions();
 222          $multioption->resetOptionCounter();
 223          $contentObjectAttribute->setContent( $multioption );
 224          return true;
 225      }
 226  
 227      /*!
 228       Fetches the http post variables for collected information
 229      */
 230      function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
 231      {
 232          $multioptionValue = $http->postVariable( $base . "_data_multioption_value_" . $contentObjectAttribute->attribute( "id" ) );
 233          $collectionAttribute->setAttribute( 'data_int', $multioptionValue );
 234          return true;
 235      }
 236  
 237      /*!
 238       This function performs specific actions.
 239  
 240       It has some special actions with parameters which is done by exploding
 241       $action into several parts with delimeter '_'.
 242       The first element is the name of specific action to perform.
 243       The second element will contain the key value or id.
 244  
 245       The various operation's that is performed by this function are as follow.
 246       - new-option - A new option is added to a multioption.
 247       - remove-selected-option - Removes a selected option.
 248       - new_multioption - Adds a new multioption.
 249       - remove_selected_multioption - Removes all multioptions given by a selection list
 250      */
 251      function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
 252      {
 253          $actionlist = explode( "_", $action );
 254          if ( $actionlist[0] == "new-option" )
 255          {
 256              $multioption =& $contentObjectAttribute->content();
 257  
 258              $multioption->addOption( ( $actionlist[1] - 1 ), "", "", "");
 259              $contentObjectAttribute->setContent( $multioption );
 260              $contentObjectAttribute->store();
 261          }
 262          else if ( $actionlist[0] == "remove-selected-option" )
 263          {
 264              $multioption =& $contentObjectAttribute->content();
 265              $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" ) . "_" . $actionlist[1];
 266              $array_remove = $http->hasPostVariable( $postvarname ) ? $http->postVariable( $postvarname ) : array();
 267              $multioption->removeOptions( $array_remove, $actionlist[1] - 1 );
 268              $contentObjectAttribute->setContent( $multioption );
 269              $contentObjectAttribute->store();
 270          }
 271          else
 272          {
 273              switch ( $action )
 274              {
 275                  case "new_multioption" :
 276                  {
 277                      $multioption =& $contentObjectAttribute->content();
 278                      $newID = $multioption->addMultiOption( "" ,0,false );
 279                      $multioption->addOption( $newID, "", "", "" );
 280                      $multioption->addOption( $newID, "" ,"", "" );
 281                      $contentObjectAttribute->setContent( $multioption );
 282                      $contentObjectAttribute->store();
 283                  } break;
 284  
 285                  case "remove_selected_multioption":
 286                  {
 287                      $multioption =& $contentObjectAttribute->content();
 288                      $postvarname = "ContentObjectAttribute" . "_data_multioption_remove_" . $contentObjectAttribute->attribute( "id" );
 289                      $array_remove = $http->hasPostVariable( $postvarname )? $http->postVariable( $postvarname ) : array();
 290                      $multioption->removeMultiOptions( $array_remove );
 291                      $contentObjectAttribute->setContent( $multioption );
 292                      $contentObjectAttribute->store();
 293                  } break;
 294  
 295                  default:
 296                  {
 297                      eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZMultiOptionType" );
 298                  } break;
 299              }
 300          }
 301      }
 302  
 303      /*!
 304       \reimp
 305       Finds the option which has the correct ID , if found it returns an option structure.
 306  
 307       \param $optionString must contain the multioption ID an underscore (_) and a the option ID.
 308      */
 309      function productOptionInformation( &$objectAttribute, $optionID, &$productItem )
 310      {
 311          $multioption =& $objectAttribute->attribute( 'content' );
 312  
 313          foreach ( $multioption->attribute( 'multioption_list' ) as $multioptionElement )
 314          {
 315              foreach ( $multioptionElement['optionlist'] as $option )
 316              {
 317                  if ( $option['option_id'] != $optionID )
 318                      continue;
 319  
 320                  return array( 'id' => $option['option_id'],
 321                                'name' => $multioptionElement['name'],
 322                                'value' => $option['value'],
 323                                'additional_price' => $option['additional_price'] );
 324              }
 325          }
 326      }
 327  
 328      /*!
 329       \reimp
 330      */
 331      function title( &$contentObjectAttribute, $name = "name" )
 332      {
 333          $multioption =& $contentObjectAttribute->content();
 334          $value = $multioption->attribute( $name );
 335          return $value;
 336      }
 337  
 338      /*!
 339        \reimp
 340        \return \c true if there are more than one multioption in the list.
 341      */
 342      function hasObjectAttributeContent( &$contentObjectAttribute )
 343      {
 344          $multioption =& $contentObjectAttribute->content();
 345          $multioptions = $multioption->attribute( 'multioption_list' );
 346          return count( $multioptions ) > 0;
 347      }
 348  
 349      /*!
 350       Sets default multioption values.
 351      */
 352      function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
 353      {
 354          if ( $currentVersion == false )
 355          {
 356              $multioption =& $contentObjectAttribute->content();
 357              if ( $multioption )
 358              {
 359                  $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
 360                  $multioption->setName( $contentClassAttribute->attribute( 'data_text1' ) );
 361                  $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
 362                  $contentObjectAttribute->setContent( $multioption );
 363              }
 364          }
 365          else
 366          {
 367              $dataText = $originalContentObjectAttribute->attribute( "data_text" );
 368              $contentObjectAttribute->setAttribute( "data_text", $dataText );
 369          }
 370      }
 371  
 372      /*!
 373       \reimp
 374      */
 375      function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
 376      {
 377          $defaultValueName = $base . EZ_MULTIOPTION_DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
 378          if ( $http->hasPostVariable( $defaultValueName ) )
 379          {
 380              $defaultValueValue = $http->postVariable( $defaultValueName );
 381  
 382              if ( $defaultValueValue == "" )
 383              {
 384                  $defaultValueValue = "";
 385              }
 386              $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
 387              return true;
 388          }
 389          return false;
 390      }
 391  
 392      function toString( $contentObjectAttribute )
 393      {
 394  
 395          $content = $contentObjectAttribute->attribute( 'content' );
 396  
 397          $multioptionArray = array();
 398  
 399          $setName = $content->attribute( 'name' );
 400          $multioptionArray[] = $setName;
 401  
 402          $multioptionList = $content->attribute( 'multioption_list' );
 403  
 404          foreach ( $multioptionList as $key => $option )
 405          {
 406              $optionArray = array();
 407              $optionArray[] = $option['name'];
 408              $optionArray[] = $option['default_option_id'];
 409              foreach ( $option['optionlist'] as $key => $value )
 410              {
 411                  $optionArray[] = $value['value'];
 412                  $optionArray[] = $value['additional_price'];
 413              }
 414              $multioptionArray[] = eZStringUtils::implodeStr( $optionArray, '|' );
 415          }
 416          return eZStringUtils::implodeStr( $multioptionArray, "&" );
 417      }
 418  
 419  
 420      function fromString( &$contentObjectAttribute, $string )
 421      {
 422          if ( $string == '' )
 423              return true;
 424  
 425          $multioptionArray = eZStringUtils::explodeStr( $string, '&' );
 426  
 427          $multioption = new eZMultiOption( "" );
 428  
 429          $multioption->OptionCounter = 0;
 430          $multioption->Options = array();
 431          $multioption->Name = array_shift( $multioptionArray );
 432          $priority = 1;
 433          foreach ( $multioptionArray as $multioptionStr )
 434          {
 435              $optionArray = eZStringUtils::explodeStr( $multioptionStr, '|' );
 436  
 437  
 438              $newID = $multioption->addMultiOption( array_shift( $optionArray ),
 439                                              $priority,
 440                                              array_shift( $optionArray ) );
 441              $optionID = 0;
 442              $count = count( $optionArray );
 443              for ( $i = 0; $i < $count; $i +=2 )
 444              {
 445                  $multioption->addOption( $newID, $optionID, array_shift( $optionArray ), array_shift( $optionArray ) );
 446                  $optionID++;
 447              }
 448              $priority++;
 449          }
 450  
 451          $contentObjectAttribute->setAttribute( "data_text", $multioption->xmlString() );
 452  
 453          return $multioption;
 454  
 455      }
 456  
 457      /*!
 458       \reimp
 459      */
 460      function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 461      {
 462          $defaultValue = $classAttribute->attribute( 'data_text1' );
 463          $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'default-value', $defaultValue ) );
 464      }
 465  
 466      /*!
 467       \reimp
 468      */
 469      function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 470      {
 471          $defaultValue = $attributeParametersNode->elementTextContentByName( 'default-value' );
 472          $classAttribute->setAttribute( 'data_text1', $defaultValue );
 473      }
 474  
 475      /*!
 476       \reimp
 477      */
 478      function serializeContentObjectAttribute( &$package, &$objectAttribute )
 479      {
 480          $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
 481  
 482          $xml = new eZXML();
 483          $domDocument = $xml->domTree( $objectAttribute->attribute( 'data_text' ) );
 484          $node->appendChild( $domDocument->root() );
 485  
 486          return $node;
 487      }
 488  
 489      /*!
 490       \reimp
 491      */
 492      function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
 493      {
 494          $rootNode = $attributeNode->firstChild();
 495          $xmlString = $rootNode->attributeValue( 'local_name' ) == 'data-text' ? '' : $rootNode->toString( 0 );
 496          $objectAttribute->setAttribute( 'data_text', $xmlString );
 497      }
 498  }
 499  
 500  eZDataType::register( EZ_DATATYPESTRING_MULTIOPTION, "ezmultioptiontype" );
 501  
 502  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7