This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/campaign/campaign.inc.php

635 lines
16 KiB
PHP
Raw Normal View History

<?php
/**
* AgileBill - Open Billing Software
*
* This body of work is free software; you can redistribute it and/or
* modify it under the terms of the Open AgileBill License
* License as published at http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
*
* For questions, help, comments, discussion, etc., please join the
2009-08-03 04:10:16 +00:00
* Agileco community forums at http://forum.agileco.com/
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
2009-08-03 04:10:16 +00:00
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @version 1.4.93
*/
2009-08-03 04:10:16 +00:00
class campaign extends OSB_module {
##############################
## CLICK ##
##############################
function click($VAR)
2009-08-03 04:10:16 +00:00
{
### Set the forward URL
if(!empty($VAR['caid']))
{
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'campaign WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr(@$VAR['caid']);
2009-08-03 04:10:16 +00:00
$result = $db->Execute($sql);
if (strlen($result->fields['url']) >= 1) $url = $result->fields['url'];
2009-08-03 04:10:16 +00:00
}
if(empty($url)) $url = URL;
echo '<script language="JavaScript"> <!-- START
document.location="'.$url.'";
// END -->
</SCRIPT>';
### LOG the click
if(!isset($VAR['_log']) && !empty($VAR['caid']))
{
if(isset($VAR['file']))
$file_no = $VAR['file'];
else
$file_no = '1';
$count_field= 'clicked'.$file_no;
$count = $result->fields[$count_field] + 1;
$sql = 'UPDATE ' . AGILE_DB_PREFIX . 'campaign SET
'.$count_field.' = ' . $db->qstr($count) . ' WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr(@$VAR['caid']);
$result = $db->Execute($sql);
}
}
##############################
## DISPLAY ##
##############################
function display($VAR)
{
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'campaign WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr(@$VAR['id']);
$result = $db->Execute($sql);
if(isset($VAR['file']))
$file_no = $VAR['file'];
else
$file_no = '1';
$file = PATH_FILES . 'campaign_' . $VAR['id'] . '_' . $file_no .'.dat';
$type = 'type'.$file_no;
$name = 'file'.$file_no;
$count_field= 'served'.$file_no;
$count = $result->fields[$count_field] + 1;
if($result->RecordCount() > 0)
{
### Open the file
if (@$file=fopen($file, 'r'))
{
### Display the correct headers:
header ("Content-type: " . $result->fields[$type]);
header ("Content-Disposition: inline; filename=" . $result->fields[$name]);
header ("Content-Description: PHP/INTERBASE Generated Data" );
fpassthru($file);
### Increment the file
if(!isset($VAR['_log']))
{
$sql = 'UPDATE ' . AGILE_DB_PREFIX . 'campaign SET
'.$count_field.' = ' . $db->qstr($count) . ' WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr(@$VAR['id']);
$result = $db->Execute($sql);
}
exit;
}
}
echo 'Sorry, the campaign or required file does not exist!';
}
##############################
## ADD ##
##############################
2009-08-03 04:10:16 +00:00
function xadd($VAR)
{
global $_FILES;
# Validate the files
for($i=1; $i<=12; $i++) {
if(isset($_FILES['upload_file'.$i]) && $_FILES['upload_file'.$i]['size'] > 0) {
$VAR['campaign_type'.$i] = $_FILES['upload_file'.$i]['type'];
2009-08-03 04:10:16 +00:00
$VAR['campaign_file'.$i] = $_FILES['upload_file'.$i]['name'];
}
}
2009-08-03 04:10:16 +00:00
## Attempt to add the record:
$type = "add";
2009-08-03 04:10:16 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$campaign_id = $db->add($VAR, $this, $type);
### Copy the files & delete temp files
if($campaign_id > 0) {
for($i=1; $i<=12; $i++) {
if(isset($_FILES['upload_file'.$i]) && $_FILES['upload_file'.$i]['size'] > 0)
{
if(!copy($_FILES['upload_file'.$i]['tmp_name'], PATH_FILES . 'campaign_'.$campaign_id.'_'.$i.'.dat'))
{
### ERROR:
2009-08-03 04:10:16 +00:00
}
#unlink any temp files
unlink($_FILES['upload_file'.$i]['tmp_name']);
}
}
2009-08-03 04:10:16 +00:00
}
}
2009-08-03 04:10:16 +00:00
/**
* View a record
*/
public function view($VAR) {
$smart = parent::view($VAR);
2009-08-03 04:10:16 +00:00
# Get the stats for each advertisement:
$budget = $smart['budget'];
2009-08-03 04:10:16 +00:00
# Total Impressions, Total Clicks, & Accounts referred
$smart['impressions_total'] = 0;
$smart['clicks_total'] = 0;
$smart['accounts_referred'] = $this->referred_accounts($smart['id']);
2009-08-03 04:10:16 +00:00
# Invoices referred
$inv = $this->referred_invoices($smart['id']);
$smart['invoices_referred'] = $inv[0];
$smart['invoices_revenue'] = round($inv[1],2);
2009-08-03 04:10:16 +00:00
# Total impressions & clicks:
$impressions_total = 0;
$clicks_total = 0;
2009-08-03 04:10:16 +00:00
for ($ii=1; $ii<=12; $ii++) {
$smart['results'][$ii]['served'] = $smart['served'.$ii];
$smart['results'][$ii]['clicked'] = $smart['clicked'.$ii];
$smart['results'][$ii]['file'] = $smart['file'.$ii];
2009-08-03 04:10:16 +00:00
$impressions_total += $smart['results'][$ii]['served'];
$clicks_total += $smart['results'][$ii]['clicked'];
2009-08-03 04:10:16 +00:00
if (empty($smart['results'][$ii]['served']))
$smart['results'][$ii]['show'] = false;
else
$smart['results'][$ii]['show'] = true;
}
2009-08-03 04:10:16 +00:00
$smart['impressions_total'] = $impressions_total;
$smart['clicks_total'] = $clicks_total;
# Impr. percentage, Clicks percentage, CTR
$ctr_count = 0;
$ctr_amt = 0;
for ($ii=1; $ii<=12; $ii++) {
if ($smart['results'][$ii]['show']) {
# Impression percentage
$impressions_percentage = $smart['results'][$ii]['served']/$impressions_total;
$smart['results'][$ii]['impressions_percentage'] = sprintf('(%s%%)',round($impressions_percentage*100));
# Clicks percentage
2009-08-03 04:10:16 +00:00
$clicks_percentage = $smart['results'][$ii]['clicked']/$clicks_total;
$smart['results'][$ii]['clicks_percentage'] = sprintf('(%s%%)',round($clicks_percentage*100));
# Cost
2009-08-03 04:10:16 +00:00
$impressions_percentage = $smart['results'][$ii]['served']/$impressions_total;
$smart['results'][$ii]['cost'] = round($budget*$impressions_percentage,2);
# CTR
2009-08-03 04:10:16 +00:00
$ctr = ($smart['results'][$ii]['clicked']/$smart['results'][$ii]['served'])*100;
if ($ctr < 0)
$smart['results'][$ii]['ctr'] = round($ctr).'%';
else
$smart['results'][$ii]['ctr'] = round($ctr,1).'%';
2009-08-03 04:10:16 +00:00
$ctr_amt += $ctr;
} else {
2009-08-03 04:10:16 +00:00
$smart['results'][$ii]['impressions_percentage'] = '';
$smart['results'][$ii]['clicks_percentage'] = '';
$smart['results'][$ii]['cost'] = 0;
$smart['results'][$ii]['ctr'] = '0%';
}
2009-08-03 04:10:16 +00:00
$ctr_count++;
}
2009-08-03 04:10:16 +00:00
# CTR Avg:
if ($ctr_count > 0)
$smart['ctr_avg'] = round($ctr_amt/$ctr_count,2).'%';
else
$smart['ctr_avg'] = '0%';
2009-08-03 04:10:16 +00:00
# CPC Avg:
if ($clicks_total > 0) {
$cpc_avg = $budget / $clicks_total;
2009-08-03 04:10:16 +00:00
if ($cpc_avg < .1)
$smart['cpc_avg'] = round($cpc_avg,3);
else
2009-08-03 04:10:16 +00:00
$smart['cpc_avg'] = round($cpc_avg,2);
} else {
$smart['cpc_avg'] = 0;
}
2009-08-03 04:10:16 +00:00
# CPI Avg:
if ($impressions_total > 0) {
$cpi_avg = $budget / $impressions_total;
2009-08-03 04:10:16 +00:00
if ($cpi_avg < .1)
$smart['cpi_avg'] = round($cpi_avg,3);
else
$smart['cpi_avg'] = round($cpi_avg,2);
} else {
$smart['cpi_avg'] = 0;
}
# Cost per Conversion:
if ($inv[0] > 0)
$smart['conversion_cost'] = round($budget/$inv[0],2);
else
$smart['conversion_cost'] = 0;
# Avg sales amount:
if($inv[0] > 0 && $budget > 0)
$smart['invoice_avg'] = round($inv[1]/$inv[0],2);
else
$smart['invoice_avg'] = 0;
# ROI
if ($inv[0] > 0 && $budget > 0)
$smart['roi'] = round(($inv[1]/$budget)*100).'%';
else
$smart['roi'] = '---';
# Impression to Buy Ratio
if($inv[0] > 0 && $impressions_total > 0) {
$ratio = round($impressions_total/$inv[0]);
$percent = round(($inv[0]/$impressions_total)*100,3);
$smart['impr_to_buy'] = sprintf('%s / 1 (%s)%%',$ratio,$percent);
} else {
$smart['impr_to_buy'] = '---';
}
# Click-thru to Buy ratio
if ($inv[0] > 0 && $clicks_total > 0) {
$ratio = round($clicks_total/$inv[0]);
$percent = round(($inv[0]/$clicks_total)*100,2);
$smart['click_to_buy'] = sprintf('%s / 1 (%s)%%',$ratio,$percent);
} else {
$smart['click_to_buy'] = '---';
}
global $smarty;
2009-08-03 04:10:16 +00:00
$smarty->assign('record',$smart);
}
### Get the number of referred accounts:
function referred_accounts ($id) {
$dba = new CORE_database;
$db = &DB();
$sql = $dba->sql_select("account","id","campaign_id = $id", false, $db);
$rs = $db->Execute($sql);
return $rs->RecordCount();
}
### Get the number of referred invoices:
function referred_invoices ($id) {
$dba = new CORE_database;
$db = &DB();
$sql = $dba->sql_select("invoice","id,total_amt","campaign_id = $id", false, $db);
$rs = $db->Execute($sql);
if ($rs->RecordCount() == 0) {
return Array(0,0);
} else {
$total = 0;
while(!$rs->EOF) {
$total += $rs->fields['total_amt'];
$rs->MoveNext();
}
}
return Array($rs->RecordCount(), $total);
2009-08-03 04:10:16 +00:00
}
##############################
## AFFILIATE VIEW ##
##############################
function affiliate($VAR)
2009-08-03 04:10:16 +00:00
{
$type = "view";
2009-03-28 05:20:19 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$db->view($VAR, $this, $type);
2009-08-03 04:10:16 +00:00
}
##############################
## UPDATE ##
##############################
function update($VAR)
{
# Validate the files
for($i=1; $i<=12; $i++) {
if(isset($_FILES['upload_file'.$i]) && $_FILES['upload_file'.$i]['size'] > 0) {
$VAR['campaign_type'.$i] = $_FILES['upload_file'.$i]['type'];
2009-08-03 04:10:16 +00:00
$VAR['campaign_file'.$i] = $_FILES['upload_file'.$i]['name'];
}
}
# Store the record
$type = "update";
2009-03-28 05:20:19 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$db = new CORE_database;
$rs = $db->update($VAR, $this, $type);
### Copy the files
if($rs) {
for($i=1; $i<=12; $i++) {
if(isset($_FILES['upload_file'.$i]) && $_FILES['upload_file'.$i]['size'] > 0)
{
if(!copy($_FILES['upload_file'.$i]['tmp_name'], PATH_FILES . 'campaign_'.$VAR['campaign_id'].'_'.$i.'.dat'))
{
### ERROR:
}
}
}
2009-08-03 04:10:16 +00:00
}
}
##############################
## DELETE ADDs ##
##############################
function delete_add($VAR)
2009-08-03 04:10:16 +00:00
{
$field = 'file'.$VAR['file'];
$db = &DB();
$sql = 'UPDATE ' . AGILE_DB_PREFIX . 'campaign
SET
'.$field.' = \'\'
WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr($VAR["campaign_id"]);
$result = $db->Execute($sql);
error_reporting(0);
if(!unlink(PATH_FILES . 'campaign_'.$VAR["campaign_id"].'_' . $VAR['file'] . '.dat'))
{
global $C_debug;
$C_debug->error('file.inc.php','delete', PATH_FILES . 'file_'.$id[$i].
'.dat does not exist for deletion. File could not be deleted.');
}
# Restore the proper error level
$error_reporting_eval = 'error_reporting('.ERROR_REPORTING.');';
eval($error_reporting_eval);
}
##############################
## DELETE ##
##############################
function delete($VAR)
2009-08-03 04:10:16 +00:00
{
$db = &DB();
$id = $this->table . '_id';
# generate the list of ID's
$id_list = '';
$ii=0;
if(isset($VAR["delete_id"]))
{
2009-03-28 05:20:19 +00:00
$id = explode(',',$VAR["delete_id"]);
}
elseif (isset($VAR["id"]))
{
2009-03-28 05:20:19 +00:00
$id = explode(',',$VAR["id"]);
}
for($i=0; $i<count($id); $i++)
{
if($id[$i] != '')
{
if($i == 0)
2009-08-03 04:10:16 +00:00
{
$id_list .= " id = " . $db->qstr($id[$i]) . " ";
$ii++;
}
else
{
$id_list .= " OR id = " . $db->qstr($id[$i]) . " ";
$ii++;
2009-08-03 04:10:16 +00:00
}
}
}
if($ii>0)
{
# generate the full query
$q = "DELETE FROM
".AGILE_DB_PREFIX."$this->table
WHERE
$id_list
AND
site_id = '" . DEFAULT_SITE . "'";
# execute the query
$result = $db->Execute($q);
# error reporting
if ($result === false)
{
global $C_debug;
2009-08-03 04:10:16 +00:00
$C_debug->error('campaign.inc.php','delete', $db->ErrorMsg());
}
else
{
for($i=0; $i<count($id); $i++)
{
if($id[$i] != '')
{
error_reporting(0);
2009-08-03 04:10:16 +00:00
for($ii=1; $ii<=12; $ii++)
unlink(PATH_FILES . 'campaign_'.$id[$i].'_'.$ii.'.dat');
# Restore the proper error level
$error_reporting_eval = 'error_reporting('.ERROR_REPORTING.');';
2009-08-03 04:10:16 +00:00
eval($error_reporting_eval);
}
}
# Alert delete message
global $C_debug, $C_translate;
$C_translate->value["CORE"]["module_name"] = $C_translate->translate('name',$this->module,"");
$message = $C_translate->translate('alert_delete_ids',"CORE","");
2009-08-03 04:10:16 +00:00
$C_debug->alert($message);
2009-08-03 04:10:16 +00:00
}
}
}
##############################
## SEARCH SHOW ##
##############################
2009-08-03 04:10:16 +00:00
function search_show($VAR) {
$smart = parent::search_show($VAR);
2009-08-03 04:10:16 +00:00
for($i=0; $i<count($smart); $i++) {
# Get the stats for each advertisement:
2009-08-03 04:10:16 +00:00
$budget = $smart[$i]['budget'];
# Invoices referred
2009-08-03 04:10:16 +00:00
$inv = $this->referred_invoices($smart[$i]['id']);
$smart[$i]['invoices_referred'] = $inv[0];
$smart[$i]['invoices_revenue'] = $inv[1];
2009-08-03 04:10:16 +00:00
@$smart[$i]['invoice_avg'] = $inv[1] / $inv[0];
# total impressions & clicks:
$impressions_total=0;
$clicks_total=0;
for($ii=1; $ii<=12; $ii++)
2009-08-03 04:10:16 +00:00
{
$impressions_total += $smart[$i]["served".$ii];
$clicks_total += $smart[$i]["clicked".$ii];
if(empty($smart[$i]["served".$ii]) && empty($smart[$i]["served".$ii]) && empty($smart[$i]["served".$ii]))
$smart[$i]["show".$ii] = false;
else
2009-08-03 04:10:16 +00:00
$smart[$i]["show".$ii] = true;
}
$smart[$i]['impressions_total'] = $impressions_total;
$smart[$i]['clicks_total'] = $clicks_total;
2009-08-03 04:10:16 +00:00
# Impr. percentage, Clicks percentage, CTR
$ctr_count = 0;
$ctr_amt = 0;
for($ii=1; $ii<=12; $ii++)
2009-08-03 04:10:16 +00:00
{
# CTR
if( $smart[$i]["show".$ii] ) {
2009-08-03 04:10:16 +00:00
$ctr = ( $smart[$i]["clicked".$ii] / $smart[$i]["served".$ii] ) * 100;
if($ctr < 0)
$smart[$i]['ctr'.$ii] = round($ctr).'%';
2009-08-03 04:10:16 +00:00
else
$smart[$i]['ctr'.$ii] = round($ctr,1).'%';
$ctr_count++;
$ctr_amt += $ctr;
} else {
$smart[$i]['ctr'.$ii] = "0%";
$ctr_count++;
}
}
# CTR Avg:
if($ctr_count > 0)
$smart[$i]['ctr_avg'] = round( $ctr_amt / $ctr_count, 2) .'%';
2009-08-03 04:10:16 +00:00
else
$smart[$i]['ctr_avg'] = "0%";
# CPC Avg:
if($clicks_total > 0) {
$cpc_avg = $budget / $clicks_total;
if($cpc_avg < .1)
$smart[$i]['cpc_avg'] = round($cpc_avg,3);
2009-08-03 04:10:16 +00:00
else
$smart[$i]['cpc_avg'] = round($cpc_avg,2);
} else {
$smart[$i]['cpc_avg'] = 0;
}
# CPI Avg:
if($impressions_total > 0) {
$cpi_avg = $budget / $impressions_total;
if($cpi_avg < .1)
$smart[$i]['cpi_avg'] = round($cpi_avg,3);
else
$smart[$i]['cpi_avg'] = round($cpi_avg,2);
} else {
$smart[$i]['cpi_avg'] = 0;
}
# Cost per Conversion:
if($inv[0] > 0 && $budget > 0)
$smart[$i]['conversion_cost'] = round($budget / $inv[0], 2);
else
$smart[$i]['conversion_cost'] = 0;
# ROI
if($inv[0] > 0 && $budget > 0)
$smart[$i]['roi'] = round(($inv[1] / $budget) * 100).'%';
else
2009-08-03 04:10:16 +00:00
$smart[$i]['roi'] = '---';
}
global $smarty;
2009-08-03 04:10:16 +00:00
$smarty->assign('search_show',$smart);
}
##############################
## SEARCH EXPORT ##
2009-08-03 04:10:16 +00:00
##############################
function search_export($VAR)
{
2009-08-03 04:10:16 +00:00
# require the export class
require_once (PATH_CORE . "export.inc.php");
# Call the correct export function for inline browser display, download, email, or web save.
if($VAR["format"] == "excel")
{
$type = "export_excel";
2009-03-28 05:20:19 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$export = new CORE_export;
2009-08-03 04:10:16 +00:00
$export->search_excel($VAR, $this, $type);
}
else if ($VAR["format"] == "pdf")
{
$type = "export_pdf";
2009-03-28 05:20:19 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$export = new CORE_export;
2009-08-03 04:10:16 +00:00
$export->search_pdf($VAR, $this, $type);
}
else if ($VAR["format"] == "xml")
{
$type = "export_xml";
2009-03-28 05:20:19 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$export = new CORE_export;
$export->search_xml($VAR, $this, $type);
}
else if ($VAR["format"] == "csv")
{
$type = "export_csv";
2009-03-28 05:20:19 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$export = new CORE_export;
$export->search_csv($VAR, $this, $type);
}
else if ($VAR["format"] == "tab")
{
$type = "export_tab";
2009-03-28 05:20:19 +00:00
$this->method["$type"] = explode(",", $this->method["$type"]);
$export = new CORE_export;
$export->search_tab($VAR, $this, $type);
2009-08-03 04:10:16 +00:00
}
}
}
2009-08-03 04:10:16 +00:00
?>