/**
  * Creates a new optionset field.
  * @param name The field name
  * @param title The field title
  * @param source DataObjectSet
  * @param value The current value
  * @param form The parent form
  */
 function __construct($name, $title = "", $addresses = null, $value = "", $form = null)
 {
     $this->addresses = $addresses;
     $source = array();
     if ($this->addresses) {
         $source = $this->addresses->map("ID", "FullString");
     }
     parent::__construct($name, $title, $source, $value, $form);
 }
 function getCMSFields()
 {
     if ($this->FolderID) {
         // New token - select file:
         $folder = DataObject::get_by_id('Folder', $this->FolderID);
         $files = new DataObjectSet();
         if ($folder->myChildren()) {
             foreach ($folder->myChildren() as $file) {
                 if (!$file instanceof Folder) {
                     $files->push($file);
                 }
             }
             $files->sort('Name');
         }
         $fileField = new DropdownField('FileID', 'File', $files->map('ID', 'Name'));
     } else {
         // Existing token:
         $fileField = new ReadonlyField('FileDummy', 'File', $this->File()->Name);
     }
     $fields = new FieldSet();
     $fields->push($root = new TabSet('Root'));
     $root->push($main = new Tab('Main'));
     $main->push($fileField);
     if (ClassInfo::exists('DatetimeField')) {
         // 2.4.x
         $main->push($expiry_field = new DatetimeField('Expiry', 'Expiry'));
         $expiry_field->getDateField()->setConfig('showcalendar', true);
         $expiry_field->getTimeField()->setConfig('showdropdown', true);
     } else {
         // 2.3.x
         $main->push($expiry_field = new PopupDateTimeField('Expiry', 'Expiry'));
     }
     $main->push(new ReadonlyField('MemberDummyField', 'Member', $this->MemberNice()));
     if ($this->ID) {
         $main->push(new ReadonlyField('Token', 'Token'));
     }
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }
 private function checkNewsPageDataObjectSet(DataObjectSet $newsPages, array $expectedURLSegments)
 {
     $actualURLSegments = array_values($newsPages->map('ID', 'URLSegment'));
     $this->assertEquals($expectedURLSegments, $actualURLSegments);
 }
 /**
  * Get options for a product and return for use in the form
  * Must get options for nextAttributeID, but these options should be filtered so 
  * that only the options for the variations that match attributeID and optionID
  * are returned.
  * 
  * In other words, do not just return options for a product, return options for product
  * variations.
  * 
  * Usually called via AJAX.
  * 
  * @param SS_HTTPRequest $request
  * @return String JSON encoded string for use to update options in select fields on Product page
  */
 public function options(SS_HTTPRequest $request)
 {
     $data = array();
     $product = $this->data();
     $options = new DataObjectSet();
     $variations = $product->Variations();
     $filteredVariations = new DataObjectSet();
     $attributeOptions = $request->postVar('Options');
     $nextAttributeID = $request->postVar('NextAttributeID');
     //Filter variations to match attribute ID and option ID
     //Variations need to have the same option for each attribute ID in POST data to be considered
     if ($variations && $variations->exists()) {
         foreach ($variations as $variation) {
             $variationOptions = array();
             //if ($attributeOptions && is_array($attributeOptions)) {
             foreach ($attributeOptions as $attributeID => $optionID) {
                 //Get option for attribute ID, if this variation has options for every attribute in the array then add it to filtered
                 $attributeOption = $variation->getOptionForAttribute($attributeID);
                 if ($attributeOption && $attributeOption->ID == $optionID) {
                     $variationOptions[$attributeID] = $optionID;
                 }
             }
             //}
             if ($variationOptions == $attributeOptions && $variation->isEnabled()) {
                 $filteredVariations->push($variation);
             }
         }
     }
     //Find options in filtered variations that match next attribute ID
     //All variations must have options for all attributes so this is belt and braces really
     if ($filteredVariations && $filteredVariations->exists()) {
         foreach ($filteredVariations as $variation) {
             $attributeOption = $variation->getOptionForAttribute($nextAttributeID);
             if ($attributeOption) {
                 $options->push($attributeOption);
             }
         }
     }
     if ($options && $options->exists()) {
         $map = $options->map();
         //This resets the array counter to 0 which ruins the attribute IDs
         //array_unshift($map, 'Please Select');
         $data['options'] = $map;
         $data['count'] = count($map);
         $data['nextAttributeID'] = $nextAttributeID;
     }
     return json_encode($data);
 }
 /**
  * Find an identical item in the order/cart, item is identical if the 
  * productID, version and the options for the item are the same. Used to increase 
  * quantity of items that already exist in the cart/Order.
  * 
  * @see Order::addItem()
  * @param DatObject $product
  * @param DataObjectSet $productOptions
  * @return DataObject
  */
 function findIdenticalItem($product, DataObjectSet $productOptions)
 {
     foreach ($this->Items() as $item) {
         if ($item->ObjectID == $product->ID && $item->ObjectVersion == $product->Version) {
             $productOptionsMap = array();
             $existingOptionsMap = array();
             if ($productOptions) {
                 $productOptionsMap = $productOptions->map('ID', 'Version');
             }
             if ($item) {
                 foreach ($item->ItemOptions() as $itemOption) {
                     $productOption = $itemOption->Object();
                     $existingOptionsMap[$productOption->ID] = $productOption->Version;
                 }
             }
             if ($productOptionsMap == $existingOptionsMap) {
                 return $item;
             }
         }
     }
 }