From 5340dce8ad70a8cfc62116144f668f22845a2f31 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 5 Apr 2017 22:31:50 -0400 Subject: Add stage class --- app/class/stage.class.php | 137 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 app/class/stage.class.php diff --git a/app/class/stage.class.php b/app/class/stage.class.php new file mode 100644 index 0000000..31f6a94 --- /dev/null +++ b/app/class/stage.class.php @@ -0,0 +1,137 @@ +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"; + 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 = new 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; + } + + /* + * 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 = new pad($this->getParent()); + + $tmp = $next->stage; + $prev->stage = $next->guid; + $next->stage = $this->guid; + $this->stage = $tmp; + + $next->saveObj(); + $prev->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; + + return $prev->moveForward(); + } +} + +?> -- cgit v1.2.3