PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/polokk/tudu-web-1
PHP | 551 lines | 336 code | 76 blank | 139 comment | 60 complexity | 243cccf7b7a336d5243b090732e6f664 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: Label.php 2590 2012-12-31 10:04:53Z 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_Label extends Oray_Dao_Abstract
  22. {
  23. /**
  24. * Initialize object
  25. *
  26. * Called from {@link __construct()} as final step of object instantiation.
  27. *
  28. * @return void
  29. */
  30. public function init()
  31. {}
  32. /**
  33. * Get labels
  34. *
  35. * SEELCT unique_id AS uniqueid, label_id AS labelid, label_alias AS labelalias,
  36. * total_num AS total_num, unread_num AS unreadnum,
  37. * is_system AS issystem, color, bgcolor, sync_time AS synctime
  38. * FORM td_label
  39. * WHERE unique_id = ?
  40. * ORDER BY label_name ASC
  41. *
  42. * @param array $condition
  43. * @param array $filter
  44. * @param array $sort
  45. * @param int $maxCount
  46. * @return Oray_Dao_Recordset
  47. */
  48. public function getLabels(array $condition, $filter = null, $sort = null, $maxCount = null)
  49. {
  50. $table = "td_label";
  51. $columns = "unique_id AS uniqueid, label_id AS labelid, label_alias AS labelalias, "
  52. . "total_num AS totalnum, unread_num AS unreadnum, is_show AS isshow, display, "
  53. . "is_system AS issystem, color, bgcolor, sync_time AS synctime, order_num AS ordernum";
  54. $where = array();
  55. $order = array();
  56. $limit = "";
  57. // $condition ...
  58. if (isset($condition['uniqueid'])) {
  59. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  60. }
  61. if (isset($filter['issystem'])) {
  62. $where[] = 'is_system = ' . $filter['issystem'] ? 1 : 0;
  63. }
  64. if (empty($where)) {
  65. return new Oray_Dao_Recordset();
  66. }
  67. // $filter ...
  68. // WHERE
  69. $where = implode(' AND ', $where);
  70. if ($where) {
  71. $where = 'WHERE ' . $where;
  72. }
  73. $sort = $this->_formatSort($sort);
  74. foreach ($sort as $key => $val) {
  75. switch ($key) {
  76. case 'alias':
  77. $key = 'label_alias';
  78. break;
  79. case 'issystem':
  80. $key = 'is_system';
  81. break;
  82. case 'ordernum':
  83. $key = 'order_num';
  84. break;
  85. default:
  86. continue 2;
  87. }
  88. $order[] = $key . ' ' . $val;
  89. }
  90. // ORDER
  91. $order = implode(', ', $order);
  92. if ($order) {
  93. $order = 'ORDER BY ' . $order;
  94. }
  95. // LIMIT
  96. if (is_int($maxCount) && $maxCount > 0) {
  97. $limit = 'LIMIT ' . $maxCount;
  98. }
  99. $sql = "SELECT $columns FROM $table $where $order $limit";
  100. $records = $this->_db->fetchAll($sql);
  101. return new Oray_Dao_Recordset($records, 'Dao_Td_Tudu_Record_Label');
  102. }
  103. /**
  104. *
  105. * @param array $condition
  106. * @param array $filter
  107. */
  108. public function getLabel(array $condition, $filter = null)
  109. {
  110. $table = "td_label";
  111. $columns = "unique_id AS uniqueid, label_id AS labelid, label_alias AS labelalias, "
  112. . "total_num AS totalnum, unread_num AS unreadnum, is_show AS isshow, "
  113. . "is_system AS issystem, color, bgcolor, sync_time AS synctime, order_num AS ordernum";
  114. $where = array();
  115. // $condition ...
  116. if (isset($condition['uniqueid'])) {
  117. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  118. }
  119. if (isset($filter['labelid'])) {
  120. $where[] = 'label_id = ' . $this->_db->quote($filter['labelid']);
  121. }
  122. if (empty($where)) {
  123. return null;
  124. }
  125. $where = 'WHERE ' . implode(' AND ', $where);
  126. $sql = "SELECT $columns FROM $table $where LIMIT 1";
  127. try {
  128. $record = $this->_db->fetchRow($sql);
  129. if (!$record) {
  130. return null;
  131. }
  132. return Oray_Dao::record('Dao_Td_Tudu_Record_Label', $record);
  133. } catch (Zend_Db_Exception $e) {
  134. $this->_catchException($e, __METHOD__);
  135. return null;
  136. }
  137. }
  138. /**
  139. * Get labels
  140. *
  141. * @param string $uniqueId
  142. * @param array $filter
  143. * @param array $sort
  144. * @param int $maxCount
  145. * @return Oray_Dao_Recordset
  146. */
  147. public function getLabelsByUniqueId($uniqueId, $filter = null, $sort = 'alias ASC', $maxCount = null)
  148. {
  149. return $this->getLabels(array('uniqueid' => $uniqueId), $filter, $sort, $maxCount);
  150. }
  151. /**
  152. *
  153. * @param array $condition
  154. * @return int
  155. */
  156. public function getLabelCount(array $condition)
  157. {
  158. $table = 'td_label';
  159. $where = array();
  160. if (!empty($condition['uniqueid'])) {
  161. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  162. }
  163. if (isset($condition['issystem'])) {
  164. $where[] = 'is_system = ' . ($condition['issystem'] ? 1 : 0);
  165. }
  166. if (!$where) {
  167. return false;
  168. }
  169. $where = implode(' AND ', $where);
  170. $sql = "SELECT COUNT(0) FROM {$table} WHERE {$where}";
  171. $count = (int) $this->_db->fetchOne($sql);
  172. return $count;
  173. }
  174. /**
  175. * Create label
  176. *
  177. * @param $params
  178. * @return int|false
  179. */
  180. public function createLabel(array $params)
  181. {
  182. if (empty($params['uniqueid'])
  183. || !isset($params['labelid'])
  184. || !isset($params['labelalias'])) {
  185. return false;
  186. }
  187. $table = "td_label";
  188. $bind = array(
  189. 'unique_id' => $params['uniqueid'],
  190. 'label_id' => $params['labelid'],
  191. 'label_alias' => $params['labelalias'],
  192. 'is_system' => empty($params['issystem']) ? 0 : 1
  193. );
  194. if (isset($params['isshow'])) {
  195. $bind['is_show'] = $params['isshow'];
  196. }
  197. if (isset($params['color'])) {
  198. $bind['color'] = $params['color'];
  199. }
  200. if (isset($params['bgcolor'])) {
  201. $bind['bgcolor'] = $params['bgcolor'];
  202. }
  203. if (isset($params['ordernum']) && is_int($params['ordernum'])) {
  204. $bind['order_num'] = $params['ordernum'];
  205. }
  206. if (isset($params['display']) && is_int($params['display'])) {
  207. $bind['display'] = $params['display'];
  208. }
  209. try {
  210. $this->_db->insert($table, $bind);
  211. } catch (Zend_Db_Exception $e) {
  212. $this->_catchException($e, __METHOD__);
  213. return false;
  214. }
  215. return true;
  216. }
  217. /**
  218. * Update label
  219. *
  220. * @param string $uniqueId
  221. * @param string $labelId
  222. * @param array $params
  223. * @return boolean
  224. */
  225. public function updateLabel($uniqueId, $labelId, array $params)
  226. {
  227. // 系统标签禁止修改
  228. if (!is_string($labelId) || $labelId[0] === '^') {
  229. return false;
  230. }
  231. $table = "td_label";
  232. $bind = array();
  233. $where = "unique_id = " . $this->_db->quote($uniqueId)
  234. . " AND label_id = " . $this->_db->quote($labelId);
  235. if (isset($params['labelalias'])) {
  236. $bind['label_alias'] = $params['labelalias'];
  237. }
  238. if (isset($params['isshow'])) {
  239. $bind['is_show'] = $params['isshow'];
  240. }
  241. if (isset($params['color'])) {
  242. $bind['color'] = $params['color'];
  243. }
  244. if (isset($params['bgcolor'])) {
  245. $bind['bgcolor'] = $params['bgcolor'];
  246. }
  247. if (isset($params['ordernum']) && is_int($params['ordernum'])) {
  248. $bind['order_num'] = $params['ordernum'];
  249. }
  250. if (!$bind) {
  251. return false;
  252. }
  253. try {
  254. $this->_db->update($table, $bind, $where);
  255. } catch (Zend_Db_Exception $e) {
  256. $this->_catchException($e, __METHOD__);
  257. return false;
  258. }
  259. return true;
  260. }
  261. /**
  262. * 更新标签显示方式
  263. *
  264. * @param string $uniqueId
  265. * @param string $labelId
  266. * @param string $type
  267. * @return boolean
  268. */
  269. public function showLabel($uniqueId, $labelId, $type)
  270. {
  271. //所有图度、图度箱不允许此操作
  272. if ($labelId == '^all' || $labelId == '^i') {
  273. return false;
  274. }
  275. $table = "td_label";
  276. $bind = array('is_show' => $type);
  277. $where = "unique_id = " . $this->_db->quote($uniqueId)
  278. . " AND label_id = " . $this->_db->quote($labelId);
  279. try {
  280. $this->_db->update($table, $bind, $where);
  281. } catch (Zend_Db_Exception $e) {
  282. $this->_catchException($e, __METHOD__);
  283. return false;
  284. }
  285. return true;
  286. }
  287. /**
  288. *
  289. * @param string $uniqueId
  290. * @param string $labelId
  291. * @param string $type
  292. * @return boolean
  293. */
  294. public function sortLabel($uniqueId, $labelId, $type, $issytem = false)
  295. {
  296. $labels = $this->getLabelsByUniqueId($uniqueId, array('issystem' => $issytem), array('ordernum' => 'DESC'))->toArray();
  297. $count = count($labels);
  298. $index = 0;
  299. $target = null;
  300. $exchange = null;
  301. if ($count <= 1) {
  302. return false;
  303. }
  304. foreach ($labels as $idx => $label) {
  305. if ($issytem) {
  306. if ($label['labelalias'] == $labelId) {
  307. $index = $idx;
  308. $target = $label;
  309. break;
  310. }
  311. } else {
  312. if ($label['labelid'] == $labelId) {
  313. $index = $idx;
  314. $target = $label;
  315. break;
  316. }
  317. }
  318. }
  319. if (($type == 'up' && $index == 0) || ($type == 'down' && $index == $count - 1)) {
  320. return false;
  321. }
  322. $exchangeIndex = $type == 'up' ? $index - 1 : $index + 1;
  323. $exchange = $labels[$exchangeIndex];
  324. try {
  325. if ($issytem) {
  326. $this->_db->update(
  327. 'td_label',
  328. array('order_num' => $exchange['ordernum']),
  329. 'unique_id = ' . $this->_db->quote($uniqueId) . ' AND label_alias = ' . $this->_db->quote($labelId)
  330. );
  331. $this->_db->update(
  332. 'td_label',
  333. array('order_num' => $target['ordernum']),
  334. 'unique_id = ' . $this->_db->quote($uniqueId) . ' AND label_alias = ' . $this->_db->quote($exchange['labelalias'])
  335. );
  336. } else {
  337. $this->updateLabel($uniqueId, $labelId, array(
  338. 'ordernum' => $exchange['ordernum']
  339. ));
  340. $this->updateLabel($uniqueId, $exchange['labelid'], array(
  341. 'ordernum' => $target['ordernum']
  342. ));
  343. }
  344. } catch (Zend_Db_Exception $e) {
  345. $this->_catchException($e, __METHOD__);
  346. return false;
  347. }
  348. return true;
  349. }
  350. /**
  351. * Delete label
  352. *
  353. * @param string $uniqueId
  354. * @param string $labelId
  355. * @return boolean
  356. */
  357. public function deleteLabel($uniqueId, $labelId)
  358. {
  359. // 系统标签禁止删除
  360. if (!is_string($labelId) || $labelId[0] === '^') {
  361. return false;
  362. }
  363. $uniqueId = $this->_db->quote($uniqueId);
  364. $labelId = $this->_db->quote($labelId);
  365. $sqls[] = "DELETE FROM td_tudu_label WHERE unique_id = $uniqueId AND label_id = $labelId";
  366. $sqls[] = "DELETE FROM td_label WHERE unique_id = $uniqueId AND label_id = $labelId";
  367. try {
  368. foreach($sqls as $sql) {
  369. $this->_db->query($sql);
  370. }
  371. } catch (Zend_Db_Exception $e) {
  372. $this->_catchException($e, __METHOD__);
  373. return false;
  374. }
  375. return true;
  376. }
  377. /**
  378. * 整理标签排序
  379. *
  380. * @param $uniqueId
  381. */
  382. public function tidyLabelSort($uniqueId)
  383. {
  384. $uniqueId = $this->_db->quote($uniqueId);
  385. $sql = 'SELECT label_id AS labelid, order_num AS ordernum FROM td_label '
  386. . 'WHERE unique_id = ' . $uniqueId . ' AND is_system = 0 ORDER BY order_num DESC';
  387. $records = $this->_db->fetchAll($sql);
  388. $count = count($records);
  389. try {
  390. foreach ($records as $index => $record) {
  391. $this->_db->update(
  392. 'td_label',
  393. array('order_num' => $count - $index),
  394. 'unique_id = ' . $uniqueId . ' AND label_id = ' . $this->_db->quote($record['labelid'])
  395. );
  396. }
  397. } catch (Zend_Db_Exception $e) {
  398. $this->_catchException($e, __METHOD__);
  399. return false;
  400. }
  401. return true;
  402. }
  403. /**
  404. * 图度数自增
  405. *
  406. * @param $uniqueId
  407. * @param $labelId
  408. * @param $value
  409. * @return boolean
  410. */
  411. public function increment($uniqueId, $labelId, $value = 1)
  412. {
  413. if ($value < 1) return false;
  414. $sql = 'UPDATE td_label SET total_num = total_num + ' . (int) $value
  415. . ' WHERE unique_id = ' . $this->_db->quote($uniqueId)
  416. . ' AND label_id = ' . $this->_db->quote($labelId);
  417. try {
  418. $this->_db->query($sql);
  419. } catch (Zend_Db_Exception $e) {
  420. $this->_catchException($e, __METHOD__);
  421. return false;
  422. }
  423. return true;
  424. }
  425. /**
  426. * 图度数自减
  427. *
  428. * @param $uniqueId
  429. * @param $labelId
  430. * @param $value
  431. * @return boolean
  432. */
  433. public function decrement($uniqueId, $labelId, $value = 1)
  434. {
  435. if ($value < 1) return false;
  436. $sql = 'UPDATE td_label SET total_num = total_num - ' . (int) $value
  437. . ' WHERE unique_id = ' . $this->_db->quote($uniqueId)
  438. . ' AND label_id = ' . $this->_db->quote($labelId);
  439. try {
  440. $this->_db->query($sql);
  441. } catch (Zend_Db_Exception $e) {
  442. $this->_catchException($e, __METHOD__);
  443. return false;
  444. }
  445. return true;
  446. }
  447. /**
  448. * 统计标签图度数
  449. *
  450. * @param string $uniqueId
  451. * @param string $labelId
  452. */
  453. public function calculateLabel($uniqueId, $labelId)
  454. {
  455. $sql = "call sp_td_calculate_label(" . $this->_db->quote($uniqueId) . "," . $this->_db->quote($labelId) . ")";
  456. try {
  457. $this->_db->query($sql);
  458. } catch (Zend_Db_Exception $e) {
  459. $this->_catchException($e, __METHOD__);
  460. return false;
  461. }
  462. return true;
  463. }
  464. /**
  465. * 获取标签ID
  466. *
  467. *
  468. * @return string
  469. */
  470. public static function getLabelId()
  471. {
  472. $labelId = base_convert(substr(microtime(true) * 10000, 0, -1), 10, 16) . str_pad(dechex(mt_rand(0, 0xfffff)), 5, '0', STR_PAD_LEFT);
  473. return $labelId;
  474. }
  475. }