public function newAction() { if ($this->request->isPost()) { $userSession = $this->session->get("userSession"); $thing = new Things(); $thing->created = date('Y-m-d H:i:s'); $thing->modified = date('Y-m-d H:i:s'); $thing->member_id = $userSession['id']; $thing->name = $this->request->getPost('name'); $thing->details = $this->request->getPost('details'); $thing->price = $this->request->getPost('price'); $thing->thing_condition_id = $this->request->getPost('thing_condition_id'); $thing->thing_category_id = $this->request->getPost('thing_category_id'); if ($thing->create()) { $id = $thing->id; $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>New item has been posted'); return $this->response->redirect('thing/view/' . $id); } } $thingConditions = ThingConditions::find(); $this->view->setVar('thingConditions', $thingConditions); $thingCategories = ThingCategories::find(); $this->view->setVar('thingCategories', $thingCategories); }
public function updateAction($id = null) { if ($this->request->isPost()) { $imageCheckpoint = array(); $userSession = $this->session->get("userSession"); $id = $this->request->getPost('id'); $details = $this->request->getPost('details'); $price = str_replace(',', '', $this->request->getPost('price')); $condition = $this->request->getPost('thing_condition_id'); $category = $this->request->getPost('thing_category_id'); $thing = Things::findFirst($id); $thing->modified = date('Y-m-d H:i:s'); $thing->member_id = $userSession['id']; $thing->name = $this->request->getPost('name'); $thing->details = $details; $thing->thing_category_id = $category; $thing->thing_condition_id = $condition; //$this->view->disable(); if ($thing->update()) { for ($i = 1; $i <= 7; $i++) { $fileId = $this->request->getPost('fileId' . $i); if (!empty($fileId)) { $eventPhotos = ThingPhotos::findFirst($fileId); $eventPhotos->created = date('Y-m-d H:i:s'); $eventPhotos->modified = date('Y-m-d H:i:s'); $eventPhotos->member_id = $userSession['id']; $eventPhotos->caption = $this->request->getPost('caption' . $i); $eventPhotos->update(); } $imageCheckpoint[] = $fileId; } $this->view->disable(); print_r($imageCheckpoint); $params = implode("','", $imageCheckpoint); $robots = ThingPhotos::query()->where("id NOT IN (:params:)")->bind(array("params" => $params))->execute(); foreach ($robots as $toBeDeletedImg) { $trashImg = 'img/thing/' . $toBeDeletedImg->filename; unlink($trashImg); $d = ThingPhotos::findFirst($toBeDeletedImg->id); $d->delete(); } if ($this->request->hasFiles() == true) { //ini_set('upload_max_filesize', '64M'); set_time_limit(1200); $uploads = $this->request->getUploadedFiles(); $isUploaded = false; #do a loop to handle each file individually foreach ($uploads as $upload) { $imageName = $upload->getKey(); //$this->request->getPost('image'.$ctr); $inputRow = str_replace('image', '', $imageName); $fileId = $this->request->getPost('fileId' . $inputRow); #define a “unique” name and a path to where our file must go $fileName = $upload->getName(); $fileInfo = new SplFileInfo($fileName); $fileExt = $fileInfo->getExtension(); $fileExt = strtolower($fileExt); $newFileName = substr(md5(uniqid(rand(), true)), 0, 10) . date('ymdhis') . '.' . $fileExt; //$fileExt = $upload->getExtension(); $fileImageExt = array('jpeg', 'jpg', 'png'); $fileType = ''; $filePath = ''; $path = ''; if (in_array($fileExt, $fileImageExt)) { $path = 'img/thing/' . $newFileName; $recentPhotos[] = 'img/thing/' . $newFileName; $filePath = 'img/thing/'; //$fileType = 'Image'; } #move the file and simultaneously check if everything was ok $upload->moveTo($path) ? $isUploaded = true : ($isUploaded = false); if ($isUploaded) { if (!empty($fileId)) { $realtyPhotos = ThingPhotos::findFirst($fileId); $realtyPhotos->modified = date('Y-m-d H:i:s'); $realtyPhotos->member_id = $userSession['id']; $realtyPhotos->file_path = $filePath; $realtyPhotos->filename = $newFileName; $realtyPhotos->update(); } else { $realtyPhotos = new ThingPhotos(); $realtyPhotos->created = date('Y-m-d H:i:s'); $realtyPhotos->modified = date('Y-m-d H:i:s'); $realtyPhotos->member_id = $userSession['id']; $realtyPhotos->thing_id = $id; $realtyPhotos->file_path = $filePath; $realtyPhotos->filename = $newFileName; $realtyPhotos->caption = $this->request->getPost('caption' . $inputRow); $realtyPhotos->create(); } } } #if any file couldn't be moved, then throw an message // $realtyPhotos = RealtyPhotos::find('realty_id ="'.$id.'"'); // foreach ($realtyPhotos as $photo) { // if (in_array($photo->filename, $recentPhotos)) { // $check = "okay"; // } else { // if (unlink('img/realty/'. $photo->filename)) { // $check = "okay"; // } else { // $this->view->disable(); // echo "unable to remove image from img folder."; // } // } // } } //$this->view->disable(); return $this->response->redirect('thing/view/' . $id); } // thing update } // is post //$this->view->disable(); $thing = Things::findFirst($id); if (!$thing) { return $this->response->redirect('thing/index'); } $this->view->setVar('thing', $thing); $thingPhotos = ThingPhotos::find('thing_id =' . $id); $this->view->setVar('thingPhotos', $thingPhotos); $thingConditions = ThingConditions::find(); $this->view->setVar('thingConditions', $thingConditions); $thingCategories = ThingCategories::find(); $this->view->setVar('thingCategories', $thingCategories); }