PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Dao/Td/Flow/Flow.php

https://github.com/polokk/tudu-web-1
PHP | 592 lines | 370 code | 96 blank | 126 comment | 75 complexity | 824d49d8873920a8f4500bef0796f56a MD5 | raw file
  1. <?php
  2. /**
  3. * Flow Dao
  4. * 工作流
  5. *
  6. * LICENSE
  7. *
  8. * @category Dao
  9. * @package Dao_Td
  10. * @subpackage Tudu
  11. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  12. * @link http://www.tudu.com/
  13. * @author Oray-Yongfa
  14. * @version $Id: Flow.php 2809 2013-04-07 09:57:05Z cutecube $
  15. */
  16. /**
  17. * @category Dao
  18. * @package Dao_Td
  19. * @subpackage Tudu
  20. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  21. */
  22. class Dao_Td_Flow_Flow extends Oray_Dao_Abstract
  23. {
  24. /**
  25. * SELECT
  26. * flow_id AS flowid, org_id AS orgid, board_id AS boardid, unique_id AS uniqueid, subject, description,
  27. * avaliable, is_valid AS isvalid, cc, elapsed_time AS elapsedtime, content, steps, create_time AS createtime
  28. * FROM td_flow
  29. * WHERE flow_id = :flowid AND unique_id = :uniqueid
  30. * LIMIT 0, 1
  31. *
  32. * @param array $condition
  33. * @param array $filter
  34. * @return boolean|NULL
  35. */
  36. public function getFlow(array $condition, $filter = null)
  37. {
  38. if (empty($condition['flowid'])) {
  39. return false;
  40. }
  41. $table = 'td_flow';
  42. $columns = 'flow_id AS flowid, org_id AS orgid, board_id AS boardid, unique_id AS uniqueid, subject, description, class_id as classid, '
  43. . 'avaliable, is_valid AS isvalid, cc, elapsed_time AS elapsedtime, content, steps, create_time AS createtime';
  44. $where = array();
  45. $where[] = 'flow_id = ' . $this->_db->quote($condition['flowid']);
  46. if (isset($condition['uniqueid'])) {
  47. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  48. }
  49. if (!empty($filter) && array_key_exists('isvalid', $filter)) {
  50. if (null !== $filter['isvalid']) {
  51. $where[] = 'is_valid = ' . (int) $filter['isvalid'];
  52. }
  53. }
  54. $where = implode(' AND ', $where);
  55. if ($where) {
  56. $where = 'WHERE ' . $where;
  57. }
  58. $sql = "SELECT {$columns} FROM {$table} {$where} LIMIT 0, 1";
  59. $record = $this->_db->fetchRow($sql);
  60. if (!$record) {
  61. return null;
  62. }
  63. return Oray_Dao::record('Dao_Td_Flow_Record_Flow', $record);
  64. }
  65. /**
  66. * SELECT
  67. * f.flow_id AS flowid, f.org_id AS orgid, f.board_id AS boardid, f.unique_id AS uniqueid, f.subject, f.description,
  68. * f.avaliable, f.is_valid AS isvalid, f.create_time AS createtime, b.parent_board_id AS parentid
  69. * FROM td_flow f
  70. * LEFT JOIN td_board b ON f.board_id = b.board_id AND f.org_id = b.org_id
  71. * WHERE f.org_id = ? [AND f.board_id = ?]
  72. * ORDER BY ??
  73. * LIMIT ?
  74. *
  75. * @param array $condition
  76. * @param array $filter
  77. * @param mixed $sort
  78. * @param int $maxCount
  79. */
  80. public function getFlows(array $condition, $filter = null, $sort = null, $maxCount = null)
  81. {
  82. $table = 'td_flow f '
  83. . 'LEFT JOIN td_board b ON f.board_id = b.board_id AND f.org_id = b.org_id ';
  84. $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, '
  85. . 'f.avaliable, f.is_valid AS isvalid, f.create_time AS createtime, b.parent_board_id AS parentid';
  86. $recordClass = "Dao_Td_Flow_Record_Flow";
  87. $where = array();
  88. $order = array();
  89. $limit = '';
  90. if (isset($condition['orgid'])) {
  91. $where[] = 'f.org_id = ' . $this->_db->quote($condition['orgid']);
  92. }
  93. if (isset($condition['boardid'])) {
  94. $where[] = 'f.board_id = ' . $this->_db->quote($condition['boardid']);
  95. }
  96. if (isset($condition['keyword'])) {
  97. $keyword = $this->_db->quote("%{$condition['keyword']}%");
  98. $like = array();
  99. $like[] = "f.subject LIKE {$keyword}";
  100. $like[] = "f.description LIKE {$keyword}";
  101. $where[] = '(' . implode(' OR ', $like) . ')';
  102. }
  103. if (isset($condition['uniqueid'])) {
  104. $table .= 'LEFT JOIN td_flow_favor ff ON ff.flow_id = f.flow_id AND ff.unique_id = ' . $this->_db->quote($condition['uniqueid']);
  105. $columns .= ', ff.weight';
  106. }
  107. if (!empty($filter) && array_key_exists('isvalid', $filter)) {
  108. if (null !== $filter['isvalid']) {
  109. $where[] = 'f.is_valid = ' . (int) $filter['isvalid'];
  110. }
  111. } else {
  112. $where[] = 'is_valid = 1';
  113. }
  114. if (empty($where)) {
  115. return new Oray_Dao_Recordset();
  116. }
  117. $where = implode(' AND ', $where);
  118. if ($where) {
  119. $where = 'WHERE ' . $where;
  120. }
  121. $sort = $this->_formatSort($sort);
  122. foreach ($sort as $key => $val) {
  123. switch ($key) {
  124. case 'createtime':
  125. $key = 'f.create_time';
  126. break;
  127. default:
  128. continue 2;
  129. }
  130. $order[] = $key . ' ' . $val;
  131. }
  132. $order = implode(', ', $order);
  133. if ($order) {
  134. $order = 'ORDER BY ' . $order;
  135. }
  136. if (is_int($maxCount) && $maxCount > 0) {
  137. $limit = 'LIMIT ' . $maxCount;
  138. }
  139. $sql = "SELECT $columns FROM $table $where $order $limit";
  140. $records = $this->_db->fetchAll($sql);
  141. return new Oray_Dao_Recordset($records, $recordClass);
  142. }
  143. /**
  144. * 此方法目前没有使用
  145. *
  146. * @param array $condition
  147. * @param mixed $sort
  148. * @param int $page
  149. * @param int $pageSize
  150. */
  151. public function getFlowPage(array $condition = null, $sort = null, $page = null, $pageSize = null)
  152. {
  153. $table = 'td_flow';
  154. $columns = 'flow_id AS flowid, org_id AS orgid, board_id AS boardid, unique_id AS uniqueid, subject, description, class_id as classid, '
  155. . 'is_valid AS isvalid, create_time AS createtime';
  156. $where = array();
  157. $order = array();
  158. $recordClass = "Dao_Td_Flow_Record_Flow";
  159. if (isset($condition['orgid'])) {
  160. $where[] = 'org_id = ' . $this->_db->quote($condition['orgid']);
  161. }
  162. if (isset($condition['uniqueid'])) {
  163. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  164. }
  165. if (isset($condition['boardid'])) {
  166. $where[] = 'board_id = ' . $this->_db->quote($condition['boardid']);
  167. }
  168. if (isset($condition['isvalid'])) {
  169. $where[] = 'is_valid = ' . $this->_db->quote($condition['isvalid']);
  170. }
  171. $where = implode(' AND ', $where);
  172. if ($where) {
  173. $where = 'WHERE ' . $where;
  174. }
  175. $sort = $this->_formatSort($sort);
  176. foreach ($sort as $key => $val) {
  177. switch ($key) {
  178. case 'createtime':
  179. $key = 'create_time';
  180. break;
  181. default:
  182. continue 2;
  183. break;
  184. }
  185. $order[] = $key . ' ' . $val;
  186. }
  187. $order = implode(', ', $order);
  188. if ($order) {
  189. $order = 'ORDER BY ' . $order;
  190. }
  191. if (null === $pageSize && null === $page) {
  192. $sql = "SELECT $columns FROM $table $where $order";
  193. } else {
  194. // 使用默认的分页大小
  195. if (null === $pageSize) {
  196. $pageSize = self::$_defaultPageSize;
  197. }
  198. if ($page < 1) $page = 1;
  199. $sql = "SELECT $columns FROM $table $where $order LIMIT " . $pageSize * ($page - 1) . ", " . $pageSize;
  200. }
  201. $records = $this->_db->fetchAll($sql);
  202. return new Oray_Dao_Recordset($records, $recordClass);
  203. }
  204. /**
  205. *
  206. * @param string $flowId
  207. * @param array $filter
  208. * @return boolean
  209. */
  210. public function getFlowById($flowId, $filter = null)
  211. {
  212. return $this->getFlow(array('flowid' => $flowId), $filter);
  213. }
  214. /**
  215. * 工作流是否已使用
  216. *
  217. * @param string $flowId
  218. */
  219. public function isValidFlow($flowId, $orgId = null)
  220. {
  221. $sql = 'SELECT COUNT(0) FROM td_tudu WHERE flow_id = ' . $this->_db->quote($flowId);
  222. if (!empty($orgId)) {
  223. $sql .= ' AND org_id = ' . $this->_db->quote($orgId);
  224. }
  225. $count = (int) $this->_db->fetchOne($sql);
  226. return $count > 0;
  227. }
  228. /**
  229. * 创建工作流
  230. */
  231. public function createFlow(array $params)
  232. {
  233. if (empty($params['flowid'])
  234. || empty($params['orgid'])
  235. || empty($params['boardid'])
  236. || empty($params['uniqueid'])
  237. || !array_key_exists('subject', $params))
  238. {
  239. return false;
  240. }
  241. $table = 'td_flow';
  242. $bind = array(
  243. 'flow_id' => $params['flowid'],
  244. 'org_id' => $params['orgid'],
  245. 'board_id' => $params['boardid'],
  246. 'unique_id' => $params['uniqueid'],
  247. 'subject' => $params['subject']
  248. );
  249. if (!empty($params['description'])) {
  250. $bind['description'] = $params['description'];
  251. }
  252. if (!empty($params['avaliable'])) {
  253. $bind['avaliable'] = $params['avaliable'];
  254. }
  255. if (!empty($params['isvalid'])) {
  256. $bind['is_valid'] = (int) $params['isvalid'];
  257. }
  258. if (!empty($params['cc'])) {
  259. $bind['cc'] = $params['cc'];
  260. }
  261. if (!empty($params['elapsedtime'])) {
  262. $bind['elapsed_time'] = (int) $params['elapsedtime'];
  263. }
  264. if (!empty($params['content'])) {
  265. $bind['content'] = $params['content'];
  266. }
  267. if (isset($params['steps'])) {
  268. $bind['steps'] = $params['steps'];
  269. }
  270. if (!empty($params['createtime'])) {
  271. $bind['create_time'] = !empty($params['createtime']) ? (int) $params['createtime'] : time();
  272. }
  273. if (isset($params['classid'])) {
  274. $bind['class_id'] = $params['classid'];
  275. }
  276. try {
  277. $this->_db->insert($table, $bind);
  278. } catch (Zend_Db_Exception $e) {
  279. $this->_catchException($e, __METHOD__);
  280. return false;
  281. }
  282. return $params['flowid'];
  283. }
  284. /**
  285. * 更新工作流
  286. */
  287. public function updateFlow($flowId, array $params)
  288. {
  289. if (empty($flowId)) {
  290. return false;
  291. }
  292. $table = 'td_flow';
  293. $bind = array();
  294. $where = "flow_id = " . $this->_db->quote($flowId);
  295. if (isset($params['subject'])) {
  296. $bind['subject'] = $params['subject'];
  297. }
  298. if (array_key_exists('description', $params)) {
  299. $bind['description'] = $params['description'];
  300. }
  301. if (isset($params['avaliable'])) {
  302. $bind['avaliable'] = $params['avaliable'];
  303. }
  304. if (isset($params['isvalid'])) {
  305. $bind['is_valid'] = (int) $params['isvalid'];
  306. }
  307. if (array_key_exists('cc', $params)) {
  308. $bind['cc'] = $params['cc'];
  309. }
  310. if (isset($params['steps'])) {
  311. $bind['steps'] = $params['steps'];
  312. }
  313. if (array_key_exists('elapsedtime', $params)) {
  314. $bind['elapsed_time'] = $params['elapsedtime'];
  315. }
  316. if (isset($params['content'])) {
  317. $bind['content'] = $params['content'];
  318. }
  319. if (array_key_exists('classid', $params)) {
  320. $bind['class_id'] = $params['classid'];
  321. }
  322. if (!$bind) {
  323. return false;
  324. }
  325. try {
  326. $this->_db->update($table, $bind, $where);
  327. } catch (Zend_Db_Exception $e) {
  328. $this->_catchException($e, __METHOD__);
  329. return false;
  330. }
  331. return true;
  332. }
  333. /**
  334. * 删除工作流
  335. */
  336. public function deleteFlow($orgId, $flowId)
  337. {
  338. if (empty($orgId) || empty($flowId)) {
  339. return false;
  340. }
  341. $sql = 'DELETE FROM td_flow WHERE org_id = ' . $this->_db->quote($orgId) . ' AND '
  342. . 'flow_id = ' . $this->_db->quote($flowId);
  343. try {
  344. $this->_db->query($sql);
  345. } catch (Zend_Db_Exception $e) {
  346. $this->_catchException($e, __METHOD__);
  347. return false;
  348. }
  349. return true;
  350. }
  351. /**
  352. * 添加常用
  353. *
  354. * @param $orgId
  355. * @param $flowId
  356. * @param $uniqueId
  357. */
  358. public function addFavor($flowId, $uniqueId)
  359. {
  360. if (empty($flowId) || empty($uniqueId)) {
  361. return false;
  362. }
  363. $table = 'td_flow_favor';
  364. $bind = array(
  365. 'flow_id' => $flowId,
  366. 'unique_id' => $uniqueId,
  367. 'weight' => 1,
  368. 'update_time' => time()
  369. );
  370. try {
  371. $this->_db->insert($table, $bind);
  372. } catch (Zend_Db_Exception $e) {
  373. $this->_catchException($e, __METHOD__);
  374. return false;
  375. }
  376. return true;
  377. }
  378. /**
  379. * 添加常用
  380. *
  381. * @param $orgId
  382. * @param $flowId
  383. * @param $uniqueId
  384. */
  385. public function updateFavor($flowId, $uniqueId, array $params)
  386. {
  387. if (empty($flowId) || empty($uniqueId)) {
  388. return false;
  389. }
  390. $table = 'td_flow_favor';
  391. $bind = array();
  392. $where = 'unique_id = ' . $this->_db->quote($uniqueId) . ' '
  393. . 'flow_id = ' . $this->_db->quote($flowId);
  394. if (!empty($params['weight'])) {
  395. $bind['weight'] = $params['weight'];
  396. }
  397. if (!empty($params['updatetime'])) {
  398. $bind['update_time'] = $params['updatetime'];
  399. }
  400. try {
  401. $this->_db->insert($table, $bind, $where);
  402. } catch (Zend_Db_Exception $e) {
  403. $this->_catchException($e, __METHOD__);
  404. return false;
  405. }
  406. return true;
  407. }
  408. /**
  409. * 获取常用工作流信息
  410. *
  411. * @param $flowId
  412. * @param $uniqueId
  413. */
  414. public function getFavor($flowId, $uniqueId)
  415. {
  416. $sql = "SELECT unique_id AS uniqueid, flow_id AS flowid, weight, update_time AS updatetime "
  417. . "FROM td_flow_favor "
  418. . "WHERE flow_id = " . $this->_db->quote($flowId) . ' AND '
  419. . "unique_id = " . $this->_db->quote($flowId);
  420. try {
  421. $record = $this->_db->fetchRow($sql);
  422. if (!$record) {
  423. return null;
  424. }
  425. return $record;
  426. } catch (Zend_Db_Exception $e) {
  427. $this->_catchException($e, __METHOD__);
  428. return false;
  429. }
  430. }
  431. /**
  432. * 格式化地址
  433. *
  434. * 地址以 “邮箱[空格]姓名[换行]邮箱[空格]姓名”格式保存
  435. * 返回格式
  436. * array(
  437. * address1 => array(name1, userid1, domain1, address1, extend1),
  438. * address2 => array(name2, userid2, domain2, address2, extend2),
  439. * address3 => array(name3, userid3, domain3, address3, extend3)
  440. * )
  441. *
  442. * @param string $address
  443. * @param boolean $first 是否仅返回第一条记录,如果发件人仅有一个
  444. * @return array
  445. */
  446. public static function formatAddress($address, $first = false)
  447. {
  448. $ret = array();
  449. $pattern = "/(([\^]?[\w-\.\_]+)(?:@([^ ]+))?)? ([^\n]*)/";
  450. if ($first) {
  451. preg_match($pattern, $address, $matches);
  452. if ($matches) {
  453. $ret = array($matches[4], $matches[2], $matches[3], $matches[1]);
  454. }
  455. } else {
  456. preg_match_all($pattern, $address, $matches);
  457. for ($i = 0; $i < count($matches[0]); $i++) {
  458. if ($matches[1][$i]) {
  459. $ret[$matches[1][$i]] = array($matches[4][$i], $matches[2][$i], $matches[3][$i], $matches[1][$i]);
  460. } else {
  461. $ret[] = array($matches[4][$i], $matches[2][$i], $matches[3][$i], $matches[1][$i]);
  462. }
  463. }
  464. }
  465. return $ret;
  466. }
  467. /**
  468. * 格式化参与人员群组
  469. *
  470. * return
  471. * array(groupoid => name ...)
  472. *
  473. * @param string $str
  474. * @return array
  475. */
  476. public static function formatAvaliable($str)
  477. {
  478. $ret = array();
  479. if (!$str) return $ret;
  480. return explode("\n", trim($str, "\n"));
  481. }
  482. /**
  483. * 生成工作流步骤ID
  484. */
  485. public static function getStepId()
  486. {
  487. return 'F-' . base_convert(strrev(time()) . rand(0, 999), 10, 32);
  488. }
  489. /**
  490. * 获取工作流ID
  491. */
  492. public static function getFlowId()
  493. {
  494. return base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
  495. }
  496. }