Leads_List.php
<?php
require APPPATH."libraries/koolreport/autoload.php";
if (!function_exists('getCurrentDate')){
require APPPATH."libraries/koolreport/functions/getCurrentDate.php";
}
if (!function_exists('Leads_createGroupBy')){
require APPPATH."libraries/koolreport/functions/Leads_createGroupBy.php";
}
use \koolreport\processes\Sort;
use \koolreport\processes\Group;
use \koolreport\processes\Limit;
use \koolreport\processes\ColumnMeta;
use \koolreport\processes\RemoveColumn;
use \koolreport\processes\ColumnRename;
use \koolreport\pivot\processes\Pivot;
class Leads_List extends \koolreport\KoolReport
{
use \koolreport\codeigniter\Friendship;
use \koolreport\inputs\Bindable;
use \koolreport\export\Exportable;
function settings()
{
return array(
"dataSources"=>array(
"groupBySelect"=>array(
"class"=>'\koolreport\datasources\ArrayDataSource',
"dataFormat"=>"associate",
"groupBySelect"=>array(
array("Status","statusC"),
array("Source","sourceC"),
array("Company","companyC"),
array("City","cityC"),
array("Country","countryC"),
array("Language","languageC"),
array("State","stateC"),
array("Staff","staffC"),
array("Zip","zipC"),
)
),
),
);
}
function setup()
{
//setup Query to create the table based on the fields selected
if (isset($this->params["Leads_MultiSelectFields"])){
$fielsSelected = Leads_setQueryTableFields($this->params["Leads_MultiSelectFields"]);
$MultiSelectFields = $this->params["Leads_MultiSelectFields"];
}else{
$fielsSelected = "
, tblleads.phonenumber as phone
, datediff(current_date(), tblleads.dateadded) as age
, datediff(current_date(), tblleads.last_status_change) as statusage
";
$MultiSelectFields = "";
}
if(isset($this->params["Leads_SelectCustomField"]) and !empty($this->params["Leads_SelectCustomField"])){
$fielsSelected .= ", tblcustomfieldsvalues.value as ".verifyCustomFieldName($this->params["Leads_SelectCustomField"]);
$Leads_SelectCustomField = $this->params["Leads_SelectCustomField"];
}else{
$Leads_SelectCustomField = "";
}
if(isset($this->params["Leads_SelectCustomFields"]) and !empty($this->params["Leads_SelectCustomFields"])){
$fielsSelected .= ", tblcustomfieldsvalues.value as ".verifyCustomFieldName($this->params["Leads_SelectCustomFields"]);
}
$today = getCurrentDate();
$currentMonth = substr($today, 0, 8);
if(isset($this->params["Leads_dateRange_start"])){
$start = substr($this->params["Leads_dateRange_start"], 0, 10);
}else{
$start = $currentMonth."01";
$end = $today;
}
if(isset($this->params["Leads_dateRange_end"])){
$end = substr($this->params["Leads_dateRange_end"], 0, 10);
}else{
$start = $currentMonth."01";
$end = $today;
}
if(isset($this->params["Leads_SelectCity"])){
$SelectCity = $this->params["Leads_SelectCity"];
}else{
$SelectCity = "";
}
if(isset($this->params["Leads_SelectLanguage"])){
$SelectLanguage = $this->params["Leads_SelectLanguage"];
}else{
$SelectLanguage = "";
}
if(isset($this->params["Leads_SelectMaxAge"])){
$SelectMaxAge = $this->params["Leads_SelectMaxAge"];
}else{
$SelectMaxAge = "";
}
if(isset($this->params["Leads_SelectMaxStatusAge"])){
$SelectMaxStatusAge = $this->params["Leads_SelectMaxStatusAge"];
}else{
$SelectMaxStatusAge = "";
}
if(isset($this->params["Leads_SelectZip"])){
$SelectZip = $this->params["Leads_SelectZip"];
}else{
$SelectZip = "";
}
if(isset($this->params["Leads_SelectState"])){
$SelectState = $this->params["Leads_SelectState"];
}else{
$SelectState = "";
}
if(isset($this->params["Leads_SelectState"])){
$SelectState = $this->params["Leads_SelectState"];
}else{
$SelectState = "";
}
if(isset($this->params["Leads_SelectCountry"])){
$SelectCountry = $this->params["Leads_SelectCountry"];
}else{
$SelectCountry = "";
}
if(isset($this->params["Leads_SelectCustomFieldValue"])){
$Leads_SelectCustomFieldValue = $this->params["Leads_SelectCustomFieldValue"];
}else{
$Leads_SelectCustomFieldValue = "";
}
if(isset($this->params["Leads_MultiSelectStaff"])){
$MultiSelectStaff = $this->params["Leads_MultiSelectStaff"];
}else{
$MultiSelectStaff = "";
}
if(isset($this->params["Leads_MultiSelectStatus"])){
$MultiSelectStatus = $this->params["Leads_MultiSelectStatus"];
}else{
$MultiSelectStatus = "";
}
if(isset($this->params["Leads_MultiSelectSource"])){
$SelectSource = $this->params["Leads_MultiSelectSource"];
}else{
$SelectSource = "";
}
// $start = '2015-01-01';
// $end = '2019-01-01';
//Table content
$this->src('default')
->query("SELECT tblleads.name as leadsC
, tblleads.email as emailC
, tblleads.phonenumber as phoneC
, tblleadsstatus.name as statusC
, tblleadsstatus.id as statusIDC
, tblleads.lastcontact as lastcontactC
, tblleads.is_public as publicC
, tblstaff.firstname as staffC
, datediff(current_date(), tblleads.dateadded) as ageC
, datediff(current_date(), tblleads.last_status_change) as statusageC
, CAST(tblcustomfieldsvalues.value as SIGNED) as customfieldvalueC
, tblcustomfieldsvalues.fieldid as customfieldidC
, tblleadssources.name as sourceC
, tblleads.company as companyC
, tblleads.zip as zipC
, tblleads.state as stateC
, tblleads.city as cityC
, tblcountries.short_name as countryC
, tblleads.default_language as languageC
".$fielsSelected."
FROM tblleads
LEFT JOIN tblstaff
ON tblstaff.staffid = tblleads.assigned
LEFT JOIN tblcountries
ON tblcountries.country_id = tblleads.country
LEFT JOIN tblleadsstatus
ON tblleadsstatus.id = tblleads.status
LEFT JOIN tblleadssources
ON tblleadssources.id = tblleads.source
LEFT JOIN tblcustomfieldsvalues on tblcustomfieldsvalues.relid = tblleads.id and fieldto = 'leads'
WHERE tblleads.dateadded >= :start
AND tblleads.dateadded <= :end
".
(($MultiSelectStatus != "")?"AND tblleadsstatus.id IN :MultiSelectStatus":"")
."
".
(($MultiSelectStaff != "")?"AND tblleads.assigned IN :MultiSelectStaff":"")
."
".
(($SelectCity !="")?"AND tblleads.city IN :SelectCity":"")
."
".
(($SelectZip !="")?"AND tblleads.zip IN :SelectZip":"")
."
".
(($SelectState != "")?"AND tblleads.state IN :SelectState":"")
."
".
(($SelectCountry !="")?"AND tblcountries.short_name IN :SelectCountry":"")
."
".
(($SelectSource !="")?"AND tblleadssources.id IN :SelectSource":"")
."
".
(($SelectLanguage !="")?"AND tblleads.default_language IN :SelectLanguage":"")
."
".
(($SelectMaxAge !="")?"AND datediff(current_date(), tblleads.dateadded) >= :SelectMaxAge":"")
."
".
(($SelectMaxStatusAge !="")?"AND datediff(current_date(), tblleads.last_status_change) >= :SelectMaxStatusAge":"")
."
".
((isset($this->params["Leads_SelectCustomField"]) and $this->params["Leads_SelectCustomField"]!="")?"AND tblcustomfieldsvalues.fieldid = :Leads_SelectCustomField":"")
."
".
((isset($this->params["Leads_SelectCustomFieldValue"]) and $this->params["Leads_SelectCustomFieldValue"]!="")?"AND tblcustomfieldsvalues.value = :Leads_SelectCustomFieldValue":"")
."
group by tblleads.id
")
->params(array(
":start"=>$start,
":end"=>$end,
":MultiSelectStatus"=>$MultiSelectStatus,
":MultiSelectStaff"=>$MultiSelectStaff,
":SelectCity"=>$SelectCity,
":SelectZip"=>$SelectZip,
":SelectState"=>$SelectState,
":SelectCountry"=>$SelectCountry,
":Leads_SelectCustomFieldValue"=>$Leads_SelectCustomFieldValue,
":Leads_SelectCustomField"=>$Leads_SelectCustomField,
":SelectSource"=>$SelectSource,
":SelectLanguage"=>$SelectLanguage,
":SelectMaxAge"=>(int)$SelectMaxAge,
":SelectMaxStatusAge"=>(int)$SelectMaxStatusAge,
))
->saveTo($source);
$source->pipe($this->dataStore("result1"));
//verify selected fields of Group By
if(isset($this->params["Leads_SelectFieldsGroupBy"]) and !empty($this->params["Leads_SelectFieldsGroupBy"])){
$groupByTotal = $this->params["Leads_SelectFieldsGroupBy"];
$groupByTotalName = ucwords(str_replace("C", "", str_replace('_', " ", $this->params["Leads_SelectFieldsGroupBy"])));
}else{
$groupByTotal = "statusC";
$groupByTotalName = "Status";
}
//verify GroupBySelection
$createPivotDimensions = Leads_createGroupBy($groupByTotal);
$pivotTotal = "customfieldvalueC";
if(isset($this->params["Leads_SelectCustomFields"]) and !empty($this->params["Leads_SelectCustomFields"] !="")){
$pivotTotalName = verifyCustomFieldName($this->params["Leads_SelectCustomFields"]);
}else{
//take the last custom field created
$pivotTotalName = "customfieldvalueAll";
}
$source->pipe(new ColumnRename(array(
$pivotTotal=>$pivotTotalName,
"leadsC"=>"Leads",
)))
->pipe(new ColumnMeta(array(
'Leads'=>array(
'type' => 'number',
'decimals'=>2,
),
)))
->pipe(new Pivot(array(
'dimensions'=>array(
'row'=>$createPivotDimensions
// 'row'=>"statusC, staffC, Leads"
),
'aggregates'=>array(
'count'=>'Leads',
'sum'=>$pivotTotalName,
'min'=>$pivotTotalName,
'max'=>$pivotTotalName,
'avg'=>$pivotTotalName,
)
)))
->pipe($this->dataStore('result2'));
//charts Result
$source
->pipe(new ColumnRename(array(
"leadsC"=>"Leads",
)))
->pipe(new Group(array(
"by"=>$groupByTotal,
"sum"=>$pivotTotal,
"count"=>"Leads",
)))
->pipe($this->dataStore('groupedLeadsStatus'));
//Table result
$source
//Remove Columns
->pipe(new RemoveColumn(array("phoneC","statusIDC","publicC","ageC","zipC","statusageC","customfieldvalueC","customfieldidC","sourceC","companyC","stateC","cityC","countryC","languageC")))
//Rename Columns
// echo ' <i class="fa fa-exclamation-circle" aria-hidden="true" data-toggle="tooltip" data-title="Leads Name Exclamation" data-original-title="" title=""></i>';
->pipe(new ColumnRename(array(
"leadsC"=>_l('lead_add_edit_name'),
"emailC"=>"Email",
"phone"=>_l('lead_add_edit_phonenumber'),
"statusC"=>"Status",
// "age"=>'<i class="fa fa-exclamation-circle" aria-hidden="true" data-toggle="tooltip" data-title="Leads Age in days Exclamation" data-original-title="" title=""></i>Age (days)',
"age"=>_l('kr_leads_age'),
"statusage"=>_l('kr_leads_status_age'),
"lastcontactC"=>_l('leads_dt_last_contact'),
"staffC"=>'Staff',
"staff"=>_l('staff_members'),
"datecreated"=>"Date Created",
"state"=>_l('state'),
"zip"=>_l('zip'),
"country"=>_l('country'),
"city"=>_l('city'),
"email"=>_l('email'),
)))
->pipe($this->dataStore("result3"));
}
}
?>