예제 #1
0
파일: Promo.php 프로젝트: pr-of-it/profit
 public function actionSave()
 {
     if (!empty($_POST['promo'][PromoModel::PK])) {
         $item = PromoModel::findByPK($_POST['promo'][PromoModel::PK]);
     } else {
         $item = new PromoModel();
     }
     $item->fill($_POST['promo']);
     $item->save();
     $this->redirect('/admin/promo/');
 }
예제 #2
0
 public function postSubmit()
 {
     if (Input::get('_action') == 'addProcess') {
         $validator = Validator::make(array('Title (EN)' => Input::get('title_en'), 'Title (ID)' => Input::get('title_id'), 'Photo' => Input::file('image'), 'File' => Input::file('file_location')), array('Title (EN)' => 'required', 'Title (ID)' => 'required', 'Photo' => 'image|max:2048', 'File' => 'required'));
         if ($validator->fails()) {
             return Redirect::route('admin.promo.add')->withErrors($validator)->withInput();
         }
         $promo = new Promo();
         $promo->title_en = Input::get('title_en');
         $promo->title_id = Input::get('title_id');
         $promo->content_id = Input::get('content_id');
         $promo->content_en = Input::get('content_en');
         $promo->file_name = Input::get('file_name');
         if (!file_exists($this->upload_path)) {
             mkdir($this->upload_path, 0775, true);
         }
         if (!is_null(Input::file('image'))) {
             $file = Input::file('image');
             if ($file->isValid()) {
                 if (!empty($promo->picture)) {
                     File::delete($promo->picture);
                 }
                 $extension = $file->getClientOriginalExtension();
                 $img = Image::make($file->getRealPath());
                 $img->resize(480, 480, function ($constraint) {
                     $constraint->aspectRatio();
                 });
                 $img->interlace();
                 $name = 'promo_' . uniqid();
                 $fileName = $this->upload_path . Str::slug($name) . '.' . $extension;
                 $img->save($fileName);
                 $promo->picture = $fileName;
             }
         }
         if (!is_null(Input::file('file_location'))) {
             $file = Input::file('file_location');
             if ($file->isValid()) {
                 if (!empty($promo->file_location)) {
                     File::delete($promo->file_location);
                 }
                 $extension = $file->getClientOriginalExtension();
                 //$name = 'promo_'.uniqid();
                 $name = $file->getClientOriginalName();
                 $fileName = Str::slug($name) . '.' . $extension;
                 $file->move($this->upload_path, $fileName);
                 $promo->file_location = $this->upload_path . $fileName;
             }
         }
         $promo->save();
         if (!$promo->id) {
             throw new \Exception('Promo insert error');
         }
     } elseif (Input::get('_action') == 'editProcess') {
         if (Input::has('id')) {
             $validator = Validator::make(array('Title (EN)' => Input::get('title_en'), 'Title (ID)' => Input::get('title_id'), 'Photo' => Input::file('image')), array('Title (EN)' => 'required', 'Title (ID)' => 'required', 'Photo' => 'image|max:2048'));
             if ($validator->fails()) {
                 return Redirect::route('admin.promo.edit', array('id' => Input::get('id')))->withErrors($validator)->withInput();
             }
             $promo = Promo::find(Input::get('id'));
             if (empty($promo->file_location)) {
                 $validator = Validator::make(array('File' => Input::file('file_location')), array('File' => 'required'));
                 if ($validator->fails()) {
                     return Redirect::route('admin.promo.edit', array('id' => Input::get('id')))->withErrors($validator)->withInput();
                 }
             }
             $promo->title_en = Input::get('title_en');
             $promo->title_id = Input::get('title_id');
             $promo->content_id = Input::get('content_id');
             $promo->content_en = Input::get('content_en');
             $promo->file_name = Input::get('file_name');
             if (!file_exists($this->upload_path)) {
                 mkdir($this->upload_path, 0775, true);
             }
             if (!is_null(Input::file('image'))) {
                 $file = Input::file('image');
                 if ($file->isValid()) {
                     if (!empty($promo->picture)) {
                         File::delete($promo->picture);
                     }
                     $extension = $file->getClientOriginalExtension();
                     $img = Image::make($file->getRealPath());
                     $img->resize(480, 480, function ($constraint) {
                         $constraint->aspectRatio();
                     });
                     $img->interlace();
                     $name = 'promo_' . uniqid();
                     $fileName = $this->upload_path . Str::slug($name) . '.' . $extension;
                     $img->save($fileName);
                     $promo->picture = $fileName;
                 }
             }
             if (!is_null(Input::file('file_location'))) {
                 $file = Input::file('file_location');
                 if ($file->isValid()) {
                     if (!empty($promo->file_location)) {
                         File::delete($promo->file_location);
                     }
                     $extension = $file->getClientOriginalExtension();
                     //$name = 'promo_'.uniqid();
                     $name = $file->getClientOriginalName();
                     $fileName = Str::slug($name) . '.' . $extension;
                     $file->move($this->upload_path, $fileName);
                     $promo->file_location = $this->upload_path . $fileName;
                 }
             }
             $promo->save();
             if ($promo->active) {
                 $this->clearCache();
             }
             if (!$promo->id) {
                 throw new \Exception('Promo insert error');
             }
         }
     }
     return Redirect::route('admin.promo');
 }