summaryrefslogtreecommitdiffstats
path: root/app/class/stage.class.php
blob: 5d24465a1574c0dab6989a102ecc82e0fa774762 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?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/obj.class.php";
require_once "class/pad.class.php";
require_once "class/issue.class.php";

/*
 * This class models Scrott pad stages.  Stages form a pipeline through
 * which issues can progress.
 */
class stage extends obj
{
    /*
     * Constructor
     */
    public function __construct(?string $guid = NULL)
    {
        $this->fields['stages'] = array(
            "guid",
            "stage",
        );

        parent::__construct($guid);
        $this->expectType("stage");
    }

    /*
     * Initialize a new stage object with the given name and parent
     * pad.
     */
    public static function initNew(string $name, pad $parent) : stage
    {
        $stage = new stage();
        $stage->setParent($parent);
        $stage->name = $name;
        $stage->objtype = "stage";
        $stage->saveObj();
        return $stage;
    }

    /*
     * Get the stage following this one in the pipeline.  NULL is
     * returned if no stage is next.
     */
    public function getNext() : ?stage
    {
        if (!isset($this->stage) || $this->stage == "")
            return NULL;

        return new stage($this->stage);
    }

    /*
     * Get the stage preceding this one in the pipeline.  NULL is
     * returned if no stage is previous.
     */
    public function getPrev() : ?stage
    {
        $pad = $this->getParent();

        if ($pad->stage == $this->guid)
            return NULL;

        $query = "SELECT guid FROM stages WHERE stage = '" . database::esc($this->guid) . "'";
        $res = database::query($query);

        if (count($res) == 0)
            return NULL;

        return new stage($res[0]['guid']);
    }

    /*
     * Get an array of all stages reachable from this one.  The array
     * is in proper order and includes the current stage.
     */
    public function getArray() : array
    {
        $stages = array();
        $curr = $this;

        do $stages[] = $curr;
        while (($curr = $curr->getNext()));

        return $stages;
    }

    /*
     * Get all issues in this stage, sorted by due date, then by
     * issue number.
     */
    public function getIssues_ordByDueByNumb() : array
    {
        $query = "SELECT o.guid FROM objects o JOIN issues i ON o.guid = i.guid " .
            "WHERE o.objtype = 'issue' AND o.parent = '" . database::esc($this->guid) .
            "' ORDER BY i.due, i.numb DESC";
        $res = database::query($query);

        $issues = array();

        foreach ($res as $i)
            $issues[] = new issue($i['guid']);

        return $issues;
    }

    /*
     * Reorder the stages of a pipeline by moving this one forward.
     * This swaps the places of the current stage and the one following
     * it.  If this stage is the last in its pipeline, nothing is done
     * and false is returned.
     */
    public function moveForward() : bool
    {
        if (!($next = $this->getNext()))
            return false;

        if (!($prev = $this->getPrev()))
            $prev = $this->getParent();

        $tmp = $next->stage;
        $prev->stage = $next->guid;
        $next->stage = $this->guid;
        $this->stage = $tmp;

        $next->saveObj();
        $prev->saveObj();
        $this->saveObj();
        return true;
    }

    /*
     * Reorder the stages of a pipeline by moving this one backward.
     * This swaps the places of the current stage and the one preceding
     * it.  If this stage is the first in its pipeline, nothing is done
     * and false is returned.
     */
    public function moveBackward() : bool
    {
        if (!($prev = $this->getPrev()))
            return false;

        $ret = $prev->moveForward();
        $this->refreshObj();
        return $ret;
    }

    /*
     * Insert a stage object in this pipeline, following $this object
     */
    public function insertStage(stage $stage) : void
    {
        $stage->stage = $this->stage;
        $this->stage = $stage->guid;
        $stage->saveObj();
        $this->saveObj();
    }

    /*
     * Remove this stage object and move all of its issues.  Issues are
     * moved to the given stage object.  Additionally, the pad may be
     * given, in which case, those issues will be closed.
     */
    public function removeStage(obj $mvt) : void
    {
        if (!($prev = $this->getPrev()))
            $prev = $this->getParent();

        foreach ($this->getIssues_ordByDueByNumb() as $i)
            $i->setParent($mvt);

        $prev->stage = $this->stage;
        $prev->saveObj();

        $this->delObj();
    }
}

?>