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_profiles extends BaseModel
0025 {
0026 
0027     public function __construct(&$db)
0028     {
0029         parent::__construct($db, $db->getTableConfig());
0030         $this->setName('profiles');
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 getProfiles($status = 'active', $clientId = null, $ownerId = null, $search = null, $ids = null, array $favoriteIds = null, $sort = 'name', $perpage = 20, $page = 1)
0042     {
0043         $statementOption = '';
0044         $where = array();
0045         $values = array();
0046         $order = 'name ASC';
0047         $offset = 0;
0048 
0049         if ($status != 'all') {
0050             $active = 1;
0051             if ($status == 'inactive') {
0052                 $active = 0;
0053             }
0054             $where[] = 'active = :active';
0055             $values[':active'] = $active;
0056         }
0057         if ($clientId) {
0058             $where[] = 'client_id = :client_id';
0059             $values[':client_id'] = $clientId;
0060         }
0061         if ($ownerId) {
0062             $where[] = 'owner_id = :owner_id';
0063             $values[':owner_id'] = $ownerId;
0064         }
0065         if ($search) {
0066             $isSearchable = false;
0067             foreach (explode(' ', $search) as $keyword) {
0068                 if ($keyword && strlen($keyword) > 2) {
0069                     $keyword = $this->getDb()->quote("%$keyword%");
0070                     $where[] = "(name LIKE $keyword"
0071                         . " OR description LIKE $keyword)";
0072                     $isSearchable = true;
0073                 }
0074             }
0075             if (!$isSearchable) {
0076                 return null;
0077             }
0078         }
0079         if ($ids) {
0080             $_ids = array();
0081             foreach (explode(',', $ids) as $id) {
0082                 $id = trim($id);
0083                 if ($id) {
0084                     $_ids[] = $this->getDb()->quote($id);
0085                 }
0086             }
0087             if ($_ids) {
0088                 $where[] = 'id IN (' . implode(',', $_ids) . ')';
0089             }
0090         }
0091         if (!empty($favoriteIds['ownerIds'])) {
0092             $where[] = $this->_convertFavoriteIdsToStatement(
0093                 $favoriteIds,
0094                 array('ownerId' => 'owner_id')
0095             );
0096         }
0097 
0098         if ($where) {
0099             $statementOption = 'WHERE ' . implode(' AND ', $where);
0100         }
0101 
0102         if ($sort == 'newest') {
0103             $order = 'id DESC';
0104         }
0105 
0106         if ($page > 1) {
0107             $offset = ($page - 1) * $perpage;
0108         }
0109 
0110         $profiles = $this->fetchRowset(
0111             $statementOption
0112             . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0113             $values
0114         );
0115 
0116         if (!$profiles) {
0117             return null;
0118         }
0119 
0120         $pagination = Flooer_Utility_Pagination::paginate(
0121             $this->count($statementOption, $values),
0122             $perpage,
0123             $page
0124         );
0125 
0126         return array(
0127             'profiles' => $profiles,
0128             'pagination' => $pagination
0129         );
0130     }
0131 
0132     public function getProfile($id)
0133     {
0134         return $this->fetchRow(
0135             'WHERE id = :id'
0136             . ' LIMIT 1',
0137             array(':id' => $id)
0138         );
0139     }
0140 
0141     public function getProfileByClientIdAndOwnerId($clientId, $ownerId)
0142     {
0143         return $this->fetchRow(
0144             'WHERE client_id = :client_id'
0145             . ' AND owner_id = :owner_id'
0146             . ' LIMIT 1',
0147             array(
0148                 ':client_id' => $clientId,
0149                 ':owner_id' => $ownerId
0150             )
0151         );
0152     }
0153 
0154 }