Created
August 20, 2011 02:31
-
-
Save leevigraham/1158529 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Load member data into cache for performance | |
* | |
* @access public | |
* @param $Channel The current Channel object including all data relating to categories and custom fields | |
* @param $query_result array Entries for the current tag | |
* @return array The modified query result | |
* @see http://expressionengine.com/public_beta/docs/development/extension_hooks/module/channel/index.html | |
*/ | |
public function channel_entries_query_result($Channel, $query_result){ | |
$EE =& get_instance(); | |
if($EE->extensions->last_call != FALSE) { | |
$query_result = $EE->extensions->last_call; | |
} | |
// If the pfields are not yet ready return the result to prevent errors | |
if(!isset($Channel->pfields[1])) { | |
return $query_result; | |
} | |
// Find all the member_select fields for this channel | |
$fields = array(); | |
foreach($Channel->pfields[1] as $field_id => $field_type) { | |
if($field_type == $this->addon_id) { | |
$fields[] = 'field_id_' . $field_id; | |
} | |
} | |
// If no matching fields are found return the result to prevent errors | |
if(!count($fields)) { | |
return $query_result; | |
} | |
// Build an array of member ids we need to fetch | |
$required_member_ids = ''; | |
foreach ($query_result as $entry) { | |
foreach ($fields as $field) { | |
if($entry[$field] > 0) { | |
$required_member_ids .= "|" . $entry[$field]; | |
} | |
} | |
} | |
if($required_member_ids == ""){ | |
return $query_result; | |
} | |
unset($i); | |
// explode the string into a unqiue array | |
$required_member_ids_init = array_unique(explode("|", $required_member_ids)); | |
$required_member_ids = array(); | |
// Loop over the required members and test if they have already been loaded | |
if(isset($EE->session->cache[$this->addon_id]["members"])){ | |
foreach($required_member_ids_init as $k => $v) { | |
if(! array_key_exists($v, $EE->session->cache[$this->addon_id]["members"])) { | |
$required_member_ids[] = $v; | |
} | |
} | |
} | |
// Are there required members? | |
if(! empty($required_member_ids)) { | |
// Get their details | |
$member_query = $EE->db | |
->select("*, | |
LCASE(`exp_member_groups`.`group_title`) AS `group_url_title`, | |
exp_member_groups.group_title AS `group_title`, | |
exp_member_groups.group_description AS `group_description` | |
") | |
->from("exp_members") | |
->join("exp_member_data", 'exp_members.member_id = exp_member_data.member_id') | |
->join("exp_member_groups", 'exp_members.group_id = exp_member_groups.group_id') | |
->where_in('exp_members.member_id', $required_member_ids) | |
->get(); | |
// Loop over the member and add them to the cache | |
foreach ($member_query->result_array() as $member) { | |
$altered_member = $member; | |
$altered_member['group_url_title'] = preg_replace('/[^a-z0-9]/', '-', $altered_member['group_url_title']); | |
$EE->session->cache[$this->addon_id]["members"][$member["member_id"]] = $member; | |
} | |
} | |
// Return the entry query unaffected | |
// the tag parsing will take care of the rest | |
return $query_result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment