/** * [POST] * [PARAMS.id] * Update item base */ public function updateItem() { $id = (int) $this->f3->get('PARAMS.id'); $result = array('success' => false, 'message' => '', 'total' => 0, 'records' => array()); try { $item = new Item($this->db); // MySQL doesn't understand boolean -> need to convert boolean back to 1 / 0 $this->f3->set('POST.isActive', $this->toBool($this->f3->get('POST.isActive')) ? 1 : 0); $this->f3->set('POST.isVegetarian', $this->toBool($this->f3->get('POST.isVegetarian')) ? 1 : 0); $this->f3->set('POST.isPopular', $this->toBool($this->f3->get('POST.isPopular')) ? 1 : 0); $this->f3->set('POST.isSpecial', $this->toBool($this->f3->get('POST.isSpecial')) ? 1 : 0); // Get categoryId, accepted format: categoryId, coming format: category[id] $this->f3->set('POST.categoryId', $this->f3->get('POST.category[id]')); // Load data into memory and only update fields that have presence in POST $item->edit($id); $result[status] = 'success'; $result[success] = true; $result[message] = 'Successfully updated item'; } catch (Exception $e) { $result[message] = 'Update failed. Please try again later'; } header('Content-Type: application/json'); echo json_encode($result, JSON_NUMERIC_CHECK); exit; }
public function edit($id) { $this->load->library('jdf'); $this->load->helper('form'); $this->load->library('form_validation'); $this->load->library("session"); if ($this->form_validation->run('item/add') === FALSE) { $this->load->view("templates/errorForm.php", array("fields" => array('rating', 'isDone', 'task', 'date', 'endTime', 'startTime'))); } else { $item = new Item($id); $user = new User($this->session->getStudentId()); if ($this->input->post("isDone") == 1) { $state = ITEM_STATE_DONE; } else { $state = ITEM_STATE_UNDONE; } $start = makeTime($this->input->post("date"), $this->input->post("startTime")); $end = makeTime($this->input->post("date"), $this->input->post("endTime")); try { $item->edit($start, $end, $this->input->post("description"), $state, $this->session->getStudentId(), $this->input->post("rating"))->update(); $this->load->view("item/item_created.php", array("item" => $this->setFormat(array($item)))); } catch (Item_Overlap_With_Other_Item $e) { $this->load->view("item/item_error.php"); Item_Error::Item_Overlap_With_Other_Item(); } catch (Item_Create_With_Zero_Duration $e) { $this->load->view("item/item_error.php"); Item_Error::Item_Create_With_Zero_Duration(); } catch (Item_Feedback_Wrong $e) { $this->load->view("item/item_error.php"); Item_Error::Item_Feedback_Wrong(); } catch (Item_Start_Greater_Than_End_Exception $e) { $this->load->view("item/item_error.php"); Item_Error::Item_Start_Greater_Than_End_Exception(); } catch (Task_Not_Found $e) { $this->load->view("task/task_error.php"); Task_Error::Task_Not_Found(); } catch (Item_is_not_owned_to_this_user $e) { $this->load->view("item/item_error.php"); Item_Error::Item_is_not_owned_to_this_user(); } } }
public function update() { $item = new Item($this->db); if ($this->f3->exists('POST.update')) { $item->edit($this->f3->get('POST.tok')); //get id getIdByTok $getIdByTok = new Item($this->db); $getIdByTok->getIdByTok($this->f3->get('POST.tok')); $iid = $this->f3->get('ID.id'); //del tags $tgs = new Tag2Item($this->db); $tgs->getByItemId($iid); if (count($tgs->getByItemId($iid)) > 0) { foreach ($tgs->getByItemId($iid) as $t) { $itemid = $t['id']; $delti = new Tag2Item($this->db); $delti->delete($itemid); } } //update tags $tags = explode(',', $this->f3->get('POST.tags')); foreach ($tags as $t) { $t = trim($t); if ($t != '') { $this->f3->clear('TAGS'); $ifexists = new Tag($this->db); $ifexists->getByName(strtolower($t)); if ($this->f3->exists('TAGS.id')) { $tid = $this->f3->get('TAGS.id'); } else { //insert new tag $newtag = new Tag($this->db); $newtag->title = strtolower(preg_replace('|[^0-9a-z \\-\\/+]|', '', $t)); $newtag->label = preg_replace('|[^0-9A-Za-z \\-\\/+]|', '', $t); $newtag->url = toUrl($t); //get unique tok $utok = new Tag($this->db); $randtok = rand(100000000, 999999999); while ($utok->tagcountByTok($randtok) > 0) { $randtok = rand(100000000, 999999999); } $newtag->tok = $randtok; $newtag->add(); $tid = $newtag->_id; } //add to Tag2Item $t2i = new Tag2Item($this->db); //insert lastinsertedid $t2i->tid = $tid; $t2i->iid = $iid; $t2i->add(); } } $this->f3->set('COOKIE.message', 'The bookmark has been successfully saved!'); $this->f3->set('COOKIE.messagetype', 'alert-success hide5s'); $this->f3->reroute('/'); } else { $item->getById($this->f3->get('PARAMS.tok')); $this->f3->set('item', $item); $this->f3->set('active', $this->f3->get('PARAMS.tok')); $this->f3->set('page_head', 'Update Item'); //template $this->f3->set('view', 'item/update.htm'); $tgs = new TagList($this->db); $this->f3->set('tgs', $tgs->getitemtags($this->f3->get('POST.tok'))); $this->f3->set('ctrcount', count($tgs->getitemtags($this->f3->get('POST.tok')))); //menu $this->f3->set('topmenu', 'i'); //breadcrumbs $this->f3->set('breadcrumb', array(array("url" => NULL, "name" => "Update bookmark"))); } }