/** * 流程流转 * * @param project_name 项目名称 * @param flow lib/flow类 * @param from 当前步骤名称 * @param to 跳转到的步骤名称 * @param action_status 执行动作对应的状态 * */ public static function turnTo($flow, $from, $to, $action_status) { $user = User::info(); $steps = Config::get('flow.' . $flow->tpl_name . '.steps'); $to_config = $steps[$to]; $runing_config = $steps[$flow->running_step]; $flow_mod = Model\Flow::find($flow->flow_id); if (empty($flow_mod)) { throw new Exception("流程id不存在"); } $flow_info = $flow_mod->getAttributes(); // 如果当前流程执行方式为accept(先接受后执行) $history_user = Step::getHistoryUser($flow->tpl_name, $flow->flow_id, $to_config['roles'][0]); switch ($to_config['run_type']) { case 'accept-only': // 始终先接受后执行 $to_status = Status::ARRIVED; $to_accepted_users = ''; $to_accepted_roles = ''; break; case 'history': // 从历史记录中找执行人 $to_status = Status::ACCEPT; $to_accepted_users = $history_user['created_user']; $to_accepted_roles = $to_config['roles'][0]; // 单角色可以这样使用,多角色后续再行考虑,当前需求无此要求 break; case 'accept': default: // 如果有历史则使用历史,如果没有历史则先接受再执行 if (!empty($history_user)) { $to_status = Status::ACCEPT; $to_accepted_users = $history_user['created_user']; $to_accepted_roles = $to_config['roles'][0]; // 单角色可以这样使用,多角色后续再行考虑,当前需求无此要求 } else { $to_status = Status::ARRIVED; $to_accepted_users = ''; $to_accepted_roles = ''; } break; } Log::info("accepted_users:" . $to_accepted_users . " accepted_roles:" . $to_accepted_roles); // 如果之前已经有执行人 if (!empty($flow_info['accepted_users'])) { $from_accepted_users = explode(",", $flow_info['accepted_users']); // 去除当前执行人 $from_accepted_users_reverse = array_flip($from_accepted_users); unset($from_accepted_users_reverse[$user->name]); $from_accepted_users = array_flip($from_accepted_users_reverse); // 添加跳转到的步骤执行人 if (!empty($to_accepted_users)) { $from_accepted_users[] = $to_accepted_users; } $to_accepted_users = implode(',', $from_accepted_users); } // 如果之前已经有执行人 if (!empty($flow_info['accepted_roles'])) { $from_accepted_roles = explode(",", $flow_info['accepted_roles']); // 去除当前执行人 $from_accepted_roles_reverse = array_flip($from_accepted_roles); unset($from_accepted_roles_reverse[$flow->running_role]); $from_accepted_roles = array_flip($from_accepted_roles_reverse); // 添加跳转到的步骤执行人 if (!empty($to_accepted_roles)) { $from_accepted_roles[] = $to_accepted_roles; } $to_accepted_roles = implode(',', $from_accepted_roles); } $content = Arr::get($flow->request, 'content'); $real_content = Arr::get($flow->request, 'real_content'); // 更新流程主表 $flow_mod->update(array('current_step' => $to, 'current_status' => $to_status, 'accepted_users' => $to_accepted_users, 'accepted_roles' => $to_accepted_roles)); $data = Arr::get($flow->request, 'data'); foreach ($data as &$item) { $item = urlencode($item); } $data_json = empty($data) ? '' : json_encode($data); $data_json = urldecode($data_json); // 新增步骤执行记录 $step = Model\Step::create(array('project_name' => $flow->tpl_name, 'flow_id' => $flow->flow_id, 'title' => $runing_config['title'], 'real_title' => $runing_config['title'], 'content' => $content, 'real_content' => $real_content, 'step' => $flow->running_step, 'status' => $action_status, 'data' => $data_json, 'created_user' => $user->name, 'created_role' => $flow->running_role)); // 添加hook self::addHooks("after_step", $flow, $step, $from, $to, $action_status); }
/** * 流转 */ private function turnTo($dest_action, $dest_status) { $flow_id = $this->flow->flow_id; $flow = Model\Flow::find($flow_id); if (empty($flow)) { throw new Exception("流程id不存在"); } $flow_info = $flow->getAttributes(); $from = $flow_info['current_step']; // 目标步骤获取优先级 页面手动设置>系统配置 if (isset($this->flow->request['dest'])) { $to = $this->flow->request['dest']['dest_step']; $dest_status = $this->flow->request['dest']['dest_status']; } else { $steps = Config::get('flow.' . $this->flow->tpl_name . '.steps'); $current_config = $steps[$from]; $to = $current_config[$dest_action]; } // 校验是否可以流转 Util\Condition::checkTransitionCondition($this->flow); // 流转 Util\Step::turnTo($this->flow, $from, $to, $dest_status); }