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/staff/staff.inc.php
2011-05-03 09:49:04 +10:00

264 lines
7.1 KiB
PHP

<?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
*
* Originally authored by Tony Landis, AgileBill LLC
*
* Recent modifications by Deon George
*
* @author Deon George <deonATleenooksDOTnet>
* @copyright 2009 Deon George
* @link http://osb.leenooks.net
*
* @link http://www.agileco.com/
* @copyright 2004-2008 Agileco, LLC.
* @license http://www.agileco.com/agilebill/license1-4.txt
* @author Tony Landis <tony@agileco.com>
* @package AgileBill
* @subpackage Module:Staff
*/
/**
* The main AgileBill Staff Class
*
* @package AgileBill
* @subpackage Module:Staff
*/
class staff extends OSB_module {
/**
* Get the staff who are a member of a department
*
* @param string $dep Department to find users
* @return array Staff Ids who are a member of the department
* @uses staff_department
*/
public function sDepartmentMember($dep) {
require_once(PATH_MODULES.'staff_department/staff_department.inc.php');
$sdo = new staff_department;
if (! $department = $sdo->sql_GetRecords(array('where'=>array('name'=>$dep))))
return array();
$department = array_pop($department);
$result = array();
foreach ($this->sql_GetRecords(array('where'=>'department_avail IS NOT NULL')) as $record) {
$department_avail = unserialize($record['department_avail']);
if (in_array($department['id'],$department_avail))
array_push($result,$record['account_id']);
}
return $result;
}
/**
* Get the list of emails and names of users in a department
*
* @param string $dep Name of Department to obtain
* @return array Emails and Full Names
* @uses account
*/
public function sDepartmentMemberEmail($dep) {
$result = array();
$members = $this->sDepartmentMember($dep);
if (count($members)) {
require_once(PATH_MODULES.'account/account.inc.php');
$ao = new account;
foreach ($ao->sql_GetRecords(array('where'=>sprintf('id IN (%s)',implode(',',$members)))) as $record)
$result[$record['email']] = sprintf('%s %s',$record['first_name'],$record['last_name']);
}
return $result;
}
/**
* EMAIL ONE STAFF MEMBER
*/
function contact($VAR) {
global $C_translate,$C_debug,$C_vars;
# Validate the required vars (account_id, message, subject)
if (@$VAR['mail_email'] != '' && @$VAR['mail_name'] != '' && @$VAR['mail_subject'] != '' && @$VAR['mail_message'] != '') {
include_once(PATH_CORE.'validate.inc.php');
$validate = new CORE_validate;
if (! $validate->validate_email($VAR['mail_email'],'')) {
$C_debug->alert($C_translate->translate('validate_email','',''));
$C_vars->strip_slashes_all();
return;
}
@$s = $VAR['mail_staff_id'];
@$d = $VAR['mail_department_id'];
# Nothing to do
if ($s > 0) {
} elseif($d > 0) {
# Verify the specified department && get the associated account:
$db = &DB();
$dept = $db->Execute(sqlSelect($db,'staff_department','default_staff_id'));
if ($dept->RecordCount() == 0) {
$C_debug->alert($C_translate->translate('error_dept_non_exist','staff',''));
$C_vars->strip_slashes_all();
return;
}
$s = $dept->fields['default_staff_id'];
} else {
# staff/dept not specified
$C_debug->alert($C_translate->translate('error_staff_dept','staff',''));
$C_vars->strip_slashes_all();
return;
}
## Verify the specified staff account && get the associated account:
$db = &DB();
$staff = $db->Execute(sqlSelect($db,'staff','account_id'));
if ($staff->RecordCount() == 0) {
$C_debug->alert($C_translate->translate('error_staff_non_exist','staff',''));
$C_vars->strip_slashes_all();
return;
}
$account_id = $staff->fields['account_id'];
$account = $db->Execute(sqlSelect($db,'account','email,first_name,last_name'));
if ($account->RecordCount() == 0) {
$C_debug->alert($C_translate->translate('error_staff_non_exist','staff',''));
$C_vars->strip_slashes_all();
return;
}
# Validate any static vars, if defined
$this->validated = true;
if (! empty($VAR['static_relation'])) {
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('staff', $this->val_error);
if ($all_error != false && gettype($all_error) == 'array') {
$this->validated = false;
} else {
$this->validated = true;
# Get the fields and values and append to the message text...
while (list($id,$value) = each($VAR['static_relation'])) {
if (! empty($value) && ! empty($id)) {
# Get the name:
$db = &DB();
$rs = $db->Execute(sqlSelect($db,'static_relation','static_var_id'));
$var_id = $rs->fields['static_var_id'];
$rs = $db->Execute(sqlSelect($db,'static_var','name'));
$name = $rs->fields['name'];
$ul = str_replace('.','-',$name);
$VAR['mail_message'] .= "\r\n\r\n";
$VAR['mail_message'] .= $ul;
$VAR['mail_message'] .= "\r\n";
$VAR['mail_message'] .= $name;
$VAR['mail_message'] .= "\r\n";
$VAR['mail_message'] .= $ul;
$VAR['mail_message'] .= "\r\n";
$VAR['mail_message'] .= $value;
}
}
}
}
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;
}
# OK to send the email:
$E['from_html'] = true;
$E['from_name'] = $VAR['mail_name'];
$E['from_email'] = $VAR['mail_email'];
$db = &DB();
$setup_email = $db->Execute(sqlSelect($db,'setup_email','*'));
$E['priority'] = $VAR['mail_priority'];
$E['html'] = '0';
$E['subject'] = $VAR['mail_subject'];
$E['body_text'] = $VAR['mail_message'];
$E['to_email'] = $account->fields['email'];
$E['to_name'] = $account->fields['first_name'];
if ($setup_email->fields['type'] == 0) {
$type = 0;
} else {
$type = 1;
$E['server'] = $setup_email->fields['server'];
$E['account'] = $setup_email->fields['username'];
$E['password'] = $setup_email->fields['password'];
}
if ($setup_email->fields['cc_list'] != '')
$E['cc_list'] = explode(',',$setup_email->fields['cc_list']);
if ($setup_email->fields['bcc_list'] != '')
$E['bcc_list'] = explode(',',$setup_email->fields['bcc_list']);
# Call the mail() or smtp() function to send
require_once(PATH_CORE.'email.inc.php');
$email = new CORE_email;
if ($type == 0)
$email->PHP_Mail($E);
else
$email->SMTP_Mail($E);
} else {
# Error message:
$C_debug->alert($C_translate->translate('error_req_fields','staff',''));
$C_vars->strip_slashes_all();
return;
}
# Success message:
$C_debug->alert($C_translate->translate('mail_sent','staff',''));
# Stripslashes
$C_vars->strip_slashes_all();
}
}
?>