Json::entries PHP Method

entries() public method

public entries ( $entry_ids = null )
  public function entries($entry_ids = null)
  {
    $this->initialize('entries');

    //exit if ajax request is required and not found
    if ($this->check_xhr_required())
    {
      return '';
    }

    //instantiate channel module object
    if (empty($this->channel))
    {
      require_once PATH_MOD.'channel/mod.channel'.EXT;

      $this->channel = new Channel;
    }

    $this->channel->initialize();

    $order_by_string = '';

    if (is_array($entry_ids))
    {
      $this->entries_entry_ids = $entry_ids;
      $order_by_string = 'FIELD(t.entry_id,'.implode(',', $entry_ids).')';
    }
    else
    {
      //run through the channel module process to grab the entries
      $this->channel->uri = ($this->channel->query_string != '') ? $this->channel->query_string : 'index.php';

      if ($this->channel->enable['custom_fields'] === TRUE)
      {
        $this->channel->fetch_custom_channel_fields();
      }

      $save_cache = FALSE;

      if (ee()->config->item('enable_sql_caching') == 'y')
      {
        if (FALSE == ($this->channel->sql = $this->channel->fetch_cache()))
        {
          $save_cache = TRUE;
        }
        else
        {
          if (ee()->TMPL->fetch_param('dynamic') != 'no')
          {
            if (preg_match("#(^|\/)C(\d+)#", $this->channel->query_string, $match) OR in_array($this->channel->reserved_cat_segment, explode("/", $this->channel->query_string)))
            {
              $this->channel->cat_request = TRUE;
            }
          }
        }
      }

      if ( ! $this->channel->sql)
      {
        $this->channel->build_sql_query();
      }

      if (preg_match('/t\.entry_id IN \(([\d,]+)\)/', $this->channel->sql, $match))
      {
        $this->entries_entry_ids = explode(',', $match[1]);
      }

      if (preg_match('/ORDER BY (.*)?/', $this->channel->sql, $match))
      {
        $order_by_string = $match[1];
      }
    }

    if ($this->entries_entry_ids)
    {
      $this->entries_custom_fields = ee()->db->select('channel_fields.*, channels.channel_id')
                                             ->from('channel_fields')
                                             ->join('channels', 'channel_fields.group_id = channels.field_group')
                                             ->where('channels.site_id', ee()->config->item('site_id'))
                                             ->where_in('channels.channel_name', explode('|', ee()->TMPL->fetch_param('channel')))
                                             ->get()
                                             ->result_array();

      $default_fields = array(
        't.title',
        't.url_title',
        't.entry_id',
        't.channel_id',
        't.author_id',
        't.status',
        't.entry_date',
        't.edit_date',
        't.expiration_date',
      );

      $select = array();

      if ( ! empty($this->fields))
      {
        foreach ($default_fields as $field)
        {
          $key = substr($field, 2);

          if (in_array($key, $this->fields))
          {
            $select[] = $field;
          }
        }
      }
      else
      {
        $select = $default_fields;
      }

      foreach ($this->entries_custom_fields as &$field)
      {
        if (empty($this->fields) || in_array($field['field_name'], $this->fields))
        {
          $select[] = 'wd.'.ee()->db->protect_identifiers('field_id_'.$field['field_id']).' AS '.ee()->db->protect_identifiers($field['field_name']);
        }
      }

      //we need entry_id, always grab it
      if ( ! in_array('t.entry_id', $select))
      {
        $select[] = 't.entry_id';
      }

      ee()->db->select(implode(', ', $select), FALSE)
              ->from('channel_titles t')
              ->join('channel_data wd', 't.entry_id = wd.entry_id')
              ->where_in('t.entry_id', $this->entries_entry_ids);

      if ($order_by_string)
      {
        if (strpos($order_by_string, 'w.') !== FALSE)
        {
          ee()->db->join('channels w', 't.channel_id = w.channel_id');
        }

        if (strpos($order_by_string, 'm.') !== FALSE)
        {
          ee()->db->join('members m', 'm.member_id = t.author_id');
        }

        if (strpos($order_by_string, 'md.') !== FALSE)
        {
          ee()->db->join('member_data md', 'm.member_id = md.member_id');
        }

        if (ee()->TMPL->fetch_param('display_by') === 'week' && strpos($order_by_string, 'yearweek') !== FALSE)
        {
          $yearweek = TRUE;

          $offset = ee()->localize->zones[ee()->config->item('server_timezone')] * 3600;

          $format = (ee()->TMPL->fetch_param('start_day') === 'Monday') ? '%x%v' : '%X%V';

          ee()->db->select("DATE_FORMAT(FROM_UNIXTIME(entry_date + $offset), '$format') AS yearweek", FALSE);
        }

        ee()->db->order_by($order_by_string, '', FALSE);
      }

      $query = $this->channel->query = ee()->db->get();

      $show_categories = ee()->TMPL->fetch_param('show_categories') === 'yes';

      if ($show_categories)
      {
        $this->channel->fetch_categories();

        if (ee()->TMPL->fetch_param('show_category_group'))
        {
          $show_category_group = explode('|', ee()->TMPL->fetch_param('show_category_group'));
        }
      }

      $this->entries = $query->result_array();

      $query->free_result();

      foreach ($this->entries as &$entry)
      {
        if (isset($yearweek))
        {
          unset($entry['yearweek']);
        }

        //format dates as javascript unix time (in microseconds!)
        if (isset($entry['entry_date']))
        {
          $entry['entry_date'] = $this->date_format($entry['entry_date']);
        }

        if (isset($entry['edit_date']))
        {
          $entry['edit_date'] = $this->date_format(strtotime($entry['edit_date']));
        }

        if (isset($entry['expiration_date']))
        {
          $entry['expiration_date'] = $this->date_format($entry['expiration_date']);
        }

        foreach ($this->entries_custom_fields as &$field)
        {
          //call our custom callback for this fieldtype if it exists
          if (isset($entry[$field['field_name']]) && is_callable(array($this, 'entries_'.$field['field_type'])))
          {
            $entry[$field['field_name']] = call_user_func(array($this, 'entries_'.$field['field_type']), $entry['entry_id'], $field, $entry[$field['field_name']], $entry);
          }
        }

        if ($show_categories)
        {
          $entry['categories'] = array();

          if (isset($this->channel->categories[$entry['entry_id']]))
          {
            foreach ($this->channel->categories[$entry['entry_id']] as $raw_category)
            {
              if ( ! empty($show_category_group) && ! in_array($raw_category[5], $show_category_group))
              {
                continue;
              }

              $category = array(
                'category_id' => (int) $raw_category[0],
                'parent_id' => (int) $raw_category[1],
                'category_name' => $raw_category[2],
                'category_image' => $raw_category[3],
                'category_description' => $raw_category[4],
                'category_group' => $raw_category[5],
                'category_url_title' => $raw_category[6],
              );

              foreach ($this->channel->catfields as $cat_field)
              {
                $category[$cat_field['field_name']] = (isset($raw_category['field_id_'.$cat_field['field_id']])) ? $raw_category['field_id_'.$cat_field['field_id']] : '';
              }

              $entry['categories'][] = $category;
            }
          }
        }

        $entry['entry_id'] = (int) $entry['entry_id'];

        if (isset($entry['channel_id']))
        {
          $entry['channel_id'] = (int) $entry['channel_id'];
        }

        if (isset($entry['author_id']))
        {
          $entry['author_id'] = (int) $entry['author_id'];
        }
      }
    }

    ee()->load->library('javascript');

    ee()->load->library('typography');

    return $this->respond($this->entries, array(ee()->typography, 'parse_file_paths'));
  }