File indexing completed on 2024-12-15 05:21:36
0001 <?php 0002 0003 /** 0004 * ocs-webserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-webserver. 0009 * 0010 * This program is free software: you can redistribute it and/or modify 0011 * it under the terms of the GNU Affero General Public License as 0012 * published by the Free Software Foundation, either version 3 of the 0013 * License, or (at your option) any later version. 0014 * 0015 * This program is distributed in the hope that it will be useful, 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0018 * GNU Affero General Public License for more details. 0019 * 0020 * You should have received a copy of the GNU Affero General Public License 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0022 **/ 0023 class Application_Model_DbTable_PploadFiles extends Local_Model_Table 0024 { 0025 /** @var Zend_Cache_Core */ 0026 protected $cache; 0027 0028 protected $_name = "ppload_files"; 0029 0030 protected $_keyColumnsForRow = array('id'); 0031 0032 protected $_key = 'id'; 0033 0034 0035 0036 0037 /** 0038 * @inheritDoc 0039 */ 0040 public function init() 0041 { 0042 parent::init(); // TODO: Change the autogenerated stub 0043 $this->cache = Zend_Registry::get('cache'); 0044 } 0045 0046 0047 /** 0048 * @param int $projectId Description 0049 * @return array 0050 */ 0051 public function fetchFilesForProject($collection_id) 0052 { 0053 0054 if(empty($collection_id)) { 0055 return null; 0056 } 0057 0058 $sql = " select * 0059 from ppload.ppload_files f 0060 where f.collection_id = :collection_id 0061 order by f.created_timestamp desc 0062 "; 0063 /* 0064 $sql = " select * 0065 , 0066 (select tag.tag_fullname from tag_object, tag where tag_type_id = 3 and tag_group_id = 8 and tag_object.tag_id = tag.tag_id and tag_object.is_deleted = 0 0067 and tag_object_id = f.id ) packagename 0068 , 0069 (select tag.tag_fullname from tag_object, tag where tag_type_id = 3 and tag_group_id = 9 and tag_object.tag_id = tag.tag_id and tag_object.is_deleted = 0 0070 and tag_object_id = f.id ) archname 0071 0072 from ppload.ppload_files f 0073 where f.collection_id = :collection_id 0074 order by f.created_timestamp desc 0075 "; 0076 * 0077 */ 0078 $result = $this->_db->query($sql,array('collection_id' => $collection_id))->fetchAll(); 0079 return $result; 0080 } 0081 0082 public function fetchFilesCntForProject($collection_id) 0083 { 0084 0085 if(empty($collection_id)) { 0086 return 0; 0087 } 0088 0089 $sql = " select count(1) as cnt 0090 from ppload.ppload_files f 0091 where f.collection_id = :collection_id and f.active = 1 0092 "; 0093 $result = $this->_db->query($sql,array('collection_id' => $collection_id))->fetchAll(); 0094 return $result[0]['cnt']; 0095 } 0096 0097 0098 public function fetchCountDownloadsTodayForProject($collection_id) 0099 { 0100 if(empty($collection_id)) { 0101 return 0; 0102 } 0103 0104 $today = (new DateTime())->modify('-1 day'); 0105 $filterDownloadToday = $today->format("Y-m-d H:i:s"); 0106 0107 $sql = " SELECT COUNT(1) AS cnt 0108 FROM ppload.ppload_files_downloaded f 0109 WHERE f.collection_id = " . $collection_id . " 0110 AND f.downloaded_timestamp >= '" . $filterDownloadToday . "' 0111 "; 0112 $result = $this->_db->query($sql)->fetchAll(); 0113 return $result[0]['cnt']; 0114 } 0115 0116 0117 private function fetchAllFiles($collection_id, $ignore_status = true, $activeFiles = false, $perpage = 1000, $page = 1) 0118 { 0119 0120 if(empty($collection_id)) { 0121 return null; 0122 } 0123 0124 $sql = " select * 0125 from ppload.ppload_files f 0126 where f.collection_id = :collection_id 0127 "; 0128 if($ignore_status == FALSE && $activeFiles == TRUE) { 0129 $sql .= " and f.active = 1"; 0130 } 0131 if($ignore_status == FALSE && $activeFiles == FALSE) { 0132 $sql .= " and f.active = 0"; 0133 } 0134 $offset = 0; 0135 if ($page > 1) { 0136 $offset = ($page - 1) * $perpage; 0137 } 0138 0139 $sql .= " ORDER BY f.name ASC LIMIT ".$perpage. " OFFSET ".$offset; 0140 $result = $this->_db->query($sql,array('collection_id' => $collection_id, ))->fetchAll(); 0141 return $result; 0142 } 0143 0144 public function fetchAllFilesForProject($collection_id) 0145 { 0146 return $this->fetchAllFiles($collection_id, true); 0147 } 0148 0149 public function fetchAllActiveFilesForProject($collection_id) 0150 { 0151 return $this->fetchAllFiles($collection_id, false, true); 0152 } 0153 0154 public function fetchActiveFileWithIndex($collection_id, $index) 0155 { 0156 return $this->fetchAllFiles($collection_id, false, true, 1, $index); 0157 } 0158 0159 public function fetchAllInactiveFilesForProject($collection_id) 0160 { 0161 return $this->fetchAllFiles($collection_id, false, false); 0162 } 0163 0164 public function fetchAllActiveFilesForFileInfo($collection_id, $fileIds = null) { 0165 0166 if(empty($collection_id)) { 0167 return null; 0168 } 0169 0170 $sql = " select * 0171 from ppload.ppload_files f 0172 where f.collection_id = :collection_id 0173 and f.active = 1 0174 "; 0175 if(null != $fileIds && count($fileIds) > 0) { 0176 $sql .= " and f.id in (".$fileIds.")"; 0177 } 0178 $sql .= " ORDER BY f.name ASC "; 0179 $result = $this->_db->query($sql,array('collection_id' => $collection_id, ))->fetchAll(); 0180 return $result; 0181 0182 } 0183 0184 public function getFilesTest(array $params = null) 0185 { 0186 0187 $collection_id = null; 0188 if(!empty($params['collection_id'])) { 0189 $collection_id = $params['collection_id']; 0190 } 0191 0192 $ocs_compatibility = null; 0193 if(!empty($params['ocs_compatibility'])) { 0194 $ocs_compatibility = $params['ocs_compatibility'] == 'compatible'; 0195 } 0196 0197 if(empty($collection_id)) { 0198 return null; 0199 } 0200 0201 $sql = " SELECT f.*, 0202 ( 0203 select GROUP_CONCAT(ta.tag_name) AS file_tags FROM tag_object t 0204 LEFT JOIN tag ta ON ta.tag_id = t.tag_id AND ta.is_active = 1 0205 WHERE t.tag_type_id = 3 AND t.tag_object_id = f.id AND t.is_deleted = 0 0206 0207 ) AS file_tags 0208 0209 from ppload.ppload_files f 0210 where 1=1 0211 "; 0212 0213 if($collection_id) { 0214 $sql .= " and f.collection_id = ".$collection_id; 0215 } 0216 if($ocs_compatibility && $ocs_compatibility == true) { 0217 $sql .= " and f.ocs_compatible = 1"; 0218 } 0219 0220 $result = $this->_db->query($sql)->fetchAll(); 0221 return $result; 0222 0223 return $this->_request('GET', 'files/index', $params); 0224 } 0225 0226 0227 public function getFiles($originId = null, $status = 'active', $clientId = null, $ownerId = null, $collectionId = null, $collectionStatus = 'active', $collectionCategory = null, $collectionTags = null, $collectionContentId = null, $types = null, $category = null, $tags = null, $ocsCompatibility = 'all', $contentId = null, $search = null, $ids = null, array $favoriteIds = null, $downloadedTimeperiodBegin = null, $downloadedTimeperiodEnd = null, $sort = 'name', $perpage = 20, $page = 1) 0228 { 0229 $prefix = 'ppload_'; 0230 $name = 'ppload.files'; 0231 $columns = $this->getColumns(); 0232 0233 $statementOption = ''; 0234 $where = array(); 0235 $values = array(); 0236 $order = "{$prefix}files.name ASC"; 0237 $offset = 0; 0238 0239 if ($originId) { 0240 $where[] = "{$prefix}files.origin_id = :origin_id"; 0241 $values[':origin_id'] = $originId; 0242 } 0243 if ($status != 'all') { 0244 $active = 1; 0245 if ($status == 'inactive') { 0246 $active = 0; 0247 } 0248 $where[] = "{$prefix}files.active = :active"; 0249 $values[':active'] = $active; 0250 } 0251 if ($clientId) { 0252 $where[] = "{$prefix}files.client_id = :client_id"; 0253 $values[':client_id'] = $clientId; 0254 } 0255 if ($ownerId) { 0256 $where[] = "{$prefix}files.owner_id = :owner_id"; 0257 $values[':owner_id'] = $ownerId; 0258 } 0259 if ($collectionId) { 0260 $where[] = "{$prefix}files.collection_id = :collection_id"; 0261 $values[':collection_id'] = $collectionId; 0262 } 0263 if ($collectionStatus != 'all') { 0264 $collectionActive = 1; 0265 if ($collectionStatus == 'inactive') { 0266 $collectionActive = 0; 0267 } 0268 $where[] = "{$prefix}collections.active = :collection_active"; 0269 $values[':collection_active'] = $collectionActive; 0270 } 0271 if ($collectionCategory !== null && $collectionCategory !== '') { 0272 $where[] = "{$prefix}collections.category = :collection_category"; 0273 $values[':collection_category'] = $collectionCategory; 0274 } 0275 if ($collectionTags !== null && $collectionTags !== '') { 0276 foreach (explode(',', $collectionTags) as $tag) { 0277 $tag = trim($tag); 0278 if ($tag) { 0279 $where[] = "({$prefix}collections.tags = " . $this->getDb()->quote($tag) 0280 . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("$tag,%") 0281 . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag,%") 0282 . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag") . ')'; 0283 } 0284 } 0285 } 0286 if ($collectionContentId !== null && $collectionContentId !== '') { 0287 $where[] = "{$prefix}collections.content_id = :collection_content_id"; 0288 $values[':collection_content_id'] = $collectionContentId; 0289 } 0290 if ($types) { 0291 $_types = array(); 0292 foreach (explode(',', $types) as $type) { 0293 $type = trim($type); 0294 if ($type) { 0295 $_types[] = $this->getDb()->quote($type); 0296 } 0297 } 0298 if ($_types) { 0299 $where[] = "{$prefix}files.type IN (" . implode(',', $_types) . ')'; 0300 } 0301 } 0302 if ($category !== null && $category !== '') { 0303 $where[] = "{$prefix}files.category = :category"; 0304 $values[':category'] = $category; 0305 } 0306 if ($tags !== null && $tags !== '') { 0307 foreach (explode(',', $tags) as $tag) { 0308 $tag = trim($tag); 0309 if ($tag) { 0310 $where[] = "({$prefix}files.tags = " . $this->getDb()->quote($tag) 0311 . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("$tag,%") 0312 . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag,%") 0313 . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag") . ')'; 0314 } 0315 } 0316 } 0317 if ($ocsCompatibility != 'all') { 0318 $ocsCompatible = null; 0319 if ($ocsCompatibility == 'compatible') { 0320 $ocsCompatible = 1; 0321 } 0322 else if ($ocsCompatibility == 'incompatible') { 0323 $ocsCompatible = 0; 0324 } 0325 if ($ocsCompatible !== null) { 0326 $where[] = "{$prefix}files.ocs_compatible = :ocs_compatible"; 0327 $values[':ocs_compatible'] = $ocsCompatible; 0328 } 0329 } 0330 if ($contentId !== null && $contentId !== '') { 0331 $where[] = "{$prefix}files.content_id = :content_id"; 0332 $values[':content_id'] = $contentId; 0333 } 0334 if ($search) { 0335 $isSearchable = false; 0336 foreach (explode(' ', $search) as $keyword) { 0337 if ($keyword && strlen($keyword) > 2) { 0338 $keyword = $this->getDb()->quote("%$keyword%"); 0339 $where[] = "({$prefix}files.name LIKE $keyword" 0340 . " OR {$prefix}files.title LIKE $keyword" 0341 . " OR {$prefix}files.description LIKE $keyword)"; 0342 $isSearchable = true; 0343 } 0344 } 0345 if (!$isSearchable) { 0346 return null; 0347 } 0348 } 0349 if ($ids) { 0350 $_ids = array(); 0351 foreach (explode(',', $ids) as $id) { 0352 $id = trim($id); 0353 if ($id) { 0354 $_ids[] = $this->getDb()->quote($id); 0355 } 0356 } 0357 if ($_ids) { 0358 $where[] = "{$prefix}files.id IN (" . implode(',', $_ids) . ')'; 0359 } 0360 } 0361 if (!empty($favoriteIds['ownerIds']) 0362 || !empty($favoriteIds['collectionIds']) 0363 || !empty($favoriteIds['fileIds']) 0364 ) { 0365 $where[] = $this->_convertFavoriteIdsToStatement( 0366 $favoriteIds, 0367 array( 0368 'ownerId' => "{$prefix}files.owner_id", 0369 'collectionId' => "{$prefix}files.collection_id", 0370 'fileId' => "{$prefix}files.id" 0371 ) 0372 ); 0373 } 0374 0375 if ($where) { 0376 $statementOption = 'WHERE ' . implode(' AND ', $where); 0377 } 0378 0379 if ($sort == 'newest') { 0380 $order = "{$prefix}files.id DESC"; 0381 } 0382 else if ($sort == 'recent') { 0383 $order = "{$prefix}files.downloaded_timestamp DESC"; 0384 } 0385 else if ($sort == 'frequent') { 0386 $order = "{$prefix}files.downloaded_count DESC"; 0387 } 0388 0389 if ($page > 1) { 0390 $offset = ($page - 1) * $perpage; 0391 } 0392 0393 $files = null; 0394 $pagination = null; 0395 0396 if ($downloadedTimeperiodBegin || $downloadedTimeperiodEnd) { 0397 $_downloadedTimeperiodBegin = $this->_getTimestamp(0); 0398 if ($downloadedTimeperiodBegin) { 0399 $_downloadedTimeperiodBegin = $downloadedTimeperiodBegin; 0400 } 0401 $_downloadedTimeperiodBegin = $this->getDb()->quote($_downloadedTimeperiodBegin); 0402 0403 $_downloadedTimeperiodEnd = $this->_getTimestamp(); 0404 if ($downloadedTimeperiodEnd) { 0405 $_downloadedTimeperiodEnd = $downloadedTimeperiodEnd; 0406 } 0407 $_downloadedTimeperiodEnd = $this->getDb()->quote($_downloadedTimeperiodEnd); 0408 0409 $_from = '(' 0410 . " SELECT {$prefix}files_downloaded.file_id AS file_id," 0411 . " COUNT({$prefix}files_downloaded.file_id) AS count" 0412 . " FROM {$prefix}files_downloaded" 0413 . " WHERE {$prefix}files_downloaded.downloaded_timestamp" 0414 . " BETWEEN {$_downloadedTimeperiodBegin} AND {$_downloadedTimeperiodEnd}" 0415 . " GROUP BY {$prefix}files_downloaded.file_id" 0416 . ') AS downloaded_timeperiod'; 0417 0418 $_join = "LEFT OUTER JOIN {$prefix}files" 0419 . " ON {$prefix}files.id = downloaded_timeperiod.file_id" 0420 . ' ' . $this->_join; 0421 0422 $_columns = str_replace( 0423 "{$prefix}files.downloaded_count AS downloaded_timeperiod_count", 0424 'downloaded_timeperiod.count AS downloaded_timeperiod_count', 0425 $this->_columns 0426 ); 0427 0428 if ($sort == 'frequent') { 0429 $order = 'downloaded_timeperiod.count DESC'; 0430 } 0431 0432 $this->setPrefix(''); 0433 $this->setName($_from); 0434 $this->setColumns($_columns); 0435 0436 $files = $this->fetchRowset( 0437 $_join . ' ' . $statementOption 0438 . " ORDER BY $order LIMIT $perpage OFFSET $offset", 0439 $values 0440 ); 0441 0442 $this->setPrefix($prefix); 0443 $this->setName($name); 0444 $this->setColumns($columns); 0445 0446 if (!$files) { 0447 return null; 0448 } 0449 0450 $this->setPrefix(''); 0451 $this->setName($_from); 0452 $this->setColumns($_columns); 0453 0454 $pagination = Flooer_Utility_Pagination::paginate( 0455 $this->count($_join . ' ' . $statementOption, $values), 0456 $perpage, 0457 $page 0458 ); 0459 0460 $this->setPrefix($prefix); 0461 $this->setName($name); 0462 $this->setColumns($columns); 0463 } 0464 else { 0465 $this->setColumns($this->_columns); 0466 $files = $this->fetchRowset( 0467 $this->_join . ' ' . $statementOption 0468 . " ORDER BY $order LIMIT $perpage OFFSET $offset", 0469 $values 0470 ); 0471 $this->setColumns($columns); 0472 0473 if (!$files) { 0474 return null; 0475 } 0476 0477 $this->setColumns($this->_columns); 0478 $pagination = Flooer_Utility_Pagination::paginate( 0479 $this->count($this->_join . ' ' . $statementOption, $values), 0480 $perpage, 0481 $page 0482 ); 0483 $this->setColumns($columns); 0484 } 0485 0486 return array( 0487 'files' => $files, 0488 'pagination' => $pagination 0489 ); 0490 } 0491 0492 public function getFile($id) 0493 { 0494 $prefix = $this->getPrefix(); 0495 $columns = $this->getColumns(); 0496 0497 $this->setColumns($this->_columns); 0498 $file = $this->fetchRow( 0499 $this->_join 0500 . " WHERE {$prefix}files.id = :id" 0501 . ' LIMIT 1', 0502 array(':id' => $id) 0503 ); 0504 $this->setColumns($columns); 0505 0506 if ($file) { 0507 return $file; 0508 } 0509 return null; 0510 } 0511 }