From 564b05408d00568cc595cd646c29c1f3e6014d5c Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Mon, 15 May 2017 16:17:18 +0000
Subject: [PATCH] Agents Access Export

Add ability to export agents access in CSV format
---
 include/class.export.php           | 32 ++++++++++++++++++++
 include/class.staff.php            | 47 ++++++++++++++++++++----------
 include/staff/staffmembers.inc.php |  6 ++++
 scp/staff.php                      |  5 +++-
 4 files changed, 74 insertions(+), 16 deletions(-)

diff --git a/include/class.export.php b/include/class.export.php
index 97bfd8a4a..c4f5fe235 100644
--- a/include/class.export.php
+++ b/include/class.export.php
@@ -258,6 +258,38 @@ class Export {
         return false;
     }
 
+    static function  agents($agents, $filename='', $how='csv') {
+
+        // Filename or stream to export agents to
+        $filename = $filename ?: sprintf('Agents-%s.csv',
+                strftime('%Y%m%d'));
+        Http::download($filename, "text/$how");
+        $depts = Dept::getDepartments();
+        echo self::dumpQuery($agents, array(
+                    '::getName'  =>  'Name',
+                    '::getUsername' => 'Username',
+                    '::getStatus' => 'Status',
+                    'permissions' => 'Permissions',
+                    '::getDept'  => 'Primary Department',
+                    ) + $depts,
+                $how,
+                array('modify' => function(&$record, $keys, $obj) use ($depts) {
+
+                   if (($i = array_search('permissions', $keys)))
+                       $record[$i] = implode(",", array_keys($obj->getPermission()->getInfo()));
+
+                    $roles = $obj->getRoles();
+                    foreach ($depts as $k => $v) {
+                        if (is_numeric($k) && ($i = array_search($k, $keys)) !== false) {
+                            $record[$i] = $roles[$k] ?: '';
+                        }
+                    }
+                    return $record;
+                    })
+                );
+        exit;
+
+    }
 }
 
 class ResultSetExporter {
diff --git a/include/class.staff.php b/include/class.staff.php
index e5ed7e775..deccb868f 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -432,23 +432,27 @@ implements AuthenticatedUser, EmailContact, TemplateVariable {
         return isset($this->locale) ? $this->locale : 0;
     }
 
+    function getRoles() {
+        if (!isset($this->_roles)) {
+            $this->_roles = array($this->dept_id => $this->role);
+            foreach($this->dept_access as $da)
+                $this->_roles[$da->dept_id] = $da->role;
+        }
+
+        return $this->_roles;
+    }
+
     function getRole($dept=null) {
         $deptId = is_object($dept) ? $dept->getId() : $dept;
-        if ($deptId && $deptId != $this->dept_id) {
-            if (isset($this->_roles[$deptId]))
-                return $this->_roles[$deptId];
-
-            if ($access = $this->dept_access->findFirst(array('dept_id' => $deptId)))
-                return $this->_roles[$deptId] = $access->role;
+        $roles = $this->getRoles();
+        if (isset($roles[$deptId]))
+            return $roles[$deptId];
 
-            if (!$this->usePrimaryRoleOnAssignment())
-                // View only access
-                return new Role(array());
+        if ($this->usePrimaryRoleOnAssignment())
+            return $this->role;
 
-            // Fall through to primary role
-        }
-        // For the primary department, use the primary role
-        return $this->role;
+        // View only access
+        return new Role(array());
     }
 
     function hasPerm($perm, $global=true) {
@@ -477,10 +481,14 @@ implements AuthenticatedUser, EmailContact, TemplateVariable {
         return TRUE;
     }
 
-    function isactive() {
+    function isActive() {
         return $this->isactive;
     }
 
+    function getStatus() {
+        return $this->isActive() ? __('Active') : __('Locked');
+    }
+
     function isVisible() {
          return $this->isvisible;
     }
@@ -490,7 +498,7 @@ implements AuthenticatedUser, EmailContact, TemplateVariable {
     }
 
     function isAvailable() {
-        return ($this->isactive() && !$this->onVacation());
+        return ($this->isActive() && !$this->onVacation());
     }
 
     function showAssignedOnly() {
@@ -1137,6 +1145,15 @@ implements AuthenticatedUser, EmailContact, TemplateVariable {
         return true;
     }
 
+    static function export($criteria=null, $filename='') {
+        include_once(INCLUDE_DIR.'class.error.php');
+
+        $agents = Staff::objects();
+        // Sort based on name formating
+        $agents = self::nsort($agents);
+        Export::agents($agents, $filename);
+    }
+
 }
 
 interface RestrictedAccess {
diff --git a/include/staff/staffmembers.inc.php b/include/staff/staffmembers.inc.php
index 97f7c1265..9c3c76020 100644
--- a/include/staff/staffmembers.inc.php
+++ b/include/staff/staffmembers.inc.php
@@ -143,6 +143,12 @@ $agents->limit($pageNav->getLimit())->offset($pageNav->getStart());
                                 <?php echo __( 'Change Department'); ?>
                             </a>
                         </li>
+                        <li>
+                            <a class="no-pjax" href="staff.php?a=export">
+                                <i class="icon-download-alt icon-fixed-width"></i>
+                                <?php echo __( 'Export Agents'); ?>
+                            </a>
+                        </li>
                         <!-- TODO: Implement "Reset Access" mass action
                     <li><a class="dialog-first" href="#staff/reset-access">
                     <i class="icon-puzzle-piece icon-fixed-width"></i>
diff --git a/scp/staff.php b/scp/staff.php
index 9b2f4a999..c2731b9f2 100644
--- a/scp/staff.php
+++ b/scp/staff.php
@@ -171,8 +171,11 @@ if($_POST){
 
 $page='staffmembers.inc.php';
 $tip_namespace = 'staff.agent';
-if($staff || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add'))) {
+if ($staff || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add'))) {
     $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');
-- 
GitLab