File indexing completed on 2024-12-22 05:33:08

0001 <?php
0002 
0003 /**
0004  * ocs-fileserver
0005  *
0006  * Copyright 2016 by pling GmbH.
0007  *
0008  * This file is part of ocs-fileserver.
0009  *
0010  * ocs-fileserver is free software: you can redistribute it and/or modify
0011  * it under the terms of the GNU Affero General Public License as published by
0012  * the Free Software Foundation, either version 3 of the License, or
0013  * (at your option) any later version.
0014  *
0015  * ocs-fileserver 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 
0024 class table_collections extends BaseModel
0025 {
0026 
0027     protected $_columns = '';
0028 
0029     protected $_join = '';
0030 
0031     public function __construct(&$db)
0032     {
0033         parent::__construct($db, $db->getTableConfig());
0034         $this->setName('collections');
0035         $this->setPrimaryInsert(true);
0036 
0037         $prefix = $this->getPrefix();
0038 
0039         $this->_columns = "{$prefix}collections.id AS id,"
0040             . "{$prefix}collections.active AS active,"
0041             . "{$prefix}collections.client_id AS client_id,"
0042             . "{$prefix}collections.owner_id AS owner_id,"
0043             . "{$prefix}profiles.id AS profile_id,"
0044             . "{$prefix}profiles.name AS profile_name,"
0045             . "{$prefix}collections.name AS name,"
0046             . "{$prefix}collections.files AS files,"
0047             . "{$prefix}collections.size AS size,"
0048             . "{$prefix}collections.title AS title,"
0049             . "{$prefix}collections.description AS description,"
0050             . "{$prefix}collections.category AS category,"
0051             . "{$prefix}collections.tags AS tags,"
0052             . "{$prefix}collections.version AS version,"
0053             . "{$prefix}collections.content_id AS content_id,"
0054             . "{$prefix}collections.content_page AS content_page,"
0055             . "{$prefix}collections.downloaded_timestamp AS downloaded_timestamp,"
0056             . "{$prefix}collections.downloaded_count AS downloaded_count,"
0057             . "{$prefix}collections.downloaded_count AS downloaded_timeperiod_count,"
0058             . "{$prefix}collections.created_timestamp AS created_timestamp,"
0059             . "{$prefix}collections.updated_timestamp AS updated_timestamp";
0060 
0061         $this->_join = "LEFT OUTER JOIN {$prefix}profiles"
0062             . " ON {$prefix}profiles.client_id = {$prefix}collections.client_id"
0063             . " AND {$prefix}profiles.owner_id = {$prefix}collections.owner_id";
0064     }
0065 
0066     public function __set($key, $value)
0067     {
0068         $value = $this->_convertArrayToObject($value);
0069         unset(
0070             $value->id,
0071             $value->created_timestamp,
0072             $value->created_ip,
0073             $value->updated_timestamp,
0074             $value->updated_ip
0075         );
0076         $value->updated_timestamp = $this->_getTimestamp();
0077         $value->updated_ip = $this->_getIp();
0078         if (!isset($this->$key)) {
0079             $value->downloaded_timestamp = $value->updated_timestamp;
0080             $value->downloaded_ip = $value->updated_ip;
0081             $value->downloaded_count = 0;
0082             $value->created_timestamp = $value->updated_timestamp;
0083             $value->created_ip = $value->updated_ip;
0084         }
0085         parent::__set($key, $value);
0086     }
0087 
0088     public function getCollections($status = 'active', $clientId = null, $ownerId = null, $category = null, $tags = null, $contentId = null, $search = null, $ids = null, array $favoriteIds = null, $downloadedTimeperiodBegin = null, $downloadedTimeperiodEnd = null, $sort = 'name', $perpage = 20, $page = 1)
0089     {
0090         $prefix = $this->getPrefix();
0091         $name = $this->getName();
0092         $columns = $this->getColumns();
0093 
0094         $statementOption = '';
0095         $where = array();
0096         $values = array();
0097         $order = "{$prefix}collections.name ASC";
0098         $offset = 0;
0099 
0100         if ($status != 'all') {
0101             $active = 1;
0102             if ($status == 'inactive') {
0103                 $active = 0;
0104             }
0105             $where[] = "{$prefix}collections.active = :active";
0106             $values[':active'] = $active;
0107         }
0108         if ($clientId) {
0109             $where[] = "{$prefix}collections.client_id = :client_id";
0110             $values[':client_id'] = $clientId;
0111         }
0112         if ($ownerId) {
0113             $where[] = "{$prefix}collections.owner_id = :owner_id";
0114             $values[':owner_id'] = $ownerId;
0115         }
0116         if ($category !== null && $category !== '') {
0117             $where[] = "{$prefix}collections.category = :category";
0118             $values[':category'] = $category;
0119         }
0120         if ($tags !== null && $tags !== '') {
0121             foreach (explode(',', $tags) as $tag) {
0122                 $tag = trim($tag);
0123                 if ($tag) {
0124                     $where[] = "({$prefix}collections.tags = " . $this->getDb()->quote($tag)
0125                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("$tag,%")
0126                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0127                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0128                 }
0129             }
0130         }
0131         if ($contentId !== null && $contentId !== '') {
0132             $where[] = "{$prefix}collections.content_id = :content_id";
0133             $values[':content_id'] = $contentId;
0134         }
0135         if ($search) {
0136             $isSearchable = false;
0137             foreach (explode(' ', $search) as $keyword) {
0138                 if ($keyword && strlen($keyword) > 2) {
0139                     $keyword = $this->getDb()->quote("%$keyword%");
0140                     $where[] = "({$prefix}collections.name LIKE $keyword"
0141                         . " OR {$prefix}collections.title LIKE $keyword"
0142                         . " OR {$prefix}collections.description LIKE $keyword)";
0143                     $isSearchable = true;
0144                 }
0145             }
0146             if (!$isSearchable) {
0147                 return null;
0148             }
0149         }
0150         if ($ids) {
0151             $_ids = array();
0152             foreach (explode(',', $ids) as $id) {
0153                 $id = trim($id);
0154                 if ($id) {
0155                     $_ids[] = $this->getDb()->quote($id);
0156                 }
0157             }
0158             if ($_ids) {
0159                 $where[] = "{$prefix}collections.id IN (" . implode(',', $_ids) . ')';
0160             }
0161         }
0162         if (!empty($favoriteIds['ownerIds'])
0163             || !empty($favoriteIds['collectionIds'])
0164         ) {
0165             $where[] = $this->_convertFavoriteIdsToStatement(
0166                 $favoriteIds,
0167                 array(
0168                     'ownerId' => "{$prefix}collections.owner_id",
0169                     'collectionId' => "{$prefix}collections.id"
0170                 )
0171             );
0172         }
0173 
0174         if ($where) {
0175             $statementOption = 'WHERE ' . implode(' AND ', $where);
0176         }
0177 
0178         if ($sort == 'newest') {
0179             $order = "{$prefix}collections.id DESC";
0180         }
0181         else if ($sort == 'recent') {
0182             $order = "{$prefix}collections.downloaded_timestamp DESC";
0183         }
0184         else if ($sort == 'frequent') {
0185             $order = "{$prefix}collections.downloaded_count DESC";
0186         }
0187 
0188         if ($page > 1) {
0189             $offset = ($page - 1) * $perpage;
0190         }
0191 
0192         $collections = null;
0193         $pagination = null;
0194 
0195         if ($downloadedTimeperiodBegin || $downloadedTimeperiodEnd) {
0196             $_downloadedTimeperiodBegin = $this->_getTimestamp(0);
0197             if ($downloadedTimeperiodBegin) {
0198                 $_downloadedTimeperiodBegin = $downloadedTimeperiodBegin;
0199             }
0200             $_downloadedTimeperiodBegin = $this->getDb()->quote($_downloadedTimeperiodBegin);
0201 
0202             $_downloadedTimeperiodEnd = $this->_getTimestamp();
0203             if ($downloadedTimeperiodEnd) {
0204                 $_downloadedTimeperiodEnd = $downloadedTimeperiodEnd;
0205             }
0206             $_downloadedTimeperiodEnd = $this->getDb()->quote($_downloadedTimeperiodEnd);
0207 
0208             $_from = '('
0209                 . " SELECT {$prefix}collections_downloaded.collection_id AS collection_id,"
0210                 . " COUNT({$prefix}collections_downloaded.collection_id) AS count"
0211                 . " FROM {$prefix}collections_downloaded"
0212                 . " WHERE {$prefix}collections_downloaded.downloaded_timestamp"
0213                 . " BETWEEN {$_downloadedTimeperiodBegin} AND {$_downloadedTimeperiodEnd}"
0214                 . " GROUP BY {$prefix}collections_downloaded.collection_id"
0215                 . ') AS downloaded_timeperiod';
0216 
0217             $_join = "LEFT OUTER JOIN {$prefix}collections"
0218                 . " ON {$prefix}collections.id = downloaded_timeperiod.collection_id"
0219                 . ' ' . $this->_join;
0220 
0221             $_columns = str_replace(
0222                 "{$prefix}collections.downloaded_count AS downloaded_timeperiod_count",
0223                 'downloaded_timeperiod.count AS downloaded_timeperiod_count',
0224                 $this->_columns
0225             );
0226 
0227             if ($sort == 'frequent') {
0228                 $order = 'downloaded_timeperiod.count DESC';
0229             }
0230 
0231             $this->setPrefix('');
0232             $this->setName($_from);
0233             $this->setColumns($_columns);
0234 
0235             $collections = $this->fetchRowset(
0236                 $_join . ' ' . $statementOption
0237                 . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0238                 $values
0239             );
0240 
0241             $this->setPrefix($prefix);
0242             $this->setName($name);
0243             $this->setColumns($columns);
0244 
0245             if (!$collections) {
0246                 return null;
0247             }
0248 
0249             $this->setPrefix('');
0250             $this->setName($_from);
0251             $this->setColumns($_columns);
0252 
0253             $pagination = Flooer_Utility_Pagination::paginate(
0254                 $this->count($_join . ' ' . $statementOption, $values),
0255                 $perpage,
0256                 $page
0257             );
0258 
0259             $this->setPrefix($prefix);
0260             $this->setName($name);
0261             $this->setColumns($columns);
0262         }
0263         else {
0264             $this->setColumns($this->_columns);
0265             $collections = $this->fetchRowset(
0266                 $this->_join . ' ' . $statementOption
0267                 . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0268                 $values
0269             );
0270             $this->setColumns($columns);
0271 
0272             if (!$collections) {
0273                 return null;
0274             }
0275 
0276             $this->setColumns($this->_columns);
0277             $pagination = Flooer_Utility_Pagination::paginate(
0278                 $this->count($this->_join . ' ' . $statementOption, $values),
0279                 $perpage,
0280                 $page
0281             );
0282             $this->setColumns($columns);
0283         }
0284 
0285         return array(
0286             'collections' => $collections,
0287             'pagination' => $pagination
0288         );
0289     }
0290 
0291     public function getCollection($id)
0292     {
0293         $prefix = $this->getPrefix();
0294         $columns = $this->getColumns();
0295 
0296         $this->setColumns($this->_columns);
0297         $collection = $this->fetchRow(
0298             $this->_join
0299             . " WHERE {$prefix}collections.id = :id"
0300             . ' LIMIT 1',
0301             array(':id' => $id)
0302         );
0303         $this->setColumns($columns);
0304 
0305         if ($collection) {
0306             return $collection;
0307         }
0308         return null;
0309     }
0310 
0311     public function updateDownloadedStatus($id)
0312     {
0313         if (isset($this->$id)) {
0314             parent::__set($id, array(
0315                 'downloaded_timestamp' => $this->_getTimestamp(),
0316                 'downloaded_ip' => $this->_getIp(),
0317                 'downloaded_count' => $this->$id->downloaded_count + 1
0318             ));
0319         }
0320     }
0321 
0322 }