Created
May 26, 2014 06:24
-
-
Save kamaulynder/89c572cdb1a1fa3e7f4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') OR die('No direct script access.'); | |
/** | |
* Ushahidi API Media Controller | |
* | |
* @author Ushahidi Team <[email protected]> | |
* @package Ushahidi\Application\Controllers | |
* @copyright 2013 Ushahidi | |
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | |
*/ | |
use Ushahidi\Entity\Media; | |
class Controller_Api_Media extends Ushahidi_Api | |
{ | |
/** | |
* @var array List of HTTP methods which support body content | |
*/ | |
protected $_methods_with_body_content = array | |
( | |
Http_Request::PUT, | |
); | |
/** | |
* @var string oauth2 scope required for access | |
*/ | |
protected $_scope_required = 'media'; | |
/** | |
* Load resource object | |
* | |
* @return void | |
*/ | |
protected function _resource() | |
{ | |
parent::_resource(); | |
$this->_resource = 'media'; | |
$this->_resource = ORM::factory('Media'); | |
// Get post | |
if ($media_id = $this->request->param('id', 0)) | |
{ | |
// Respond with set | |
$media = ORM::factory('Media', $media_id); | |
if (! $media->loaded()) | |
{ | |
throw new HTTP_Exception_404('Media does not exist. ID: \':id\'', array( | |
':id' => $this->request->param('id', 0), | |
)); | |
} | |
$this->_resource = $media; | |
} | |
} | |
/** | |
* Retrieve all media for a User | |
* | |
* GET /api/media | |
* | |
* @return void | |
*/ | |
public function action_get_index_collection() | |
{ | |
$results = array(); | |
$this->_prepare_order_limit_params(); | |
$user = $this->request->query('user'); | |
if(! empty($user)) | |
{ | |
$repo = service('repository.media'); | |
try | |
{ | |
$results = $repo->getAllForUser($user); | |
} | |
catch (InvalidArgumentException $e) | |
{ | |
throw HTTP_Exception::factory(400, $e->getMessage()); | |
} | |
} | |
$results = array_map(array($this, '_for_api'), $results); | |
$count = count($results); | |
// Current/Next/Prev urls | |
$params = array( | |
'limit' => $this->_record_limit, | |
'offset' => $this->_record_offset, | |
); | |
// Only add order/orderby if they're already set | |
if ($this->request->query('orderby') OR $this->request->query('order')) | |
{ | |
$params['orderby'] = $this->_record_orderby; | |
$params['order'] = $this->_record_order; | |
} | |
$prev_params = $next_params = $params; | |
$next_params['offset'] = $params['offset'] + $params['limit']; | |
$prev_params['offset'] = $params['offset'] - $params['limit']; | |
$prev_params['offset'] = ($prev_params['offset'] > 0) ? $prev_params['offset'] : 0; | |
$curr = URL::site($this->request->uri().URL::query($params), $this->request); | |
$next = URL::site($this->request->uri().URL::query($next_params), $this->request); | |
$prev = URL::site($this->request->uri().URL::query($prev_params), $this->request); | |
// Respond with media details | |
$this->_response_payload = array( | |
'count' => $count, | |
'results' => $results, | |
'limit' => $this->_record_limit, | |
'offset' => $this->_record_offset, | |
'order' => $this->_record_order, | |
'orderby' => $this->_record_orderby, | |
'curr' => $curr, | |
'next' => $next, | |
'prev' => $prev, | |
); | |
} | |
/** | |
* Retrieve a Media | |
* | |
* GET /api/media/:id | |
* | |
* @return void | |
*/ | |
public function action_get_index() | |
{ | |
$id = $this->request->param('id'); | |
$repo = service('repository.media'); | |
try | |
{ | |
$media = $repo->get($id); | |
} | |
catch (InvalidArgumentException $e) | |
{ | |
throw HTTP_Exception::factory(400, $e->getMessage()); | |
} | |
$this->_response_payload = $this->_for_api($media); | |
} | |
/** | |
* Create a media | |
* | |
* POST /api/media | |
* | |
* @return void | |
*/ | |
public function action_post_index_collection() | |
{ | |
// Validation object for additional validation (not in model) | |
$max_file_size = Kohana::$config->load('media.max_file_upload_size'); | |
$media_data = Validation::factory(array_merge($_FILES,$this->request->post())) | |
->rule('file', 'not_empty') | |
->rule('file','Upload::valid') | |
->rule('file','Upload::type', array(':value', array('gif','jpg','jpeg','png'))) | |
->rule('file','Upload::size', array(':value', $max_file_size)); | |
// Save details to the database | |
$media = $this->resource(); | |
$repo = service('repository.media'); | |
try | |
{ | |
// Validate base post data | |
if ($media_data->check() === FALSE) | |
{ | |
throw new Validation_Exception($media_data, 'Failed to validate media'); | |
} | |
$media->file_url = $media_data['file']; | |
// Set caption if it is set | |
if (isset($media_data['caption'])) | |
{ | |
$media->caption = $media_data['caption']; | |
$repo->add($media); | |
} | |
} | |
catch (ORM_Validation_Exception $e) | |
{ | |
throw new HTTP_Exception_400('Validation Error: \':errors\'', array( | |
':errors' => implode(', ', Arr::flatten($e->errors('models'))) | |
)); | |
} | |
catch (Validation_Exception $e) | |
{ | |
throw new HTTP_Exception_400('Validation Error: \':errors\'', array( | |
':errors' => implode(', ', Arr::flatten($e->array->errors('api/posts'))) | |
)); | |
} | |
catch (InvalidArgumentException $e) | |
{ | |
throw HTTP_Exception::factory(400, $e->getMessage()); | |
} | |
// Return the newly created media | |
$this->_response_payload = $this->_for_api($media); | |
$this->_response_payload['allowed_methods'] = $this->_allowed_methods($media); | |
} | |
/** | |
* Delete a media | |
* | |
* DELETE /api/media/:id | |
* | |
* @return void | |
*/ | |
public function action_delete_index() | |
{ | |
$id = $this->request->param('id'); | |
$repo = service('repository.media'); | |
try | |
{ | |
$media = $repo->remove($id); | |
} | |
catch (InvalidArgumentException $e) | |
{ | |
throw HTTP_Exception::factory(400, $e->getMessage()); | |
} | |
// Respond with posts | |
$this->_response_payload = array('count' => $media); | |
$this->_response_payload['allowed_methods'] = $this->_allowed_methods(); | |
} | |
protected function _for_api(\Ushahidi\Entity\Media $media) | |
{ | |
return (array) $media + array( | |
'allowed_methods' => | |
$this->_allowed_methods() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment