From 37db482851a7724520e5db7aef89dcd8490cc081 Mon Sep 17 00:00:00 2001
From: Malf Furious <m@lfurio.us>
Date: Thu, 1 Jun 2017 23:35:44 -0400
Subject: Add issue class

---
 app/class/issue.class.php | 117 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 117 insertions(+)
 create mode 100644 app/class/issue.class.php

diff --git a/app/class/issue.class.php b/app/class/issue.class.php
new file mode 100644
index 0000000..e439dff
--- /dev/null
+++ b/app/class/issue.class.php
@@ -0,0 +1,117 @@
+<?php
+
+/*
+ * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * For more information, please refer to UNLICENSE
+ */
+
+require_once "class/object.class.php";
+require_once "class/stage.class.php";
+require_once "class/user.class.php";
+require_once "class/mesg.class.php";
+
+/*
+ * This class models Scrott issues.  Issues represent units of work, can
+ * be assigned to users, and advance through a pipeline.
+ */
+class issue extends object
+{
+    /*
+     * Constructor
+     */
+    public function __construct(?string $guid = NULL)
+    {
+        $this->fields['issues'] = array(
+            "guid",
+            "numb",
+            "assignee",
+            "seen",
+            "description",
+            "due",
+            "tags",
+        );
+
+        parent::__construct($guid);
+        $this->expectType("issue");
+    }
+
+    /*
+     * Initialize a new issue object with the given name, parent, and
+     * owner.
+     */
+    public static function initNew(string $name, user $owner, stage $parent) : issue
+    {
+        $pad = $parent->getParent();
+        $numb = $pad->issueNumb++;
+        $pad->saveObj();
+
+        $issue = new issue();
+        $issue->setOwner($owner);
+        $issue->setParent($parent);
+        $issue->name = $name;
+        $issue->objtype = "issue";
+        $issue->numb = $numb;
+        $issue->saveObj();
+        return $issue;
+    }
+
+    /*
+     * Get all activity for this issue.  Messages are sorted by date
+     * created.
+     */
+    public function getMesgs_ordByDatetime() : array
+    {
+        $query = "SELECT guid FROM objects WHERE objtype = 'mesg' AND " .
+            "parent = '" . database::esc($this->guid) . "' ORDER BY created";
+        $res = database::query($query);
+
+        $mesgs = array();
+
+        foreach ($res as $m)
+            $mesgs[] = new mesg($m['guid']);
+
+        return $mesgs;
+    }
+
+    /*
+     * Reset the seen flag and reassign this issue.
+     */
+    public function assignTo(user $assignee) : void
+    {
+        $this->seen = 0;
+        $this->assignee = $assignee->guid;
+        $this->saveObj();
+    }
+
+    /*
+     * Advance this issue in the pipeline, closing it if already in the
+     * last stage.
+     */
+    public function advance() : void
+    {
+        $stage = $this->getParent();
+
+        if (!($next = $stage->getNext()))
+            $this->close();
+
+        $this->setParent($next);
+    }
+
+    /*
+     * Mark this issue as completed and closed.
+     */
+    public function close() : void
+    {
+        $pad = $this->getParent()->getParent();
+        $this->setParent($pad);
+    }
+}
+
+?>
-- 
cgit v1.2.3


From eb4b42f32856b0e5616b700614c8c68d77cd0ea3 Mon Sep 17 00:00:00 2001
From: Malf Furious <m@lfurio.us>
Date: Sat, 3 Jun 2017 17:24:31 -0400
Subject: issue:  Fix bug in function advance()

The call to setParent() should have been in an else.  It was being
called every time...
---
 app/class/issue.class.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/app/class/issue.class.php b/app/class/issue.class.php
index e439dff..1286e1a 100644
--- a/app/class/issue.class.php
+++ b/app/class/issue.class.php
@@ -100,8 +100,8 @@ class issue extends object
 
         if (!($next = $stage->getNext()))
             $this->close();
-
-        $this->setParent($next);
+        else
+            $this->setParent($next);
     }
 
     /*
-- 
cgit v1.2.3


From da90fc87384b1daa8104cdea14fb1b52f0f747b7 Mon Sep 17 00:00:00 2001
From: Malf Furious <m@lfurio.us>
Date: Sat, 3 Jun 2017 17:31:16 -0400
Subject: issue: Fix bug in functions advance() and close()

If the issue is already closed, these functions should do nothing.
Continuing the logic in these functions could currupt the database.
---
 app/class/issue.class.php | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/app/class/issue.class.php b/app/class/issue.class.php
index 1286e1a..3127a87 100644
--- a/app/class/issue.class.php
+++ b/app/class/issue.class.php
@@ -98,6 +98,9 @@ class issue extends object
     {
         $stage = $this->getParent();
 
+        if ($stage->objtype != "stage")
+            return;
+
         if (!($next = $stage->getNext()))
             $this->close();
         else
@@ -110,7 +113,9 @@ class issue extends object
     public function close() : void
     {
         $pad = $this->getParent()->getParent();
-        $this->setParent($pad);
+
+        if ($pad)
+            $this->setParent($pad);
     }
 }
 
-- 
cgit v1.2.3