From 55ac962677670f3c62d42d249763fc3a1c4eb5b9 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Thu, 26 May 2016 04:11:35 +0000
Subject: [PATCH] List Item Preview

Add a preview tool tip to show list item properties on mouse-over.
---
 include/ajax.forms.php                        | 14 ++++++++++
 include/class.dynamic_forms.php               | 22 ++++++++++++++++
 include/class.list.php                        | 20 +++++++++++++-
 .../templates/list-item-preview.tmpl.php      | 26 +++++++++++++++++++
 .../staff/templates/list-item-row.tmpl.php    |  7 ++++-
 scp/ajax.php                                  |  1 +
 6 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100644 include/staff/templates/list-item-preview.tmpl.php

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index f99870a97..520e8584a 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -128,6 +128,20 @@ class DynamicFormsAjaxAPI extends AjaxController {
         include(STAFFINC_DIR . 'templates/list-items.tmpl.php');
     }
 
+    function previewListItem($list_id, $item_id) {
+
+        $list = DynamicList::lookup($list_id);
+        if (!$list)
+            Http::response(404, 'No such list item');
+
+        $list = CustomListHandler::forList($list);
+        if (!($item = $list->getItem( (int) $item_id)))
+            Http::response(404, 'No such list item');
+
+        $form = $list->getListItemBasicForm($item->ht, $item);
+        include(STAFFINC_DIR . 'templates/list-item-preview.tmpl.php');
+    }
+
     function saveListItem($list_id, $item_id) {
         global $thisstaff;
 
diff --git a/include/class.dynamic_forms.php b/include/class.dynamic_forms.php
index 6131a294b..624294c2d 100644
--- a/include/class.dynamic_forms.php
+++ b/include/class.dynamic_forms.php
@@ -1446,6 +1446,28 @@ class SelectionField extends FormField {
         return parent::getWidget($widgetClass);
     }
 
+    function display($value) {
+        global $thisstaff;
+
+        if (!is_array($value)
+                || !$thisstaff // Only agents can preview for now
+                || !($list=$this->getList()))
+            return parent::display($value);
+
+        $display = array();
+        foreach ($value as $k => $v) {
+            if (is_numeric($k)
+                    && ($i=$list->getItem((int) $k))
+                    && $i->hasProperties())
+                $display[] = $i->display();
+            else // Perhaps deleted  entry
+                $display[] = $v;
+        }
+
+        return implode(',', $display);
+
+    }
+
     function parse($value) {
 
         if (!($list=$this->getList()))
diff --git a/include/class.list.php b/include/class.list.php
index 84322f700..d578b73c2 100644
--- a/include/class.list.php
+++ b/include/class.list.php
@@ -70,6 +70,7 @@ interface CustomListItem {
 
     function getConfiguration();
 
+    function hasProperties();
     function isEnabled();
     function isDeletable();
     function isEnableable();
@@ -664,6 +665,10 @@ class DynamicListItem extends VerySimpleModel implements CustomListItem {
         $this->clearStatus(self::ENABLED);
     }
 
+    function hasProperties() {
+        return ($this->getForm() && $this->getForm()->getFields());
+    }
+
     function getId() {
         return $this->get('id');
     }
@@ -733,6 +738,10 @@ class DynamicListItem extends VerySimpleModel implements CustomListItem {
         return $this->getConfigurationForm();
     }
 
+    function getFields() {
+        return $this->getForm()->getFields();
+    }
+
     function getVar($name) {
         $config = $this->getConfiguration();
         $name = mb_strtolower($name);
@@ -768,6 +777,15 @@ class DynamicListItem extends VerySimpleModel implements CustomListItem {
         return $this->toString();
     }
 
+    function display() {
+        return sprintf('<a class="preview" href="#"
+                data-preview="#list/%d/items/%d/preview">%s</a>',
+                $this->getListId(),
+                $this->getId(),
+                $this->getValue()
+                );
+    }
+
     function update($vars, &$errors=array()) {
 
         if (!$vars['value']) {
@@ -1102,7 +1120,7 @@ implements CustomListItem, TemplateVariable {
         return $this->set($field, $this->get($field) | $flag);
     }
 
-    protected function hasProperties() {
+    function hasProperties() {
         return ($this->get('properties'));
     }
 
diff --git a/include/staff/templates/list-item-preview.tmpl.php b/include/staff/templates/list-item-preview.tmpl.php
new file mode 100644
index 000000000..db9b87f8c
--- /dev/null
+++ b/include/staff/templates/list-item-preview.tmpl.php
@@ -0,0 +1,26 @@
+<?php
+$name = $item->getValue();
+if ($abbrev=$item->getAbbrev())
+    $name = sprintf('%s (%s)', $name, $abbrev);
+
+?>
+<h2><?php echo Format::htmlchars($name); ?></h2>
+<hr/>
+
+<?php
+if ($item->hasProperties()) { ?>
+<div>
+    <table class="custom-info" width="100%">
+        <?php
+        foreach ($item->getFields() as $f) {
+        ?>
+            <tr><td style="width:30%;"><?php echo
+                Format::htmlchars($f->get('label')); ?>:</td>
+            <td><?php echo $f->display($f->value); ?></td>
+            </tr>
+        <?php }
+        ?>
+    </table>
+</div>
+<?php
+} ?>
diff --git a/include/staff/templates/list-item-row.tmpl.php b/include/staff/templates/list-item-row.tmpl.php
index 574f28e97..b54fee23f 100644
--- a/include/staff/templates/list-item-row.tmpl.php
+++ b/include/staff/templates/list-item-row.tmpl.php
@@ -7,12 +7,17 @@
             <input type="checkbox" value="<?php echo $id; ?>" class="mass nowarn"/>
         </td>
         <td>
-            <a class="field-config"
+            <a class="field-config preview"
                style="overflow:inherit"
                href="#list/<?php
                 echo $list->getId(); ?>/item/<?php
                 echo $id ?>/update"
                id="item-<?php echo $id; ?>"
+               data-preview="<?php echo
+                sprintf('#/list/%d/items/%d/preview',
+                       $item->getListId(),
+                       $item->getId());
+                ?>"
             ><?php
                 echo sprintf('<i class="icon-edit" %s></i> ',
                         ($prop_fields && !$item->getConfiguration())
diff --git a/scp/ajax.php b/scp/ajax.php
index f01cf70b4..de27157dc 100644
--- a/scp/ajax.php
+++ b/scp/ajax.php
@@ -71,6 +71,7 @@ $dispatcher = patterns('',
         url_get('^(?P<list>\w+)/items/search$', 'searchListItems'),
         url_get('^(?P<list>\w+)/item/(?P<id>\d+)/update$', 'getListItem'),
         url_post('^(?P<list>\w+)/item/(?P<id>\d+)/update$', 'saveListItem'),
+        url_get('^(?P<list>\w+)/items/(?P<id>\d+)/preview$', 'previewListItem'),
         url('^(?P<list>\w+)/item/add$', 'addListItem'),
         url('^(?P<list>\w+)/import$', 'importListItems'),
         url('^(?P<list>\w+)/manage$', 'massManageListItems'),
-- 
GitLab