Skip to content
Permalink
Browse files

Export Email

This commit allows us to email audit exports to an Agent if we reach the max execution limit.
  • Loading branch information...
aydreeihn committed Aug 13, 2019
1 parent 2c350f3 commit f26128381af009d91af7ea476b7486fdb19ed7f6
Showing with 53 additions and 86 deletions.
  1. +52 −40 include/class.export.php
  2. +0 −17 scp/audits.php
  3. +0 −14 scp/staff.php
  4. +1 −3 scp/tickets.php
  5. +0 −12 scp/users.php
@@ -30,7 +30,7 @@ static function dumpQuery($sql, $headers, $how='csv', $options=array()) {
'json' => 'JsonResultsExporter'
);
$exp = new $exporters[$how]($sql, $headers, $options);
return $exp->dump();
return $exp->dump($options['tmp'] ? true : false);
}
# XXX: Think about facilitated exporting. For instance, we might have a
@@ -322,59 +322,72 @@ static function departmentMembers($dept, $agents, $filename='', $how='csv') {
exit;
}
static function audits($type, $filename='', $tableInfo='', $object='', $how='csv', $show_viewed=true) {
static function audits($type, $abbrv='', $state='', $filename='', $tableInfo='', $object='', $how='csv', $show_viewed=true) {
$headings = array('Description', 'Timestamp', 'IP');
switch ($type) {
case 'audit':
$state = $_REQUEST['state'];
$sql = AuditEntry::objects()->filter(array('object_type'=>$_REQUEST['type']));
if ($state && $state != 'All')
$sql = $sql->filter(array('state'=>$state));
$sql = AuditEntry::objects()->filter(array('object_type'=>$abbrv));
if ($state && $state != 'All') {
$eventId = Event::getIdByName(strtolower($state));
$sql = $sql->filter(array('event_id'=>$eventId));
}
if ($_REQUEST['starttime'] && $_REQUEST['endtime'])
$sql = $sql->filter(array('timestamp__range' =>
array('"'.$_REQUEST['starttime'].'"', '"'.$_REQUEST['endtime'].'"', true)));
$sql = $sql->order_by('timestamp');
$sql = $sql->order_by('-timestamp');
$tableInfo = $sql;
break;
case 'user':
$sql = AuditEntry::objects()->filter(array('user_id'=>$object->getId()))->order_by('timestamp');
$sql = AuditEntry::objects()->filter(array('user_id'=>$object))->order_by('-timestamp');
break;
case 'staff':
$sql = AuditEntry::objects()->filter(array('staff_id'=>$object->getId()))->order_by('timestamp');
$sql = AuditEntry::objects()->filter(array('staff_id'=>$object))->order_by('-timestamp');
break;
case 'ticket':
$sql = AuditEntry::objects()->filter(array('object_id'=>$object->getId(), 'object_type'=>'T'))->order_by('timestamp');
$sql = AuditEntry::objects()->filter(array('object_id'=>$object, 'object_type'=>'T'))->order_by('-timestamp');
break;
}
if (!$show_viewed)
$sql = $sql->filter(Q::not(array('event_id'=>Event::getIdByName('viewed'))))->order_by('timestamp');
//Download the file
Http::download($filename, "text/$how");
echo self::dumpQuery($sql, $headings,
$how,
array('modify' => function(&$record, $keys, $obj) use ($tableInfo) {
foreach ($tableInfo as $k => $v) {
if (is_object($v)) {
$description = AuditEntry::getDescription($v, true);
$v = $v->ht;
}
if (is_numeric($k) && ($i = in_array(
is_array($obj->ht) ? $obj->ht['id'] : $obj->id,
$v)) !== false) {
$description = $description ?: $v['description'];
$record[0] = $description;
$record[1] = $v['timestamp'];
$record[2] = $v['ip'];
}
}
return $record;
})
);
exit;
$sql = $sql->filter(Q::not(array('event_id'=>Event::getIdByName('viewed'))))->order_by('-timestamp');
// Create and store export start time
$_SESSION['export']['start'] = microtime(true);
// Store filename
$_SESSION['export']['filename'] = $filename;
// Create and store export temp name
$prefix = base64_encode(sha1(microtime(), true));
$_SESSION['export']['tempname'] = str_replace(
array('=','+','/'),
array('','-','_'),
substr($prefix, 0, 5) . $sha1);
// Create temp file to build CSV
$output = fopen(tempnam(sys_get_temp_dir(),
$_SESSION['export']['tempname']), 'w+');
// Save path in session for later
$_SESSION['export']['tempath'] = stream_get_meta_data($output)['uri'];
$delimiter = Internationalization::getCSVDelimiter();
// Output a UTF-8 BOM (byte order mark)
fputs($output, chr(0xEF) . chr(0xBB) . chr(0xBF));
fputcsv($output, $headings, $delimiter);
$row = array();
foreach ($sql as $key => $value) {
if (is_object($value)) {
$description = AuditEntry::getDescription($value, true);
$value = $value->ht;
}
$row[0] = $description;
$row[1] = $value['timestamp'];
$row[2] = $value['ip'];
fputcsv($output, $row, $delimiter);
}
// Create and store export end time
$_SESSION['export']['end'] = microtime(true);
}
}
@@ -729,12 +742,10 @@ function getDelimiter() {
return Internationalization::getCSVDelimiter();
}
function dump() {
function dump($tmp=false) {
if (!$this->output)
$this->output = fopen('php://output', 'w');
$delimiter = $this->getDelimiter();
// Output a UTF-8 BOM (byte order mark)
fputs($this->output, chr(0xEF) . chr(0xBB) . chr(0xBF));
@@ -748,7 +759,8 @@ function($v){
}, $row),
$delimiter);
fclose($this->output);
if (!$tmp)
fclose($this->output);
}
}
@@ -18,23 +18,6 @@
if (PluginManager::auditPlugin())
require_once(sprintf('phar:///%s/plugins/audit.phar/class.audit.php', INCLUDE_DIR));
switch (strtolower($_REQUEST['t'])) {
case 'audits':
if (PluginManager::auditPlugin() && $_REQUEST['a'] == 'export') {
foreach (AuditEntry::getTypes() as $abbrev => $info) {
if ($_REQUEST['type'] == $abbrev)
$name = AuditEntry::getObjectName($info[0]);
}
$show = AuditEntry::$show_view_audits;
$filename = sprintf('%s-audits-%s.csv', $name, strftime('%Y%m%d'));
if (!Export::audits('audit', $filename, '', '', 'csv', $show))
$errors['err'] = __('Unable to dump query results.')
.' '.__('Internal error occurred');
}
break;
}
$page= sprintf('phar:///%s/plugins/audit.phar/templates/auditlogs.tmpl.php', INCLUDE_DIR);
$nav->setTabActive('dashboard');
$ost->addExtraHeader('<meta name="tip-namespace" content="dashboard.audit_logs" />',
@@ -182,27 +182,13 @@
$page='staffmembers.inc.php';
$tip_namespace = 'staff.agent';
if($staff || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add'))) {
if (PluginManager::auditPlugin() && strtolower($_REQUEST['t']) == 'audits') {
require_once(sprintf('phar:///%s/plugins/audit.phar/class.audit.php', INCLUDE_DIR));
$show = AuditEntry::$show_view_audits;
$filename = sprintf('%s-audits-%s.csv',
$staff->getName(), strftime('%Y%m%d'));
$tableInfo = AuditEntry::getTableInfo($staff, true);
if (!Export::audits('staff', $filename, $tableInfo, $staff, 'csv', $show))
$errors['err'] = __('Unable to dump query results.')
.' '.__('Internal error occurred');
}
if ($staff && ($pdept=$staff->getDept()) && !$pdept->isActive())
$warn = sprintf(__('%s is assigned a %s that is not active.'), __('Agent'), __('Primary Department'));
$page='staff.inc.php';
} elseif ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'export')) {
if (!Staff::export())
$errors['err'] = sprintf(__('Unable to export %s.'), __('Agents'));
}
$nav->setTabActive('staff');
$ost->addExtraHeader('<meta name="tip-namespace" content="' . $tip_namespace . '" />',
"$('#content').data('tipNamespace', '".$tip_namespace."');");
@@ -513,9 +513,7 @@
if (!Export::audits('ticket', $filename, $tableInfo, $ticket, 'csv', $show))
$errors['err'] = __('Unable to dump query results.')
.' '.__('Internal error occurred');
} elseif($_REQUEST['a'] == 'print' && !$ticket->pdfExport($_REQUEST['psize'], $_REQUEST['notes']))
$errors['err'] = __('Unable to export the ticket to PDF for print.')
.' '.__('Internal error occurred');
}
} else {
$inc = 'templates/queue-tickets.tmpl.php';
if ($_REQUEST['a']=='open' &&
@@ -214,18 +214,6 @@
.' '.__('Internal error occurred');
}
break;
case 'audits':
if (PluginManager::auditPlugin() && $_REQUEST['a'] == 'export') {
require_once(sprintf('phar:///%s/plugins/audit.phar/class.audit.php', INCLUDE_DIR));
$show = AuditEntry::$show_view_audits;
$filename = sprintf('%s-audits-%s.csv',
$user->getName(), strftime('%Y%m%d'));
$tableInfo = AuditEntry::getTableInfo($user, true);
if (!Export::audits('user', $filename, $tableInfo, $user, 'csv', $show))
$errors['err'] = __('Unable to dump query results.')
.' '.__('Internal error occurred');
}
break;
}
}

0 comments on commit f261283

Please sign in to comment.
You can’t perform that action at this time.