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

   1  <?php
   2  //
   3  // Definition of eZOption 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 eZMultiOption ezmultioption.php
  31    \ingroup eZDatatype
  32    \brief Encapsulates multiple options in one datatype.
  33  
  34    This class encapsulates multiple options by storing an array
  35    with multioption elements each containing a list of options.
  36    Managing multioptions is done using addMultiOption(), removeMultiOption() and changeMultiOption().
  37    For a multioption you can add and remove options with addOption() and removeOptions().
  38    For storage use the xmlString() and decodeXML() methods.
  39  
  40    A \a multioption is a list of the group of options.
  41    All multioption's name are displayed simultaneously.
  42  
  43    Multioption consists of id, name, priority and default_option_id.
  44    - id                : is a unique representation of all multioptions.
  45    - name              : name of multioption.
  46    - priority          : Priority value is is used to sort the multioption after sorting
  47                          it will change the original priority value to 1,2,3...
  48    - default_option_id : is to displaying the default option from the list.
  49    - optionlist        : list of all option elements.
  50  
  51    An \a option is a list of values in which only one value can be selected
  52    at a time from drop down combobox.
  53    In the following e.g. Model-A,Model-B, Model-C are options for Model multioption
  54    and Red and Blue are options for Color multioption.
  55  
  56    Option consists of id, option_id, value and additional_price.
  57    - id               : unique representation of options in a multioption.
  58    - option_id        : option_id is unique among all options, it is used to
  59                         retrieve values for shop or similar operations.
  60    - value            : value is a string and it is a value used as display for that option.
  61    - additional_price : This is an additional price that will add to the original price
  62                         and will display in currency format for that value.
  63                         If the price value is not given then it will will not display
  64                         anything in that place.
  65  
  66    In the following example Model and Color is known as an multioption.
  67    Examples:
  68    1. If a motor car company is having different models and colors then it will be.
  69  
  70    \code
  71          CAR
  72           Model
  73               ----------------------
  74               |                 |\/|
  75               ----------------------
  76               | Model - A £ 100.00 |
  77               | Model - B £ 200.00 |
  78               | Model - C £ 300.00 |
  79               ----------------------
  80  
  81               Color
  82               -------------
  83               |        |\/|
  84               -------------
  85               |  Red      |
  86               |  Blue     |
  87               -------------
  88    \endcode
  89  
  90    The xmlstring for the above eg will look like as follow
  91    \code
  92    Array( [0] => Array(
  93             [id] => 1
  94             [name] => CAR
  95             [priority] => 1
  96             [default_option_id] => 2
  97             [optionlist] => Array( [0] => Array( [id] => 1 [option_id] => 1 [value] => Model-A [additional_price] => 100 )
  98                                    [1] => Array( [id] => 2 [option_id] => 2 [value] => Model-B [additional_price] => 200 )
  99                                    [2] => Array( [id] => 3 [option_id] => 5 [value] => Model-C [additional_price] => 300 )
 100                                  )
 101                       )
 102           [1] => Array(
 103             [id] => 2
 104             [name] => Color
 105             [priority] => 2
 106             [default_option_id] => 1
 107             [optionlist] => Array( [0] => Array( [id] => 1 [option_id] => 3 [value] => Red [additional_price] => )
 108                                    [1] => Array( [id] => 2 [option_id] => 4 [value] => Blue[additional_price] => )
 109                                  )
 110                       )
 111         )
 112    \endcode
 113  
 114    Example of how to crete an option, adding multioptions and options
 115    and finally retrieving the xml structure.
 116    \code
 117     include_once( "kernel/classes/datatypes/ezoption/ezmultioption.php" );
 118     $option = new eZOption( "Car" );
 119     $newID = $option->addMultiOption("Model",$priority,false);
 120        $option->addOption( $newID, "", "Model - A", "100", false );
 121        $option->addOption( $newID, "", "Model - B", "100", false );
 122        $option->addOption( $newID, "", "Model - C", "100", false );
 123  
 124     $newID = $option->addMultiOption( "Color", $priority, false );
 125        $option->addOption( $newID, "", "Red", "", false );
 126        $option->addOption( $newID, "", "Blue", "", false );
 127    // Serialize the class to an XML document
 128    $xmlString =& $option->xmlString();
 129    \endcode
 130  */
 131  
 132  include_once ( "lib/ezxml/classes/ezxml.php" );
 133  
 134  class eZMultiOption
 135  {
 136      /*!
 137       Initializes with empty multioption list.
 138      */
 139      function eZMultiOption( $name )
 140      {
 141          $this->Name = $name;
 142          $this->Options = array();
 143          $this->MultiOptionCount = 0;
 144          $this->OptionCounter = 0;
 145      }
 146  
 147      /*!
 148        Adds an Multioption named \a $name
 149        \param $name contains the name of multioption.
 150        \param $multiOptionPriority is stored for displaying the array in order.
 151        \param $defaultValue is stored to display the options by default.
 152        \return The ID of the multioption that was added.
 153      */
 154      function addMultiOption( $name, $multiOptionPriority, $defaultValue )
 155      {
 156          $this->MultiOptionCount += 1;
 157          $this->Options[$this->MultiOptionCount] = array( "id" => $this->MultiOptionCount,
 158                                                           "name" => $name,
 159                                                           'priority'=> $multiOptionPriority,
 160                                                           "default_option_id" => $defaultValue,
 161                                                           'optionlist' => array() );
 162          return $this->MultiOptionCount;
 163      }
 164  
 165      /*!
 166        Adds an Option to multioption \a $name
 167        \param $newID is the element key value to which the new option will be added.
 168        \param $optionValue is the original value to display for users.
 169        \param $optionAdditionalPrice is a price value that is used to store price of the option values.
 170      */
 171      function addOption( $newID, $OptionID, $optionValue, $optionAdditionalPrice )
 172      {
 173          $key = count( $this->Options[$newID]['optionlist'] ) + 1;
 174          if ( strlen( $OptionID ) == 0 )
 175          {
 176              $this->OptionCounter += 1;
 177              $OptionID = $this->OptionCounter;
 178          }
 179          $this->Options[$newID]['optionlist'][] = array( "id" => $key,
 180                                                          "option_id" => $OptionID,
 181                                                          "value" => $optionValue,
 182                                                          'additional_price' => $optionAdditionalPrice );
 183      }
 184  
 185      /*!
 186       Sorts the current multioption on the basis of it's priority value.
 187       After softing array it calles the function changeMultiOptionID,
 188       which again rearragnes the current priority value to 1,2,3......etc.
 189      */
 190      function sortMultiOptions()
 191      {
 192          usort( $this->Options, create_function( '$a, $b', 'if ( $a["priority"] == $b["priority"] ) { return 0; } return ( $a["priority"] < $b["priority"] ) ? -1 : 1;' ) );
 193          $this->changeMultiOptionID();
 194      }
 195  
 196      /*!
 197        Finds the largest \c option_id among the options and sets it as \a $this->OptionCounter
 198      */
 199      function resetOptionCounter()
 200      {
 201          $maxValue = 0;
 202          foreach ( $this->Options as $optionList )
 203          {
 204              foreach ( $optionList['optionlist'] as $option )
 205              {
 206                  if ( $maxValue < $option['option_id'] )
 207                  {
 208                      $maxValue = $option['option_id'];
 209                  }
 210              }
 211          }
 212          $this->OptionCounter = $maxValue;
 213      }
 214  
 215      /*!
 216        Change the id of multioption in ascending order.
 217      */
 218      function changeMultiOptionId()
 219      {
 220          $i = 1 ;
 221          foreach ( $this->Options as $key => $opt )
 222          {
 223              $this->Options[$key]['id'] = $i++;
 224          }
 225          $this->MultiOptionCount = $i - 1;
 226      }
 227  
 228      /*!
 229        Remove MultiOption from the array.
 230        After calling this function all the options associated with that multioption will be removed.
 231        This function also calles to changeMultiOption to reset the key value of multioption array.
 232        \param $array_remove is the array of those multiOptions which is selected to remove.
 233        \sa removeOptions()
 234      */
 235      function removeMultiOptions( $array_remove )
 236      {
 237          $options =& $this->Options;
 238          foreach ( $array_remove as $id )
 239          {
 240              unset( $options[ $id - 1 ] );
 241          }
 242          $options = array_values( $options );
 243          $this->changeMultiOptionId();
 244      }
 245  
 246      /*!
 247        Remove Options from the multioption.
 248        This function first remove selected options and then reset the key value if all options for that multioption.
 249        \param $arrayRemove is a list of all array elements which is selected to remove from the multioptions.
 250        \param $optionid is the key value if multioption from which it is required to remove the options.
 251        \sa removeMultiOptions()
 252      */
 253      function removeOptions( $arrayRemove, $optionId )
 254      {
 255          $options =& $this->Options;
 256          foreach ( $arrayRemove as  $id )
 257          {
 258              unset( $options[$optionId]['optionlist'][$id - 1] );
 259          }
 260          $options = array_values( $options );
 261          $i = 1;
 262          foreach ( $options[$optionId]['optionlist'] as $key => $opt )
 263          {
 264              $options[$optionId]['optionlist'][$key]['id'] = $i;
 265              $i++;
 266          }
 267      }
 268  
 269      /*!
 270       \return list of supported attributes
 271      */
 272      function attributes()
 273      {
 274          return array( 'name',
 275                        'multioption_list' );
 276      }
 277  
 278      /*!
 279        Returns true if object have an attribute.
 280        The valid attributes are \c name and \c multioption_list.
 281        \param $name contains the name of attribute
 282      */
 283      function hasAttribute( $name )
 284      {
 285          return in_array( $name, $this->attributes() );
 286      }
 287  
 288      /*!
 289      Returns an attribute. The valid attributes are \c name and \c multioption_list
 290      \a name contains the name of multioption
 291      \a multioption_list contains the list of all multioptions.
 292      */
 293      function &attribute( $name )
 294      {
 295          switch ( $name )
 296          {
 297              case "name" :
 298              {
 299                  return $this->Name;
 300              } break;
 301  
 302              case "multioption_list" :
 303              {
 304                  return $this->Options;
 305              } break;
 306              default:
 307              {
 308                  eZDebug::writeError( "Attribute '$name' does not exist", 'eZMultiOption::attribute' );
 309                  $retValue = null;
 310                  return $retValue;
 311              }break;
 312          }
 313      }
 314  
 315      /*!
 316      Will decode an xml string and initialize the eZ Multi option object.
 317      If $xmlString is on empty then it will call addMultiOption() and addOption() functions
 318      to create new multioption else it will decode the xml string.
 319      \param $smlString contain the complete data structure for multioptions.
 320      \sa xmlString()
 321      */
 322      function decodeXML( $xmlString )
 323      {
 324          $this->OptionCounter = 0;
 325          $this->Options = array();
 326          if ( $xmlString != "" )
 327          {
 328              $xml = new eZXML();
 329              $dom =& $xml->domTree( $xmlString );
 330              $root =& $dom->root();
 331              // set the name of the node
 332              $this->Name = $root->elementTextContentByName( "name" );
 333              $this->OptionCounter = $root->attributeValue("option_counter");
 334              $multioptionsNode = $root->elementByName( "multioptions" );
 335              $multioptionsList = $multioptionsNode->elementsByName( "multioption" );
 336              //Loop for MultiOptions
 337              foreach ( $multioptionsList as $multioption )
 338              {
 339                  $newID = $this->addMultiOption( $multioption->attributeValue( "name" ),
 340                                                  $multioption->attributeValue( "priority" ),
 341                                                  $multioption->attributeValue( "default_option_id" ) );
 342                  $optionNode = $multioption->elementsByName( "option" );
 343                  foreach ( $optionNode as $option )
 344                  {
 345                      $this->addOption( $newID, $option->attributeValue( "option_id" ), $option->attributeValue( "value" ), $option->attributeValue( "additional_price" ) );
 346                  }
 347              }
 348          }
 349          else
 350          {
 351              //The control come here while creaging new object for MultiOption
 352              $nodeID = $this->addMultiOption( "", 0, false );
 353              $this->addOption( $nodeID, "", "", "" );
 354          }
 355      }
 356  
 357      /*!
 358       Will return the XML string for this MultiOption set.
 359       \sa decodeXML()
 360      */
 361      function &xmlString()
 362      {
 363          $doc = new eZDOMDocument( "MultiOption" );
 364          $root = $doc->createElementNode( "ezmultioption" );
 365          $doc->setRoot( $root );
 366          $name = $doc->createElementNode( "name" );
 367          $nameValue = $doc->createTextNode( $this->Name );
 368          $name->appendChild( $nameValue );
 369          $optionCounter = $doc->createAttributeNode( 'option_counter', $this->OptionCounter );
 370          $root->appendAttribute( $optionCounter );
 371  
 372          $root->appendChild( $name );
 373          $multioptions = $doc->createElementNode( "multioptions" );
 374          $root->appendChild( $multioptions );
 375          foreach ( $this->Options as $multioption )
 376          {
 377              unset( $multioptionNode );
 378              $multioptionNode = $doc->createElementNode( "multioption" );
 379              $multioptionNode->appendAttribute( $doc->createAttributeNode( "id", $multioption['id'] ) );
 380              $multioptionNode->appendAttribute( $doc->createAttributeNode( "name", $multioption['name'] ) );
 381              $multioptionNode->appendAttribute( $doc->createAttributeNode( "priority", $multioption['priority'] ) );
 382              $multioptionNode->appendAttribute( $doc->createAttributeNode( 'default_option_id', $multioption['default_option_id'] ) );
 383              foreach ( $multioption['optionlist'] as $option )
 384              {
 385                  unset( $optionNode );
 386                  $optionNode = $doc->createElementNode( "option" );
 387                  $optionNode->appendAttribute( $doc->createAttributeNode( "id", $option['id'] ) );
 388                  $optionNode->appendAttribute( $doc->createAttributeNode( "option_id", $option['option_id'] ) );
 389                  $optionNode->appendAttribute( $doc->createAttributeNode( "value", $option['value'] ) );
 390                  $optionNode->appendAttribute( $doc->createAttributeNode( 'additional_price', $option['additional_price'] ) );
 391                  $multioptionNode->appendChild( $optionNode );
 392              }
 393              $multioptions->appendChild( $multioptionNode );
 394          }
 395          $xml = $doc->toString();
 396          return $xml;
 397      }
 398  
 399      /// \privatesection
 400      /// Contains the Option name
 401      var $Name;
 402      /// Contains the Options
 403      var $Options;
 404      /// Contains the multioption counter value
 405      var $MultiOptionCount;
 406      /// Contains the option counter value
 407      var $OptionCounter;
 408  }
 409  ?>


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