* @package AgileBill * @version 1.4.93 */ class affiliate extends OSB_module { private $id_prefix = 'AB-'; ########################################### ### AJAX Auto-selector ########################################### function autoselect($VAR) { $db = &DB(); $p = AGILE_DB_PREFIX; if (empty($VAR['affiliate_search'])) { $where = "{$p}affiliate.id > 0"; $type = 1; } elseif (is_numeric($VAR['affiliate_search'])) { $where = "{$p}affiliate.id LIKE ".$db->qstr($VAR['search']."%") ; $type = 1; } elseif (eregi(" ", $VAR['affiliate_search'])) { $arr = explode(" ", $VAR['affiliate_search']); $where = "{$p}account.first_name = ".$db->qstr($arr[0])." AND ". "{$p}account.last_name LIKE ".$db->qstr($arr[1].'%') ; $type = 2; } elseif (eregi("@", $VAR['affiliate_search'])) { $where = "{$p}account.email LIKE ".$db->qstr('%'.$VAR['affiliate_search'].'%') ; $type = 3; } else { $where = "{$p}account.username LIKE ".$db->qstr($VAR['affiliate_search'].'%')." OR ". "{$p}account.first_name LIKE ".$db->qstr($VAR['affiliate_search'].'%')." OR ". "{$p}account.last_name LIKE ".$db->qstr($VAR['affiliate_search'].'%'); $type = 4; } $q = "SELECT DISTINCT {$p}affiliate.id, {$p}account.first_name, {$p}account.last_name, {$p}account.username, {$p}account.email FROM {$p}account LEFT JOIN {$p}affiliate ON {$p}account.id = {$p}affiliate.account_id WHERE ( $where ) AND {$p}affiliate.id IS NOT NULL AND {$p}affiliate.site_id = " . DEFAULT_SITE . " AND {$p}account.site_id = " . DEFAULT_SITE." ORDER BY {$p}account.first_name,{$p}account.last_name"; $result = $db->SelectLimit($q, 10); echo '
" . $q; #echo "
" . $q_save; #exit; # run the database query $result = $db->Execute($q); # error reporting if ($result === false) { global $C_debug; $C_debug->error('affiliate.inc.php','search', $db->ErrorMsg()); return false; } # get the result count: $results = $result->RecordCount(); # get the first record id: if($results == 1) $record_id = $result->fields['id']; # define the DB vars as a Smarty accessible block global $smarty; # Create the definition for fast-forwarding to a single record: if ($results == 1 && !isset($this->fast_forward)) { $smarty->assign('record_id', $record_id); } # create the search record: if($results > 0) { # create the search record include_once(PATH_CORE . 'search.inc.php'); $search = new CORE_search; $arr['module'] = $this->module; $arr['sql'] = $q_save; $arr['limit'] = $limit; $arr['order_by']= $order_by; $arr['results'] = $results; $search->add($arr); # define the search id and other parameters for Smarty $smarty->assign('search_id', $search->id); # page: $smarty->assign('page', '1'); # limit: $smarty->assign('limit', $limit); # order_by: $smarty->assign('order_by', $order_by); } # define the result count $smarty->assign('results', $results); } ############################## ## SEARCH SHOW ## ############################## function xsearch_show($VAR) { $type = "search"; $this->method["$type"] = explode(",", $this->method["$type"]); # set the field list for this method: $arr = $this->method[$type]; $field_list = ''; $i=0; while (list ($key, $value) = each ($arr)) { if($i == 0) { $field_var = $this->table . '_' . $value; $field_list .= AGILE_DB_PREFIX . "affiliate" . "." . $value; // determine if this record is linked to another table/field if($this->field[$value]["asso_table"] != "") { $this->linked[] = array('field' => $value, 'link_table' => $this->field[$value]["asso_table"], 'link_field' => $this->field[$value]["asso_field"]); } } else { $field_var = $this->table . '_' . $value; $field_list .= "," . AGILE_DB_PREFIX . "affiliate" . "." . $value; // determine if this record is linked to another table/field if($this->field[$value]["asso_table"] != "") { $this->linked[] = array('field' => $value, 'link_table' => $this->field[$value]["asso_table"], 'link_field' => $this->field[$value]["asso_field"]); } } $i++; } # get the search details: if(isset($VAR['search_id'])) { include_once(PATH_CORE . 'search.inc.php'); $search = new CORE_search; $search->get($VAR['search_id']); } else { # invalid search! echo '
The search terms submitted were invalid!'; # translate... # alert if(isset($this->trigger["$type"])) { include_once(PATH_CORE . 'trigger.inc.php'); $trigger = new CORE_trigger; $trigger->trigger($this->trigger["$type"], 0, $VAR); } } # get the sort order details: if(isset($VAR['order_by']) && $VAR['order_by'] != "") { $order_by = ' ORDER BY ' . AGILE_DB_PREFIX . 'affiliate.'.$VAR['order_by']; $smarty_order = $VAR['order_by']; } else { $order_by = ' ORDER BY ' . AGILE_DB_PREFIX . 'affiliate.'.$this->order_by; $smarty_order = $search->order_by; } # determine the sort order if(isset($VAR['desc'])) { $order_by .= ' DESC'; $smarty_sort = 'desc='; } else if(isset($VAR['asc'])) { $order_by .= ' ASC'; $smarty_sort = 'asc='; } else { if (!eregi('date',$smarty_order)) { $order_by .= ' ASC'; $smarty_sort = 'asc='; } else { $order_by .= ' DESC'; $smarty_sort = 'desc='; } } # generate the full query $db = &DB(); $q = eregi_replace("%%fieldList%%", $field_list, $search->sql); $q = eregi_replace("%%tableList%%", AGILE_DB_PREFIX.$construct->table, $q); $q = eregi_replace("%%whereList%%", "", $q); $q .= " ".AGILE_DB_PREFIX . "affiliate."."site_id = " . $db->qstr(DEFAULT_SITE); $q .= $order_by; ////////////////// #echo "$q
"; $current_page=1; $offset=-1; if (!empty($VAR['page'])) $current_page = $VAR['page']; if (empty($search->limit)) $search->limit=25; if($current_page>1) $offset = (($current_page * $search->limit) - $search->limit); $result = $db->SelectLimit($q, $search->limit, $offset); # error reporting if ($result === false) { global $C_debug; $C_debug->error('affiliate.inc.php','search_show', $db->ErrorMsg()); if(isset($this->trigger["$type"])) { include_once(PATH_CORE . 'trigger.inc.php'); $trigger = new CORE_trigger; $trigger->trigger($this->trigger["$type"], 0, $VAR); } return; } # put the results into a smarty accessable array $i=0; $class_name = TRUE; while (!$result->EOF) { $smart[$i] = $result->fields; if($class_name) { $smart[$i]['_C'] = 'row1'; $class_name = FALSE; } else { $smart[$i]['_C'] = 'row2'; $class_name = TRUE; } ### Get the sessions referred by this affiliate: $sql2 = "SELECT id FROM ".AGILE_DB_PREFIX."session WHERE affiliate_id = ". $db->qstr($result->fields['id'])." AND site_id = ". $db->qstr(DEFAULT_SITE); $result2 = $db->Execute($sql2); $smart[$i]["stats_sessions"] = $result2->RecordCount(); ### Get the accounts referred by this affiliate: $sql2 = "SELECT id FROM ".AGILE_DB_PREFIX."account WHERE affiliate_id = ". $db->qstr($result->fields['id'])." AND site_id = ". $db->qstr(DEFAULT_SITE); $result2 = $db->Execute($sql2); $smart[$i]["stats_accounts"] = $result2->RecordCount(); ### Get the invoices referred by this affiliate: $sql2 = "SELECT id,total_amt FROM ".AGILE_DB_PREFIX."invoice WHERE affiliate_id = ". $db->qstr($result->fields['id'])." AND site_id = ". $db->qstr(DEFAULT_SITE); $result2 = $db->Execute($sql2); $smart[$i]["stats_invoices"] = $result2->RecordCount(); $total = 0; while(!$result2->EOF) { $total += $result2->fields['total_amt']; $result2->MoveNext(); } $smart[$i]["stats_invoices_amt"] = $total; ### Get the commissions issued to this affiliate: $sql2 = "SELECT commission FROM ".AGILE_DB_PREFIX."invoice_commission WHERE affiliate_id = ". $db->qstr($result->fields['id'])." AND site_id = ". $db->qstr(DEFAULT_SITE); $result2 = $db->Execute($sql2); $total = 0; while(!$result2->EOF) { $total += $result2->fields['commission']; $result2->MoveNext(); } $smart[$i]["stats_commissions"] = $total; $result->MoveNext(); $i++; } # get any linked fields if($i > 0) { $db_join = new CORE_database; $this->result = $db_join->join_fields($smart, $this->linked); } else { $this->result = $smart; } # get the result count: $results = $result->RecordCount(); # define the DB vars as a Smarty accessible block global $smarty; # define the results $smarty->assign($this->table, $this->result); $smarty->assign('page', $VAR['page']); $smarty->assign('order', $smarty_order); $smarty->assign('sort', $smarty_sort); $smarty->assign('limit', $search->limit); $smarty->assign('search_id',$search->id); $smarty->assign('results', $search->results); # get the total pages for this search: if(empty($search->limit)) $this->pages = 1; else $this->pages = intval($search->results / $search->limit); if ($search->results % $search->limit) $this->pages++; # total pages $smarty->assign('pages', $this->pages); # current page $smarty->assign('page', $current_page); $page_arr = ''; for($i=0; $i <= $this->pages; $i++) { if ($this->page != $i) $page_arr[] = $i; } # page array for menu $smarty->assign('page_arr', $page_arr); } ############################## ## STATIC VARS ## ############################## function static_var($VAR) { global $smarty; require_once(PATH_CORE . 'static_var.inc.php'); $static_var = new CORE_static_var; if(ereg('search', $VAR['_page'])) $arr = $static_var->generate_form($this->module, 'add', 'search'); else $arr = $static_var->generate_form($this->module, 'add', 'update'); if(gettype($arr) == 'array') { ### Set everything as a smarty array, and return: $smarty->assign('show_static_var', true); $smarty->assign('static_var', $arr); return true; } else { ### Or if no results: $smarty->assign('show_static_var', false); return false; } } } ?>