PageRenderTime 57ms CodeModel.GetById 2ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 1ms

/library/Tudu/Tudu/Extension/Flow.php

https://github.com/polokk/tudu-web-1
PHP | 1987 lines | 1210 code | 294 blank | 483 comment | 256 complexity | 0c9edfc89510f9445be3eab29d3445e1 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/**
   3 * Tudu Library
   4 *
   5 * LICENSE
   6 *
   7 *
   8 * @category   Tudu
   9 * @package    Tudu_Model
  10 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  11 * @link       http://www.oray.com/
  12 * @version    $Id: Flow.php 2755 2013-02-22 09:50:47Z chenyongfa $
  13 */
  14
  15/**
  16 * 工作流扩展
  17 *
  18 * @category   Tudu
  19 * @package    Tudu_Tudu_Storage
  20 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  21 */
  22class Tudu_Tudu_Extension_Flow extends Tudu_Tudu_Extension_Abstract
  23{
  24
  25    /**
  26     *
  27     * @var string
  28     */
  29    const NODE_HEAD = '^head';
  30    const NODE_END  = '^end';
  31    const NODE_BREAK  = '^break';
  32    const NODE_UPPER  = '^upper';
  33
  34    /**
  35     *
  36     * @var 部门列表
  37     */
  38    protected $_depts;
  39
  40    /**
  41     * 创建步骤
  42     *
  43     * @param $tuduId
  44     * @param $params
  45     */
  46    public function createStep(array $params)
  47    {
  48        return $this->getDao('Dao_Td_Tudu_Step')->createStep($params);
  49    }
  50
  51    /**
  52     * 更新步骤
  53     *
  54     * @param $tuduId
  55     * @param $stepId
  56     * @param $params
  57     */
  58    public function updateStep($tuduId, $stepId, array $params)
  59    {
  60        return $this->getDao('Dao_Td_Tudu_Step')->updateStep($tuduId, $stepId, $params);
  61    }
  62
  63    /**
  64     * 获取步骤数据
  65     *
  66     * @param $tuduId
  67     * @param $stepId
  68     */
  69    public function getStepById($tuduId, $stepId)
  70    {
  71        return $this->getDao('Dao_Td_Tudu_Step')->getStep(array('tuduid' => $tuduId, 'stepid' => $stepId));
  72    }
  73
  74    /**
  75     * 获取步骤列表
  76     *
  77     * @param $tuduId
  78     */
  79    public function getSteps($tuduId)
  80    {
  81        return $this->getDao('Dao_Td_Tudu_Step')->getSteps(array('tuduid' => $tuduId), null, 'ordernum ASC');
  82    }
  83
  84    /**
  85     * 获取工作流
  86     *
  87     * @param $flowId
  88     */
  89    public function getFlowById($flowId)
  90    {
  91        return $this->getDao('Dao_Td_Flow_Flow')->getFlow(array('flowid' => $flowId));
  92    }
  93
  94    /**
  95     *
  96     * @param $orgId
  97     * @param $uniqueId
  98     * @param $users
  99     */
 100    public function prepareRecipients($orgId, $uniqueId, array $users)
 101    {
 102
 103        $addressBook = Tudu_AddressBook::getInstance();
 104
 105        $recipients = array();
 106
 107        foreach ($users as $key => $item) {
 108            $user = $addressBook->searchUser($orgId, $item['email']);
 109            if (null === $user) {
 110                $user = $addressBook->searchContact($uniqueId, $item['email'], $item['truename']);
 111
 112                if (null === $user) {
 113                    $user = $addressBook->prepareContact($item['email'], $item['truename']);
 114                }
 115            }
 116
 117            $user['accepterinfo'] = $user['email'] . ' ' . $user['truename'];
 118            $user['percent']      = isset($item['percent']) ? (int) $item['percent'] : 0;
 119
 120            if (isset($item['processindex'])) {
 121                $user['processindex'] = $item['processindex'];
 122            }
 123
 124            if (isset($item['stepstatus'])) {
 125                $user['stepstatus'] = $item['stepstatus'];
 126            }
 127
 128            $recipients[$user['uniqueid']] = $user;
 129        }
 130
 131        return $recipients;
 132    }
 133
 134    /**
 135     * 添加步骤用户
 136     *
 137     * @param $tuduId
 138     * @param $stepId
 139     * @param $users
 140     */
 141    public function addStepUsers($tuduId, $stepId, array $users, $startIndex = null)
 142    {
 143        /* @var $daoStep Dao_Td_Tudu_Step */
 144        $daoStep = $this->getDao('Dao_Td_Tudu_Step');
 145
 146        $isOrder = is_int($startIndex);
 147
 148        $count = 0;
 149        foreach ($users as $item) {
 150            $processIndex = isset($item['processindex'])
 151                          ? $item['processindex']
 152                          : ($isOrder ? ++$startIndex : 0);
 153
 154            $status = isset($item['stepstatus'])
 155                    ? $item['stepstatus']
 156                    : ($isOrder && $count == 0 ? 1 : 0);
 157
 158            $params = array(
 159                'tuduid'       => $tuduId,
 160                'stepid'       => $stepId,
 161                'uniqueid'     => $item['uniqueid'],
 162                'userinfo'     => $item['accepterinfo'],
 163                'status'       => $status,
 164                'processindex' => $processIndex
 165            );
 166
 167            if (array_key_exists('percent', $item)) {
 168                $params['percent'] = (int) $item['percent'];
 169            }
 170
 171            $daoStep->addUser($params);
 172
 173            $count ++;
 174        }
 175
 176        return $count > 0;
 177    }
 178
 179    /**
 180     *
 181     * @param string $tuduId
 182     * @param string $stepId
 183     * @param string $uniqueId
 184     * @param array  $params
 185     * @return boolean
 186     */
 187    public function updateStepUser($tuduId, $stepId, $uniqueId, array $params)
 188    {
 189        return $this->getDao('Dao_Td_Tudu_Step')->updateUser($tuduId, $stepId, $uniqueId, $params);
 190    }
 191
 192    /**
 193     *
 194     * @param string $tuduId
 195     */
 196    public function getTuduStepUsers($tuduId)
 197    {
 198        return $this->getDao('Dao_Td_Tudu_Step')->getTuduStepUsers($tuduId);
 199    }
 200
 201    /**
 202     * 获取步骤执行人列表
 203     *
 204     * @param string $tuduId
 205     * @param string $stepId
 206     */
 207    public function getStepUsers($tuduId, $stepId)
 208    {
 209        return $this->getDao('Dao_Td_Tudu_Step')->getUsers($tuduId, $stepId);
 210    }
 211
 212    /**
 213     *
 214     * @param string $tuduId
 215     * @param string $stepId
 216     * @param string $uniqueId
 217     */
 218    public function getStepUser($tuduId, $stepId, $uniqueId)
 219    {
 220        return $this->getDao('Dao_Td_Tudu_Step')->getCurrentStep($tuduId, $stepId, $uniqueId);
 221    }
 222
 223    /**
 224     * 删除步骤执行人
 225     *
 226     * @param $tuduId
 227     * @param $stepId
 228     * @param $uniqueId
 229     */
 230    public function removeStepUsers($tuduId, $stepId, $uniqueId = null)
 231    {
 232        return $this->getDao('Dao_Td_Tudu_Step')->deleteUsers($tuduId, $stepId, $uniqueId);
 233    }
 234
 235    /**
 236     * 更新步骤用户
 237     *
 238     * @param $tuduId
 239     * @param $stepId
 240     * @param $users
 241     */
 242    public function updateStepUsers(Tudu_Tudu_Storage_Tudu &$tudu, $stepId, array $users)
 243    {
 244        $currentUsers = $this->getStepUsers($tudu->tuduId, $stepId);
 245
 246        /* @var $daoTudu Dao_Td_Tudu_Tudu */
 247        $daoTudu = $this->getDao('Dao_Td_Tudu_Tudu');
 248
 249        $processIndex = null;
 250        $removes = array();
 251        foreach ($currentUsers as $item) {
 252            list($email, $trueName) = explode(' ', $item['userinfo'], 2);
 253            if ($item['status'] == 2 && !array_key_exists($email, $users)) {
 254                $processIndex = (int) $item['processindex'];
 255            } else {
 256                if (null === $processIndex) {
 257                    $processIndex = (int) $item['processindex'] - 1;
 258                }
 259
 260                $removes[] = $item['uniqueid'];
 261            }
 262
 263            // 正在审批的移除待审批标签
 264            if ($item['status'] == 1) {
 265                $daoTudu->deleteLabel($tudu->tuduId, $item['uniqueid'], '^e');
 266
 267                if (!array_key_exists($email, $tudu->to)
 268                    && !array_key_exists($email, $tudu->cc)
 269                    && $email != $tudu->sender)
 270                {
 271                    $daoTudu->deleteLabel($tudu->tuduId, $item['uniqueid'], '^i');
 272                }
 273            }
 274        }
 275
 276        foreach ($users as $k => $item) {
 277            $users[$k]['processindex'] = $users[$k]['processindex'] + $processIndex;
 278        }
 279
 280        if ($removes) {
 281            $this->removeStepUsers($tudu->tuduId, $stepId, $removes);
 282        }
 283
 284        // 添加审批人
 285        $recipients = $this->prepareRecipients($tudu->orgId, $stepId, $users);
 286        $this->addStepUsers($tudu->tuduId, $stepId, $recipients, $processIndex);
 287    }
 288
 289    /**
 290     * 删除步骤
 291     *
 292     * @param $tuduId
 293     * @param $stepId
 294     */
 295    public function deleteStep($tuduId, $stepId)
 296    {
 297        return $this->getDao('Dao_Td_Tudu_Step')->deleteStep($tuduId, $stepId);
 298    }
 299
 300    /**
 301     * 删除后续步骤
 302     *
 303     * @param string $tuduId
 304     * @param string $stepId
 305     */
 306    public function deleteNextSteps($tuduId, $stepId)
 307    {
 308        return $this->getDao('Dao_Td_Tudu_Step')->removeNextSteps($tuduId, $stepId);
 309    }
 310
 311    /**
 312     * 删除所有步骤
 313     *
 314     * @param $tuduId
 315     * @param $stepId
 316     */
 317    public function deleteAllSteps($tuduId)
 318    {
 319        return $this->getDao('Dao_Td_Tudu_Step')->deleteSteps($tuduId);
 320    }
 321
 322    /**
 323     * 设置图度当前步骤
 324     *
 325     * @param $tuduId
 326     * @param $stepId
 327     */
 328    public function flowToStep($tuduId, $stepId)
 329    {
 330        //return $this->getDao('Dao_Td_Tudu_Tudu')->updateTudu($tuduId, array('stepid' => $stepId));
 331    }
 332
 333    /**
 334     *
 335     * @param $tudu
 336     * @param $stepId
 337     * @param $users
 338     */
 339    public function cancelStep(Tudu_Tudu_Storage_Tudu &$tudu, $stepId)
 340    {
 341        $this->updateStep($tudu->tuduId, $stepId, array('status' => 4));
 342
 343        $users = $this->getStepUsers($tudu->tuduId, $stepId);
 344
 345        /* @var $daoTudu Dao_Td_Tudu_Tudu */
 346        $daoTudu = $this->getDao('Dao_Td_Tudu_Tudu');
 347
 348        foreach ($users as $item) {
 349            list($email, $trueName) = explode(' ', $item['userinfo'], 2);
 350
 351
 352            // 正在审批的移除待审批标签
 353            if ($item['status'] == 1) {
 354                $daoTudu->deleteLabel($tudu->tuduId, $item['uniqueid'], '^e');
 355
 356                if (!array_key_exists($email, $tudu->to)
 357                    && !array_key_exists($email, $tudu->cc)
 358                    && $email != $tudu->sender)
 359                {
 360                    $daoTudu->deleteLabel($tudu->tuduId, $item['uniqueid'], '^i');
 361                }
 362            }
 363        }
 364    }
 365
 366    /**
 367     *
 368     * @param Tudu_Tudu_Storage_Tudu $tudu
 369     * @param array                  $params
 370     * @return array
 371     */
 372    public function onPrepare(Tudu_Tudu_Storage_Tudu &$tudu, array $params)
 373    {
 374        if ($tudu->flowId && !$tudu->stepId) {
 375            $this->onPrepareFlow($tudu);
 376        }
 377
 378        if ($tudu->reviewer && !$tudu->to && $tudu->type != 'notice') {
 379            $tudu->to = Tudu_Tudu_Storage::formatRecipients($tudu->from);
 380        }
 381    }
 382
 383    /**
 384     * 整理工作步骤
 385     *
 386     * @param Tudu_Tudu_Storage_Tudu $tudu
 387     */
 388    public function onPrepareFlow(Tudu_Tudu_Storage_Tudu &$tudu)
 389    {
 390        $flow = $this->getFlowById($tudu->flowId);
 391
 392        if (null !== $flow) {
 393            $flow = $flow->toArray();
 394
 395            // 没有步骤
 396            if (count($flow['steps']) <= 0) {
 397                require_once 'Tudu/Tudu/Exception.php';
 398                throw new Tudu_Tudu_Exception('Flow has not any steps', Tudu_Tudu_Exception::CODE_FLOW_STEP_NULL);
 399            }
 400
 401            // 整理抄送人
 402            if (!empty($flow['cc'])) {
 403                $cc = array();
 404                foreach ($flow['cc'] as $key => $item) {
 405                    if (false !== strpos($key, '@')) {
 406                        $cc[$key] = array('email' => $key, 'truename' => $item[0]);
 407                    } else {
 408                        $cc[$key] = array('groupid' => $key, 'truename' => $item[0]);
 409                    }
 410                }
 411                $tudu->cc = array_merge($tudu->cc, $cc);
 412            }
 413
 414            // 第一步骤ID
 415            $steps       = $flow['steps'];
 416            $prevUsers   = array(array('email' => $tudu->email));
 417            $prevType    = 0;
 418            $addressBook = Tudu_AddressBook::getInstance();
 419            $depts       = $this->_getDepartments($tudu->orgId);
 420            $tuduSteps   = array();
 421            $orderNum    = 1;
 422
 423            foreach ($steps as $key => $step) {
 424                $stepId = $step['id'];
 425
 426                if ($step['type'] == 1) {
 427                    // 上级审批
 428                    if ($step['users'] == '^upper') {
 429                        // 上一步是审批
 430                        $reviewerIds = array();
 431
 432                        if ($prevType == 1) {
 433                            foreach ($prevUsers as $item) {
 434                                foreach ($item as $user) {
 435                                    $user = $addressBook->searchUser($tudu->orgId, $user['email']);
 436
 437                                    if (!$user) {
 438                                        require_once 'Tudu/Tudu/Exception.php';
 439                                        throw new Tudu_Tudu_Exception('User is not exists', Tudu_Tudu_Exception::CODE_NOT_EXISTS_USER);
 440                                    }
 441
 442                                    $moderatorIds = $this->_getUpper($user['email'], $tudu->orgId, $user['deptid']);
 443                                    foreach ($moderatorIds as $uid) {
 444                                        $reviewerIds[] = $uid;
 445                                    }
 446                                }
 447                            }
 448                        } else {
 449                            foreach ($prevUsers as $user) {
 450                                $user = $addressBook->searchUser($tudu->orgId, $user['email']);
 451
 452                                if (!$user) {
 453                                    require_once 'Tudu/Tudu/Exception.php';
 454                                    throw new Tudu_Tudu_Exception('User is not exists', Tudu_Tudu_Exception::CODE_NOT_EXISTS_USER);
 455                                }
 456
 457                                $moderatorIds = $this->_getUpper($user['email'], $tudu->orgId, $user['deptid']);
 458                                foreach ($moderatorIds as $uid) {
 459                                    $reviewerIds[] = $uid;
 460                                }
 461                            }
 462                        }
 463
 464                        if (empty($reviewerIds)) {
 465                            require_once 'Tudu/Tudu/Exception.php';
 466                            throw new Tudu_Tudu_Exception('Upper is not exists', Tudu_Tudu_Exception::CODE_NOT_EXISTS_UPPER);
 467                        }
 468
 469                        $reviewers   = array();
 470                        $reviewerIds = array_unique($reviewerIds);
 471                        foreach ($reviewerIds as $uId) {
 472                            $user = $addressBook->searchUser($tudu->orgId, $uId . '@' . $tudu->orgId);
 473
 474                            if (empty($user)) {
 475                                require_once 'Tudu/Tudu/Exception.php';
 476                                throw new Tudu_Tudu_Exception('User is not exists', Tudu_Tudu_Exception::CODE_NOT_EXISTS_USER);
 477                            }
 478
 479                            $reviewers[] = $user;
 480                        }
 481
 482                        $users     = array($reviewers);
 483                        $prevUsers = $users;
 484
 485                        // 指定
 486                    } else {
 487                        $prevUsers = $users = Tudu_Tudu_Storage::formatReviewer($step['users']);
 488
 489                        foreach ($users as $item) {
 490                            foreach ($item as $u) {
 491                                $user = $addressBook->searchUser($tudu->orgId, $u['email']);
 492
 493                                if (!$user) {
 494                                    require_once 'Tudu/Tudu/Exception.php';
 495                                    throw new Tudu_Tudu_Exception('User is not exists', Tudu_Tudu_Exception::CODE_NOT_EXISTS_USER);
 496                                }
 497                            }
 498                        }
 499                    }
 500
 501                    $recipients   = array();
 502                    $processIndex = 1;
 503                    foreach ($users as $item) {
 504                        foreach ($item as $user) {
 505                            $recipients[] = array(
 506                                'email'        => $user['email'],
 507                                'truename'     => $user['truename'],
 508                                'processindex' => $processIndex,
 509                                'stepstatus'   => $processIndex == 1 ? 1 : 0
 510                            );
 511                        }
 512                        $processIndex ++;
 513                    }
 514
 515                    $tuduSteps[$stepId] = array(
 516                        'orgid'      => $tudu->orgId,
 517                        'tuduid'     => $tudu->tuduId,
 518                        'uniqueid'   => $tudu->uniqueId,
 519                        'stepid'     => $stepId,
 520                        'type'       => Dao_Td_Tudu_Step::TYPE_EXAMINE,
 521                        'prevstepid' => $step['prev'],
 522                        'nextstepid' => $step['next'],
 523                        'users'      => $recipients,
 524                        'ordernum'   => $orderNum ++,
 525                        'createtime' => time()
 526                    );
 527
 528                } else {
 529                    $prevUsers = $users = Tudu_Tudu_Storage::formatRecipients($step['users']);
 530
 531                    foreach ($prevUsers as $u) {
 532                        $user = $addressBook->searchUser($tudu->orgId, $u['email']);
 533
 534                        if (!$user) {
 535                            require_once 'Tudu/Tudu/Exception.php';
 536                            throw new Tudu_Tudu_Exception('User is not exists', Tudu_Tudu_Exception::CODE_NOT_EXISTS_USER);
 537                        }
 538                    }
 539
 540                    $tuduSteps[$stepId] = array(
 541                        'orgid'      => $tudu->orgId,
 542                        'tuduid'     => $tudu->tuduId,
 543                        'uniqueid'   => $tudu->uniqueId,
 544                        'stepid'     => $stepId,
 545                        'type'       => $step['type'] == 2 ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE,
 546                        'prevstepid' => $step['prev'],
 547                        'nextstepid' => $step['next'],
 548                        'users'      => $users,
 549                        'ordernum'   => $orderNum ++,
 550                        'createtime' => time()
 551                    );
 552                }
 553
 554                $prevType  = $step['type'];
 555            }
 556
 557            $tudu->steps = $tuduSteps;
 558            // 第一步骤ID
 559            $firstStep    = reset($tuduSteps);
 560            $tudu->stepId = $firstStep['stepid'];
 561            if ($firstStep['type'] == 1) {
 562                $users = $firstStep['users'];
 563                $reviewers = array();
 564                foreach ($users as $user) {
 565                    if ($user['processindex'] == 1) {
 566                        $reviewers[] = $user;
 567                    }
 568                }
 569
 570                $tudu->reviewer = array($reviewers);
 571            } else {
 572                $tudu->to = $firstStep['users'];
 573            }
 574            $tudu->status = Dao_Td_Tudu_Tudu::STATUS_DOING;
 575        }
 576    }
 577
 578    /**
 579     * 根据工作流创建图度步骤
 580     *
 581     * @param $tudu
 582     */
 583    public function createFlowSteps(Tudu_Tudu_Storage_Tudu &$tudu)
 584    {
 585
 586        if ($tudu->flowId && $tudu->steps) {
 587
 588            $steps    = $tudu->steps;
 589
 590            // 第一步骤ID
 591            /*$firstStep    = reset($steps);
 592            $tudu->stepId = $firstStep['stepid'];
 593            if ($firstStep['type'] == 1) {
 594                $users = $firstStep['users'];
 595                $reviewers = array();
 596                foreach ($users as $user) {
 597                    if ($user['processindex'] == 1) {
 598                        $reviewers[] = $user;
 599                    }
 600                }
 601
 602                $tudu->reviewer = array($reviewers);
 603            } else {
 604                $tudu->to = $firstStep['users'];
 605            }*/
 606
 607        } else {
 608            $steps = array();
 609
 610            $currentStepId = null;
 611            $orderNum      = 1;
 612            if ($tudu->reviewer) {
 613                $reviewers  = $tudu->reviewer;
 614                $users = array();
 615                $processIndex = 1;
 616
 617                foreach ($reviewers as $item) {
 618                    foreach ($item as $reviewer) {
 619                        $users[] = array(
 620                            'email'        => $reviewer['email'],
 621                            'truename'     => $reviewer['truename'],
 622                            'processindex' => $processIndex,
 623                            'stepstatus'   => $processIndex == 1 ? 1 : 0
 624                        );
 625                    }
 626
 627                    $processIndex ++;
 628                }
 629
 630                $stepId  = Dao_Td_Tudu_Step::getStepId();
 631                $steps[$stepId] = array(
 632                    'orgid' => $tudu->orgId,
 633                    'tuduid' => $tudu->tuduId,
 634                    'uniqueid' => $tudu->uniqueId,
 635                    'stepid'   => $stepId,
 636                    'type'     => Dao_Td_Tudu_Step::TYPE_EXAMINE,
 637                    'prevstepid' => self::NODE_HEAD,
 638                    'nextstepid' => self::NODE_END,
 639                    'users'      => $users,
 640                    'ordernum'   => $orderNum ++,
 641                    'createtime' => time()
 642                );
 643
 644                $currentStepId = $stepId;
 645            }
 646
 647            if ($tudu->to) {
 648                $stepId  = Dao_Td_Tudu_Step::getStepId();
 649                $prevId  = self::NODE_HEAD;
 650
 651                if ($currentStepId) {
 652                    $steps[$currentStepId]['nextstepid'] = $stepId;
 653                    $prevId = $currentStepId;
 654                } else {
 655                    $currentStepId = $stepId;
 656                }
 657
 658                $steps[$stepId] = array(
 659                    'orgid' => $tudu->orgId,
 660                    'tuduid' => $tudu->tuduId,
 661                    'uniqueid' => $tudu->uniqueId,
 662                    'stepid'   => $stepId,
 663                    'type'     => $tudu->acceptMode ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE,
 664                    'prevstepid' => $prevId,
 665                    'nextstepid' => self::NODE_END,
 666                    'users'      => $tudu->to,
 667                    'ordernum'   => $orderNum ++,
 668                    'createtime' => time()
 669                );
 670            }
 671        }
 672/*
 673        foreach ($tudu->steps as $key => $step) {
 674            $stepId = $step['id'];
 675
 676            if ($step['type'] == 1) {
 677                $users = array();
 678                $processIndex = 1;
 679                $reviewers = array();
 680
 681                if ($step['users'] == self::NODE_UPPER) {
 682                    // 取上一步骤
 683                    $prevStep = $tudu->steps[$key - 1];
 684                    if (isset($prevStep)) {
 685                        $addressBook = Tudu_AddressBook::getInstance();
 686                        $reviewerIds = array();
 687                        if ($prevStep['type'] == 1) {
 688                            $prevUsers = Tudu_Tudu_Storage::formatReviewer($prevStep['users']);
 689                            foreach ($prevUsers as $item) {
 690                                foreach ($item as $user) {
 691                                    $user = $addressBook->searchUser($tudu->orgId, $user['email']);
 692                                    if ($user) {
 693                                        $dept = $this->getDepartment($tudu->orgId, $user['deptid']);
 694                                        if (empty($dept->moderators)) {
 695                                            continue;
 696                                        }
 697                                        foreach ($dept->moderators as $m) {
 698                                            $reviewerIds[] = $m;
 699                                        }
 700                                    }
 701                                }
 702                            }
 703                        } else {
 704                            $prevUsers = Tudu_Tudu_Storage::formatRecipients($prevStep['users']);
 705                            foreach ($prevUsers as $user) {
 706                                $user = $addressBook->searchUser($tudu->orgId, $user['email']);
 707                                if ($user) {
 708                                    $dept = $this->getDepartment($tudu->orgId, $user['deptid']);
 709                                    if (empty($dept->moderators)) {
 710                                        continue;
 711                                    }
 712                                    foreach ($dept->moderators as $m) {
 713                                        $reviewerIds[] = $m;
 714                                    }
 715                                }
 716                            }
 717                        }
 718
 719                        if (empty($reviewerIds)) {
 720                            //throw new Zend_Controller_Action_Exception('', 404);
 721                        }
 722
 723                        $reviewerIds = array_unique($reviewerIds);
 724                        foreach ($reviewerIds as $userId) {
 725                            $user = $addressBook->searchUser($tudu->orgId, $userId . '@' . $tudu->orgId);
 726                            if ($user) {
 727                                end($reviewers);
 728                                $last = key($reviewers);
 729                                $reviewers[$last][] = array('email' => $user['email'], 'truename' => $user['truename']);
 730                            }
 731                        }
 732                    }
 733                } else {
 734                    $reviewers    = Tudu_Tudu_Storage:: _formatReviewer($step['users']);
 735                }
 736
 737                foreach ($reviewers as $item) {
 738                    foreach ($item as $reviewer) {
 739                        $users[] = array(
 740                            'email'        => $reviewer['email'],
 741                            'truename'     => $reviewer['truename'],
 742                            'processindex' => $processIndex,
 743                            'stepstatus'   => $processIndex == 1 ? 1 : 0
 744                        );
 745                    }
 746
 747                    $processIndex ++;
 748                }
 749
 750                $steps[$stepId] = array(
 751                    'orgid'      => $tudu->orgId,
 752                    'tuduid'     => $tudu->tuduId,
 753                    'uniqueid'   => $tudu->uniqueId,
 754                    'stepid'     => $stepId,
 755                    'type'       => Dao_Td_Tudu_Step::TYPE_EXAMINE,
 756                    'prevstepid' => $step['prev'],
 757                    'nextstepid' => $step['next'],
 758                    'users'      => $users,
 759                    'ordernum'   => $orderNum ++
 760                );
 761
 762            } else {
 763                $recipients = Tudu_Tudu_Storage::formatRecipients($step['users']);
 764
 765                $steps[$stepId] = array(
 766                    'orgid'      => $tudu->orgId,
 767                    'tuduid'     => $tudu->tuduId,
 768                    'uniqueid'   => $tudu->uniqueId,
 769                    'stepid'     => $stepId,
 770                    'type'       => $step['type'] == 2 ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE,
 771                    'prevstepid' => $step['prev'],
 772                    'nextstepid' => $step['next'],
 773                    'users'      => $recipients,
 774                    'ordernum'   => $orderNum ++
 775                );
 776            }
 777        }
 778*/
 779        foreach ($steps as $step) {
 780            if ($this->createStep($step)) {
 781                $recipients = $this->prepareRecipients($tudu->orgId, $tudu->uniqueId, $step['users']);
 782
 783                $processIndex = $step['type'] == Dao_Td_Tudu_Step::TYPE_EXAMINE ? 0 : null;
 784
 785                $this->addStepUsers($tudu->tuduId, $step['stepid'], $recipients, $processIndex);
 786            }
 787        }
 788
 789        $tudu->stepNum = count($steps);
 790
 791        return true;
 792    }
 793
 794    /**
 795     * 更新图度工作流后续步骤
 796     *
 797     * @param $tudu
 798     */
 799    public function updateFlowSteps(Tudu_Tudu_Storage_Tudu &$tudu)
 800    {
 801        /*if ($tudu->stepId != self::NODE_HEAD && $tudu->stepId != self::NODE_BREAK && !$tudu->isDraft()) {
 802            return false;
 803        }*/
 804
 805        $steps = $this->getSteps($tudu->tuduId)->toArray('stepid');
 806
 807        $currentStep = $tudu->stepId && false === strpos($tudu->stepId, '^') ? $steps[$tudu->stepId] : array_pop($steps);
 808
 809        $stepNum   = count($steps);
 810        $newSteps  = array();
 811
 812        // 当前为审批步骤
 813        if ($currentStep['type'] == Dao_Td_Tudu_Step::TYPE_EXAMINE) {
 814            $stepId   = $currentStep['stepid'];
 815            $reviewer = array();
 816            $pidx     = null;
 817
 818            // 获取步骤审批人
 819            $users = $this->getStepUsers($tudu->tuduId, $stepId);//var_dump($users);exit;
 820            foreach ($users as &$user) {
 821                if ($user['status'] == 2) {
 822                    continue;
 823                }
 824                $userInfo = explode(' ', $user['userinfo']);
 825
 826                if ($pidx == $user['processindex']) {
 827                    end($reviewer);
 828                    $last = key($reviewer);
 829                    $reviewer[$last][] = array('email' => $userInfo[0], 'truename' => $userInfo[1]);
 830                } else {
 831                    $reviewer[] = array(array('email' => $userInfo[0], 'truename' => $userInfo[1]));
 832                }
 833                $pidx = $user['processindex'];
 834            }
 835
 836            $tudu->reviewer = $reviewer;
 837            $tudu->stepId   = $stepId;
 838            var_dump($tudu->reviewer);
 839        // 其他
 840        } else {
 841
 842        }
 843        exit;
 844    }
 845
 846    /**
 847     * 创建图度步骤
 848     *
 849     * @param Tudu_Tudu_Storage_Tudu $tudu
 850     */
 851    public function createTuduSteps(Tudu_Tudu_Storage_Tudu &$tudu)
 852    {
 853        $steps = array();
 854
 855        $currentStepId = null;
 856        $orderNum      = 1;
 857        if ($tudu->reviewer) {
 858            $reviewers  = $tudu->reviewer;
 859            $users = array();
 860            $processIndex = 1;
 861
 862            foreach ($reviewers as $item) {
 863                foreach ($item as $reviewer) {
 864                    $users[] = array(
 865                        'email'        => $reviewer['email'],
 866                        'truename'     => $reviewer['truename'],
 867                        'processindex' => $processIndex,
 868                        'stepstatus'   => $processIndex == 1 ? 1 : 0
 869                    );
 870                }
 871
 872                $processIndex ++;
 873            }
 874
 875            $stepId  = Dao_Td_Tudu_Step::getStepId();
 876            $steps[$stepId] = array(
 877                'orgid' => $tudu->orgId,
 878                'tuduid' => $tudu->tuduId,
 879                'uniqueid' => $tudu->uniqueId,
 880                'stepid'   => $stepId,
 881                'type'     => Dao_Td_Tudu_Step::TYPE_EXAMINE,
 882                'prevstepid' => self::NODE_HEAD,
 883                'nextstepid' => self::NODE_END,
 884                'users'      => $users,
 885                'ordernum'   => $orderNum ++,
 886                'createtime' => time()
 887            );
 888
 889            $currentStepId = $stepId;
 890        }
 891
 892        if ($tudu->to) {
 893            $stepId  = Dao_Td_Tudu_Step::getStepId();
 894            $prevId  = self::NODE_HEAD;
 895
 896            if ($currentStepId) {
 897                $steps[$currentStepId]['nextstepid'] = $stepId;
 898                $prevId = $currentStepId;
 899            } else {
 900                $currentStepId = $stepId;
 901            }
 902
 903            if ($tudu->stepto) {
 904                $countTo = count($tudu->stepto);
 905                $i = 0;
 906                foreach ($tudu->stepto as $item) {
 907                    $i++;
 908                    $users = array();
 909                    foreach ($item as $to) {
 910                        $users[] = array(
 911                            'email'        => $to['email'],
 912                            'truename'     => $to['truename']
 913                        );
 914                    }
 915
 916                    $nextStepId = Dao_Td_Tudu_Step::getStepId();
 917
 918                    $steps[$stepId] = array(
 919                        'orgid' => $tudu->orgId,
 920                        'tuduid' => $tudu->tuduId,
 921                        'uniqueid' => $tudu->uniqueId,
 922                        'stepid'   => $stepId,
 923                        'type'     => $tudu->acceptMode ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE,
 924                        'prevstepid' => $prevId,
 925                        'nextstepid' => $countTo == $i ? self::NODE_END : $nextStepId,
 926                        'users'      => $users,
 927                        'ordernum'   => $orderNum ++,
 928                        'createtime' => time()
 929                    );
 930
 931                    $prevId = $stepId;
 932                    $stepId  = $nextStepId;
 933                }
 934            }
 935        }
 936
 937        foreach ($steps as $step) {
 938            if ($this->createStep($step)) {
 939                $recipients = $this->prepareRecipients($tudu->orgId, $tudu->uniqueId, $step['users']);
 940
 941                $processIndex = $step['type'] == Dao_Td_Tudu_Step::TYPE_EXAMINE ? 0 : null;
 942
 943                $this->addStepUsers($tudu->tuduId, $step['stepid'], $recipients, $processIndex);
 944            }
 945        }
 946
 947        $tudu->stepId  = $currentStepId;
 948        $tudu->stepNum = count($steps);
 949
 950        return true;
 951    }
 952
 953    /**
 954     * 更新图度后续步骤
 955     *
 956     * @param $tudu
 957     */
 958    public function updateTuduSteps(Tudu_Tudu_Storage_Tudu &$tudu, $cancelCurrent = true)
 959    {
 960        // 草稿
 961        if ($tudu->isDraft() || $tudu->stepId ==  self::NODE_HEAD || $tudu->stepId == self::NODE_BREAK) {
 962            $this->deleteAllSteps($tudu->tuduId);
 963            if ($tudu->flowId) {
 964                $this->onPrepareFlow($tudu);
 965                if ($tudu->isChange('to')) {
 966                    $manager = Tudu_Tudu_Manager::getInstance();
 967                    $to = $tudu->getAttribute('to');
 968                    $to = isset($to) ? Tudu_Tudu_Storage::formatReceiver($to) : null;
 969                    $manager->updateTudu($tudu->tuduId, array('to' => $to));
 970                }
 971                return $this->createFlowSteps($tudu);
 972            } else {
 973                return $this->createTuduSteps($tudu);
 974            }
 975        }
 976
 977        if ($tudu->flowId) {
 978            return true;
 979            //return $this->updateFlowSteps($tudu);
 980        }
 981
 982        $steps = $this->getSteps($tudu->tuduId)->toArray('stepid');
 983
 984        if ($tudu->stepId && false === strpos($tudu->stepId, '^')) {
 985            $currentStep = $steps[$tudu->stepId];
 986        } else {
 987            foreach ($steps as $step) {
 988                if ($step['stepstatus'] != 4) {
 989                    $currentStep = $step;
 990                    break;
 991                }
 992            }
 993        }
 994        //$currentStep = $tudu->stepId && false === strpos($tudu->stepId, '^') ? $steps[$tudu->stepId] : array_pop($steps);
 995
 996        // 当前为审批步骤
 997        $stepNum  = count($steps);
 998        $newSteps = array();
 999        $updatePrevStepId = null;
1000        if (isset($currentStep) && $currentStep['type'] == Dao_Td_Tudu_Step::TYPE_EXAMINE) {
1001
1002            $nextStepId = $currentStep['stepid'];
1003            $execStepId = Dao_Td_Tudu_Step::getStepId();
1004
1005            if ($tudu->reviewer) {
1006                $reviewers  = $tudu->reviewer;
1007                $users = array();
1008                $processIndex = 1;
1009
1010                foreach ($reviewers as $item) {
1011                    foreach ($item as $reviewer) {
1012                        $users[] = array(
1013                            'email'        => $reviewer['email'],
1014                            'truename'     => $reviewer['truename'],
1015                            'processindex' => $processIndex,
1016                            'stepstatus'   => $processIndex == 1 ? 1 : 0
1017                        );
1018                    }
1019
1020                    $processIndex ++;
1021                }
1022
1023                // 更新审批步骤审批人
1024                $this->updateStepUsers($tudu, $currentStep['stepid'], $users);
1025
1026                $updatePrevStepId = $prevStepId = $currentStep['stepid'];
1027
1028                $tudu->stepId = $currentStep['stepid'];
1029
1030            } else {
1031                // 审批步骤作废
1032                //$this->cancelStep($tudu, $currentStep['stepid']);
1033
1034                $this->updateStepUsers($tudu, $currentStep['stepid'], array());
1035
1036                $updatePrevStepId = $prevStepId = $currentStep['prevstepid'];
1037
1038                $tudu->stepId = $execStepId;
1039            }
1040
1041            if ($tudu->type != 'notice') {
1042                $updateNextStepId = $stepId = $execStepId;
1043                if ($tudu->stepto) {
1044                    $countTo = count($tudu->stepto);
1045                    $i = 0;
1046                    $orderNum = $currentStep['ordernum'];
1047                    foreach ($tudu->stepto as $item) {
1048                        $i++;
1049                        $users = array();
1050                        foreach ($item as $to) {
1051                            $users[] = array(
1052                                'email'        => $to['email'],
1053                                'truename'     => $to['truename']
1054                            );
1055                        }
1056
1057                        $nextStepId = Dao_Td_Tudu_Step::getStepId();
1058
1059                        $newSteps[] = array(
1060                            'orgid' => $tudu->orgId,
1061                            'tuduid' => $tudu->tuduId,
1062                            'uniqueid' => $tudu->uniqueId,
1063                            'stepid'   => $stepId,
1064                            'type'     => $tudu->acceptMode ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE,
1065                            'prevstepid' => $prevStepId,
1066                            'nextstepid' => $countTo == $i ? self::NODE_END : $nextStepId,
1067                            'users'      => $users,
1068                            'ordernum'   => ++$orderNum,
1069                            'createtime' => time()
1070                        );
1071
1072                        $prevStepId = $stepId;
1073                        $stepId  = $nextStepId;
1074                    }
1075                }
1076                $nextStepId = $stepId;
1077
1078            } else {
1079                $updateNextStepId = $nextStepId = self::NODE_END;
1080            }
1081
1082        // 其他
1083        } else {
1084            $isChangeTo = false;
1085
1086            // 比较图度执行人
1087            $stepUsers = $this->getTuduStepUsers($tudu->tuduId);
1088            $stepTo = array(); //修改前的步骤执行人
1089            $stepSize = 0; //修改前的步骤个数
1090            $tempStepId = null;
1091            foreach ($stepUsers as &$u) {
1092                if ($tudu->stepId == $u['stepid'] && $u['type'] == Dao_Td_Tudu_Step::TYPE_EXECUTE && $u['stepstatus'] != 4) {
1093                    if ($tempStepId != $u['stepid']) {
1094                        $stepSize++;
1095                    }
1096                    $tempStepId = $u['stepid'];
1097                    $info = explode(' ', $u['userinfo']);
1098                    $u['email']    = $info[0];
1099                    $u['truename'] = $info[1];
1100                    $stepTo[$u['email']] = $u;
1101                }
1102            }
1103
1104            $currentTo = array(); //当前修改后的执行人
1105            $currentStepSize = 0; //当前修改后步骤个数
1106            foreach ($tudu->stepto as $item) {
1107                foreach ($item as $to) {
1108                    $currentTo[$to['email']] = $to;
1109                }
1110                $currentStepSize++;
1111            }
1112            $src = array_keys($stepTo);
1113            $cur = array_keys($currentTo);
1114            $srcCount = count($src);
1115
1116            $isChangeTo = $stepSize != $currentStepSize || count($cur) != $srcCount || count(array_uintersect($src, $cur, "strcasecmp")) != $srcCount;
1117
1118            // 步骤作废
1119            if ($isChangeTo || $tudu->isChange('acceptmode')) {
1120                if ($cancelCurrent) {
1121                    $this->updateStep($tudu->tuduId, $currentStep['stepid'], array('status' => 4));
1122                    $updatePrevStepId = $prevStepId = $currentStep['prevstepid'];
1123                } else {
1124                    $updatePrevStepId = $prevStepId = $currentStep['stepid'];
1125                }
1126            } else {
1127                $updatePrevStepId = $prevStepId = $currentStep['stepid'];
1128                $tudu->stepId = $prevStepId;
1129            }
1130
1131            $updateNextStepId = $nextStepId = null;
1132            $prevId     = $prevStepId;
1133            $orderNum   = $currentStep['ordernum'];
1134            if ($tudu->reviewer) {
1135                // 前一步骤作废
1136                if ($cancelCurrent) {
1137                    $this->updateStep($tudu->tuduId, $prevId, array('status' => 4));
1138                }
1139
1140                $reviewers  = $tudu->reviewer;
1141                $users = array();
1142                $processIndex = 1;
1143
1144                foreach ($reviewers as $item) {
1145                    foreach ($item as $reviewer) {
1146                        $users[] = array(
1147                            'email'        => $reviewer['email'],
1148                            'truename'     => $reviewer['truename'],
1149                            'processindex' => $processIndex
1150                        );
1151                    }
1152
1153                    $processIndex ++;
1154                }
1155
1156                $stepId = Dao_Td_Tudu_Step::getStepId();
1157/*插入修改人
1158                $nextStepId = Dao_Td_Tudu_Step::getStepId();
1159
1160                $newSteps[$stepId] = array(
1161                    'orgid'  => $tudu->orgId,
1162                    'tuduid'     => $tudu->tuduId,
1163                    'stepid'     => $stepId,
1164                    'uniqueid'   => $tudu->uniqueId,
1165                    'prevstepid' => $prevId,
1166                    'nextstepid' => $nextStepId,
1167                    'type'       => Dao_Td_Tudu_Step::TYPE_EXECUTE,
1168                    'ordernum'   => ++$orderNum,
1169                    'createtime' => time(),
1170                    'users'      => array(0 => array('email' => $tudu->email, 'truename' => $tudu->poster))
1171                );
1172
1173                $prevId = $stepId;
1174                $stepId = $nextStepId;
1175*/
1176
1177                $newSteps[$stepId] = array(
1178                    'orgid'  => $tudu->orgId,
1179                    'tuduid'     => $tudu->tuduId,
1180                    'stepid'     => $stepId,
1181                    'uniqueid'   => $tudu->uniqueId,
1182                    'prevstepid' => $prevId,
1183                    'nextstepid' => '^end',
1184                    'type'       => Dao_Td_Tudu_Step::TYPE_EXAMINE,
1185                    'ordernum'   => ++$orderNum,
1186                    'createtime' => time(),
1187                    'users'      => $users
1188                );
1189
1190                $prevId = $stepId;
1191
1192                if (!$nextStepId) {
1193                    $updateNextStepId = $nextStepId = $stepId;
1194                }
1195            }
1196
1197            if ($isChangeTo || count($newSteps) || $tudu->isChange('acceptmode')) {
1198                $stepId = Dao_Td_Tudu_Step::getStepId();
1199                if (!$nextStepId) {
1200                    $updateNextStepId = $nextStepId = $stepId;
1201                }
1202
1203                if (isset($newSteps[$prevId])) {
1204                    $newSteps[$prevId]['nextstepid'] = $stepId;
1205                }
1206
1207                // 转发
1208                if ($tudu->action == 'forward') {
1209                    $newSteps[$stepId] = array(
1210                        'orgid'      => $tudu->orgId,
1211                        'tuduid'     => $tudu->tuduId,
1212                        'stepid'     => $stepId,
1213                        'uniqueid'   => $tudu->uniqueId,
1214                        'prevstepid' => $prevId,
1215                        'nextstepid' => '^end',
1216                        'type'       => $tudu->acceptMode ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE,
1217                        'ordernum'   => ++$orderNum,
1218                        'createtime' => time(),
1219                        'users'      => $tudu->to
1220                    );
1221
1222                } else {
1223                    // 修改
1224                    if ($tudu->stepto) {
1225                        $countTo = count($tudu->stepto);
1226                        $i = 0;
1227                        foreach ($tudu->stepto as $item) {
1228                            $i++;
1229                            $users = array();
1230                            foreach ($item as $to) {
1231                                $users[] = array(
1232                                    'email'        => $to['email'],
1233                                    'truename'     => $to['truename']
1234                                );
1235                            }
1236
1237                            $nextId = Dao_Td_Tudu_Step::getStepId();
1238
1239                            $newSteps[$stepId] = array(
1240                                'orgid' => $tudu->orgId,
1241                                'tuduid' => $tudu->tuduId,
1242                                'uniqueid' => $tudu->uniqueId,
1243                                'stepid'   => $stepId,
1244                                'type'     => $tudu->acceptMode ? Dao_Td_Tudu_Step::TYPE_CLAIM : Dao_Td_Tudu_Step::TYPE_EXECUTE,
1245                                'prevstepid' => $prevId,
1246                                'nextstepid' => $countTo == $i ? self::NODE_END : $nextId,
1247                                'users'      => $users,
1248                                'ordernum'   => ++$orderNum,
1249                                'createtime' => time()
1250                            );
1251
1252                            $prevId = $stepId;
1253                            $stepId  = $nextId;
1254                        }
1255                    }
1256                }
1257            }
1258
1259            if ($nextStepId) {
1260                $tudu->stepId = $updateNextStepId;
1261            }
1262        }
1263
1264        if (!empty($newSteps)) {
1265            // 移除后随未开始执行的步骤
1266            foreach ($steps as $step) {
1267                if ($step['ordernum'] > $currentStep['ordernum']) {
1268                    $this->deleteStep($tudu->tuduId, $step['stepid']);
1269                    $stepNum--;
1270                }
1271            }
1272
1273            foreach ($newSteps as $step) {
1274                if ($this->createStep($step)) {
1275                    $recipients = $this->prepareRecipients($tudu->orgId, $tudu->uniqueId, $step['users']);
1276
1277                    $processIndex = $step['type'] == Dao_Td_Tudu_Step::TYPE_EXAMINE ? 0 : null;
1278
1279                    $this->addStepUsers($tudu->tuduId, $step['stepid'], $recipients, $processIndex);
1280
1281                    $stepNum++;
1282                }
1283            }
1284
1285            $this->updateStep($tudu->tuduId, $updatePrevStepId, array('nextstepid' => $updateNextStepId));
1286        }
1287
1288        $tudu->stepNum = $stepNum;
1289        /*return $this->getDao('Dao_Td_Tudu_Tudu')->updateTudu($tudu->tuduId,
1290            array(
1291                'stepid'  => $tudu->stepId,
1292                'stepnum' => $stepNum
1293            )
1294        );*/
1295    }
1296
1297    /**
1298     * 更新图度后执行事件
1299     */
1300    public function postUpdate(Tudu_Tudu_Storage_Tudu &$tudu)
1301    {
1302        if ($this->updateTuduSteps($tudu)) {
1303            return $this->getDao('Dao_Td_Tudu_Tudu')->updateTudu($tudu->tuduId,
1304                array(
1305                    'stepid'  => $tudu->stepId,
1306                    'stepnum' => $tudu->stepNum
1307                )
1308            );
1309        }
1310
1311        return true;
1312    }
1313
1314    /**
1315     *
1316     */
1317    public function postCreate(Tudu_Tudu_Storage_Tudu &$tudu)
1318    {
1319        if ($tudu->flowId) {
1320            $ret = $this->createFlowSteps($tudu);
1321        } else {
1322            $ret = $this->createTuduSteps($tudu);
1323        }
1324        if ($ret) {
1325            return $this->getDao('Dao_Td_Tudu_Tudu')->updateTudu($tudu->tuduId,
1326                array(
1327                    'stepid'  => $tudu->stepId,
1328                    'stepnum' => $tudu->stepNum
1329                )
1330            );
1331        }
1332
1333        return true;
1334    }
1335
1336    /**
1337     * 申请审批
1338     *
1339     * @param Tudu_Tudu_Storage_Tudu $tudu
1340     */
1341    public function onApply(Tudu_Tudu_Storage_Tudu &$tudu)
1342    {
1343        if (!$tudu->reviewer) {
1344            return false;
1345        }
1346
1347        if ($this->updateTuduSteps($tudu)) {
1348            return $this->getDao('Dao_Td_Tudu_Tudu')->updateTudu($tudu->tuduId,
1349                array(
1350                    'stepid'  => $tudu->stepId,
1351                    'stepnum' => $tudu->stepNum
1352                )
1353            );
1354        }
1355    }
1356
1357    /**
1358     * 公告审批
1359     *
1360     * @param Tudu_Tudu_Storage_Tudu $tudu
1361     * @param boolean $isAgree
1362     */
1363    public function noticeReview(Tudu_Tudu_Storage_Tudu &$tudu, $isAgree)
1364    {
1365        // 同意
1366        if ($isAgree) {
1367            // 更新当前用户状态
1368            $this->updateStepUser($tudu->tuduId, $tudu->stepId, $tudu->uniqueId, array('status' => 2));
1369
1370            $steps = $this->getSteps($tudu->tuduId)->toArray('stepid');
1371
1372            if (!isset($steps[$tudu->stepId])) {
1373                return false;
1374            }
1375
1376            $step = $steps[$tudu->stepId];
1377
1378            $users = $this->getStepUsers($tudu->tuduId, $tudu->stepId);
1379
1380            // 当前审批人
1381            $nextUser  = false;
1382            $nextStep  = true;
1383            $nextIndex = null;
1384            $currIndex = null;
1385            $reviewer  = array();
1386            $tudu->sameReview = false;
1387            foreach ($users as $user) {
1388                if ($user['uniqueid'] == $tudu->uniqueId) {
1389                    $currentUser = $user;
1390                    $currIndex   = $user['processindex'];
1391
1392                    if (empty($reviewer)) {
1393                        $nextUser    = true;
1394                    }
1395                    continue ;
1396                }
1397
1398                // 还有用户在审核中,跳过流程
1399                if ($user['status'] == 1) {
1400                    list ($userName, $trueName) = explode(' ', $user['userinfo']);
1401                    $reviewer[] = array('email' => $userName, 'truename' => $trueName);
1402                    $nextStep = false;
1403                    $nextUser = false;
1404                    $tudu->sameReview = true;
1405                }
1406
1407                // 转到本步骤后续执行人
1408                if ($nextUser) {
1409                    if ($user['status'] == 2) {
1410                        continue ;
1411                    }
1412                    if (null === $nextIndex) {
1413                        $nextIndex = $user['processindex'];
1414                    }
1415
1416                    if (null !== $nextIndex && $user['processindex'] != $nextIndex) {
1417                        break ;
1418                    }
1419
1420                    if (null !== $currIndex && $currIndex == $user['processindex']) {
1421                        break ;
1422                    }
1423
1424                    list ($userName, $trueName) = expl…

Large files files are truncated, but you can click here to view the full file