public function __invoke($entities)
 {
     if (!is_array($entities)) {
         throw new FormatterException('Collection formatter requries an array of entities');
     }
     $output = ['type' => 'FeatureCollection', 'features' => []];
     foreach ($entities as $entity) {
         $geometries = [];
         foreach ($entity->values as $attribute => $values) {
             foreach ($values as $value) {
                 if ($geometry = $this->valueToGeometry($value)) {
                     $geometries[] = $geometry;
                 }
             }
         }
         if (!empty($geometries)) {
             $output['features'][] = ['type' => 'Feature', 'geometry' => ['type' => 'GeometryCollection', 'geometries' => $geometries], 'properties' => ['title' => $entity->title, 'description' => $entity->content, 'id' => $entity->id, 'url' => URL::site(Ushahidi_Api::url($entity->getResource(), $entity->id), Request::current())]];
         }
     }
     if ($this->search->bbox) {
         if (is_array($this->search->bbox)) {
             $bbox = $this->search->bbox;
         } else {
             $bbox = explode(',', $this->search->bbox);
         }
         $output['bbox'] = $bbox;
     }
     return $output;
 }
Example #2
0
 /**
  * Prepare set data for API, along with all its 
  * groups and attributes
  * 
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => Ushahidi_Api::url('sets', $this->id), 'name' => $this->name, 'filter' => $this->filter, 'user' => empty($this->user_id) ? NULL : array('id' => $this->user_id, 'url' => Ushahidi_Api::url('users', $this->user_id)), 'created' => ($created = DateTime::createFromFormat('U', $this->created)) ? $created->format(DateTime::W3C) : $this->created, 'updated' => ($updated = DateTime::createFromFormat('U', $this->updated)) ? $updated->format(DateTime::W3C) : $this->updated);
     } else {
         $response = array('errors' => array('Set does not exist'));
     }
     return $response;
 }
Example #3
0
 /**
  * Prepare group data for API
  *
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => Ushahidi_Api::url('forms/' . $this->form_id . '/groups', $this->id), 'form' => empty($this->form_id) ? NULL : array('url' => Ushahidi_Api::url('forms', $this->form_id), 'id' => $this->form_id), 'label' => $this->label, 'priority' => $this->priority, 'icon' => $this->icon, 'attributes' => array());
         foreach ($this->form_attributes->find_all() as $attribute) {
             $response['attributes'][] = $attribute->for_api();
         }
     } else {
         $response = array('errors' => array('Group does not exist'));
     }
     return $response;
 }
Example #4
0
 /**
  * Prepare form data for API, along with all its
  * stages and attributes
  *
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => Ushahidi_Api::url('forms/', $this->id), 'name' => $this->name, 'description' => $this->description, 'type' => $this->type, 'disabled' => (bool) $this->disabled, 'created' => ($created = DateTime::createFromFormat('U', $this->created)) ? $created->format(DateTime::W3C) : $this->created, 'updated' => ($updated = DateTime::createFromFormat('U', $this->updated)) ? $updated->format(DateTime::W3C) : $this->updated, 'groups' => array());
         foreach ($this->form_stages->find_all() as $stages) {
             $response['stages'][] = $stage->for_api();
         }
     } else {
         $response = array('errors' => array('Form does not exist'));
     }
     return $response;
 }
Example #5
0
 /**
  * Prepare media data for API
  *
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         // Set image dimensions from the config file
         $medium_width = Kohana::$config->load('media.image_medium_width');
         $medium_height = Kohana::$config->load('media.image_medium_height');
         $thumbnail_width = Kohana::$config->load('media.image_thumbnail_width');
         $thumbnail_height = Kohana::$config->load('media.image_thumbnail_height');
         $upload_path = Kohana::$config->load('media.media_upload_dir');
         $relative_path = str_replace(Kohana::$config->load('imagefly.source_dir'), '', Kohana::$config->load('media.media_upload_dir'));
         $original_file = $upload_path . $this->o_filename;
         $response = array('id' => $this->id, 'user' => empty($this->user_id) ? NULL : array('id' => $this->user_id, 'url' => Ushahidi_Api::url('users', $this->user_id)), 'url' => Ushahidi_Api::url('media', $this->id), 'caption' => $this->caption, 'mime' => $this->mime, 'original_file_url' => URL::site(Media::uri($relative_path . $this->o_filename), Request::current()), 'original_file_size' => is_file($original_file) ? filesize($upload_path . $this->o_filename) : 0, 'original_width' => $this->o_width, 'original_height' => $this->o_height, 'medium_file_url' => $this->_resized_url($medium_width, $medium_height, $relative_path . $this->o_filename), 'medium_width' => $medium_width, 'medium_height' => $medium_height, 'thumbnail_file_url' => $this->_resized_url($thumbnail_width, $thumbnail_height, $relative_path . $this->o_filename), 'thumbnail_width' => $thumbnail_width, 'thumbnail_height' => $thumbnail_height, 'created' => ($created = DateTime::createFromFormat('U', $this->created)) ? $created->format(DateTime::W3C) : $this->created, 'updated' => ($updated = DateTime::createFromFormat('U', $this->updated)) ? $updated->format(DateTime::W3C) : $this->updated);
     } else {
         $response = array('errors' => array('Media does not exist'));
     }
     return $response;
 }
Example #6
0
 /**
  * Renders the view object to a string. Global and local data are merged
  * and extracted to create local variables within the view file.
  *
  *     $output = View::render();
  *
  * [!!] Global variables with the same key name as local variables will be
  * overwritten by the local variable.
  *
  * @param    string  view filename
  * @return   string
  * @throws   Kohana_View_Exception
  * @uses     View::capture
  */
 public function render($model = NULL)
 {
     if ($model == NULL) {
         $model = $this->model;
     }
     $response = array();
     if ($model->loaded()) {
         $response = array('id' => $model->id, 'url' => URL::site('api/v' . Ushahidi_Api::version() . '/users/' . $model->id, Request::current()), 'username' => $model->username, 'realname' => $model->realname, 'role' => $model->role, 'gravatar' => md5($model->email));
         // Return full user details if user has permission
         if ($this->_acl->is_allowed($this->_user, $model, 'get_full')) {
             $response['email'] = $model->email;
             $response['logins'] = $model->logins;
             $response['last_login'] = $model->last_login;
             $response['failed_attempts'] = $model->failed_attempts;
             $response['last_attempt'] = $model->last_attempt;
             $response['created'] = ($created = DateTime::createFromFormat('U', $model->created)) ? $created->format(DateTime::W3C) : $model->created;
             $response['updated'] = ($updated = DateTime::createFromFormat('U', $model->updated)) ? $updated->format(DateTime::W3C) : $model->updated;
         }
     }
     return $response;
 }
Example #7
0
 /**
  * Load resource object
  *
  * @return  void
  */
 protected function _resource()
 {
     parent::_resource();
     $this->_resource = 'export';
 }
Example #8
0
 /**
  * Format relations into url/id arrays
  * @param  string $resource resource name as used in urls
  * @param  int    $id       resource id
  * @return array
  */
 protected function get_relation($resource, $id)
 {
     return !$id ? NULL : ['id' => $id, 'url' => URL::site(Ushahidi_Api::url($resource, $id), Request::current())];
 }
Example #9
0
 /**
  * Load resource object
  *
  * @return void
  */
 protected function _resource()
 {
     parent::_resource();
     $this->_resource = 'stats';
 }
Example #10
0
 /**
  * Prepare data for API
  *
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => Ushahidi_Api::url('contacts', $this->id), 'user' => empty($this->user_id) ? NULL : array('id' => $this->user_id, 'url' => Ushahidi_Api::url('users', $this->user_id)), 'contact' => $this->contact, 'type' => $this->type, 'data_provider' => $this->data_provider, 'created' => ($created = DateTime::createFromFormat('U', $this->created)) ? $created->format(DateTime::W3C) : $this->created);
     } else {
         $response = array('errors' => array('Message does not exist'));
     }
     return $response;
 }
Example #11
0
 /**
  * Prepare form data for API, along with all its
  * groups and attributes
  *
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => Ushahidi_Api::url('tags', $this->id), 'parent' => empty($this->parent_id) ? NULL : array('id' => $this->parent_id, 'url' => Ushahidi_Api::url('tags', $this->parent_id)), 'tag' => $this->tag, 'slug' => $this->slug, 'type' => $this->type, 'color' => $this->color ? '#' . $this->color : null, 'icon' => $this->icon, 'description' => $this->description, 'priority' => $this->priority, 'created' => ($created = DateTime::createFromFormat('U', $this->created)) ? $created->format(DateTime::W3C) : $this->created);
     } else {
         $response = array('errors' => array('Tag does not exist'));
     }
     return $response;
 }
Example #12
0
 /**
  * Prepare model data for API
  *
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => Ushahidi_Api::url('messages', $this->id), 'parent' => empty($this->parent_id) ? NULL : array('id' => $this->parent_id, 'url' => Ushahidi_Api::url('messages', $this->parent_id)), 'contact' => empty($this->contact_id) ? NULL : $this->contact->for_api(), 'post' => empty($this->post_id) ? NULL : array('id' => $this->post_id, 'url' => Ushahidi_Api::url('posts', $this->post_id)), 'data_provider' => $this->data_provider, 'data_provider_message_id' => $this->data_provider_message_id, 'title' => $this->title, 'message' => $this->message, 'datetime' => ($date = DateTime::createFromFormat('Y-m-d H:i:s', $this->datetime)) ? $date->format(DateTime::W3C) : $this->datetime, 'type' => $this->type, 'status' => $this->status, 'direction' => $this->direction, 'created' => ($created = DateTime::createFromFormat('U', $this->created)) ? $created->format(DateTime::W3C) : $this->created);
     } else {
         $response = array('errors' => array('Message does not exist'));
     }
     return $response;
 }
Example #13
0
 public function url()
 {
     switch ($this->type) {
         case 'revision':
             return Ushahidi_Api::url('posts/' . $this->parent_id . '/revisions', $this->id);
             break;
         case 'translation':
             return Ushahidi_Api::url('posts/' . $this->parent_id . '/translations', $this->id);
             break;
         case 'report':
         default:
             // @todo maybe put 'updates' url as /post/:parent_id/updates/:id
             return Ushahidi_Api::url('posts', $this->id);
             break;
     }
 }
Example #14
0
 /**
  * Prepare attribute data for API
  *
  * @return array $response - array to be returned by API (as json)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => Ushahidi_Api::url('attributes', $this->id), 'key' => $this->key, 'label' => $this->label, 'input' => $this->input, 'type' => $this->type, 'required' => $this->required ? TRUE : FALSE, 'default' => $this->default, 'priority' => $this->priority, 'options' => $this->options, 'cardinality' => $this->cardinality);
     } else {
         $response = array('errors' => array('Attribute does not exist'));
     }
     return $response;
 }