Ejemplo n.º 1
0
 public function postStore()
 {
     $id = \Input::get('id');
     /*
      * Validate
      */
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required|unique:modules,name' . (isset($id) ? ',' . $id : ''), 'short_description' => 'required', 'sku' => 'required|alpha_dash|unique:modules,sku' . (isset($id) ? ',' . $id : ''), 'category_id' => 'required', 'tags' => 'regex:/^[a-z,0-9 -]+$/i');
     $validation = \Validator::make(\Input::all(), $rules);
     if ($validation->passes()) {
         $name = \Input::get('name');
         $sku = \Input::get('sku');
         $short_description = \Input::get('short_description');
         $long_description = \Input::get('long_description');
         $image = \Input::file('image');
         $featured = \Input::get('featured') == '' ? FALSE : TRUE;
         $active = \Input::get('active') == '' ? FALSE : TRUE;
         $category_id = \Input::get('category_id');
         $tags = \Input::get('tags');
         $cn_name = \Input::get('cn_name');
         $cn_short_description = \Input::get('cn_short_description');
         $cn_long_description = \Input::get('cn_long_description');
         $options = array('name' => $cn_name, 'short_description' => $cn_short_description, 'long_description' => $cn_long_description);
         $module = isset($id) ? Module::find($id) : new Module();
         $module->name = $name;
         $module->sku = $sku;
         $module->short_description = $short_description;
         $module->long_description = $long_description;
         $module->featured = $featured;
         $module->active = $active;
         $module->options = json_encode($options);
         $module->category_id = $category_id;
         // Create or save changes
         $module->save();
         // Save pricelist
         foreach (Membership::all() as $membership) {
             $pricelist = Pricelist::where('module_id', $module->id)->where('membership_id', $membership->id)->first();
             if ($pricelist == null) {
                 $pricelist = new Pricelist();
                 $pricelist->module_id = $module->id;
                 $pricelist->membership_id = $membership->id;
             }
             $price = \Input::get('price_' . $membership->id);
             if (!empty($price)) {
                 $pricelist->price = $price;
                 $pricelist->save();
             } else {
                 // Empty price means to delete, but check that it is not used
                 $pricelist_used = UserPricelist::where('pricelist_id', $pricelist->id)->get();
                 if (count($pricelist_used) == 0) {
                     $pricelist->delete();
                 }
             }
         }
         // Save medias
         $media_checkbox = \Input::get('media_checkbox');
         if (isset($id)) {
             // Remove all existing medias
             $existing_medias = ModuleMediaMembership::where('module_id', $module->id)->get();
             foreach ($existing_medias as $remove_media) {
                 $remove_media->delete();
             }
         }
         if (is_array($media_checkbox)) {
             foreach ($media_checkbox as $check) {
                 $media_pair = explode('_', $check);
                 $media_id = $media_pair[0];
                 $membership_id = $media_pair[1];
                 $modMediaMembership = new ModuleMediaMembership();
                 $modMediaMembership->module_id = $module->id;
                 $modMediaMembership->membership_id = $membership_id;
                 $modMediaMembership->media_id = $media_id;
                 $modMediaMembership->save();
             }
         }
         if (!empty($tags)) {
             // Delete old tags
             $module->deleteAllTags();
             // Save tags
             foreach (explode(',', $tags) as $tagName) {
                 $newTag = new Tag();
                 $tagName_trim = trim($tagName);
                 // Trim space from beginning and end
                 $newTag->name = strtolower($tagName_trim);
                 $module->tags()->save($newTag);
             }
         }
         if (\Input::hasFile('image')) {
             // Delete all existing images for edit
             if (isset($id)) {
                 $module->deleteAllImages();
             }
             //set the name of the file
             $originalFilename = $image->getClientOriginalName();
             $filename = $sku . \Str::random(20) . '.' . \File::extension($originalFilename);
             //Upload the file
             $isSuccess = $image->move('assets/img/modules', $filename);
             if ($isSuccess) {
                 // create photo
                 $newimage = new Image();
                 $newimage->path = $filename;
                 // save photo to the loaded model
                 $module->images()->save($newimage);
             }
         }
     } else {
         if (isset($id)) {
             return \Redirect::to('admin/modules/edit/' . $id)->withErrors($validation)->withInput();
         } else {
             return \Redirect::to('admin/modules/create')->withErrors($validation)->withInput();
         }
     }
     return \Redirect::to('admin/modules');
 }