From cad3baa67d4b3c71c4247f557d7fbc4181578805 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Tue, 19 Aug 2014 08:40:48 +0000
Subject: [PATCH] Support custom statuses deletion

Statuses with tickets cannot be deleted
Fix orm to class gracefully when joins are not inspected
Add create date to new statuses
---
 include/class.list.php                        | 33 ++++++++++++++++---
 include/class.orm.php                         | 12 ++++---
 include/class.user.php                        |  3 ++
 .../staff/templates/status-options.tmpl.php   |  2 +-
 scp/lists.php                                 |  4 ++-
 5 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/include/class.list.php b/include/class.list.php
index d1eae4160..a88040d8b 100644
--- a/include/class.list.php
+++ b/include/class.list.php
@@ -748,6 +748,11 @@ class TicketStatus  extends VerySimpleModel implements CustomListItem {
         'table' => TICKET_STATUS_TABLE,
         'ordering' => array('name'),
         'pk' => array('id'),
+        'joins' => array(
+            'tickets' => array(
+                'reverse' => 'TicketModel.status',
+                )
+        )
     );
 
     var $_list;
@@ -831,11 +836,19 @@ class TicketStatus  extends VerySimpleModel implements CustomListItem {
 
     function isDeletable() {
 
-        return !($this->isInternal() || $this->isDefault());
+        return !($this->isInternal()
+                || $this->isDefault()
+                || $this->getNumTickets());
     }
 
     function isInternal() {
-        return ($this->hasFlag('mode', self::INTERNAL));
+        return ($this->isDefault()
+                || $this->hasFlag('mode', self::INTERNAL));
+    }
+
+
+    function getNumTickets() {
+        return $this->tickets->count();
     }
 
     function getId() {
@@ -961,11 +974,11 @@ class TicketStatus  extends VerySimpleModel implements CustomListItem {
 
     function delete() {
 
+        // Statuses with tickets are not deletable
         if (!$this->isDeletable())
             return false;
 
-        // TODO: Delete and do house cleaning (move tickets..etc)
-
+        return parent::delete();
     }
 
     function toString() {
@@ -976,6 +989,15 @@ class TicketStatus  extends VerySimpleModel implements CustomListItem {
         return $this->toString();
     }
 
+    static function create($ht) {
+
+        if (!isset($ht['mode']))
+            $ht['mode'] = 1;
+
+        $ht['created'] = new SqlFunction('NOW');
+
+        return  parent::create($ht);
+    }
 
     static function lookup($var, $list= false) {
 
@@ -993,7 +1015,6 @@ class TicketStatus  extends VerySimpleModel implements CustomListItem {
 
         $properties = JsonDataEncoder::encode($ht['properties']);
         unset($ht['properties']);
-        $ht['created'] = new SqlFunction('NOW');
         if ($status = TicketStatus::create($ht)) {
             $status->save(true);
             $status->_config = new Config('TS.'.$status->getId());
@@ -1007,4 +1028,6 @@ class TicketStatus  extends VerySimpleModel implements CustomListItem {
         include(STAFFINC_DIR . 'templates/status-options.tmpl.php');
     }
 }
+
+TicketStatus::_inspect();
 ?>
diff --git a/include/class.orm.php b/include/class.orm.php
index f46fd8ea5..264582fb6 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -42,10 +42,14 @@ class VerySimpleModel {
         elseif (isset(static::$meta['joins'][$field])) {
             // TODO: Support instrumented lists and such
             $j = static::$meta['joins'][$field];
-            $class = $j['fkey'][0];
-            $v = $this->ht[$field] = $class::lookup(
-                array($j['fkey'][1] => $this->ht[$j['local']]));
-            return $v;
+            // Make sure joins were inspected
+            if (isset($j['fkey'])
+                    && ($class = $j['fkey'][0])
+                    && class_exists($class)) {
+                $v = $this->ht[$field] = $class::lookup(
+                    array($j['fkey'][1] => $this->ht[$j['local']]));
+                return $v;
+            }
         }
         if (isset($default))
             return $default;
diff --git a/include/class.user.php b/include/class.user.php
index 127a078d5..8130551a9 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -39,6 +39,9 @@ class TicketModel extends VerySimpleModel {
         'joins' => array(
             'user' => array(
                 'constraint' => array('user_id' => 'UserModel.id')
+            ),
+            'status' => array(
+                'constraint' => array('status_id' => 'TicketStatus.id')
             )
         )
     );
diff --git a/include/staff/templates/status-options.tmpl.php b/include/staff/templates/status-options.tmpl.php
index 5d056a659..5fc39f32a 100644
--- a/include/staff/templates/status-options.tmpl.php
+++ b/include/staff/templates/status-options.tmpl.php
@@ -44,7 +44,7 @@ foreach($actions as $k => $v) {
                  <a class="no-pjax tickets-action"
                     href="#tickets/status/<?php echo $k; ?>/<?php
                     echo $s->getId(); ?>"> <i
-                        class="icon-tag"></i> <?php echo $s->getName(); ?></a> </li>
+                        class="icon-tag"></i> <?php echo __($s->getName()); ?></a> </li>
             <?php
             } ?>
           </ul>
diff --git a/scp/lists.php b/scp/lists.php
index 02b41d117..b3c424de7 100644
--- a/scp/lists.php
+++ b/scp/lists.php
@@ -95,9 +95,11 @@ if($_POST) {
                 if ($errors)
                      $errors['err'] = $errors['err'] ?: sprintf(__('Unable to update %s. Correct error(s) below and try again!'),
                         __('custom list items'));
-                else
+                else {
+                    $list->_items = null;
                     $msg = sprintf(__('Successfully updated %s'),
                         __('this custom list'));
+                }
 
             } elseif ($errors)
                 $errors['err'] = $errors['err'] ?: sprintf(__('Unable to update %s. Correct error(s) below and try again!'),
-- 
GitLab