public function onAfterWrite() { $conf = $this->FlexiFormConf(); // ensure valid config ////////////////////// if (!$conf->exists()) { if ($name = $this->getFlexiFormDefaultHandlerName()) { if ($handler = FlexiFormHandler::get()->filter('HandlerName', $name)->first()) { $conf->HandlerID = $handler->ID; } } $conf->FlexiFormID = $this->owner->ID; $conf->FlexiFormClass = $this->owner->class; $conf->write(); $this->owner->FlexiFormConfigID = $conf->ID; $this->owner->write(); } // initialize fields on new forms ///////////////////////////////// if (!$this->FlexiFormConf('InitializedFields')) { $definitions = $this->getFlexiFormInitialFields(); if (!empty($definitions)) { $fields = $this->owner->FlexiFormFields(); foreach ($definitions as $definition) { if (!is_array($definition) || !isset($definition['Name']) || !isset($definition['Type'])) { throw new ValidationException('Initial Field Definitions must be an associative array, with at least Name and Type provided.'); } // lookup field name, prioritizing Readonly fields if (!($field = FlexiFormField::get()->sort('Readonly', 'DESC')->filter(array('FieldName' => $definition['Name'], 'ClassName' => $definition['Type']))->first())) { $field = FlexiFormUtil::CreateFlexiField($definition['Type'], $definition); } $extraFields = array(); foreach (array_intersect_key($definition, $fields->getExtraFields()) as $property => $value) { $extraFields[$property] = $value; } $fields->add($field, $extraFields); } $conf->InitializedFields = true; $conf->write(); } } // seed Form Identifier /////////////////////// if (!$this->FlexiFormID()) { $conf = $this->FlexiFormConf(); // @TODO perhaps base on title of extended object?? $conf->FormIdentifier = "{$this->owner->class}_{$this->owner->ID}"; $conf->write(); } return parent::onAfterWrite(); }
public function onConfigUpdate(FlexiFormConfig $config, DataObject $flexi) { $fields = $flexi->FlexiFormFields(); $requested_groups = (array) Controller::curr()->getRequest()->requestVar('FlexiFormMailChimpGroups'); $current_groups = array(); // remove non requested groups from field list foreach ($fields as $field) { if ($field->is_a('FlexiMailChimpInterestGroupField')) { if (in_array($field->InterestGroupID, $requested_groups)) { $current_groups[] = $field->InterestGroupID; } else { $fields->remove($field); $field->delete(); } } } // calculate groups that need to be added to field list $new_groups = array_diff($requested_groups, $current_groups); if (!empty($new_groups)) { $client = new FlexiFormMailChimpClient($flexi->FlexiFormSetting('MailChimpApiKey')->getValue()); $list_id = $flexi->FlexiFormSetting('MailChimpListID')->getValue(); $groups = $this->getInterestGroups($list_id, $client); foreach ($new_groups as $group_id) { if ($group = $groups->find('id', $group_id)) { $options = array(); foreach ($group['groups'] as $option) { // mailchimp subscribe API uses names vs. IDs... //$options[$option['id']] = $option['name']; $options[$option['name']] = $option['name']; } $field = FlexiFormUtil::CreateFlexiField('FlexiMailChimpInterestGroupField', array('Name' => $group['name'], 'InterestGroupID' => $group['id'], 'InterestGroupFormField' => $group['form_field'], 'Readonly' => true, 'Options' => $options)); $fields->add($field, array('Prompt' => $group['name'])); } } } return parent::onConfigUpdate($config, $flexi); }
public static function AutoCreateFlexiField($className, $definition) { $readonly = isset($definition['Readonly']) && $definition['Readonly']; $filter = array('FieldName' => $definition['Name'], 'Readonly' => $readonly); // allow same names on non readonly fields if they're different classes if (!$readonly) { $filter['ClassName'] = $className; } // only create field if it's name doesn't yet exist if (!FlexiFormField::get()->filter($filter)->first()) { if ($obj = FlexiFormUtil::CreateFlexiField($className, $definition)) { $prefix = $obj->Readonly ? 'Readonly' : 'Normal'; DB::alteration_message("flexiform - Created {$prefix} {$className} named `{$obj->FieldName}`.", "created"); } } }