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('event/new/');
         } else {
             $this->view->disable();
             echo "validation is either failed or passed";
         }
         $userSession = $this->session->get("userSession");
         $eventName = $this->request->getPost("name");
         $website = $this->request->getPost("website");
         $telephone = $this->request->getPost("telephone");
         $street = $this->request->getPost("street");
         $city = $this->request->getPost("city");
         $info = $this->request->getPost("event_info");
         $category = $this->request->getPost("category");
         $country = $this->request->getPost("country");
         $raw_address = $this->request->getPost("address");
         $latitude = $this->request->getPost("lat");
         $longitude = $this->request->getPost("lng");
         $dates = $this->request->getPost("date");
         $fromTimes = $this->request->getPost("fromTime");
         $toTimes = $this->request->getPost("toTime");
         $event = new Events();
         $event->created = date('Y-m-d H:i:s');
         $event->modified = date('Y-m-d H:i:s');
         $event->member_id = $userSession['id'];
         $event->name = $eventName;
         $event->event_date = $this->request->getPost("date");
         $event->website = $website;
         $event->telephone = $telephone;
         $event->street = $street;
         $event->city = $city;
         $event->country_id = $country;
         $event->event_category_id = $category;
         $event->lng = $longitude;
         $event->lat = $latitude;
         $event->eventinfo = $info;
         if ($event->create()) {
             $id = $event->id;
             $i = 0;
             foreach ($dates as $date) {
                 $eventDatetime = new EventDatetimes();
                 $eventDatetime->event_id = $id;
                 $eventDatetime->date = $date;
                 $eventDatetime->from_time = $fromTimes[$i];
                 $eventDatetime->to_time = $toTimes[$i];
                 $i++;
                 if (!$eventDatetime->create()) {
                     $this->view->disable();
                     echo "failed to insert event date and time in event_datetimes table. :(";
                 }
             }
             $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>New event has been posted');
             if ($this->request->hasFiles() == true) {
                 $uploads = $this->request->getUploadedFiles();
                 $isUploaded = false;
                 $ctr = 1;
                 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/event/' . $newFileName;
                         $filePath = 'img/event/';
                         //$fileType = 'Image';
                     }
                     # move the file and simultaneously check if everything was ok
                     $upload->moveTo($path) ? $isUploaded = true : ($isUploaded = false);
                     if ($isUploaded) {
                         $eventPhotos = new EventPhotos();
                         $eventPhotos->created = date('Y-m-d H:i:s');
                         $eventPhotos->modified = date('Y-m-d H:i:s');
                         $eventPhotos->member_id = $userSession['id'];
                         $eventPhotos->event_id = $id;
                         $eventPhotos->file_path = $filePath;
                         $eventPhotos->filename = $newFileName;
                         $eventPhotos->caption = $this->request->getPost('caption');
                         //$ctr++;
                         if ($eventPhotos->create()) {
                             echo "success";
                         } else {
                             $this->view->disable();
                             print_r($eventPhotos->getMessages());
                         }
                     }
                 }
             }
         } else {
             $this->view->disable();
             print_r($event->getMessages());
         }
         return $this->response->redirect('event/view/' . $id);
     }
     $events = Events::find();
     $categories = EventsCategories::find();
     $countries = Countries::find();
     $this->view->setVar('events', $events);
     $this->view->setVars(['events' => $events, 'categories' => $categories, 'countries' => $countries]);
 }
 public function new_eventAction()
 {
     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);
         $events = new Events();
         $events->created = date('Y-m-d H:i:s');
         $events->modified = date('Y-m-d H:i:s');
         $events->member_id = $userSession['id'];
         $events->name = $this->request->getPost('name');
         $events->website = $this->request->getPost('website');
         $events->telephone = $this->request->getPost('telephone');
         $events->street = $this->request->getPost('street');
         $events->city = $this->request->getPost('city');
         $events->country_id = $this->request->getPost('country_id');
         $events->lat = $lat;
         $events->lng = $lng;
         $events->event_date = $this->request->getPost('eventdate');
         $events->event_category_id = $this->request->getPost('event_category_id');
         $events->eventinfo = $this->request->getPost('eventinfo');
         if ($events->create()) {
             $id = $events->id;
             $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>New event 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->event_id = $events->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('events/view/' . $id);
         }
     }
     $countries = Countries::find();
     $this->view->setVar('countries', $countries);
     $eventsCategories = EventsCategories::find();
     $this->view->setVar('eventsCategories', $eventsCategories);
 }