public function updateAction($id = null)
 {
     if ($this->request->isPost()) {
         $id = $this->request->getPost('id');
         $countryId = $this->request->getPost('country_id');
         $country = Countries::findFirst(array('columns' => '*', 'conditions' => 'id LIKE :id:', 'bind' => array('id' => $countryId)));
         $countryName = '';
         if ($country) {
             $countryName = $country->country;
         }
         $address = str_replace(' ', '+', $this->request->getPost('street') . '+' . $this->request->getPost('city') . '+' . $countryName);
         $userSession = $this->session->get("userSession");
         $content = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=' . $address . '&key=AIzaSyAbpLPfBH8sNdVSzMULD_BZN9qrAqbL3V8');
         $json = json_decode($content, true);
         $lat = $json['results'][0]['geometry']['location']['lat'];
         $lng = $json['results'][0]['geometry']['location']['lng'];
         //error_log('LAT : '.$lat.' LONG : '.$lng);
         $property = Realties::findFirst($id);
         $property->modified = date('Y-m-d H:i:s');
         $property->member_id = $userSession['id'];
         $property->name = $this->request->getPost('name');
         $property->details = $this->request->getPost('details');
         $property->price = str_replace(',', '', $this->request->getPost('price'));
         $property->street = $this->request->getPost('street');
         $property->city = $this->request->getPost('city');
         $property->country_id = $this->request->getPost('country_id');
         $property->lat = $lat;
         $property->lng = $lng;
         $property->realty_condition_id = $this->request->getPost('realty_condition_id');
         $property->realty_category_id = $this->request->getPost('realty_category_id');
         $property->realty_type_id = $this->request->getPost('realty_type_id');
         $property->house_area = $this->request->getPost('house_area');
         $property->lot_area = $this->request->getPost('lot_area');
         $property->bedrooms = $this->request->getPost('bedrooms');
         $property->bathrooms = $this->request->getPost('bathrooms');
         $property->parking = $this->request->getPost('parking');
         $property->pets_allowed = $this->request->getPost('pets_allowed');
         if ($property->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)) {
                     $realtyPhotos = RealtyPhotos::findFirst($fileId);
                     $realtyPhotos->created = date('Y-m-d H:i:s');
                     $realtyPhotos->modified = date('Y-m-d H:i:s');
                     $realtyPhotos->member_id = $userSession['id'];
                     $realtyPhotos->caption = $this->request->getPost('caption' . $i);
                     $realtyPhotos->update();
                 }
             }
             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/realty/' . $newFileName;
                         $filePath = 'img/realty/';
                         //$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 = RealtyPhotos::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 RealtyPhotos();
                             $realtyPhotos->created = date('Y-m-d H:i:s');
                             $realtyPhotos->modified = date('Y-m-d H:i:s');
                             $realtyPhotos->member_id = $userSession['id'];
                             $realtyPhotos->realty_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
             }
             return $this->response->redirect('real_estate/view/' . $id);
         }
     }
     $property = Realties::findFirst($id);
     if (!$property) {
         return $this->response->redirect('real_estate/index');
     }
     $this->view->setVar('property', $property);
     $realtyPhotos = RealtyPhotos::find('realty_id ="' . $id . '"');
     $this->view->setVar('realtyPhotos', $realtyPhotos);
     $countries = Countries::find();
     $this->view->setVar('countries', $countries);
     $realtyConditions = RealtyConditions::find();
     $this->view->setVar('realtyConditions', $realtyConditions);
     $realtyTypes = RealtyTypes::find();
     $this->view->setVar('realtyTypes', $realtyTypes);
     $realtyCategories = RealtyCategories::find();
     $this->view->setVar('realtyCategories', $realtyCategories);
 }
 public function newAction()
 {
     if ($this->request->isPost()) {
         $countryId = $this->request->getPost('country_id');
         $country = Countries::findFirst(array('columns' => '*', 'conditions' => 'id LIKE :id:', 'bind' => array('id' => $countryId)));
         $countryName = '';
         if ($country) {
             $countryName = $country->country;
         }
         $address = str_replace(' ', '+', $this->request->getPost('street') . '+' . $this->request->getPost('city') . '+' . $countryName);
         $userSession = $this->session->get("userSession");
         $content = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=' . $address . '&key=AIzaSyAbpLPfBH8sNdVSzMULD_BZN9qrAqbL3V8');
         $json = json_decode($content, true);
         $lat = $json['results'][0]['geometry']['location']['lat'];
         $lng = $json['results'][0]['geometry']['location']['lng'];
         //error_log('LAT : '.$lat.' LONG : '.$lng);
         $business = new Business();
         $business->created = date('Y-m-d H:i:s');
         $business->modified = date('Y-m-d H:i:s');
         $business->member_id = $userSession['id'];
         $business->name = $this->request->getPost('name');
         $business->website = $this->request->getPost('website');
         $business->telephone = $this->request->getPost('telephone');
         $business->street = $this->request->getPost('street');
         $business->city = $this->request->getPost('city');
         $business->country_id = $this->request->getPost('country_id');
         $business->lat = $lat;
         $business->lng = $lng;
         $business->business_category_id = $this->request->getPost('business_category_id');
         if ($business->create()) {
             $id = $business->id;
             $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>New business has been created');
             if (!empty($this->request->getPost('review'))) {
                 $review = new Reviews();
                 $review->created = date('Y-m-d H:i:s');
                 $review->modified = date('Y-m-d H:i:s');
                 $review->member_id = $userSession['id'];
                 $review->business_id = $business->id;
                 $review->content = $this->request->getPost('review');
                 $review->rate = $this->request->getPost('rate');
                 if ($review->create()) {
                     $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>You\'re review has been submitted.');
                 }
             }
             return $this->response->redirect('business/view/' . $id);
         }
     }
     $countries = Countries::find();
     $this->view->setVar('countries', $countries);
     $realtyConditions = RealtyConditions::find();
     $this->view->setVar('realtyConditions', $realtyConditions);
     $realtyTypes = RealtyTypes::find();
     $this->view->setVar('realtyTypes', $realtyTypes);
     $realtyCategories = RealtyCategories::find();
     $this->view->setVar('realtyCategories', $realtyCategories);
 }