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

/library/Dao/Td/Attachment/File.php

https://github.com/polokk/tudu-web-1
PHP | 349 lines | 204 code | 53 blank | 92 comment | 31 complexity | 70666e5f3b0756c25e4605bd32c192dd 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: File.php 1998 2012-07-17 02:41:07Z web_op $
  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_Attachment_File 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. *
  34. *
  35. * @param $condition
  36. * @param $filter
  37. * @return Dao_Td_Attachment_Record_File
  38. */
  39. public function getFile(array $condition, $filter = null)
  40. {
  41. $table = "td_attachment a "
  42. . "LEFT JOIN td_attach_post ap ON a.file_id = ap.file_id";
  43. $columns = "a.file_id AS fileid, a.file_name AS filename, a.size, a.type, a.path, "
  44. . "ap.tudu_id AS tuduid, ap.post_id AS postid, a.unique_id as uniqueid, "
  45. . "a.create_time AS createtime";
  46. $where = array();
  47. if (isset($condition['fileid'])) {
  48. $where[] = 'a.file_id = ' . $this->_db->quote($condition['fileid']);
  49. }
  50. if (!$where) {
  51. return null;
  52. }
  53. $where = implode(' AND ', $where);
  54. $sql = "SELECT {$columns} FROM {$table} WHERE {$where} LIMIT 1";
  55. $record = $this->_db->fetchRow($sql);
  56. if (!$record) {
  57. return null;
  58. }
  59. return Oray_Dao::record('Dao_Td_Attachment_Record_File', $record);
  60. }
  61. /**
  62. *
  63. * @param array $condition
  64. * @param array $sort
  65. */
  66. public function getTuduFiles(array $condition, $filter = null, $sort = null)
  67. {
  68. $table = 'td_attach_post ap '
  69. . 'LEFT JOIN td_attachment a ON ap.file_id = a.file_id '
  70. . 'LEFT JOIN td_post p ON ap.post_id = p.post_id AND ap.tudu_id = p.tudu_id';
  71. $columns = 'a.file_id AS fileid, a.file_name AS filename, a.size, a.create_time AS createtime, '
  72. . 'p.poster, ap.tudu_id AS tuduid, ap.post_id AS postid, a.unique_id as uniqueid, '
  73. . 'ap.is_attach AS isattach';
  74. $where = array();
  75. $order = array();
  76. if (isset($condition['tuduid'])) {
  77. $where[] = 'ap.tudu_id = ' . $this->_db->quote($condition['tuduid']);
  78. }
  79. if (!empty($filter) && isset($filter['isattach'])) {
  80. $where[] = 'ap.is_attach = 1';
  81. }
  82. if (empty($where)) {
  83. return new Oray_Dao_Recordset();
  84. }
  85. $where = implode(' AND ', $where);
  86. if ($where) {
  87. $where = 'WHERE ' . $where;
  88. }
  89. $sort = $this->_formatSort($sort);
  90. foreach ($sort as $key => $val) {
  91. switch ($key) {
  92. default:
  93. continue 2;
  94. }
  95. $order[] = $key . ' ' . $val;
  96. }
  97. $order = implode(', ', $order);
  98. if ($order) {
  99. $order = 'ORDER BY ' . $order;
  100. }
  101. $sql = "SELECT $columns FROM $table $where $order";
  102. $records = $this->_db->fetchAll($sql);
  103. return new Oray_Dao_Recordset($records, 'Dao_Td_Attachment_Record_File');
  104. }
  105. /**
  106. * Get records
  107. *
  108. * SELECT
  109. * a.file_id AS fileid, a.file_name AS filename, a.size, a.type, a.path,
  110. * ap.tudu_id, ap.post_id,
  111. * a.create_time AS createtime FROM td_attachment a
  112. * LEFT JOIN td_attach_post ap ON a.file_id = ap.file_id
  113. * WHERE ap.tudu_id = ? AND ap.post_id = ?
  114. *
  115. * @param array $condition
  116. * @param array $filter
  117. * @param array $sort
  118. * @param int $maxCount
  119. * @return Oray_Dao_Recordset
  120. */
  121. public function getFiles(array $condition, $filter = null, $sort = null, $maxCount = null)
  122. {
  123. $table = "td_attachment a "
  124. . "LEFT JOIN td_attach_post ap ON a.file_id = ap.file_id";
  125. $columns = "a.file_id AS fileid, a.file_name AS filename, a.size, a.type, a.path, ap.is_attach as isattach, "
  126. . "ap.tudu_id AS tuduid, ap.post_id AS postid, a.unique_id as uniqueid, "
  127. . "a.create_time AS createtime";
  128. $where = array();
  129. $order = array();
  130. $limit = '';
  131. if (isset($condition['tuduid'])) {
  132. $where[] = 'ap.tudu_id = ' . $this->_db->quote($condition['tuduid']);
  133. }
  134. if (isset($condition['postid'])) {
  135. $where[] = 'ap.post_id = ' . $this->_db->quote($condition['postid']);
  136. }
  137. if (!empty($condition['fileid'])) {
  138. if (is_string($condition['fileid'])) {
  139. $condition['fileid'] = array($condition['fileid']);
  140. }
  141. $condition['fileid'] = array_map(array($this->_db, 'quote'), $condition['fileid']);
  142. $where[] = 'a.file_id IN (' . implode(',', $condition['fileid']) . ')';
  143. }
  144. if (empty($where)) {
  145. return new Oray_Dao_Recordset();
  146. }
  147. if (!empty($filter) && array_key_exists('isattachment', $filter)) {
  148. if (null !== $filter['isattachment']) {
  149. $where[] = 'ap.is_attach = ' . (int) $filter['isattachment'];
  150. }
  151. } else {
  152. $where[] = 'ap.is_attach = 1';
  153. }
  154. // WHERE
  155. $where = implode(' AND ', $where);
  156. if ($where) {
  157. $where = 'WHERE ' . $where;
  158. }
  159. $sort = $this->_formatSort($sort);
  160. foreach ($sort as $key => $val) {
  161. switch ($key) {
  162. default:
  163. continue 2;
  164. }
  165. $order[] = $key . ' ' . $val;
  166. }
  167. // ORDER
  168. $order = implode(', ', $order);
  169. if ($order) {
  170. $order = 'ORDER BY ' . $order;
  171. }
  172. // LIMIT
  173. if (is_int($maxCount) && $maxCount > 0) {
  174. $limit = 'LIMIT ' . $maxCount;
  175. }
  176. $sql = "SELECT $columns FROM $table $where $order $limit";
  177. $records = $this->_db->fetchAll($sql);
  178. return new Oray_Dao_Recordset($records, 'Dao_Td_Attachment_Record_File');
  179. }
  180. /**
  181. * Create file
  182. *
  183. * @param $params
  184. * @return string|false
  185. */
  186. public function createFile(array $params)
  187. {
  188. if (empty($params['fileid'])) {
  189. return false;
  190. }
  191. $createTime = empty($params['createtime']) ? time() : (int) $params['createtime'];
  192. $table = "td_attachment";
  193. $bind = array(
  194. //'org_id' => $params['orgid'],
  195. 'file_id' => $params['fileid'],
  196. 'file_name' => $params['filename'],
  197. 'size' => (int) $params['size'],
  198. 'type' => $params['type'],
  199. 'path' => $params['path'],
  200. 'create_time' => $createTime
  201. );
  202. if (isset($params['uniqueid'])) {
  203. $bind['unique_id'] = $params['uniqueid'];
  204. }
  205. if (isset($params['isnetdisk'])) {
  206. $bind['is_netdisk'] = $params['isnetdisk'] ? 1 : 0;
  207. }
  208. if (!empty($params['orgid'])) {
  209. $bind['org_id'] = $params['orgid'];
  210. }
  211. try {
  212. $this->_db->insert($table, $bind);
  213. } catch (Zend_Db_Exception $e) {
  214. // 主键冲突
  215. if (23000 === $e->getCode()) {
  216. return true;
  217. }
  218. $this->_catchException($e, __METHOD__);
  219. return false;
  220. }
  221. return $params['fileid'];
  222. }
  223. /**
  224. * 增加文件关联
  225. *
  226. * @param string $tuduId
  227. * @param string $postId
  228. * @param string $fileId
  229. * @param boolean $isAttachment
  230. * @return boolean
  231. */
  232. public function addPost($tuduId, $postId, $fileId, $isAttachment = true)
  233. {
  234. $table = "td_attach_post";
  235. $bind = array(
  236. 'tudu_id' => $tuduId,
  237. 'post_id' => $postId,
  238. 'file_id' => $fileId,
  239. 'is_attach' => (int) $isAttachment
  240. );
  241. try {
  242. $this->_db->insert($table, $bind);
  243. } catch (Zend_Db_Exception $e) {
  244. // 主键冲突
  245. if (23000 === $e->getCode()) {
  246. return true;
  247. }
  248. //$this->_catchException($e, __METHOD__);
  249. return false;
  250. }
  251. return true;
  252. }
  253. /**
  254. * 删除文件关联
  255. *
  256. * @param string $tuduId
  257. * @param string $postId
  258. * @param string $fileId
  259. * @return boolean
  260. */
  261. public function deletePost($tuduId, $postId, $fileId = null)
  262. {
  263. $sql = "DELETE FROM td_attach_post"
  264. . " WHERE tudu_id = " . $this->_db->quote($tuduId)
  265. . " AND post_id = " . $this->_db->quote($postId);
  266. if ($fileId) {
  267. $sql .= " AND file_id = " . $this->_db->quote($fileId);
  268. }
  269. try {
  270. $this->_db->query($sql);
  271. } catch (Zend_Db_Exception $e) {
  272. $this->_catchException($e, __METHOD__);
  273. return false;
  274. }
  275. return true;
  276. }
  277. /**
  278. *
  279. */
  280. public function getQuotaUsed($orgId)
  281. {
  282. $sql = 'SELECT SUM(size) FROM td_attachment WHERE org_id = ' . $this->_db->quote($orgId);
  283. $size = (int) $this->_db->fetchOne($sql);
  284. return $size;
  285. }
  286. /**
  287. * 获取文件ID
  288. *
  289. * @return string
  290. */
  291. public static function getFileId()
  292. {
  293. $fileId = md5(microtime(true) . (mt_rand(0, 0xfffff)));
  294. return $fileId;
  295. }
  296. }