From 86e6e239e70dd903e6516da2df5ba2b1a52eb7cf Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 20 Jul 2015 11:07:25 -0500
Subject: [PATCH] list: Adjust workflow for new items

* Make the "Items" tab the default
* Drop the "Showing ..." text and add a count to the items tab
* Redirect to "Items" tab when creating a new list
* Add a new list item by ajax rather than refresh
---
 include/ajax.forms.php                      | 22 +++++++++++------
 include/staff/dynamic-list.inc.php          | 26 +++++++++++++--------
 include/staff/templates/list-items.tmpl.php | 20 +++++++---------
 scp/lists.php                               |  6 +++++
 4 files changed, 45 insertions(+), 29 deletions(-)

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index 0e440573c..e0980efe0 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -183,6 +183,16 @@ class DynamicFormsAjaxAPI extends AjaxController {
             $item->save();
         }
 
+        Http::response(201, $this->encode(array(
+            'id' => $item->getId(),
+            'row' => $this->_renderListItem($item, $list),
+            'success' => true,
+        )));
+    }
+
+    function _renderListItem($item, $list=false) {
+        $list = $list ?: $item->list;
+
         // Send the whole row back
         $prop_fields = array();
         foreach ($list->getConfigurationForm()->getFields() as $f) {
@@ -198,12 +208,7 @@ class DynamicFormsAjaxAPI extends AjaxController {
         ob_start();
         $item->_config = null;
         include STAFFINC_DIR . 'templates/list-item-row.tmpl.php';
-        $html = ob_get_clean();
-        Http::response(201, $this->encode(array(
-            'id' => $item->getId(),
-            'row' => $html,
-            'success' => true,
-        )));
+        return ob_get_clean();
     }
 
     function searchListItems($list_id) {
@@ -259,7 +264,10 @@ class DynamicFormsAjaxAPI extends AjaxController {
             $data['list_id'] = $list->getId();
             $item = DynamicListItem::create($data);
             if ($item->save() && $item->setConfiguration())
-                Http::response(201, $this->encode(array('success' => true)));
+                Http::response(201, $this->encode(array(
+                    'success' => true,
+                    'row' => $this->_renderListItem($item, $list)
+                )));
         }
 
         include(STAFFINC_DIR . 'templates/list-item-properties.tmpl.php');
diff --git a/include/staff/dynamic-list.inc.php b/include/staff/dynamic-list.inc.php
index 3755f2e38..7d52ad1c1 100644
--- a/include/staff/dynamic-list.inc.php
+++ b/include/staff/dynamic-list.inc.php
@@ -28,15 +28,17 @@ $info=Format::htmlchars(($errors && $_POST) ? array_merge($info,$_POST) : $info)
     <?php echo $list ? $list->getName() : __('Add new list'); ?></h2>
 
 <ul class="clean tabs" id="list-tabs">
-    <li class="active"><a href="#definition">
+    <li <?php if (!$list) echo 'class="active"'; ?>><a href="#definition">
         <i class="icon-plus"></i> <?php echo __('Definition'); ?></a></li>
-    <li><a href="#items">
-        <i class="icon-list"></i> <?php echo __('Items'); ?></a></li>
+<?php if ($list) { ?>
+    <li class="active"><a href="#items">
+        <i class="icon-list"></i> <?php echo sprintf(__('Items (%d)'), $list->items->count()); ?></a></li>
+<?php } ?>
     <li><a href="#properties">
         <i class="icon-asterisk"></i> <?php echo __('Properties'); ?></a></li>
 </ul>
 <div id="list-tabs_container">
-<div id="definition" class="tab_content">
+<div id="definition" class="tab_content <?php if ($list) echo 'hidden'; ?>">
     <table class="form_table" width="940" border="0" cellspacing="0" cellpadding="2">
     <thead>
         <tr>
@@ -215,11 +217,15 @@ $info=Format::htmlchars(($errors && $_POST) ? array_merge($info,$_POST) : $info)
     </tbody>
 </table>
 </div>
-<div id="items" class="hidden tab_content">
+
+<?php if ($list) { ?>
+<div id="items" class="tab_content">
 <?php
     $pjax_container = '#items';
     include STAFFINC_DIR . 'templates/list-items.tmpl.php'; ?>
 </div>
+<?php } ?>
+
 <p class="centered">
     <input type="submit" name="submit" value="<?php echo $submit_text; ?>">
     <input type="reset"  name="reset"  value="<?php echo __('Reset'); ?>">
@@ -237,11 +243,11 @@ $(function() {
         $.dialog(url, [201], function (xhr, resp) {
           var json = $.parseJSON(resp);
           if (json && json.success) {
-            if (json.id && json.row) {
-              $('#list-item-' + json.id).replaceWith(json.row);
-            }
-            else {
-              $.pjax.reload('#pjax-container');
+            if (json.row) {
+              if (json.id)
+                $('#list-item-' + json.id).replaceWith(json.row);
+              else
+                $('#list-items').append(json.row);
             }
           }
         });
diff --git a/include/staff/templates/list-items.tmpl.php b/include/staff/templates/list-items.tmpl.php
index a2dbb1630..6f4de9991 100644
--- a/include/staff/templates/list-items.tmpl.php
+++ b/include/staff/templates/list-items.tmpl.php
@@ -6,23 +6,19 @@
         if ($list->getSortMode() == 'SortCol')
             $pageNav->setSlack(1);
         $pageNav->setURL('lists.php?id='.$list->getId().'&a=items');
-        $showing=$pageNav->showing().' '.__('list items');
-        ?>
-    <?php }
-        else $showing = __('Add a few initial items to the list');
+    }
     ?>
     <div style="margin: 5px 0">
+    <?php if ($list) { ?>
     <div class="pull-left">
         <input type="text" placeholder="<?php echo __('Search items'); ?>"
             data-url="ajax.php/list/<?php echo $list->getId(); ?>/items/search"
             size="25" id="items-search" value="<?php
             echo Format::htmlchars($_POST['search']); ?>"/>
     </div>
-    <?php if ($list) { ?>
     <div class="pull-right">
-        <em style="display:inline-block; padding-bottom: 3px;"><?php echo $showing; ?></em>
         <?php if ($list->allowAdd()) { ?>
-        <a class="action-button field-config"
+        <a class="green button action-button field-config"
             href="#list/<?php
             echo $list->getId(); ?>/item/add">
             <i class="icon-plus-sign"></i>
@@ -41,15 +37,15 @@
         </span>
         <div id="action-dropdown-more" class="action-dropdown anchor-right">
             <ul>
-                <li><a class="items-action" href="#list/<?php echo $list->getId(); ?>/delete">
-                    <i class="icon-trash icon-fixed-width"></i>
-                    <?php echo __('Delete'); ?></a></li>
                 <li><a class="items-action" href="#list/<?php echo $list->getId(); ?>/disable">
                     <i class="icon-ban-circle icon-fixed-width"></i>
                     <?php echo __('Disable'); ?></a></li>
                 <li><a class="items-action" href="#list/<?php echo $list->getId(); ?>/enable">
                     <i class="icon-ok-sign icon-fixed-width"></i>
                     <?php echo __('Enable'); ?></a></li>
+                <li class="danger"><a class="items-action" href="#list/<?php echo $list->getId(); ?>/delete">
+                    <i class="icon-trash icon-fixed-width"></i>
+                    <?php echo __('Delete'); ?></a></li>
             </ul>
         </div>
     </div>
@@ -78,7 +74,7 @@ if ($list) {
     <table class="form_table fixed" width="940" border="0" cellspacing="0" cellpadding="2">
     <thead>
         <tr>
-            <th width="24" nowrap></th>
+            <th width="28" nowrap></th>
             <th><?php echo __('Value'); ?></th>
 <?php foreach ($prop_fields as $F) { ?>
             <th><?php echo $F->getLocal('label'); ?></th>
@@ -86,7 +82,7 @@ if ($list) {
         </tr>
     </thead>
 
-    <tbody <?php if (!isset($_POST['search']) && $list && $list->get('sort_mode') == 'SortCol') { ?>
+    <tbody id="list-items" <?php if (!isset($_POST['search']) && $list && $list->get('sort_mode') == 'SortCol') { ?>
             class="sortable-rows" data-sort="sort-"<?php } ?>>
         <?php
         if ($list) {
diff --git a/scp/lists.php b/scp/lists.php
index 89173a88e..03a6daaae 100644
--- a/scp/lists.php
+++ b/scp/lists.php
@@ -5,6 +5,7 @@ require_once(INCLUDE_DIR.'class.list.php');
 
 $list=null;
 $criteria=array();
+$redirect = false;
 if ($_REQUEST['id'])
     $criteria['id'] = $_REQUEST['id'];
 elseif ($_REQUEST['type'])
@@ -98,6 +99,8 @@ if($_POST) {
                  $form = $list->getForm(true);
                  $msg = sprintf(__('Successfully added %s'),
                     __('this custom list'));
+                 // Redirect to list page
+                 $redirect = "lists.php?id={$list->id}#items";
             } elseif ($errors) {
                 $errors['err']=sprintf(__('Unable to add %s. Correct error(s) below and try again.'),
                     __('this custom list'));
@@ -173,6 +176,9 @@ if($_POST) {
     }
 }
 
+if ($redirect)
+    Http::redirect($redirect);
+
 $page='dynamic-lists.inc.php';
 if($list && !strcasecmp(@$_REQUEST['a'],'items') && isset($_SERVER['HTTP_X_PJAX'])) {
     $page='templates/list-items.tmpl.php';
-- 
GitLab