示例#1
0
 function to_array($options = array())
 {
     $options = array_merge(array('auth' => false, 'render' => true, 'expand' => false), $options);
     $koken_url_info = $this->config->item('koken_url_info');
     $exclude = array('deleted', 'total_count', 'video_count', 'audio_count', 'featured_image_id', 'custom_featured_image', 'tags_old', 'old_slug');
     $dates = array('created_on', 'modified_on', 'published_on', 'featured_on');
     $strings = array('title', 'content', 'excerpt');
     $bools = array('published', 'featured');
     if (!$this->published) {
         $this->published_on = time();
     }
     list($data, $public_fields) = $this->prepare_for_output($options, $exclude, $bools, $dates, $strings);
     if (strlen(trim($data['draft'])) === 0) {
         $data['draft'] = $data['content'];
     }
     if (strlen(trim($data['draft_title'])) === 0) {
         $data['draft_title'] = $data['title'];
     }
     if (!$data['featured']) {
         unset($data['featured_on']);
     }
     if ($data['page_type'] != 0) {
         unset($data['featured']);
         unset($data['featured_on']);
     }
     if (!$options['auth']) {
         unset($data['internal_id']);
         unset($data['draft']);
         unset($data['draft_title']);
     }
     if (array_key_exists('page_type', $data)) {
         switch ($data['page_type']) {
             case 1:
                 $data['page_type'] = 'page';
                 break;
             default:
                 $data['page_type'] = 'essay';
         }
     }
     $data['__koken__'] = $data['page_type'];
     $data['tags'] = $this->_get_tags_for_output($options);
     $data['categories'] = array('count' => is_null($this->category_count) ? $this->categories->count() : (int) $this->category_count, 'url' => $koken_url_info->base . 'api.php?/text/' . $data['id'] . '/categories');
     $data['topics'] = array('count' => is_null($this->album_count) ? $this->albums->count() : (int) $this->album_count, 'url' => $koken_url_info->base . 'api.php?/text/' . $data['id'] . '/topics');
     if (is_numeric($this->featured_image_id) && !$this->featured_image->id) {
         $this->featured_image->get();
     }
     if ($this->featured_image->id && $this->featured_image->deleted == 0) {
         $data['featured_image'] = $this->featured_image->to_array();
     } else {
         if (!empty($this->custom_featured_image)) {
             $c = new Content();
             $data['featured_image'] = $c->to_array_custom($this->custom_featured_image);
         } else {
             $data['featured_image'] = false;
         }
     }
     $rendered = Shutter::shortcodes($data['content'], array($this, $options));
     if ($options['render']) {
         if ($options['expand']) {
             $rendered = preg_replace('/\\[read_more([^\\]]+)?\\]/', '<a id="more"></a>', $rendered);
         } else {
             $more = strpos($rendered, '[read_more');
             if ($more !== false) {
                 preg_match('/\\[read_more(?:\\s*label="(.*)")?\\]/', $rendered, $matches);
                 $rendered = substr($rendered, 0, $more);
                 $data['read_more'] = true;
                 $data['read_more_label'] = count($matches) > 1 ? $matches[1] : 'Read more';
             }
         }
     }
     if (!isset($data['read_more'])) {
         $data['read_more'] = false;
     }
     preg_match_all('/<koken:load source="content" filter:id="(\\d+)">/', $rendered, $loads);
     if (count($loads[0]) > 1) {
         $this->ids_for_array_index = array_unique($loads[1]);
         $rendered = '<koken:load source="contents" filter:id="' . join(',', $this->ids_for_array_index) . '">' . $rendered . '</koken:load>';
         $rendered = preg_replace_callback('/<koken:load source="content" filter:id="(\\d+)">(.*)<\\/koken:load>/msU', array($this, '_array_index_callback'), $rendered);
     }
     if (empty($options) || isset($options['render']) && $options['render']) {
         $data['content'] = $rendered;
         if (isset($data['draft'])) {
             $data['draft'] = Shutter::shortcodes($data['draft'], array($this, $options));
         }
     }
     if (empty($data['excerpt'])) {
         $rendered = preg_replace('/<script.*>.*?<\\/script>/msU', '', $rendered);
         $rendered = preg_replace('/<figure class="k-content-embed">.*?<\\/figure>/msU', '', $rendered);
         $clean_parts = explode(' ', preg_replace('/([\\.\\?\\!]+)([^\\s]\\s*[a-z][a-z\\s]*)/', '$1 $2', trim(strip_tags(preg_replace('/\\{\\{.*\\}\\}/', '', html_entity_decode($rendered))))));
         $excerpt = '';
         while (count($clean_parts) && ($next = array_shift($clean_parts)) && strlen(trim($excerpt) . ' ' . trim($next)) <= 254) {
             $excerpt .= ' ' . trim($next);
         }
         $data['excerpt'] = trim($excerpt);
         if (count($clean_parts)) {
             $data['excerpt'] = preg_replace('/[^\\w]$/u', '', $data['excerpt']) . '…';
         }
         $more = strpos($data['excerpt'], '[read_more');
         if ($more !== false) {
             $data['excerpt'] = trim(substr($data['excerpt'], 0, $more));
         }
     }
     if (isset($options['order_by']) && in_array($options['order_by'], array('created_on', 'modified_on', 'published_on'))) {
         $data['date'] =& $data[$options['order_by']];
     } else {
         if ($data['page_type'] === 'essay') {
             $data['date'] =& $data['published_on'];
         }
     }
     $cat = isset($options['category']) ? $options['category'] : (isset($options['context']) && strpos($options['context'], 'category-') === 0 ? str_replace('category-', '', $options['context']) : false);
     if ($cat) {
         if (is_numeric($cat)) {
             foreach ($this->categories->get_iterated() as $c) {
                 if ($c->id == $cat) {
                     $cat = $c->slug;
                     break;
                 }
             }
         }
     }
     $data['url'] = $this->url(array('date' => $data['published_on'], 'tag' => isset($options['tags']) ? $options['tags'] : (isset($options['context']) && strpos($options['context'], 'tag-') === 0 ? str_replace('tag-', '', $options['context']) : false), 'category' => $cat));
     if ($data['url']) {
         list($data['__koken_url'], $data['url']) = $data['url'];
         $data['canonical_url'] = $data['url'];
     }
     return Shutter::filter('api.text', array($data, $this, $options));
 }