/** * Creates a workflow item - a definition, action, transition or any subclasses * of these. * * @param array $data * @param Form $form * @return string */ public function doCreateWorkflowItem($data, $form) { // assume the form name is in the form CreateTypeForm $data = $form->getData(); $type = 'Workflow' . substr($form->Name(), 6, -4); $allowSelf = $type != 'WorkflowAction'; // determine the class to create - if it is manually specified then use that, // falling back to creating an object of the root type if allowed. if (isset($data['Class']) && class_exists($data['Class'])) { $class = $data['Class']; $valid = is_subclass_of($class, $type) || $allowSelf && $class == $type; if (!$valid) { return new SS_HTTPResponse(null, 400, _t('AdvancedWorkflowAdmin.INVALIDITEM', 'An invalid workflow item was specified.')); } } else { $class = $type; if (!$allowSelf) { return new SS_HTTPResponse(null, 400, _t('AdvancedWorkflowAdmin.MUSTSPECIFYITEM', 'You must specify a workflow item to create.')); } } // check that workflow actions and transitions have valid parent id values. if ($type != 'WorkflowDefinition') { $parentId = $data['ParentID']; $parentClass = $type == 'WorkflowAction' ? 'WorkflowDefinition' : 'WorkflowAction'; if (!is_numeric($parentId) || !DataObject::get_by_id($parentClass, $parentId)) { return new SS_HTTPResponse(null, 400, _t('AdvancedWorkflowAdmin.INVALIDPARENT', 'An invalid parent was specified.')); } } // if an add form can be returned without writing a new rcord to the database, // then just do that if (array_key_exists($class, $this->getManagedModels())) { $form = $this->{$type}()->AddForm(); $title = singleton($type)->singular_name(); if ($type == 'WorkflowTransition') { $form->dataFieldByName('ActionID')->setValue($parentId); } } else { $record = new $class(); $record->Title = sprintf(_t('AdvancedWorkflowAdmin.NEWITEM', 'New %s'), $record->singular_name()); if ($type == 'WorkflowAction') { $record->WorkflowDefID = $parentId; } elseif ($type == 'WorkflowTransition') { $record->ActionID = $parentId; } $record->write(); $control = $this->getRecordControllerClass('WorkflowDefinition'); $control = new $control($this->{$type}(), null, $record->ID); $form = $control->EditForm(); $title = $record->singular_name(); } return new SS_HTTPResponse($this->isAjax() ? $form->forAjaxTemplate() : $form->forTemplate(), 200, sprintf(_t('AdvancedWorkflowAdmin.CREATEITEM', 'Fill out this form to create a "%s".'), $title)); }