public function updateAction($id = null)
 {
     if ($this->request->isPost()) {
         $imageCheckpoint = array();
         $userSession = $this->session->get("userSession");
         $id = $this->request->getPost('id');
         $auto = Automotives::findFirst($id);
         $auto->name = $this->request->getPost("name");
         $auto->location = $this->request->getPost("location");
         $auto->details = $this->request->getPost("details");
         $auto->price = str_replace(',', '', $this->request->getPost('price'));
         $auto->brand = $this->request->getPost("brand");
         $auto->model = $this->request->getPost("model");
         $auto->year_model = $this->request->getPost("year_model");
         $auto->condition_id = $this->request->getPost("condition");
         $auto->body_type = $this->request->getPost("body_type");
         $auto->transmission = $this->request->getPost("transmission");
         $auto->mileage = $this->request->getPost("mileage");
         $auto->fuel_type_id = $this->request->getPost("fuel_type");
         if ($auto->update()) {
             $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>New property has been updated');
             for ($i = 1; $i <= 7; $i++) {
                 $fileId = $this->request->getPost('fileId' . $i);
                 if (!empty($fileId)) {
                     $autoPhotos = AutomotivePhotos::findFirst($fileId);
                     $autoPhotos->created = date('Y-m-d H:i:s');
                     $autoPhotos->modified = date('Y-m-d H:i:s');
                     $autoPhotos->member_id = $userSession['id'];
                     $autoPhotos->caption = $this->request->getPost('caption' . $i);
                     $autoPhotos->update();
                 }
                 $imageCheckpoint[] = $fileId;
             }
             $params = implode("','", $imageCheckpoint);
             $robots = AutomotivePhotos::query()->where("id NOT IN (:params:)")->bind(array("params" => $params))->execute();
             foreach ($robots as $toBeDeletedImg) {
                 $trashImg = 'img/realty/' . $toBeDeletedImg->filename;
                 unlink($trashImg);
                 $d = AutomotivePhotos::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/car/' . $newFileName;
                         $recentPhotos[] = 'img/car/' . $newFileName;
                         $filePath = 'img/car/';
                         //$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 = AutomotivePhotos::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 AutomotivePhotos();
                             $realtyPhotos->created = date('Y-m-d H:i:s');
                             $realtyPhotos->modified = date('Y-m-d H:i:s');
                             $realtyPhotos->member_id = $userSession['id'];
                             $realtyPhotos->automotive_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.";
                 //         }
                 //     }
                 // }
             }
             return $this->response->redirect('car_and_truck/view/' . $id);
         } else {
             $this->view->disable();
             print_r($auto->getMessages());
         }
     }
     //$this->view->disable();
     $auto = Automotives::findFirst($id);
     if (!$auto) {
         return $this->response->redirect('car_and_truck/index');
     }
     $this->view->setVar('auto', $auto);
     $autoPhotos = AutomotivePhotos::find("automotive_id = {$id}");
     $this->view->setVar('autoPhotos', $autoPhotos);
 }
Exemplo n.º 2
0
 public function searchAction()
 {
     $keyword = $this->request->get("find");
     $location = $this->request->get("near");
     $searchWords = '';
     $this->view->setVar('find', $keyword);
     $this->view->setVar('near', $near);
     $country = Countries::findFirst(array('columns' => '*', 'conditions' => 'country LIKE :country:', 'bind' => array('country' => $location)));
     $countryId = '';
     if ($country) {
         $countryId = $country->id;
     }
     $conditions = '';
     if (!empty($keyword)) {
         $conditions .= ' OR name LIKE :name:';
         $bind['name'] = '%' . $keyword . '%';
         $searchWords .= ', ' . $keyword;
     }
     if (!empty($location)) {
         $conditions .= ' OR street LIKE :street: OR city LIKE :city:';
         $bind['street'] = '%' . $location . '%';
         $bind['city'] = '%' . $location . '%';
         $searchWords .= ', ' . $location;
     }
     if (!empty($countryId)) {
         $conditions .= ' OR country_id = :country_id:';
         $bind['country_id'] = $countryId;
         $searchWords .= ', ' . $country->country;
     }
     $searchWords = substr($searchWords, 2);
     $business = Business::find(array('columns' => '*', 'conditions' => substr($conditions, 3), 'bind' => $bind));
     $this->view->setVar('business', $business);
     //CARS and TRUCKS
     $conditions = '';
     $bind = array();
     if (!empty($keyword)) {
         $conditions = ' OR name LIKE :name:';
         $bind['name'] = '%' . $keyword . '%';
     }
     if (!empty($keyword)) {
         $conditions .= ' OR brand LIKE :brand:';
         $bind['brand'] = '%' . $keyword . '%';
     }
     if (!empty($keyword)) {
         $conditions .= ' OR model LIKE :model:';
         $bind['model'] = '%' . $keyword . '%';
     }
     if (!empty($location)) {
         $conditions .= ' OR location LIKE :location:';
         $bind['location'] = $location;
     }
     $autos = Automotives::find(['columns' => '*', 'conditions' => substr($conditions, 3), 'bind' => $bind]);
     $this->view->setVar('autos', $autos);
     $conditions = '';
     $bind = array();
     if (!empty($keyword)) {
         $conditions = 'name LIKE :name:';
         $bind['name'] = '%' . $keyword . '%';
     }
     $things = Things::find(['columns' => '*', 'conditions' => $conditions, 'bind' => $bind]);
     $this->view->setVar('things', $things);
     //JOBS
     $conditions = '';
     $bind = array();
     if (!empty($keyword)) {
         $conditions .= ' OR position LIKE :position:';
         $bind['position'] = '%' . $keyword . '%';
         $searchWords .= ', ' . $keyword;
     }
     if (!empty($keyword)) {
         $conditions .= ' OR company LIKE :company:';
         $bind['company'] = '%' . $keyword . '%';
         $searchWords .= ', ' . $keyword;
     }
     if (!empty($location)) {
         $conditions .= ' OR street LIKE :street:';
         $bind['street'] = '%' . $location . '%';
         $conditions .= ' OR city LIKE :city:';
         $bind['city'] = '%' . $location . '%';
         $searchWords .= ', ' . $location;
     }
     if (!empty($countryId)) {
         $conditions .= ' OR country_id LIKE :country_id:';
         $bind['country_id'] = $countryId;
         $searchWords .= ', ' . $country->country;
     }
     $jobs = Jobs::find(array('columns' => '*', 'conditions' => substr($conditions, 3), 'bind' => $bind));
     $this->view->setVar('jobs', $jobs);
 }