/** * Channels restful api */ public function action_manage() { $this->template = ""; $this->auto_render = FALSE; switch ($this->request->method()) { case "POST": $channel_array = json_decode($this->request->body(), TRUE); $channel_config = Swiftriver_Plugins::get_channel_config($channel_array['channel']); if (!$channel_config) { throw new HTTP_Exception_400(); } $channel_orm = $this->river->get_channel($channel_array['channel']); // Make sure the channel is enabled for the case where a disabled // channel is being re-added. if (!(bool) $channel_orm->filter_enabled) { $channel_orm->filter_enabled = TRUE; $channel_orm->save(); } echo json_encode(array('id' => $channel_orm->id, 'channel' => $channel_orm->channel, 'name' => $channel_config['name'], 'enabled' => (bool) $channel_orm->filter_enabled, 'options' => $this->river->get_channel_options($channel_orm))); break; case "PUT": $channel_array = json_decode($this->request->body(), TRUE); $channel_orm = $this->river->get_channel($channel_array['channel']); $channel_orm->filter_enabled = $channel_array['enabled']; $channel_orm->save(); break; case "DELETE": $channel_id = intval($this->request->param('id', 0)); $channel_orm = $this->river->get_channel_by_id($channel_id); if ($channel_orm) { $channel_orm->delete(); } break; } }
/** * Channel options restful api */ public function action_options() { $this->template = ""; $this->auto_render = FALSE; sleep(5); switch ($this->request->method()) { case "DELETE": $channel_id = intval($this->request->param('id', 0)); $this->river_service->delete_channel($this->river['id'], $channel_id); break; case "POST": $channel_array = json_decode($this->request->body(), TRUE); $channel_config = Swiftriver_Plugins::get_channel_config($channel_array['channel']); if (!$channel_config) { throw new HTTP_Exception_400(); } try { $channel_array = $this->river_service->create_channel_from_array($this->river['id'], $channel_array); echo json_encode($channel_array); } catch (Swiftriver_Exception_Channel_Option $e) { $this->response->status(400); $this->response->headers('Content-Type', 'application/json'); echo json_encode(array('error' => $e->getMessage())); } break; case "PUT": $channel_id = intval($this->request->param('id', 0)); $channel_array = json_decode($this->request->body(), TRUE); $channel_config = Swiftriver_Plugins::get_channel_config($channel_array['channel']); if (!$channel_config) { throw new HTTP_Exception_400(); } try { $channel_array = $this->river_service->update_channel_from_array($this->river['id'], $channel_id, $channel_array); echo json_encode($channel_array); } catch (Swiftriver_Exception_Channel_Option $e) { $this->response->status(400); $this->response->headers('Content-Type', 'application/json'); echo json_encode(array('error' => $e->getMessage())); } break; default: throw new HTTP_Exception_405(); } }
/** * Return a river array with subscription and collaboration * status populated for $querying_account * * @param Model_User $user * @param Model_User $querying_account * @return array * */ public static function get_array($river, $querying_account) { $river['url'] = self::get_base_url($river); $river['expired'] = FALSE; $river['is_owner'] = $river['account']['id'] == $querying_account['id']; // Calculate % of the river that is occupied $drop_count = $river['drop_count']; $drop_quota = $river['drop_quota']; $percent_full = ($drop_count > 0 and $drop_quota > 0) ? round($drop_count / $drop_quota * 100, 2) : 0; $river['percent_full'] = $percent_full; // Is the querying account collaborating on the river? $river['is_collaborator'] = FALSE; foreach ($querying_account['collaborating_rivers'] as $r) { if ($river['id'] == $r['id']) { // $river['is_owner'] = TRUE; $river['is_collaborator'] = TRUE; } } // Is the querying account following the river? $river['following'] = FALSE; foreach ($querying_account['following_rivers'] as $r) { if ($river['id'] == $r['id']) { $river['following'] = TRUE; } } // Get display name from channel plugins and disabled channels if (isset($river['channels'])) { $channels = array(); foreach ($river['channels'] as $channel) { if (!Swiftriver_Plugins::get_channel_config($channel['channel'])) { continue; } $channel['display_name'] = ''; $channel['parameters'] = json_decode($channel['parameters'], TRUE); Swiftriver_Event::run('swiftriver.channel.format', $channel); $channels[] = $channel; } $river['channels'] = $channels; } return $river; }
/** * Get a river's channel options with configuration added * * @param Model_Channel_Filter $channel_orm * @param int $id Id of channel filter to be returned * @return array */ public function get_channel_options($channel_orm, $id = NULL) { $options = array(); $channel_config = Swiftriver_Plugins::get_channel_config($channel_orm->channel); $query = $channel_orm->channel_filter_options; if ($id) { $query->where('id', '=', $id); } foreach ($query->find_all() as $channel_option) { $option = json_decode($channel_option->value); $option->id = $channel_option->id; $option->key = $channel_option->key; if (!isset($channel_config['options'][$channel_option->key])) { continue; } $options[] = $option; } return $options; }