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