eZ Publish  [4.0]
ezoption.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZOption class
00004 //
00005 // Created on: <28-Jun-2002 11:05:48 bf>
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 eZOption ezoption.php
00033   \ingroup eZDatatype
00034   \brief eZOption handles option set datatypes
00035 
00036   \code
00037 
00038   //include_once( "kernel/classes/datatypes/ezoption/ezoption.php" );
00039 
00040   $option = new eZOption( "Colour" );
00041   $option->addValue( "Red" );
00042   $option->addValue( "Green" );
00043 
00044   // Serialize the class to an XML document
00045   $xmlString = $option->xmlString();
00046 
00047   \endcode
00048 */
00049 
00050 class eZOption
00051 {
00052     /*!
00053     */
00054     function eZOption( $name )
00055     {
00056         $this->Name = $name;
00057         $this->Options = array();
00058         $this->OptionCount = 0;
00059     }
00060 
00061     /*!
00062      Sets the name of the option
00063     */
00064     function setName( $name )
00065     {
00066         $this->Name = $name;
00067     }
00068 
00069 
00070     /*!
00071      Returns the name of the option set.
00072     */
00073     function name()
00074     {
00075         return $this->Name;
00076     }
00077 
00078     /*!
00079      Adds an option
00080     */
00081     function addOption( $valueArray )
00082     {
00083         $value = isset( $valueArray['value'] ) ? $valueArray['value'] : '';
00084         $additional_price = isset( $valueArray['additional_price'] ) ? $valueArray['additional_price'] : '';
00085         $this->Options[] = array( "id" => $this->OptionCount,
00086                                   "value" => $value,
00087                                   'additional_price' => $additional_price,
00088                                   "is_default" => false );
00089 
00090         $this->OptionCount += 1;
00091     }
00092 
00093     function insertOption( $valueArray, $beforeID )
00094     {
00095         array_splice( $this->Options, $beforeID, 0 ,  array( array( "id" => $this->OptionCount,
00096                                                                     "value" => $valueArray['value'],
00097                                                                     'additional_price' => $valueArray['additional_price'],
00098                                                                     "is_default" => false ) ) );
00099         $this->OptionCount += 1;
00100     }
00101 
00102     function removeOptions( $array_remove )
00103     {
00104         $shiftvalue = 0;
00105         foreach( $array_remove as $id )
00106         {
00107             array_splice( $this->Options, $id - $shiftvalue, 1 );
00108             $shiftvalue++;
00109         }
00110         $this->OptionCount -= $shiftvalue;
00111     }
00112 
00113     function attributes()
00114     {
00115         return array( 'name',
00116                       'option_list' );
00117     }
00118 
00119     function hasAttribute( $name )
00120     {
00121         return in_array( $name, $this->attributes() );
00122     }
00123 
00124     function attribute( $name )
00125     {
00126         switch ( $name )
00127         {
00128             case "name" :
00129             {
00130                 return $this->Name;
00131             }break;
00132             case "option_list" :
00133             {
00134                 return $this->Options;
00135             }break;
00136             default:
00137             {
00138                 eZDebug::writeError( "Attribute '$name' does not exist", 'eZOption::attribute' );
00139                 return null;
00140             }break;
00141         }
00142     }
00143 
00144     /*!
00145      Will decode an xml string and initialize the eZ option object
00146     */
00147     function decodeXML( $xmlString )
00148     {
00149         if ( $xmlString != "" )
00150         {
00151             $dom = new DOMDocument( '1.0', 'utf-8' );
00152             $success = $dom->loadXML( $xmlString );
00153 
00154             // set the name of the node
00155             $nameNode = $dom->getElementsByTagName( "name" )->item( 0 );
00156             $this->setName( $nameNode->textContent );
00157 
00158             $optionNodes = $dom->getElementsByTagName( "option" );
00159             $this->OptionCount = 0;
00160 
00161             foreach ( $optionNodes as $optionNode )
00162             {
00163                 $this->addOption( array( 'value' => $optionNode->textContent,
00164                                          'additional_price' => $optionNode->getAttribute( 'additional_price' ) ) );
00165             }
00166         }
00167         else
00168         {
00169             $this->addOption( "" );
00170             $this->addOption( "" );
00171         }
00172     }
00173 
00174     /*!
00175      Will return the XML string for this option set.
00176     */
00177     function xmlString( )
00178     {
00179         $doc = new DOMDocument( '1.0', 'utf-8' );
00180 
00181         $root = $doc->createElement( "ezoption" );
00182         $doc->appendChild( $root );
00183 
00184         $name = $doc->createElement( "name", $this->Name );
00185         $root->appendChild( $name );
00186 
00187         $options = $doc->createElement( "options" );
00188         $root->appendChild( $options );
00189 
00190         $id=0;
00191         foreach ( $this->Options as $option )
00192         {
00193             unset( $optionNode );
00194             $optionNode = $doc->createElement( "option", $option["value"] );
00195             $optionNode->setAttribute( "id", $option['id'] );
00196             $optionNode->setAttribute( 'additional_price', $option['additional_price'] );
00197             $options->appendChild( $optionNode );
00198         }
00199 
00200         $xml = $doc->saveXML();
00201 
00202         return $xml;
00203     }
00204 
00205     /// Contains the Option name
00206     public $Name;
00207 
00208     /// Contains the Options
00209     public $Options;
00210 
00211     /// Contains the option counter value
00212     public $OptionCount;
00213 }
00214 
00215 ?>