Example #1
0
 /**
  *
  * @param Model_Tudu_Tudu $tudu
  */
 public function filter(Model_Tudu_Tudu &$tudu)
 {
     $apply = $tudu->getExtension('Model_App_Attend_Tudu_Apply');
     if (!$apply) {
         return;
     }
     /* @var $daoApply Dao_App_Attend_Apply */
     $daoApply = Tudu_Dao_Manager::getDao('Dao_App_Attend_Apply', Tudu_Dao_Manager::DB_APP);
     if ($tudu->tuduId) {
         $this->_fromApply = $daoApply->getApply(array('tuduid' => $tudu->tuduId));
         if ($this->_fromApply) {
             $apply->applyId = $this->_fromApply->applyId;
             if ($tudu->operation == 'review') {
                 $apply->setAttributes($this->_fromApply->toArray());
             }
         }
     }
     // 创建审批流程
     if ($tudu->operation == 'send' || $tudu->operation == 'save') {
         $error = null;
         $issend = $tudu->operation == 'send';
         do {
             if (!$apply->categoryId) {
                 $error = 'Invalid Category id for attend apply';
                 break;
             }
             if ($issend && (!$apply->startTime && $apply->categoryId != '^checkin')) {
                 $error = 'Invalid Starttime for attend apply';
                 break;
             }
             if ($issend && (!$apply->endTime && $apply->categoryId != '^checkin')) {
                 $error = 'Invalid Endtime for attend apply';
                 break;
             }
         } while (false);
         // 参数错误
         if (null != $error) {
             require_once 'Model/App/Attend/Exception.php';
             $code = false !== strpos($error, 'Category') ? Model_App_Attend_Exception::APPLY_MISSING_CATEGORYID : Model_App_Attend_Exception::APPLY_INVALID_TIME;
             throw new Model_App_Attend_Exception($error, $code);
         }
         /* @var $daoCategory Dao_App_Attend_Category */
         $daoCategory = Tudu_Dao_Manager::getDao('Dao_App_Attend_Category', Tudu_Dao_Manager::DB_APP);
         $category = $daoCategory->getCategory(array('categoryid' => $apply->categoryId, 'orgid' => $apply->orgId));
         if (null === $category) {
             require_once 'Model/App/Attend/Exception.php';
             throw new Model_App_Attend_Exception('Category id: [' . $apply->categoryId . '] is not exists', Model_App_Attend_Exception::CATEGORY_NOT_EXISTS);
         }
         $tudu->subject = $category->categoryName;
         // 创建图度工作流
         if ($tudu->operation == 'send' && ($apply->status == 0 || $apply->status == 3) && !$this->_fromApply) {
             require_once 'Model/Tudu/Extension/Flow.php';
             $flow = new Model_Tudu_Extension_Flow(array('orgid' => $tudu->orgId));
             $steps = $category->flowSteps;
             $stepIds = array();
             foreach ($steps as $item) {
                 $stepId = isset($item['stepid']) ? $item['stepid'] : Dao_Td_Tudu_Flow::getStepId(null);
                 $stepIds[] = $stepId;
                 $flow->addStep(array('stepid' => $stepId, 'prev' => !empty($item['prev']) ? $item['prev'] : '^head', 'next' => !empty($item['next']) ? $item['next'] : '^end', 'type' => 1, 'description' => isset($item['description']) ? $item['description'] : null, 'subject' => isset($item['subject']) ? $item['subject'] : null));
                 if (is_array($item['sections'])) {
                     foreach ($item['sections'] as $section) {
                         $flow->addStepSection($stepId, $section);
                     }
                 } else {
                     $flow->addStepSection($stepId, $item['sections'], $tudu);
                 }
             }
             $flow->flowTo($stepIds[0]);
             $users = $flow->getCurrentUser();
             $flow->isPrepared = true;
             $tudu->reviewer = $users;
             $tudu->setExtension($flow);
         }
         // 新建的
         if (!$this->_fromApply) {
             if ($apply->target) {
                 list($userName, $trueName) = explode(' ', $apply->target);
                 $addressBook = Tudu_AddressBook::getInstance();
                 $user = $addressBook->searchUser($userName);
                 if ($user) {
                     $apply->uniqueId = $user['uniqueid'];
                     $apply->userInfo = $userName . ' ' . $trueName;
                     $tudu->bcc = array(array('uniqueid' => $user['uniqueid'], 'username' => $user['username'], 'truename' => $user['truename']));
                 }
             } else {
                 $apply->uniqueId = $apply->senderId;
                 $apply->userInfo = $tudu->poster . ' ' . $tudu->email;
             }
         }
     }
 }
Example #2
0
 /**
  * 编辑图度时进行的流程
  *
  * @param Model_Tudu_Tudu $tudu
  */
 public function updateTuduFlow(Model_Tudu_Tudu &$tudu)
 {
     /* @var $flow Model_Tudu_Extension_Flow */
     $flow = $tudu->getExtension('Model_Tudu_Extension_Flow');
     if (null === $flow) {
         /**
          * @see Model_Tudu_Extension_Flow
          */
         require_once 'Model/Tudu/Extension/Flow.php';
         $flow = new Model_Tudu_Extension_Flow();
     }
     if ($flow->isPrepared) {
         return;
     }
     $isCreate = true;
     if ($tudu->tuduId) {
         /* @var $daoFlow Dao_Td_Tudu_Flow */
         $daoFlow = Tudu_Dao_Manager::getDao('Dao_Td_Tudu_Flow', Tudu_Dao_Manager::DB_TS);
         $savedFlow = $daoFlow->getFlow(array('tuduid' => $tudu->tuduId));
         $isCreate = false;
         if (null != $savedFlow) {
             $flow->fromFlow = true;
         }
         if (!$tudu->isDraft && $savedFlow) {
             $flowInfo = $savedFlow->toArray();
             $flowSteps = array();
             foreach ($flowInfo['steps'] as $sid => $st) {
                 if (empty($sid) || empty($st)) {
                     continue;
                 }
                 $flowSteps[$sid] = $st;
             }
             $flowInfo['steps'] = $flowSteps;
             // 工作流,检查是否有拒绝或不同意项目,然后重发
             if ($tudu->flowId || $tudu->fromTudu && $tudu->fromTudu->appId == 'attend') {
                 $isBreak = false;
                 $flowTo = $flowInfo['currentstepid'];
                 foreach ($flowInfo['steps'] as $sid => $st) {
                     foreach ($st['section'] as $idx => $sec) {
                         foreach ($sec as $i => $u) {
                             if (isset($u['status']) && $u['status'] > 2) {
                                 $isTo = 0 === strpos($flowTo, '^') || null === $flowTo;
                                 $flowInfo['steps'][$sid]['section'][$idx][$i]['status'] = $isTo ? 1 : 0;
                                 if ($isTo) {
                                     $flowTo = $st['stepid'];
                                 }
                             }
                         }
                     }
                 }
                 $flow->setAttributes($flowInfo);
                 $flow->flowTo($flowTo);
                 //var_dump($flow->steps);exit;
                 if (0 !== strpos($flow->currentStepId, '^')) {
                     $step = $flow->getStep($flow->currentStepId);
                     $users = $flow->getCurrentUser();
                     $receiver = array();
                     foreach ($users as $u) {
                         $receiver[$u['username']] = array('uniqueid' => $u['uniqueid'], 'email' => $u['username'], 'username' => $u['username'], 'truename' => $u['truename']);
                     }
                     if ($step['type'] == 2) {
                         $tudu->acceptMode = 1;
                     }
                     if ($step['type'] == 1) {
                         $tudu->reviewer = $receiver;
                     } else {
                         $tudu->to = $receiver;
                     }
                 }
                 return;
             }
         } else {
             $isCreate = true;
         }
     }
     if (!isset($flowInfo)) {
         $flowInfo = array('orgid' => $tudu->orgId, 'tuduid' => $tudu->tuduId);
     }
     $flow->setAttributes($flowInfo);
     // 已经存在流程
     // 1. 定位当前步骤
     // 2. 对比当前执行和审批是否与输入一致
     // 3. 不一致则标记原有,一样则维持原状
     if ($isCreate) {
         if ($tudu->flowId) {
             $this->prepareFlowFromTemplate($tudu, $flow);
         } else {
             $this->renewTuduFlow($tudu, $flow);
         }
     } else {
         if (!$flow->steps || !is_array($flow->steps)) {
             return $this->appendTuduFlow($tudu, $flow);
         }
         $steps = $flow->steps;
         $lastStep = end($steps);
         $currentStepId = $flow->currentStepId == '^end' ? $lastStep['stepid'] : $flow->currentStepId;
         $step = isset($flow->steps[$currentStepId]) ? $flow->steps[$currentStepId] : null;
         $isDisagree = false;
         foreach ($flow->steps as $item) {
             if (!isset($item['section']) || !is_array($item['section'])) {
                 continue;
             }
             foreach ($item['section'] as $sec) {
                 foreach ($sec as $u) {
                     if ($item['type'] == 1 && isset($u['status']) && $u['status'] > 2) {
                         if (null === $step && 0 === strpos($flow->currentStepId, '^')) {
                             $step = $item;
                             $flow->currentStepId = $item['stepid'];
                         }
                         $isDisagree = $item['type'] == 1;
                         break 3;
                         // 拒绝的
                     } elseif ($item['type'] == 0 && isset($u['status']) && ($u['status'] <= 1 || $u['status'] == 3)) {
                         if (null === $step && 0 === strpos($flow->currentStepId, '^')) {
                             $step = $item;
                             $flow->currentStepId = $item['stepid'];
                         }
                         break 3;
                     }
                 }
             }
         }
         if (null === $step) {
             if (empty($flow->steps)) {
                 return $this->appendTuduFlow($tudu, $flow);
             }
             return;
         }
         $isModified = false;
         $isReview = $step['type'] == 1;
         $users = $isReview ? (array) $tudu->reviewer : (array) $tudu->to;
         $isPass = true;
         $base = 0;
         $mBase = 0;
         $secIdx = 0;
         // 处理输入
         $section = $step['section'][$step['currentSection']];
         $currentSection = array();
         $completeUsers = array();
         $currentUsers = array();
         $currentStepId = $flow->currentStepId;
         $prev = $flow->currentStepId;
         $flowTo = null;
         $flowToSec = null;
         // 保持当前已经完成或审批通过的帐号信息
         foreach ($section as $u) {
             $currentUsers[] = $u['username'];
             if ($u['status'] == 2) {
                 $currentSection[] = $u;
                 $completeUsers[] = $u['username'];
             }
         }
         // 当前步骤为执行,把非完成的步骤设为取消
         if (!$isReview) {
             $section = $step['section'][$step['currentSection']];
             $to = reset($users);
             $isCancel = count($section) != count($to) && $tudu->operation == 'send' && !$isDisagree;
             if (!$isCancel) {
                 $toArr = array();
                 foreach ($to as $item) {
                     $toArr[] = $item['username'];
                 }
                 foreach ($section as $u) {
                     if (!in_array($u['username'], $toArr)) {
                         $isCancel = !$isDisagree;
                         break;
                     }
                 }
             }
             if ($isCancel) {
                 $flow->cancelStepSection($step['stepid'], $step['currentSection']);
                 $prev = $step['prev'];
             }
         }
         // 移除当前步骤后续
         //if (!$isDisagree) {
         /*for ($i = $step['currentSection'] + 1, $l = count($step['section']); $i < $l; $i++) {
               $flow->removeStepSection($step['stepid'], $i);
           }*/
         //}
         // 移除后续的所有步骤
         $steps = $flow->getSteps();
         $isExceed = false;
         foreach ($steps as $id => $item) {
             if ($id == $flow->currentStepId) {
                 $isExceedSection = false;
                 foreach ($item['section'] as $idx => $sec) {
                     if ($idx == $item['currentSection']) {
                         $isExceedSection = true;
                         if ($isReview) {
                             continue;
                         }
                     }
                     if ($isExceedSection) {
                         $flow->removeStepSection($id, $idx);
                     }
                 }
                 $isExceed = true;
                 continue;
             }
             if ($isExceed) {
                 $flow->deleteStep($id);
                 if ($id == $prev) {
                     $prev = $item['prev'];
                 }
             }
         }
         if (empty($flow->steps[$flow->currentStepId]['section'])) {
             $flow->deleteStep($step['stepid']);
             if ($isReview) {
                 $isReview = false;
             }
         }
         if (!isset($flow->steps[$prev])) {
             $prev = '^head';
         }
         // 添加审批人
         if ($tudu->reviewer) {
             $reviewer = $tudu->reviewer;
             if (!$isReview) {
                 $stepId = $flow->addStep(array('type' => Dao_Td_Tudu_Step::TYPE_EXAMINE, 'prev' => $prev));
                 if (!$tudu->flowId) {
                     $flow->updateStep($prev, array('next' => $stepId));
                 }
                 $prev = $stepId;
             } else {
                 $stepId = $flow->currentStepId;
             }
             $flowTo = $stepId;
             foreach ($reviewer as $idx => $sec) {
                 $users = $isReview && $idx == 0 ? $currentSection : array();
                 if ($isReview && $idx == 0 && isset($flow->steps[$currentStepId]['currentSection'])) {
                     $secIdx = $flow->steps[$currentStepId]['currentSection'];
                     $section = $flow->steps[$currentStepId]['section'][$secIdx];
                     foreach ($section as $i => $cu) {
                         if (!isset($sec[$cu['username']]) && $cu['status'] != 2) {
                             $flow->removeStepSectionUser($currentStepId, $secIdx, $cu['username']);
                         } elseif ($cu['status'] == 3) {
                             $flow->updateStepSectionUser($currentStepId, $secIdx, $cu['username'], array('status' => 1));
                             $completeUsers[] = $cu['username'];
                         }
                     }
                 }
                 foreach ($sec as $u) {
                     // 跳过已经完成审批的用户
                     if ($idx == 0 && $isReview) {
                         if (!in_array($u['username'], $completeUsers)) {
                             $u['status'] = 1;
                             $flow->addStepSectionUser($currentStepId, $flow->steps[$currentStepId]['currentSection'], $u);
                         }
                         continue;
                     }
                     $users[] = $u;
                 }
                 if (!empty($users) && !($isReview && $idx == 0)) {
                     $flow->addStepSection($stepId, $users);
                 }
             }
             $tudu->reviewer = reset($reviewer);
             // 移除当前审批人
         } elseif ($isReview) {
             $secIdx = $flow->steps[$currentStepId]['currentSection'];
             $section = $flow->steps[$currentStepId]['section'][$secIdx];
             foreach ($section as $i => $cu) {
                 if ($cu['status'] != 2) {
                     $flow->removeStepSectionUser($currentStepId, $secIdx, $cu['username']);
                 }
             }
             if (empty($flow->steps[$currentStepId]['section'])) {
                 $flow->deleteStep($step['stepid']);
             }
         }
         if (!count($flow->steps)) {
             $prev = '^head';
         }
         if ($prev && 0 !== strpos($prev, '^head')) {
             $step = $flow->getStep($prev);
             if (empty($step['section'])) {
                 $flow->deleteStep($prev);
                 $prev = '^head';
             }
         }
         // 添加当前步骤
         if ($tudu->to) {
             $to = $tudu->to;
             $executeStepId = $flow->addStep(array('type' => $tudu->acceptMode ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE, 'prev' => $prev, 'next' => '^end'));
             if (null === $flowTo) {
                 $flowTo = $executeStepId;
             }
             foreach ($to as $idx => $sec) {
                 $users = !$isReview && $idx == 0 ? $currentSection : array();
                 foreach ($sec as $u) {
                     if ($idx == 0 && !$isReview && in_array($u['username'], $completeUsers)) {
                         continue;
                     }
                     $users[] = $u;
                 }
                 $flow->addStepSection($executeStepId, $users);
             }
             $tudu->to = reset($to);
         }
         $flow->flowTo($flowTo);
     }
 }