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_favorites extends BaseModel
0025 {
0026 
0027     public function __construct(&$db)
0028     {
0029         parent::__construct($db, $db->getTableConfig());
0030         $this->setName('favorites');
0031         $this->setPrimaryInsert(true);
0032     }
0033 
0034     public function __set($key, $value)
0035     {
0036         $value = $this->_convertArrayToObject($value);
0037         unset($value->id);
0038         parent::__set($key, $value);
0039     }
0040 
0041     public function getFavorites($clientId = null, $userId = null, $ownerId = null, $collectionId = null, $fileId = null, $ids = null, $perpage = 20, $page = 1)
0042     {
0043         $statementOption = '';
0044         $where = array();
0045         $values = array();
0046         $offset = 0;
0047 
0048         if ($clientId) {
0049             $where[] = 'client_id = :client_id';
0050             $values[':client_id'] = $clientId;
0051         }
0052         if ($userId) {
0053             $where[] = 'user_id = :user_id';
0054             $values[':user_id'] = $userId;
0055         }
0056         if ($ownerId) {
0057             $where[] = 'owner_id = :owner_id';
0058             $values[':owner_id'] = $ownerId;
0059         }
0060         if ($collectionId) {
0061             $where[] = 'collection_id = :collection_id';
0062             $values[':collection_id'] = $collectionId;
0063         }
0064         if ($fileId) {
0065             $where[] = 'file_id = :file_id';
0066             $values[':file_id'] = $fileId;
0067         }
0068         if ($ids) {
0069             $_ids = array();
0070             foreach (explode(',', $ids) as $id) {
0071                 $id = trim($id);
0072                 if ($id) {
0073                     $_ids[] = $this->getDb()->quote($id);
0074                 }
0075             }
0076             if ($_ids) {
0077                 $where[] = 'id IN (' . implode(',', $_ids) . ')';
0078             }
0079         }
0080 
0081         if ($where) {
0082             $statementOption = 'WHERE ' . implode(' AND ', $where);
0083         }
0084 
0085         if ($page > 1) {
0086             $offset = ($page - 1) * $perpage;
0087         }
0088 
0089         $favorites = $this->fetchRowset(
0090             $statementOption
0091             . " ORDER BY id ASC LIMIT $perpage OFFSET $offset",
0092             $values
0093         );
0094 
0095         if (!$favorites) {
0096             return null;
0097         }
0098 
0099         $pagination = Flooer_Utility_Pagination::paginate(
0100             $this->count($statementOption, $values),
0101             $perpage,
0102             $page
0103         );
0104 
0105         return array(
0106             'favorites' => $favorites,
0107             'pagination' => $pagination
0108         );
0109     }
0110 
0111     public function getFavoriteOwners($clientId, $userId)
0112     {
0113         return $this->fetchRowset(
0114             'WHERE client_id = :client_id'
0115             . ' AND user_id = :user_id'
0116             . ' AND owner_id IS NOT NULL'
0117             . ' AND collection_id IS NULL'
0118             . ' AND file_id IS NULL',
0119             array(
0120                 ':client_id' => $clientId,
0121                 ':user_id' => $userId
0122             )
0123         );
0124     }
0125 
0126     public function getFavoriteOwner($clientId, $userId, $ownerId)
0127     {
0128         return $this->fetchRow(
0129             'WHERE client_id = :client_id'
0130             . ' AND user_id = :user_id'
0131             . ' AND owner_id = :owner_id'
0132             . ' AND collection_id IS NULL'
0133             . ' AND file_id IS NULL'
0134             . ' LIMIT 1',
0135             array(
0136                 ':client_id' => $clientId,
0137                 ':user_id' => $userId,
0138                 ':owner_id' => $ownerId
0139             )
0140         );
0141     }
0142 
0143     public function getFavoriteCollections($clientId, $userId)
0144     {
0145         return $this->fetchRowset(
0146             'WHERE client_id = :client_id'
0147             . ' AND user_id = :user_id'
0148             . ' AND collection_id IS NOT NULL'
0149             . ' AND file_id IS NULL',
0150             array(
0151                 ':client_id' => $clientId,
0152                 ':user_id' => $userId
0153             )
0154         );
0155     }
0156 
0157     public function getFavoriteCollection($clientId, $userId, $collectionId)
0158     {
0159         return $this->fetchRow(
0160             'WHERE client_id = :client_id'
0161             . ' AND user_id = :user_id'
0162             . ' AND collection_id = :collection_id'
0163             . ' AND file_id IS NULL'
0164             . ' LIMIT 1',
0165             array(
0166                 ':client_id' => $clientId,
0167                 ':user_id' => $userId,
0168                 ':collection_id' => $collectionId
0169             )
0170         );
0171     }
0172 
0173     public function getFavoriteFiles($clientId, $userId)
0174     {
0175         return $this->fetchRowset(
0176             'WHERE client_id = :client_id'
0177             . ' AND user_id = :user_id'
0178             . ' AND file_id IS NOT NULL',
0179             array(
0180                 ':client_id' => $clientId,
0181                 ':user_id' => $userId
0182             )
0183         );
0184     }
0185 
0186     public function getFavoriteFile($clientId, $userId, $fileId)
0187     {
0188         return $this->fetchRow(
0189             'WHERE client_id = :client_id'
0190             . ' AND user_id = :user_id'
0191             . ' AND file_id = :file_id'
0192             . ' LIMIT 1',
0193             array(
0194                 ':client_id' => $clientId,
0195                 ':user_id' => $userId,
0196                 ':file_id' => $fileId
0197             )
0198         );
0199     }
0200 
0201     public function deleteByCollectionId($collectionId)
0202     {
0203         $primary = $this->getPrimary();
0204         $this->setPrimary('collection_id');
0205         unset($this->$collectionId);
0206         $this->setPrimary($primary);
0207     }
0208 
0209     public function deleteByFileId($fileId)
0210     {
0211         $primary = $this->getPrimary();
0212         $this->setPrimary('file_id');
0213         unset($this->$fileId);
0214         $this->setPrimary($primary);
0215     }
0216 
0217 }