From 9733a06af32e6b409558795f69f89b7c59487a55 Mon Sep 17 00:00:00 2001
From: Malf Furious <m@lfurio.us>
Date: Tue, 23 Oct 2018 01:31:19 -0400
Subject: datamods:  Implement new issue modal form handler

POSTs to the dm-issue-add form are handled by this new handler.  Pad
data is passed via the form, we create the new issue in the pad's first
stage, crediting the current logged in user.  New issue is unassigned.

I call `location()` at the bottom, since not doing so leaves the page in
a weird state.  The reason being that most of the page rendering logic
operates off of the 'pageObj', which is instanciated and assigned prior
to the invocation of this handler.  This is to be fleshed-out during the
v0.2-v0.3 development iteration.

Signed-off-by: Malf Furious <m@lfurio.us>
---
 app/model/datamods.php | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/app/model/datamods.php b/app/model/datamods.php
index 8eaff23..c7c7da1 100644
--- a/app/model/datamods.php
+++ b/app/model/datamods.php
@@ -18,6 +18,8 @@ require_once "class/user.class.php";
 require_once "class/group.class.php";
 require_once "class/pad.class.php";
 require_once "class/stage.class.php";
+require_once "class/issue.class.php";
+require_once "class/mesg.class.php";
 
 /*
  * Action: dm-group-add - New group modal
@@ -62,4 +64,40 @@ if (isAction("dm-pad-add"))
     $td->insertStage($ip);
 }
 
+/*
+ * Action: dm-issue-add - New issue modal
+ */
+if (isAction("dm-issue-add"))
+{
+    $form = new form();
+    $form->text("pad");
+    $form->text("name");
+    $form->text("mesg", false);
+
+    if (!$form->populate(input()))
+        return;
+
+    $pad  = new pad($form->pad);
+    $user = user::getCurrent();
+
+    if (!$user->canCreateSub($pad))
+    {
+        logError(ERROR, "You do not have permission to open an issue for '" . $pad->name . "'");
+        return;
+    }
+
+    $stages = $pad->getStages();
+
+    if (count($stages) == 0)
+    {
+        logError(ERROR, "Cannot create new issue, '" . $pad->name . "' doesn't have any stages");
+        return;
+    }
+
+    $mesg = mesg::initNewDiscussion($form->name, $form->mesg, $user, $pad);
+    $issue = issue::initNew($mesg, $user, $stages[0]);
+
+    location(); // bug mitigation
+}
+
 ?>
-- 
cgit v1.2.3