eZ Publish  [4.0]
ezmultioption2.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZMultioOption class
00004 //
00005 // Created on: <07-Jul-2007 15:52:24 sp>
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 eZMultiOption2 ezmultioption2.php
00033   \ingroup eZDatatype
00034   \brief Encapsulates multiple options in one datatype.
00035 */
00036 //include_once( "lib/ezxml/classes/ezxml.php" );
00037 
00038 class eZMultiOption2
00039 {
00040     /*!
00041      Initializes with empty multioption2 list.
00042     */
00043     function eZMultiOption2( $name, $id = 0, $multioptionIDCounter = 0, $optionCounter = 0, $groupID = 0 )
00044     {
00045         $this->Name = $name;
00046         $this->ID = $id;
00047         $this->GroupID = $groupID;
00048         $this->Options = array();
00049         $this->ChildGroupList = array();
00050         $this->MultiOptionIDCounter = $multioptionIDCounter;
00051         $this->OptionCounter = $optionCounter;
00052         $this->GroupIDCounter = $groupID;
00053         $this->Rules = array();
00054     }
00055 
00056     function setGroupIDCounter( $groupIDCounter )
00057     {
00058         $this->GroupIDCounter = $groupIDCounter;
00059     }
00060 
00061     function getGroupIDCounter()
00062     {
00063         return $this->GroupIDCounter;
00064     }
00065     function setMultiOptionIDCounter( $multioptionIDCounter )
00066     {
00067         $this->MultiOptionIDCounter = $multioptionIDCounter;
00068     }
00069 
00070     function getMultiOptionIDCounter()
00071     {
00072         return $this->MultiOptionIDCounter;
00073     }
00074     function setOptionCounter( $optionCounter )
00075     {
00076         $this->OptionCounter = $optionCounter;
00077     }
00078 
00079     function getOptionCounter()
00080     {
00081         return $this->OptionCounter;
00082     }
00083 
00084     function addChildGroup( $group, $multioptionID = false )
00085     {
00086         if ( $group->GroupID == 0 )
00087         {
00088             $this->GroupIDCounter++;
00089             $group->GroupIDCounter = $this->GroupIDCounter;
00090             $group->GroupID = $this->GroupIDCounter;
00091         }
00092         else
00093         {
00094             $this->initCounters( $group );
00095         }
00096         if ( $multioptionID === false )
00097         {
00098             $this->ChildGroupList[] = $group;
00099         }
00100         else
00101         {
00102             $this->Options[$multioptionID]['child_group'] = $group;
00103         }
00104         return count( $this->ChildGroupList );
00105     }
00106 
00107     /*!
00108       Adds an Multioption named \a $name
00109       \param $name contains the name of multioption.
00110       \param $multiOptionPriority is stored for displaying the array in order.
00111       \param $defaultValue is stored to display the options by default.
00112       \return The ID of the multioption that was added.
00113     */
00114     function addMultiOption( $name, $multiOptionPriority, $defaultValue, $multiOptionID )
00115     {
00116         if ( strlen( $multiOptionID ) == 0 )
00117         {
00118             $this->MultiOptionIDCounter++;
00119             $multiOptionID = $this->MultiOptionIDCounter;
00120         }
00121         else
00122         {
00123             if ( $this->MultiOptionIDCounter < $multiOptionID )
00124             {
00125                 $this->MultiOptionIDCounter = $multiOptionID;
00126             }
00127         }
00128 
00129         $this->Options[] = array( "id" => count( $this->Options ),
00130                                   "multioption_id" => $multiOptionID,
00131                                   "name" => $name,
00132                                   'priority'=> $multiOptionPriority,
00133                                   "default_option_id" => $defaultValue,
00134                                   'imageoption' => false,
00135                                   'optionlist' => array() );
00136         return count( $this->Options ) - 1;
00137     }
00138 
00139     /*!
00140       Adds an Option to multioption \a $name
00141       \param $newID is the element key value to which the new option will be added.
00142       \param $optionValue is the original value to display for users.
00143       \param $optionAdditionalPrice is a price value that is used to store price of the option values.
00144     */
00145     function addOption( $newID, $OptionID, $optionValue, $optionAdditionalPrice, $isSelectable = 1, $objectID = 0 )
00146     {
00147         $key = count( $this->Options[$newID]['optionlist'] ) + 1;
00148         if ( strlen( $OptionID ) == 0 )
00149         {
00150             $this->OptionCounter += 1;
00151             $OptionID = $this->OptionCounter;
00152         }
00153         else if ( $OptionID > $this->OptionCounter )
00154         {
00155             $this->OptionCounter = $OptionID;
00156         }
00157         $this->Options[$newID]['optionlist'][] = array( "id" => $key,
00158                                                         "option_id" => $OptionID,
00159                                                         "value" => $optionValue,
00160                                                         'additional_price' => $optionAdditionalPrice,
00161                                                         'is_selectable' => $isSelectable );
00162         if ( $objectID )
00163         {
00164             $this->Options[$newID]['optionlist'][count( $this->Options[$newID]['optionlist'])-1]['object'] = $objectID;
00165             $this->Options[$newID]['imageoption'] = 1;
00166 
00167         }
00168         return $OptionID;
00169     }
00170 
00171     function addOptionForMultioptionID( $multioptionID, $OptionID, $optionValue, $optionAdditionalPrice )
00172     {
00173         $searchResult = $this->findMultiOption( $multioptionID );
00174         if ( !$searchResult )
00175             return null;
00176         $group = $searchResult['group'];
00177         $this->initCounters( $group );
00178         return $group->addOption( $searchResult['id'], $OptionID, $optionValue, $optionAdditionalPrice );
00179     }
00180 
00181     function setObjectForOption( $multioptionID, $optionID, $objectID )
00182     {
00183         $searchResult = $this->findMultiOption( $multioptionID );
00184         if ( !$searchResult )
00185             return null;
00186         $group = $searchResult['group'];
00187 
00188         if ( isset( $group->Options[$searchResult['id']]['optionlist'][$optionID] ) )
00189         {
00190             $group->Options[$searchResult['id']]['optionlist'][$optionID]['object'] = $objectID;
00191             $group->Options[$searchResult['id']]['imageoption'] = 1;
00192         }
00193     }
00194     function removeObjectFromOption( $multioptionID, $optionID )
00195     {
00196         $searchResult = $this->findMultiOption( $multioptionID );
00197         if ( !$searchResult )
00198             return null;
00199         $group = $searchResult['group'];
00200 
00201         if ( isset( $group->Options[$searchResult['id']]['optionlist'][$optionID]['object'] ) )
00202         {
00203             unset( $group->Options[$searchResult['id']]['optionlist'][$optionID]['object'] );
00204             $imageoption = 0;
00205             foreach ( $group->Options[$searchResult['id']]['optionlist'] as $option )
00206             {
00207                 $imageoption = isset( $option['object'] ) ? 1 : 0;
00208             }
00209             $group->Options[$searchResult['id']]['imageoption'] = $imageoption;
00210         }
00211     }
00212 
00213     function findGroup( $groupID, $depth = 0, $groupStack = array() )
00214     {
00215         $groupStack[] = array( $this->attribute( 'group_id' ), $depth );
00216         if ( $depth > 15 )
00217         {
00218             var_dump( $groupStack );
00219             die( "depth=$depth groupID=$groupID" );
00220         }
00221         foreach ( $this->Options as $key => $option )
00222         {
00223             if ( isset( $option['child_group'] ) )
00224             {
00225                 if ( $option['child_group']->attribute( "group_id") == $groupID )
00226                     return $this->Options[$key]['child_group'];
00227                 else
00228                 {
00229                     if ( $group = $this->Options[$key]['child_group']->findGroup( $groupID, $depth + 1, $groupStack ) )
00230                         return $group;
00231                 }
00232             }
00233         }
00234 
00235         foreach ( array_keys( $this->ChildGroupList ) as $key  )
00236         {
00237             if ( $this->ChildGroupList[$key]->attribute( 'group_id' ) == $groupID )
00238             {
00239                 return $this->ChildGroupList[$key];
00240             }
00241 
00242             if ( $group = $this->ChildGroupList[$key]->findGroup( $groupID, $depth + 1, $groupStack ) )
00243                 return $group;
00244         }
00245         $dummyGroup = null;
00246         return $dummyGroup;
00247     }
00248 
00249     function findMultiOption( $multioptionID, $depth = 0 )
00250     {
00251         foreach ( $this->Options as $key => $optionList )
00252         {
00253             if ( $optionList['multioption_id'] == $multioptionID )
00254             {
00255                 $option = $this->Options[$key];
00256                 return array( "group" => $this,
00257                               "id" => $option['id'] );
00258             }
00259             if ( isset( $optionList['child_group'] ) )
00260             {
00261                 $group = $this->Options[$key]['child_group'];
00262                 $returnArray = $group->findMultiOption( $multioptionID,$depth+1 );
00263                 if ( $returnArray )
00264                 {
00265                     return $returnArray;
00266                 }
00267             }
00268         }
00269 
00270         foreach ( array_keys( $this->ChildGroupList ) as $key  )
00271         {
00272             $returnArray =$this->ChildGroupList[$key]->findMultiOption( $multioptionID,$depth+1 );
00273             if ( $returnArray )
00274             {
00275                 return $returnArray;
00276             }
00277         }
00278         return null;
00279     }
00280 
00281     function findOption( $multioption, $optionID )
00282     {
00283         if ( $multioption )
00284         {
00285             $optionFound = false;
00286             foreach( $multioption['optionlist'] as $option )
00287             {
00288                 if ( $option['option_id'] == $optionID )
00289                 {
00290                     $option['multioption_name'] = $multioption['name'];
00291                     $optionFound = $option;
00292                     break;
00293                 }
00294             }
00295             return $optionFound;
00296         }
00297 
00298         foreach ( $this->Options as $key => $optionList )
00299         {
00300             if( $option = $this->findOption( $optionList, $optionID ) )
00301             {
00302                 return $option;
00303             }
00304             else if ( array_key_exists( 'child_group',  $optionList ) && $optionList['child_group'] )
00305             {
00306                 if ( $option = $optionList['child_group']->findOption( false, $optionID ) )
00307                      return $option;
00308             }
00309         }
00310 
00311         foreach ( array_keys( $this->ChildGroupList ) as $key  )
00312         {
00313             if ( $option = $this->ChildGroupList[$key]->findOption( false, $optionID ) )
00314                     return $option;
00315         }
00316         return false;
00317     }
00318 
00319     function runFunctionForAllGroups( $func, $params )
00320     {
00321         foreach ( $this->Options as $key => $optionList )
00322         {
00323             if ( isset( $optionList['child_group'] ) )
00324             {
00325                 $optionList['child_group']->runFunctionForAllGroups( $func, $params );
00326                 $optionList['child_group']->$func( $params );
00327             }
00328         }
00329         foreach ( array_keys( $this->ChildGroupList ) as $key  )
00330         {
00331             $this->ChildGroupList[$key]->runFunctionForAllGroups( $func, $params );
00332             $this->ChildGroupList[$key]->$func( $params );
00333         }
00334     }
00335 
00336 
00337     function resetCounters()
00338     {
00339         $this->resetOptionCounter();
00340     }
00341 
00342     /*!
00343       Finds the largest \c option_id among the options and sets it as \a $this->OptionCounter
00344     */
00345     function resetOptionCounter()
00346     {
00347         $maxValue = 0;
00348         foreach ( $this->Options as $optionList )
00349         {
00350             foreach ( $optionList['optionlist'] as $option )
00351             {
00352                 if ( $maxValue < $option['option_id'] )
00353                 {
00354                     $maxValue = $option['option_id'];
00355                 }
00356             }
00357         }
00358         $this->OptionCounter = $maxValue;
00359     }
00360 
00361     /*!
00362       Change the id of multioption in ascending order.
00363     */
00364     function changeMultiOptionId()
00365     {
00366         $i = 0 ;
00367         foreach ( $this->Options as $key => $opt )
00368         {
00369             $this->Options[$key]['id'] = $i++;
00370         }
00371         $this->MultiOptionCount = $i - 1;
00372     }
00373 
00374     function removeChildGroup( $groupID, $depth = 0 )
00375     {
00376         if ( $depth > 15 )
00377             die( "depth=$depth" );
00378         $removed = false;
00379         foreach ( $this->Options as $key => $option )
00380         {
00381             if ( isset( $option['child_group'] ) )
00382             {
00383                  if (  $option['child_group']->attribute( "group_id" ) == $groupID )
00384                  {
00385                      unset( $this->Options[$key]['child_group'] );
00386                      return true;
00387                  }
00388                  else
00389                  {
00390                      if ( $option['child_group']->removeChildGroup( $groupID, $depth + 1 ) )
00391                          return true;
00392                  }
00393             }
00394         }
00395 
00396         foreach ( array_keys( $this->ChildGroupList ) as $key  )
00397         {
00398             if ( $this->ChildGroupList[$key]->attribute( 'group_id' ) == $groupID )
00399             {
00400                 unset( $this->ChildGroupList[$key] );
00401                 return true;
00402             }
00403             if ( $this->ChildGroupList[$key]->removeChildGroup( $groupID, $depth + 1 ) )
00404                 return true;
00405         }
00406         return false;
00407     }
00408 
00409     /*!
00410       Remove MultiOption from the array.
00411       After calling this function all the options associated with that multioption will be removed.
00412       This function also calles to changeMultiOption to reset the key value of multioption array.
00413       \param $array_remove is the array of those multiOptions which is selected to remove.
00414       \sa removeOptions()
00415     */
00416     function removeMultiOptions( $array_remove )
00417     {
00418         foreach ( $array_remove as $id )
00419         {
00420             unset( $this->Options[ $id ] );
00421         }
00422         $this->Options = array_values( $this->Options );
00423         $this->changeMultiOptionId();
00424     }
00425 
00426 
00427     /*!
00428       Remove Options from the multioption.
00429       This function first remove selected options and then reset the key value if all options for that multioption.
00430       \param $arrayRemove is a list of all array elements which is selected to remove from the multioptions.
00431       \param $optionid is the key value if multioption from which it is required to remove the options.
00432       \sa removeMultiOptions()
00433     */
00434     function removeOptions( $arrayRemove, $optionId )
00435     {
00436         foreach ( $arrayRemove as  $id )
00437         {
00438             unset( $this->Options[$optionId]['optionlist'][$id - 1] );
00439         }
00440         $this->Options = array_values( $this->Options );
00441         $i = 1;
00442         foreach ( $this->Options[$optionId]['optionlist'] as $key => $opt )
00443         {
00444             $this->Options[$optionId]['optionlist'][$key]['id'] = $i;
00445             $i++;
00446         }
00447     }
00448 
00449     function getIDsFromMultioptions( $params)
00450     {
00451         if ( $params['group'] )
00452         {
00453             foreach ( $this->Options as $multioption )
00454             {
00455                 $params['group']->MultioptionIDList[] = $multioption['multioption_id'];
00456                 foreach ( $multioption['optionlist'] as $option )
00457                 {
00458                     $params['group']->OptionIDList[] = $option['option_id'];
00459                 }
00460             }
00461         }
00462     }
00463 
00464     function cleanupRules( )
00465     {
00466         $this->runFunctionForAllGroups( 'getIDsFromMultioptions', array( 'group' => $this ) );
00467         foreach( $this->Rules as $key => $ruleForOption )
00468         {
00469             if ( ! in_array( $key, $this->OptionIDList ) )
00470             {
00471                 unset( $this->Rules[$key] );
00472                 continue;
00473             }
00474 
00475             foreach ( $ruleForOption as $moption => $rule )
00476             {
00477                 if ( !in_array( $moption, $this->MultioptionIDList ) )
00478                 {
00479                     unset( $this->Rules[$key][$moption] );
00480                     if ( count( $this->Rules[$key] ) == 0 )
00481                     {
00482                         unset( $this->Rules[$key] );
00483                         break;
00484                     }
00485                     continue;
00486                 }
00487                 foreach( $rule  as $index =>$optionID )
00488                 {
00489                     if ( !in_array( $optionID, $this->OptionIDList ) )
00490                     {
00491                         unset( $this->Rules[$key][$moption][$index] );
00492                         if( count( $rule ) == 0 )
00493                         {
00494                             unset( $this->Rules[$key][$moption] );
00495                             break;
00496                         }
00497                         continue;
00498                     }
00499 
00500                 }
00501                 if( count( $this->Rules[$key] ) == 0 )
00502                 {
00503                     unset( $this->Rules[$key] );
00504                     break;
00505                 }
00506             }
00507         }
00508     }
00509 
00510     function addOptionToRules( $multioptionID, $optionID )
00511     {
00512         $rules = $this->Rules;
00513         foreach( $this->Rules as $key => $ruleForOption )
00514         {
00515             foreach ( $ruleForOption as $moption => $rule )
00516             {
00517                 if ( $multioptionID == $moption )
00518                 {
00519                     $this->Rules[$key][$moption][] = $optionID;
00520                 }
00521             }
00522         }
00523     }
00524 
00525     /*!
00526      \return list of supported attributes
00527     */
00528     function attributes()
00529     {
00530         return array( 'name',
00531                       'id',
00532                       'group_id',
00533                       'rules',
00534                       'multioption_list',
00535                       'optiongroup_list' );
00536     }
00537 
00538     /*!
00539       Returns true if object have an attribute.
00540       The valid attributes are \c name and \c multioption_list.
00541       \param $name contains the name of attribute
00542     */
00543     function hasAttribute( $name )
00544     {
00545         return in_array( $name, $this->attributes() );
00546     }
00547 
00548     /*!
00549     Returns an attribute. The valid attributes are \c name and \c multioption_list
00550     \a name contains the name of multioption
00551     \a multioption_list contains the list of all multioptions.
00552     */
00553     function attribute( $name )
00554     {
00555         switch ( $name )
00556         {
00557             case "name" :
00558             {
00559                 return $this->Name;
00560             } break;
00561             case "id" :
00562             {
00563                 return $this->ID;
00564             } break;
00565             case "group_id":
00566             {
00567                 return $this->GroupID;
00568             } break;
00569             case "rules":
00570             {
00571                 return $this->Rules;
00572             } break;
00573             case "multioption_list" :
00574             {
00575                 return $this->Options;
00576             } break;
00577             case "optiongroup_list":
00578             {
00579                 return $this->ChildGroupList;
00580             } break;
00581             default:
00582             {
00583                 eZDebug::writeError( "Attribute '$name' does not exist", 'eZMultiOption::attribute' );
00584                 $retValue = null;
00585                 return $retValue;
00586             }break;
00587         }
00588     }
00589 
00590     function initCountersRecursive()
00591     {
00592         $this->runFunctionForAllGroups( 'initCounters', $this );
00593     }
00594 
00595     function initCounters( $group )
00596     {
00597         if ( $this->GroupIDCounter < $this->attribute( 'group_id' ) )
00598             $this->GroupIDCounter = $this->attribute( 'group_id' );
00599 
00600         if ( $this->GroupIDCounter < $group->getGroupIDCounter() )
00601             $this->GroupIDCounter = $group->getGroupIDCounter();
00602 
00603         if ( $this->OptionCounter < $group->getOptionCounter() )
00604             $this->OptionCounter = $group->getOptionCounter();
00605 
00606         if ( $this->getMultiOptionIDCounter() < $group->getMultiOptionIDCounter() )
00607             $this->setMultiOptionIDCounter( $group->getMultiOptionIDCounter() );
00608     }
00609 
00610     /*!
00611     Will decode an xml string and initialize the eZ Multi option object.
00612     If $xmlString is on empty then it will call addMultiOption() and addOption() functions
00613     to create new multioption else it will decode the xml string.
00614     \param $smlString contain the complete data structure for multioptions.
00615     \sa xmlString()
00616     */
00617     function decodeXML( $xmlString )
00618     {
00619         $this->OptionCounter = 0;
00620         $this->Options = array();
00621         if ( $xmlString != "" )
00622         {
00623             $dom = new DOMDocument( '1.0', 'utf-8' );
00624             $success = $dom->loadXML( $xmlString );
00625 
00626             $root = $dom->documentElement;
00627 
00628             if ( $root->localName == 'ezmultioption' )
00629             {
00630                 $this->initFromXMLCompat( $root );
00631                 return;
00632             }
00633 
00634             $this->initGroupFromDom( $root );
00635 
00636             $rulesNode = $root->getElementsByTagName( "rules" )->item( 0 );
00637             if ( !$rulesNode )
00638                 return;
00639             $ruleList = $rulesNode->getElementsByTagName( "rule" );
00640             //Loop for rules
00641             $rules = array();
00642             foreach ( $ruleList as $ruleNode )
00643             {
00644                 $optionID = $ruleNode->getAttribute( "option_id" );
00645                 $ruleDataNodeList = $ruleNode->getElementsByTagName( 'rule_data' );
00646                 $ruleForOption = array();
00647                 foreach ( $ruleDataNodeList as $ruleDataNode )
00648                 {
00649                     $multioptionID = $ruleDataNode->getAttribute( "multioption_id" );
00650                     $includeOptionNodeList = $ruleDataNode->getElementsByTagName( 'option_id' );
00651                     $includeOptions = array();
00652                     foreach ( $includeOptionNodeList as $includeOptionNode )
00653                     {
00654                         $includeOptions[] = $includeOptionNode->textContent;
00655                     }
00656                     $ruleForOption[$multioptionID] = $includeOptions;
00657                 }
00658                 $rules[$optionID] = $ruleForOption;
00659             }
00660             $this->Rules = $rules;
00661         }
00662         else
00663         {
00664             //The control come here while creaging new object for MultiOption
00665             $group = new eZMultiOption2( '' );
00666             $this->addChildGroup( $group );
00667             $nodeID = $group->addMultiOption( "", 0, false , '' );
00668             $group->addOption( $nodeID, "", "", "" );
00669             $this->initCounters( $group );
00670             unset( $group);
00671         }
00672     }
00673 
00674     function initFromXMLCompat( $root, $new = false )
00675     {
00676         if ( $root && $root->getAttribute( 'option_counter' ) > 0 )
00677         {
00678             $this->Name = '';
00679             $this->OptionCounter = 0;
00680             $this->MultiOptionIDCounter = 0;
00681             $this->GroupIDCounter = 1;
00682             $this->GroupID = 0;
00683             $this->ID = 0;
00684 
00685             $multiOptionGroup = new eZMultiOption2( '', 0, 0, 0, 1 );
00686             $multiOptionGroup->initCounters( $this );
00687             $multiOptionGroup->initGroupFromDom( $root );
00688             $this->initCounters( $multiOptionGroup );
00689             $this->ChildGroupList[] = $multiOptionGroup;
00690 
00691         }
00692     }
00693 
00694     function initGroupFromDom( $root, $new = false )
00695     {
00696         $xpath = new DOMXPath( $root->ownerDocument );
00697 
00698         if ( $root && $root->getAttribute("option_counter") > 0 )
00699         {
00700             // set the name of the node
00701             $this->Name = $xpath->query( 'name', $root )->item( 0 )->textContent;
00702             $this->OptionCounter = $root->getAttribute("option_counter");
00703             $this->MultiOptionIDCounter = $root->getAttribute("multioption_counter")
00704                                           ?  $root->getAttribute("multioption_counter")
00705                                           : $this->MultiOptionIDCounter;
00706 
00707             $this->GroupIDCounter = $root->getAttribute( 'group_counter ') ? $root->getAttribute( 'group_counter ') : $this->GroupIDCounter;
00708             $this->GroupID = $root->getAttribute( 'group_id' ) ? $root->getAttribute( 'group_id' ) : $this->GroupID ;
00709             $this->ID = $root->getAttribute( 'id' ) ? $root->getAttribute( 'id' ) : $this->ID;
00710 
00711             $multioptionsList = $xpath->query( "multioptions/multioption", $root );
00712             //Loop for MultiOptions
00713             foreach ( $multioptionsList as $multioption )
00714             {
00715                 $newID = $this->addMultiOption( $multioption->getAttribute( "name" ),
00716                                                 $multioption->getAttribute( "priority" ),
00717                                                 $multioption->getAttribute( "default_option_id" ),
00718                                                 $multioption->getAttribute( "multioption_id" ) );
00719 
00720                 $optionNode = $xpath->query( "option", $multioption );
00721                 foreach ( $optionNode as $option )
00722                 {
00723                     $isSelectable = $option->getAttribute( "is_selectable" ) === false ? 1 : $option->getAttribute( "is_selectable" );
00724                     $this->addOption( $newID,
00725                                       $option->getAttribute( "option_id" ),
00726                                       $option->getAttribute( "value" ),
00727                                       $option->getAttribute( "additional_price" ),
00728                                       $isSelectable,
00729                                       $option->getAttribute( "object" ) );
00730                 }
00731                 $groupNode = $xpath->query( "optiongroup", $multioption )->item( 0 );
00732                 if( $groupNode )
00733                 {
00734                     $multiOptionGroup = new eZMultiOption2( '' );
00735                     $multiOptionGroup->initCounters( $this );
00736                     $multiOptionGroup->initGroupFromDom( $groupNode );
00737                     $this->initCounters( $multiOptionGroup );
00738                     $this->Options[$newID]['child_group'] = $multiOptionGroup;
00739                 }
00740 
00741             }
00742             $this->changeMultiOptionId();
00743 
00744             $groupList = $xpath->query( "groups/optiongroup", $root );
00745             foreach ( $groupList as $group )
00746             {
00747                 $multiOptionGroup = new eZMultiOption2( '' );
00748                 $multiOptionGroup->initCounters( $this );
00749                 $multiOptionGroup->initGroupFromDom( $group );
00750                 $this->initCounters( $multiOptionGroup );
00751                 $this->ChildGroupList[] = $multiOptionGroup;
00752             }
00753 
00754         }
00755         else
00756         {
00757             //The control come here while creating new object for MultiOption
00758             if ( $new )
00759             {
00760                 $nodeID = $this->addMultiOption( "", 0, false, '' );
00761                 $this->addOption( $nodeID, "", "", "" );
00762             }
00763         }
00764 
00765     }
00766 
00767     /*!
00768      Will return the XML string for this MultiOption set.
00769      \sa decodeXML()
00770     */
00771     function xmlString()
00772     {
00773         $doc = new DOMDocument( '1.0', 'utf-8' );
00774         $root = $doc->createElement( "ezmultioption2" );
00775         $doc->appendChild( $root );
00776 
00777 
00778         $this->createDomElementForGroup( $doc, $root );
00779 
00780         $rulesNode = $doc->createElement( "rules" );
00781 
00782         foreach ( $this->Rules as $ruleFor => $rule )
00783         {
00784             unset( $ruleNode );
00785             $ruleNode = $doc->createElement( "rule" );
00786             $ruleNode->setAttribute( "option_id", $ruleFor );
00787             foreach ( $rule as $multioptionID => $ruleData )
00788             {
00789                 unset( $ruleDataNode );
00790                 $ruleDataNode = $doc->createElement( "rule_data" );
00791                 $ruleDataNode->setAttribute( "multioption_id", $multioptionID );
00792                 foreach ( $ruleData as $optionID )
00793                 {
00794                     unset( $includeNode );
00795                     $includeNode = $doc->createElement( "option_id", $optionID );
00796                     $ruleDataNode->appendChild( $includeNode );
00797                 }
00798                 $ruleNode->appendChild( $ruleDataNode );
00799             }
00800             $rulesNode->appendChild( $ruleNode );
00801         }
00802         $root->appendChild( $rulesNode );
00803 
00804         $xml = $doc->saveXML();
00805         return $xml;
00806     }
00807 
00808     function createDomElementForGroup( $doc, $groupNode, $depth = 0 )
00809     {
00810         $root = $groupNode;
00811         $root->setAttribute( 'option_counter', $this->OptionCounter );
00812         $root->setAttribute( 'multioption_counter', $this->MultiOptionIDCounter );
00813         $root->setAttribute( 'group_counter', $this->GroupIDCounter );
00814 
00815         $root->setAttribute( 'group_id', $this->GroupID );
00816         $root->setAttribute( 'id', $this->ID );
00817 
00818         $name = $doc->createElement( "name", $this->Name );
00819         $root->appendChild( $name );
00820 
00821         $multioptions = $doc->createElement( "multioptions" );
00822         $root->appendChild( $multioptions );
00823 
00824         foreach ( $this->Options as $multioption )
00825         {
00826             unset( $multioptionNode );
00827             $multioptionNode = $doc->createElement( "multioption" );
00828             $multioptionNode->setAttribute( "id", $multioption['id'] );
00829             $multioptionNode->setAttribute( "name", $multioption['name'] );
00830             $multioptionNode->setAttribute( "multioption_id", $multioption['multioption_id'] );
00831             $multioptionNode->setAttribute( "priority", $multioption['priority'] );
00832             $multioptionNode->setAttribute( 'default_option_id', $multioption['default_option_id'] );
00833 
00834             if ( isset( $multioption['imageoption'] ) && $multioption['imageoption'] )
00835                     $multioptionNode->setAttribute( "imageoption", $multioption['imageoption'] );
00836 
00837             foreach ( $multioption['optionlist'] as $option )
00838             {
00839                 unset( $optionNode );
00840                 $optionNode = $doc->createElement( "option" );
00841                 $optionNode->setAttribute( "id", $option['id'] );
00842                 $optionNode->setAttribute( "option_id", $option['option_id'] );
00843                 $optionNode->setAttribute( "value", $option['value'] );
00844 
00845                 if ( isset( $option['object'] ) && $option['object']  )
00846                 {
00847                     $optionNode->setAttribute( "object", $option['object'] );
00848                 }
00849                 $optionNode->setAttribute( 'additional_price', $option['additional_price'] );
00850                 $optionNode->setAttribute( 'is_selectable', $option['is_selectable'] );
00851                 $multioptionNode->appendChild( $optionNode );
00852             }
00853             if ( array_key_exists( 'child_group', $multioption ) && $multioption['child_group'] )
00854             {
00855                 $childGroup = $multioption['child_group'];
00856                 unset( $childGroupNode );
00857                 $childGroupNode = $doc->createElement( "optiongroup" );
00858                 $childGroupNode->setAttribute( "id", $childGroup->ID );
00859                 $childGroup->createDomElementForGroup( $doc, $childGroupNode, $depth + 1 );
00860                 $multioptionNode->appendChild( $childGroupNode );
00861             }
00862             $multioptions->appendChild( $multioptionNode );
00863         }
00864 
00865         $groups = $doc->createElement( "groups" );
00866 
00867         foreach ( $this->ChildGroupList as $childGroup )
00868         {
00869             unset( $childGroupNode );
00870             $childGroupNode = $doc->createElement( "optiongroup" );
00871             $childGroupNode->setAttribute( "id", $childGroup->ID );
00872             $childGroup->createDomElementForGroup( $doc, $childGroupNode, $depth + 1 );
00873             $groups->appendChild( $childGroupNode );
00874         }
00875         $root->appendChild( $groups );
00876         return $root;
00877 
00878     }
00879     /// \privatesection
00880     /// Contains the Option name
00881     public $Name;
00882     public $GroupID;
00883     /// Contains the Options
00884     public $Options;
00885     /// Contains the multioption counter value
00886     public $MultiOptionIDCounter;
00887     public $GroupIDCounter;
00888     /// Contains the option counter value
00889     public $OptionCounter;
00890     public $ChildGroupList;
00891     public $MultioptionIDList = array();
00892     public $OptionIDList = array();
00893 }
00894 ?>