|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZOption class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package kernel 00009 */ 00010 00011 /*! 00012 \class eZOption ezoption.php 00013 \ingroup eZDatatype 00014 \brief eZOption handles option set datatypes 00015 00016 \code 00017 00018 $option = new eZOption( "Colour" ); 00019 $option->addValue( "Red" ); 00020 $option->addValue( "Green" ); 00021 00022 // Serialize the class to an XML document 00023 $xmlString = $option->xmlString(); 00024 00025 \endcode 00026 */ 00027 00028 class eZOption 00029 { 00030 function eZOption( $name ) 00031 { 00032 $this->Name = $name; 00033 $this->Options = array(); 00034 $this->OptionCount = 0; 00035 } 00036 00037 /*! 00038 Sets the name of the option 00039 */ 00040 function setName( $name ) 00041 { 00042 $this->Name = $name; 00043 } 00044 00045 00046 /*! 00047 Returns the name of the option set. 00048 */ 00049 function name() 00050 { 00051 return $this->Name; 00052 } 00053 00054 /*! 00055 Adds an option 00056 */ 00057 function addOption( $valueArray ) 00058 { 00059 $value = isset( $valueArray['value'] ) ? $valueArray['value'] : ''; 00060 $additional_price = isset( $valueArray['additional_price'] ) ? $valueArray['additional_price'] : ''; 00061 $this->Options[] = array( "id" => $this->OptionCount, 00062 "value" => $value, 00063 'additional_price' => $additional_price, 00064 "is_default" => false ); 00065 00066 $this->OptionCount += 1; 00067 } 00068 00069 function insertOption( $valueArray, $beforeID ) 00070 { 00071 array_splice( $this->Options, $beforeID, 0 , array( array( "id" => $this->OptionCount, 00072 "value" => $valueArray['value'], 00073 'additional_price' => $valueArray['additional_price'], 00074 "is_default" => false ) ) ); 00075 $this->OptionCount += 1; 00076 } 00077 00078 function removeOptions( $array_remove ) 00079 { 00080 $shiftvalue = 0; 00081 foreach( $array_remove as $id ) 00082 { 00083 array_splice( $this->Options, $id - $shiftvalue, 1 ); 00084 $shiftvalue++; 00085 } 00086 $this->OptionCount -= $shiftvalue; 00087 } 00088 00089 function attributes() 00090 { 00091 return array( 'name', 00092 'option_list' ); 00093 } 00094 00095 function hasAttribute( $name ) 00096 { 00097 return in_array( $name, $this->attributes() ); 00098 } 00099 00100 function attribute( $name ) 00101 { 00102 switch ( $name ) 00103 { 00104 case "name" : 00105 { 00106 return $this->Name; 00107 }break; 00108 case "option_list" : 00109 { 00110 return $this->Options; 00111 }break; 00112 default: 00113 { 00114 eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ ); 00115 return null; 00116 }break; 00117 } 00118 } 00119 00120 /*! 00121 Will decode an xml string and initialize the eZ option object 00122 */ 00123 function decodeXML( $xmlString ) 00124 { 00125 if ( $xmlString != "" ) 00126 { 00127 $dom = new DOMDocument( '1.0', 'utf-8' ); 00128 $success = $dom->loadXML( $xmlString ); 00129 00130 // set the name of the node 00131 $nameNode = $dom->getElementsByTagName( "name" )->item( 0 ); 00132 $this->setName( $nameNode->textContent ); 00133 00134 $optionNodes = $dom->getElementsByTagName( "option" ); 00135 $this->OptionCount = 0; 00136 00137 foreach ( $optionNodes as $optionNode ) 00138 { 00139 $this->addOption( array( 'value' => $optionNode->textContent, 00140 'additional_price' => $optionNode->getAttribute( 'additional_price' ) ) ); 00141 } 00142 } 00143 else 00144 { 00145 $this->addOption( "" ); 00146 $this->addOption( "" ); 00147 } 00148 } 00149 00150 /*! 00151 Will return the XML string for this option set. 00152 */ 00153 function xmlString( ) 00154 { 00155 $doc = new DOMDocument( '1.0', 'utf-8' ); 00156 00157 $root = $doc->createElement( "ezoption" ); 00158 $doc->appendChild( $root ); 00159 00160 $name = $doc->createElement( "name" ); 00161 $name->appendChild( $doc->createCDATASection( $this->Name ) ); 00162 $root->appendChild( $name ); 00163 00164 $options = $doc->createElement( "options" ); 00165 $root->appendChild( $options ); 00166 00167 foreach ( $this->Options as $option ) 00168 { 00169 $optionNode = $doc->createElement( "option" ); 00170 $optionNode->appendChild( $doc->createCDATASection( $option["value"] ) ); 00171 $optionNode->setAttribute( "id", $option['id'] ); 00172 $optionNode->setAttribute( 'additional_price', $option['additional_price'] ); 00173 $options->appendChild( $optionNode ); 00174 } 00175 00176 $xml = $doc->saveXML(); 00177 00178 return $xml; 00179 } 00180 00181 /// Contains the Option name 00182 public $Name; 00183 00184 /// Contains the Options 00185 public $Options; 00186 00187 /// Contains the option counter value 00188 public $OptionCount; 00189 } 00190 00191 ?>