public function get(RESTApiRequest $request)
 {
     //throw new RESTNotAllowedMethod("Please use /tv-channel/[ch_id]/epg instead");
     if (empty($this->nested_params['ch_id'])) {
         throw new RESTBadRequest("ch_id required");
     }
     $ch_ids = explode(',', $this->nested_params['ch_id']);
     $epg_data = array();
     foreach ($ch_ids as $ch_id) {
         $channel = \Itv::getChannelById((int) $ch_id);
         if (empty($channel)) {
             throw new RESTNotFound("Channel " . intval($ch_id) . " not found");
         }
         $from = (int) $request->getParam('from');
         $to = (int) $request->getParam('to');
         $next = (int) $request->getParam('next');
         if (!empty($next)) {
             $epg_data[(int) $ch_id] = $this->filter($this->manager->getCurProgramAndFewNext($channel['id'], $next));
         } else {
             $from = empty($from) ? "" : date("Y-m-d H:i:s", $from);
             $to = empty($to) ? "" : date("Y-m-d H:i:s", $to);
             $epg = $this->manager->getEpgForChannelsOnPeriod(array($channel['id']), $from, $to);
             $epg_data[(int) $ch_id] = $this->filter($epg[$channel['id']]);
         }
     }
     if (count($epg_data) == 1) {
         $keys = array_keys($epg_data);
         return $epg_data[$keys[0]];
     } else {
         return $epg_data;
     }
 }
 private function prepareQuery(RESTApiRequest $request)
 {
     $raw_videos = $this->manager->getRawAll();
     $search = $request->getSearch();
     if ($search !== null) {
         $raw_videos->like(array('video.name' => '%' . $search . '%', 'o_name' => '%' . $search . '%', 'actors' => '%' . $search . '%', 'director' => '%' . $search . '%', 'year' => '%' . $search . '%'), 'OR');
     }
     if ($request->getParam('mark') == 'favorite') {
         $raw_videos->in('id', $this->favorites);
     } elseif ($request->getParam('mark') == 'not_ended') {
         $raw_videos->in('id', array_keys($this->not_ended));
     }
     if (!empty($this->video_id)) {
         $raw_videos->where(array('id' => $this->video_id));
     }
     if (!empty($this->genres_ids)) {
         $raw_videos->group_in(array('cat_genre_id_1' => $this->genres_ids, 'cat_genre_id_2' => $this->genres_ids, 'cat_genre_id_3' => $this->genres_ids, 'cat_genre_id_4' => $this->genres_ids), 'OR');
     }
     return $raw_videos;
 }
 public function get(RESTApiRequest $request)
 {
     if ($request->getParam('mark') == 'favorite') {
         $this->favorite_filter_enabled = true;
     }
     $channels = $this->manager->getRawAllUserChannels($this->user_id);
     if ($this->favorite_filter_enabled) {
         $channels->in('id', $this->fav_channels);
     }
     if ($request->getLimit() !== null) {
         $channels->limit($request->getLimit(), $request->getOffset());
     }
     if ($this->genre_id) {
         $channels->where(array('tv_genre_id' => $this->genre_id));
     }
     if (!empty($this->channel_id)) {
         $channels->where(array('id' => $this->channel_id));
     }
     return $this->filter($channels->get()->all());
 }
 public function get(RESTApiRequest $request, $parent_id)
 {
     if (empty($this->params['users.id'])) {
         throw new RESTBadRequest("User required");
     }
     $user_id = $this->params['users.id'];
     $user = \Stb::getById($user_id);
     if (empty($user)) {
         throw new RESTNotFound("User not found");
     }
     $itv = \Itv::getInstance();
     $this->user_channels = $itv->getAllUserChannelsIdsByUid($user['id']);
     if (!in_array($parent_id, $this->user_channels)) {
         throw new RESTForbidden("User don't have access to this channel");
     }
     $channel = \Itv::getById($parent_id);
     if (empty($channel)) {
         throw new RESTNotFound("Channel not found");
     }
     $start = $request->getParam('start');
     if ($start) {
         // todo: time shift!
         throw new RESTNotFound("Time shift in progress...");
     }
     $urls = \Itv::getUrlsForChannel($channel['id']);
     if (!empty($urls)) {
         $link = $urls[0]['id'];
     } else {
         $link = null;
     }
     try {
         $url = $itv->getUrlByChannelId($parent_id, $link);
     } catch (\ItvChannelTemporaryUnavailable $e) {
         throw new RESTNotFound($e->getMessage());
     }
     if (preg_match("/(\\S+:\\/\\/\\S+)/", $url, $match)) {
         $url = $match[1];
     }
     return $url;
 }