/** * Download a file * * @return void */ public function downloadTask() { $file = Request::getVar('file', ''); $item = Request::getInt('post', 0); $size = Request::getWord('size', ''); $post = Post::getInstance($item); // Instantiate an attachment object $asset = Asset::getInstance($file, $post->get('item_id')); // Ensure record exist if (!$asset->get('id') || $post->item()->get('state') == 2) { throw new Exception(Lang::txt('COM_COLLECTIONS_FILE_NOT_FOUND'), 404); } // Check authorization if ($post->collection()->get('access') == 4 && User::isGuest()) { throw new Exception(Lang::txt('COM_COLLECTIONS_ERROR_ACCESS_DENIED_TO_FILE'), 403); } // Check authorization if (!$post->collection()->canAccess(User::get('id'))) { throw new Exception(Lang::txt('COM_COLLECTIONS_ERROR_ACCESS_DENIED_TO_FILE'), 403); } // Ensure we have a path if (!$asset->get('filename')) { throw new Exception(Lang::txt('COM_COLLECTIONS_FILE_NOT_FOUND'), 404); } $file = $asset->file($size); // Get the configured upload path $filename = $asset->filespace() . DS . $asset->get('item_id') . DS . ltrim($file, DS); // Ensure the file exist if (!file_exists($filename)) { throw new Exception(Lang::txt('COM_COLLECTIONS_FILE_NOT_FOUND') . ' ' . $filename, 404); } $ext = strtolower(Filesystem::extension($filename)); // Initiate a new content server and serve up the file $server = new Server(); $server->filename($filename); $server->disposition('attachment'); if (in_array($ext, array('jpg', 'jpeg', 'jpe', 'png', 'gif'))) { $server->disposition('inline'); } $server->acceptranges(false); // @TODO fix byte range support if (!$server->serve()) { // Should only get here on error throw new Exception(Lang::txt('COM_COLLECTIONS_SERVER_ERROR'), 500); } else { exit; } }
/** * Vote for an item * * @return void */ private function _vote() { // Incoming $id = Request::getInt('post', 0); // Get the post model $post = \Components\Collections\Models\Post::getInstance($id); // Record the vote if (!$post->item()->vote()) { $this->setError($post->item()->getError()); } // Display updated item stats if called via AJAX $no_html = Request::getInt('no_html', 0); if ($no_html) { echo Lang::txt('PLG_GROUPS_COLLECTIONS_POST_LIKES', $post->item()->get('positive')); exit; } // Get the collection model $collection = $this->model->collection($post->get('collection_id')); // Display the main listing App::redirect(Route::url('index.php?option=' . $this->option . '&cn=' . $this->group->get('cn') . '&active=' . $this->_name . '&scope=' . $collection->get('alias'))); }
/** * Delete a post * * @apiMethod DELETE * @apiUri /collections/{id}/posts/{id} * @apiParameter { * "name": "id", * "description": "Entry identifier", * "type": "integer", * "required": true, * "default": null * } * @return void */ public function deleteTask() { $this->requiresAuthentication(); $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; if (count($ids) <= 0) { throw new Exception(Lang::txt('COM_COLLECTIONS_ERROR_MISSING_ID'), 500); } foreach ($ids as $id) { $row = new Post(intval($id)); if (!$row->exists()) { throw new Exception(Lang::txt('COM_COLLECTIONS_ERROR_MISSING_RECORD'), 404); } if (!$row->delete()) { throw new Exception($row->getError(), 500); } } $this->send(null, 204); }
/** * Vote for an item * * @return void */ private function _vote() { // Incoming $id = Request::getInt('post', 0); // Get the post model $post = \Components\Collections\Models\Post::getInstance($id); // Record the vote if (!$post->item()->vote()) { $this->setError($post->item()->getError()); } // Display updated item stats if called via AJAX $no_html = Request::getInt('no_html', 0); if ($no_html) { echo Lang::txt('PLG_GROUPS_COLLECTIONS_POST_LIKES', $post->item()->get('positive')); exit; } // Get the collection model $collection = $this->model->collection($post->get('collection_id')); $url = Route::url('index.php?option=' . $this->option . '&cn=' . $this->group->get('cn') . '&active=' . $this->_name . '&scope=' . $collection->get('alias')); // Record the activity $recipients = array(['group', $this->group->get('gidNumber')], ['collection', $collection->get('id')], ['user', $post->item()->get('created_by')], ['user', User::get('id')]); Event::trigger('system.logActivity', ['activity' => ['action' => 'voted', 'scope' => 'collections.item', 'scope_id' => $post->item()->get('id'), 'description' => Lang::txt('PLG_GROUPS_COLLECTIONS_ACTIVITY_ITEM_VOTED', '<a href="' . $url . '">' . $collection->get('title') . '</a>'), 'details' => array('collection_id' => $collection->get('id'), 'post_id' => $post->get('id'), 'item_id' => $post->item()->get('id'))], 'recipients' => $recipients]); // Display the main listing App::redirect($url); }
/** * Get basic metadata for a post * * @return void */ public function metadataTask() { $id = Request::getInt('post', 0); $post = new Post($id); if (!Request::getInt('no_html', 0)) { // Output messsage and redirect App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller)); return; } $response = new \stdClass(); $response->id = $id; $response->reposts = Lang::txt('COM_COLLECTIONS_NUM_REPOSTS', $post->item()->get('reposts', 0)); $response->comments = Lang::txt('COM_COLLECTIONS_NUM_COMMENTS', $post->item()->get('comments', 0)); $response->likes = Lang::txt('COM_COLLECTIONS_NUM_LIKES', $post->item()->get('positive', 0)); echo json_encode($response); }
/** * Delete one or more entries * * @return void */ public function removeTask() { // Check for request forgeries Request::checkToken(); // Incoming $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; if (count($ids) > 0) { // Loop through all the IDs foreach ($ids as $id) { $entry = new Post(intval($id)); // Delete the entry if (!$entry->delete()) { \Notify::error($entry->getError()); } } } // Set the redirect App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_COLLECTIONS_ITEMS_DELETED')); }
/** * Vote for an item * * @return void */ private function _vote() { // Incoming $id = Request::getInt('post', 0); // Get the post model $post = \Components\Collections\Models\Post::getInstance($id); // Record the vote if (!$post->item()->vote()) { $this->setError($post->item()->getError()); } // Display updated item stats if called via AJAX $no_html = Request::getInt('no_html', 0); if ($no_html) { echo Lang::txt('%s likes', $post->item()->get('positive')); exit; } // Get the collection model $collection = $this->model->collection($post->get('collection_id')); // Display the main listing App::redirect(Route::url($this->member->getLink() . '&active=' . $this->_name . '&task=' . $collection->get('alias'))); }
/** * Set and get a specific post * * @param integer $id Post ID * @return object CollectionsModelPost */ public function post($id = null) { // If the current post isn't set // OR the ID passed doesn't equal the current post's ID if (!isset($this->_post) || $id !== null && (int) $this->_post->get('id') != $id) { // Reset current offering $this->_post = null; // If the list of all posts is available ... if (isset($this->_posts) && $this->_posts instanceof ItemList) { // Find a post in the list that matches the ID passed foreach ($this->posts() as $key => $post) { if ((int) $post->get('id') == $id) { // Set current offering $this->_post = $post; break; } } } if (!$this->_post) { $this->_post = Post::getInstance($id); } } // Return current post return $this->_post; }