PageRenderTime 79ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/library/Dao/Td/Netdisk/File.php

https://github.com/polokk/tudu-web-1
PHP | 376 lines | 233 code | 66 blank | 77 comment | 36 complexity | 4955343965acf6158fc160374cfc3d8a 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 1328 2011-11-28 03:19:22Z 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_Netdisk_File extends Oray_Dao_Abstract
  22. {
  23. /**
  24. * 获取文件记录
  25. *
  26. * @param $condition
  27. * @param $filter
  28. */
  29. public function getFile(array $condition, $filter = null)
  30. {
  31. $table = 'nd_file';
  32. $columns = 'org_id AS orgid, unique_id AS uniqueid, folder_id AS folderid, file_id AS fileid, is_from_attach as isfromattach, '
  33. . 'attach_file_id AS attachfileid, file_name AS filename, size, path, type, is_share AS isshare, create_time AS createtime, '
  34. . 'from_unique_id AS fromuniqueid, from_file_id AS fromfileid';
  35. $where = array();
  36. if (!empty($condition['uniqueid'])) {
  37. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  38. }
  39. if (!empty($condition['fileid'])) {
  40. $where[] = 'file_id = ' . $this->_db->quote($condition['fileid']);
  41. }
  42. if (empty($where)) {
  43. return null;
  44. }
  45. $where = implode(' AND ', $where);
  46. $sql = "SELECT {$columns} FROM {$table} WHERE {$where}";
  47. try {
  48. $record = $this->_db->fetchRow($sql);
  49. if (!$record) {
  50. return null;
  51. }
  52. return Oray_Dao::record('Dao_Td_Netdisk_Record_File', $record);
  53. } catch (Zend_Db_Exception $e) {
  54. $this->_catchException($e, __METHOD__);
  55. return null;
  56. }
  57. }
  58. /**
  59. *
  60. * @param array $condition
  61. * @param array $filter
  62. * @return Oray_Dao_Recordset
  63. */
  64. public function getFiles(array $condition, $filter = null, $sort = null, $maxCount = null)
  65. {
  66. $table = 'nd_file';
  67. $columns = 'org_id AS orgid, unique_id AS uniqueid, folder_id AS folderid, file_id AS fileid, is_from_attach as isfromattach, '
  68. . 'attach_file_id AS attachfileid, file_name AS filename, size, path, type, is_share AS isshare, create_time AS createtime, '
  69. . 'from_unique_id AS fromuniqueid, from_file_id AS fromfileid';
  70. $where = array();
  71. $order = array();
  72. $limit = '';
  73. if (!empty($condition['uniqueid'])) {
  74. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  75. }
  76. if (!empty($condition['orgid'])) {
  77. $where[] = 'org_id = ' . $this->_db->quote($condition['orgid']);
  78. }
  79. if (!empty($condition['folderid'])) {
  80. $where[] = 'folder_id = ' . $this->_db->quote($condition['folderid']);
  81. }
  82. if (!empty($condition['fileid'])) {
  83. if (is_array($condition['fileid'])) {
  84. $fileid = array_map(array($this->_db, 'quote'), $condition['fileid']);
  85. $where[] = 'file_id in (' . implode(',', $fileid) . ')';
  86. } else {
  87. $where[] = 'file_id = ' .$this->_db->quote($condition['fileid']);
  88. }
  89. }
  90. if (!$where) {
  91. return new Oray_Dao_Recordset();
  92. }
  93. // WHERE
  94. $where = implode(' AND ', $where);
  95. if ($where) {
  96. $where = 'WHERE ' . $where;
  97. }
  98. $sort = $this->_formatSort($sort);
  99. foreach ($sort as $key => $val) {
  100. switch ($key) {
  101. case 'createtime':
  102. $key = 'create_time';
  103. break;
  104. default:
  105. continue 2;
  106. }
  107. $order[] = $key . ' ' . $val;
  108. }
  109. // ORDER
  110. $order = implode(', ', $order);
  111. if ($order) {
  112. $order = 'ORDER BY ' . $order;
  113. }
  114. // LIMIT
  115. if (is_int($maxCount) && $maxCount > 0) {
  116. $limit = 'LIMIT ' . $maxCount;
  117. }
  118. $sql = "SELECT $columns FROM $table $where $order $limit";
  119. try {
  120. $records = $this->_db->fetchAll($sql);
  121. return new Oray_Dao_Recordset($records, 'Dao_Td_Netdisk_Record_File');
  122. } catch (Zend_Db_Exception $e) {
  123. $this->_catchException($e, __METHOD__);
  124. return new Oray_Dao_Recordset();
  125. }
  126. }
  127. /**
  128. * 获取文件数
  129. * @param array $condition
  130. */
  131. public function getFileCount(array $condition)
  132. {
  133. $where = array();
  134. if (!empty($condition['uniqueid'])) {
  135. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  136. }
  137. if (!empty($condition['folderid'])) {
  138. $where[] = 'folder_id = ' . $this->_db->quote($condition['folderid']);
  139. }
  140. if (!$where) {
  141. return false;
  142. }
  143. $where = implode(' AND ', $where);
  144. $sql = "SELECT COUNT(0) FROM nd_file WHERE {$where}";
  145. try {
  146. return (int) $this->_db->fetchOne($sql);
  147. } catch (Zend_Db_Exception $e) {
  148. $this->_catchException($e, __METHOD__);
  149. return false;
  150. }
  151. }
  152. /**
  153. * 判断文件是否已存在
  154. *
  155. * @param array $condition
  156. */
  157. public function existFile(array $condition)
  158. {
  159. $where = array();
  160. if (!empty($condition['uniqueid'])) {
  161. $where[] = 'unique_id = ' . $this->_db->quote($condition['uniqueid']);
  162. }
  163. if (!empty($condition['folderid'])) {
  164. $where[] = 'folder_id = ' . $this->_db->quote($condition['folderid']);
  165. }
  166. if (!empty($condition['fileid'])) {
  167. $where[] = 'file_id = ' . $this->_db->quote($condition['fileid']);
  168. }
  169. if (!empty($condition['fromfileid'])) {
  170. $where[] = 'from_file_id = ' . $this->_db->quote($condition['fromfileid']);
  171. }
  172. if (!empty($condition['attachfileid'])) {
  173. $where[] = 'attach_file_id = ' . $this->_db->quote($condition['attachfileid']);
  174. }
  175. if (!$where) {
  176. return false;
  177. }
  178. $where = implode(' AND ', $where);
  179. $sql = "SELECT COUNT(0) FROM nd_file WHERE {$where}";
  180. try {
  181. return (int) $this->_db->fetchOne($sql);
  182. } catch (Zend_Db_Exception $e) {
  183. $this->_catchException($e, __METHOD__);
  184. return false;
  185. }
  186. }
  187. /**
  188. * 创建网盘文件记录
  189. *
  190. * @param array $params
  191. */
  192. public function createFile(array $params)
  193. {
  194. if (empty($params['fileid']) || empty($params['uniqueid'])) {
  195. return false;
  196. }
  197. $sql = "call sp_nd_add_file("
  198. . $this->_db->quote($params['orgid']) . ", "
  199. . $this->_db->quote($params['uniqueid']) . ", "
  200. . $this->_db->quote($params['fileid']) . ", "
  201. . $this->_db->quote($params['folderid']) . ", "
  202. . $this->_db->quote($params['path']) . ", "
  203. . $this->_db->quote($params['filename']) . ", "
  204. . $this->_db->quote($params['size']) . ", "
  205. . $this->_db->quote($params['type']) . ")";
  206. try {
  207. $ret = $this->_db->fetchOne($sql);
  208. } catch (Zend_Db_Exception $e) {
  209. if (23000 === $e->getCode()) {
  210. return 1;
  211. }
  212. $this->_catchException($e, __METHOD__);
  213. return false;
  214. }
  215. return (int) $ret;
  216. }
  217. /**
  218. *
  219. * @param string $uniqueId
  220. * @param string $fileId
  221. * @param array $params
  222. * @return boolean
  223. */
  224. public function updateFile($uniqueId, $fileId, array $params)
  225. {
  226. if (empty($uniqueId) || empty($fileId)) {
  227. return false;
  228. }
  229. $table = 'nd_file';
  230. $bind = array();
  231. $where = 'unique_id = ' . $this->_db->quote($uniqueId)
  232. . ' AND file_id = ' . $this->_db->quote($fileId);
  233. if (!empty($params['filename'])) {
  234. $bind['file_name'] = $params['filename'];
  235. }
  236. if (isset($params['isfromattach'])) {
  237. $bind['is_from_attach'] = $params['isfromattach'] ? 1 : 0;
  238. }
  239. if (array_key_exists('attachfileid', $params)) {
  240. $bind['attach_file_id'] = $params['attachfileid'];
  241. }
  242. if (isset($params['isshare'])) {
  243. $bind['is_share'] = $params['isshare'];
  244. }
  245. if (array_key_exists('fromuniqueid', $params)) {
  246. $bind['from_unique_id'] = $params['fromuniqueid'];
  247. }
  248. if (array_key_exists('fromfileid', $params)) {
  249. $bind['from_file_id'] = $params['fromfileid'];
  250. }
  251. try {
  252. $this->_db->update($table, $bind, $where);
  253. } catch (Zend_Db_Execption $e) {
  254. $this->_catchExecption($e, __METHOD__);
  255. return false;
  256. }
  257. return true;
  258. }
  259. /**
  260. *
  261. * @param string $uniqueId
  262. * @param string $folderId
  263. * @return boolean
  264. */
  265. public function deleteFile($uniqueId, $fileId)
  266. {
  267. $sql = 'call sp_nd_delete_file(' . $this->_db->quote($uniqueId)
  268. . ',' . $this->_db->quote($fileId) . ')';
  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. * @param $uniqueId
  281. * @param $fileId
  282. * @param $folderId
  283. */
  284. public function moveFile($uniqueId, $fileId, $folderId)
  285. {
  286. /*$sql = 'call sp_nd_move_file('
  287. . $this->_db->quote($uniqueId) . ','
  288. . $this->_db->quote($fileId) . ','
  289. . $this->_db->quote($folderId) . ');';*/
  290. $sql = 'UPDATE nd_file SET folder_id = ' . $this->_db->quote($folderId) . ' WHERE '
  291. . 'unique_id = ' . $this->_db->quote($uniqueId) . ' AND file_id = ' . $this->_db->quote($fileId);
  292. try {
  293. $this->_db->query($sql);
  294. } catch (Zend_Db_Exception $e) {
  295. $this->_catchException($e, __METHOD__);
  296. return false;
  297. }
  298. return true;
  299. }
  300. /**
  301. * 获取文件ID
  302. *
  303. * @return string
  304. */
  305. public static function getFileId()
  306. {
  307. $fileId = md5(microtime(true) . (mt_rand(0, 0xfffff)));
  308. return $fileId;
  309. }
  310. }