public function delete($id) { // login check - if fail, return no data to stop error flagging to user if ((int) $this->check_access() < 8) { $this->response(null, null, 401); } if (!is_numeric($id)) { $this->response(null, null, 400); } $db = new RazorDB(); // delete page $db->connect("page"); $db->delete_rows(array("column" => "id", "value" => (int) $id)); $db->disconnect(); // remove any page_content items $db->connect("page_content"); $db->delete_rows(array("column" => "page_id", "value" => (int) $id)); $db->disconnect(); // remove any menu_items $db->connect("menu_item"); $db->delete_rows(array("column" => "page_id", "value" => (int) $id)); $db->disconnect(); // return the basic user details $this->response("success", "json"); }
public function post($data) { // login check - if fail, return no data to stop error flagging to user if ((int) $this->check_access() < 10) { $this->response(null, null, 401); } // menu item $db = new RazorDB(); $db->connect("menu_item"); // 1. grab all menus in position order $options = array("order" => array("column" => "position", "direction" => "asc")); $search = array("column" => "id", "not" => true, "value" => null); $all_menu_items = $db->get_rows($search, $options); $all_menu_items = $all_menu_items["result"]; // 2. make flat arrays $new_menus_flat = array(); foreach ($data as $menu) { // set up menu item arrays if (!isset($new_menus_flat[$menu["id"]])) { $new_menus_flat[$menu["id"]] = array(); } foreach ($menu["menu_items"] as $mi) { if (isset($mi["id"])) { $new_menus_flat[$menu["id"]][] = $mi["id"]; } if (isset($mi["sub_menu"]) & !empty($mi["sub_menu"])) { foreach ($mi["sub_menu"] as $sub_menu_item) { if (isset($sub_menu_item["id"])) { $new_menus_flat[$menu["id"]][] = $sub_menu_item["id"]; } } } } } $current_menus_flat = array(); foreach ($all_menu_items as $ami) { // set up menu item arrays if (!isset($current_menus_flat[$ami["menu_id"]])) { $current_menus_flat[$ami["menu_id"]] = array(); } $current_menus_flat[$ami["menu_id"]][] = $ami["id"]; // at same time remove any items missing if (!in_array($ami["id"], $new_menus_flat[$ami["menu_id"]])) { $db->delete_rows(array("column" => "id", "value" => (int) $ami["id"])); } } // 3. update all of sent menu data, by looping through the new $data foreach ($data as $new_menu) { $pos = 1; // each menu foreach ($new_menu["menu_items"] as $nmi) { if (isset($nmi["id"]) && in_array($nmi["id"], $current_menus_flat[$new_menu["id"]])) { // update menu item $search = array("column" => "id", "value" => $nmi["id"]); $db->edit_rows($search, array("position" => $pos)); } else { // add new item $row = array("menu_id" => (int) $new_menu["id"], "position" => $pos, "level" => 1, "page_id" => $nmi["page_id"], "link_id" => 0); $db->add_rows($row); } $pos++; // now check for sub menu if (isset($nmi["sub_menu"]) && !empty($nmi["sub_menu"])) { foreach ($nmi["sub_menu"] as $nsmi) { if (isset($nsmi["id"]) && in_array($nsmi["id"], $current_menus_flat[$new_menu["id"]])) { // update menu item $search = array("column" => "id", "value" => $nsmi["id"]); $db->edit_rows($search, array("position" => $pos)); } else { // add new item $row = array("menu_id" => (int) $new_menu["id"], "position" => $pos, "level" => 2, "page_id" => $nsmi["page_id"], "link_id" => 0); $db->add_rows($row); } $pos++; } } } } $db->disconnect(); $this->response("success", "json"); }
public function delete($id) { // check we have a logged in user if ((int) $this->check_access() < 1) { $this->response(null, null, 401); } if (empty($id)) { $this->response(null, null, 400); } if ($id == 1) { $this->response(null, null, 400); } $id = (int) $id; $db = new RazorDB(); $db->connect("user"); if ($this->user["id"] == $id) { // this is your account, allow removal of own account $search = array("column" => "id", "value" => $this->user["id"]); $db->delete_rows($search); $response = "reload"; } elseif ($this->check_access() == 10) { // if not account owner, but acces of 10, can remove account $search = array("column" => "id", "value" => $id); $db->delete_rows($search); $response = "success"; } else { $this->response(null, null, 401); } $db->disconnect(); $this->response($response, "json"); }
public function post($data) { // login check - if fail, return no data to stop error flagging to user if ((int) $this->check_access() < 10) { $this->response(null, null, 401); } if (!isset($data["content"])) { $this->response(null, null, 400); } // update content $db = new RazorDB(); $db->connect("content"); // update or add content $new_content_map = array(); foreach ($data["content"] as $key => $content) { if (!isset($content["content_id"]) || !isset($content["content"]) || empty($content["content"])) { unset($data["content"][$key]); continue; } if (stripos($content["content_id"], "new-") === false) { // update $search = array("column" => "id", "value" => $content["content_id"]); $db->edit_rows($search, array("content" => $content["content"], "name" => $content["name"])); } else { // add new content and map the ID to the new id for locations table $row = array("content" => $content["content"], "name" => $content["name"]); $result = $db->add_rows($row); $new_content_map[$content["content_id"]] = $result["result"][0]["id"]; } } $db->disconnect(); // update or add locations $db = new RazorDB(); $db->connect("page_content"); // 1. first take snapshot of current $search = array("column" => "page_id", "value" => (int) $data["page_id"]); $current_page_content = $db->get_rows($search); $current_page_content = $current_page_content["result"]; // 2. iterate through updating or adding, make a note of all id's $page_content_map = array(); foreach ($data["locations"] as $location => $columns) { foreach ($columns as $column => $blocks) { foreach ($blocks as $pos => $block) { if ($block["id"] != "new") { // update $search = array("column" => "id", "value" => $block["id"]); $row = array("location" => $location, "column" => (int) $column, "position" => $pos + 1, "json_settings" => json_encode($block["settings"])); if (isset($block["extension"])) { $row["extension"] = $block["extension"]; } $db->edit_rows($search, $row); $page_content_map[] = $block["id"]; } else { // add new, if new, add, if new but already present add, else add as ext $new_content_id = isset($block["content_id"], $new_content_map[$block["content_id"]]) ? $new_content_map[$block["content_id"]] : (isset($block["content_id"]) && is_numeric($block["content_id"]) ? $block["content_id"] : null); if (!empty($new_content_id) || isset($block["extension"])) { $row = array("page_id" => (int) $data["page_id"], "content_id" => $new_content_id, "location" => $location, "column" => (int) $column, "position" => $pos + 1); if (isset($block["extension"])) { $row["extension"] = $block["extension"]; $row["json_settings"] = isset($block["settings"]) ? json_encode($block["settings"]) : null; } $result = $db->add_rows($row); $page_content_map[] = $result["result"][0]; } } } } } // 3. run through id's affected against snapshot, if any missing, remove them. foreach ($current_page_content as $row) { if (!in_array($row["id"], $page_content_map)) { $db->delete_rows(array("column" => "id", "value" => (int) $row["id"])); } } $db->disconnect(); // return the basic user details $this->response("success", "json"); }