Exemplo n.º 1
0
 /**
  * Store a setting
  *
  * @return Response
  */
 public function store()
 {
     if (!Input::has('setting')) {
         return new JSend('error', (array) Input::all(), 'Tidak ada data setting.');
     }
     $errors = new MessageBag();
     DB::beginTransaction();
     //1. Validate StoreSetting Parameter
     $setting = Input::get('setting');
     if (is_null($setting['id'])) {
         $is_new = true;
     } else {
         $is_new = false;
     }
     //2. Validate setting parameter
     //2a. Slider
     if (!$errors->count() && $setting['type'] == 'slider') {
         $setting_data = \App\Models\Slider::findornew($setting['id']);
         $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"', 'ended_at' => 'date_format:"Y-m-d H:i:s"|after:started_at'];
         $validator = Validator::make($setting, $setting_rules);
     } elseif (!$errors->count() && in_array($setting['type'], ['about_us', 'why_join', 'term_and_condition'])) {
         $setting_data = \App\Models\StorePage::findornew($setting['id']);
         $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"'];
         $validator = Validator::make($setting, $setting_rules);
     } elseif (!$errors->count() && in_array($setting['type'], ['url', 'logo', 'facebook_url', 'twitter_url', 'instagram_url', 'email', 'phone', 'address', 'bank_information'])) {
         $setting_data = \App\Models\Store::findornew($setting['id']);
         $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"'];
         $validator = Validator::make($setting, $setting_rules);
     } else {
         $setting_data = \App\Models\Policy::findornew($setting['id']);
         $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"'];
         $validator = Validator::make($setting, $setting_rules);
     }
     if (!$validator->passes()) {
         $errors->add('StoreSetting', $validator->errors());
     } else {
         //if validator passed, save setting
         $setting_data = $setting_data->fill($setting);
         if (!$setting_data->save()) {
             $errors->add('StoreSetting', $setting_data->getError());
         }
     }
     //3. save image for slider
     if (!$errors->count() && isset($setting['images']) && is_array($setting['images']) && $setting_data['type'] == 'slider') {
         $image_current_ids = [];
         foreach ($setting['images'] as $key => $value) {
             if (!$errors->count()) {
                 $image_data = \App\Models\Image::findornew($value['id']);
                 $image_rules = ['thumbnail' => 'required|max:255', 'image_xs' => 'required|max:255', 'image_sm' => 'required|max:255', 'image_md' => 'required|max:255', 'image_lg' => 'required|max:255', 'is_default' => 'boolean'];
                 $validator = Validator::make($value, $image_rules);
                 //if there was image and validator false
                 if (!$validator->passes()) {
                     $errors->add('Image', $validator->errors());
                 } else {
                     $value['imageable_id'] = $setting_data['id'];
                     $value['imageable_type'] = get_class($setting_data);
                     $image_data = $image_data->fill($value);
                     if (!$image_data->save()) {
                         $errors->add('Image', $image_data->getError());
                     } else {
                         $image_current_ids[] = $image_data['id'];
                     }
                 }
             }
             //if there was no error, check if there were things need to be delete
             if (!$errors->count()) {
                 $images = \App\Models\Image::imageableid($setting['id'])->get(['id'])->toArray();
                 $image_should_be_ids = [];
                 foreach ($images as $key => $value) {
                     $image_should_be_ids[] = $value['id'];
                 }
                 $difference_image_ids = array_diff($image_should_be_ids, $image_current_ids);
                 if ($difference_image_ids) {
                     foreach ($difference_image_ids as $key => $value) {
                         $image_data = \App\Models\Image::find($value);
                         if (!$image_data->delete()) {
                             $errors->add('Image', $image_data->getError());
                         }
                     }
                 }
             }
         }
     }
     if ($errors->count()) {
         DB::rollback();
         return new JSend('error', (array) Input::all(), $errors);
     }
     DB::commit();
     if ($setting_data['type'] == 'slider') {
         $final_setting = \App\Models\Slider::id($setting_data['id'])->with(['images'])->first()->toArray();
     } else {
         $final_setting = \App\Models\StoreSetting::id($setting_data['id'])->first()->toArray();
     }
     return new JSend('success', (array) $final_setting);
 }