For the sake of quick sorting in the browser, I needed a Javascript array of events with thumbnails. I’m not going to go through all of my logic here (if anyone wants to question it, I’ll answer) but I settled on JSON. The need for thumbnails meant uploading a separate image, resizing manually, or utilizing existing code.
CodeIgniter does have an internal library to resize images, and I’ve used it in the past, but this time I decided to use Imgsizer. This is the resulting code:
<?php
# Load the list of our upload directories:
$file_dirs = $this->EE->functions->fetch_file_paths();
# Ensure that the Imgsizer plugin exists and is loaded.
# NOTE: PHP *must* be run on Output for this to work.
if (isset($this->EE->addons->_map['plugins']['imgsizer'])) {
require_once($this->EE->addons->_map['plugins']['imgsizer']['path'].$this->EE->addons->_map['plugins']['imgsizer']['file']);
$imgsizer = new Imgsizer();
$events = array();
# SQL query to pull events. cd.field_id_3 and cd.field_id_5 are custom fields that I am referencing directly.
$res = $this->EE->db->query('SELECT ct.url_title, DATE_FORMAT(FROM_UNIXTIME(cd.field_id_3), "%b %e, %Y") AS `date`, ct.title, MONTH(FROM_UNIXTIME(cd.field_id_3)) AS `month`, YEAR(FROM_UNIXTIME(cd.field_id_3)) AS `year`, IF (cd.field_id_5 = "", "/images/default.jpg", cd.field_id_5) AS event_image FROM `exp_channel_titles` ct LEFT JOIN `exp_channel_data` cd ON (cd.entry_id = ct.entry_id) LEFT JOIN `exp_channels` c ON (ct.channel_id = c.channel_id) WHERE c.channel_name = "events" AND (ct.expiration_date = 0 OR ct.expiration_date > NOW()) ORDER BY cd.field_id_3 ASC');
if ($res->num_rows() > 0) {
foreach ($res->result_array() as $row) {
# ->tagparams is an array that contains the parameters we typically pass to the tag. Let's clear it.
$this->EE->TMPL->tagparams = array();
# Now fill the array with the parameters we want to send exp:imgsizer:size. We need to
# replace the {filedir_##} tags ourself.
$this->EE->TMPL->tagparams['src'] = preg_replace('/^{filedir_(\d+)}/e', '$file_dirs[\1]', $row['event_image']);
$this->EE->TMPL->tagparams['width'] = 320;
$this->EE->TMPL->tagparams['height'] = 108;
# If you want to pretend there is a closing tag, you could provide the contained data here. Otherwise, since
# this PHP is being run on Output, we need to make sure ExpressionEngine has not left anything in here.
$this->EE->TMPL->tagdata = '';
# And... call the exp:imgsizer:size function...
$row['event_image'] = $imgsizer->size();
# Store the rest of our SQL data in an array by year and month...
$events[$row['year']][$row['month']][] = $row;
}
}
# Now we can JSON-encode the output array.
$json = json_encode($events);
}
?>It can surely be adapted for other plugins as the need arises.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.