[ 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/ezoption/ -> ezoptiontype.php (source)

   1  <?php
   2  //
   3  // Definition of eZOptionType class
   4  //
   5  //Created on: <28-Jun-2002 11:12:51 sp>
   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 eZOptionType ezoptiontype.php
  31    \ingroup eZDatatype
  32    \brief Stores option values
  33  
  34  */
  35  
  36  include_once ( "kernel/classes/ezdatatype.php" );
  37  
  38  include_once ( "kernel/classes/datatypes/ezoption/ezoption.php" );
  39  include_once ( 'lib/ezutils/classes/ezstringutils.php' );
  40  
  41  define( "EZ_OPTION_DEFAULT_NAME_VARIABLE", "_ezoption_default_name_" );
  42  
  43  define( "EZ_DATATYPESTRING_OPTION", "ezoption" );
  44  
  45  class eZOptionType extends eZDataType
  46  {
  47      function eZOptionType()
  48      {
  49          $this->eZDataType( EZ_DATATYPESTRING_OPTION, ezi18n( 'kernel/classes/datatypes', "Option", 'Datatype name' ),
  50                             array( 'serialize_supported' => true ) );
  51      }
  52  
  53      /*!
  54      */
  55      function validateCollectionAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
  56      {
  57          $classAttribute =& $contentObjectAttribute->contentClassAttribute();
  58          if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
  59          {
  60              $value = $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
  61  
  62              if ( $contentObjectAttribute->validateIsRequired() and !$value )
  63              {
  64                  $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  65                                                                       'Input required.' ) );
  66                  return EZ_INPUT_VALIDATOR_STATE_INVALID;
  67              }
  68          }
  69          else
  70          {
  71              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
  72                                                                   'Input required.' ) );
  73              return EZ_INPUT_VALIDATOR_STATE_INVALID;
  74          }
  75      }
  76  
  77      /*!
  78       Validates the input and returns true if the input was
  79       valid for this datatype.
  80      */
  81      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
  82      {
  83          $count = 0;
  84          $classAttribute =& $contentObjectAttribute->contentClassAttribute();
  85          if ( $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) ) )
  86          {
  87              $idList = $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) );
  88              $valueList = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
  89              $dataName = $http->postVariable( $base . "_data_option_name_" . $contentObjectAttribute->attribute( "id" ) );
  90  
  91              if ( $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) ) )
  92                  $optionAdditionalPriceList = $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) );
  93              else
  94                  $optionAdditionalPriceList = array();
  95  
  96              for ( $i = 0; $i < count( $valueList ); ++$i )
  97                  if ( trim( $valueList[$i] ) <> '' )
  98                  {
  99                      ++$count;
 100                      break;
 101                  }
 102              if ( $contentObjectAttribute->validateIsRequired() and trim( $dataName ) == '' )
 103              {
 104                  $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 105                                                                       'NAME is required.' ) );
 106                  return EZ_INPUT_VALIDATOR_STATE_INVALID;
 107              }
 108              if ( $count != 0 )
 109              {
 110                  for ( $i=0;$i<count( $idList );$i++ )
 111                  {
 112                      $value =  $valueList[$i];
 113                      if ( trim( $value )== "" )
 114                      {
 115                          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 116                                                                               'The option value must be provided.' ) );
 117                          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 118                      }
 119                      if ( isset( $optionAdditionalPriceList[$i] ) &&
 120                           strlen( $optionAdditionalPriceList[$i] ) &&
 121                           !preg_match( "#^[-|+]?[0-9]+(\.){0,1}[0-9]{0,2}$#", $optionAdditionalPriceList[$i] ) )
 122                      {
 123                          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 124                                                                               'The Additional price value is not valid.' ) );
 125                          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 126                      }
 127                  }
 128              }
 129          }
 130          if ( $contentObjectAttribute->validateIsRequired() and
 131               !$classAttribute->attribute( 'is_information_collector' ) )
 132          {
 133              if ( $count == 0 )
 134              {
 135                  $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 136                                                                       'At least one option is required.' ) );
 137                  return EZ_INPUT_VALIDATOR_STATE_INVALID;
 138              }
 139          }
 140          return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 141      }
 142  
 143      /*!
 144       Store content
 145      */
 146      function storeObjectAttribute( &$contentObjectAttribute )
 147      {
 148          $option =& $contentObjectAttribute->content();
 149          $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
 150      }
 151  
 152      /*!
 153       Returns the content.
 154      */
 155      function &objectAttributeContent( &$contentObjectAttribute )
 156      {
 157          $option = new eZOption( "" );
 158  
 159          $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
 160  
 161          return $option;
 162      }
 163  
 164      /*!
 165       Returns the meta data used for storing search indeces.
 166      */
 167      function metaData( $contentObjectAttribute )
 168      {
 169          return $contentObjectAttribute->attribute( "data_text" );
 170      }
 171  
 172      /*!
 173       Fetches the http post var integer input and stores it in the data instance.
 174      */
 175      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 176      {
 177          $optionName = $http->postVariable( $base . "_data_option_name_" . $contentObjectAttribute->attribute( "id" ) );
 178          if ( $http->hasPostVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) ) )
 179              $optionIDArray = $http->postVariable( $base . "_data_option_id_" . $contentObjectAttribute->attribute( "id" ) );
 180          else
 181              $optionIDArray = array();
 182          if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
 183              $optionValueArray = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
 184          else
 185              $optionValueArray = array();
 186          if ( $http->hasPostVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) ) )
 187              $optionAdditionalPriceArray = $http->postVariable( $base . "_data_option_additional_price_" . $contentObjectAttribute->attribute( "id" ) );
 188          else
 189              $optionAdditionalPriceArray = array();
 190  
 191          $option = new eZOption( $optionName );
 192  
 193          $i = 0;
 194          foreach ( $optionIDArray as $id )
 195          {
 196              $option->addOption( array( 'value' => $optionValueArray[$i],
 197                                         'additional_price' => ( isset( $optionAdditionalPriceArray[$i] ) ? $optionAdditionalPriceArray[$i] : 0 ) ) );
 198              $i++;
 199          }
 200          $contentObjectAttribute->setContent( $option );
 201          return true;
 202      }
 203  
 204  
 205      /*!
 206       Fetches the http post variables for collected information
 207      */
 208      function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
 209      {
 210          if ( $http->hasPostVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) ) )
 211          {
 212              $optionValue = $http->postVariable( $base . "_data_option_value_" . $contentObjectAttribute->attribute( "id" ) );
 213  
 214              $collectionAttribute->setAttribute( 'data_int', $optionValue );
 215              $attr =& $contentObjectAttribute->attribute( 'contentclass_attribute' );
 216  
 217              return true;
 218          }
 219          return false;
 220      }
 221  
 222      /*!
 223      */
 224      function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
 225      {
 226          switch ( $action )
 227          {
 228              case "new_option" :
 229              {
 230                  $option =& $contentObjectAttribute->content( );
 231  
 232                  $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" );
 233                  if ( $http->hasPostVariable( $postvarname ) )
 234                  {
 235                      $idArray = $http->postVariable( $postvarname );
 236                      $beforeID = array_shift( $idArray );
 237                      if ( $beforeID >= 0 )
 238                      {
 239                          $option->insertOption( array(), $beforeID );
 240  //                         eZDebug::writeDebug( $option, "option added before $beforeID" );
 241                          $contentObjectAttribute->setContent( $option );
 242                          $contentObjectAttribute->store();
 243                          $option = new eZOption( "" );
 244                          $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
 245                          $contentObjectAttribute->setContent( $option );
 246                          return;
 247                      }
 248                  }
 249                  $option->addOption( "" );
 250                  $contentObjectAttribute->setContent( $option );
 251                  $contentObjectAttribute->store();
 252              }break;
 253              case "remove_selected" :
 254              {
 255                  $option =& $contentObjectAttribute->content( );
 256                  $postvarname = "ContentObjectAttribute" . "_data_option_remove_" . $contentObjectAttribute->attribute( "id" );
 257                  $array_remove = $http->postVariable( $postvarname );
 258                  $option->removeOptions( $array_remove );
 259                  $contentObjectAttribute->setContent( $option );
 260                  $contentObjectAttribute->store();
 261                  $option = new eZOption( "" );
 262                  $option->decodeXML( $contentObjectAttribute->attribute( "data_text" ) );
 263                  $contentObjectAttribute->setContent( $option );
 264              }break;
 265              default :
 266              {
 267                  eZDebug::writeError( "Unknown custom HTTP action: " . $action, "eZOptionType" );
 268              }break;
 269          }
 270      }
 271  
 272      /*!
 273       Finds the option which has the ID that matches \a $optionID, if found it returns
 274       an option structure.
 275      */
 276      function productOptionInformation( &$objectAttribute, $optionID, &$productItem )
 277      {
 278          $option =& $objectAttribute->attribute( 'content' );
 279          foreach( $option->attribute( 'option_list' ) as $optionArray )
 280          {
 281              if ( $optionArray['id'] == $optionID )
 282              {
 283                  return array( 'id' => $optionArray['id'],
 284                                'name' => $option->attribute( 'name' ),
 285                                'value' => $optionArray['value'],
 286                                'additional_price' => $optionArray['additional_price'] );
 287              }
 288          }
 289          return false;
 290      }
 291  
 292      /*!
 293       Returns the integer value.
 294      */
 295      function title( &$contentObjectAttribute, $name = "name" )
 296      {
 297          $option =& $contentObjectAttribute->content( );
 298  
 299          $value = $option->attribute( $name );
 300  
 301          return $value;
 302      }
 303  
 304      function hasObjectAttributeContent( &$contentObjectAttribute )
 305      {
 306          $option =& $contentObjectAttribute->content( );
 307          $options = $option->attribute( 'option_list' );
 308          return count( $options ) > 0;
 309      }
 310  
 311      /*!
 312       Sets the default value.
 313      */
 314      function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
 315      {
 316          if ( $currentVersion == false )
 317          {
 318              $option =& $contentObjectAttribute->content();
 319              $contentClassAttribute =& $contentObjectAttribute->contentClassAttribute();
 320              if ( !$option )
 321              {
 322                  $option = new eZOption( $contentClassAttribute->attribute( 'data_text1' ) );
 323              }
 324              else
 325              {
 326                  $option->setName( $contentClassAttribute->attribute( 'data_text1' ) );
 327              }
 328              $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
 329              $contentObjectAttribute->setContent( $option );
 330          }
 331          else
 332          {
 333              $dataText = $originalContentObjectAttribute->attribute( "data_text" );
 334              $contentObjectAttribute->setAttribute( "data_text", $dataText );
 335          }
 336      }
 337  
 338      /*!
 339       \reimp
 340      */
 341      function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
 342      {
 343          $defaultValueName = $base . EZ_OPTION_DEFAULT_NAME_VARIABLE . $classAttribute->attribute( 'id' );
 344          if ( $http->hasPostVariable( $defaultValueName ) )
 345          {
 346              $defaultValueValue = $http->postVariable( $defaultValueName );
 347  
 348              if ($defaultValueValue == "")
 349              {
 350                  $defaultValueValue = "";
 351              }
 352              $classAttribute->setAttribute( 'data_text1', $defaultValueValue );
 353              return true;
 354          }
 355          return false;
 356      }
 357  
 358      function toString( $contentObjectAttribute )
 359      {
 360  
 361          $option = $contentObjectAttribute->attribute( 'content' );
 362          $optionArray = array();
 363          $optionArray[] = $option->attribute( 'name' );
 364  
 365          $optionList = $option->attribute( 'option_list' );
 366  
 367          foreach ( $optionList as $key => $value )
 368          {
 369              $optionArray[] = $value['value'];
 370              $optionArray[] = $value['additional_price'];
 371          }
 372          return eZStringUtils::implodeStr( $optionArray, "|" );
 373      }
 374  
 375  
 376      function fromString( &$contentObjectAttribute, $string )
 377      {
 378          if ( $string == '' )
 379              return true;
 380  
 381          $optionArray = eZStringUtils::explodeStr( $string, '|' );
 382  
 383          $option = new eZOption( );
 384  
 385          $option->OptionCount = 0;
 386          $option->Options = array();
 387          $option->Name = array_shift( $optionArray );
 388          $count = count( $optionArray );
 389          for ( $i = 0; $i < $count; $i +=2 )
 390          {
 391  
 392              $option->addOption( array( 'value' => array_shift( $optionArray ),
 393                                         'additional_price' => array_shift( $optionArray ) ) );
 394          }
 395  
 396  
 397          $contentObjectAttribute->setAttribute( "data_text", $option->xmlString() );
 398  
 399          return $option;
 400  
 401      }
 402      /*!
 403       \reimp
 404      */
 405      function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 406      {
 407          $defaultValue = $classAttribute->attribute( 'data_text1' );
 408          $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'default-value', $defaultValue ) );
 409      }
 410  
 411      /*!
 412       \reimp
 413      */
 414      function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 415      {
 416          $defaultValue = $attributeParametersNode->elementTextContentByName( 'default-value' );
 417          $classAttribute->setAttribute( 'data_text1', $defaultValue );
 418      }
 419  
 420      /*!
 421       \reimp
 422      */
 423      function serializeContentObjectAttribute( &$package, &$objectAttribute )
 424      {
 425          $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
 426  
 427          $xml = new eZXML();
 428          $domDocument = $xml->domTree( $objectAttribute->attribute( 'data_text' ) );
 429          $node->appendChild( $domDocument->root() );
 430  
 431          return $node;
 432      }
 433  
 434      /*!
 435       \reimp
 436      */
 437      function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
 438      {
 439          $rootNode = $attributeNode->firstChild();
 440          $xmlString = $rootNode->attributeValue( 'local_name' ) == 'data-text' ? '' : $rootNode->toString( 0 );
 441          $objectAttribute->setAttribute( 'data_text', $xmlString );
 442      }
 443  
 444      /*!
 445       \reimp
 446      */
 447      function isInformationCollector()
 448      {
 449          return true;
 450      }
 451  }
 452  
 453  eZDataType::register( EZ_DATATYPESTRING_OPTION, "ezoptiontype" );
 454  
 455  ?>


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