Skip to content

Instantly share code, notes, and snippets.

@westonwatson
Created February 14, 2012 21:52
Show Gist options
  • Save westonwatson/1830818 to your computer and use it in GitHub Desktop.
Save westonwatson/1830818 to your computer and use it in GitHub Desktop.
grab_leads method, grabs lead counts of all account under a reseller organized by service
//searchin' for what?
$id = $_REQUEST['id'];
//no layout, via AJAX
$this->render_layout=false;
//No id to request
if (!$id) exit;
//leads by service
$leads_by_service = array();
//find reseller by id
$reseller = new Reseller;
$reseller = $reseller->find($id);
//if reseller found
if ($reseller->id){
//if leads
if (is_array($reseller->accounts)){
foreach($reseller->accounts as $account){
//loop accounts
if ($account->leads){
//loop leads
foreach($account->leads as $lead){
//increment array value with service_id as index
if ($lead->inquiries){
foreach($lead->inquiries as $inquiry){
//use inquiry->service_id as index
$leads_by_service[$inquiry->service_id ? $inquiry->service_id : 0]++;
}
}else{
//there's a lead with no inquiry, should I count this?
$leads_by_service[0]++;
}
}
}
}
}else{
//no leads, return null
die (json_encode(null));
}
//serve lead counts by service in json
die ( json_encode($leads_by_service) );
}else{
//no reseller, return null
die (json_encode(null));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment