/**
  * @return FieldList
  */
 public function getCMSFields()
 {
     Requirements::javascript(USERFORMS_DIR . '/javascript/Recipient.js');
     // Determine optional field values
     $form = $this->getFormParent();
     // predefined choices are also candidates
     $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID);
     // if they have email fields then we could send from it
     $validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID);
     // For the subject, only one-line entry boxes make sense
     $validSubjectFields = ArrayList::create(EditableTextField::get()->filter('ParentID', $form->ID)->exclude('Rows:GreaterThan', 1)->toArray());
     $validSubjectFields->merge($multiOptionFields);
     // To address cannot be unbound, so restrict to pre-defined lists
     $validEmailToFields = $multiOptionFields;
     // Build fieldlist
     $fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm'));
     // Configuration fields
     $fields->addFieldsToTab('Root.EmailDetails', array(FieldGroup::create(TextField::create('EmailSubject', _t('UserDefinedForm.TYPESUBJECT', 'Type subject'))->setAttribute('style', 'min-width: 400px;'), DropdownField::create('SendEmailSubjectFieldID', _t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'), $validSubjectFields->map('ID', 'Title'))->setEmptyString(''))->setTitle(_t('UserDefinedForm.EMAILSUBJECT', 'Email subject')), FieldGroup::create(TextField::create('EmailAddress', _t('UserDefinedForm.TYPETO', 'Type to address'))->setAttribute('style', 'min-width: 400px;'), DropdownField::create('SendEmailToFieldID', _t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), $validEmailToFields->map('ID', 'Title'))->setEmptyString(' '))->setTitle(_t('UserDefinedForm.SENDEMAILTO', 'Send email to'))->setDescription(_t('UserDefinedForm.SENDEMAILTO_DESCRIPTION', 'You may enter multiple email addresses as a comma separated list.')), TextField::create('EmailFrom', _t('UserDefinedForm.FROMADDRESS', 'Send email from'))->setDescription(_t('UserDefinedForm.EmailFromContent', "The from address allows you to set who the email comes from. On most servers this " . "will need to be set to an email address on the same domain name as your site. " . "For example on yoursite.com the from address may need to be something@yoursite.com. " . "You can however, set any email address you wish as the reply to address.")), FieldGroup::create(TextField::create('EmailReplyTo', _t('UserDefinedForm.TYPEREPLY', 'Type reply address'))->setAttribute('style', 'min-width: 400px;'), DropdownField::create('SendEmailFromFieldID', _t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), $validEmailFromFields->map('ID', 'Title'))->setEmptyString(' '))->setTitle(_t('UserDefinedForm.REPLYADDRESS', 'Email for reply to'))->setDescription(_t('UserDefinedForm.REPLYADDRESS_DESCRIPTION', 'The email address which the recipient is able to \'reply\' to.'))));
     // Only show the preview link if the recipient has been saved.
     if (!empty($this->EmailTemplate)) {
         $preview = sprintf('<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>', "admin/pages/edit/EditForm/field/EmailRecipients/item/{$this->ID}/preview", _t('UserDefinedForm.PREVIEW_EMAIL', 'Preview email'), _t('UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', 'Note: Unsaved changes will not appear in the preview.'));
     } else {
         $preview = sprintf('<em>%s</em>', _t('UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', 'You can preview this email once you have saved the Recipient.'));
     }
     // Email templates
     $fields->addFieldsToTab('Root.EmailContent', array(CheckboxField::create('HideFormData', _t('UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')), CheckboxField::create('SendPlain', _t('UserDefinedForm.SENDPLAIN', 'Send email as plain text? (HTML will be stripped)')), DropdownField::create('EmailTemplate', _t('UserDefinedForm.EMAILTEMPLATE', 'Email template'), $this->getEmailTemplateDropdownValues())->addExtraClass('toggle-html-only'), HTMLEditorField::create('EmailBodyHtml', _t('UserDefinedForm.EMAILBODYHTML', 'Body'))->addExtraClass('toggle-html-only'), TextareaField::create('EmailBody', _t('UserDefinedForm.EMAILBODY', 'Body'))->addExtraClass('toggle-plain-only'), LiteralField::create('EmailPreview', '<div id="EmailPreview" class="field toggle-html-only">' . $preview . '</div>')));
     // Custom rules for sending this field
     $grid = new GridField("CustomRules", _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), $this->CustomRules(), $this->getRulesConfig());
     $grid->setDescription(_t('UserDefinedForm.RulesDescription', 'Emails will only be sent to the recipient if the custom rules are met. If no rules are defined, this receipient will receive notifications for every submission.'));
     $fields->addFieldsToTab('Root.CustomRules', array(new DropdownField('CustomRulesCondition', _t('UserDefinedForm.SENDIF', 'Send condition'), array('Or' => 'Any conditions are true', 'And' => 'All conditions are true')), $grid));
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }
 function getFormField()
 {
     $field = parent::getFormField();
     return singleton('DefaultEditableFieldHelper')->updateFormField($this, $field);
 }
 /**
  * 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;
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * @return FieldList
  */
 public function getCMSFields()
 {
     $fields = new FieldList(new TextField('EmailSubject', _t('UserDefinedForm.EMAILSUBJECT', 'Email subject')), new LiteralField('EmailFromContent', '<p>' . _t('UserDefinedForm.EmailFromContent', "The from address allows you to set who the email comes from. On most servers this " . "will need to be set to an email address on the same domain name as your site. " . "For example on yoursite.com the from address may need to be something@yoursite.com. " . "You can however, set any email address you wish as the reply to address.") . "</p>"), new TextField('EmailFrom', _t('UserDefinedForm.FROMADDRESS', 'Send email from')), new TextField('EmailReplyTo', _t('UserDefinedForm.REPLYADDRESS', 'Email for reply to')), new TextField('EmailAddress', _t('UserDefinedForm.SENDEMAILTO', 'Send email to')), new CheckboxField('HideFormData', _t('UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')), new CheckboxField('SendPlain', _t('UserDefinedForm.SENDPLAIN', 'Send email as plain text? (HTML will be stripped)')), new TextareaField('EmailBody', _t('UserDefinedForm.EMAILBODY', 'Body')));
     $formID = $this->FormID != 0 ? $this->FormID : Session::get('CMSMain.currentPage');
     $dropdowns = array();
     // if they have email fields then we could send from it
     $validEmailFields = EditableEmailField::get()->filter('ParentID', (int) $formID);
     // for the subject, only one-line entry boxes make sense
     $validSubjectFields = EditableTextField::get()->filter('ParentID', (int) $formID)->filterByCallback(function ($item, $list) {
         return (int) $item->getSetting('Rows') === 1;
     });
     // predefined choices are also candidates
     $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', (int) $formID);
     $fields->insertAfter($dropdowns[] = new DropdownField('SendEmailFromFieldID', _t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), $validEmailFields->map('ID', 'Title')), 'EmailReplyTo');
     $validEmailFields = new ArrayList($validEmailFields->toArray());
     $validEmailFields->merge($multiOptionFields);
     $validSubjectFields->merge($multiOptionFields);
     $fields->insertAfter($dropdowns[] = new DropdownField('SendEmailToFieldID', _t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), $validEmailFields->map('ID', 'Title')), 'EmailAddress');
     $fields->insertAfter($dropdowns[] = new DropdownField('SendEmailSubjectFieldID', _t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'), $validSubjectFields->map('ID', 'Title')), 'EmailSubject');
     foreach ($dropdowns as $dropdown) {
         $dropdown->setHasEmptyDefault(true);
         $dropdown->setEmptyString(" ");
     }
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }