public function save($entity, DatabaseTransaction $transaction = NULL) { $workflow = $entity->getWorkflow(); // To avoid double posting, check if this transition already exist. if (empty($entity->tid)) { if ($workflow) { $config_transitions = $workflow->getTransitionsBySidTargetSid($entity->sid, $entity->target_sid); $config_transition = reset($config_transitions); if ($config_transition) { $entity->tid = $config_transition->tid; } } } // Create the machine_name. This can be used to rebuild/revert the Feature in a target system. if (empty($entity->name)) { $entity->name = $entity->sid . '_' . $entity->target_sid; } $return = parent::save($entity, $transaction); if ($return) { // Save in current workflow for the remainder of this page request. // Keep in sync with Workflow::getTransitions() ! $workflow = $entity->getWorkflow(); if ($workflow) { $workflow->transitions[$entity->tid] = $entity; // $workflow->sortTransitions(); } } // Reset the cache for the affected workflow, to force reload upon next page_load. workflow_reset_cache($entity->wid); return $return; }
public function delete($ids, DatabaseTransaction $transaction = NULL) { // @todo: replace with parent. foreach ($ids as $id) { if ($state = workflow_state_load($id)) { $wid = $state->wid; db_delete('workflow_states')->condition('sid', $state->sid)->execute(); // Reset the cache for the affected workflow. workflow_reset_cache($wid); } } }
/** * Given information, update or insert a new workflow. * * This also handles importing, rebuilding, reverting from Features, * as defined in workflow.features.inc. * todo: reverting does not refresh States and transitions, since no * machine_name was present. As of 7.x-2.3, the machine_name exists in * Workflow and WorkflowConfigTransition, so rebuilding is possible. * * When changing this function, test with the following situations: * - maintain Workflow in Admin UI; * - clone Workflow in Admin UI; * - create/revert/rebuild Workflow with Features; @see workflow.features.inc * - save Workflow programmatically; */ public function save($create_creation_state = TRUE) { // Are we saving a new Workflow? $is_new = !empty($this->is_new); // Are we rebuilding, reverting a new Workflow? @see workflow.features.inc $is_rebuild = !empty($this->is_rebuild); $is_reverted = !empty($this->is_reverted); // If rebuild by Features, make some conversions. if (!$is_rebuild && !$is_reverted) { // Avoid troubles with features clone/revert/.. unset($this->module); } else { $role_map = isset($this->system_roles) ? $this->system_roles : array(); if ($role_map) { // Remap roles. They can come from another system with shifted role IDs. // See also https://drupal.org/node/1702626 . $this->tab_roles = _workflow_rebuild_roles($this->tab_roles, $role_map); foreach ($this->transitions as &$transition) { $transition['roles'] = _workflow_rebuild_roles($transition['roles'], $role_map); } } // Insert the type_map when building from Features. if ($this->typeMap) { foreach ($this->typeMap as $node_type) { workflow_insert_workflow_type_map($node_type, $this->wid); } } } // After update.php or import feature, label might be empty. @todo: remove in D8. if (empty($this->label)) { $this->label = $this->name; } $return = parent::save(); // If a workflow is cloned in Admin UI, it contains data from original workflow. // Redetermine the keys. if ($is_new && $this->states) { foreach ($this->states as $state) { // Can be array when cloning or with features. $state = is_array($state) ? new WorkflowState($state) : $state; // Set up a conversion table, while saving the states. $old_sid = $state->sid; $state->wid = $this->wid; // @todo: setting sid to FALSE should be done by entity_ui_clone_entity(). $state->sid = FALSE; $state->save(); $sid_conversion[$old_sid] = $state->sid; } // Reset state cache. $this->getStates(TRUE, TRUE); foreach ($this->transitions as &$transition) { // Can be array when cloning or with features. $transition = is_array($transition) ? new WorkflowConfigTransition($transition, 'WorkflowConfigTransition') : $transition; // Convert the old sids of each transitions before saving. // @todo: is this be done in 'clone $transition'? // (That requires a list of transitions without tid and a wid-less conversion table.) if (isset($sid_conversion[$transition->sid])) { $transition->tid = FALSE; $transition->sid = $sid_conversion[$transition->sid]; $transition->target_sid = $sid_conversion[$transition->target_sid]; $transition->save(); } } } // Make sure a Creation state exists. if ($is_new) { $state = $this->getCreationState(); } // Make sure the default roles are permitted in transitions for better UX. if ($is_new && count(workflow_load_multiple()) == 1) { foreach (user_roles() as $rid => $name) { $perms = array('participate in workflow' => 1); user_role_change_permissions($rid, $perms); // <=== Enable Roles } } workflow_reset_cache($this->wid); return $return; }