Exemple #1
0
 public function putAd($id)
 {
     $data = post();
     try {
         DB::beginTransaction();
         $default = array('name' => '', 'slug' => '', 'description' => '', 'price' => '', 'address' => '', 'category_id' => '', 'type_id' => '', 'tag_ids' => '', 'lat' => '', 'lng' => '', 'step' => 0, 'action' => '');
         $merge = array_merge($default, $data);
         $merge = HelperClassified::trim_value($merge);
         extract($merge);
         switch ($action) {
             case 'desc':
                 $rules = ['name' => 'required', 'description' => 'required', 'category_id' => 'required|numeric|min:1', 'type_id' => 'required|numeric|min:1', 'price' => 'required|numeric', 'step' => ''];
                 break;
             case 'location':
                 $rules = ['address' => 'required', 'lat' => 'required', 'lng' => 'required'];
                 break;
             case 'property':
                 $rules = ['tag_ids' => 'array'];
                 break;
             default:
                 $rules = ['name' => 'required', 'description' => 'required', 'category_id' => 'required|numeric|min:1', 'type_id' => 'required|numeric|min:1', 'address' => 'required', 'price' => 'required|numeric', 'lat' => 'required', 'lng' => 'required', 'tag_ids' => 'array', 'area' => 'numeric'];
                 break;
         }
         $error = HelperClassified::valid($rules);
         if ($error != null) {
             return Response::json(array('status' => 'error', 'message' => $error), 500);
         }
         $record = Ad::find($id);
         if (empty($record) || $record->user_id != Auth::getUser()->id) {
             return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'error_user')), 500);
         }
         $lat = is_float($lat) ? floatval($lat) : '';
         $lng = is_float($lng) ? floatval($lng) : '';
         if (empty($lat) && empty($lng)) {
             $record->name = str_limit($name, $limit = 125);
             $record->slug = empty($slug) ? HelperClassified::slug_utf8($name) : $slug;
             $record->description = str_limit($description, $limit = 500);
             $record->price = preg_replace("/[^0-9]/", "", $price);
             $record->address = $address;
             $record->category_id = intval($category_id);
             $record->type_id = intval($type_id);
             $record->lat = $lat;
             $record->lng = $lng;
             $record->save();
         }
         if (!$record->validate()) {
             $message = $record->errors()->all();
             return Response::json(array('status' => 'error', 'message_array' => $message), 500);
         }
         $city = '';
         $state = '';
         if ($lat && $lng && ($lat != $record->lat && $lng != $record->lng)) {
             $response = @file_get_contents(sprintf('https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&language=vi_VN', $lat, $lng));
             $json_resp = json_decode($response);
             if (isset($json_resp->results[0]->address_components) && is_array($json_resp->results[0]->address_components)) {
                 $allow = true;
                 foreach ($json_resp->results[0]->address_components as $i => $component) {
                     if (!empty($component->types) && in_array('country', $component->types) && $component->short_name != 'VN') {
                         $allow = false;
                     }
                     if ($allow && !empty($component->types) && in_array('administrative_area_level_1', $component->types)) {
                         $city = $component->short_name;
                     }
                     if ($allow && !empty($component->types) && in_array('administrative_area_level_2', $component->types)) {
                         $state = $component->long_name;
                     }
                 }
             }
         }
         if ($record->id) {
             $default = array('ad_id' => $record->id, 'tag_ids' => $tag_ids, 'city_tag' => $city, 'state_tag' => $state);
             Tag::save_tag($default);
         }
         DB::commit();
         return Response::json(array('status' => 'success', 'data' => $record));
     } catch (Exception $ex) {
         DB::rollback();
         return Response::json(array('status' => 'error', 'message' => $ex->getMessage()), 500);
     }
 }
Exemple #2
0
 public function beforeSave()
 {
     // Check user id co hop le hay khong
     if ($this->getAttribute('user_id') && Auth::check()) {
         $user_id = $this->getAttribute('user_id');
         if ($user_id != Auth::getUser()->id) {
             throw new Exception('Tin này không phải của người dùng hiện tại!', 500);
         }
     }
     // Format lai price
     $price = $this->getAttribute('price');
     $price = str_replace(',', '', $price);
     $price = doubleval($price);
     $this->setAttribute('price', $price);
     // Format user_id
     $user_id = $this->getAttribute('user_id');
     if (empty($user_id)) {
         $this->setAttribute('user_id', Auth::getUser()->id);
     }
     // Tao slug full_text va snippet content cho fulltext search
     $description = Str::words($this->getAttribute('description'));
     $slug = str_replace('-', ' ', $this->getAttribute('slug'));
     $slug_address = HelperClassified::slug_utf8($this->getAttribute('address'));
     $slug_address = str_replace('-', ' ', $slug_address);
     $arr_text = array();
     $arr_text[] = $this->getAttribute('name');
     $arr_text[] = $slug;
     $arr_text[] = $description;
     $arr_text[] = $this->getAttribute('address');
     $arr_text[] = $slug_address;
     $text = implode(' ', $arr_text);
     $this->setAttribute('full_text', $text);
     // Cap nhat publish time
     if ($this->getAttribute('status') == 1) {
         $this->setAttribute('published_at', time());
     }
     // Cap nhat trang thai link share count sns
     if (!empty($this->attributes['id'])) {
         AdShareCount::where('ad_id', '=', $this->attributes['id'])->update(array('status' => $this->getAttribute('status')));
     }
 }