static function get_menus() { $db = new DB\SQL('mysql:host=localhost;dbname=MealDB', 'root', ''); $menuInst = new DB\SQL\Mapper($db, "MENU"); $menus = array(); $menuInst->load(''); while (!$menuInst->dry()) { $id = $menuInst->id; $name = $menuInst->name; $menus[$id] = $name; $menuInst->next(); } return $menus; }
public function moveCategory($catID = 0, $direction = NULL, $parent = NULL) { if ($parent === NULL) { $sql = "SELECT C.parent_cid \r\n\t\t\t\t\t\tFROM `tbl_categories`C \r\n\t\t\t\t\t\tWHERE C.`cid`= :catID"; $data = $this->exec($sql, [":catID" => $catID]); if (empty($data[0]) or !is_numeric($data[0]['parent_cid'])) { return FALSE; } $parent = $data[0]['parent_cid']; } $categories = new \DB\SQL\Mapper($this->db, $this->prefix . 'categories'); $categories->load(["parent_cid = ?", $parent], ['order' => "inorder " . ($direction == "up" ? "DESC" : "ASC")]); $elements = $categories->count(["parent_cid = ?", $parent]); // when moving elements upwards, we need to invert the entire logic if ($direction == "up") { $i = $elements + 1; } else { $i = 0; } // init vacant spot $vacant = -1; while (!$categories->dry()) { // gets dry when we passed the last record if ($direction == "up") { $i--; } else { $i++; } if ($categories->cid == $catID and $direction !== NULL) { if ($direction == "up") { $categories->inorder = max(1, $i - 1); } else { $categories->inorder = min($elements, $i + 1); } // remember the vacant spot $vacant = $i; } elseif ($direction == "down" and $i == $vacant + 1) { $categories->inorder = $i - 1; } elseif ($direction == "up" and $i == $vacant - 1) { $categories->inorder = $i + 1; } else { $categories->inorder = $i; } $categories->save(); // moves forward even when the internal pointer is on last record $categories->next(); } return $parent; }