forked from OrayDev/tudu-web
/
Flow.php
195 lines (164 loc) · 4.36 KB
/
Flow.php
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
<?php
/**
* Tudu 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.oray.com/
* @version $Id: Cycle.php 1353 2011-12-06 10:18:49Z cutecube $
*/
/**
* @see Oray_Dao_Abstract
*/
require_once 'Oray/Dao/Abstract.php';
/**
* @category Dao
* @package Dao_Td
* @subpackage Tudu
* @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
*/
class Dao_Td_Tudu_Flow extends Oray_Dao_Abstract
{
/**
*
* @var int
*/
const STEP_TYPE_EXECUTE = 0;
const STEP_TYPE_EXAMINE = 1;
const STEP_TYPE_CLAIM = 2;
/**
*
* @param array $condition
* @param array $filter
* @return Dao_Td_Record_Flow
*/
public function getFlow(array $condition, $filter = null)
{
$table = 'td_tudu_flow';
$columns = 'org_id as orgid, tudu_id as tuduid, flow_id as flowid, current_step_id as currentstepid, steps';
$where = array();
$bind = array();
if (isset($condition['tuduid'])) {
$where[] = 'tudu_id = :tuduid';
$bind['tuduid'] = $condition['tuduid'];
}
if (empty($where)) {
return null;
}
$where = implode(' AND ', $where);
$sql = "SELECT {$columns} FROM {$table} WHERE {$where} LIMIT 1";
try {
$record = $this->_db->fetchRow($sql, $bind);
if (!$record) {
return null;
}
return Oray_Dao::record('Dao_Td_Tudu_Record_Flow', $record);
} catch (Zend_Db_Exception $e) {
$this->_catchException($e, __METHOD__);
return null;
}
}
/**
*
* @param array $params
* @return boolean
*/
public function createFlow(array $params)
{
if (empty($params['orgid'])
|| empty($params['tuduid']))
{
return false;
}
$table = 'td_tudu_flow';
$bind = array(
'org_id' => $params['orgid'],
'tudu_id' => $params['tuduid']
);
if (!empty($params['flowid'])) {
$bind['flow_id'] = $params['flowid'];
}
if (!empty($params['steps']) && is_array($params['steps'])) {
$bind['steps'] = self::formatSteps($params['steps']);
}
if (!empty($params['currentstepid'])) {
$bind['current_step_id'] = $params['currentstepid'];
}
if (isset($params['stepnum'])) {
$bind['step_num'] = (int) $params['stepnum'];
}
try {
$this->_db->insert($table, $bind);
} catch (Zend_Db_Exception $e) {
$this->_catchException($e, __METHOD__);
return false;
}
return true;
}
/**
*
* @param array $params
* @return boolean
*/
public function updateFlow($tuduId, array $params)
{
$table = 'td_tudu_flow';
$bind = array();
$where = 'tudu_id = ' . $this->_db->quote($tuduId);
if (!empty($params['steps']) && is_array($params['steps'])) {
$bind['steps'] = self::formatSteps($params['steps']);
}
if (!empty($params['currentstepid'])) {
$bind['current_step_id'] = $params['currentstepid'];
}
if (isset($params['stepnum'])) {
$bind['step_num'] = (int) $params['stepnum'];
}
if (empty($bind)) {
return false;
}
try {
$this->_db->update($table, $bind, $where);
} catch (Zend_Db_Exception $e) {
$this->_catchException($e, __METHOD__);
return false;
}
return true;
}
/**
*
* @param string $steps
* @return array
*/
public static function parseSteps($steps)
{
$ret = @json_decode($steps, true);
if (false == $ret) {
return array();
}
return $ret;
}
/**
*
* @param array $steps
* @return string
*/
public static function formatSteps(array $steps)
{
$ret = json_encode($steps);
return $ret;
}
/**
*
* @param string $tuduId
*/
public static function getStepId($tuduId)
{
return 'ST-' . base_convert(strrev(time()) . rand(0, 999), 10, 32);
}
}