Пример #1
0
 /**
  * Generate tab separated values(TSV). Yes not CSV. CSV doesn't seem to
  * work well with MS Excel when encoded as UTF8.
  *
  * @param  array $header    The TSV header
  * @param  array $posts     The post details for the TSV rows
  * @return string           The generated TSV string
  */
 private function _generate_tsv($header, $posts)
 {
     $sep = "\t";
     // use tabs as separators. So MS Excel on Windows and Macs can handle UTF data
     $eol = "\r\n";
     // line breaks
     $custom_form_values = array();
     $custom_form_unique_headers = array();
     foreach ($posts as $post) {
         foreach (Model_Form_Attribute::attribute_types() as $type) {
             $results = ORM::factory('Post_' . ucfirst($type))->where('post_id', '=', $post->id)->with('form_attribute')->find_all();
             foreach ($results as $result) {
                 if (is_array($result->value)) {
                     // Multi level array.
                     // Join parent form attribute key with sub array's form attribute key for the tsv header
                     foreach ($result->value as $key => $value) {
                         $header_value = $result->form_attribute->key . "." . $key;
                         $custom_form_values[$post->id][$header_value] = $value;
                     }
                 } else {
                     $custom_form_values[$post->id][$result->form_attribute->key] = $result->value;
                 }
             }
         }
     }
     $custom_form_headers = array();
     foreach ($custom_form_values as $value) {
         foreach ($value as $key => $val) {
             $custom_form_headers[] = $key;
         }
     }
     // Remove duplicate custom forms headers
     $custom_form_unique_headers = array_unique($custom_form_headers);
     // Merge custom form headers and the previously defined headers above
     $header = array_merge($header, $custom_form_unique_headers);
     // Make sure all headers are in upper case
     $header = array_map('strtoupper', $header);
     $tsv = count($header) ? '"' . implode('"' . $sep . '"', $header) . '"' . $eol : '';
     // Build csv rows
     foreach ($posts as $post) {
         $tsv_rows = array();
         $tsv_rows[] = $post->id;
         $tsv_rows[] = $post->parent_id;
         $tsv_rows[] = $post->user_id;
         $tsv_rows[] = $post->form_id;
         $tsv_rows[] = $post->title;
         $tsv_rows[] = $post->content;
         $tsv_rows[] = $post->type;
         $tsv_rows[] = $post->status;
         $tsv_rows[] = $post->slug;
         $tsv_rows[] = $post->locale;
         $tsv_rows[] = $post->created;
         $tsv_rows[] = $post->updated;
         // Make tag values CSV
         $tag_titles = array();
         foreach ($post->tags->find_all() as $tag) {
             if ($tag->tag) {
                 $tag_titles[] = $tag->tag;
             }
         }
         $tsv_rows[] = implode(',', $tag_titles);
         // Make set values CSV
         $set_names = array();
         foreach ($post->sets->find_all() as $set) {
             if ($set->name) {
                 $set_names[] = $set->name;
             }
         }
         $tsv_rows[] = implode(',', $set_names);
         // Custom forms
         foreach ($custom_form_values as $key => $values) {
             // Make sure the post has custom forms
             if ($key == $post->id) {
                 foreach ($custom_form_unique_headers as $value) {
                     if (isset($values[$value])) {
                         $tsv_rows[] = $values[$value];
                     } else {
                         $tsv_rows[] = '';
                     }
                 }
             }
         }
         $format = [$this, '_normalize_empty_value'];
         $tsv_rows = array_map($format, $tsv_rows);
         // Implode as tab separated values
         $tsv .= '"' . implode('"' . $sep . '"', $tsv_rows) . '"' . $eol;
     }
     return chr(255) . chr(254) . mb_convert_encoding($tsv, 'UTF-16LE', 'UTF-8');
 }
Пример #2
0
 /**
  * Prepare single post for api ( ++ Hairy :) )
  * along with values from attached tables
  *
  * @return array $response
  * @todo the queries need some optimizing (EAV Fun)
  */
 public function for_api()
 {
     $response = array();
     if ($this->loaded()) {
         $response = array('id' => $this->id, 'url' => $this->url(), 'parent' => empty($this->parent_id) ? NULL : array('id' => $this->parent_id, 'url' => Ushahidi_Api::url('posts', $this->parent_id)), 'user' => empty($this->user_id) ? NULL : array('id' => $this->user_id, 'url' => Ushahidi_Api::url('users', $this->user_id)), 'form' => empty($this->form_id) ? NULL : array('id' => $this->form_id, 'url' => Ushahidi_Api::url('forms', $this->form_id)), 'title' => $this->title, 'content' => $this->content, 'status' => $this->status, 'type' => $this->type, 'slug' => $this->slug, 'locale' => $this->locale, '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, 'values' => array(), 'tags' => array());
         // Create the Super Union
         // @todo generalize this - how do plugins add other attribute types?
         $response['values'] = array();
         foreach (Model_Form_Attribute::attribute_types() as $type) {
             ORM::factory('Post_' . ucfirst($type))->load_values_for_post($this->id, $response['values']);
         }
         // Get tags
         foreach ($this->tags->find_all() as $tag) {
             // @todo use $tag->for_api() once thats built
             $response['tags'][] = array('id' => $tag->id, 'url' => Ushahidi_Api::url('tags', $tag->id));
         }
     } else {
         $response = array('errors' => array('Post does not exist'));
     }
     return $response;
 }