diff --git a/bootstrap.php b/bootstrap.php
index ff09b15b66be4a7433d2f46b4dd8c07431eabe46..794613840f288b9f3dde80176777593f5c4243c2 100644
--- a/bootstrap.php
+++ b/bootstrap.php
@@ -138,6 +138,7 @@ class Bootstrap {
         define('QUEUE_COLUMN_TABLE', $prefix.'queue_columns');
         define('QUEUE_SORT_TABLE', $prefix.'queue_sort');
         define('QUEUE_SORTING_TABLE', $prefix.'queue_sorts');
+        define('QUEUE_EXPORT_TABLE', $prefix.'queue_export');
 
         define('API_KEY_TABLE',$prefix.'api_key');
         define('TIMEZONE_TABLE',$prefix.'timezone');
diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index e999dcd686b8d13dfb27f8f71cdef0a577f8f4b6..9e8bce892bc6230dff7c9cfb920f2d26198e3ca4 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -515,7 +515,7 @@ function refer($tid, $target=null) {
               ':title' => sprintf(__('Ticket #%s: %s %s'),
                   $ticket->getNumber(),
                   __('Update'),
-                  $field->getlabel()
+                  $field->getLabel()
                   ),
               ':action' => sprintf('#tickets/%d/field/%s/edit',
                   $ticket->getId(), $field->getId())
diff --git a/include/class.export.php b/include/class.export.php
index 21e89bf809212a978ec92b83de4a5f9beb647c48..6b7b4be5d54319e6f886046540f0b1daf31f4070 100644
--- a/include/class.export.php
+++ b/include/class.export.php
@@ -40,7 +40,7 @@ class Export {
     #      SQL is exported, but for something like tickets, we will need to
     #      export attached messages, reponses, and notes, as well as
     #      attachments associated with each, ...
-    static function dumpTickets($sql, $how='csv') {
+    static function dumpTickets($sql, $target=array(), $how='csv') {
         // Add custom fields to the $sql statement
         $cdata = $fields = array();
         foreach (TicketForm::getInstance()->getFields() as $f) {
@@ -81,26 +81,7 @@ class Export {
             $S->get('junk');
 
         return self::dumpQuery($tickets,
-            array(
-                'number' =>         __('Ticket Number'),
-                'created' =>        __('Date Created'),
-                'cdata.subject' =>  __('Subject'),
-                'user.name' =>      __('From'),
-                'user.default_email.address' => __('From Email'),
-                'cdata.:priority.priority_desc' => __('Priority'),
-                'dept::getLocalName' => __('Department'),
-                'topic::getName' => __('Help Topic'),
-                'source' =>         __('Source'),
-                'status::getName' =>__('Current Status'),
-                'lastupdate' =>     __('Last Updated'),
-                'est_duedate' =>    __('Due Date'),
-                'isoverdue' =>      __('Overdue'),
-                'isanswered' =>     __('Answered'),
-                'staff::getName' => __('Agent Assigned'),
-                'team::getName' =>  __('Team Assigned'),
-                'thread_count' =>   __('Thread Count'),
-                'attachment_count' => __('Attachment Count'),
-            ) + $cdata,
+            $target,
             $how,
             array('modify' => function(&$record, $keys) use ($fields) {
                 foreach ($fields as $k=>$f) {
@@ -113,9 +94,9 @@ class Export {
             );
     }
 
-    static  function saveTickets($sql, $filename, $how='csv') {
+    static  function saveTickets($sql, $fields, $filename, $how='csv') {
         Http::download($filename, "text/$how");
-        self::dumpTickets($sql, $how);
+        self::dumpTickets($sql, $fields, $how);
         exit;
     }
 
diff --git a/include/class.queue.php b/include/class.queue.php
index 675e32d2d54de7a448f3238999312387aec8ab0d..5c80573686a4a969820cb902ae1e2fa34c2a6b2e 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -38,6 +38,9 @@ class CustomQueue extends VerySimpleModel {
                 'constraint' => array('sort_id' => 'QueueSort.id'),
                 'null' => true,
             ),
+            'exports' => array(
+                'reverse' => 'QueueExport.queue',
+            ),
             'parent' => array(
                 'constraint' => array(
                     'parent_id' => 'CustomQueue.id',
@@ -59,8 +62,10 @@ class CustomQueue extends VerySimpleModel {
     const FLAG_INHERIT_COLUMNS =  0x0010; // Inherit column layout from parent
     const FLAG_INHERIT_SORTING =  0x0020; // Inherit advanced sorting from parent
     const FLAG_INHERIT_DEF_SORT = 0x0040; // Inherit default selected sort
+    const FLAG_INHERIT_EXPORT  =  0x0080; // Inherit export fields from parent
+
 
-    const FLAG_INHERIT_EVERYTHING = 0x78; // Maskf or all INHERIT flags
+    const FLAG_INHERIT_EVERYTHING = 0x158; // Maskf or all INHERIT flags
 
     var $criteria;
     var $_conditions;
@@ -75,6 +80,9 @@ class CustomQueue extends VerySimpleModel {
         // Ensure valid state
         if ($this->hasFlag(self::FLAG_INHERIT_COLUMNS) && !$this->parent_id)
             $this->clearFlag(self::FLAG_INHERIT_COLUMNS);
+
+       if ($this->hasFlag(self::FLAG_INHERIT_EXPORT) && !$this->parent_id)
+            $this->clearFlag(self::FLAG_INHERIT_EXPORT);
     }
 
     function getId() {
@@ -502,6 +510,69 @@ class CustomQueue extends VerySimpleModel {
         return $this->_conditions;
     }
 
+    function getExportableFields() {
+        $cdata = $fields = array();
+        foreach (TicketForm::getInstance()->getFields() as $f) {
+            // Ignore core fields
+            if (in_array($f->get('name'), array('priority')))
+                continue;
+            // Ignore non-data fields
+            elseif (!$f->hasData() || $f->isPresentationOnly())
+                continue;
+
+            $name = $f->get('name') ?: 'field_'.$f->get('id');
+            $key = 'cdata.'.$name;
+            $cdata[$key] = $f->getLocal('label');
+        }
+
+        // Standard export fields if none is provided.
+        $fields = array(
+                'number' =>         __('Ticket Number'),
+                'created' =>        __('Date Created'),
+                'cdata.subject' =>  __('Subject'),
+                'user.name' =>      __('From'),
+                'user.default_email.address' => __('From Email'),
+                'cdata.:priority.priority_desc' => __('Priority'),
+                'dept::getLocalName' => __('Department'),
+                'topic::getName' => __('Help Topic'),
+                'source' =>         __('Source'),
+                'status::getName' =>__('Current Status'),
+                'lastupdate' =>     __('Last Updated'),
+                'est_duedate' =>    __('Due Date'),
+                'isoverdue' =>      __('Overdue'),
+                'isanswered' =>     __('Answered'),
+                'staff::getName' => __('Agent Assigned'),
+                'team::getName' =>  __('Team Assigned'),
+                'thread_count' =>   __('Thread Count'),
+                'attachment_count' => __('Attachment Count'),
+                ) + $cdata;
+
+
+        return $fields;
+    }
+
+    function getExportFields() {
+
+        $fields = array();
+        if ($this->parent_id
+            && $this->hasFlag(self::FLAG_INHERIT_EXPORT)
+            && $this->parent
+        ) {
+            $fields = $this->parent->getExportFields();
+        }
+        elseif (count($this->exports)) {
+            $fields = $this->exports;
+        }
+        elseif ($this->isAQueue())
+            return  $this->getExportableFields();
+
+        $_fields = array();
+        foreach ($fields as  $f)
+            $_fields[$f->path] = $f->getHeading();
+
+        return $_fields;
+    }
+
     function getColumns($use_template=false) {
         if ($this->columns_id
             && ($q = CustomQueue::lookup($this->columns_id))
@@ -526,11 +597,11 @@ class CustomQueue extends VerySimpleModel {
         // Last resort — use standard columns
         foreach (array(
             QueueColumn::placeholder(array(
-		"id" => 1,
+                "id" => 1,
                 "heading" => "Number",
                 "primary" => 'number',
                 "width" => 85,
-		"bits" => QueueColumn::FLAG_SORTABLE,
+                "bits" => QueueColumn::FLAG_SORTABLE,
                 "filter" => "link:ticketP",
                 "annotations" => '[{"c":"TicketSourceDecoration","p":"b"}]',
                 "conditions" => '[{"crit":["isanswered","set",null],"prop":{"font-weight":"bold"}}]',
@@ -540,14 +611,14 @@ class CustomQueue extends VerySimpleModel {
                 "heading" => "Created",
                 "primary" => 'created',
                 "width" => 100,
-		"bits" => QueueColumn::FLAG_SORTABLE,
+                "bits" => QueueColumn::FLAG_SORTABLE,
             )),
             QueueColumn::placeholder(array(
                 "id" => 3,
                 "heading" => "Subject",
                 "primary" => 'cdata__subject',
                 "width" => 250,
-		"bits" => QueueColumn::FLAG_SORTABLE,
+                "bits" => QueueColumn::FLAG_SORTABLE,
                 "filter" => "link:ticket",
                 "annotations" => '[{"c":"TicketThreadCount","p":">"},{"c":"ThreadAttachmentCount","p":"a"},{"c":"OverdueFlagDecoration","p":"<"}]',
                 "truncate" => 'ellipsis',
@@ -637,6 +708,17 @@ class CustomQueue extends VerySimpleModel {
         ));
     }
 
+    function export($filename, $format) {
+
+        if (!($query=$this->getBasicQuery()))
+            return false;
+
+        if (!($fields=$this->getFields()))
+            return false;
+
+        return Export::saveTickets($query, $fields, $filename, $format);
+    }
+
     /**
      * Add critiera to a query based on the constraints configured for this
      * queue. The criteria of the parent queue is also automatically added
@@ -774,6 +856,11 @@ class CustomQueue extends VerySimpleModel {
         return $this->hasFlag(self::FLAG_INHERIT_COLUMNS);
     }
 
+    function inheritExport() {
+        return ($this->hasFlag(self::FLAG_INHERIT_EXPORT) ||
+                !count($this->exports));
+    }
+
     function inheritSorting() {
         return $this->hasFlag(self::FLAG_INHERIT_SORTING);
     }
@@ -865,6 +952,8 @@ class CustomQueue extends VerySimpleModel {
             $this->parent_id > 0 && isset($vars['inherit']));
         $this->setFlag(self::FLAG_INHERIT_COLUMNS,
             $this->parent_id > 0 && isset($vars['inherit-columns']));
+        $this->setFlag(self::FLAG_INHERIT_EXPORT,
+            $this->parent_id > 0 && isset($vars['inherit-fields']));
         $this->setFlag(self::FLAG_INHERIT_SORTING,
             $this->parent_id > 0 && isset($vars['inherit-sorting']));
 
@@ -906,6 +995,34 @@ class CustomQueue extends VerySimpleModel {
             $this->columns->sort(function($c) { return $c->sort; });
         }
 
+        // Update export fields for the queue
+        if (isset($vars['exports']) &&
+                 !$this->hasFlag(self::FLAG_INHERIT_EXPORT)) {
+            $new = $vars['exports'];
+            $order = array_keys($new);
+            foreach ($this->exports as $f) {
+                $key = $f->getPath();
+                if (!isset($vars['exports'][$key])) {
+                    $this->exports->remove($f);
+                    continue;
+                }
+                $info = $vars['exports'][$key];
+                $f->set('heading', $info['heading']);
+                $f->set('sort',array_search($key, $order));
+                unset($new[$key]);
+            }
+
+            foreach($new as $k => $field) {
+                $f = QueueExport::create(array(
+                            //'queue_id' => $this->getId(),
+                            'path' => $k,
+                            'heading' => $field['heading'],
+                            'sort' => array_search($k, $order)));
+                $this->exports->add($f);
+            }
+            $this->exports->sort(function($f) { return $f->sort; });
+        }
+
         // Update advanced sorting options for the queue
         if (isset($vars['sorts']) && !$this->hasFlag(self::FLAG_INHERIT_SORTING)) {
             $new = $order = $vars['sorts'];
@@ -1002,6 +1119,7 @@ class CustomQueue extends VerySimpleModel {
             $move_children($this);
         }
         return $this->columns->saveAll()
+            && $this->exports->saveAll()
             && $this->sorts->saveAll();
     }
 
@@ -1971,6 +2089,39 @@ extends VerySimpleModel {
     }
 }
 
+class QueueExport
+extends VerySimpleModel {
+    static $meta = array(
+        'table' => QUEUE_EXPORT_TABLE,
+        'pk' => array('id'),
+        'joins' => array(
+            'queue' => array(
+                'constraint' => array('queue_id' => 'CustomQueue.id'),
+            ),
+        ),
+        'select_related' => array('queue'),
+        'ordering' => array('sort'),
+    );
+
+
+    function getPath() {
+        return $this->path;
+    }
+
+    function getField() {
+        return $this->getPath();
+    }
+
+    function getHeading() {
+        return $this->heading;
+    }
+
+    static function create($vars=false) {
+        $inst = new static($vars);
+        return $inst;
+    }
+}
+
 class QueueColumnGlue
 extends VerySimpleModel {
     static $meta = array(
diff --git a/include/staff/queue.inc.php b/include/staff/queue.inc.php
index ea01dd55871c6512643a557b789ab9b367f6f39b..bf41bd1a48b42204c95fbec0b7fda987229c032a 100644
--- a/include/staff/queue.inc.php
+++ b/include/staff/queue.inc.php
@@ -47,6 +47,8 @@ else {
       <?php echo __('Sort'); ?></a></li>
     <li><a href="#conditions-tab"><i class="icon-exclamation-sign"></i>
       <?php echo __('Conditions'); ?></a></li>
+    <li><a href="#export-columns"><i class="icon-download"></i>
+      <?php echo __('Export'); ?></a></li>
     <li><a href="#preview-tab"><i class="icon-eye-open"></i>
       <?php echo __('Preview'); ?></a></li>
   </ul>
@@ -115,7 +117,7 @@ else {
               echo 'selected="selected"'; ?>>— <?php echo __('None'); ?> —</option>
           <option value="::" <?php if ($queue->filter == "::")
               echo 'selected="selected"'; ?>>— <?php echo __('Inherit from parent');
-            if ($queue->parent 
+            if ($queue->parent
                 && ($qf = $queue->parent->getQuickFilterField()))
                 echo sprintf(' (%s)', $qf->getLabel()); ?> —</option>
 <?php foreach (CustomQueue::getSearchableFields('Ticket') as $path=>$f) {
@@ -139,7 +141,7 @@ else {
               echo 'selected="selected"'; ?>>— <?php echo __('None'); ?> —</option>
           <option value="::" <?php if ($queue->isDefaultSortInherited())
               echo 'selected="selected"'; ?>>— <?php echo __('Inherit from parent');
-            if ($queue->parent 
+            if ($queue->parent
                 && ($sort = $queue->parent->getDefaultSort()))
                 echo sprintf(' (%s)', $sort->getName()); ?> —</option>
 <?php foreach ($queue->getSortOptions() as $sort) { ?>
@@ -165,8 +167,17 @@ else {
     </div>
     <?php include STAFFINC_DIR . "templates/queue-columns.tmpl.php"; ?>
   </div>
-    
-    
+
+  <div class="hidden tab_content" id="export-columns">
+    <div>
+      <h3 class="title"><?php echo __("Manage Export fields this queue"); ?>
+        <div class="sub-title"><?php echo __(
+            'Add, and remove the fields in this list using the options below. Drag fields to reorder them.');
+            ?></div>
+      </h3>
+    </div>
+    <?php include STAFFINC_DIR . "templates/queue-fields.tmpl.php"; ?>
+  </div>
   <div class="hidden tab_content" id="sorting-tab">
     <h3 class="title"><?php echo __("Manage Queue Sorting"); ?>
       <div class="sub-title"><?php echo __(
diff --git a/include/staff/templates/advanced-search.tmpl.php b/include/staff/templates/advanced-search.tmpl.php
index 577f66962305b047b6165a66b3a41ebb8ad91106..c2cd1c1d442a833807b9adbcf6aeb8c4d4c36d0a 100644
--- a/include/staff/templates/advanced-search.tmpl.php
+++ b/include/staff/templates/advanced-search.tmpl.php
@@ -11,9 +11,11 @@ foreach (CustomQueue::queues() as  $q)
     $queues[$q->id] = $q->getFullName();
 asort($queues);
 $queues = array(0 => ('—'.__("My Searches").'—')) + $queues;
+$queue = $search;
+$qname = $search->getName() ?:  __('Advanced Ticket Search');
 ?>
 <div id="advanced-search" class="advanced-search">
-<h3 class="drag-handle"><?php echo __('Advanced Ticket Search');?></h3>
+<h3 class="drag-handle"><?php echo Format::htmlchars($qname); ?></h3>
 <a class="close" href=""><i class="icon-remove-circle"></i></a>
 <hr/>
 
@@ -41,9 +43,10 @@ foreach ($queues as $id => $name) {
       <div class="error"><?php echo Format::htmlchars($errors['name']); ?></div>
     </div>
   </div>
-<ul class="tabs">
-    <li class="active"><a href="#criteria"><?php echo __('Criteria'); ?></a></li>
-    <li><a href="#columns"><?php echo __('Columns'); ?></a></li>
+<ul class="clean tabs">
+    <li class="active"><a href="#criteria"><i class="icon-search"></i> <?php echo __('Criteria'); ?></a></li>
+    <li><a href="#columns"><i class="icon-columns"></i> <?php echo __('Columns'); ?></a></li>
+    <li><a href="#fields"><i class="icon-download"></i> <?php echo __('Export'); ?></a></li>
 </ul>
 
 <div class="tab_content" id="criteria">
@@ -68,10 +71,13 @@ foreach ($queues as $id => $name) {
 
 <div class="tab_content hidden" id="columns">
     <?php
-    $queue = $search;
-    include STAFFINC_DIR . "templates/queue-columns.tmpl.php"; ?>
+    include STAFFINC_DIR . "templates/queue-columns.tmpl.php";
+    ?>
+</div>
+<div class="tab_content hidden" id="fields">
+    <?php
+    include STAFFINC_DIR . "templates/queue-fields.tmpl.php";  ?>
 </div>
-
   <hr/>
   <div>
     <div class="buttons pull-right">
diff --git a/include/staff/templates/queue-fields.tmpl.php b/include/staff/templates/queue-fields.tmpl.php
new file mode 100644
index 0000000000000000000000000000000000000000..be207a07c7fe9dd5f54a0be06256c6779ed07b95
--- /dev/null
+++ b/include/staff/templates/queue-fields.tmpl.php
@@ -0,0 +1,149 @@
+<table class="table">
+<?php
+if ($queue->parent) { ?>
+  <tbody>
+    <tr>
+      <td colspan="3">
+        <input type="checkbox" name="inherit-fields" <?php
+          if ($queue->inheritExport()) echo 'checked="checked"'; ?>
+          onchange="javascript:$(this).closest('table').find('.if-not-inherited').toggle(!$(this).prop('checked'));" />
+        <?php echo __('Inherit export fields from the parent queue'); ?>
+        <br /><br />
+      </td>
+    </tr>
+  </tbody>
+<?php }
+      // Adhoc Advanced search does not have customizable export, but saved
+      // ones do
+      elseif ($queue->__new__) { ?>
+  <tbody>
+    <tr>
+      <td colspan="3">
+        <input type="checkbox" name="inherit-fields" <?php
+          if (count($queue->exports) == 0) echo 'checked="checked"';
+          if ($queue instanceof SavedSearch) echo 'disabled="disabled"'; ?>
+          onchange="javascript:$(this).closest('table').find('.if-not-inherited').toggle(!$(this).prop('checked'));" />
+        <?php echo __('Use standard export fields'); ?>
+        <br /><br />
+      </td>
+    </tr>
+  </tbody>
+<?php }
+$hidden_cols = $queue->inheritExport();
+?>
+  <tbody class="if-not-inherited <?php if ($hidden_cols) echo 'hidden'; ?>">
+    <tr class="header">
+      <td nowrap><small><b><?php echo __('Heading'); ?></b></small></td>
+      <td><small><b><?php echo __('Field'); ?></b></small></td>
+      <td><small>&nbsp;</small></td>
+    </tr>
+  </tbody>
+  <tbody class="sortable-rows if-not-inherited <?php if ($hidden_cols) echo 'hidden'; ?>">
+    <tr id="field-template" class="hidden field-entry">
+      <td nowrap>
+        <i class="faded-more icon-sort"></i>
+        <input type="hidden" data-name="name" />
+        <input type="text" size="25" data-name="heading"
+          data-translate-tag="" />
+      </td>
+      <td><span>Field</span></td>
+      <td>
+        <a href="#" class="pull-right drop-field" title="<?php echo __('Delete');
+          ?>"><i class="icon-trash"></i></a>
+      </td>
+    </tr>
+  </tbody>
+  <tbody id="fields" class="if-not-inherited  <?php if ($hidden_cols) echo 'hidden'; ?>">
+    <tr class="header">
+        <td colspan="3"></td>
+    </tr>
+    <tr>
+    <td colspan="3" id="append-field">
+    <i class="icon-plus-sign"></i>
+    <select id="add-field" name="new-field" style="max-width: 300px;">
+        <option value="">— <?php echo __('Add Other Field'); ?> —</option>
+    <?php
+    $fields = $queue->getExportableFields();
+    if (is_array($fields)) {
+    foreach ($fields as $path => $label) { ?>
+        <option value="<?php echo $path; ?>" <?php
+            if (isset($state[$path])) echo 'disabled="disabled"';
+            ?>><?php echo $label; ?></option>
+    <?php }
+    } ?>
+    </select>
+    <button type="button" class="green button">
+          <?php echo __('Add'); ?>
+    </button>
+    </td></tr>
+  </tbody>
+</table>
+<script>
++function() {
+var Q = setInterval(function() {
+  if ($('#append-field').length == 0)
+    return;
+  clearInterval(Q);
+
+  var addField = function(field, info) {
+    console.log(field, info);
+    if (!field) return;
+
+    var i  = $('#fields tr.field-entry').length;
+    var copy = $('#field-template').clone(),
+        name_prefix = 'exports['+ field +']';
+
+    copy.find('input[data-name]').each(function() {
+      var $this = $(this),
+          name = $this.data('name');
+
+      if (info[name] !== undefined) {
+        $this.val(info[name]);
+      }
+      $this.attr('name', name_prefix + '[' + name + ']');
+    });
+    copy.find('span').text(info['name']);
+    copy.attr('id', '').show().insertBefore($('#field-template'));
+    copy.removeClass('hidden');
+    if (info['trans'] !== undefined) {
+      var input = copy.find('input[data-translate-tag]')
+        .attr('data-translate-tag', info['trans']);
+      if ($.fn.translatable)
+        input.translatable();
+      // Else it will be made translatable when the JS library is loaded
+    }
+    copy.find('a.drop-field').click(function() {
+      $('<option>')
+        .attr('value', copy.find('input[data-name=field]').val())
+        .text(info.name)
+        .insertBefore($('#add-field')
+          .find('[data-quick-add]')
+        );
+      copy.fadeOut(function() { $(this).remove(); });
+      return false;
+    });
+
+    var selected = $('#add-field').find("option[value='" + escape(field) +
+            "']");
+    selected.remove();
+  };
+
+  $('#append-field').find('button').on('click', function() {
+    var selected = $('#add-field').find(':selected'),
+        field = selected.val();
+    if (!field)
+        return;
+    addField(field, {name: selected.text(), heading: selected.text()});
+    return false;
+  });
+<?php
+    foreach ($queue->getExportFields() as $k => $v) {
+    echo sprintf('addField(%s, {name: %s, heading: %s});',
+    JsonDataEncoder::encode($k),
+    JsonDataEncoder::encode($v),
+    JsonDataEncoder::encode($v));
+  }
+?>
+}, 25);
+}();
+</script>
diff --git a/include/upgrader/streams/core.sig b/include/upgrader/streams/core.sig
index dd4c44655d02dbfd7cb3b534a82fcc77a3e98751..e791d7b4a64d84a4f7c28ead42d9b5e37d04fc9c 100644
--- a/include/upgrader/streams/core.sig
+++ b/include/upgrader/streams/core.sig
@@ -1 +1 @@
-86707325fc571e56242fccc46fd24466
+526c601bc1748febb192f854699f170a
diff --git a/include/upgrader/streams/core/86707325-526c601b.patch.sql b/include/upgrader/streams/core/86707325-526c601b.patch.sql
new file mode 100644
index 0000000000000000000000000000000000000000..25939f92a962dc731db52623f33bf703814967e5
--- /dev/null
+++ b/include/upgrader/streams/core/86707325-526c601b.patch.sql
@@ -0,0 +1,23 @@
+/**
+ * @signature 526c601bc1748febb192f854699f170a
+ * @version v1.11.0
+ * @title Add Custom Export for Queues
+ *
+ * This patch adds a table for custom export for custom queues
+ */
+
+CREATE TABLE `%TABLE_PREFIX%queue_export` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `queue_id` int(11) unsigned NOT NULL,
+  `path` varchar(64) NOT NULL DEFAULT '',
+  `heading` varchar(64) DEFAULT NULL,
+  `sort` int(10) unsigned NOT NULL DEFAULT '1',
+  PRIMARY KEY (`id`),
+  KEY `queue_id` (`queue_id`)
+) DEFAULT CHARSET=utf8;
+
+
+ -- Finished with patch
+UPDATE `%TABLE_PREFIX%config`
+    SET `value` = '526c601bc1748febb192f854699f170a'
+    WHERE `key` = 'schema_signature' AND `namespace` = 'core';
diff --git a/scp/tickets.php b/scp/tickets.php
index 5f7002bfc9d2b84251c2921174f381aafe7c6f49..bd87497462fa0a7f4d76e844dc8df5ef0bce0ba3 100644
--- a/scp/tickets.php
+++ b/scp/tickets.php
@@ -536,18 +536,14 @@ if($ticket) {
     if ($_REQUEST['a']=='open' &&
             $thisstaff->hasPerm(Ticket::PERM_CREATE, false))
         $inc = 'ticket-open.inc.php';
-    elseif($_REQUEST['a'] == 'export') {
-        $ts = strftime('%Y%m%d');
-        if (isset($queue) && $queue) {
-            // XXX: Check staff access?
-            if (!($query = $queue->getBasicQuery()))
-                $errors['err'] = __('Query token not found');
-            elseif (!Export::saveTickets($query, "tickets-$ts.csv", 'csv'))
-                $errors['err'] = __('Unable to dump query results.')
-                    .' '.__('Internal error occurred');
-        }
-    }
-    elseif ($queue) {
+    elseif ($_REQUEST['a'] == 'export' && $queue) {
+        // XXX: Check staff access?
+        $filename = sprintf('%s Tickets-%s', $queue->getName(),
+                strftime('%Y%m%d'));
+        if (!$queue->export($filename, 'csv'))
+            $errors['err'] = __('Unable to export results.')
+                .' '.__('Internal error occurred');
+    } elseif ($queue) {
         // XXX: Check staff access?
         $quick_filter = @$_REQUEST['filter'];
         $tickets = $queue->getQuery(false, $quick_filter);
diff --git a/setup/inc/streams/core/install-mysql.sql b/setup/inc/streams/core/install-mysql.sql
index 6173192180554dd625160ec0f0a9cd62adc678b9..6d5ef523778ea663f14f6d6d94b54030e89d7e47 100644
--- a/setup/inc/streams/core/install-mysql.sql
+++ b/setup/inc/streams/core/install-mysql.sql
@@ -901,6 +901,17 @@ CREATE TABLE `%TABLE_PREFIX%queue_sorts` (
   PRIMARY KEY (`queue_id`, `sort_id`)
 ) DEFAULT CHARSET=utf8;
 
+DROP TABLE IF EXISTS `%TABLE_PREFIX%queue_export`;
+CREATE TABLE `%TABLE_PREFIX%queue_export` (
+  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+  `queue_id` int(11) unsigned NOT NULL,
+  `path` varchar(64) NOT NULL DEFAULT '',
+  `heading` varchar(64) DEFAULT NULL,
+  `sort` int(10) unsigned NOT NULL DEFAULT '1',
+  PRIMARY KEY (`id`),
+  KEY `queue_id` (`queue_id`)
+) DEFAULT CHARSET=utf8;
+
 DROP TABLE IF EXISTS `%TABLE_PREFIX%translation`;
 CREATE TABLE `%TABLE_PREFIX%translation` (
   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,