File indexing completed on 2025-02-09 07:14:34

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 Default_Model_DbTable_PploadFiles extends Local_Model_Table
0024 {
0025     /** @var  Zend_Cache_Core */
0026     protected $cache;
0027     
0028     protected $_name = "ppload.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 f.*, pf.id as ppload_file_preview_id, pf.url_preview, pf.url_thumb 
0059                      from ppload.ppload_files f 
0060                      LEFT JOIN ppload.ppload_file_preview pf ON pf.collection_id = f.collection_id AND pf.file_id = f.id
0061                      where f.collection_id = :collection_id     
0062                      order by f.created_timestamp desc               
0063                    ";        
0064         /*
0065         $sql = " select * 
0066                      ,
0067                      (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
0068                      and tag_object_id = f.id ) packagename
0069                     ,
0070                     (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
0071                     and tag_object_id = f.id ) archname
0072 
0073                      from ppload.ppload_files f 
0074                      where f.collection_id = :collection_id     
0075                      order by f.created_timestamp desc               
0076                    ";        
0077          * 
0078          */
0079         $result = $this->_db->query($sql,array('collection_id' => $collection_id))->fetchAll();      
0080         return $result;
0081     }        
0082 
0083     public function fetchFilesCntForProject($collection_id)
0084     {
0085 
0086         if(empty($collection_id)) {
0087             return 0;
0088         }
0089         
0090         $sql = " select  count(1) as cnt
0091                      from ppload.ppload_files f 
0092                      where f.collection_id = :collection_id and f.active = 1                  
0093                    ";        
0094         $result = $this->_db->query($sql,array('collection_id' => $collection_id))->fetchAll();      
0095         return $result[0]['cnt'];
0096     }     
0097     
0098     
0099     public function fetchCountDownloadsTodayForProject($collection_id)
0100     {
0101         if(empty($collection_id)) {
0102             return 0;
0103         }
0104         
0105         $today = (new DateTime())->modify('-1 day');
0106         $filterDownloadToday = $today->format("Y-m-d H:i:s");
0107 
0108         $this->_db->query("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED")->execute();
0109 
0110         $this->_db->beginTransaction();
0111         
0112         $sql = "    SELECT COUNT(1) AS cnt
0113                     FROM ppload.ppload_files_downloaded f
0114                     WHERE f.collection_id = " . $collection_id . " 
0115                     AND f.downloaded_timestamp >= '" . $filterDownloadToday . "'               
0116                     ";        
0117         $result = $this->_db->query($sql)->fetchAll();   
0118         
0119         $this->_db->commit();
0120         
0121         return $result[0]['cnt'];
0122     }  
0123     
0124     public function fetchCountDownloadsTodayForProjectNew($collection_id)
0125     {
0126         if(empty($collection_id)) {
0127             return 0;
0128         }
0129         
0130         $today = (new DateTime())->modify('-1 day');
0131         $filterDownloadToday = $today->format("Y-m-d H:i:s");
0132 
0133         $this->_db->query("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED")->execute();
0134 
0135         $this->_db->beginTransaction();
0136         
0137         $sql = "    SELECT COUNT(1) AS cnt
0138                     FROM ppload.ppload_files_downloaded_unique f
0139                     WHERE f.collection_id = " . $collection_id . " 
0140                     AND f.downloaded_timestamp >= '" . $filterDownloadToday . "'               
0141                     ";        
0142         $result = $this->_db->query($sql)->fetchAll(); 
0143         
0144         $this->_db->commit();
0145         
0146         return $result[0]['cnt'];
0147     }
0148     
0149     
0150     public function fetchCountDownloadsForFileAllTime($collectionId, $file_id)
0151     {
0152         if(empty($file_id) || empty($collectionId)) {
0153             return 0;
0154         }
0155         
0156         $sql = "    SELECT count_dl AS cnt
0157                     FROM ppload.stat_ppload_files_downloaded f
0158                     WHERE f.collection_id = " . $collectionId . " 
0159                     AND f.file_id = " . $file_id . "
0160                    ";        
0161         $result = $this->_db->query($sql)->fetchAll();      
0162         return $result[0]['cnt'];
0163     }     
0164 
0165     public function fetchCountDownloadsForFileToday($collectionId, $file_id)
0166     {
0167         if(empty($file_id) || empty($collectionId)) {
0168             return 0;
0169         }
0170         
0171         $sql = "    SELECT COUNT(1) AS cnt
0172                     FROM ppload.ppload_files_downloaded f
0173                     WHERE f.collection_id = " . $collectionId . " 
0174                     AND f.file_id = " . $file_id . "
0175                     AND f.downloaded_timestamp >= DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:01')  
0176                    ";        
0177         $result = $this->_db->query($sql)->fetchAll();      
0178         return $result[0]['cnt'];
0179     } 
0180 
0181 
0182     public function fetchCountDownloadsForFileTodayNew($collectionId, $file_id)
0183     {
0184         if(empty($file_id) || empty($collectionId)) {
0185             return 0;
0186         }
0187         
0188         $sql = "    SELECT COUNT(1) AS cnt
0189                     FROM ppload.ppload_files_downloaded_unique f
0190                     WHERE f.collection_id = " . $collectionId . " 
0191                     AND f.file_id = " . $file_id . "
0192                     AND f.downloaded_timestamp >= DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:01')  
0193                    ";        
0194         $result = $this->_db->query($sql)->fetchAll();      
0195         return $result[0]['cnt'];
0196     }         
0197 
0198     
0199     private function fetchAllFiles($collection_id, $ignore_status = true, $activeFiles = false, $forAdmin = false)
0200     {
0201         
0202         if(empty($collection_id)) {
0203             return null;
0204         }
0205         /*
0206         $sql = "    select  *
0207                      from ppload.ppload_files f 
0208                      where f.collection_id = :collection_id 
0209                    ";        
0210          * 
0211          */
0212         
0213         //Admin Select with extended data
0214         $sqlAdmin = "SELECT  f.*
0215                     , 0 AS count_dl_today
0216                     , count_dl_uk_today.cnt AS count_dl_uk_today
0217                     ,0 AS count_dl_all
0218                     ,(SELECT count_dl AS cnt
0219                         FROM ppload.stat_ppload_files_downloaded_nounique f4
0220                         WHERE f4.collection_id = f.collection_id AND f4.file_id = f.id) AS count_dl_all_nouk
0221                     ,(SELECT count_dl AS cnt
0222                         FROM ppload.stat_ppload_files_downloaded_unique f3
0223                         WHERE f3.collection_id = f.collection_id AND f3.file_id = f.id) AS count_dl_all_uk
0224 
0225                     from ppload.ppload_files f 
0226                     LEFT JOIN (
0227                             SELECT COUNT(1) AS cnt, collection_id, file_id
0228                               FROM ppload.ppload_files_downloaded_unique f2
0229                               WHERE f2.downloaded_timestamp >= DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:01') 
0230                               GROUP BY collection_id, file_id
0231                     ) count_dl_uk_today ON count_dl_uk_today.collection_id = f.collection_id AND count_dl_uk_today.file_id = f.id
0232                     where f.collection_id = :collection_id  
0233                     ";
0234         $sqlNormal = "SELECT  f.*
0235                     , 0 AS count_dl_today
0236                     , count_dl_uk_today.cnt AS count_dl_uk_today
0237                     ,0 AS count_dl_all
0238                     ,(SELECT count_dl AS cnt
0239                         FROM ppload.stat_ppload_files_downloaded_nounique f4
0240                         WHERE f4.collection_id = f.collection_id AND f4.file_id = f.id) AS count_dl_all_nouk
0241                     ,(SELECT count_dl AS cnt
0242                         FROM ppload.stat_ppload_files_downloaded_unique f3
0243                         WHERE f3.collection_id = f.collection_id AND f3.file_id = f.id) AS count_dl_all_uk
0244                     from ppload.ppload_files f 
0245                     LEFT JOIN (
0246                             SELECT COUNT(1) AS cnt, collection_id, file_id
0247                               FROM ppload.ppload_files_downloaded_unique f2
0248                               WHERE f2.downloaded_timestamp >= DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:01') 
0249                               GROUP BY collection_id, file_id
0250                     ) count_dl_uk_today ON count_dl_uk_today.collection_id = f.collection_id AND count_dl_uk_today.file_id = f.id
0251                     where f.collection_id = :collection_id  
0252                     ";
0253         
0254         $sql = $sqlNormal;
0255         if($forAdmin == true) {
0256             $sql = $sqlAdmin;
0257         }
0258         if($ignore_status == FALSE && $activeFiles == TRUE) {
0259            $sql .= " and f.active = 1";
0260         }
0261         if($ignore_status == FALSE && $activeFiles == FALSE) {
0262            $sql .= " and f.active = 0";
0263         }
0264         $result = $this->_db->query($sql,array('collection_id' => $collection_id, ))->fetchAll();      
0265         return $result;
0266     }
0267 
0268     /*
0269     * @$collection_ids array of ids
0270     */
0271     private function fetchAllFilesExtended($collection_ids, $ignore_status = true, $activeFiles = false)
0272     {
0273         
0274         if(empty($collection_ids) || sizeof($collection_ids)==0) {
0275             return null;
0276         }
0277 
0278         $sql = "    select  *
0279                      from ppload.ppload_files f 
0280                      where f.collection_id in (".implode(',',$collection_ids).") ";        
0281 
0282         if($ignore_status == FALSE && $activeFiles == TRUE) {
0283            $sql .= " and f.active = 1 ";
0284         }
0285         if($ignore_status == FALSE && $activeFiles == FALSE) {
0286            $sql .= " and f.active = 0 ";
0287         }
0288 
0289         $sql.="order by f.collection_id,f.created_timestamp desc ";
0290 
0291         $result = $this->_db->query($sql)->fetchAll();      
0292         return $result;
0293     }
0294 
0295     
0296     public function fetchAllFilesForProject($collection_id, $isForAdmin = false)
0297     {
0298         return $this->fetchAllFiles($collection_id, true, false, $isForAdmin);
0299     }   
0300 
0301     public function fetchAllFilesForCollection($collection_ids)
0302     {
0303         return $this->fetchAllFilesExtended($collection_ids, true);
0304     } 
0305     
0306     public function fetchAllActiveFilesForCollection($collection_ids)
0307     {
0308         return $this->fetchAllFilesExtended($collection_ids, false, true);
0309     } 
0310     
0311     public function fetchAllActiveFilesForProject($collection_id, $isForAdmin = false)
0312     {
0313         return $this->fetchAllFiles($collection_id, false, true, $isForAdmin);
0314     }   
0315 
0316     public function fetchAllInactiveFilesForProject($collection_id, $isForAdmin = false)
0317     {
0318         return $this->fetchAllFiles($collection_id, false, false, $isForAdmin);
0319     }   
0320     
0321 }