File indexing completed on 2024-04-14 15:55:12

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_media 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('media');
0035         $this->setPrimaryInsert(true);
0036 
0037         $prefix = $this->getPrefix();
0038 
0039         $this->_columns = "{$prefix}media.id AS id,"
0040             . "{$prefix}media.client_id AS client_id,"
0041             . "{$prefix}media.owner_id AS owner_id,"
0042             . "{$prefix}profiles.id AS profile_id,"
0043             . "{$prefix}profiles.name AS profile_name,"
0044             . "{$prefix}media.collection_id AS collection_id,"
0045             . "{$prefix}collections.title AS collection_title,"
0046             . "{$prefix}collections.category AS collection_category,"
0047             . "{$prefix}collections.tags AS collection_tags,"
0048             . "{$prefix}collections.version AS collection_version,"
0049             . "{$prefix}collections.content_id AS collection_content_id,"
0050             . "{$prefix}collections.content_page AS collection_content_page,"
0051             . "{$prefix}media.file_id AS file_id,"
0052             . "{$prefix}files.name AS file_name,"
0053             . "{$prefix}files.type AS file_type,"
0054             . "{$prefix}files.size AS file_size,"
0055             . "{$prefix}files.title AS file_title,"
0056             . "{$prefix}files.category AS file_category,"
0057             . "{$prefix}files.tags AS file_tags,"
0058             . "{$prefix}files.version AS file_version,"
0059             . "{$prefix}files.ocs_compatible AS file_ocs_compatible,"
0060             . "{$prefix}files.content_id AS file_content_id,"
0061             . "{$prefix}files.content_page AS file_content_page,"
0062             . "{$prefix}media.artist_id AS artist_id,"
0063             . "{$prefix}media_artists.name AS artist_name,"
0064             . "{$prefix}media.album_id AS album_id,"
0065             . "{$prefix}media_albums.name AS album_name,"
0066             . "{$prefix}media.title AS title,"
0067             . "{$prefix}media.genre AS genre,"
0068             . "{$prefix}media.track AS track,"
0069             . "{$prefix}media.creationdate AS creationdate,"
0070             . "{$prefix}media.bitrate AS bitrate,"
0071             . "{$prefix}media.playtime_seconds AS playtime_seconds,"
0072             . "{$prefix}media.playtime_string AS playtime_string,"
0073             . "{$prefix}media.played_timestamp AS played_timestamp,"
0074             . "{$prefix}media.played_count AS played_count,"
0075             . "{$prefix}media.played_count AS played_timeperiod_count";
0076 
0077         $this->_join = "LEFT OUTER JOIN {$prefix}profiles"
0078             . " ON {$prefix}profiles.client_id = {$prefix}media.client_id"
0079             . " AND {$prefix}profiles.owner_id = {$prefix}media.owner_id"
0080             . " LEFT OUTER JOIN {$prefix}collections"
0081             . " ON {$prefix}collections.id = {$prefix}media.collection_id"
0082             . " LEFT OUTER JOIN {$prefix}files"
0083             . " ON {$prefix}files.id = {$prefix}media.file_id"
0084             . " LEFT OUTER JOIN {$prefix}media_artists"
0085             . " ON {$prefix}media_artists.id = {$prefix}media.artist_id"
0086             . " LEFT OUTER JOIN {$prefix}media_albums"
0087             . " ON {$prefix}media_albums.id = {$prefix}media.album_id";
0088     }
0089 
0090     public function __set($key, $value)
0091     {
0092         $value = $this->_convertArrayToObject($value);
0093         unset($value->id);
0094         if (!isset($this->$key)) {
0095             $value->played_timestamp = $this->_getTimestamp();
0096             $value->played_ip = $this->_getIp();
0097             $value->played_count = 0;
0098         }
0099         parent::__set($key, $value);
0100     }
0101 
0102     public function getGenres($clientId = null, $ownerId = null, $collectionId = null, $collectionCategory = null, $collectionTags = null, $collectionContentId = null, $fileId = null, $fileTypes = null, $fileCategory = null, $fileTags = null, $fileOcsCompatibility = 'all', $fileContentId = null, $artistId = null, $albumId = null, $genre = null, $search = null, array $favoriteIds = null, $sort = 'name', $perpage = 20, $page = 1)
0103     {
0104         $prefix = $this->getPrefix();
0105         $columns = $this->getColumns();
0106 
0107         $_columns = "DISTINCT {$prefix}media.client_id AS client_id,"
0108             . "{$prefix}media.genre AS genre";
0109 
0110         $_join = "LEFT OUTER JOIN {$prefix}collections"
0111             . " ON {$prefix}collections.id = {$prefix}media.collection_id"
0112             . " LEFT OUTER JOIN {$prefix}files"
0113             . " ON {$prefix}files.id = {$prefix}media.file_id";
0114 
0115         $statementOption = '';
0116         $where = array();
0117         $values = array();
0118         $order = "{$prefix}media.genre ASC";
0119         $offset = 0;
0120 
0121         if ($clientId) {
0122             $where[] = "{$prefix}media.client_id = :client_id";
0123             $values[':client_id'] = $clientId;
0124         }
0125         if ($ownerId) {
0126             $where[] = "{$prefix}media.owner_id = :owner_id";
0127             $values[':owner_id'] = $ownerId;
0128         }
0129         if ($collectionId) {
0130             $where[] = "{$prefix}media.collection_id = :collection_id";
0131             $values[':collection_id'] = $collectionId;
0132         }
0133         if ($collectionCategory !== null && $collectionCategory !== '') {
0134             $where[] = "{$prefix}collections.category = :collection_category";
0135             $values[':collection_category'] = $collectionCategory;
0136         }
0137         if ($collectionTags !== null && $collectionTags !== '') {
0138             foreach (explode(',', $collectionTags) as $tag) {
0139                 $tag = trim($tag);
0140                 if ($tag) {
0141                     $where[] = "({$prefix}collections.tags = " . $this->getDb()->quote($tag)
0142                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("$tag,%")
0143                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0144                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0145                 }
0146             }
0147         }
0148         if ($collectionContentId !== null && $collectionContentId !== '') {
0149             $where[] = "{$prefix}collections.content_id = :collection_content_id";
0150             $values[':collection_content_id'] = $collectionContentId;
0151         }
0152         if ($fileId) {
0153             $where[] = "{$prefix}media.file_id = :file_id";
0154             $values[':file_id'] = $fileId;
0155         }
0156         if ($fileTypes) {
0157             $_fileTypes = array();
0158             foreach (explode(',', $fileTypes) as $fileType) {
0159                 $fileType = trim($fileType);
0160                 if ($fileType) {
0161                     $_fileTypes[] = $this->getDb()->quote($fileType);
0162                 }
0163             }
0164             if ($_fileTypes) {
0165                 $where[] = "{$prefix}files.type IN (" . implode(',', $_fileTypes) . ')';
0166             }
0167         }
0168         if ($fileCategory !== null && $fileCategory !== '') {
0169             $where[] = "{$prefix}files.category = :file_category";
0170             $values[':file_category'] = $fileCategory;
0171         }
0172         if ($fileTags !== null && $fileTags !== '') {
0173             foreach (explode(',', $fileTags) as $tag) {
0174                 $tag = trim($tag);
0175                 if ($tag) {
0176                     $where[] = "({$prefix}files.tags = " . $this->getDb()->quote($tag)
0177                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("$tag,%")
0178                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0179                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0180                 }
0181             }
0182         }
0183         if ($fileOcsCompatibility != 'all') {
0184             $fileOcsCompatible = null;
0185             if ($fileOcsCompatibility == 'compatible') {
0186                 $fileOcsCompatible = 1;
0187             }
0188             else if ($fileOcsCompatibility == 'incompatible') {
0189                 $fileOcsCompatible = 0;
0190             }
0191             if ($fileOcsCompatible !== null) {
0192                 $where[] = "{$prefix}files.ocs_compatible = :file_ocs_compatible";
0193                 $values[':file_ocs_compatible'] = $fileOcsCompatible;
0194             }
0195         }
0196         if ($fileContentId !== null && $fileContentId !== '') {
0197             $where[] = "{$prefix}files.content_id = :file_content_id";
0198             $values[':file_content_id'] = $fileContentId;
0199         }
0200         if ($artistId) {
0201             $where[] = "{$prefix}media.artist_id = :artist_id";
0202             $values[':artist_id'] = $artistId;
0203         }
0204         if ($albumId) {
0205             $where[] = "{$prefix}media.album_id = :album_id";
0206             $values[':album_id'] = $albumId;
0207         }
0208         if ($genre) {
0209             $where[] = "{$prefix}media.genre = :genre";
0210             $values[':genre'] = $genre;
0211         }
0212         if ($search) {
0213             $isSearchable = false;
0214             $_genre = array();
0215             foreach (explode(' ', $search) as $keyword) {
0216                 if ($keyword && strlen($keyword) > 2) {
0217                     $keyword = $this->getDb()->quote("%$keyword%");
0218                     $_genre[] = "{$prefix}media.genre LIKE $keyword";
0219                     $isSearchable = true;
0220                 }
0221             }
0222             if (!$isSearchable) {
0223                 return null;
0224             }
0225             $where[] = '(' . implode(' OR ', $_genre) . ')';
0226         }
0227         if (!empty($favoriteIds['ownerIds'])
0228             || !empty($favoriteIds['collectionIds'])
0229             || !empty($favoriteIds['fileIds'])
0230         ) {
0231             $where[] = $this->_convertFavoriteIdsToStatement(
0232                 $favoriteIds,
0233                 array(
0234                     'ownerId' => "{$prefix}media.owner_id",
0235                     'collectionId' => "{$prefix}media.collection_id",
0236                     'fileId' => "{$prefix}media.file_id"
0237                 )
0238             );
0239         }
0240 
0241         if ($where) {
0242             $statementOption = 'WHERE ' . implode(' AND ', $where)
0243                 . " AND {$prefix}media.genre IS NOT NULL";
0244         }
0245         else {
0246             $statementOption = "WHERE {$prefix}media.genre IS NOT NULL";
0247         }
0248 
0249         if ($sort == 'newest') {
0250             $order = "{$prefix}media.id DESC";
0251         }
0252 
0253         if ($page > 1) {
0254             $offset = ($page - 1) * $perpage;
0255         }
0256 
0257         $this->setColumns($_columns);
0258         $genres = $this->fetchRowset(
0259             $_join . ' ' . $statementOption
0260             . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0261             $values
0262         );
0263         $this->setColumns($columns);
0264 
0265         if (!$genres) {
0266             return null;
0267         }
0268 
0269         $this->setColumns($_columns);
0270         $pagination = Flooer_Utility_Pagination::paginate(
0271             count((array) $this->fetchRowset($_join . ' ' . $statementOption, $values)),
0272             $perpage,
0273             $page
0274         );
0275         $this->setColumns($columns);
0276 
0277         return array(
0278             'genres' => $genres,
0279             'pagination' => $pagination
0280         );
0281     }
0282 
0283     public function getOwners($clientId = null, $ownerId = null, $collectionId = null, $collectionCategory = null, $collectionTags = null, $collectionContentId = null, $fileId = null, $fileTypes = null, $fileCategory = null, $fileTags = null, $fileOcsCompatibility = 'all', $fileContentId = null, $artistId = null, $albumId = null, $genre = null, $search = null, array $favoriteIds = null, $sort = 'name', $perpage = 20, $page = 1)
0284     {
0285         $prefix = $this->getPrefix();
0286         $columns = $this->getColumns();
0287 
0288         $_columns = "DISTINCT {$prefix}media.client_id AS client_id,"
0289             . "{$prefix}media.owner_id AS owner_id,"
0290             . "{$prefix}profiles.id AS profile_id,"
0291             . "{$prefix}profiles.name AS profile_name";
0292 
0293         $_join = "LEFT OUTER JOIN {$prefix}profiles"
0294             . " ON {$prefix}profiles.client_id = {$prefix}media.client_id"
0295             . " AND {$prefix}profiles.owner_id = {$prefix}media.owner_id"
0296             . " LEFT OUTER JOIN {$prefix}collections"
0297             . " ON {$prefix}collections.id = {$prefix}media.collection_id"
0298             . " LEFT OUTER JOIN {$prefix}files"
0299             . " ON {$prefix}files.id = {$prefix}media.file_id";
0300 
0301         $statementOption = '';
0302         $where = array();
0303         $values = array();
0304         $order = "{$prefix}profiles.name ASC";
0305         $offset = 0;
0306 
0307         if ($clientId) {
0308             $where[] = "{$prefix}media.client_id = :client_id";
0309             $values[':client_id'] = $clientId;
0310         }
0311         if ($ownerId) {
0312             $where[] = "{$prefix}media.owner_id = :owner_id";
0313             $values[':owner_id'] = $ownerId;
0314         }
0315         if ($collectionId) {
0316             $where[] = "{$prefix}media.collection_id = :collection_id";
0317             $values[':collection_id'] = $collectionId;
0318         }
0319         if ($collectionCategory !== null && $collectionCategory !== '') {
0320             $where[] = "{$prefix}collections.category = :collection_category";
0321             $values[':collection_category'] = $collectionCategory;
0322         }
0323         if ($collectionTags !== null && $collectionTags !== '') {
0324             foreach (explode(',', $collectionTags) as $tag) {
0325                 $tag = trim($tag);
0326                 if ($tag) {
0327                     $where[] = "({$prefix}collections.tags = " . $this->getDb()->quote($tag)
0328                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("$tag,%")
0329                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0330                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0331                 }
0332             }
0333         }
0334         if ($collectionContentId !== null && $collectionContentId !== '') {
0335             $where[] = "{$prefix}collections.content_id = :collection_content_id";
0336             $values[':collection_content_id'] = $collectionContentId;
0337         }
0338         if ($fileId) {
0339             $where[] = "{$prefix}media.file_id = :file_id";
0340             $values[':file_id'] = $fileId;
0341         }
0342         if ($fileTypes) {
0343             $_fileTypes = array();
0344             foreach (explode(',', $fileTypes) as $fileType) {
0345                 $fileType = trim($fileType);
0346                 if ($fileType) {
0347                     $_fileTypes[] = $this->getDb()->quote($fileType);
0348                 }
0349             }
0350             if ($_fileTypes) {
0351                 $where[] = "{$prefix}files.type IN (" . implode(',', $_fileTypes) . ')';
0352             }
0353         }
0354         if ($fileCategory !== null && $fileCategory !== '') {
0355             $where[] = "{$prefix}files.category = :file_category";
0356             $values[':file_category'] = $fileCategory;
0357         }
0358         if ($fileTags !== null && $fileTags !== '') {
0359             foreach (explode(',', $fileTags) as $tag) {
0360                 $tag = trim($tag);
0361                 if ($tag) {
0362                     $where[] = "({$prefix}files.tags = " . $this->getDb()->quote($tag)
0363                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("$tag,%")
0364                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0365                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0366                 }
0367             }
0368         }
0369         if ($fileOcsCompatibility != 'all') {
0370             $fileOcsCompatible = null;
0371             if ($fileOcsCompatibility == 'compatible') {
0372                 $fileOcsCompatible = 1;
0373             }
0374             else if ($fileOcsCompatibility == 'incompatible') {
0375                 $fileOcsCompatible = 0;
0376             }
0377             if ($fileOcsCompatible !== null) {
0378                 $where[] = "{$prefix}files.ocs_compatible = :file_ocs_compatible";
0379                 $values[':file_ocs_compatible'] = $fileOcsCompatible;
0380             }
0381         }
0382         if ($fileContentId !== null && $fileContentId !== '') {
0383             $where[] = "{$prefix}files.content_id = :file_content_id";
0384             $values[':file_content_id'] = $fileContentId;
0385         }
0386         if ($artistId) {
0387             $where[] = "{$prefix}media.artist_id = :artist_id";
0388             $values[':artist_id'] = $artistId;
0389         }
0390         if ($albumId) {
0391             $where[] = "{$prefix}media.album_id = :album_id";
0392             $values[':album_id'] = $albumId;
0393         }
0394         if ($genre) {
0395             $where[] = "{$prefix}media.genre = :genre";
0396             $values[':genre'] = $genre;
0397         }
0398         if ($search) {
0399             $isSearchable = false;
0400             $_profile = array();
0401             foreach (explode(' ', $search) as $keyword) {
0402                 if ($keyword && strlen($keyword) > 2) {
0403                     $keyword = $this->getDb()->quote("%$keyword%");
0404                     $_profile[] = "{$prefix}profiles.name LIKE $keyword";
0405                     $isSearchable = true;
0406                 }
0407             }
0408             if (!$isSearchable) {
0409                 return null;
0410             }
0411             $where[] = '(' . implode(' OR ', $_profile) . ')';
0412         }
0413         if (!empty($favoriteIds['ownerIds'])
0414             || !empty($favoriteIds['collectionIds'])
0415             || !empty($favoriteIds['fileIds'])
0416         ) {
0417             $where[] = $this->_convertFavoriteIdsToStatement(
0418                 $favoriteIds,
0419                 array(
0420                     'ownerId' => "{$prefix}media.owner_id",
0421                     'collectionId' => "{$prefix}media.collection_id",
0422                     'fileId' => "{$prefix}media.file_id"
0423                 )
0424             );
0425         }
0426 
0427         if ($where) {
0428             $statementOption = 'WHERE ' . implode(' AND ', $where);
0429         }
0430 
0431         if ($sort == 'newest') {
0432             $order = "{$prefix}media.owner_id DESC";
0433         }
0434 
0435         if ($page > 1) {
0436             $offset = ($page - 1) * $perpage;
0437         }
0438 
0439         $this->setColumns($_columns);
0440         $owners = $this->fetchRowset(
0441             $_join . ' ' . $statementOption
0442             . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0443             $values
0444         );
0445         $this->setColumns($columns);
0446 
0447         if (!$owners) {
0448             return null;
0449         }
0450 
0451         $this->setColumns($_columns);
0452         $pagination = Flooer_Utility_Pagination::paginate(
0453             count((array) $this->fetchRowset($_join . ' ' . $statementOption, $values)),
0454             $perpage,
0455             $page
0456         );
0457         $this->setColumns($columns);
0458 
0459         return array(
0460             'owners' => $owners,
0461             'pagination' => $pagination
0462         );
0463     }
0464 
0465     public function getCollections($clientId = null, $ownerId = null, $collectionId = null, $collectionCategory = null, $collectionTags = null, $collectionContentId = null, $fileId = null, $fileTypes = null, $fileCategory = null, $fileTags = null, $fileOcsCompatibility = 'all', $fileContentId = null, $artistId = null, $albumId = null, $genre = null, $search = null, array $favoriteIds = null, $sort = 'name', $perpage = 20, $page = 1)
0466     {
0467         $prefix = $this->getPrefix();
0468         $columns = $this->getColumns();
0469 
0470         $_columns = "DISTINCT {$prefix}media.client_id AS client_id,"
0471             . "{$prefix}media.owner_id AS owner_id,"
0472             . "{$prefix}profiles.id AS profile_id,"
0473             . "{$prefix}profiles.name AS profile_name,"
0474             . "{$prefix}media.collection_id AS collection_id,"
0475             . "{$prefix}collections.title AS collection_title";
0476 
0477         $_join = "LEFT OUTER JOIN {$prefix}profiles"
0478             . " ON {$prefix}profiles.client_id = {$prefix}media.client_id"
0479             . " AND {$prefix}profiles.owner_id = {$prefix}media.owner_id"
0480             . " LEFT OUTER JOIN {$prefix}collections"
0481             . " ON {$prefix}collections.id = {$prefix}media.collection_id"
0482             . " LEFT OUTER JOIN {$prefix}files"
0483             . " ON {$prefix}files.id = {$prefix}media.file_id";
0484 
0485         $statementOption = '';
0486         $where = array();
0487         $values = array();
0488         $order = "{$prefix}collections.title ASC";
0489         $offset = 0;
0490 
0491         if ($clientId) {
0492             $where[] = "{$prefix}media.client_id = :client_id";
0493             $values[':client_id'] = $clientId;
0494         }
0495         if ($ownerId) {
0496             $where[] = "{$prefix}media.owner_id = :owner_id";
0497             $values[':owner_id'] = $ownerId;
0498         }
0499         if ($collectionId) {
0500             $where[] = "{$prefix}media.collection_id = :collection_id";
0501             $values[':collection_id'] = $collectionId;
0502         }
0503         if ($collectionCategory !== null && $collectionCategory !== '') {
0504             $where[] = "{$prefix}collections.category = :collection_category";
0505             $values[':collection_category'] = $collectionCategory;
0506         }
0507         if ($collectionTags !== null && $collectionTags !== '') {
0508             foreach (explode(',', $collectionTags) as $tag) {
0509                 $tag = trim($tag);
0510                 if ($tag) {
0511                     $where[] = "({$prefix}collections.tags = " . $this->getDb()->quote($tag)
0512                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("$tag,%")
0513                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0514                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0515                 }
0516             }
0517         }
0518         if ($collectionContentId !== null && $collectionContentId !== '') {
0519             $where[] = "{$prefix}collections.content_id = :collection_content_id";
0520             $values[':collection_content_id'] = $collectionContentId;
0521         }
0522         if ($fileId) {
0523             $where[] = "{$prefix}media.file_id = :file_id";
0524             $values[':file_id'] = $fileId;
0525         }
0526         if ($fileTypes) {
0527             $_fileTypes = array();
0528             foreach (explode(',', $fileTypes) as $fileType) {
0529                 $fileType = trim($fileType);
0530                 if ($fileType) {
0531                     $_fileTypes[] = $this->getDb()->quote($fileType);
0532                 }
0533             }
0534             if ($_fileTypes) {
0535                 $where[] = "{$prefix}files.type IN (" . implode(',', $_fileTypes) . ')';
0536             }
0537         }
0538         if ($fileCategory !== null && $fileCategory !== '') {
0539             $where[] = "{$prefix}files.category = :file_category";
0540             $values[':file_category'] = $fileCategory;
0541         }
0542         if ($fileTags !== null && $fileTags !== '') {
0543             foreach (explode(',', $fileTags) as $tag) {
0544                 $tag = trim($tag);
0545                 if ($tag) {
0546                     $where[] = "({$prefix}files.tags = " . $this->getDb()->quote($tag)
0547                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("$tag,%")
0548                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0549                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0550                 }
0551             }
0552         }
0553         if ($fileOcsCompatibility != 'all') {
0554             $fileOcsCompatible = null;
0555             if ($fileOcsCompatibility == 'compatible') {
0556                 $fileOcsCompatible = 1;
0557             }
0558             else if ($fileOcsCompatibility == 'incompatible') {
0559                 $fileOcsCompatible = 0;
0560             }
0561             if ($fileOcsCompatible !== null) {
0562                 $where[] = "{$prefix}files.ocs_compatible = :file_ocs_compatible";
0563                 $values[':file_ocs_compatible'] = $fileOcsCompatible;
0564             }
0565         }
0566         if ($fileContentId !== null && $fileContentId !== '') {
0567             $where[] = "{$prefix}files.content_id = :file_content_id";
0568             $values[':file_content_id'] = $fileContentId;
0569         }
0570         if ($artistId) {
0571             $where[] = "{$prefix}media.artist_id = :artist_id";
0572             $values[':artist_id'] = $artistId;
0573         }
0574         if ($albumId) {
0575             $where[] = "{$prefix}media.album_id = :album_id";
0576             $values[':album_id'] = $albumId;
0577         }
0578         if ($genre) {
0579             $where[] = "{$prefix}media.genre = :genre";
0580             $values[':genre'] = $genre;
0581         }
0582         if ($search) {
0583             $isSearchable = false;
0584             $_collection = array();
0585             foreach (explode(' ', $search) as $keyword) {
0586                 if ($keyword && strlen($keyword) > 2) {
0587                     $keyword = $this->getDb()->quote("%$keyword%");
0588                     $_collection[] = "{$prefix}collections.title LIKE $keyword";
0589                     $isSearchable = true;
0590                 }
0591             }
0592             if (!$isSearchable) {
0593                 return null;
0594             }
0595             $where[] = '(' . implode(' OR ', $_collection) . ')';
0596         }
0597         if (!empty($favoriteIds['ownerIds'])
0598             || !empty($favoriteIds['collectionIds'])
0599             || !empty($favoriteIds['fileIds'])
0600         ) {
0601             $where[] = $this->_convertFavoriteIdsToStatement(
0602                 $favoriteIds,
0603                 array(
0604                     'ownerId' => "{$prefix}media.owner_id",
0605                     'collectionId' => "{$prefix}media.collection_id",
0606                     'fileId' => "{$prefix}media.file_id"
0607                 )
0608             );
0609         }
0610 
0611         if ($where) {
0612             $statementOption = 'WHERE ' . implode(' AND ', $where);
0613         }
0614 
0615         if ($sort == 'newest') {
0616             $order = "{$prefix}media.collection_id DESC";
0617         }
0618 
0619         if ($page > 1) {
0620             $offset = ($page - 1) * $perpage;
0621         }
0622 
0623         $this->setColumns($_columns);
0624         $collections = $this->fetchRowset(
0625             $_join . ' ' . $statementOption
0626             . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0627             $values
0628         );
0629         $this->setColumns($columns);
0630 
0631         if (!$collections) {
0632             return null;
0633         }
0634 
0635         $this->setColumns($_columns);
0636         $pagination = Flooer_Utility_Pagination::paginate(
0637             count((array) $this->fetchRowset($_join . ' ' . $statementOption, $values)),
0638             $perpage,
0639             $page
0640         );
0641         $this->setColumns($columns);
0642 
0643         return array(
0644             'collections' => $collections,
0645             'pagination' => $pagination
0646         );
0647     }
0648 
0649     public function getIndex($clientId = null, $ownerId = null, $collectionId = null, $collectionCategory = null, $collectionTags = null, $collectionContentId = null, $fileId = null, $fileTypes = null, $fileCategory = null, $fileTags = null, $fileOcsCompatibility = 'all', $fileContentId = null, $artistId = null, $albumId = null, $genre = null, $search = null, $ids = null, array $favoriteIds = null, $playedTimeperiodBegin = null, $playedTimeperiodEnd = null, $sort = 'name', $perpage = 20, $page = 1)
0650     {
0651         $prefix = $this->getPrefix();
0652         $name = $this->getName();
0653         $columns = $this->getColumns();
0654 
0655         $statementOption = '';
0656         $where = array();
0657         $values = array();
0658         $order = "{$prefix}media.title ASC";
0659         $offset = 0;
0660 
0661         if ($clientId) {
0662             $where[] = "{$prefix}media.client_id = :client_id";
0663             $values[':client_id'] = $clientId;
0664         }
0665         if ($ownerId) {
0666             $where[] = "{$prefix}media.owner_id = :owner_id";
0667             $values[':owner_id'] = $ownerId;
0668         }
0669         if ($collectionId) {
0670             $where[] = "{$prefix}media.collection_id = :collection_id";
0671             $values[':collection_id'] = $collectionId;
0672         }
0673         if ($collectionCategory !== null && $collectionCategory !== '') {
0674             $where[] = "{$prefix}collections.category = :collection_category";
0675             $values[':collection_category'] = $collectionCategory;
0676         }
0677         if ($collectionTags !== null && $collectionTags !== '') {
0678             foreach (explode(',', $collectionTags) as $tag) {
0679                 $tag = trim($tag);
0680                 if ($tag) {
0681                     $where[] = "({$prefix}collections.tags = " . $this->getDb()->quote($tag)
0682                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("$tag,%")
0683                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0684                         . " OR {$prefix}collections.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0685                 }
0686             }
0687         }
0688         if ($collectionContentId !== null && $collectionContentId !== '') {
0689             $where[] = "{$prefix}collections.content_id = :collection_content_id";
0690             $values[':collection_content_id'] = $collectionContentId;
0691         }
0692         if ($fileId) {
0693             $where[] = "{$prefix}media.file_id = :file_id";
0694             $values[':file_id'] = $fileId;
0695         }
0696         if ($fileTypes) {
0697             $_fileTypes = array();
0698             foreach (explode(',', $fileTypes) as $fileType) {
0699                 $fileType = trim($fileType);
0700                 if ($fileType) {
0701                     $_fileTypes[] = $this->getDb()->quote($fileType);
0702                 }
0703             }
0704             if ($_fileTypes) {
0705                 $where[] = "{$prefix}files.type IN (" . implode(',', $_fileTypes) . ')';
0706             }
0707         }
0708         if ($fileCategory !== null && $fileCategory !== '') {
0709             $where[] = "{$prefix}files.category = :file_category";
0710             $values[':file_category'] = $fileCategory;
0711         }
0712         if ($fileTags !== null && $fileTags !== '') {
0713             foreach (explode(',', $fileTags) as $tag) {
0714                 $tag = trim($tag);
0715                 if ($tag) {
0716                     $where[] = "({$prefix}files.tags = " . $this->getDb()->quote($tag)
0717                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("$tag,%")
0718                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag,%")
0719                         . " OR {$prefix}files.tags LIKE " . $this->getDb()->quote("%,$tag") . ')';
0720                 }
0721             }
0722         }
0723         if ($fileOcsCompatibility != 'all') {
0724             $fileOcsCompatible = null;
0725             if ($fileOcsCompatibility == 'compatible') {
0726                 $fileOcsCompatible = 1;
0727             }
0728             else if ($fileOcsCompatibility == 'incompatible') {
0729                 $fileOcsCompatible = 0;
0730             }
0731             if ($fileOcsCompatible !== null) {
0732                 $where[] = "{$prefix}files.ocs_compatible = :file_ocs_compatible";
0733                 $values[':file_ocs_compatible'] = $fileOcsCompatible;
0734             }
0735         }
0736         if ($fileContentId !== null && $fileContentId !== '') {
0737             $where[] = "{$prefix}files.content_id = :file_content_id";
0738             $values[':file_content_id'] = $fileContentId;
0739         }
0740         if ($artistId) {
0741             $where[] = "{$prefix}media.artist_id = :artist_id";
0742             $values[':artist_id'] = $artistId;
0743         }
0744         if ($albumId) {
0745             $where[] = "{$prefix}media.album_id = :album_id";
0746             $values[':album_id'] = $albumId;
0747         }
0748         if ($genre) {
0749             $where[] = "{$prefix}media.genre = :genre";
0750             $values[':genre'] = $genre;
0751         }
0752         if ($search) {
0753             $isSearchable = false;
0754             foreach (explode(' ', $search) as $keyword) {
0755                 if ($keyword && strlen($keyword) > 2) {
0756                     $keyword = $this->getDb()->quote("%$keyword%");
0757                     $where[] = "({$prefix}profiles.name LIKE $keyword"
0758                         . " OR {$prefix}collections.title LIKE $keyword"
0759                         . " OR {$prefix}files.name LIKE $keyword"
0760                         . " OR {$prefix}files.title LIKE $keyword"
0761                         . " OR {$prefix}media_artists.name LIKE $keyword"
0762                         . " OR {$prefix}media_albums.name LIKE $keyword"
0763                         . " OR {$prefix}media.title LIKE $keyword)";
0764                     $isSearchable = true;
0765                 }
0766             }
0767             if (!$isSearchable) {
0768                 return null;
0769             }
0770         }
0771         if ($ids) {
0772             $_ids = array();
0773             foreach (explode(',', $ids) as $id) {
0774                 $id = trim($id);
0775                 if ($id) {
0776                     $_ids[] = $this->getDb()->quote($id);
0777                 }
0778             }
0779             if ($_ids) {
0780                 $where[] = "{$prefix}media.id IN (" . implode(',', $_ids) . ')';
0781             }
0782         }
0783         if (!empty($favoriteIds['ownerIds'])
0784             || !empty($favoriteIds['collectionIds'])
0785             || !empty($favoriteIds['fileIds'])
0786         ) {
0787             $where[] = $this->_convertFavoriteIdsToStatement(
0788                 $favoriteIds,
0789                 array(
0790                     'ownerId' => "{$prefix}media.owner_id",
0791                     'collectionId' => "{$prefix}media.collection_id",
0792                     'fileId' => "{$prefix}media.file_id"
0793                 )
0794             );
0795         }
0796 
0797         if ($where) {
0798             $statementOption = 'WHERE ' . implode(' AND ', $where);
0799         }
0800 
0801         if ($sort == 'newest') {
0802             $order = "{$prefix}media.id DESC";
0803         }
0804         else if ($sort == 'track') {
0805             $order = "{$prefix}media.track ASC";
0806         }
0807         else if ($sort == 'recent') {
0808             $order = "{$prefix}media.played_timestamp DESC";
0809         }
0810         else if ($sort == 'frequent') {
0811             $order = "{$prefix}media.played_count DESC";
0812         }
0813 
0814         if ($page > 1) {
0815             $offset = ($page - 1) * $perpage;
0816         }
0817 
0818         $index = null;
0819         $pagination = null;
0820 
0821         if ($playedTimeperiodBegin || $playedTimeperiodEnd) {
0822             $_playedTimeperiodBegin = $this->_getTimestamp(0);
0823             if ($playedTimeperiodBegin) {
0824                 $_playedTimeperiodBegin = $playedTimeperiodBegin;
0825             }
0826             $_playedTimeperiodBegin = $this->getDb()->quote($_playedTimeperiodBegin);
0827 
0828             $_playedTimeperiodEnd = $this->_getTimestamp();
0829             if ($playedTimeperiodEnd) {
0830                 $_playedTimeperiodEnd = $playedTimeperiodEnd;
0831             }
0832             $_playedTimeperiodEnd = $this->getDb()->quote($_playedTimeperiodEnd);
0833 
0834             $_from = '('
0835                 . " SELECT {$prefix}media_played.media_id AS media_id,"
0836                 . " COUNT({$prefix}media_played.media_id) AS count"
0837                 . " FROM {$prefix}media_played"
0838                 . " WHERE {$prefix}media_played.played_timestamp"
0839                 . " BETWEEN {$_playedTimeperiodBegin} AND {$_playedTimeperiodEnd}"
0840                 . " GROUP BY {$prefix}media_played.media_id"
0841                 . ') AS played_timeperiod';
0842 
0843             $_join = "LEFT OUTER JOIN {$prefix}media"
0844                 . " ON {$prefix}media.id = played_timeperiod.media_id"
0845                 . ' ' . $this->_join;
0846 
0847             $_columns = str_replace(
0848                 "{$prefix}media.played_count AS played_timeperiod_count",
0849                 'played_timeperiod.count AS played_timeperiod_count',
0850                 $this->_columns
0851             );
0852 
0853             if ($sort == 'frequent') {
0854                 $order = 'played_timeperiod.count DESC';
0855             }
0856 
0857             $this->setPrefix('');
0858             $this->setName($_from);
0859             $this->setColumns($_columns);
0860 
0861             $index = $this->fetchRowset(
0862                 $_join . ' ' . $statementOption
0863                 . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0864                 $values
0865             );
0866 
0867             $this->setPrefix($prefix);
0868             $this->setName($name);
0869             $this->setColumns($columns);
0870 
0871             if (!$index) {
0872                 return null;
0873             }
0874 
0875             $this->setPrefix('');
0876             $this->setName($_from);
0877             $this->setColumns($_columns);
0878 
0879             $pagination = Flooer_Utility_Pagination::paginate(
0880                 $this->count($_join . ' ' . $statementOption, $values),
0881                 $perpage,
0882                 $page
0883             );
0884 
0885             $this->setPrefix($prefix);
0886             $this->setName($name);
0887             $this->setColumns($columns);
0888         }
0889         else {
0890             $this->setColumns($this->_columns);
0891             $index = $this->fetchRowset(
0892                 $this->_join . ' ' . $statementOption
0893                 . " ORDER BY $order LIMIT $perpage OFFSET $offset",
0894                 $values
0895             );
0896             $this->setColumns($columns);
0897 
0898             if (!$index) {
0899                 return null;
0900             }
0901 
0902             $this->setColumns($this->_columns);
0903             $pagination = Flooer_Utility_Pagination::paginate(
0904                 $this->count($this->_join . ' ' . $statementOption, $values),
0905                 $perpage,
0906                 $page
0907             );
0908             $this->setColumns($columns);
0909         }
0910 
0911         return array(
0912             'index' => $index,
0913             'pagination' => $pagination
0914         );
0915     }
0916 
0917     public function getMedia($id)
0918     {
0919         $prefix = $this->getPrefix();
0920         $columns = $this->getColumns();
0921 
0922         $this->setColumns($this->_columns);
0923         $media = $this->fetchRow(
0924             $this->_join
0925             . " WHERE {$prefix}media.id = :id"
0926             . ' LIMIT 1',
0927             array(':id' => $id)
0928         );
0929         $this->setColumns($columns);
0930 
0931         if ($media) {
0932             return $media;
0933         }
0934         return null;
0935     }
0936 
0937     public function getAlbumId($clientId, $artistName, $albumName)
0938     {
0939         $prefix = $this->getPrefix();
0940         $columns = $this->getColumns();
0941 
0942         $_columns = "{$prefix}media.album_id AS album_id";
0943 
0944         $_join = "LEFT OUTER JOIN {$prefix}media_artists"
0945             . " ON {$prefix}media_artists.id = {$prefix}media.artist_id"
0946             . " LEFT OUTER JOIN {$prefix}media_albums"
0947             . " ON {$prefix}media_albums.id = {$prefix}media.album_id";
0948 
0949         $this->setColumns($_columns);
0950         $result = $this->fetchRow(
0951             $_join
0952             . " WHERE {$prefix}media.client_id = :client_id"
0953             . " AND {$prefix}media_artists.name = :artist_name"
0954             . " AND {$prefix}media_albums.name = :album_name"
0955             . " LIMIT 1",
0956             array(
0957                 ':client_id' => $clientId,
0958                 ':artist_name' => $artistName,
0959                 ':album_name' => $albumName
0960             )
0961         );
0962         $this->setColumns($columns);
0963 
0964         if ($result) {
0965             return $result->album_id;
0966         }
0967         return null;
0968     }
0969 
0970     public function updatePlayedStatus($id)
0971     {
0972         if (isset($this->$id)) {
0973             parent::__set($id, array(
0974                 'played_timestamp' => $this->_getTimestamp(),
0975                 'played_ip' => $this->_getIp(),
0976                 'played_count' => $this->$id->played_count + 1
0977             ));
0978         }
0979     }
0980 
0981     public function deleteByCollectionId($collectionId)
0982     {
0983         $primary = $this->getPrimary();
0984         $this->setPrimary('collection_id');
0985         unset($this->$collectionId);
0986         $this->setPrimary($primary);
0987     }
0988 
0989     public function deleteByFileId($fileId)
0990     {
0991         $primary = $this->getPrimary();
0992         $this->setPrimary('file_id');
0993         unset($this->$fileId);
0994         $this->setPrimary($primary);
0995     }
0996 
0997 }