예제 #1
0
 public function postAdQuick()
 {
     $data = post();
     $default = array('category_id' => '', 'type_id' => 2, 'address' => '', 'lat' => '', 'lng' => '', 'price' => 0, 'area' => 0);
     $merge = array_merge($default, $data);
     $merge = \DLNLab\Classified\Classes\HelperClassified::trim_value($merge);
     extract($merge);
     $user = Auth::getUser();
     // Kiem tra user hien tai da > 3 ad draft chua
     $counts = Ad::whereRaw('user_id = ? AND status = 0', array($user->id))->count();
     if ($counts >= CLF_LIMIT_AD_PRIVATE) {
         return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'not_create_ad')), 500);
     }
     $rules = ['category_id' => 'required|numeric|min:1', 'type_id' => 'required|numeric|min:1', 'address' => 'required', 'price' => 'required|numeric', 'area' => 'numeric'];
     $error = HelperClassified::valid($rules);
     if ($error != null) {
         return Response::json(array('status' => 'error', 'message' => $error), 500);
     }
     // Format lai tag ids
     if ($tag_ids) {
         foreach ($tag_ids as $id) {
             if (!empty($id) || intval($id) > 0 || !in_array($id, $tags)) {
                 $tags[] = $id;
             }
         }
         if (!count($tags)) {
             return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'not_create_ad')), 500);
         }
     }
     $name = Ad::gen_auto_ad_name($merge);
     $slug = HelperClassified::slug_utf8($name);
     try {
         DB::beginTransaction();
         $record = new Ad();
         $record->name = $name;
         $record->slug = $slug;
         $record->address = $address;
         $record->category_id = $category_id;
         $record->type_id = $type_id;
         $record->price = $price;
         $record->lat = $lat;
         $record->lng = $lng;
         $record->save();
         // Them vao bang ads_tags
         /*$arr_insert = array();
           if (! empty($tags)) {
               foreach ($tags as $id) {
                   $arr_insert[] = array('ad_id' => $record->id, 'tag_id' => $id);
               }
               DB::table('dlnlab_classified_ads_tags')->insert($arr_insert);
           }*/
         if ($area) {
             $record = new AdInfor();
             $record->area = $area;
             $record->save();
         }
         DB::commit();
         return Response::json(array('status' => 'success', 'data' => $record->id));
     } catch (Exception $ex) {
         DB::rollback();
         return Response::json(array('status' => 'error', 'message' => $error), 500);
     }
 }