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 newAction() { if ($this->request->isPost()) { $messages = $this->formValidate(); if (count($messages)) { $this->view->disable(); $errorMsg = ''; foreach ($messages as $msg) { $errorMsg .= $msg . "<br>"; } $this->flash->error('<button type="button" class="close" data-dismiss="alert">×</button>' . $errorMsg); return $this->response->redirect('thing/new/'); } $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->price = str_replace(',', '', $this->request->getPost('price')); $thing->thing_category_id = $this->request->getPost('thing_category_id'); $thing->thing_condition_id = $this->request->getPost('thing_condition_id'); $thing->details = $this->request->getPost('details'); if ($thing->create()) { $id = $thing->id; $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>New item has been posted'); if ($this->request->hasFiles() == true) { $uploads = $this->request->getUploadedFiles(); $isUploaded = false; $ctr = 1; # do a loop to handle each file individually foreach ($uploads as $upload) { # 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; $fileImageExt = array('jpeg', 'jpg', 'png'); $fileType = ''; $filePath = ''; $path = ''; if (in_array($fileExt, $fileImageExt)) { $path = '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) { $thingPhotos = new ThingPhotos(); $thingPhotos->created = date('Y-m-d H:i:s'); $thingPhotos->modified = date('Y-m-d H:i:s'); $thingPhotos->member_id = 1; $thingPhotos->thing_id = $id; $thingPhotos->file_path = $filePath; $thingPhotos->filename = $newFileName; $thingPhotos->caption = $this->request->getPost('caption' . $ctr); $ctr++; if ($thingPhotos->create()) { return $this->response->redirect('thing/view/' . $thing->id); } else { print_r($thingPhotos->getMessages()); } } } } } $this->view->disable(); } # get thing's categories and conditions $this->view->setVars(['thingConditions' => ThingConditions::find(), 'thingCategories' => ThingCategories::find()]); }