0)
$where_list .= " OR ";
$where_list .= " {$pre}ticket.department_id = ".$db->qstr($avail[$i]);
}
$where_list .= ' ) AND ';
}
$q = "SELECT DISTINCT ".AGILE_DB_PREFIX."ticket.id FROM ".AGILE_DB_PREFIX."ticket ";
$q_save = "SELECT DISTINCT %%fieldList%% FROM ".AGILE_DB_PREFIX."ticket ";
######## GET ANY STATIC VARS TO SEARCH ##########
$join_list = '';
if(!empty($VAR["static_relation"]) && count( $VAR["static_relation"] > 0 )) {
while(list($idx, $value) = each ($VAR["static_relation"])) {
if($value != "") {
$join_list .= " INNER JOIN {$pre}static_var_record AS s{$idx} ON
(
s{$idx}.record_id = {$pre}{$this->table}.id
AND
s{$idx}.static_var_relation_id = '{$idx}'
AND
s{$idx}.site_id = ".$db->qstr(DEFAULT_SITE)."
AND";
if(ereg("%", $value))
$join_list .= " s{$idx}.value LIKE ".$db->qstr($VAR["static_relation"]["$idx"]);
else
$join_list .= " s{$idx}.value = ".$db->qstr($VAR["static_relation"]["$idx"]);
$join_list .= " ) ";
}
}
}
######## END STATIC VAR SEARCH ##################
# standard where list
$q .= $join_list . $where_list ." ".AGILE_DB_PREFIX."ticket.site_id = " . $db->qstr(DEFAULT_SITE);
$q_save .= $join_list . $where_list ." %%whereList%% ";
################## DEBUG ##################
#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('ticket.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 search_show($VAR)
{
$this->construct();
### Get the departments this staff member is authorized for:
$dbs = &DB();
$sql = 'SELECT id, department_avail FROM ' . AGILE_DB_PREFIX . 'staff WHERE
site_id = ' . $dbs->qstr(DEFAULT_SITE) . ' AND
account_id = ' . $dbs->qstr(SESS_ACCOUNT);
$result = $dbs->Execute($sql);
if($result->RecordCount() == 0)
{
### ERROR: this account does not have a staff id
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('staff_no_account','ticket',''));
return;
}
else
{
$staff_id = $result->fields['id'];
@$avail = unserialize($result->fields['department_avail']);
}
$type = "search";
$this->method["$type"] = split(",", $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 . "ticket" . "." . $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 . "ticket" . "." . $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 . 'ticket.'.$VAR['order_by'];
$smarty_order = $VAR['order_by'];
} else {
$order_by = ' ORDER BY ' . AGILE_DB_PREFIX . 'ticket.'.$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 . "ticket."."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('ticket.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;
}
$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)
{
$this->construct();
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, 'user_add', 'search');
else
$arr = $static_var->generate_form($this->module, 'user_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;
}
}
##############################
## REPLY ##
##############################
function reply($VAR)
{
$this->construct();
global $smarty;
if(!isset($VAR['ticket_id']) || $VAR['ticket_id'] == '')
### ERROR: ID must be set!!
{
global $C_debug, $C_translate, $C_vars;
$C_debug->alert($C_translate->translate('user_ticket_nonexist','ticket',''));
$C_vars->strip_slashes_all();
return false;
}
if(!isset($VAR['ticket_reply']) || $VAR['ticket_reply'] == '')
### ERROR: Ticket Reply must be set!!
{
global $C_debug, $C_translate, $C_vars;
$C_debug->alert($C_translate->translate('staff_response_required','ticket',''));
$C_vars->strip_slashes_all();
return;
}
### Get the ticket record
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr($VAR['ticket_id']);
$ticket = $db->Execute($sql);
if($ticket->RecordCount() == 0)
{
### ERROR: ticket doesn't exist!
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_ticket_nonexist','ticket',''));
return false;
}
$status = $ticket->fields['status'];
$department_id = $ticket->fields['department_id'];
### Get the staff ID:
$dbs = &DB();
$sql = 'SELECT id FROM ' . AGILE_DB_PREFIX . 'staff WHERE
site_id = ' . $dbs->qstr(DEFAULT_SITE) . ' AND
account_id = ' . $dbs->qstr(SESS_ACCOUNT);
$staff = $dbs->Execute($sql);
$staff_id = $staff->fields['id'];
### Update the ticket status:
$sql = 'UPDATE ' . AGILE_DB_PREFIX . 'ticket SET
date_last = ' . $dbs->qstr(time()) .',
last_reply = ' . $dbs->qstr( '1' ) .',
staff_id = ' . $dbs->qstr($staff_id) .'
WHERE
site_id = ' . $dbs->qstr(DEFAULT_SITE) . ' AND
id = ' . $dbs->qstr($VAR['ticket_id']);
$update = $dbs->Execute($sql);
### Add the message Record to the DB
$db = &DB();
$id = $db->GenID(AGILE_DB_PREFIX . "" . 'ticket_message_id');
$sql = 'INSERT INTO ' . AGILE_DB_PREFIX . 'ticket_message SET
site_id = ' . $db->qstr(DEFAULT_SITE) . ',
staff_id = ' . $db->qstr($staff_id) . ',
id = ' . $db->qstr($id) . ',
ticket_id = ' . $db->qstr($VAR['ticket_id']) . ',
date_orig = ' . $db->qstr(time()) . ',
message = ' . $db->qstr(htmlspecialchars($VAR['ticket_reply']), get_magic_quotes_gpc()) ;
$result = $db->Execute($sql);
if(!empty($VAR['enable_user_notice']))
{
###################################################################
### Mail the user the new_ticket email template
require_once(PATH_MODULES . 'email_template/email_template.inc.php');
$my = new email_template;
if(!empty($ticket->fields["email"]))
{
global $VAR;
$VAR['key'] = $this->key($ticket->fields["email"]);
$VAR['email'] = trim($ticket->fields["email"]);
$VAR['message'] = trim(htmlspecialchars($VAR['ticket_reply']));
$my->send('ticket_staff_update_user', $ticket->fields["email"], $VAR['ticket_id'], '', '');
}
else
{
$sql = 'SELECT email FROM ' . AGILE_DB_PREFIX . 'account WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr($ticket->fields['account_id']);
$account = $db->Execute($sql);
global $VAR;
$VAR['key'] = $this->key($ticket->fields["email"]);
$VAR['email'] = trim($account->fields['email']);
$VAR['message'] = trim(htmlspecialchars($VAR['ticket_reply']));
$my->send('ticket_staff_update_user', $ticket->fields["account_id"], $VAR['ticket_id'], '', '');
}
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('staff_response_success','ticket',''));
}
# send the reply to the staff members
$this->send_staff_emails($VAR['ticket_id'], SESS_ACCOUNT, 0, 'change');
}
##############################
## USER LIST ##
##############################
function user_list($VAR)
{
$this->construct();
global $C_debug, $C_translate, $smarty;
if(SESS_LOGGED == false) {
if(!$this->is_key_match($VAR)) {
$C_debug->alert($C_translate->translate('login_required', '', ''));
return false;
}
}
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket WHERE site_id = ' . $db->qstr(DEFAULT_SITE);
if(SESS_LOGGED == true) {
$sql .= ' AND (
account_id = ' . $db->qstr(SESS_ACCOUNT) . '
) ';
}
if (SESS_LOGGED == true && !empty($VAR['email']) )
$sql .= ' OR ';
else if (SESS_LOGGED == false && !empty($VAR['email']) )
$sql .= ' AND ';
if ( !empty($VAR['email']) ) {
$sql .= ' (
email = ' . $db->qstr(@$VAR['email']) . '
AND
email != ' . $db->qstr('') . '
AND
email IS NOT NULL
)
ORDER BY status, date_last DESC';
}
$result = $db->Execute($sql);
if($result->RecordCount() == 0)
{
### Or if no results:
$smarty->assign('ticket_results', false);
return false;
}
else
{
while (!$result->EOF)
{
$smart[] = $result->fields;
$result->MoveNext();
}
## Set everything as a smarty array, and return:
$smarty->assign('ticket_results', $smart);
return true;
}
}
##############################
## USER UPDATE ##
##############################
function user_update($VAR)
{
$this->construct();
global $smarty;
if(SESS_LOGGED == false) {
if(!$this->is_key_match($VAR)) {
$smarty->assign('ticket', false);
return false;
}
}
if(!isset($VAR['id']) || $VAR['id'] == '')
### ERROR: ID must be set!!
{
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_ticket_nonexist','ticket',''));
}
### Check it this is a request to close the ticket
if(isset($VAR['ticket_status']))
{
### Close the ticket
$db = &DB();
$sql = 'UPDATE ' . AGILE_DB_PREFIX . 'ticket
SET
status = ' . $db->qstr('2') .',
date_last = ' . $db->qstr(time()) .'
WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . '
AND
(
account_id = ' . $db->qstr(SESS_ACCOUNT) . ' OR
email = ' . $db->qstr(@$VAR["email"]) . '
)
AND
id = ' . $db->qstr($VAR['id']);
$ticket = $db->Execute($sql);
return;
}
if(!isset($VAR['ticket_reply']) || $VAR['ticket_reply'] == '')
### ERROR: Ticket Reply must be set!!
{
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('staff_response_required','ticket',''));
}
### Get the ticket record
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket
WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . '
AND
(
account_id = ' . $db->qstr(SESS_ACCOUNT) . ' OR
email = ' . $db->qstr(@$VAR["email"]) . '
)
AND
id = ' . $db->qstr($VAR['id']);
$ticket = $db->Execute($sql);
if($ticket->RecordCount() == 0)
{
### ERROR: ticket doesn't exist!
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_ticket_nonexist','ticket',''));
return false;
}
$status = $ticket->fields['status'];
$department_id = $ticket->fields['department_id'];
$authorized = false;
if($status == '2')
{
if(SESS_LOGGED == true)
{
### Check if user is auth for this department still!
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket_department WHERE
id = ' . $db->qstr($department_id) . ' AND
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
status = ' . $db->qstr('1');
$result = $db->Execute($sql);
if($result->RecordCount() == 0)
{
### ERROR: not authorized to reopen this ticket!
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_not_auth_reopen','ticket',''));
return false;
}
$i = 0;
$arr = unserialize($result->fields['group_id']);
global $C_auth;
for($i=0; $iauth_group_by_id($arr[$i])) $authorized = true;
}
}
else
{
### Check if this department is authorized for the 'All Users' group id (0):
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket_department WHERE
id = ' . $db->qstr($department_id) . ' AND
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
status = ' . $db->qstr('1');
$result = $db->Execute($sql);
if($result->RecordCount() == 0)
{
### ERROR: not authorized to reopen this ticket!
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_not_auth_reopen','ticket',''));
return false;
}
$i = 0;
$arr = unserialize($result->fields['group_id']);
global $C_auth;
for($i=0; $ialert($C_translate->translate('user_not_auth_reopen','ticket',''));
return false;
}
elseif (!$authorized && SESS_LOGGED == false)
{
### ERROR: not authorized to reopen this ticket & should try to login!
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_not_auth_reopen_login','ticket',''));
return false;
}
if($status == '0' || $status == '2')
$status = '0';
else
$status = '1';
### Update the ticket status:
$sql = 'UPDATE ' . AGILE_DB_PREFIX . 'ticket SET
status = ' . $db->qstr($status) .',
last_reply = ' . $db->qstr( '2' ) .',
date_last = ' . $db->qstr(time()) .'
WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
account_id = ' . $db->qstr(SESS_ACCOUNT) . ' AND
id = ' . $db->qstr($VAR['id']);
$update = $db->Execute($sql);
### Add the message Record to the DB
$db = &DB();
$id = $db->GenID(AGILE_DB_PREFIX . "" . 'ticket_message_id');
$sql = 'INSERT INTO ' . AGILE_DB_PREFIX . 'ticket_message SET
site_id = ' . $db->qstr(DEFAULT_SITE) . ',
id = ' . $db->qstr($id) . ',
ticket_id = ' . $db->qstr($VAR['id']) . ',
date_orig = ' . $db->qstr(time()) . ',
message = ' . $db->qstr(htmlspecialchars($VAR['ticket_reply']), get_magic_quotes_gpc()) ;
$result = $db->Execute($sql);
###################################################################
### Mail the user the new_ticket email template
require_once(PATH_MODULES . 'email_template/email_template.inc.php');
$my = new email_template;
global $VAR;
$VAR['email'] = trim($ticket->fields['email']);
$VAR['key'] = $this->key($ticket->fields['email']);
if(SESS_LOGGED)
$my->send('ticket_user_update', SESS_ACCOUNT, $VAR['id'], '', '');
else
$my->send('ticket_user_update', $ticket->fields['email'], $VAR['id'], '', '');
###################################################################
### Get any staff members who should be mailed
$db = &DB();
$sql = 'SELECT id,account_id,department_avail FROM ' . AGILE_DB_PREFIX . 'staff
WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
notify_change = ' . $db->qstr("1");
$result = $db->Execute($sql);
if($result->RecordCount() > 0)
{
while(!$result->EOF)
{
@$avail = unserialize($result->fields['department_avail']);
for($i=0; $isend('ticket_user_update_staff', $result->fields['account_id'], $VAR['id'], $avail[$i], '');
$i = count($avail);
}
}
$result->MoveNext();
}
}
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_update_success','ticket',''));
return;
}
##############################
## USER VIEW ##
##############################
function user_view($VAR)
{
$this->construct();
global $C_debug, $C_translate, $smarty;
if(SESS_LOGGED == false) {
if(!$this->is_key_match($VAR)) {
$smarty->assign('ticket', false);
return false;
}
}
if(empty($VAR['id']))
### ERROR: ID must be set!!
{
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_ticket_nonexist','ticket',''));
$smarty->assign('ticket', false);
return false;
}
### Get the ticket record
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket
WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . '
AND
(
account_id = ' . $db->qstr(SESS_ACCOUNT) . '
OR
email = ' . $db->qstr(@$VAR['email']) . '
)
AND
id = ' . $db->qstr($VAR['id']);
$result = $db->Execute($sql);
if($result->RecordCount() == 0)
{
### Or if no results:
$smarty->assign('ticket', false);
return false;
}
else
{
### Get any replies...
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket_message WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
ticket_id = ' . $db->qstr($VAR['id']) . '
ORDER BY date_orig';
$replys = $db->Execute($sql);
while (!$replys->EOF)
{
$reply[] = $replys->fields;
$replys->MoveNext();
}
## Set everything as a smarty array, and return:
$smarty->assign('ticket', $result->fields);
$smarty->assign('ticket_reply', $reply);
### Get the static vars:
require_once(PATH_CORE . 'static_var.inc.php');
$static_var = new CORE_static_var;
$arr = $static_var->view_form($this->module, $VAR['id']);
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;
}
}
}
##############################
## USER ADD ##
##############################
function user_add($VAR)
{
$this->construct();
global $C_debug, $C_translate, $C_vars, $smarty;
### Strip Slashes
global $VAR;
$C_vars->strip_slashes_all();
####################################################################
### Check that the required fields are set:
### ticket_department_id, ticket_subject, ticket_body
####################################################################
$fields = Array('priority',
'department_id',
'subject',
'body');
for($i=0; $itable . '_' . $field;
if(!isset($VAR["$field_name"]) || trim($VAR["$field_name"]) == "")
{
$this->val_error[] = Array('field' => $this->table . '_' . $field,
'field_trans' => $C_translate->translate('field_' . $field, $this->module, ""), # translate
'error' => $C_translate->translate('validate_any',"", ""));
}
}
####################################################################
### Get required static_Vars and validate them... return an array
### w/ ALL errors...
####################################################################
require_once(PATH_CORE . 'static_var.inc.php');
$static_var = new CORE_static_var;
if(!isset($this->val_error)) $this->val_error = false;
$all_error = $static_var->validate_form($this->module, $this->val_error);
if($all_error != false && gettype($all_error) == 'array')
$this->validated = false;
else
$this->validated = true;
### Validate e-mail
if(!SESS_LOGGED)
{
include_once(PATH_CORE . 'validate.inc.php' );
$C_validate = new CORE_validate;
if(empty($VAR['ticket_email'])) {
$this->validated = false;
$smarty->assign('ticket_email', true);
$all_error[] = Array ( 'field' => 'ticket_email',
'field_trans' => $C_translate->translate('field_email', "ticket", ""),
'error' => $C_translate->translate('validate_any',"", ""));
}
elseif (!$C_validate->validate_email(@$VAR['ticket_email'], false))
{
$this->validated = false;
$smarty->assign('ticket_email', true);
$all_error[] = Array ( 'field' => 'ticket_email',
'field_trans' => $C_translate->translate('field_email', "ticket", ""),
'error' => $C_translate->translate('validate_email',"", ""));
}
$this->email = $VAR['ticket_email'];
} else {
# Get the e-mail addy from the user's account
$db = &DB();
$sql = 'SELECT email FROM ' . AGILE_DB_PREFIX . 'account WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr(SESS_ACCOUNT);
$result = $db->Execute($sql);
$VAR['ticket_email'] = $result->fields['email'];
$this->email = $result->fields['email'];
}
###################################################################
### Check that the user is authorized for this department
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket_department WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
id = ' . $db->qstr($VAR['ticket_department_id']) . ' AND
status = ' . $db->qstr('1');
$result = $db->Execute($sql);
if($result->RecordCount() == 0)
{
###################################################################
### ERROR: The selected department is inactive or invalid
$C_debug->alert($C_translate->translate('department_invalid', 'ticket', ''));
return false;
}
global $C_auth;
$i = 0;
$dept_auth = false;
while(!$result->EOF)
{
$arr = unserialize($result->fields['group_id']);
if(!SESS_LOGGED)
{
### Check if the specified department is authorized for the 'All Users' group (0):
for($i=0; $ialert($C_translate->translate('login_required', '', ''));
return false;
}
}
else
{
for($i=0; $iauth_group_by_id($arr[$i])) {
$dept_auth = true;
}
}
}
$result->MoveNext();
}
if(!$dept_auth)
{
###################################################################
### ERROR: The current user does not have access to the selected department!
$C_debug->alert($C_translate->translate('department_not_auth', 'ticket', ''));
return false;
}
else
{
####################################################################
# If validation was failed, skip the db insert &
# set the errors & origonal fields as Smarty objects,
# and change the page to be loaded.
####################################################################
if(!$this->validated)
{
global $smarty;
# set the errors as a Smarty Object
$smarty->assign('form_validation', $all_error);
# set the page to be loaded
if(!defined("FORCE_PAGE"))
{
define('FORCE_PAGE', $VAR['_page_current']);
}
global $C_vars;
$C_vars->strip_slashes_all();
return;
}
###################################################################
### Assemble the SQL & Insert the ticket
$db = &DB();
$id = $db->GenID(AGILE_DB_PREFIX . 'ticket_id');
$sql = 'INSERT INTO ' . AGILE_DB_PREFIX . 'ticket SET
site_id = ' . $db->qstr(DEFAULT_SITE) . ',
id = ' . $db->qstr($id) . ',
date_orig = ' . $db->qstr(time()) . ',
date_last = ' . $db->qstr(time()) . ',
date_expire = ' . $db->qstr(time()+86400*7) . ',
account_id = ' . $db->qstr(SESS_ACCOUNT) . ',
department_id=' . $db->qstr($VAR['ticket_department_id']) . ',
status = ' . $db->qstr(0) . ',
last_reply = 0,
priority = ' . $db->qstr($VAR['ticket_priority']) . ',
subject = ' . $db->qstr($VAR['ticket_subject']) . ',
email = ' . $db->qstr($VAR['ticket_email']) . ',
body = ' . $db->qstr(htmlspecialchars($VAR['ticket_body'])) ;
$result = $db->Execute($sql);
# error reporting:
if ($result === false)
{
global $C_debug;
$C_debug->error('ticket.inc.php','user_add', $db->ErrorMsg());
return false;
}
###################################################################
### Insert the static vars...
$static_var->add($VAR, $this->module, $id);
###################################################################
### Mail the user the new_ticket email template
require_once(PATH_MODULES . 'email_template/email_template.inc.php');
$VAR['email'] = trim($this->email);
$VAR['key'] = $this->key($this->email);
$my = new email_template;
$my->send('ticket_user_add', $this->email, $id, '', '');
unset($VAR['key']);
unset($VAR['email']);
###################################################################
### Get any staff members who should be mailed
$db = &DB();
$sql = 'SELECT id,account_id,department_avail FROM ' . AGILE_DB_PREFIX . 'staff
WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
notify_new = ' . $db->qstr("1");
$result = $db->Execute($sql);
if($result->RecordCount() > 0)
{
while(!$result->EOF)
{
@$avail = unserialize($result->fields['department_avail']);
for($i=0; $isend('ticket_user_add_staff', $result->fields['account_id'], $id, $avail[$i], 'sql3');
$i = count($avail);
}
}
$result->MoveNext();
}
}
}
global $C_debug, $C_translate;
$C_debug->alert($C_translate->translate('user_add_success','ticket',''));
}
##############################
## DOES KEY MATCH? ##
##############################
function is_key_match($VAR)
{
global $smarty;
if (SESS_LOGGED == true)
{
$smarty->assign('ticket_key', true);
return true;
}
if (empty($VAR['key']) || empty($VAR['email']))
{
$smarty->assign('ticket_key', false);
return false;
}
if (strtolower($VAR['key']) == strtolower( $this->key( $VAR['email'] ) ) )
{
$smarty->assign('ticket_key', true);
return true;
### define as cookies:
} else {
$smarty->assign('ticket_key', false);
return false;
}
}
##############################
## GENERATE KEY ##
##############################
function key ($email) {
$key = strtoupper( md5(strtolower(trim($email)) . md5(LICENSE_KEY)) );
return $key;
}
##############################
## IS USER AUTH FOR DEPTS? ##
##############################
function is_user_auth($VAR)
{
/* check if current session is authorized for any ticket departments..
and return true/false...
*/
global $smarty;
$db = &DB();
$sql = 'SELECT * FROM ' . AGILE_DB_PREFIX . 'ticket_department WHERE
site_id = ' . $db->qstr(DEFAULT_SITE) . ' AND
status = ' . $db->qstr('1');
$result = $db->Execute($sql);
if($result->RecordCount() == 0)
{
$smarty->assign('display', false);
return false;
}
global $C_auth;
$ii = 0;
while(!$result->EOF)
{
@$arr = unserialize($result->fields['group_id']);
for($i=0; $iauth_group_by_id($arr[$i]))
{
### Add to the array
$ii++;
$arr_smarty[] = Array( 'name' => $result->fields['name'],
'description' => $result->fields['description'],
'id' => $result->fields['id']);
$i=count($arr);
}
}
$result->MoveNext();
}
if($ii == "0")
{
$smarty->assign('display', false);
return false;
}
else
{
$smarty->assign('display', true);
$smarty->assign('results', $arr_smarty);
return true;
}
}
# Open the constructor for this mod
function construct()
{
# name of this module:
$this->module = "ticket";
# location of the construct XML file:
$this->xml_construct = PATH_MODULES . "" . $this->module . "/" . $this->module . "_construct.xml";
# open the construct file for parsing
if(defined('AJAX')) {
require_once(PATH_CORE.'xml.inc.php');
require_once(PATH_CORE.'translate.inc.php');
$C_translate= new CORE_translate;
global $C_translate;
}
$C_xml = new CORE_xml;
$construct = $C_xml->xml_to_array($this->xml_construct);
$this->method = $construct["construct"]["method"];
$this->trigger = $construct["construct"]["trigger"];
$this->field = $construct["construct"]["field"];
$this->table = $construct["construct"]["table"];
$this->module = $construct["construct"]["module"];
$this->cache = $construct["construct"]["cache"];
$this->order_by = $construct["construct"]["order_by"];
$this->limit = $construct["construct"]["limit"];
}
}
?>