tudu-web-1 /library/Dao/Td/Flow/Flow.php

Language PHP Lines 592
MD5 Hash 824d49d8873920a8f4500bef0796f56a
Repository https://github.com/polokk/tudu-web-1.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
<?php
/**
 * Flow Dao
 * 工作流
 *
 * LICENSE
 *
 * @category   Dao
 * @package    Dao_Td
 * @subpackage Tudu
 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
 * @link       http://www.tudu.com/
 * @author     Oray-Yongfa
 * @version    $Id: Flow.php 2809 2013-04-07 09:57:05Z cutecube $
 */

/**
 * @category   Dao
 * @package    Dao_Td
 * @subpackage Tudu
 * @copyright  Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
 */
class Dao_Td_Flow_Flow extends Oray_Dao_Abstract
{
    /**
     * SELECT
     * flow_id AS flowid, org_id AS orgid, board_id AS boardid, unique_id AS uniqueid, subject, description,
     * avaliable, is_valid AS isvalid, cc, elapsed_time AS elapsedtime, content, steps, create_time AS createtime
     * FROM td_flow
     * WHERE flow_id = :flowid AND unique_id = :uniqueid
     * LIMIT 0, 1
     *
     * @param array $condition
     * @param array $filter
     * @return boolean|NULL
     */
    public function getFlow(array $condition, $filter = null)
    {
        if (empty($condition['flowid'])) {
            return false;
        }

        $table   = 'td_flow';
        $columns = 'flow_id AS flowid, org_id AS orgid, board_id AS boardid, unique_id AS uniqueid, subject, description, class_id as classid, '
                 . 'avaliable, is_valid AS isvalid, cc, elapsed_time AS elapsedtime, content, steps, create_time AS createtime';
        $where   = array();

        $where[] = 'flow_id = ' . $this->_db->quote($condition['flowid']);

        if (isset($condition['uniqueid'])) {
            $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
        }

        if (!empty($filter) && array_key_exists('isvalid', $filter)) {
            if (null !== $filter['isvalid']) {
                $where[] = 'is_valid = ' . (int) $filter['isvalid'];
            }
        }

        $where = implode(' AND ', $where);
        if ($where) {
            $where = 'WHERE ' . $where;
        }

        $sql = "SELECT {$columns} FROM {$table} {$where} LIMIT 0, 1";

        $record = $this->_db->fetchRow($sql);

        if (!$record) {
            return null;
        }

        return Oray_Dao::record('Dao_Td_Flow_Record_Flow', $record);
    }

    /**
     * SELECT
     * f.flow_id AS flowid, f.org_id AS orgid, f.board_id AS boardid, f.unique_id AS uniqueid, f.subject, f.description,
     * f.avaliable, f.is_valid AS isvalid, f.create_time AS createtime, b.parent_board_id AS parentid
     * FROM td_flow f
     * LEFT JOIN td_board b ON f.board_id = b.board_id AND f.org_id = b.org_id
     * WHERE f.org_id = ? [AND f.board_id = ?]
     * ORDER BY ??
     * LIMIT ?
     *
     * @param array $condition
     * @param array $filter
     * @param mixed $sort
     * @param int   $maxCount
     */
    public function getFlows(array $condition, $filter = null, $sort = null, $maxCount = null)
    {
        $table   = 'td_flow f '
                 . 'LEFT JOIN td_board b ON f.board_id = b.board_id AND f.org_id = b.org_id ';
        $columns = 'f.flow_id AS flowid, f.org_id AS orgid, f.board_id AS boardid, f.unique_id AS uniqueid, f.subject, f.description, class_id as classid, '
                 . 'f.avaliable, f.is_valid AS isvalid, f.create_time AS createtime, b.parent_board_id AS parentid';
        $recordClass = "Dao_Td_Flow_Record_Flow";
        $where   = array();
        $order   = array();
        $limit   = '';

        if (isset($condition['orgid'])) {
            $where[] = 'f.org_id = ' . $this->_db->quote($condition['orgid']);
        }

        if (isset($condition['boardid'])) {
            $where[] = 'f.board_id = ' . $this->_db->quote($condition['boardid']);
        }

        if (isset($condition['keyword'])) {
            $keyword = $this->_db->quote("%{$condition['keyword']}%");
            $like = array();
            $like[] = "f.subject LIKE {$keyword}";
            $like[] = "f.description LIKE {$keyword}";

            $where[] = '(' . implode(' OR ', $like) . ')';
        }

        if (isset($condition['uniqueid'])) {
            $table   .= 'LEFT JOIN td_flow_favor ff ON ff.flow_id = f.flow_id AND ff.unique_id = ' . $this->_db->quote($condition['uniqueid']);
            $columns .= ', ff.weight';
        }

        if (!empty($filter) && array_key_exists('isvalid', $filter)) {
            if (null !== $filter['isvalid']) {
                $where[] = 'f.is_valid = ' . (int) $filter['isvalid'];
            }
        } else {
            $where[] = 'is_valid = 1';
        }

        if (empty($where)) {
            return new Oray_Dao_Recordset();
        }

        $where = implode(' AND ', $where);
        if ($where) {
            $where = 'WHERE ' . $where;
        }

        $sort = $this->_formatSort($sort);
        foreach ($sort as $key => $val) {
            switch ($key) {
                case 'createtime':
                    $key = 'f.create_time';
                    break;
                default:
                    continue 2;
            }
            $order[] = $key . ' ' . $val;
        }

        $order = implode(', ', $order);
        if ($order) {
            $order = 'ORDER BY ' . $order;
        }

        if (is_int($maxCount) && $maxCount > 0) {
            $limit = 'LIMIT ' . $maxCount;
        }

        $sql = "SELECT $columns FROM $table $where $order $limit";

        $records = $this->_db->fetchAll($sql);

        return new Oray_Dao_Recordset($records, $recordClass);
    }

    /**
     * 此方法目前没有使用
     *
     * @param array $condition
     * @param mixed $sort
     * @param int   $page
     * @param int   $pageSize
     */
    public function getFlowPage(array $condition = null, $sort = null, $page = null, $pageSize = null)
    {
        $table   = 'td_flow';
        $columns = 'flow_id AS flowid, org_id AS orgid, board_id AS boardid, unique_id AS uniqueid, subject, description, class_id as classid, '
                 . 'is_valid AS isvalid, create_time AS createtime';
        $where   = array();
        $order   = array();
        $recordClass = "Dao_Td_Flow_Record_Flow";

        if (isset($condition['orgid'])) {
            $where[] = 'org_id = ' . $this->_db->quote($condition['orgid']);
        }

        if (isset($condition['uniqueid'])) {
            $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
        }

        if (isset($condition['boardid'])) {
            $where[] = 'board_id = ' . $this->_db->quote($condition['boardid']);
        }

        if (isset($condition['isvalid'])) {
            $where[] = 'is_valid = ' . $this->_db->quote($condition['isvalid']);
        }

        $where = implode(' AND ', $where);
        if ($where) {
            $where = 'WHERE ' . $where;
        }

        $sort = $this->_formatSort($sort);
        foreach ($sort as $key => $val) {
            switch ($key) {
                case 'createtime':
                    $key = 'create_time';
                    break;
                default:
                    continue 2;
                    break;
            }
            $order[] = $key . ' ' . $val;
        }

        $order = implode(', ', $order);
        if ($order) {
            $order = 'ORDER BY ' . $order;
        }

        if (null === $pageSize && null === $page) {
            $sql = "SELECT $columns FROM $table $where $order";
        } else {

            // 使用默认的分页大小
            if (null === $pageSize) {
                $pageSize = self::$_defaultPageSize;
            }

            if ($page < 1) $page = 1;

            $sql = "SELECT $columns FROM $table $where $order LIMIT " . $pageSize * ($page - 1) . ", " . $pageSize;
        }

        $records = $this->_db->fetchAll($sql);

        return new Oray_Dao_Recordset($records, $recordClass);
    }

    /**
     *
     * @param string $flowId
     * @param array  $filter
     * @return boolean
     */
    public function getFlowById($flowId, $filter = null)
    {
        return $this->getFlow(array('flowid' => $flowId), $filter);
    }

    /**
     * 工作流是否已使用
     *
     * @param string $flowId
     */
    public function isValidFlow($flowId, $orgId = null)
    {
        $sql = 'SELECT COUNT(0) FROM td_tudu WHERE flow_id = ' . $this->_db->quote($flowId);

        if (!empty($orgId)) {
            $sql .= ' AND org_id = ' . $this->_db->quote($orgId);
        }

        $count = (int) $this->_db->fetchOne($sql);
        return $count > 0;
    }

    /**
     * 创建工作流
     */
    public function createFlow(array $params)
    {
        if (empty($params['flowid'])
            || empty($params['orgid'])
            || empty($params['boardid'])
            || empty($params['uniqueid'])
            || !array_key_exists('subject', $params))
        {
            return false;
        }

        $table = 'td_flow';
        $bind = array(
            'flow_id'   => $params['flowid'],
            'org_id'    => $params['orgid'],
            'board_id'  => $params['boardid'],
            'unique_id' => $params['uniqueid'],
            'subject'   => $params['subject']
        );

        if (!empty($params['description'])) {
            $bind['description'] = $params['description'];
        }

        if (!empty($params['avaliable'])) {
            $bind['avaliable'] = $params['avaliable'];
        }

        if (!empty($params['isvalid'])) {
            $bind['is_valid'] = (int) $params['isvalid'];
        }

        if (!empty($params['cc'])) {
            $bind['cc'] = $params['cc'];
        }

        if (!empty($params['elapsedtime'])) {
            $bind['elapsed_time'] = (int) $params['elapsedtime'];
        }

        if (!empty($params['content'])) {
            $bind['content'] = $params['content'];
        }

        if (isset($params['steps'])) {
            $bind['steps'] = $params['steps'];
        }

        if (!empty($params['createtime'])) {
            $bind['create_time'] = !empty($params['createtime']) ? (int) $params['createtime'] : time();
        }

        if (isset($params['classid'])) {
            $bind['class_id'] = $params['classid'];
        }

        try {
            $this->_db->insert($table, $bind);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return $params['flowid'];
    }

    /**
     * 更新工作流
     */
    public function updateFlow($flowId, array $params)
    {
        if (empty($flowId)) {
            return false;
        }

        $table = 'td_flow';
        $bind  = array();
        $where = "flow_id = " . $this->_db->quote($flowId);

        if (isset($params['subject'])) {
            $bind['subject'] = $params['subject'];
        }

        if (array_key_exists('description', $params)) {
            $bind['description'] = $params['description'];
        }

        if (isset($params['avaliable'])) {
            $bind['avaliable'] = $params['avaliable'];
        }

        if (isset($params['isvalid'])) {
            $bind['is_valid'] = (int) $params['isvalid'];
        }

        if (array_key_exists('cc', $params)) {
            $bind['cc'] = $params['cc'];
        }

        if (isset($params['steps'])) {
            $bind['steps'] = $params['steps'];
        }

        if (array_key_exists('elapsedtime', $params)) {
            $bind['elapsed_time'] = $params['elapsedtime'];
        }

        if (isset($params['content'])) {
            $bind['content'] = $params['content'];
        }

        if (array_key_exists('classid', $params)) {
            $bind['class_id'] = $params['classid'];
        }

        if (!$bind) {
            return false;
        }

        try {
            $this->_db->update($table, $bind, $where);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * 删除工作流
     */
    public function deleteFlow($orgId, $flowId)
    {
        if (empty($orgId) || empty($flowId)) {
            return false;
        }

        $sql = 'DELETE FROM td_flow WHERE org_id = ' . $this->_db->quote($orgId) . ' AND '
             . 'flow_id = ' . $this->_db->quote($flowId);

        try {
            $this->_db->query($sql);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * 添加常用
     *
     * @param $orgId
     * @param $flowId
     * @param $uniqueId
     */
    public function addFavor($flowId, $uniqueId)
    {
        if (empty($flowId) || empty($uniqueId)) {
            return false;
        }

        $table = 'td_flow_favor';
        $bind  = array(
            'flow_id' => $flowId,
            'unique_id' => $uniqueId,
            'weight' => 1,
            'update_time' => time()
        );

        try {
            $this->_db->insert($table, $bind);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * 添加常用
     *
     * @param $orgId
     * @param $flowId
     * @param $uniqueId
     */
    public function updateFavor($flowId, $uniqueId, array $params)
    {
        if (empty($flowId) || empty($uniqueId)) {
            return false;
        }

        $table = 'td_flow_favor';
        $bind  = array();
        $where = 'unique_id = ' . $this->_db->quote($uniqueId) . ' '
               . 'flow_id = ' . $this->_db->quote($flowId);

        if (!empty($params['weight'])) {
            $bind['weight'] = $params['weight'];
        }

        if (!empty($params['updatetime'])) {
            $bind['update_time'] = $params['updatetime'];
        }

        try {
            $this->_db->insert($table, $bind, $where);
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }

        return true;
    }

    /**
     * 获取常用工作流信息
     *
     * @param $flowId
     * @param $uniqueId
     */
    public function getFavor($flowId, $uniqueId)
    {
        $sql = "SELECT unique_id AS uniqueid, flow_id AS flowid, weight, update_time AS updatetime "
             . "FROM td_flow_favor "
             . "WHERE flow_id = " . $this->_db->quote($flowId) . ' AND '
             . "unique_id = " . $this->_db->quote($flowId);

        try {
            $record = $this->_db->fetchRow($sql);

            if (!$record) {
                return null;
            }

            return $record;
        } catch (Zend_Db_Exception $e) {
            $this->_catchException($e, __METHOD__);
            return false;
        }
    }

    /**
     * 格式化地址
     *
     * 地址以 “邮箱[空格]姓名[换行]邮箱[空格]姓名”格式保存
     * 返回格式
     * array(
     *     address1 => array(name1, userid1, domain1, address1, extend1),
     *     address2 => array(name2, userid2, domain2, address2, extend2),
     *     address3 => array(name3, userid3, domain3, address3, extend3)
     * )
     *
     * @param string $address
     * @param boolean $first 是否仅返回第一条记录,如果发件人仅有一个
     * @return array
     */
    public static function formatAddress($address, $first = false)
    {
        $ret = array();

        $pattern = "/(([\^]?[\w-\.\_]+)(?:@([^ ]+))?)? ([^\n]*)/";

        if ($first) {
            preg_match($pattern, $address, $matches);
            if ($matches) {
                $ret = array($matches[4], $matches[2], $matches[3], $matches[1]);
            }
        } else {
            preg_match_all($pattern, $address, $matches);
            for ($i = 0; $i < count($matches[0]); $i++) {
                if ($matches[1][$i]) {
                    $ret[$matches[1][$i]] = array($matches[4][$i], $matches[2][$i], $matches[3][$i], $matches[1][$i]);
                } else {
                    $ret[] = array($matches[4][$i], $matches[2][$i], $matches[3][$i], $matches[1][$i]);
                }
            }
        }
        return $ret;
    }

    /**
     * 格式化参与人员群组
     *
     * return
     * array(groupoid => name ...)
     *
     * @param string $str
     * @return array
     */
    public static function formatAvaliable($str)
    {
        $ret = array();

        if (!$str) return $ret;

        return explode("\n", trim($str, "\n"));
    }

    /**
     * 生成工作流步骤ID
     */
    public static function getStepId()
    {
        return 'F-' . base_convert(strrev(time()) . rand(0, 999), 10, 32);
    }

    /**
     * 获取工作流ID
     */
    public static function getFlowId()
    {
        return base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
    }
}
Back to Top