diff options
author | Simon Rettberg | 2019-07-23 13:57:20 +0200 |
---|---|---|
committer | Simon Rettberg | 2019-07-23 13:57:20 +0200 |
commit | 04828b94e9df8321feae0bfb99daa468c0d3d383 (patch) | |
tree | e3b00490dc14a0f5d6ffda5bab1ce7c82bafee52 /modules-available/roomplanner/inc/composedroom.inc.php | |
parent | slx-fixes.js: Make .cachedScript more compatible to .getScript (diff) | |
download | slx-admin-04828b94e9df8321feae0bfb99daa468c0d3d383.tar.gz slx-admin-04828b94e9df8321feae0bfb99daa468c0d3d383.tar.xz slx-admin-04828b94e9df8321feae0bfb99daa468c0d3d383.zip |
[roomplanner] Support creating recursive/composed rooms
Diffstat (limited to 'modules-available/roomplanner/inc/composedroom.inc.php')
-rw-r--r-- | modules-available/roomplanner/inc/composedroom.inc.php | 175 |
1 files changed, 152 insertions, 23 deletions
diff --git a/modules-available/roomplanner/inc/composedroom.inc.php b/modules-available/roomplanner/inc/composedroom.inc.php index 11e8455e..cdd50984 100644 --- a/modules-available/roomplanner/inc/composedroom.inc.php +++ b/modules-available/roomplanner/inc/composedroom.inc.php @@ -1,65 +1,80 @@ <?php -class ComposedRoom +class ComposedRoom extends Room { /** * @var string How to compose contained rooms. Value is either horizontal or vertical. */ - public $orientation = 'horizontal'; + private $orientation = 'horizontal'; /** * @var int[] Order in which contained rooms are composed. List of locationid. */ - public $list; + private $list; /** * @var bool Whether composed room is active, ie. visible in PVS. */ - public $enabled; + private $enabled; /** * @var int locationid of contained room that is the controlling room; */ - public $controlRoom; + private $controlRoom; - public function __construct($data) + /** + * ComposedRoom constructor. + * + * @param array|true $row DB row to instantiate from, or true to read from $_POST + */ + public function __construct($row, $sanitize = true) { - if ($data instanceof ComposedRoom) { - foreach ($data as $k => $v) { - $this->{$k} = $v; - } + if ($row === true) { + $this->orientation = Request::post('orientation', 'horizontal', 'string'); + $this->enabled = (bool)Request::post('enabled', 0, 'int'); + $this->controlRoom = Request::post('controlroom', 0, 'int'); + $vals = Request::post('sort', [], 'array'); + asort($vals, SORT_ASC | SORT_NUMERIC); + $this->list = array_keys($vals); } else { - if (is_array($data) && isset($data['roomplan'])) { + parent::__construct($row); + if (is_array($row) && isset($row['roomplan'])) { // From DB - $data = json_decode($data['roomplan'], true); - } elseif (is_string($data)) { - // Just JSON - $data = json_decode($data, true); + $row = json_decode($row['roomplan'], true); } - if (is_array($data)) { + if (is_array($row)) { foreach ($this as $k => $v) { - if (isset($data[$k])) { - $this->{$k} = $data[$k]; + if (isset($row[$k])) { + $this->{$k} = $row[$k]; } } } } - $this->sanitize(); + if ($sanitize) { + $this->sanitize(); + } } /** * Make sure all member vars have the proper type */ - private function sanitize() + protected function sanitize() { $this->orientation = ($this->orientation === 'horizontal' ? 'horizontal' : 'vertical'); settype($this->enabled, 'bool'); settype($this->list, 'array'); settype($this->controlRoom, 'int'); - foreach ($this->list as &$v) { + self::init(); + //error_log('List: ' . print_r($this->list, true)); + //error_log('Rooms: ' . print_r(self::$rooms, true)); + $old = $this->list; + $this->list = []; + foreach ($old as $v) { settype($v, 'int'); + if (isset(self::$rooms[$v])) { + $this->list[] = $v; + } } - $this->list = array_values($this->list); if (!empty($this->list) && !in_array($this->controlRoom, $this->list)) { $this->controlRoom = $this->list[0]; } @@ -71,7 +86,121 @@ class ComposedRoom public function serialize() { $this->sanitize(); - return json_encode($this); + $out = []; + foreach ($this as $k => $v) { + $out[$k] = $v; + } + return json_encode($out); + } + + public function orientation() + { + return $this->orientation; + } + + public function subLocationIds() + { + return $this->list; } + public function controlRoom() + { + return $this->controlRoom; + } + + public function machineCount() + { + $sum = 0; + foreach ($this->list as $lid) { + $sum += self::$rooms[$lid]->machineCount(); + } + return $sum; + } + + public function getSize(&$width, &$height) + { + $horz = ($this->orientation == 'horizontal'); + $width = $height = 0; + foreach ($this->list as $locId) { + self::$rooms[$locId]->getSize($w, $h); + $width = $horz ? $width + $w : max($width, $w); + $height = !$horz ? $height + $h : max($height, $h); + } + } + + public function getIniClientSection(&$i, $offX = 0, $offY = 0) + { + if (!$this->enabled) + return false; + if ($this->orientation == 'horizontal') { + $x = 1; + $y = 0; + } else { + $x = 0; + $y = 1; + } + $out = ''; + foreach ($this->list as $locId) { + $ret = self::$rooms[$locId]->getIniClientSection($i, $offX, $offY); + if ($ret !== false) { + $out .= $ret; + self::$rooms[$locId]->getSize($w, $h); + $offX += $w * $x; + $offY += $h * $y; + } + } + if (empty($out)) + return false; + return $out; + } + + public function getShiftedArray($offX = 0, $offY = 0) + { + if (!$this->enabled) + return false; + if ($this->orientation == 'horizontal') { + $x = 1; + $y = 0; + } else { + $x = 0; + $y = 1; + } + $ret = []; + foreach ($this->list as $locId) { + $new = self::$rooms[$locId]->getShiftedArray($offX, $offY); + if ($new !== false) { + $ret = array_merge($ret, $new); + self::$rooms[$locId]->getSize($w, $h); + $offX += $w * $x; + $offY += $h * $y; + } + } + if (empty($ret)) + return false; + return $ret; + } + + public function getManagerIp() + { + if (isset(self::$rooms[$this->controlRoom])) + return self::$rooms[$this->controlRoom]->getManagerIp(); + return false; + } + + public function getTutorIp() + { + if (isset(self::$rooms[$this->controlRoom])) + return self::$rooms[$this->controlRoom]->getTutorIp(); + return false; + } + + public function isLeaf() + { + return false; + } + + public function shouldSkip() + { + return !$this->enabled; + } } |