From 642dc711ac12b36a70e2ad94550e2277ca96ba0e Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Wed, 7 Jun 2017 15:59:27 -0500
Subject: [PATCH] functionality to export a Department's members and their
 roles

---
 include/class.dept.php           |  9 ++++++++
 include/class.export.php         | 36 +++++++++++++++++++++++++++++++-
 include/staff/department.inc.php | 12 ++++++++++-
 scp/departments.php              |  5 ++++-
 4 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/include/class.dept.php b/include/class.dept.php
index 8f09237c4..152fa7cf6 100644
--- a/include/class.dept.php
+++ b/include/class.dept.php
@@ -473,6 +473,15 @@ implements TemplateVariable {
             $this->flags &= ~$flag;
     }
 
+    function export($dept, $criteria=null, $filename='') {
+        include_once(INCLUDE_DIR.'class.error.php');
+        $members = $dept->getMembers();
+
+        //Sort based on name formating
+        $members = Staff::nsort($members);
+        Export::departmentMembers($dept, $members, $filename);
+    }
+
     /*----Static functions-------*/
     static function getIdByName($name, $pid=null) {
         $row = static::objects()
diff --git a/include/class.export.php b/include/class.export.php
index f9613eeba..1233a16d3 100644
--- a/include/class.export.php
+++ b/include/class.export.php
@@ -258,7 +258,7 @@ class Export {
         return false;
     }
 
-    static function  agents($agents, $filename='', $how='csv') {
+    static function agents($agents, $filename='', $how='csv') {
 
         // Filename or stream to export agents to
         $filename = $filename ?: sprintf('Agents-%s.csv',
@@ -284,6 +284,40 @@ class Export {
         exit;
 
     }
+
+static function departmentMembers($dept, $agents, $filename='', $how='csv') {
+    $primaryMembers = array();
+    foreach ($dept->getPrimaryMembers() as $agent) {
+      $primaryMembers[] = $agent->getId();
+    }
+
+    // Filename or stream to export depts' agents to
+    $filename = $filename ?: sprintf('%s-%s.csv', $dept->getName(),
+            strftime('%Y%m%d'));
+    Http::download($filename, "text/$how");
+    echo self::dumpQuery($agents, array(
+                '::getName'  =>  'Name',
+                '::getUsername' => 'Username',
+                2 => 'Access Type',
+                3 => 'Access Role',
+              ),
+            $how,
+            array('modify' => function(&$record, $keys, $obj) use ($dept, $primaries, $primaryMembers) {
+                $role = $obj->getRole($dept);
+
+                if (array_search($obj->getId(), $primaryMembers, true) === false)
+                  $type = 'Extended';
+                else {
+                  $type = 'Primary';
+                }
+
+                $record[2] = $type;
+                $record[3] = $role->name;
+                return $record;
+                })
+            );
+    exit;
+  }
 }
 
 class ResultSetExporter {
diff --git a/include/staff/department.inc.php b/include/staff/department.inc.php
index 2ea8825e3..4c8483dfc 100644
--- a/include/staff/department.inc.php
+++ b/include/staff/department.inc.php
@@ -325,7 +325,17 @@ $info = Format::htmlchars(($errors && $_POST) ? $_POST : $info);
                 <?php echo sprintf(__('Agents who are primary members of %s'), __('this department')); ?>
                 </small></div>
             </td>
-        </tr>
+            <td>
+              <?php
+                echo sprintf(
+                    '<a class="no-pjax" href="departments.php?id=%d&a=export"</a>',
+                    $dept->getId());
+                    ?>
+              <span class="action-button pull-right" data-placement="bottom" data-toggle="tooltip" title="<?php echo __('Export');?>">
+                  <i class="icon-download-alt icon-fixed-width"></i>
+              </span>
+            </td>
+          </tr>
         <?php
         if (!count($dept->members)) { ?>
         <tr><td colspan=2><em><?php
diff --git a/scp/departments.php b/scp/departments.php
index c1d860488..6e0742199 100644
--- a/scp/departments.php
+++ b/scp/departments.php
@@ -130,7 +130,10 @@ if($_POST){
 
 $page='departments.inc.php';
 $tip_namespace = 'staff.department';
-if($dept || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add'))) {
+if ($_REQUEST['a'] && $_REQUEST['a'] == 'export') {
+  if (!Dept::export($dept))
+      $errors['err'] = sprintf(__('Unable to export %s.'), __('Department'));
+} elseif ($dept || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add'))) {
     $page='department.inc.php';
 }
 
-- 
GitLab