PageRenderTime 43ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Dao/Td/Tudu/Tudu.php

https://github.com/polokk/tudu-web-1
PHP | 2328 lines | 1572 code | 332 blank | 424 comment | 323 complexity | 82f155822ecd9b08f290aa459db9b898 MD5 | raw file
  1. <?php
  2. /**
  3. * Tudu Dao
  4. *
  5. * LICENSE
  6. *
  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.oray.com/
  13. * @version $Id: Tudu.php 2809 2013-04-07 09:57:05Z cutecube $
  14. */
  15. /**
  16. * @category Dao
  17. * @package Dao_Td
  18. * @subpackage Tudu
  19. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  20. */
  21. class Dao_Td_Tudu_Tudu extends Oray_Dao_Abstract
  22. {
  23. const STATUS_UNSTART = 0; // 未开始
  24. const STATUS_DOING = 1; // 进行中
  25. const STATUS_DONE = 2; // 已完成
  26. const STATUS_REJECT = 3; // 已拒绝
  27. const STATUS_CANCEL = 4; // 已取消
  28. const SPECIAL_CYCLE = 1;
  29. const SPECIAL_VOTE = 2; // 投票
  30. const TYPE_TASK = 'task';
  31. const TYPE_NOTICE = 'notice';
  32. const TYPE_DISCUSS = 'discuss';
  33. const TYPE_MEETING = 'meeting';
  34. /**
  35. * 用户角色
  36. * @var string
  37. */
  38. const ROLE_ACCEPTER = 'to'; // 接收人
  39. const ROLE_SENDER = 'from'; // 发送人
  40. const ROLE_CC = 'cc'; // 被抄送
  41. /**
  42. * Initialize object
  43. *
  44. * Called from {@link __construct()} as final step of object instantiation.
  45. *
  46. * @return void
  47. */
  48. public function init()
  49. {}
  50. /**
  51. * Get record
  52. *
  53. * SELECT t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.cycle_id AS cycleid,
  54. * t.type, t.subject, t.from, t.to, t.cc, t.priority, t.privacy,
  55. * p.post_id AS postid, p.content, p.poster_info AS posterinfo, p.attach_num AS attachnum,
  56. * t.last_post_time AS lastposttime, t.last_poster AS lastposter,
  57. * t.view_num AS viewnum, t.reply_num AS replynum, t.log_num AS lognum,
  58. * t.percent, t.score, t.status, t.special,
  59. * t.start_time AS starttime, t.end_time AS endtime,
  60. * t.total_time AS totaltime, t.elapsed_time AS elapsedtime,
  61. * t.accept_time AS accepttime, t.create_time AS create_time,
  62. * tu.unique_id AS uniqueid, TRIM(LEADING ',' FROM tu.labels) labels,
  63. * tu.is_read AS isread, tu.is_forward AS isforward,
  64. * t.is_draft AS isdraft, t.is_done AS isdone
  65. * FROM td_tudu t
  66. * LEFT JOIN td_post p ON p.tudu_id = t.tudu_id AND p.is_first = 1
  67. * LEFT JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id AND tu.unique_id = ?
  68. * WHERE t.tudu_id = ?
  69. *
  70. * @param array $condition
  71. * @param array $filter
  72. * @return Dao_Td_Tudu_Record_Tudu
  73. */
  74. public function getTudu(array $condition, $filter = null)
  75. {
  76. if (empty($condition['tuduid'])
  77. || empty($condition['uniqueid'])) {
  78. return null;
  79. }
  80. $table = "td_tudu t "
  81. . "LEFT JOIN td_post p ON p.tudu_id = t.tudu_id AND p.is_first = 1 "
  82. . "LEFT JOIN td_tudu_group g ON g.tudu_id = p.tudu_id "
  83. . "LEFT JOIN td_class c ON t.class_id = c.class_id AND t.org_id = c.org_id AND t.board_id = c.board_id "
  84. . "LEFT JOIN td_flow f ON t.flow_id = f.flow_id AND t.org_id = f.org_id AND t.board_id = f.board_id "
  85. . "LEFT JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id AND tu.unique_id = "
  86. . $this->_db->quote($condition['uniqueid']) . " "
  87. . "LEFT JOIN td_tudu_flow fl ON fl.tudu_id = t.tudu_id";
  88. $columns = "t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.cycle_id AS cycleid, t.prev_tudu_id AS prevtuduid, "
  89. . "t.type, t.subject, t.from, t.to, t.cc, t.bcc, t.priority, t.privacy, t.password, t.is_auth AS isauth, fl.current_step_id AS stepid, "
  90. . "p.post_id AS postid, p.content, p.header, p.poster_info AS posterinfo, p.attach_num AS attachnum, t.app_id AS appid, "
  91. . "t.last_post_time AS lastposttime, t.last_poster AS lastposter, t.last_forward AS lastforward, "
  92. . "t.view_num AS viewnum, t.reply_num AS replynum, t.log_num AS lognum, t.cycle_num AS cyclenum, "
  93. . "fl.step_num as stepnum, t.percent, t.score, t.status, t.special, t.notify_all AS notifyall, t.accep_mode AS accepmode,"
  94. . "t.start_time AS starttime, t.end_time AS endtime, t.need_confirm AS needconfirm, "
  95. . "t.total_time AS totaltime, t.elapsed_time AS elapsedtime, "
  96. . "t.accept_time AS accepttime, t.create_time AS createtime, "
  97. . "tu.unique_id AS uniqueid, TRIM(LEADING ',' FROM tu.labels) labels, "
  98. . "tu.is_read AS isread, tu.is_forward AS isforward, tu.mark, "
  99. . "tu.role, tu.percent AS selfpercent, tu.forward_info AS forwardinfo, "
  100. . "tu.tudu_status AS selftudustatus, tu.accept_time AS selfaccepttime, tu.auth_code AS authcode, "
  101. . "t.is_draft AS isdraft, t.is_done AS isdone, t.is_top AS istop, p.is_send AS issend, "
  102. . "t.class_id AS classid, t.flow_id AS flowid, c.class_name AS classname, "
  103. . "g.type AS nodetype, g.parent_tudu_id AS parentid, g.root_tudu_id AS rootid";
  104. $where = array();
  105. $where[] = 't.tudu_id = ' . $this->_db->quote($condition['tuduid']);
  106. if (isset($filter['orgid'])) {
  107. $where[] = 't.org_id = ' . $this->_db->quote($filter['orgid']);
  108. }
  109. // WHERE
  110. $where = implode(' AND ', $where);
  111. $sql = "SELECT {$columns} FROM {$table} WHERE {$where} LIMIT 0, 1";
  112. $record = $this->_db->fetchRow($sql);
  113. if (!$record) {
  114. return null;
  115. }
  116. return Oray_Dao::record('Dao_Td_Tudu_Record_Tudu', $record);
  117. }
  118. /**
  119. * Get records
  120. *
  121. * SQL here..
  122. *
  123. * @param array $condition
  124. * @param array $filter
  125. * @param array $sort
  126. * @param int $maxCount
  127. * @return Oray_Dao_Recordset
  128. */
  129. public function getTudus(array $condition, $filter = null, $sort = null, $maxCount = null)
  130. {
  131. $table = "td_tudu t LEFT JOIN td_tudu_group g ON t.tudu_id = g.tudu_id";
  132. $columns = 't.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, `from`, `to`, is_draft AS isdraft, subject, '
  133. . 'g.type AS nodetype, g.parent_tudu_id AS parentid';
  134. $where = array();
  135. $order = array();
  136. $limit = '';
  137. if (!empty($condition['tuduids']) && is_array($condition['tuduids'])) {
  138. $where[] = 't.tudu_id IN(' . implode(',', array_map(array($this->_db, 'quote'), $condition['tuduids'])) . ')';
  139. }
  140. if (!empty($condition['parentid'])) {
  141. $where[] = 'g.parent_tudu_id = ' . $this->_db->quote($condition['parentid']);
  142. if (!empty($condition['uniqueid'])) {
  143. $where[] = 'g.unique_id = ' . $this->_db->quote($condition['uniqueid']);
  144. }
  145. }
  146. if (empty($where)) {
  147. return new Oray_Dao_Recordset();
  148. }
  149. // WHERE
  150. $where = implode(' AND ', $where);
  151. if ($where) {
  152. $where = 'WHERE ' . $where;
  153. }
  154. $sort = $this->_formatSort($sort);
  155. foreach ($sort as $key => $val) {
  156. switch ($key) {
  157. default:
  158. continue 2;
  159. }
  160. $order[] = $key . ' ' . $val;
  161. }
  162. // ORDER
  163. $order = implode(', ', $order);
  164. if ($order) {
  165. $order = 'ORDER BY ' . $order;
  166. }
  167. // LIMIT
  168. if (is_int($maxCount) && $maxCount > 0) {
  169. $limit = 'LIMIT ' . $maxCount;
  170. }
  171. $sql = "SELECT $columns FROM $table $where $order $limit";
  172. $records = $this->_db->fetchAll($sql);
  173. return new Oray_Dao_Recordset($records, 'Dao_Td_Tudu_Record_Tudus');
  174. }
  175. /**
  176. *
  177. * @param $condition
  178. * @param $filter
  179. */
  180. public function getUserTudus(array $condition, $filter = null, $offset = null, $count = null)
  181. {
  182. $table = "td_tudu t "
  183. . "LEFT JOIN td_tudu_group g ON g.tudu_id = t.tudu_id "
  184. . "LEFT JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id AND tu.unique_id = "
  185. . $this->_db->quote($condition['uniqueid'])
  186. . "LEFT JOIN td_post AS p ON p.tudu_id = t.tudu_id AND p.is_first = 1";
  187. $columns = "t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.is_done AS isdone, t.percent, t.cycle_id AS cycleid, t.percent, t.reply_num AS replynum, t.accept_time AS lastaccepttime, "
  188. . "t.last_post_time AS lastposttime, t.is_done, t.priority, t.privacy, t.password, t.start_time AS starttime, t.end_time AS endtime, p.attach_num AS attachnum, tu.accept_time as accepttime, "
  189. . "t.type, t.subject, t.from, t.to, t.cc, t.bcc, t.status, t.is_draft AS isdraft, t.need_confirm AS needconfirm, tu.unique_id AS uniqueid, "
  190. . "TRIM(LEADING ',' FROM tu.labels) labels, tu.role, tu.accepter_info AS accepterinfo, tu.tudu_status AS selfstatus, tu.is_read as isread, g.type as nodetype ";
  191. $where = array();
  192. $limit = '';
  193. if (empty($condition)) {
  194. return null;
  195. }
  196. if (!empty($condition['uniqueid'])) {
  197. $where[] = 'tu.unique_id = ' . $this->_db->quote($condition['uniqueid']);
  198. }
  199. if (!empty($condition['labelid'])) {
  200. $table .= ' LEFT JOIN td_tudu_label AS tl ON tl.unique_id = tu.unique_id AND tl.tudu_id = tu.tudu_id';
  201. $where[] = 'tl.label_id = ' . $this->_db->quote($condition['labelid']);
  202. }
  203. if (!empty($condition['type'])) {
  204. $where[] = 't.type = ' . $this->_db->quote($condition['type']);
  205. }
  206. // 关键字
  207. if (!empty($condition['keyword'])) {
  208. $keyword = $this->_db->quote('%' . $condition['keyword'] . '%');
  209. $where[] = 't.subject LIKE ' . $keyword;
  210. }
  211. if (!$filter || !isset($filter['role']) || $filter['role'] == true) {
  212. $where[] = 'tu.role IS NOT NULL';
  213. }
  214. if (!$filter || !isset($filter['isdone']) || $filter['isdone'] == true) {
  215. $where[] = 't.status <= 2';
  216. $where[] = 't.is_done = 0';
  217. }
  218. $where = implode(' AND ', $where);
  219. if ($where) {
  220. $where = 'WHERE ' . $where;
  221. }
  222. if (null !== $count) {
  223. $limit = 'LIMIT';
  224. if (null !== $offset) {
  225. $limit .= ' ' . (int) $offset . ',';
  226. }
  227. $limit .= ' ' . (int) $count;
  228. }
  229. $sql = "SELECT $columns FROM $table $where ORDER BY last_post_time DESC $limit";
  230. $records = $this->_db->fetchAll($sql);
  231. if (!$records) {
  232. return array();
  233. }
  234. return $records;
  235. }
  236. /**
  237. * 读取图度组
  238. *
  239. * @param array $condition
  240. * @param array $filter
  241. */
  242. public function getTuduGroups(array $condition, $filter = null)
  243. {
  244. $table = "td_tudu t "
  245. . "LEFT JOIN td_tudu_group g ON g.tudu_id = t.tudu_id "
  246. . "INNER JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id";
  247. $columns = "t.org_id AS orgid, t.board_id AS boardid, tu.unique_id AS uniqueid, t.tudu_id AS tuduid, "
  248. . "t.subject, t.type, t.from, t.to, g.type AS nodetype";
  249. $where = array();
  250. $bind = array();
  251. if (isset($condition['uniqueid'])) {
  252. $where[] = "tu.unique_id = :uniqueid";
  253. $bind['uniqueid'] = $condition['uniqueid'];
  254. }
  255. if (isset($condition['orgid'])) {
  256. $where[] = "t.org_id = :orgid";
  257. $bind['orgid'] = $condition['orgid'];
  258. }
  259. if ($filter && array_key_exists('isdraft', $filter)) {
  260. if (null !== $filter['isdraft']) {
  261. $where[] = "t.is_draft = " . ($filter['isdraft'] ? 1 : 0);
  262. }
  263. } else {
  264. $where[] = "t.is_draft = 0";
  265. }
  266. if ($filter && array_key_exists('isdone', $filter)) {
  267. if (null !== $filter['isdone']) {
  268. $where[] = "t.is_done = " . ($filter['isdone'] ? 1 : 0);
  269. }
  270. } else {
  271. $where[] = "t.is_done = 0";
  272. }
  273. $where[] = "(g.type = 'node' OR g.type = 'root')";
  274. // WHERE
  275. $where = implode(' AND ', $where);
  276. if ($where) {
  277. $where = 'WHERE ' . $where;
  278. }
  279. $sql = "SELECT $columns FROM $table $where ORDER BY t.last_post_time DESC";
  280. $records = $this->_db->fetchAll($sql, $bind);
  281. if (!$records) {
  282. return new Oray_Dao_Recordset();
  283. }
  284. return new Oray_Dao_Recordset($records, 'Dao_Td_Tudu_Record_TuduGroups');
  285. }
  286. /**
  287. *
  288. *
  289. * @param $condition
  290. * @param $filter
  291. * @param $sort
  292. * @param $maxCount
  293. */
  294. public function getGroupTudus(array $condition, $filter = null, $sort = null, $maxCount = null)
  295. {
  296. $table = "td_tudu t "
  297. . "LEFT JOIN td_post p ON p.tudu_id = t.tudu_id AND p.is_first = 1 "
  298. . "LEFT JOIN td_tudu_group g ON g.tudu_id = p.tudu_id "
  299. . "LEFT JOIN td_class c ON t.class_id = c.class_id AND t.org_id = c.org_id AND t.board_id = c.board_id "
  300. . "LEFT JOIN td_tudu_flow fl ON fl.tudu_id = t.tudu_id "
  301. . "LEFT JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id AND tu.unique_id = "
  302. . $this->_db->quote($condition['uniqueid']);
  303. $columns = "t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.cycle_id AS cycleid, t.prev_tudu_id AS prevtuduid, "
  304. . "t.type, t.subject, t.from, t.to, t.cc, t.priority, t.privacy, t.password, fl.current_step_id AS stepid, "
  305. . "p.post_id AS postid, '' AS content, '' AS posterinfo, p.attach_num AS attachnum, "
  306. . "t.last_post_time AS lastposttime, t.last_poster AS lastposter, t.last_forward AS lastforward, "
  307. . "t.view_num AS viewnum, t.reply_num AS replynum, t.log_num AS lognum, "
  308. . "t.percent, t.score, t.status, t.special, t.notify_all AS notifyall, t.accep_mode AS accepmode, "
  309. . "t.start_time AS starttime, t.end_time AS endtime, t.complete_time AS completetime, t.need_confirm AS needconfirm, "
  310. . "t.total_time AS totaltime, t.elapsed_time AS elapsedtime, "
  311. . "t.accept_time AS accepttime, t.create_time AS createtime, "
  312. . "tu.unique_id AS uniqueid, TRIM(LEADING ',' FROM tu.labels) labels, "
  313. . "tu.is_read AS isread, tu.is_forward AS isforward, tu.mark, "
  314. . "tu.role, tu.percent AS selfpercent, tu.forward_info AS forwardinfo, "
  315. . "tu.tudu_status AS selftudustatus, tu.accept_time AS selfaccepttime, "
  316. . "t.is_draft AS isdraft, t.is_done AS isdone, t.is_top AS istop, p.is_send AS issend, "
  317. . "t.class_id AS classid, t.flow_id AS flowid, c.class_name AS classname, "
  318. . "g.type AS nodetype, g.parent_tudu_id AS parentid";
  319. $where = array();
  320. $order = array();
  321. $limit = '';
  322. if (!empty($condition['tuduids']) && is_array($condition['tuduids'])) {
  323. $where[] = 't.tudu_id IN(' . implode(',', array_map(array($this->_db, 'quote'), $condition['tuduids'])) . ')';
  324. }
  325. if (!empty($condition['parentid'])) {
  326. $where[] = 'g.parent_tudu_id = ' . $this->_db->quote($condition['parentid']);
  327. }
  328. if (!empty($condition['senderid'])) {
  329. $where[] = 'g.unique_id = ' . $this->_db->quote($condition['senderid']);
  330. }
  331. if (!empty($condition['role'])) {
  332. $where[] = 'tu.role = ' . $this->_db->quote($condition['role']);
  333. }
  334. if (!empty($condition['starttime']) && !empty($condition['endtime'])) {
  335. $startTime = (int) $condition['starttime'];
  336. $endTime = (int) $condition['endtime'];
  337. $where[] = "(((t.start_time >= {$startTime} AND t.start_time <= {$endTime}) "
  338. . "OR (t.end_time >= {$startTime} AND t.end_time <= {$endTime}) "
  339. . "OR ((t.start_time <= {$startTime} OR t.start_time IS NULL) "
  340. . "AND (t.end_time >= {$endTime} OR t.end_time IS NULL)))"
  341. . "AND (((t.complete_time <= {$endTime} AND t.complete_time >= {$startTime}) "
  342. . "OR (t.complete_time >= {$endTime} AND t.complete_time >= {$startTime})) "
  343. . "OR t.complete_time IS NULL) OR (t.status <= 1 AND DATEDIFF(FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y-%m-%d'), FROM_UNIXTIME(t.end_time, '%Y-%m-%d')) > 0 AND t.end_time <= {$endTime}))";
  344. // 不显示“已取消”,和“已拒绝并确认“的图度
  345. $where[] = '(t.status <= 2 OR (t.status = 3 AND t.is_done = 0)) ';
  346. }
  347. if ($filter && array_key_exists('isdraft', $filter)) {
  348. if (null !== $filter['isdraft']) {
  349. $where[] = 't.is_draft = ' . ($filter['isdraft'] ? 1 : 0);
  350. }
  351. } else {
  352. $where[] = 't.is_draft = 0';
  353. }
  354. if (empty($where)) {
  355. return new Oray_Dao_Recordset();
  356. }
  357. // WHERE
  358. $where = implode(' AND ', $where);
  359. if ($where) {
  360. $where = 'WHERE ' . $where;
  361. }
  362. $sort = $this->_formatSort($sort);
  363. foreach ($sort as $key => $val) {
  364. switch ($key) {
  365. case 'lastposttime':
  366. $key = 'last_post_time';
  367. break;
  368. case 'subject':
  369. $key = 'subject';
  370. break;
  371. case 'endtime':
  372. $key = 'end_time';
  373. break;
  374. case 'from':
  375. $key = '`from`';
  376. break;
  377. case 'to':
  378. $key = '`to`';
  379. break;
  380. case 'istop':
  381. $key = 'is_top';
  382. break;
  383. default:
  384. continue 2;
  385. break;
  386. }
  387. $order[] = $key . ' ' . $val;
  388. }
  389. // ORDER
  390. $order = implode(', ', $order);
  391. if ($order) {
  392. $order = 'ORDER BY ' . $order;
  393. }
  394. // LIMIT
  395. if (is_int($maxCount) && $maxCount > 0) {
  396. $limit = 'LIMIT ' . $maxCount;
  397. }
  398. $sql = "SELECT $columns FROM $table $where $order $limit";
  399. $records = $this->_db->fetchAll($sql);
  400. return new Oray_Dao_Recordset($records, 'Dao_Td_Tudu_Record_Tudu');
  401. }
  402. /**
  403. * 获取用户图度箱的图度列表
  404. *
  405. * SELECT t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.cycle_id AS cycleid,
  406. * t.type, t.subject, t.from, t.to, t.cc, t.priority, t.privacy,
  407. * t.last_post_time AS lastposttime, t.last_poster AS lastposter,
  408. * t.view_num AS viewnum, t.reply_num AS replynum, t.log_num AS lognum, t.percent, t.score, t.status,
  409. * t.start_time AS starttime, t.end_time AS endtime,
  410. * t.total_time AS totaltime, t.elapsed_time AS elapsedtime,
  411. * t.create_time AS create_time, p.post_id AS postid, p.content, '' AS posterinfo, p.attach_num AS attachnum,
  412. * tu.unique_id AS uniqueid, TRIM(LEADING ',' FROM tu.labels) labels,
  413. * tu.is_read AS isread, tu.is_forward AS isforward, is_draft AS isdraft, is_done AS isdone
  414. * FROM td_tudu t
  415. * LEFT JOIN td_post p ON p.tudu_id = t.tudu_id AND p.is_first = 1
  416. * INNER JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id
  417. * LEFT JOIN td_tudu_label tl ON tl.tudu_id = t.tudu_id AND tl.unique_id = tu.unique_id
  418. * WHERE tu.unique_id = ? AND tl.label_id = ?
  419. *
  420. * @param array $condition
  421. * @param mixed $sort
  422. * @param int $page
  423. * @param int $pageSize
  424. * @return Oray_Dao_Recordset
  425. */
  426. public function getTuduPage(array $condition = null, $sort = null, $page = null, $pageSize = null)
  427. {
  428. $table = 'td_tudu t '
  429. . 'LEFT JOIN td_post p ON p.tudu_id = t.tudu_id AND p.is_first = 1 '
  430. . 'LEFT JOIN td_tudu_group g ON g.tudu_id = p.tudu_id '
  431. . 'INNER JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id '
  432. . "LEFT JOIN td_class c ON t.class_id = c.class_id AND t.org_id = c.org_id AND t.board_id = c.board_id "
  433. . 'LEFT JOIN td_tudu_label tl ON tl.tudu_id = t.tudu_id AND tl.unique_id = tu.unique_id '
  434. . 'LEFT JOIN td_tudu_cycle tc ON tc.cycle_id = t.cycle_id '
  435. . 'LEFT JOIN td_tudu_flow fl ON fl.tudu_id = t.tudu_id ';
  436. $columns = "t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.cycle_id AS cycleid, t.prev_tudu_id AS prevtuduid, "
  437. . "t.type, t.subject, t.from, t.to, t.cc, t.priority, t.privacy, t.password, fl.current_step_id AS stepid, t.app_id AS appid, "
  438. . "t.last_post_time AS lastposttime, t.last_poster AS lastposter, t.last_forward AS lastforward, t.flow_id AS flowid, "
  439. . "t.view_num AS viewnum, t.reply_num AS replynum, t.log_num AS lognum, t.percent, t.score, t.status, "
  440. . "t.special, t.start_time AS starttime, t.end_time AS endtime, t.complete_time AS completetime, t.notify_all AS notifyall, "
  441. . "t.total_time AS totaltime, t.elapsed_time AS elapsedtime, t.need_confirm AS needconfirm, "
  442. . "t.create_time AS createtime, t.accept_time AS accepttime, t.accep_mode AS accepmode, "
  443. . "p.post_id AS postid, '' AS content, '' AS posterinfo, p.attach_num AS attachnum, "
  444. . "tu.unique_id AS uniqueid, TRIM(LEADING ',' FROM tu.labels) labels, tu.mark, "
  445. . "tu.is_read AS isread, tu.is_forward AS isforward, is_draft AS isdraft, t.is_done AS isdone, t.is_top AS istop, "
  446. . "tu.role, tu.percent AS selfpercent, tu.forward_info AS forwardinfo, "
  447. . "tu.tudu_status AS selftudustatus, tu.accept_time AS selfaccepttime, "
  448. . "t.class_id AS classid, c.class_name AS classname, tc.display_date AS displaydate, "
  449. . "g.type AS nodetype, g.parent_tudu_id AS parentid";
  450. $primary = "t.tudu_id";
  451. $recordClass = "Dao_Td_Tudu_Record_Tudu";
  452. $where = array();
  453. $order = array();
  454. // $condition...
  455. if (isset($condition['tuduindexnum'])) {
  456. if (is_array($condition['tuduindexnum'])) {
  457. $arr = array_map('intval', $condition['tuduindexnum']);
  458. $where[] = 't.tudu_index_num IN (' . implode(',', $arr) . ')';
  459. } else {
  460. $where[] = 't.tudu_index_num = ' . $condition['tuduindexnum'];
  461. }
  462. }
  463. if (isset($condition['uniqueid'])) {
  464. $where[] = 'tu.unique_id = ' . $this->_db->quote($condition['uniqueid']);
  465. }
  466. if (isset($condition['label'])) {
  467. $where[] = 'tl.label_id = ' . $this->_db->quote($condition['label']);
  468. }
  469. // 板块
  470. if (isset($condition['boardid'])) {
  471. $where[] = 't.board_id = ' . $this->_db->quote($condition['boardid']);
  472. }
  473. // 主题分类
  474. if (isset($condition['classid'])) {
  475. $where[] = 't.class_id = ' . $this->_db->quote($condition['classid']);
  476. }
  477. // 查找已读状态
  478. if (isset($condition['isread'])) {
  479. $where[] = 'tu.is_read = ' . ($condition['isread'] ? 1 : 0);
  480. }
  481. // 关键字
  482. if (!empty($condition['keyword'])) {
  483. $keyword = $this->_db->quote('%' . $condition['keyword'] . '%');
  484. $where[] = 't.subject LIKE ' . $keyword;
  485. }
  486. // 发送人
  487. if (!empty($condition['from'])) {
  488. $from = $this->_db->quote('%' . $condition['from'] . '%');
  489. $where[] = 't.from LIKE ' . $from;
  490. }
  491. // 接收人
  492. if (!empty($condition['to'])) {
  493. $to = $this->_db->quote('%' . $condition['to'] . '%');
  494. $where[] = 't.to LIKE ' . $to;
  495. }
  496. // 状态
  497. if (isset($condition['status'])) {
  498. if (is_array($condition['status'])) {
  499. foreach ($condition['status'] as $item) {
  500. $status[] = $this->_db->quote($item);
  501. }
  502. $where[] = 't.status IN (' . implode(',', $status) . ')';
  503. } else if (is_int($condition['status'])) {
  504. $where[] = 't.status = ' . $condition['status'];
  505. }
  506. }
  507. // 类型
  508. if (!empty($condition['type'])) {
  509. $where[] = 't.type = ' . $this->_db->quote($condition['type']);
  510. }
  511. // 已完成
  512. if (isset($condition['isdone'])) {
  513. $where[] = 't.is_done = ' . ($condition['isdone'] ? 1 : 0);
  514. }
  515. // 甘特图
  516. if(!empty($condition['startdate']) && !empty($condition['enddate'])) {
  517. $startDate = (int) $condition['startdate'];
  518. $endDate = (int) $condition['enddate'];
  519. $where[] = "(((t.start_time >= {$startDate} AND t.start_time <= {$endDate}) "
  520. . "OR (t.end_time >= {$startDate} AND t.end_time <= {$endDate}) "
  521. . "OR ((t.start_time <= {$startDate} OR t.start_time IS NULL) "
  522. . "AND (t.end_time >= {$endDate} OR t.end_time IS NULL)))"
  523. . "AND (((t.complete_time <= {$endDate} AND t.complete_time >= {$startDate}) "
  524. . "OR (t.complete_time >= {$endDate} AND t.complete_time >= {$startDate})) "
  525. . "OR t.complete_time IS NULL) OR (t.status <= 1 AND DATEDIFF(FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y-%m-%d'), FROM_UNIXTIME(t.end_time, '%Y-%m-%d')) > 0 AND t.end_time <= {$endDate}))";
  526. }
  527. $array = array(
  528. 'createtime' => 't.create_time',
  529. 'endtime' => 't.end_time',
  530. 'starttime' => 't.start_time'
  531. );
  532. // createtime, endtime, starttime
  533. foreach ($array as $key => $col) {
  534. if (!isset($condition[$key])) {
  535. continue ;
  536. }
  537. if (is_array($condition[$key])) {
  538. $arr = array();
  539. if (isset($condition[$key]['start'])) {
  540. $arr[] = $col . ' >= ' . (int) $condition[$key]['start'];
  541. }
  542. if (isset($condition[$key]['end'])) {
  543. $arr[] = $col . ' <= ' . (int) $condition[$key]['end'];
  544. }
  545. if ($arr) {
  546. $where[] = '(' . $col . ' IS NOT NULL AND ' . implode(' AND ', $arr) . ')';
  547. }
  548. } elseif (is_int($condition[$key])) {
  549. $where[] = '(' . $col . ' IS NOT NULL AND ' . $col . ' >= ' . $condition[$key] . ')';
  550. }
  551. }
  552. // 过期
  553. if (isset($condition['expiredate']) && is_int($condition['expiredate'])) {
  554. $where[] = '(tu.is_read = 0 OR t.start_time IS NULL OR t.start_time <= ' . $condition['expiredate'] . ')';
  555. }
  556. // WHERE
  557. $where = implode(' AND ', $where);
  558. if ($where) {
  559. $where = 'WHERE ' . $where;
  560. }
  561. // 格式化排序参数
  562. $sort = $this->_formatSort($sort);
  563. foreach ($sort as $key => $val) {
  564. switch ($key) {
  565. case 'lastposttime':
  566. $key = 'last_post_time';
  567. break;
  568. case 'subject':
  569. $key = 'subject';
  570. break;
  571. case 'endtime':
  572. $key = 'end_time';
  573. break;
  574. case 'starttime':
  575. $key = 'start_time';
  576. break;
  577. case 'from':
  578. $key = '`from`';
  579. break;
  580. case 'to':
  581. $key = '`to`';
  582. break;
  583. case 'istop':
  584. $key = 'is_top';
  585. break;
  586. case 'percent':
  587. $key = 'percent';
  588. break;
  589. default:
  590. continue 2;
  591. break;
  592. }
  593. $order[] = $key . ' ' . $val;
  594. }
  595. // ORDER
  596. $order = implode(', ', $order);
  597. if ($order) {
  598. $order = 'ORDER BY ' . $order;
  599. }
  600. if (null === $pageSize && null === $page) {
  601. $sql = "SELECT $columns FROM $table $where $order";
  602. } else {
  603. // 使用默认的分页大小
  604. if (null === $pageSize) {
  605. $pageSize = self::$_defaultPageSize;
  606. }
  607. if ($page < 1) $page = 1;
  608. $sql = "SELECT $columns FROM $table $where $order LIMIT " . $pageSize * ($page - 1) . ", " . $pageSize;
  609. }
  610. $records = $this->_db->fetchAll($sql);
  611. return new Oray_Dao_Recordset($records, $recordClass);
  612. }
  613. /**
  614. * 获取版块的图度列表
  615. *
  616. * SELECT
  617. * t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.cycle_id AS cycleid,
  618. * t.type, t.subject, t.from, t.to, t.cc, t.priority, t.privacy,
  619. * t.last_post_time AS lastposttime, t.last_poster AS lastposter,
  620. * t.view_num AS viewnum, t.reply_num AS replynum, t.log_num AS lognum, t.percent, t.score, t.status,
  621. * t.special, t.start_time AS starttime, t.end_time AS endtime,
  622. * t.total_time AS totaltime, t.elapsed_time AS elapsedtime,
  623. * t.create_time AS createtime, t.accept_time AS accepttime,
  624. * p.post_id AS postid, '' AS content, '' AS posterinfo, p.attach_num AS attachnum,
  625. * tu.unique_id AS uniqueid, TRIM(LEADING ',' FROM tu.labels) labels,
  626. * tu.is_read AS isread, tu.is_forward AS isforward, is_draft AS isdraft, is_done AS isdone,
  627. * g.type AS nodetype, g.parent_tudu_id AS parentid
  628. *
  629. * FROM td_tudu t
  630. * LEFT JOIN td_tudu_group g ON g.tudu_id = p.tudu_id
  631. * LEFT JOIN td_post p ON p.tudu_id = t.tudu_id AND p.is_first = 1
  632. * LEFT JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id AND tu.unique_id = ?
  633. *
  634. * WHERE t.is_draft = 0
  635. * AND t.org_id = ?
  636. * AND t.board_id = ?
  637. *
  638. * @param array $condition
  639. * @param mixed $sort
  640. * @param int $page
  641. * @param int $pageSize
  642. * @return Oray_Dao_Recordset
  643. */
  644. public function getBoardTuduPage(array $condition = null, $sort = null, $page = null, $pageSize = null)
  645. {
  646. $uniqueId = isset($condition['uniqueid'])
  647. ? $this->_db->quote($condition['uniqueid'])
  648. : "''";
  649. $table = "td_tudu t "
  650. . "LEFT JOIN td_post p ON p.tudu_id = t.tudu_id AND p.is_first = 1 "
  651. . "LEFT JOIN td_tudu_group g ON g.tudu_id = p.tudu_id "
  652. . "LEFT JOIN td_class c ON t.class_id = c.class_id AND t.org_id = c.org_id AND t.board_id = c.board_id "
  653. . 'LEFT JOIN td_tudu_cycle tc ON tc.cycle_id = t.cycle_id '
  654. . "LEFT JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id AND tu.unique_id = " . $uniqueId;
  655. $columns = "t.org_id AS orgid, t.board_id AS boardid, t.tudu_id AS tuduid, t.cycle_id AS cycleid, "
  656. . "t.type, t.subject, t.from, t.to, t.cc, t.priority, t.privacy, t.password, "
  657. . "t.last_post_time AS lastposttime, t.last_poster AS lastposter, t.flow_id AS flowid, "
  658. . "t.view_num AS viewnum, t.reply_num AS replynum, t.log_num AS lognum, t.percent, t.score, t.status, "
  659. . "t.special, t.start_time AS starttime, t.end_time AS endtime, t.need_confirm AS needconfirm, "
  660. . "t.total_time AS totaltime, t.elapsed_time AS elapsedtime, t.accep_mode AS accepmode, "
  661. . "t.create_time AS createtime, t.accept_time AS accepttime, "
  662. . "p.post_id AS postid, '' AS content, '' AS posterinfo, p.attach_num AS attachnum, "
  663. . "tu.unique_id AS uniqueid, TRIM(LEADING ',' FROM tu.labels) labels, tu.mark, "
  664. . "tu.is_read AS isread, tu.is_forward AS isforward, is_draft AS isdraft, is_done AS isdone, t.is_top AS istop, "
  665. . "t.class_id AS classid, c.class_name AS classname, tc.display_date AS displaydate, "
  666. . "g.type AS nodetype, g.parent_tudu_id AS parentid";
  667. $primary = "t.tudu_id";
  668. $recordClass = "Dao_Td_Tudu_Record_Tudu";
  669. $where = array();
  670. $order = array();
  671. if (isset($condition['orgid'])) {
  672. $where[] = 't.org_id = ' . $this->_db->quote($condition['orgid']);
  673. }
  674. if (isset($condition['boardid'])) {
  675. if (is_array($condition['boardid'])) {
  676. $where[] = 't.board_id IN(' . implode(',', array_map(array($this->_db, 'quote'), $condition['boardid'])) . ')';
  677. } else {
  678. $where[] = 't.board_id = ' . $this->_db->quote($condition['boardid']);
  679. }
  680. }
  681. // 关键字
  682. if (!empty($condition['keyword'])) {
  683. $keyword = $this->_db->quote('%' . $condition['keyword'] . '%');
  684. $where[] = 't.subject LIKE ' . $keyword;
  685. }
  686. // 发送人
  687. if (!empty($condition['from'])) {
  688. $from = $this->_db->quote('%' . $condition['from'] . '%');
  689. $where[] = 't.from LIKE ' . $from;
  690. }
  691. // 接收人
  692. if (!empty($condition['to'])) {
  693. $to = $this->_db->quote('%' . $condition['to'] . '%');
  694. $where[] = 't.to LIKE ' . $to;
  695. }
  696. // 主题分类
  697. if (isset($condition['classid'])) {
  698. $where[] = 'c.class_id = ' . $this->_db->quote($condition['classid']);
  699. }
  700. // 类型
  701. if (!empty($condition['type'])) {
  702. if (is_array($condition['type'])) {
  703. $where[] = 't.type IN ( ' . implode(',', array_map(array($this->_db, 'quote'), $condition['type'])) . ')';
  704. } else {
  705. $where[] = 't.type = ' . $this->_db->quote($condition['type']);
  706. }
  707. }
  708. if (isset($condition['priority']) && is_int($condition['priority'])) {
  709. $where[] = 't.priority = ' . $condition['priority'];
  710. }
  711. if (isset($condition['status']) && is_int($condition['status'])) {
  712. $where[] = 't.status = ' . $condition['status'];
  713. }
  714. // 创建时间
  715. if (isset($condition['createtime'])) {
  716. if (is_array($condition['createtime'])) {
  717. $array = array();
  718. if (isset($condition['createtime']['start']) && is_int($condition['createtime']['start'])) {
  719. $array[] = 't.create_time >= ' . $condition['createtime']['start'];
  720. }
  721. if (isset($condition['createtime']['end']) && is_int($condition['createtime']['end'])) {
  722. $array[] = 't.create_time <= ' . $condition['createtime']['end'];
  723. }
  724. if ($array) {
  725. $where[] = '(' . implode(' AND ', $array) . ')';
  726. }
  727. } elseif (is_int($condition['createtime'])) {
  728. $where[] = 't.create_time >= ' . $condition['createtime'];
  729. }
  730. }
  731. // 创建时间
  732. if (isset($condition['endtime'])) {
  733. if (is_array($condition['endtime'])) {
  734. $array = array();
  735. if (isset($condition['endtime']['start']) && is_int($condition['endtime']['start'])) {
  736. $array[] = 't.end_time >= ' . $condition['endtime'];
  737. }
  738. if (isset($condition['endtime']['end']) && is_int($condition['endtime']['end'])) {
  739. $array[] = 't.end_time <= ' . $condition['endtime'];
  740. }
  741. if ($array) {
  742. $where[] = '(' . implode(' AND ', $array) . ')';
  743. }
  744. } elseif (is_int($condition['endtime'])) {
  745. $where[] = 't.end_time >= ' . $condition['endtime'];
  746. }
  747. }
  748. // 过期
  749. if (isset($condition['expiredate']) && is_int($condition['expiredate'])) {
  750. $where[] = '(t.is_done <> 1 OR t.create_time >= ' . $condition['expiredate'] . ')';
  751. }
  752. // 过滤草稿
  753. $where[] = 't.is_draft = 0';
  754. if (isset($condition['privacy'])) {
  755. $where[] = '(t.privacy = 0 OR tu.unique_id = ' . $uniqueId . ')';
  756. }
  757. if (isset($condition['self'])) {
  758. $where[] = 'tu.unique_id = ' . $uniqueId;
  759. }
  760. // WHERE
  761. $where = implode(' AND ', $where);
  762. // 格式化排序参数
  763. $sort = $this->_formatSort($sort);
  764. foreach ($sort as $key => $val) {
  765. switch ($key) {
  766. case 'lastposttime':
  767. $key = 'last_post_time';
  768. break;
  769. case 'subject':
  770. $key = 'subject';
  771. break;
  772. case 'endtime':
  773. $key = 'end_time';
  774. break;
  775. case 'istop':
  776. $key = 'is_top';
  777. break;
  778. default:
  779. continue 2;
  780. break;
  781. }
  782. $order[] = $key . ' ' . $val;
  783. }
  784. // ORDER
  785. $order = implode(', ', $order);
  786. // 使用默认的分页大小
  787. if (null === $pageSize) {
  788. $pageSize = self::$_defaultPageSize;
  789. }
  790. /**
  791. * @see Oray_Db_Paginator
  792. */
  793. require_once 'Oray/Db/Paginator.php';
  794. // 初始化分页器
  795. $paginator = new Oray_Db_Paginator(array(
  796. Oray_Db_Paginator::ADAPTER => $this->_db,
  797. Oray_Db_Paginator::RECORD_CLASS => $recordClass,
  798. Oray_Db_Paginator::PAGE_SIZE => $pageSize,
  799. Oray_Db_Paginator::TABLE => $table,
  800. Oray_Db_Paginator::PRIMARY => $primary,
  801. Oray_Db_Paginator::COLUMNS => $columns,
  802. Oray_Db_Paginator::WHERE => $where,
  803. Oray_Db_Paginator::ORDER => $order
  804. ));
  805. // 返回查询结果
  806. return $paginator->query($page);
  807. }
  808. /**
  809. * 获取符合条件的图度数量
  810. *
  811. * @param $condition
  812. */
  813. public function getTuduCount(array $condition)
  814. {
  815. $table = 'td_tudu t ';
  816. if (isset($condition['orgid'])) {
  817. $where[] = 't.org_id = ' . $this->_db->quote($condition['orgid']);
  818. }
  819. if (isset($condition['type'])) {
  820. $where[] = 't.type = ' . $this->_db->quote($condition['type']);
  821. }
  822. if (isset($condition['boardid'])) {
  823. $where[] = 't.board_id = ' . $this->_db->quote($condition['boardid']);
  824. }
  825. if (isset($condition['uniqueid'])) {
  826. $table .= 'INNER JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id ';
  827. $where[] = 'tu.unique_id = ' . $this->_db->quote($condition['uniqueid']);
  828. if (isset($condition['labelid'])) {
  829. $table .= 'LEFT JOIN td_tudu_label tl ON tl.tudu_id = t.tudu_id AND tl.unique_id = tu.unique_id ';
  830. $where[] = 'tl.label_id = ' . $this->_db->quote($condition['labelid']);
  831. }
  832. }
  833. // 草稿
  834. if (isset($condition['isdraft'])) {
  835. $where[] = 't.is_draft = ' . ($condition['isdraft'] ? 1 : 0);
  836. }
  837. // 查找已读状态
  838. if (isset($condition['isread'])) {
  839. $where[] = 'tu.is_read = ' . ($condition['isread'] ? 1 : 0);
  840. }
  841. // 关键字
  842. if (!empty($condition['keyword'])) {
  843. $keyword = $this->_db->quote('%' . $condition['keyword'] . '%');
  844. $where[] = 't.subject LIKE ' . $keyword;
  845. }
  846. // 发送人
  847. if (!empty($condition['from'])) {
  848. $from = $this->_db->quote('%' . $condition['from'] . '%');
  849. $where[] = 't.from LIKE ' . $from;
  850. }
  851. // 接收人
  852. if (!empty($condition['to'])) {
  853. $to = $this->_db->quote('%' . $condition['to'] . '%');
  854. $where[] = 't.to LIKE ' . $to;
  855. }
  856. // 状态
  857. if (isset($condition['status'])) {
  858. if (is_array($condition['status'])) {
  859. foreach ($condition['status'] as $item) {
  860. $status[] = $this->_db->quote($item);
  861. }
  862. $where[] = 't.status IN (' . implode(',', $status) . ')';
  863. } else if (is_int($condition['status'])) {
  864. $where[] = 't.status = ' . $condition['status'];
  865. }
  866. }
  867. // 版块
  868. if (!empty($condition['boardid'])) {
  869. $where[] = 't.board_id = ' . $this->_db->quote($condition['boardid']);
  870. }
  871. // 主题分类
  872. if (isset($condition['classid'])) {
  873. $where[] = 't.class_id = ' . $this->_db->quote($condition['classid']);
  874. }
  875. // 已完成
  876. if (isset($condition['isdone'])) {
  877. $where[] = 't.is_done = ' . ($condition['isdone'] ? 1 : 0);
  878. }
  879. $array = array(
  880. 'createtime' => 't.create_time',
  881. 'endtime' => 't.end_time',
  882. 'starttime' => 't.start_time'
  883. );
  884. // createtime, endtime, starttime
  885. foreach ($array as $key => $col) {
  886. if (!isset($condition[$key])) {
  887. continue ;
  888. }
  889. if (is_array($condition[$key])) {
  890. $arr = array();
  891. if (isset($condition[$key]['start'])) {
  892. $arr[] = $col . ' >= ' . (int) $condition[$key]['start'];
  893. }
  894. if (isset($condition[$key]['end'])) {
  895. $arr[] = $col . ' <= ' . (int) $condition[$key]['end'];
  896. }
  897. if ($arr) {
  898. $where[] = '(' . implode(' AND ', $arr) . ')';
  899. }
  900. } elseif (is_int($condition[$key])) {
  901. $where[] = $col . ' >= ' . $condition[$key];
  902. }
  903. }
  904. // 过期
  905. if (isset($condition['expiredate']) && is_int($condition['expiredate'])) {
  906. $where[] = '(tu.is_read = 0 OR t.start_time IS NULL OR t.start_time <= ' . $condition['expiredate'] . ')';
  907. }
  908. // WHERE
  909. $where = implode(' AND ', $where);
  910. if ($where) {
  911. $where = 'WHERE ' . $where;
  912. }
  913. $sql = "SELECT COUNT(0) FROM {$table} {$where}";
  914. $count = (int) $this->_db->fetchOne($sql);
  915. return $count;
  916. }
  917. /**
  918. * 统计符合条件的图度数量 -- 此方法将丢弃,请使用 getTuduCount 代替
  919. *
  920. * @param array $condition
  921. * @depared
  922. */
  923. public function countTudu(array $condition)
  924. {
  925. $table = 'td_tudu t '
  926. . 'INNER JOIN td_tudu_user tu ON tu.tudu_id = t.tudu_id '
  927. . 'LEFT JOIN td_tudu_label tl ON tl.tudu_id = t.tudu_id AND tl.unique_id = tu.unique_id';
  928. // $condition...
  929. if (isset($condition['uniqueid'])) {
  930. $where[] = 'tu.unique_id = ' . $this->_db->quote($condition['uniqueid']);
  931. }
  932. if (isset($condition['label'])) {
  933. $where[] = 'tl.label_id = ' . $this->_db->quote($condition['label']);
  934. }
  935. // 状态
  936. if (isset($condition['isdraft']) && is_int($condition['isdraft'])) {
  937. $where[] = 't.is_draft = ' . (int) $condition['isdraft'];
  938. }
  939. // 查找已读状态
  940. if (isset($condition['isread'])) {
  941. $where[] = 'tu.is_read = ' . ($condition['isread'] ? 1 : 0);
  942. }
  943. // 关键字
  944. if (!empty($condition['keyword'])) {
  945. $keyword = $this->_db->quote('%' . $condition['keyword'] . '%');
  946. $where[] = 't.subject LIKE ' . $keyword;
  947. }
  948. // 发送人
  949. if (!empty($condition['from'])) {
  950. $from = $this->_db->quote('%' . $condition['from'] . '%');
  951. $where[] = 't.from LIKE ' . $from;
  952. }
  953. // 接收人
  954. if (!empty($condition['to'])) {
  955. $to = $this->_db->quote('%' . $condition['to'] . '%');
  956. $where[] = 't.to LIKE ' . $to;
  957. }
  958. // 状态
  959. if (isset($condition['status']) && is_int($condition['status'])) {
  960. $where[] = 't.status = ' . $condition['status'];
  961. }
  962. // 版块
  963. if (!empty($condition['boardid'])) {
  964. $where[] = 't.board_id = ' . $this->_db->quote($condition['boardid']);
  965. }
  966. // 主题分类
  967. if (isset($condition['classid'])) {
  968. $where[] = 't.class_id = ' . $this->_db->quote($condition['classid']);
  969. }
  970. // 类型
  971. if (!empty($condition['type'])) {
  972. $where[] = 't.type = ' . $this->_db->quote($condition['type']);
  973. }
  974. // 已完成
  975. if (isset($condition['isdone'])) {
  976. $where[] = 't.is_done = ' . ($condition['isdone'] ? 1 : 0);
  977. }
  978. $array = array(
  979. 'createtime' => 't.create_time',
  980. 'endtime' => 't.end_time',
  981. 'starttime' => 't.start_time'
  982. );
  983. // createtime, endtime, starttime
  984. foreach ($array as $key => $col) {
  985. if (!isset($condition[$key])) {
  986. continue ;
  987. }
  988. if (is_array($condition[$key])) {
  989. $arr = array();
  990. if (isset($condition[$key]['start'])) {
  991. $arr[] = $col . ' >= ' . (int) $condition[$key]['start'];
  992. }
  993. if (isset($condition[$key]['end'])) {
  994. $arr[] = $col . ' <= ' . (int) $condition[$key]['end'];
  995. }
  996. if ($arr) {
  997. $where[] = '(' . implode(' AND ', $arr) . ')';
  998. }
  999. } elseif (is_int($condition[$key])) {
  1000. $where[] = $col . ' >= ' . $condition[$key];
  1001. }
  1002. }
  1003. // 过期
  1004. if (isset($condition['expiredate']) && is_int($condition['expiredate'])) {
  1005. $where[] = '(tu.is_read = 0 OR t.start_time IS NULL OR t.start_time <= ' . $condition['expiredate'] . ')';
  1006. }
  1007. // WHERE
  1008. $where = implode(' AND ', $where);
  1009. if ($where) {
  1010. $where = 'WHERE ' . $where;
  1011. }
  1012. $sql = "SELECT COUNT(0) FROM {$table} {$where}";
  1013. $count = (int) $this->_db->fetchOne($sql);
  1014. return $count;
  1015. }
  1016. /**
  1017. * Create tudu
  1018. *
  1019. * @param $params
  1020. * @return string|false
  1021. */
  1022. public function createTudu(array $params)
  1023. {
  1024. if (empty($params['orgid'])
  1025. || empty($params['boardid'])
  1026. || empty($params['tuduid'])
  1027. || empty($params['type'])
  1028. || empty($params['from'])
  1029. || !array_key_exists('subject', $params)) {
  1030. return false;
  1031. }
  1032. $address = self::formatAddress($params['from'], true);
  1033. $createTime = empty($params['createtime']) ? time() : (int) $params['createtime'];
  1034. $table = "td_tudu";
  1035. $bind = array(
  1036. 'org_id' => $params['orgid'],
  1037. 'board_id' => $params['boardid'],
  1038. 'tudu_id' => $params['tuduid'],
  1039. 'type' => $params['type'],
  1040. 'subject' => $params['subject'],
  1041. 'from' => $params['from'],
  1042. 'priority' => empty($params['priority']) ? 0 : (int) $params['priority'],
  1043. 'privacy' => empty($params['privacy']) ? 0 : (int) $params['privacy'],
  1044. 'is_draft' => 1,
  1045. 'last_poster' => $address ? $address[0] : '',
  1046. 'last_post_time' => $createTime,
  1047. 'create_time' => $createTime
  1048. );
  1049. if (isset($params['to'])) {
  1050. $bind['to'] = $params['to'];
  1051. }
  1052. if (isset($params['cc'])) {
  1053. $bind['cc'] = $params['cc'];
  1054. }
  1055. if (isset($params['bcc'])) {
  1056. $bind['bcc'] = $params['bcc'];
  1057. }
  1058. if (isset($params['starttime'])) {
  1059. $bind['start_time'] = $params['starttime'];
  1060. }
  1061. if (isset($params['endtime'])) {
  1062. $bind['end_time'] = $params['endtime'];
  1063. }
  1064. if (isset($params['totaltime'])) {
  1065. $bind['total_time'] = (int) $params['totaltime'];
  1066. }
  1067. if (isset($params['elapsedtime'])) {
  1068. $bind['elapsed_time'] = (int) $params['elapsedtime'];
  1069. }
  1070. if (isset($params['accepttime'])) {
  1071. $bind['accept_time'] = (int) $params['accepttime'];
  1072. }
  1073. if (isset($params['percent'])) {
  1074. $bind['percent'] = (int) $params['percent'];
  1075. }
  1076. if (isset($params['status'])) {
  1077. $bind['status'] = (int) $params['status'];
  1078. }
  1079. if (isset($params['viewnum'])) {
  1080. $bind['view_num'] = (int) $params['viewnum'];
  1081. }
  1082. if (isset($params['isdone'])) {
  1083. $bind['is_done'] = $params['isdone'] ? 1 : 0;
  1084. }
  1085. if (isset($params['istop'])) {
  1086. $bind['is_top'] = $params['istop'] ? 1 : 0;
  1087. }
  1088. if (isset($params['notifyall'])) {
  1089. $bind['notify_all'] = $params['notifyall'] ? 1 : 0;
  1090. }
  1091. if (!empty($params['prevtuduid'])) {
  1092. $bind['prev_tudu_id'] = $params['prevtuduid'];
  1093. }
  1094. if (!empty($params['stepid'])) {
  1095. $bind['step_id'] = $params['stepid'];
  1096. }
  1097. if (!empty($params['appid'])) {
  1098. $bind['app_id'] = $params['appid'];
  1099. }
  1100. if (isset($params['special']) && is_int($params['special'])) {
  1101. $bind['special'] = $params['special'];
  1102. }
  1103. if (isset($params['cycleid'])) {
  1104. $bind['cycle_id'] = $params['cycleid'];
  1105. }
  1106. if (isset($params['flowid'])) {
  1107. $bind['flow_id'] = $params['flowid'];
  1108. }
  1109. if (!empty($params['classid'])) {
  1110. $bind['class_id'] = $params['classid'];
  1111. }
  1112. if (isset($params['password'])) {
  1113. $bind['password'] = $params['password'];
  1114. }
  1115. if (isset($params['isauth'])) {
  1116. $bind['is_auth'] = $params['isauth'] ? 1 : 0;
  1117. }
  1118. if (isset($params['needconfirm'])) {
  1119. $bind['need_confirm'] = $params['needconfirm'] ? 1 : 0;
  1120. }
  1121. if (isset($params['cyclenum']) && is_int($params['cyclenum'])) {
  1122. $bind['cycle_num'] = $params['cyclenum'];
  1123. }
  1124. if (isset($params['acceptmode'])) {
  1125. $bind['accep_mode'] = $params['acceptmode'] ? 1 : 0;
  1126. }
  1127. try {
  1128. $this->_db->insert($table, $bind);
  1129. } catch (Zend_Db_Exception $e) {
  1130. $this->_catchException($e, __METHOD__);
  1131. return false;
  1132. }
  1133. return $params['tuduid'];
  1134. }
  1135. /**
  1136. * Update tudu
  1137. *
  1138. * @param string $tuduId
  1139. * @param array $params
  1140. * @return boolean
  1141. */
  1142. public function updateTudu($tuduId, array $params)
  1143. {
  1144. if (empty($tuduId)) {
  1145. return false;
  1146. }
  1147. $table = "td_tudu";
  1148. $bind = array();
  1149. $where = "tudu_id = " . $this->_db->quote($tuduId);
  1150. if (isset($params['subject'])) {
  1151. $bind['subject'] = $params['subject'];
  1152. }
  1153. if (!empty($params['boardid'])) {
  1154. $bind['board_id'] = $params['boardid'];
  1155. }
  1156. if (isset($params['from'])) {
  1157. $bind['from'] = $params['from'];
  1158. }
  1159. if (isset($params['to'])) {
  1160. $bind['to'] = $params['to'];
  1161. }
  1162. if (array_key_exists('cc', $params)) {
  1163. $bind['cc'] = $params['cc'];
  1164. }
  1165. if (array_key_exists('bcc', $params)) {
  1166. $bind['bcc'] = $params['bcc'];
  1167. }
  1168. if (isset($params['priority'])) {
  1169. $bind['priority'] = (int) $params['priority'];
  1170. }
  1171. if (isset($params['privacy'])) {
  1172. $bind['privacy'] = (int) $params['privacy'];
  1173. }
  1174. if (isset($params['stepnum']) && is_int($params['stepnum'])) {
  1175. $bind['step_num'] = $params['stepnum'];
  1176. }
  1177. if (array_key_exists('password', $params)) {
  1178. $bind['password'] = $params['password'];
  1179. }
  1180. if (array_key_exists('prevtuduid', $params)) {
  1181. $bind['prev_tudu_id'] = $params['prevtuduid'];
  1182. }
  1183. if (array_key_exists('stepid', $params)) {
  1184. $bind['step_id'] = $params['stepid'];
  1185. }
  1186. if (isset($params['lastposter'])) {
  1187. $bind['last_poster'] = $params['lastposter'];
  1188. }
  1189. if (isset($params['lastposttime'])) {
  1190. $bind['last_post_time'] = (int) $params['lastposttime'];
  1191. }
  1192. if (isset($params['isdraft'])) {
  1193. $bind['is_draft'] = $params['isdraft'] ? 1 : 0;
  1194. }
  1195. if (isset($params['isdone'])) {
  1196. $bind['is_done'] = $params['isdone'] ? 1 : 0;
  1197. }
  1198. if (isset($params['istop'])) {
  1199. $bind['is_top'] = $params['istop'] ? 1 : 0;
  1200. }
  1201. if (isset($params['notifyall'])) {
  1202. $bind['notify_all'] = $params['notifyall'] ? 1 : 0;
  1203. }
  1204. if (array_key_exists('starttime', $params)) {
  1205. $bind['start_time'] = $params['starttime'];
  1206. }
  1207. if (array_key_exists('endtime', $params)) {
  1208. $bind['end_time'] = $params['endtime'];
  1209. }
  1210. if (isset($params['totaltime'])) {
  1211. $bind['total_time'] = $params['totaltime'];
  1212. }
  1213. if (array_key_exists('accepttime', $params)) {
  1214. $bind['accept_time'] = $params['accepttime'];
  1215. }
  1216. if (isset($params['createtime'])) {
  1217. $bind['create_time'] = $params['createtime'];
  1218. }
  1219. if (isset($params['percent'])) {
  1220. $bind['percent'] = (int) $params['percent'];
  1221. }
  1222. if (isset($params['status'])) {
  1223. $bind['status'] = (int) $params['status'];
  1224. }
  1225. if (isset($params['special']) && is_int($params['special'])) {
  1226. $bind['special'] = $params['special'];
  1227. }
  1228. if (isset($params['score'])) {
  1229. $bind['score'] = (int) $params['score'];
  1230. }
  1231. if (array_key_exists('cycleid', $params)) {
  1232. $bind['cycle_id'] = $params['cycleid'];
  1233. }
  1234. if (array_key_exists('classid', $params)) {
  1235. $bind['class_id'] = $params['classid'];
  1236. }
  1237. if (array_key_exists('flowid', $params)) {
  1238. $bind['flow_id'] = $params['flowid'];
  1239. }
  1240. if (!empty($params['lastforward'])) {
  1241. $bind['last_forward'] = $params['lastforward'];
  1242. }
  1243. if (array_key_exists('password', $params)) {
  1244. $bind['password'] = $params['password'];
  1245. }
  1246. if (isset($params['isauth'])) {
  1247. $bind['is_auth'] = $params['isauth'] ? 1 : 0;
  1248. }
  1249. if (isset($params['needconfirm'])) {
  1250. $bind['need_confirm'] = $params['needconfirm'] ? 1 : 0;
  1251. }
  1252. if (isset($params['acceptmode'])) {
  1253. $bind['accep_mode'] = $params['acceptmode'] ? 1 : 0;
  1254. }
  1255. if (!$bind) {
  1256. return false;
  1257. }
  1258. try {
  1259. $this->_db->update($table, $bind, $where);
  1260. } catch (Zend_Db_Exception $e) {
  1261. $this->_catchException($e, __METHOD__);
  1262. return false;
  1263. }
  1264. return true;
  1265. }
  1266. /**
  1267. * Delete tudu
  1268. *
  1269. * @param string $tuduId
  1270. * @return boolean
  1271. */
  1272. public function deleteTudu($tuduId)
  1273. {
  1274. $sql = "call sp_td_delete_tudu(" . $this->_db->quote($tuduId) . ")";
  1275. try {
  1276. $ret = $this->_db->fetchOne($sql);
  1277. if ($ret == 0) {
  1278. return false;
  1279. }
  1280. } catch (Zend_Db_Exception $e) {
  1281. $this->_catchException($e, __METHOD__);
  1282. return false;
  1283. }
  1284. return true;
  1285. }
  1286. /**
  1287. * Send tudu
  1288. *
  1289. * @param $tuduId
  1290. * @return boolean
  1291. */
  1292. public function sendTudu($tuduId)
  1293. {
  1294. $sql = "call sp_td_send_tudu(" . $this->_db->quote($tuduId) . ")";
  1295. try {
  1296. $this->_db->query($sql);
  1297. } catch (Zend_Db_Exception $e) {
  1298. $this->_catchException($e, __METHOD__);
  1299. return false;
  1300. }
  1301. return true;
  1302. }
  1303. /**
  1304. * 图度移动版块
  1305. *
  1306. * @param $tuduId
  1307. * @param $boardId
  1308. * @param $classId
  1309. */
  1310. public function moveTudu($tuduId, $boardId, $classId = null)
  1311. {
  1312. $tuduId = $this->_db->quote($tuduId);
  1313. $boardId = $this->_db->quote($boardId);
  1314. $classId = $this->_db->quote($classId);
  1315. $sql = "call sp_td_move_tudu({$tuduId}, {$boardId}, {$classId})";
  1316. try {
  1317. $this->_db->query($sql);
  1318. } catch (Zend_Db_Exception $e) {
  1319. $this->_catchException($e, __METHOD__);
  1320. return false;
  1321. }
  1322. return true;
  1323. }
  1324. /**
  1325. * 获取图度关联用户
  1326. *
  1327. * @param string $tuduId
  1328. * @param array $filter
  1329. */
  1330. public function getUsers($tuduId, $filter = null)
  1331. {
  1332. $table = 'td_tudu_user u ';
  1333. $columns = 'u.unique_id as uniqueid, u.is_read as isread, u.is_forward as isforward, u.labels, u.role, u.is_foreign AS isforeign, '
  1334. . 'u.accepter_info AS accepterinfo, u.auth_code AS authcode, u.percent';
  1335. $where = array(
  1336. 'u.tudu_id = ' . $this->_db->quote($tuduId),
  1337. '(u.labels <> \'\' OR u.is_foreign = 1)'
  1338. );
  1339. if (isset($filter['role'])) {
  1340. $where[] = 'u.role = ' . $this->_db->quote($filter['role']);
  1341. }
  1342. if (isset($filter['isforeign'])) {
  1343. $where[] = 'u.is_foreign = ' . $this->_db->quote($filter['isforeign']);
  1344. }
  1345. if (isset($filter['uniqueid'])) {
  1346. if (is_array($filter['uniqueid'])) {
  1347. $arr = array_map(array($this->_db, 'quote'), $filter['uniqueid']);
  1348. $where[] = 'u.unique_id IN (' . implode(',', $arr) . ')';
  1349. } else {
  1350. $where[] = 'u.unique_id = ' . $this->_db->quote($filter['uniqueid']);
  1351. }
  1352. }
  1353. if (isset($filter['labelid'])) {
  1354. $table .= 'INNER JOIN td_tudu_label l ON u.tudu_id = l.tudu_id AND u.unique_id = l.unique_id AND l.label_id = '
  1355. . $this->_db->quote($filter['labelid']);
  1356. }
  1357. $sql = "SELECT {$columns} FROM {$table} WHERE " . implode(' AND ', $where);
  1358. $records = $this->_db->fetchAll($sql);
  1359. foreach ($records as &$record) {
  1360. $info = self::formatAddress($record['accepterinfo'], true);
  1361. $record['email'] = !empty($info[3]) ? $info[3] : null;
  1362. $record['truename'] = !empty($info[0]) ? $info[0] : null;
  1363. }
  1364. return $records;
  1365. }
  1366. /**
  1367. * 获取执行人
  1368. *
  1369. * @param string $tuduId
  1370. */
  1371. public function getAccepters($tuduId, $stepId = null)
  1372. {
  1373. $tuduId = $this->_db->quote($tuduId);
  1374. $table = 'td_tudu_user';
  1375. $columns = 'unique_id AS uniqueid, is_read AS isread, is_forward AS isforward, labels, role, percent, auth_code AS authcode, '
  1376. . 'accepter_info AS accepterinfo, tudu_status AS tudustatus, forward_info AS forwardinfo, accept_time AS accepttime, '
  1377. . 'is_foreign AS isforeign, '
  1378. . 'IF((accept_time IS NOT NULL OR tudu_status >= 2), (SELECT SUM(elapsed_time) FROM td_post WHERE tudu_id = ' . $tuduId . ' AND unique_id = td_tudu_user.unique_id), NULL) AS elapsedtime';
  1379. $where = 'tudu_id = ' . $tuduId . ' AND role = \'to\'';
  1380. if (null != $stepId) {
  1381. $where .= ' AND step_id = ' . $this->_db->quote($stepId);
  1382. }
  1383. $sql = "SELECT {$columns} FROM {$table} WHERE {$where}";
  1384. return $this->_db->fetchAll($sql);
  1385. }
  1386. /**
  1387. * 获取投递的用户信息
  1388. *
  1389. * @param $tuduId
  1390. * @param $uniqueId
  1391. */
  1392. public function getUser($tuduId, $uniqueId)
  1393. {
  1394. $columns = 'unique_id AS uniqueid, tudu_id AS tuduid, is_foreign AS isforeign, is_read AS isread, labels, role, '
  1395. . 'accepter_info AS accepterinfo, percent, tudu_status, accept_time, forward_info, auth_code AS authcode, '
  1396. . 'complete_time';
  1397. $table = 'td_tudu_user';
  1398. $where = 'tudu_id = ' . $this->_db->quote($tuduId) . ' AND unique_id = ' . $this->_db->quote($uniqueId);
  1399. $sql = "SELECT {$columns} FROM {$table} WHERE {$where} LIMIT 1";
  1400. try {
  1401. $record = $this->_db->fetchRow($sql);
  1402. if (!$record) {
  1403. return null;
  1404. }
  1405. $record['accepterinfo'] = self::formatAddress($record['accepterinfo'], true);
  1406. $record['email'] = !empty($record['accepterinfo'][3]) ? $record['accepterinfo'][3] : null;
  1407. $record['truename'] = !empty($record['accepterinfo'][0]) ? $record['accepterinfo'][0] : null;
  1408. return $record;
  1409. } catch (Zend_Db_Exception $e) {
  1410. $this->_catchException($e, __METHOD__);
  1411. return null;
  1412. }
  1413. }
  1414. /**
  1415. * 增加图度关联用户
  1416. *
  1417. * @param string $tuduId
  1418. * @param string $uniqueId
  1419. * @param array $params
  1420. * @return boolean | string
  1421. */
  1422. public function addUser($tuduId, $uniqueId, $params = null)
  1423. {
  1424. $table = "td_tudu_user";
  1425. $bind = array(
  1426. 'unique_id' => $uniqueId,
  1427. 'tudu_id' => $tuduId,
  1428. 'is_read' => empty($params['isread']) ? 0 : 1,
  1429. 'is_forward' => empty($params['isforward']) ? 0 : 1
  1430. );
  1431. if (isset($params['role'])) {
  1432. $bind['role'] = $params['role'];
  1433. }
  1434. if (isset($params['accepterinfo'])) {
  1435. $bind['accepter_info'] = $params['accepterinfo'];
  1436. }
  1437. if (isset($params['percent'])) {
  1438. $bind['percent'] = (int) $params['percent'];
  1439. }
  1440. if (isset($params['forwardinfo'])) {
  1441. $bind['forward_info'] = $params['forwardinfo'];
  1442. }
  1443. if (isset($params['tudustatus']) && is_int($params['tudustatus'])) {
  1444. $bind['tudu_status'] = $params['tudustatus'];
  1445. }
  1446. if (isset($params['isforeign'])) {
  1447. $bind['is_foreign'] = $params['isforeign'] ? 1 : 0;
  1448. }
  1449. if (isset($params['authcode'])) {
  1450. $bind['auth_code'] = $params['authcode'];
  1451. }
  1452. try {
  1453. $this->_db->insert($table, $bind);
  1454. } catch (Zend_Db_Exception $e) {
  1455. // 主键冲突
  1456. if (23000 === $e->getCode()) {
  1457. //return null;
  1458. $sql = 'SELECT labels FROM td_tudu_user WHERE unique_id = ' . $this->_db->quote($uniqueId) . ' '
  1459. . 'AND tudu_id = ' . $this->_db->quote($tuduId);
  1460. $record = $this->_db->fetchRow($sql);
  1461. if (!$record) {
  1462. return false;
  1463. }
  1464. return (string) $record['labels'];
  1465. }
  1466. $this->_catchException($e, __METHOD__);
  1467. return false;
  1468. }
  1469. return true;
  1470. }
  1471. /**
  1472. * 增加图度标签
  1473. *
  1474. * @param string $tuduId
  1475. * @param string $uniqueId
  1476. * @param string $labelId
  1477. * @return boolean
  1478. */
  1479. public function addLabel($tuduId, $uniqueId, $labelId)
  1480. {
  1481. $sql = "call sp_td_add_tudu_label("
  1482. . $this->_db->quote($tuduId) . ","
  1483. . $this->_db->quote($uniqueId) . ","
  1484. . $this->_db->quote($labelId) . ")";
  1485. try {
  1486. $this->_db->query($sql);
  1487. } catch (Zend_Db_Exception $e) {
  1488. // 主键冲突
  1489. if (23000 === $e->getCode()) {
  1490. return null;
  1491. }
  1492. $this->_catchException($e, __METHOD__);
  1493. return false;
  1494. }
  1495. return true;
  1496. }
  1497. /**
  1498. * 更新用户图度信息
  1499. *
  1500. * @param $tuduId
  1501. * @param $uniqueId
  1502. * @param $params
  1503. */
  1504. public function updateTuduUser($tuduId, $uniqueId, $params)
  1505. {
  1506. $table = "td_tudu_user";
  1507. $where = "unique_id = " . $this->_db->quote($uniqueId) . " AND tudu_id = " . $this->_db->quote($tuduId);
  1508. $bind = array();
  1509. if (isset($params['isread'])) {
  1510. $bind['is_read'] = $params['isread'] ? 1 : 0;
  1511. }
  1512. if (isset($params['isforward'])) {
  1513. $bind['is_forward'] = $params['isforward'] ? 1 : 0;
  1514. }
  1515. if (array_key_exists('accepterinfo', $params)) {
  1516. $bind['accepter_info'] = $params['accepterinfo'];
  1517. }
  1518. if (array_key_exists('percent', $params)) {
  1519. $bind['percent'] = $params['percent'];
  1520. }
  1521. if (array_key_exists('tudustatus', $params)) {
  1522. $bind['tudu_status'] = $params['tudustatus'];
  1523. }
  1524. if (array_key_exists('accepttime', $params)) {
  1525. $bind['accept_time'] = $params['accepttime'];
  1526. }
  1527. if (array_key_exists('completetime', $params)) {
  1528. $bind['complete_time'] = $params['completetime'];
  1529. }
  1530. if (array_key_exists('role', $params)) {
  1531. $bind['role'] = $params['role'];
  1532. }
  1533. if (array_key_exists('isforeign', $params)) {
  1534. $bind['is_foreign'] = $params['isforeign'] ? 1 : 0;
  1535. }
  1536. if (isset($params['forwardinfo'])) {
  1537. $bind['forward_info'] = $params['forwardinfo'];
  1538. }
  1539. if (array_key_exists('authcode', $params)) {
  1540. $bind['auth_code'] = $params['authcode'];
  1541. }
  1542. if (isset($params['mark'])) {
  1543. $bind['mark'] = $params['mark'] ? 1 : 0;
  1544. }
  1545. if (empty($bind)) {
  1546. return false;
  1547. }
  1548. try {
  1549. $this->_db->update($table, $bind, $where);
  1550. } catch (Zend_Db_Exception $e) {
  1551. $this->_catchException($e, __METHOD__);
  1552. return false;
  1553. }
  1554. return true;
  1555. }
  1556. /**
  1557. * 更新图度的标签标记
  1558. *
  1559. * @param $tuduId
  1560. * @param $uniqueId
  1561. * @param $labels
  1562. */
  1563. public function updateTuduLabels($tuduId, $uniqueId, $labels = null)
  1564. {
  1565. if (null === $labels) {
  1566. $sql = "call sp_td_update_tudu_labels(" . $this->_db->quote($tuduId) . "," . $this->_db->quote($uniqueId) . ")";
  1567. } else {
  1568. $sql = "UPDATE td_tudu_user SET labels = " . $this->_db->quote($labels)
  1569. . " WHERE unique_id = " . $this->_db->quote($uniqueId)
  1570. . " AND tudu_id = " . $this->_db->quote($tuduId);
  1571. }
  1572. try {
  1573. $this->_db->query($sql);
  1574. } catch (Zend_Db_Exception $e) {
  1575. $this->_catchException($e, __METHOD__);
  1576. return false;
  1577. }
  1578. return true;
  1579. }
  1580. /**
  1581. * 删除图度用户
  1582. *
  1583. * @param string $tuduId
  1584. * @param string $uniqueId
  1585. */
  1586. public function deleteUser($tuduId, $uniqueId)
  1587. {
  1588. $sql = "DELETE FROM td_tudu_user"
  1589. . " WHERE unique_id = " . $this->_db->quote($uniqueId)
  1590. . " AND tudu_id = " . $this->_db->quote($tuduId);
  1591. try {
  1592. $this->_db->query($sql);
  1593. } catch (Zend_Db_Exception $e) {
  1594. $this->_catchException($e, __METHOD__);
  1595. return false;
  1596. }
  1597. return true;
  1598. }
  1599. /**
  1600. * 移除接受人信息
  1601. *
  1602. * @param $tuduId
  1603. * @param $uniqueId
  1604. */
  1605. public function removeAccepter($tuduId, $uniqueId)
  1606. {
  1607. return $this->updateTuduUser($tuduId, $uniqueId, array(
  1608. 'role' => null,
  1609. 'percent' => null,
  1610. 'tudustatus' => null,
  1611. 'accepttime' => null,
  1612. 'forwardinfo' => null
  1613. ));
  1614. }
  1615. /**
  1616. * 删除图度标签
  1617. *
  1618. * @param string $tuduId
  1619. * @param string $uniqueId
  1620. * @param string $labelId
  1621. * @return boolean
  1622. */
  1623. public function deleteLabel($tuduId, $uniqueId, $labelId)
  1624. {
  1625. $sql = "call sp_td_delete_tudu_label("
  1626. . $this->_db->quote($tuduId) . ","
  1627. . $this->_db->quote($uniqueId) . ","
  1628. . $this->_db->quote($labelId) . ")";
  1629. try {
  1630. $this->_db->query($sql);
  1631. } catch (Zend_Db_Exception $e) {
  1632. $this->_catchException($e, __METHOD__);
  1633. return false;
  1634. }
  1635. return true;
  1636. }
  1637. /**
  1638. * 递增浏览次数
  1639. *
  1640. * @param $tuduId
  1641. * @return boolean
  1642. */
  1643. public function hit($tuduId)
  1644. {
  1645. $sql = "UPDATE td_tudu SET view_num = view_num + 1 WHERE tudu_id = " . $this->_db->quote($tuduId);
  1646. try {
  1647. $this->_db->query($sql);
  1648. } catch (Zend_Db_Exception $e) {
  1649. $this->_catchException($e, __METHOD__);
  1650. return false;
  1651. }
  1652. return true;
  1653. }
  1654. /**
  1655. *
  1656. * @param $tuduId
  1657. * @param $uniqueId
  1658. * @return int 返回图度状态
  1659. */
  1660. public function rejectTudu($tuduId, $uniqueId, $isFlow = false)
  1661. {
  1662. if (empty($tuduId) || empty($uniqueId)) {
  1663. return false;
  1664. }
  1665. $tuduId = $this->_db->quote($tuduId);
  1666. $uniqueId = $this->_db->quote($uniqueId);
  1667. $table = 'td_tudu_user';
  1668. $bind = array(
  1669. 'tudu_status' => self::STATUS_REJECT,
  1670. 'accept_time' => null
  1671. );
  1672. $where = "tudu_id = {$tuduId} AND unique_id = {$uniqueId}";
  1673. try {
  1674. $this->_db->update($table, $bind, $where);
  1675. if ($isFlow) {
  1676. $status = self::STATUS_REJECT;
  1677. $this->_db->query("UPDATE td_tudu SET status = {$status} WHERE tudu_id = {$tuduId}");
  1678. } else {
  1679. $sql = "SELECT AVG(tudu_status) AS status, AVG(percent) AS percent FROM td_tudu_user WHERE tudu_id = {$tuduId} AND role = 'to' AND tudu_status <> 3";
  1680. $record = $this->_db->fetchRow($sql);
  1681. $status = $record['status'];
  1682. $percent = (int) $record['percent'];
  1683. if ('' === trim($status)) {
  1684. $status = self::STATUS_REJECT;
  1685. } else {
  1686. $status = (float) $status > 1 ? (int) $status : ($status > 0 ? 1 : 0);
  1687. }
  1688. $this->_db->query("UPDATE td_tudu SET status = {$status}, percent = {$percent} WHERE tudu_id = {$tuduId}");
  1689. }
  1690. } catch (Zend_Db_Exception $e) {
  1691. $this->_catchException($e, __METHOD__);
  1692. return false;
  1693. }
  1694. return $status;
  1695. }
  1696. /**
  1697. * 标志已读状态
  1698. *
  1699. * @param string $tuduId
  1700. * @param string $uniqueId
  1701. * @param boolean $isRead
  1702. */
  1703. public function markRead($tuduId, $uniqueId, $isRead = true)
  1704. {
  1705. $tuduId = $this->_db->quote($tuduId);
  1706. $uniqueId = $this->_db->quote($uniqueId);
  1707. if ($isRead) {
  1708. $sql = 'call sp_td_mark_read(' . $tuduId . ',' . $uniqueId . ')';
  1709. } else {
  1710. $sql = 'call sp_td_mark_unread(' . $tuduId . ',' . $uniqueId . ')';
  1711. }
  1712. try {
  1713. $this->_db->query($sql);
  1714. } catch (Zend_Db_Exception $e) {
  1715. $this->_catchException($e, __METHOD__);
  1716. return false;
  1717. }
  1718. return true;
  1719. }
  1720. /**
  1721. * 设置标签下所有图度为已读
  1722. * 执行后必须重新统计标签的未读数
  1723. *
  1724. * @param string $labelId
  1725. * @param string $uniqueId
  1726. * @param boolean $isRead
  1727. */
  1728. public function markLabelRead($labelId, $uniqueId, $isRead = true)
  1729. {
  1730. $labelId = $this->_db->quote($labelId);
  1731. $uniqueId = $this->_db->quote($uniqueId);
  1732. $isRead = $isRead ? 1 : 0;
  1733. $sql = "UPDATE td_tudu_user SET is_read = {$isRead} WHERE unique_id = {$uniqueId} "
  1734. . "AND tudu_id IN (SELECT tudu_id FROM td_tudu_label WHERE unique_id = {$uniqueId} AND label_id = {$labelId})";
  1735. try {
  1736. $this->_db->query($sql);
  1737. } catch (Zend_Db_Exception $e) {
  1738. $this->_catchException($e, __METHOD__);
  1739. return false;
  1740. }
  1741. return true;
  1742. }
  1743. /**
  1744. * 设置所有关联用户为未读状态
  1745. *
  1746. * @param $tuduId
  1747. * @return boolean
  1748. */
  1749. public function markAllUnRead($tuduId)
  1750. {
  1751. $tuduId = $this->_db->quote($tuduId);
  1752. $sql = 'call sp_td_mark_all_unread(' . $tuduId . ')';
  1753. try {
  1754. $this->_db->query($sql);
  1755. } catch (Zend_Db_Exception $e) {
  1756. $this->_catchException($e, __METHOD__);
  1757. return false;
  1758. }
  1759. return true;
  1760. }
  1761. /**
  1762. * 标志为转发状态
  1763. *
  1764. * @param string $tuduId
  1765. * @param string $uniqueId
  1766. */
  1767. public function markForward($tuduId, $uniqueId)
  1768. {
  1769. $sql = "UPDATE td_tudu_user"
  1770. . " SET is_forward = 1"
  1771. . " WHERE unique_id = " . $this->_db->quote($uniqueId)
  1772. . " AND tudu_id = " . $this->_db->quote($tuduId);
  1773. try {
  1774. $this->_db->query($sql);
  1775. } catch (Zend_Db_Exception $e) {
  1776. $this->_catchException($e, __METHOD__);
  1777. return false;
  1778. }
  1779. return true;
  1780. }
  1781. /**
  1782. * 接收人更新任务进度
  1783. *
  1784. * @param $tuduId
  1785. * @param $uniqueId
  1786. * @param $percent
  1787. */
  1788. public function updateProgress($tuduId, $uniqueId, $percent)
  1789. {
  1790. $tuduId = $this->_db->quote($tuduId);
  1791. $uniqueId = $this->_db->quote($uniqueId);
  1792. $percent = null !== $percent ? min(100, abs((int) $percent)) : 'NULL';
  1793. $sql = "call sp_td_update_tudu_progress({$tuduId}, {$uniqueId}, {$percent})";
  1794. try {
  1795. $totalPercent = (int) $this->_db->fetchOne($sql);
  1796. } catch (Zend_Db_Exception $e) {
  1797. $this->_catchException($e, __METHOD__);
  1798. return false;
  1799. }
  1800. return $totalPercent;
  1801. }
  1802. /**
  1803. * 更新工作流进度
  1804. *
  1805. * @param $tuduId
  1806. * @param $uniqueId
  1807. * @param $percent
  1808. */
  1809. public function updateFlowProgress($tuduId, $uniqueId, $stepId, $percent = null, &$flowPercent = null)
  1810. {
  1811. if ($stepId == '^head' || $stepId == '^break') {
  1812. return true;
  1813. }
  1814. $sql = "SELECT steps FROM td_tudu_flow WHERE tudu_id = :tuduid";
  1815. $steps = $this->_db->fetchRow($sql, array('tuduid' => $tuduId));
  1816. if (!$steps) {
  1817. return ;
  1818. }
  1819. $steps = json_decode($steps['steps'], true);
  1820. $count = count($steps);
  1821. $currentStep = null;
  1822. $done = 0;
  1823. $type = 0;
  1824. foreach ($steps as $step) {
  1825. if ($step['stepid'] == $stepId) {
  1826. $type = $step['type'];
  1827. break;
  1828. }
  1829. $done ++;
  1830. }
  1831. $avg = 100 / $count;
  1832. $base = $avg * $done;
  1833. if (0 == $type) {
  1834. if (null != $uniqueId && is_int($percent)) {
  1835. if ($percent == 100) {
  1836. $this->_db->query('UPDATE td_tudu_user SET percent = :percent, tudu_status = :status, complete_time = :completetime WHERE tudu_id = :tuduid AND unique_id = :uniqueid', array(
  1837. 'percent' => $percent,
  1838. 'status' => 2,
  1839. 'completetime' => time(),
  1840. 'tuduid' => $tuduId,
  1841. 'uniqueid' => $uniqueId
  1842. ));
  1843. } else {
  1844. $this->_db->query('UPDATE td_tudu_user SET percent = :percent WHERE tudu_id = :tuduid AND unique_id = :uniqueid', array(
  1845. 'percent' => $percent,
  1846. 'tuduid' => $tuduId,
  1847. 'uniqueid' => $uniqueId
  1848. ));
  1849. }
  1850. }
  1851. // 步骤总体进度
  1852. $sql = "SELECT AVG(percent) FROM td_tudu_user WHERE tudu_id = :tuduid AND role = 'to' AND (tudu_status IS NULL OR tudu_status < 3)";
  1853. $percent = (int) $this->_db->fetchOne($sql, array('tuduid' => $tuduId));
  1854. $flowPercent = $base + ($avg * $percent / 100);
  1855. } elseif ($type == 1) {
  1856. $users = $currentStep['section'][$currentStep['currentSection']];
  1857. if (count($users)) {
  1858. $avgStep = $avg / count($users);
  1859. $acceptCount = 0;
  1860. foreach ($users as $user) {
  1861. if ($user['status'] == 2) {
  1862. $acceptCount ++;
  1863. }
  1864. }
  1865. } else {
  1866. $avgStep = 1;
  1867. $acceptCount = 0;
  1868. }
  1869. $flowPercent = $base + ($avgStep * $acceptCount);
  1870. } else {
  1871. $flowPercent = $base;
  1872. }
  1873. $params = array();
  1874. $params['percent'] = min(100, $flowPercent);
  1875. if ((int) $params['percent'] == 100) {
  1876. $params['status'] = 2;
  1877. $params['complete_time'] = time();
  1878. }
  1879. $this->_db->update('td_tudu', $params, 'tudu_id = ' . $this->_db->quote($tuduId));
  1880. $flowPercent = $params['percent'];
  1881. return $percent;
  1882. }
  1883. /**
  1884. * 更新最后接受时间(更新非接收人看到的图度接受状态)
  1885. *
  1886. * @param string $tuduId
  1887. */
  1888. public function updateLastAcceptTime($tuduId)
  1889. {
  1890. $tuduId = $this->_db->quote($tuduId);
  1891. $sql = "UPDATE td_tudu SET accept_time = (SELECT MAX(accept_time) FROM td_tudu_user WHERE tudu_id = {$tuduId}) "
  1892. . "WHERE tudu_id = {$tuduId}";
  1893. try {
  1894. $this->_db->query($sql);
  1895. } catch (Zend_Db_Exception $e) {
  1896. return false;
  1897. }
  1898. return true;
  1899. }
  1900. /**
  1901. * 计算父级图度的进度
  1902. *
  1903. * @param $tuduId
  1904. */
  1905. public function calParentsProgress($tuduId)
  1906. {
  1907. $tuduId = $this->_db->quote($tuduId);
  1908. $sql = "call sp_td_calculate_parents_progress({$tuduId})";
  1909. try {
  1910. $this->_db->query($sql);
  1911. } catch (Zend_Db_Exception $e) {
  1912. $this->_catchException($e, __METHOD__);
  1913. return false;
  1914. }
  1915. return true;
  1916. }
  1917. /**
  1918. * 计算图度已耗时
  1919. *
  1920. * @param $tuduId
  1921. * @return boolean
  1922. */
  1923. public function calcElapsedTime($tuduId)
  1924. {
  1925. $tuduId = $this->_db->quote($tuduId);
  1926. $sql = 'call sp_td_calculate_tudu_elapsed_time(' . $tuduId . ')';
  1927. try {
  1928. $this->_db->query($sql);
  1929. } catch (Zend_Db_Exception $e) {
  1930. $this->_catchException($e, __METHOD__);
  1931. return false;
  1932. }
  1933. return true;
  1934. }
  1935. /**
  1936. * Get tudu by id
  1937. *
  1938. * @param string $tuduId
  1939. * @param string $uniqueId
  1940. * @param array $filter
  1941. * @return Dao_Td_Tudu_Record_Tudu
  1942. */
  1943. public function getTuduById($uniqueId, $tuduId, $filter = null)
  1944. {
  1945. return $this->getTudu(array('uniqueid' => $uniqueId, 'tuduid' => $tuduId), $filter);
  1946. }
  1947. /**
  1948. * 格式化地址
  1949. *
  1950. * 地址以 “邮箱[空格]姓名[换行]邮箱[空格]姓名”格式保存
  1951. * 返回格式
  1952. * array(
  1953. * address1 => array(name1, userid1, domain1, address1, extend1),
  1954. * address2 => array(name2, userid2, domain2, address2, extend2),
  1955. * address3 => array(name3, userid3, domain3, address3, extend3)
  1956. * )
  1957. *
  1958. * @param string $address
  1959. * @param boolean $first 是否仅返回第一条记录,如果发件人仅有一个
  1960. * @return array
  1961. */
  1962. public static function formatAddress($address, $first = false)
  1963. {
  1964. $ret = array();
  1965. $pattern = "/(([\^]?[\w-\.\_]+)(?:@([^ ]+))?)? ([^\n]*)/";
  1966. if ($first) {
  1967. preg_match($pattern, $address, $matches);
  1968. if ($matches) {
  1969. $ret = array($matches[4], $matches[2], $matches[3], $matches[1]);
  1970. }
  1971. } else {
  1972. preg_match_all($pattern, $address, $matches);
  1973. for ($i = 0; $i < count($matches[0]); $i++) {
  1974. if ($matches[1][$i]) {
  1975. $ret[$matches[1][$i]] = array($matches[4][$i], $matches[2][$i], $matches[3][$i], $matches[1][$i]);
  1976. } else {
  1977. $ret[] = array($matches[4][$i], $matches[2][$i], $matches[3][$i], $matches[1][$i]);
  1978. }
  1979. }
  1980. }
  1981. return $ret;
  1982. }
  1983. /**
  1984. * 获取图度ID
  1985. *
  1986. * 生成规则:微秒级时间戳转16位 + 0xfffff最大值随机数转16位
  1987. * 格式如 129fcd77e2043a86,类似gmail生成格式
  1988. *
  1989. * @return string
  1990. */
  1991. public static function getTuduId()
  1992. {
  1993. $tuduId = base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
  1994. return $tuduId;
  1995. }
  1996. }