PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Dao/Td/Note/Note.php

https://github.com/polokk/tudu-web-1
PHP | 322 lines | 204 code | 49 blank | 69 comment | 47 complexity | bdec1709ee25c2712fd30595625d520b MD5 | raw file
  1. <?php
  2. /**
  3. * Tudu Dao
  4. *
  5. * LICENSE
  6. *
  7. *
  8. * @category Dao
  9. * @package Dao_Td
  10. * @subpackage Note
  11. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  12. * @link http://www.oray.com/
  13. * @version $Id: Note.php 2733 2013-01-31 01:41:03Z cutecube $
  14. */
  15. /**
  16. * @category Dao
  17. * @package Dao_Td
  18. * @subpackage Note
  19. * @copyright Copyright (c) 2009-2010 Shanghai Best Oray Information S&T CO., Ltd.
  20. */
  21. class Dao_Td_Note_Note extends Oray_Dao_Abstract
  22. {
  23. /**
  24. *
  25. * @param array $condition
  26. * @param array $filter
  27. * @return Dao_Td_Note_Record_Note
  28. */
  29. public function getNote(array $condition, $filter = null)
  30. {
  31. $table = 'td_note';
  32. $columns = 'org_id AS orgid, unique_id AS uniqueid, note_id AS noteid, tudu_id AS tuduid, content, color, status, '
  33. . 'create_time AS createtime, update_time AS updatetime';
  34. $where = array();
  35. if (isset($condition['noteid'])) {
  36. $where[] = 'note_id = ' . $this->_db->quote($condition['noteid']);
  37. }
  38. if (isset($condition['uniqueid'])) {
  39. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  40. }
  41. if (isset($condition['tuduid'])) {
  42. $where[] = 'tudu_id = ' . $this->_db->quote($condition['tuduid']);
  43. }
  44. if (!$where) {
  45. return null;
  46. }
  47. if (is_array($filter) && array_key_exists('status', $filter)) {
  48. if (null !== $filter['status']) {
  49. $where[] = 'status = ' . (int) $filter['status'];
  50. }
  51. } else {
  52. $where[] = 'status = 1';
  53. }
  54. // WHERE
  55. $where = implode(' AND ', $where);
  56. $sql = "SELECT {$columns} FROM {$table} WHERE {$where} LIMIT 1";
  57. try {
  58. $record = $this->_db->fetchRow($sql);
  59. if (!$record) {
  60. return null;
  61. }
  62. return Oray_Dao::record('Dao_Td_Note_Record_Note', $record);
  63. } catch (Zend_Db_Exception $e) {
  64. $this->_catchExecption($e, __METHOD__);
  65. return null;
  66. }
  67. }
  68. /**
  69. *
  70. * @param array $condition
  71. * @param array $filter
  72. * @param mixed $sort
  73. * @param int $maxCount
  74. * @return Oray_Dao_Recordset
  75. */
  76. public function getNotes(array $condition, $filter = null, $sort = null, $maxCount = null)
  77. {
  78. $table = 'td_note AS n '
  79. . 'LEFT JOIN td_tudu AS t ON n.tudu_id = t.tudu_id';
  80. $columns = 'n.org_id AS orgid, n.unique_id AS uniqueid, n.note_id AS noteid, n.content, n.color, n.status, '
  81. . 'n.tudu_id AS tuduid, t.subject, n.create_time AS createtime, n.update_time AS updatetime';
  82. $where = array();
  83. $order = array();
  84. $limit = '';
  85. if (isset($condition['orgid'])) {
  86. $where[] = 'n.org_id = ' . $this->_db->quote($condition['orgid']);
  87. }
  88. if (isset($condition['noteid'])) {
  89. $where[] = 'n.note_id = ' . $this->_db->quote($condition['noteid']);
  90. }
  91. if (isset($condition['uniqueid'])) {
  92. $where[] = 'n.unique_id = ' . $this->_db->quote($condition['uniqueid']);
  93. }
  94. if (!$where) {
  95. return null;
  96. }
  97. if (is_array($filter) && array_key_exists('status', $filter)) {
  98. if (null !== $filter['status']) {
  99. $where[] = 'n.status = ' . (int) $filter['status'];
  100. }
  101. } else {
  102. $where[] = 'n.status = 1';
  103. }
  104. // WHERE
  105. $where = implode(' AND ', $where);
  106. if ($where) {
  107. $where = 'WHERE ' . $where;
  108. }
  109. $sort = $this->_formatSort($sort);
  110. foreach ($sort as $key => $val) {
  111. switch ($key) {
  112. case 'updatetime':
  113. $key = 'n.update_time';
  114. break;
  115. case 'createtime':
  116. $key = 'n.create_time';
  117. break;
  118. default:
  119. continue 2;
  120. }
  121. $order[] = $key . ' ' . $val;
  122. }
  123. // ORDER
  124. $order = implode(', ', $order);
  125. if ($order) {
  126. $order = 'ORDER BY ' . $order;
  127. }
  128. // LIMIT
  129. if (is_int($maxCount) && $maxCount > 0) {
  130. $limit = 'LIMIT ' . $maxCount;
  131. }
  132. $sql = "SELECT {$columns} FROM {$table} {$where} {$order} {$limit}";
  133. try {
  134. $records = $this->_db->fetchAll($sql);
  135. return new Oray_Dao_Recordset($records, 'Dao_Td_Note_Record_Note');
  136. } catch (Zend_Db_Exception $e) {
  137. $this->_catchExecption($e, __METHOD__);
  138. return new Oray_Dao_Recordset();
  139. }
  140. }
  141. /**
  142. * getNotes 的快捷调用方式
  143. *
  144. * @param string $uniqueId
  145. * @param array $filter
  146. * @param mixed $sort
  147. * @param int $maxCount
  148. * @return Oray_Dao_Recordset
  149. */
  150. public function getNotesByUniqueId($uniqueId, $filter = null, $sort = null, $maxCount = null)
  151. {
  152. return $this->getNotes(array('uniqueid' => $uniqueId), $filter, $sort, $maxCount);
  153. }
  154. /**
  155. *
  156. * @param array $params
  157. */
  158. public function createNote(array $params)
  159. {
  160. if (empty($params['orgid'])
  161. || empty($params['uniqueid'])
  162. || empty($params['noteid']))
  163. {
  164. return false;
  165. }
  166. $table = 'td_note';
  167. $bind = array(
  168. 'org_id' => $params['orgid'],
  169. 'unique_id' => $params['uniqueid'],
  170. 'note_id' => $params['noteid']
  171. );
  172. if (!empty($params['tuduid'])) {
  173. $bind['tudu_id'] = $params['tuduid'];
  174. }
  175. if (!empty($params['content'])) {
  176. $bind['content'] = $params['content'];
  177. }
  178. if (isset($params['color']) && is_int($params['color'])) {
  179. $bind['color'] = $params['color'];
  180. }
  181. if (isset($params['status']) && is_int($params['status'])) {
  182. $bind['status'] = $params['status'];
  183. }
  184. if (isset($params['createtime']) && is_int($params['createtime'])) {
  185. $bind['update_time'] = $bind['create_time'] = $params['createtime'];
  186. }
  187. try {
  188. $this->_db->insert($table, $bind);
  189. } catch (Zend_Db_Exception $e) {
  190. $this->_catchException($e, __METHOD__);
  191. return false;
  192. }
  193. return $params['noteid'];
  194. }
  195. /**
  196. * 更新便签数据
  197. *
  198. * @param string $noteId
  199. * @param string $uniqueId
  200. * @param array $params
  201. * @return boolean
  202. */
  203. public function updateNote($noteId, $uniqueId, array $params, $tuduId = null)
  204. {
  205. if (empty($noteId) || empty($uniqueId)) {
  206. return false;
  207. }
  208. $table = 'td_note';
  209. $bind = array();
  210. if (array_key_exists('content', $params)) {
  211. $bind['content'] = $params['content'];
  212. }
  213. if (array_key_exists('color', $params)) {
  214. $bind['color'] = $params['color'];
  215. }
  216. if (isset($params['status']) && is_int($params['status'])) {
  217. $bind['status'] = $params['status'];
  218. }
  219. if (isset($params['updatetime']) && is_int($params['updatetime'])) {
  220. $bind['update_time'] = $params['updatetime'];
  221. }
  222. if (!$bind) {
  223. return false;
  224. }
  225. try {
  226. $where = 'note_id = ' . $this->_db->quote($noteId) . ' AND unique_id = ' . $this->_db->quote($uniqueId);
  227. if (!empty($tuduId)) {
  228. $where .= ' AND tudu_id = ' . $this->_db->quote($tuduId);
  229. }
  230. $this->_db->update($table, $bind, $where);
  231. } catch (Zend_Db_Exception $e) {
  232. $this->_catchException($e, __METHOD__);
  233. return false;
  234. }
  235. return true;
  236. }
  237. /**
  238. * 删除便签
  239. *
  240. * @param string $noteId
  241. * @param string $uniqueId
  242. */
  243. public function deleteNote($noteId, $uniqueId)
  244. {
  245. if (empty($noteId) || empty($uniqueId)) {
  246. return false;
  247. }
  248. $sql = 'DELETE FROM td_note WHERE unique_id = ' . $this->_db->quote($uniqueId);
  249. if (is_array($noteId)) {
  250. $noteId = array_map(array($this->_db, 'quote'), $noteId);
  251. $sql .= ' AND note_id IN (' . implode(',', $noteId) . ')';
  252. } else {
  253. $sql .= ' AND note_id = ' . $this->_db->quote($noteId);
  254. }
  255. try {
  256. $this->_db->query($sql);
  257. } catch (Zend_Db_Exception $e) {
  258. $this->_catchException($e, __METHOD__);
  259. return false;
  260. }
  261. return true;
  262. }
  263. /**
  264. * 生成便签ID
  265. *
  266. * @return string
  267. */
  268. public static function getNoteId()
  269. {
  270. $noteId = base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
  271. return $noteId;
  272. }
  273. }