function testPublishingMultipleOptions()
 {
     $id = $this->form->ID;
     $this->form->Fields()->removeAll();
     // test a editable option field
     $dropdown = new EditableDropdown();
     $dropdown->write();
     $checkbox = new EditableCheckboxGroupField();
     $checkbox->write();
     $option = new EditableOption();
     $option->write();
     $option2 = new EditableOption();
     $option2->write();
     $dropdown->Options()->add($option);
     $checkbox->Options()->add($option2);
     $this->form->Fields()->add($dropdown);
     $this->form->Fields()->add($checkbox);
     // upon adding it, it shouldn't  be on the live site
     $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = {$id}");
     $this->assertFalse($live);
     // and when published it should exist and the option
     $this->form->doPublish();
     $live = Versioned::get_one_by_stage("UserDefinedForm", "Live", "\"UserDefinedForm_Live\".\"ID\" = {$id}");
     $this->assertEquals($live->Fields()->Count(), 2);
     // check they have options attached
     foreach ($live->Fields() as $field) {
         $this->assertEquals($field->Options()->Count(), 1);
     }
 }
 public function getCMSFields()
 {
     $fields = parent::getCMSFields();
     $fields->removeByName(array('Options'));
     // get a list of data lists to select from
     $allLists = DataObject::get('DynamicList');
     $options = array('Please create a DynamicList!' => '(No DynamicLists available)');
     if ($allLists) {
         /* @var $allLists DataObjectSet */
         $options = $allLists->map('Title', 'Title');
     }
     $fields->addFieldToTab('Root.Main', DropdownField::create('ListTitle', _t('EditableDataListField.DYNAMICLIST_TITLE', 'List Title'), $options));
     return $fields;
 }
 public function getCMSFields()
 {
     $fields = parent::getCMSFields();
     // select another form field that has the titles of the lists to use for this list when displayed
     // The assumption being made here is that each entry in the source list has a corresponding dynamic list
     // defined for it, which we use later on.
     $options = array();
     if ($this->Parent()) {
         $sourceList = $this->Parent()->Fields();
         if ($sourceList) {
             $options = $sourceList->map('Name', 'Title');
         }
     }
     $fields->addFieldToTab('Root.Main', DropdownField::create('SourceList', _t('EditableDependentDynamicListField.SOURCE_LIST_TITLE', 'Source List'), $options));
     return $fields;
 }
 function getFieldConfiguration()
 {
     $fields = parent::getFieldConfiguration();
     // eventually replace hard-coded "Fields"?
     $baseName = "Fields[{$this->ID}]";
     $listName = $this->getSetting('ListTitle') ? $this->getSetting('ListTitle') : '';
     // get a list of data lists to select from
     $allLists = DataObject::get('DataList');
     $options = array('Please create a DataList!' => '(No DataLists available)');
     if ($allLists) {
         /* @var $allLists DataObjectSet */
         $options = $allLists->map('Title', 'Title');
     }
     $extraFields = new FieldSet(new DropDownField($baseName . "[CustomSettings][ListTitle]", _t('EditableDataListField.DATALIST_TITLE', 'List Title'), $options, $listName));
     $fields->merge($extraFields);
     return $fields;
 }
 function getFieldConfiguration()
 {
     $fields = parent::getFieldConfiguration();
     // eventually replace hard-coded "Fields"?
     $baseName = "Fields[{$this->ID}]";
     $listName = $this->getSetting('SourceList') ? $this->getSetting('SourceList') : '';
     // select another form field that has the titles of the lists to use for this list when displayed
     // The assumption being made here is that each entry in the source list has a corresponding data list
     // defined for it, which we use later on.
     $options = array();
     if ($this->Parent()) {
         $sourceList = $this->Parent()->Fields();
         if ($sourceList) {
             $options = $sourceList->map('Name', 'Title');
         }
     }
     $extraFields = new FieldSet(new DropDownField($baseName . "[CustomSettings][SourceList]", _t('EditableDependentDataListField.SOURCE_LIST_TITLE', 'Source List'), $options, $listName));
     $fields->merge($extraFields);
     return $fields;
 }
 /**
  * Import Gravity Form as User Defined Form
  */
 public function importGravityForms()
 {
     if (!class_exists('UserFormFieldEditorExtension')) {
         throw new WordpressImportException(__FUNCTION__ . ' requires User Defined Forms 3.0+');
     }
     //$list = ElementContent::get()->filter(array('HTML:PartialMatch' => '[gravityform'));
     // Get existing gravity form items
     $existingWpRecords = array();
     $list = $this->applyWordpressFilter(UserDefinedForm::get(), array('posts', 'rg_form'));
     foreach ($list as $record) {
         if (isset($record->WordpressData['gravityform'])) {
             // Handle case where a Page was the only one using a Gravity Form, so
             // the gravity form data was just attached to that existing data.
             $gfData = $record->WordpressData['gravityform'];
             if (!isset($gfData['id'])) {
                 throw new Exception('Missing "id" in "gravityform" data on #' . $record->ID . ' in its "WordpressData" column.');
             }
             $id = $gfData['id'];
             $existingWpRecords[$id] = $record;
         } else {
             // Handle case where Gravity form was used in multiple locations, wherein
             // a new page was created.
             $existingWpRecords[$record->WordpressID] = $record;
         }
     }
     $gfDB = new WordpressGravityForms($this->_db);
     foreach ($gfDB->getForms() as $gfData) {
         $gfID = (int) $gfData['id'];
         if ($existingWpRecords && isset($existingWpRecords[$gfID])) {
             // Skip existing imported gravity forms
             $this->log($record, 'nochange');
             continue;
         }
         $gfMeta = $gfDB->attachAndGetFormMeta($gfData);
         if (!isset($gfMeta['display_meta']['fields'])) {
             continue;
         }
         // Create array of EditableFormField's
         $fields = array();
         $fieldsData = $gfMeta['display_meta']['fields'];
         foreach ($fieldsData as $i => $fieldData) {
             if (!isset($fieldData['type'])) {
                 throw new WordpressImportException('Gravity Form field is missing "type"');
             }
             $type = $fieldData['type'];
             $label = isset($fieldData['label']) ? $fieldData['label'] : null;
             $field = null;
             switch ($type) {
                 case 'textarea':
                     $field = EditableTextField::create();
                     $field->Rows = 4;
                     break;
                 case 'name':
                 case 'text':
                 case 'phone':
                     $field = EditableTextField::create();
                     break;
                 case 'email':
                     $field = EditableEmailField::create();
                     break;
                 case 'number':
                     $field = EditableNumericField::create();
                     $field->MinValue = isset($fieldData['rangeMin']) ? $fieldData['rangeMin'] : 0;
                     $field->MaxValue = isset($fieldData['rangeMax']) ? $fieldData['rangeMax'] : 0;
                     break;
                 case 'radio':
                     $field = EditableRadioField::create();
                     break;
                 case 'checkbox':
                     $choices = isset($fieldData['choices']) ? $fieldData['choices'] : false;
                     if (!$choices) {
                         throw new WordpressImportException('Cannot find "choices" on ' . $type);
                     }
                     if (count($choices) == 1) {
                         $field = EditableCheckbox::create();
                         foreach ($choices as $choiceData) {
                             $label = $choiceData['text'];
                             break;
                         }
                     } else {
                         $field = EditableCheckboxGroupField::create();
                     }
                     break;
                 case 'select':
                     $field = EditableDropdown::create();
                     break;
                 case 'captcha':
                     // No Captcha field comes with User Defined Forms.
                     $field = false;
                     break;
                 case 'html':
                     // Ignore literal field
                     $field = EditableLiteralField::create();
                     $field->Content = isset($fieldData['content']) ? $fieldData['content'] : '';
                     break;
                 default:
                     //Debug::dump($fieldData);
                     throw new WordpressImportException('Gravity Form field is unhandled type "' . $type . '".');
                     break;
             }
             if ($field === null) {
                 throw new WordpressImportException('Gravity Form field is mishandled type "' . $type . '" $field isn\'t set.');
             }
             if ($field) {
                 /*$descriptionPlacement = isset($fieldData['descriptionPlacement']) ? $fieldData['descriptionPlacement'] : 'above';
                 		if ($descriptionPlacement === 'above') {
                 			$field->Title = $label;
                 		} else if ($descriptionPlacement === 'below') {
                 			$field->RightTitle = $label;
                 		} else {
                 			throw new WordpressImportException('Invalid "descriptionPlacement" value "'.$descriptionPlacement.'"');
                 		}*/
                 $field->Title = $label;
                 $field->Placeholder = isset($fieldData['placeholder']) ? $fieldData['placeholder'] : null;
                 $field->CustomErrorMessage = isset($fieldData['errorMessage']) ? $fieldData['errorMessage'] : null;
                 $field->Required = isset($fieldData['isRequired']) ? $fieldData['isRequired'] : false;
                 $field->Sort = $i + 1;
                 $choices = isset($fieldData['choices']) ? $fieldData['choices'] : false;
                 if ($choices && $field->hasMethod('Options')) {
                     foreach ($choices as $choiceData) {
                         $choice = EditableOption::create();
                         $choice->Title = $choiceData['value'];
                         $field->Options()->add($choice);
                     }
                 }
                 $fields[] = $field;
             }
         }
         // Get existing page record if only a single page is using a gravity form.
         $oneGravityFormPageRecord = null;
         $pageContent = null;
         if (isset($this->_classes_using_elemental['SiteTree']) || isset($this->_classes_using_elemental['Page'])) {
             $list = ElementContent::get()->filter(array('HTML:PartialMatch' => '[gravityform id="' . $gfID . '"'));
             $list = $list->toArray();
             if (count($list) == 1) {
                 $elementContent = $list[0];
                 $oneGravityFormPageRecord = $elementContent->Parent();
                 $pageContent = $elementContent->HTML;
             }
         } else {
             $list = SiteTree::get()->filter(array('Content:PartialMatch' => '[gravityform id="' . $gfID . '"'));
             $list = $list->toArray();
             if (count($list) == 1) {
                 $oneGravityFormPageRecord = $list[0];
                 $pageContent = $oneGravityFormPageRecord->Content;
             }
         }
         if (substr_count($pageContent, '[gravityform') > 1) {
             // If two gravity forms exist on the single page, don't make it write the UserDefinedForm
             // to an existing page.
             $oneGravityFormPageRecord = null;
         }
         /*if ($oneGravityFormPageRecord && $existingWpRecords && isset($existingWpRecords[$oneGravityFormPageRecord->WordpressID])) {
         			// Skip existing imported gravity forms
         			$this->log($record, 'nochange');
         			continue;
         		}*/
         // Create UDF
         if ($oneGravityFormPageRecord) {
             // If only one page is using a Gravity Form, transform it into a UserDefinedForm page.
             $record = $oneGravityFormPageRecord->newClassInstance('UserDefinedForm');
             $wordpressData = $record->WordpressData;
             $wordpressData['gravityform'] = $gfData;
             $record->WordpressData = $wordpressData;
         } else {
             // If multiple pages are using the same Gravity Form, just create the UserDefinedForm page.
             $record = UserDefinedForm::create();
             if (!isset($gfData['title'])) {
                 throw new WordpressImportException('Gravity Form missing "title" field.');
             }
             $record->Title = $gfData['title'];
             if (!isset($gfData['date_created'])) {
                 throw new WordpressImportException('Gravity Form missing "date_created" field.');
             }
             $record->Created = $gfData['date_created'];
             $record->WordpressData = $gfData;
         }
         foreach ($fields as $field) {
             $record->Fields()->add($field);
         }
         //Debug::dump($record->toMap());
         try {
             $isPublished = isset($gfData['is_active']) && $gfData['is_active'] || $record->isPublished();
             $this->writeAndPublishRecord($record, $isPublished);
         } catch (Exception $e) {
             $this->log($record, 'error', $e);
             throw $e;
         }
     }
 }