From ba7b77fec7f6a0a3a59ee91b9f99fed3c01e78b3 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Mon, 13 Jul 2015 05:28:53 +0000
Subject: [PATCH] Tasks Export

Add ability to export tasks
---
 include/class.export.php                      | 59 ++++++++++++++++++-
 include/i18n/en_US/help/tips/tasks.queue.yaml | 28 +++++++++
 scp/tasks.php                                 |  1 +
 3 files changed, 87 insertions(+), 1 deletion(-)
 create mode 100644 include/i18n/en_US/help/tips/tasks.queue.yaml

diff --git a/include/class.export.php b/include/class.export.php
index d12dd3bf4..a5e950c7f 100644
--- a/include/class.export.php
+++ b/include/class.export.php
@@ -99,7 +99,7 @@ class Export {
             );
     }
 
-    /* static */ function saveTickets($sql, $filename, $how='csv') {
+    static  function saveTickets($sql, $filename, $how='csv') {
         ob_start();
         self::dumpTickets($sql, $how);
         $stuff = ob_get_contents();
@@ -110,7 +110,64 @@ class Export {
         return false;
     }
 
+
+    static function dumpTasks($sql, $how='csv') {
+        // Add custom fields to the $sql statement
+        $cdata = $fields = array();
+        foreach (TaskForm::getInstance()->getFields() as $f) {
+            // Ignore non-data fields
+            if (!$f->hasData() || $f->isPresentationOnly())
+                continue;
+
+            $name = $f->get('name') ?: 'field_'.$f->get('id');
+            $key = 'cdata.'.$name;
+            $fields[$key] = $f;
+            $cdata[$key] = $f->getLocal('label');
+        }
+        // Reset the $sql query
+        $tasks = $sql->models()
+            ->select_related('dept', 'staff', 'team', 'cdata')
+            ->annotate(array(
+            'collab_count' => SqlAggregate::COUNT('thread__collaborators'),
+            'attachment_count' => SqlAggregate::COUNT('thread__entries__attachments'),
+            'thread_count' => SqlAggregate::COUNT('thread__entries'),
+        ));
+
+        return self::dumpQuery($tasks,
+            array(
+                'number' =>         __('Task Number'),
+                'created' =>        __('Date Created'),
+                'cdata.title' =>    __('Title'),
+                'dept::getLocalName' => __('Department'),
+                'flags' =>          __('Current Status'), //FIXME: self:getStatus()?
+                'duedate' =>        __('Due Date'),
+                'staff::getName' => __('Agent Assigned'),
+                'team::getName' =>  __('Team Assigned'),
+                'thread_count' =>   __('Thread Count'),
+                'attachment_count' => __('Attachment Count'),
+            ) + $cdata,
+            $how,
+            array('modify' => function(&$record, $keys) use ($fields) {
+                foreach ($fields as $k=>$f) {
+                    if (($i = array_search($k, $keys)) !== false) {
+                        $record[$i] = $f->export($f->to_php($record[$i]));
+                    }
+                }
+                return $record;
+            })
+            );
+    }
+
+
     static function saveTasks($sql, $filename, $how='csv') {
+
+        ob_start();
+        self::dumpTasks($sql, $how);
+        $stuff = ob_get_contents();
+        ob_end_clean();
+        if ($stuff)
+            Http::download($filename, "text/$how", $stuff);
+
         return false;
     }
 
diff --git a/include/i18n/en_US/help/tips/tasks.queue.yaml b/include/i18n/en_US/help/tips/tasks.queue.yaml
new file mode 100644
index 000000000..393447f0b
--- /dev/null
+++ b/include/i18n/en_US/help/tips/tasks.queue.yaml
@@ -0,0 +1,28 @@
+#
+# This is popup help messages for the Agents Panel -> Tasks
+#
+# Fields:
+# title - Shown in bold at the top of the popover window
+# content - The body of the help popover
+# links - List of links shows below the content
+#   title - Link title
+#   href - href of link (links starting with / are translated to the
+#       helpdesk installation path)
+#
+# The key names such as 'helpdesk_name' should not be translated as they
+# must match the HTML #ids put into the page template.
+#
+---
+advanced:
+    title: Advanced
+    content: >
+        Narrow down your search parameters. Once you have selected your advanced
+        search criteria and run the search, you can <span class="doc-desc-title">Export
+        </span> the data at the bottom of the page.
+
+export:
+    title: Export
+    content: >
+        Export your data currently in view in a CSV file.
+        CSV files may be opened with any spreadsheet software
+        (i.e., Microsoft Excel, Apple Pages, OpenOffice, etc.).
diff --git a/scp/tasks.php b/scp/tasks.php
index 756b9fd57..ced34f872 100644
--- a/scp/tasks.php
+++ b/scp/tasks.php
@@ -13,6 +13,7 @@
 
 require('staff.inc.php');
 require_once(INCLUDE_DIR.'class.task.php');
+require_once(INCLUDE_DIR.'class.export.php');
 
 $page = '';
 $task = null; //clean start.
-- 
GitLab