Source of file FieldAction.php
Size: 9,899 Bytes - Last Modified: 2021-12-24T05:18:45+00:00
/var/www/docs.ssmods.com/process/src/src/Model/FieldAction.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 | <?php namespace IQnection\FormBuilder\Model; use SilverStripe\ORM\DataObject; use IQnection\FormBuilder\Model\Field; use IQnection\FormBuilder\Fields\CheckboxField; use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; use Symbiote\GridFieldExtensions\GridFieldEditableColumns; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; use Symbiote\GridFieldExtensions\GridFieldTitleHeader; use SilverStripe\Forms; use IQnection\FormBuilder\Extensions\SelectField; use SilverStripe\ORM\FieldType; use IQnection\FormBuilder\FormBuilder; use IQnection\FormBuilder\Extensions\Duplicable; class FieldAction extends DataObject { private static $table_name = 'FormBuilderFieldAction'; private static $singular_name = 'Action'; private static $plural_name = 'Actions'; private static $hide_ancestor = FieldAction::class; private static $extensions = [ Duplicable::class ]; private static $db = [ ]; private static $has_one = [ 'Parent' => Field::class ]; private static $many_many = [ 'Children' => Field::class, 'ChildSelections' => SelectFieldOption::class ]; private static $many_many_extraFields = [ 'Children' => [ 'State' => 'Varchar(255)', ] ]; private static $summary_fields = [ 'ActionType' => 'Type', 'Explain' => 'Conditions' ]; private static $casting = [ 'Explain' => 'HTMLFragment' ]; private static $allowed_field_types; private static $condition_options = [ 'Has Value' => 'Has Value', 'Match' => 'Match', 'Is Empty' => 'Is Empty', 'Greater Than' => 'Greater Than', 'Less Than' => 'Less Than' ]; private static $form_builder_many_many_duplicates = [ 'Children', 'ChildSelections' ]; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName([ 'ParentID', 'Children', 'ChildSelections' ]); $fields->unshift( Forms\HeaderField::create('_actionType', 'Action: '.$this->singular_name(),1)); if (!$this->Exists()) { $fields->addFieldToTab('Root.Main', Forms\HeaderField::create('_saveWarning','You must save before adding conditions')); } else { $fields->addFieldToTab('Root.Main', Forms\HeaderField::create('_mainTitle','Conditions',1) ); $fields->addFieldToTab('Root.Main', Forms\HeaderField::create('_mainSubtitle','(All conditions must be met for action to apply)',2) ); $fields->addFieldToTab('Root.Main', $fields_gf = Forms\GridField\GridField::create( 'Children', 'Children', $this->Children(), Forms\GridField\GridFieldConfig::create(100) ->addComponent(new Forms\GridField\GridFieldButtonRow('before')) ->addComponent(new Forms\GridField\GridFieldToolbarHeader()) ->addComponent(new GridFieldTitleHeader()) ->addComponent($editableColumns = new GridFieldEditableColumns()) ->addComponent(new Forms\GridField\GridFieldDeleteAction(true)) ->addComponent($searchButton = new GridFieldAddExistingSearchButton()) )); $searchButton->setTitle('Add Condition'); $dataFields = $this->Parent()->FormBuilder()->DataFields(); if ($dataFields->Count()) { $dataFields = $dataFields->Exclude('ID',$this->Parent()->ID); } $searchButton->setSearchList($dataFields); $displayFields =[ 'Name' => [ 'title' => 'Field', 'field' => Forms\ReadonlyField::class ], 'State' => [ 'title' => 'State', 'callback' => function($fieldRecord, $col, $grid) { if ($fieldRecord instanceof FieldAction) { $states = $this->Config()->get('condition_options'); return Forms\OptionsetField::create('State','Field State') ->setSource($states); } if (!($fieldRecord instanceof \IQnection\FormBuilder\Model\Field)) { $fieldRecord = $fieldRecord->Parent(); } return $fieldRecord->ConditionOptionsField($this, '_ChildSelections'); } ] ]; $editableColumns->setDisplayFields($displayFields); } return $fields; } public function isFieldTypeAllowed($fieldType) { if (is_object($fieldType)) { $fieldType = get_class($fieldType); } $allowedTypes = $this->Config()->get('allowed_field_types'); if (is_array($allowedTypes)) { if (in_array($fieldType, $allowedTypes)) { return true; } // see if an extension was declared foreach($allowedTypes as $allowedType) { if ( (class_exists($allowedType)) && ($fieldType::has_extension($allowedType)) ) { return true; } } return false; } return true; } public function FormBuilder() { return $this->Parent()->FormBuilder(); } public function getTitle() { return $this->singular_name(); } public function testConditions($submittedValues = []) { $result = true; // all conditions must be met foreach($this->Children() as $child) { if ($child->isHidden($submittedValues)) { $submittedValues[$child->getFrontendFieldName()] = null; } if (!$this->testCondition($child->State, $child, $submittedValues)) { $result = false; break; } } $this->extend('updateTestConditions', $result, $submittedValues); return $result; } protected function testCondition($state, $testField, $values = []) { $fieldName = $testField->getFrontendFieldName(); $fieldValue = (array_key_exists($fieldName, $values)) ? $values[$fieldName] : null; switch(strtolower($state)) { case 'has value': if ( ($fieldValue !== '') && (!is_null($fieldValue)) ) { return true; } break; case 'is empty': if ( ($fieldValue === '') || (is_null($fieldValue)) ) { return true; } break; case 'match': $matchSelectionIds = $this->ChildSelections()->Column('ID'); if ( (!is_null($fieldValue)) && (count($matchSelectionIds)) ) { if ( (is_array($fieldValue)) && ($testField->hasExtension(SelectField::class)) ) { foreach($testField->Options()->byIds($matchSelectionIds)->Column('ID') as $testOption) { if (in_array($testOption, $fieldValue)) { return true; } } } else { if (in_array($fieldValue, $matchSelectionIds)) { return true; } } } break; } return false; } public function onBeforeWrite() { parent::onBeforeWrite(); $this->forceChange(); } public function onAfterWrite() { parent::onAfterWrite(); // there's some kind of bug that make the FieldAction pass instead of the actual record // create a field for each extra field so the info is passed properly and saved if ( (array_key_exists('Children',$_REQUEST)) && (isset($_REQUEST['Children']['GridFieldEditableColumns'])) ) { foreach($_REQUEST['Children']['GridFieldEditableColumns'] as $id => $extraFields) { if ($record = $this->Children()->byId($id)) { $this->Children()->add($record, $extraFields); } } } if (array_key_exists('_ChildSelections',$_REQUEST)) { $this->ChildSelections()->removeAll(); $this->ChildSelections()->addMany($_REQUEST['_ChildSelections']); } $this->FormBuilder()->clearAllCache(); } public function getBetterButtonsActions() { $actions = parent::getBetterButtonsActions(); $actions->removeByName(['action_doSaveAndAdd']); if (!$this->Exists()) { $actions->removeByName(['action_doSaveAndQuit']); $actions->fieldByName('action_save')->setTitle('Continue to Add Conditions'); } return $actions; } public function Explain() { $conditions = []; foreach($this->Children() as $conditionField) { $condition = $conditionField->Name.' '; switch($conditionField->State) { default: case 'Has Value,Match,': case 'Is Empty': $condition .= ' '.$conditionField->State; break; case 'Match': $condition .= ' Has Selected: '; $ids = $this->ChildSelections()->Column('ID'); if (count($ids)) { $condition .= implode(', ',$conditionField->Options()->Filter('ID', $ids)->Column('Value')); } else { $condition .= '(no selections)'; } break; } $conditions[] = $condition; } $this->extend('updateExplanation', $conditions); $text .= '<div>Action: '.$this->singular_name().'</div> - Conditions: <ul><li>'.implode('</li><li>',$conditions).'</li></ul>'; return FieldType\DBField::create_field(FieldType\DBHTMLVarchar::class, $text); } public function getActionData() { $js= [ 'id' => $this->ID, 'name' => 'Field: '.$this->Parent()->Name, 'action' => [ 'type' => $this->ActionType(), 'selector' => $this->Parent()->getjQuerySelector(), 'fieldType' => $this->Parent()->singular_name(), 'callback' => null, 'ownerId' => (int) $this->ParentID ], 'conditions' => [], 'conditionsHash' => null ]; foreach($this->Children() as $child) { $fieldJs = [ 'selector' => $child->getjQuerySelector(), 'state' => $child->State, 'stateCallback' => 'state'.preg_replace('/[^a-zA-Z]/','',$child->State), 'selections' => [], ]; if ( ($child->hasExtension(SelectField::class)) && ($this->ChildSelections()->Count()) ) { foreach($this->ChildSelections()->Filter('FieldID',$child->ID) as $childFieldSelection) { $fieldJs['selections'][] = [ 'selector' => $child->getOptionjQuerySelector($childFieldSelection), 'value' => $childFieldSelection->ID, 'label' => (string) $childFieldSelection->getOptionLabel() ]; } } $js['conditions'][] = $fieldJs; } $js['conditionsHash'] = md5(json_encode($js['conditions'])); return $js; } public function ActionType() { return $this->singular_name(); } public function onBeforeDelete() { parent::onBeforeDelete(); // remove relation links so the records aren't deleted $this->Children()->removeAll(); $this->ChildSelections()->removeAll(); } } |