Esempio n. 1
0
 public static function addTag($model, $name)
 {
     if ($model == null || empty($name)) {
         return false;
     }
     $checkTag = Tag::where('name', $name)->first();
     if ($checkTag) {
         $model->tags()->attach($checkTag);
     } else {
         $newTag = new Tag();
         $name_trimmed = trim($name);
         // Trim space from beginning and end
         $newTag->name = strtolower($name_trimmed);
         $model->tags()->save($newTag);
     }
     return true;
 }
 public function postStore()
 {
     $sid = Input::get('id');
     /*
      * Validate
      */
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required|unique:products,name' . (isset($sid) ? ',' . $sid : ''), 'short_description' => 'required', 'price' => 'numeric', 'sku' => 'required|alpha_dash|unique:products,sku' . (isset($sid) ? ',' . $sid : ''), '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');
         $price = Input::get('price');
         $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');
         $product = isset($sid) ? Product::find($sid) : new Product();
         if ($product == null) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('editError', "The product cannot be found because it does not exist or may have been deleted.");
             return redirect('/admin/products')->withErrors($errors);
         }
         $product->name = $name;
         $product->sku = $sku;
         $product->price = isset($price) ? $price : 0;
         $product->short_description = $short_description;
         $product->long_description = $long_description;
         $product->featured = $featured;
         $product->active = $active;
         $product->category_id = $category_id;
         $product->save();
         // Save translations
         $translations = \Config::get('redminportal::translation');
         foreach ($translations as $translation) {
             $lang = $translation['lang'];
             if ($lang == 'en') {
                 continue;
             }
             $translated_content = array('name' => \Input::get($lang . '_name'), 'short_description' => \Input::get($lang . '_short_description'), 'long_description' => \Input::get($lang . '_long_description'));
             // Check if lang exist
             $translated_model = $product->translations->where('lang', $lang)->first();
             if ($translated_model == null) {
                 $translated_model = new Translation();
             }
             $translated_model->lang = $lang;
             $translated_model->content = json_encode($translated_content);
             $product->translations()->save($translated_model);
         }
         if (!empty($tags)) {
             // Delete old tags
             $product->tags()->detach();
             // Save tags
             foreach (explode(',', $tags) as $tagName) {
                 Tag::addTag($product, $tagName);
             }
         }
         if (Input::hasFile('image')) {
             //Upload the file
             $helper_image = new RImage();
             $filename = $helper_image->upload($image, 'products/' . $product->id, true);
             if ($filename) {
                 // create photo
                 $newimage = new Image();
                 $newimage->path = $filename;
                 // save photo to the loaded model
                 $product->images()->save($newimage);
             }
         }
         //if it validate
     } else {
         if (isset($sid)) {
             return redirect('admin/products/edit/' . $sid)->withErrors($validation)->withInput();
         } else {
             return redirect('admin/products/create')->withErrors($validation)->withInput();
         }
     }
     return redirect('admin/products');
 }
Esempio n. 3
0
 public function postStore()
 {
     $sid = Input::get('id');
     $product_id = Input::get('product_id');
     /*
      * Validate
      */
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required|unique:products,name' . (isset($sid) ? ',' . $sid : ''), 'short_description' => 'required', 'price' => 'numeric', 'sku' => 'required|alpha_dash|unique:products,sku' . (isset($sid) ? ',' . $sid : ''), 'category_id' => 'required|numeric|min:1', 'tags' => 'regex:/^[a-z,0-9 -]+$/i', 'weight' => 'numeric', 'length' => 'numeric', 'width' => 'numeric', 'height' => 'numeric');
     $messages = ['category_id.min' => 'The category field is required.'];
     $validation = Validator::make(Input::all(), $rules, $messages);
     if ($validation->fails()) {
         $redirect_url = $this->pageRoute . '/create';
         if ($product_id and $sid) {
             $redirect_url = $this->pageRoute . '/edit-variant/' . $product_id . '/' . $sid;
         } elseif ($product_id and !$sid) {
             $redirect_url = $this->pageRoute . '/create-variant/' . $product_id;
         } elseif ($sid) {
             $redirect_url = $this->pageRoute . '/edit/' . $sid;
         }
         return redirect($redirect_url)->withErrors($validation)->withInput();
     }
     if ($product_id) {
         $parentProduct = Product::find($product_id);
         if (!$parentProduct) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('errorDeleteRecord', Lang::get('redminportal::messages.error_no_such_product'));
             return redirect($this->pageRoute)->withErrors($errors);
         }
     }
     $name = Input::get('name');
     $sku = Input::get('sku');
     $price = Input::get('price');
     $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');
     $weight_unit = Input::get('weight_unit');
     $volume_unit = Input::get('volume_unit');
     $weight = Input::get('weight');
     $length = Input::get('length');
     $width = Input::get('width');
     $height = Input::get('height');
     $product = isset($sid) ? Product::find($sid) : new Product();
     if ($product == null) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('errorNoSuchProduct', Lang::get('redminportal::messages.error_no_such_product'));
         return redirect($this->pageRoute)->withErrors($errors);
     }
     $product->name = $name;
     $product->sku = $sku;
     $product->price = isset($price) ? $price : 0;
     $product->short_description = $short_description;
     $product->long_description = $long_description;
     $product->featured = $featured;
     $product->active = $active;
     $product->category_id = $category_id;
     $product->weight_unit = $weight_unit;
     $product->volume_unit = $volume_unit;
     $product->weight = $weight != '' ? $weight : null;
     $product->length = $length != '' ? $length : null;
     $product->width = $width != '' ? $width : null;
     $product->height = $height != '' ? $height : null;
     $product->save();
     // Update category id of all variants
     foreach ($product->variants as $variant) {
         $variant->category_id = $category_id;
         $variant->save();
     }
     // Save translations
     $translations = \Config::get('redminportal::translation');
     foreach ($translations as $translation) {
         $lang = $translation['lang'];
         if ($lang == 'en') {
             continue;
         }
         $translated_content = array('name' => \Input::get($lang . '_name'), 'short_description' => \Input::get($lang . '_short_description'), 'long_description' => \Input::get($lang . '_long_description'));
         // Check if lang exist
         $translated_model = $product->translations->where('lang', $lang)->first();
         if ($translated_model == null) {
             $translated_model = new Translation();
         }
         $translated_model->lang = $lang;
         $translated_model->content = json_encode($translated_content);
         $product->translations()->save($translated_model);
     }
     if (!empty($tags)) {
         // Delete old tags
         $product->tags()->detach();
         // Save tags
         foreach (explode(',', $tags) as $tagName) {
             Tag::addTag($product, $tagName);
         }
     }
     if (Input::hasFile('image')) {
         //Upload the file
         $helper_image = new RImage();
         $filename = $helper_image->upload($image, 'products/' . $product->id, true);
         if ($filename) {
             // create photo
             $newimage = new Image();
             $newimage->path = $filename;
             // save photo to the loaded model
             $product->images()->save($newimage);
         }
     }
     // Link variant to parent Product
     if ($product_id) {
         // Only attach new variant
         if ($product_id and !$sid) {
             $parentProduct->variants()->attach($product->id);
         }
         return redirect($this->pageRoute . '/view-variant/' . $product->id);
     }
     return redirect($this->pageRoute);
 }
Esempio n. 4
0
 public function postStore()
 {
     $sid = \Input::get('id');
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'title' => 'required|regex:/^[a-z,0-9 ._\\(\\)-?]+$/i', 'slug' => 'required|regex:/^[a-z,0-9 ._\\(\\)-?]+$/i', 'content' => 'required');
     $validation = \Validator::make(\Input::all(), $rules);
     if ($validation->fails()) {
         return redirect('admin/posts/' . (isset($sid) ? 'edit/' . $sid : 'create'))->withErrors($validation)->withInput();
     }
     $title = \Input::get('title');
     $slug = \Input::get('slug');
     $content = \Input::get('content');
     $image = \Input::file('image');
     $private = \Input::get('private') == '' ? false : true;
     $featured = \Input::get('featured') == '' ? false : true;
     $category_id = \Input::get('category_id');
     $tags = \Input::get('tags');
     $post = isset($sid) ? Post::find($sid) : new Post();
     if ($post == null) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('editError', "The post cannot be found because it does not exist or may have been deleted.");
         return redirect('/admin/posts')->withErrors($errors);
     }
     $post->title = $title;
     $post->slug = str_replace(' ', '_', $slug);
     // Replace all space with underscore
     $post->content = $content;
     $post->private = $private;
     $post->featured = $featured;
     if ($category_id) {
         $post->category_id = $category_id;
     } else {
         $post->category_id = null;
     }
     $post->save();
     // Save translations
     $translations = \Config::get('redminportal::translation');
     foreach ($translations as $translation) {
         $lang = $translation['lang'];
         if ($lang == 'en') {
             continue;
         }
         $translated_content = array('title' => \Input::get($lang . '_title'), 'slug' => str_replace(' ', '_', \Input::get($lang . '_slug')), 'content' => \Input::get($lang . '_content'));
         // Check if lang exist
         $translated_model = $post->translations->where('lang', $lang)->first();
         if ($translated_model == null) {
             $translated_model = new Translation();
         }
         $translated_model->lang = $lang;
         $translated_model->content = json_encode($translated_content);
         $post->translations()->save($translated_model);
     }
     if (\Input::hasFile('image')) {
         //Upload the file
         $helper_image = new RImage();
         $filename = $helper_image->upload($image, 'posts/' . $post->id, true);
         if ($filename) {
             // create photo
             $newimage = new Image();
             $newimage->path = $filename;
             // save photo to the loaded model
             $post->images()->save($newimage);
         }
     }
     if (!empty($tags)) {
         // Delete old tags
         $post->tags()->detach();
         // Save tags
         foreach (explode(',', $tags) as $tagName) {
             Tag::addTag($post, $tagName);
         }
     }
     return redirect('admin/posts');
 }
 public function postStore()
 {
     $sid = \Input::get('id');
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required|unique:modules,name' . (isset($sid) ? ',' . $sid : ''), 'short_description' => 'required', 'sku' => 'required|alpha_dash|unique:modules,sku' . (isset($sid) ? ',' . $sid : ''), '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');
         $module = isset($sid) ? Module::find($sid) : new Module();
         if ($module == null) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('editError', "The module cannot be found because it does not exist or may have been deleted.");
             return \Redirect::to('/admin/modules')->withErrors($errors);
         }
         $module->name = $name;
         $module->sku = $sku;
         $module->short_description = $short_description;
         $module->long_description = $long_description;
         $module->featured = $featured;
         $module->active = $active;
         $module->category_id = $category_id;
         // Create or save changes
         $module->save();
         // Save translations
         $translations = \Config::get('redminportal::translation');
         foreach ($translations as $translation) {
             $lang = $translation['lang'];
             if ($lang == 'en') {
                 continue;
             }
             $translated_content = array('name' => \Input::get($lang . '_name'), 'short_description' => \Input::get($lang . '_short_description'), 'long_description' => \Input::get($lang . '_long_description'));
             // Check if lang exist
             $translated_model = $module->translations->where('lang', $lang)->first();
             if ($translated_model == null) {
                 $translated_model = new Translation();
             }
             $translated_model->lang = $lang;
             $translated_model->content = json_encode($translated_content);
             $module->translations()->save($translated_model);
         }
         // 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_active = \Input::get('price_active_' . $membership->id) == '' ? false : true;
             $price = \Input::get('price_' . $membership->id);
             if (!empty($price)) {
                 $pricelist->active = $price_active;
                 $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($sid)) {
             // 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->tags()->detach();
             // Save tags
             foreach (explode(',', $tags) as $tagName) {
                 Tag::addTag($module, $tagName);
             }
         }
         if (\Input::hasFile('image')) {
             //Upload the file
             $helper_image = new RImage();
             $filename = $helper_image->upload($image, 'modules/' . $module->id, true);
             if ($filename) {
                 // create photo
                 $newimage = new Image();
                 $newimage->path = $filename;
                 // save photo to the loaded model
                 $module->images()->save($newimage);
             }
         }
         //if it validate
     } else {
         if (isset($sid)) {
             return \Redirect::to('admin/modules/edit/' . $sid)->withErrors($validation)->withInput();
         } else {
             return \Redirect::to('admin/modules/create')->withErrors($validation)->withInput();
         }
     }
     return \Redirect::to('admin/modules');
 }
Esempio n. 6
0
 public function postStore()
 {
     $sid = \Input::get('id');
     if (isset($sid)) {
         $url = 'admin/bundles/edit/' . $sid;
     } else {
         $url = 'admin/bundles/create';
     }
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required|unique:products,name' . (isset($sid) ? ',' . $sid : ''), 'short_description' => 'required', 'price' => 'numeric', 'sku' => 'required|alpha_dash|unique:bundles,sku' . (isset($sid) ? ',' . $sid : ''), 'category_id' => 'required', 'tags' => 'regex:/^[a-z,0-9 -]+$/i');
     $validation = \Validator::make(\Input::all(), $rules);
     if ($validation->fails()) {
         return redirect($url)->withErrors($validation)->withInput();
     }
     // If id is set, check that it exists
     if (isset($sid)) {
         $bundle = Bundle::find($sid);
         if ($bundle == null) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('bundleError', "The bundle does not exist or may have been deleted.");
             return redirect('admin/bundles')->withErrors($errors);
         }
     }
     $name = Input::get('name');
     $sku = Input::get('sku');
     $price = Input::get('price');
     $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');
     $apply_to_models = array();
     $products = \Input::get('product_id');
     if (count($products) > 0) {
         foreach ($products as $item) {
             $model = Product::find($item);
             if ($model != null) {
                 $apply_to_models[] = $model;
             }
         }
     }
     $pricelists = \Input::get('pricelist_id');
     if (count($pricelists) > 0) {
         foreach ($pricelists as $item) {
             $model = Pricelist::find($item);
             if ($model != null) {
                 $apply_to_models[] = $model;
             }
         }
     }
     // In the worst scenario, all select items have been deleted
     if (count($apply_to_models) == 0) {
         $errors = new \Illuminate\Support\MessageBag();
         $errors->add('applyToError', "You have not selected any Product or Membership/Module under Bundled Items.");
         return redirect($url)->withErrors($errors)->withInput();
     }
     $newBundle = isset($sid) ? $bundle : new Bundle();
     $newBundle->name = $name;
     $newBundle->sku = $sku;
     $newBundle->price = isset($price) ? $price : 0;
     $newBundle->short_description = $short_description;
     $newBundle->long_description = $long_description;
     $newBundle->featured = $featured;
     $newBundle->active = $active;
     $newBundle->category_id = $category_id > 0 ? $category_id : null;
     $newBundle->save();
     // Remove all existing relationships first
     if (isset($sid)) {
         $newBundle->pricelists()->detach();
         $newBundle->products()->detach();
     }
     foreach ($apply_to_models as $apply_to_model) {
         $apply_to_model->bundles()->save($newBundle);
     }
     // Save translations
     $translations = \Config::get('redminportal::translation');
     foreach ($translations as $translation) {
         $lang = $translation['lang'];
         if ($lang == 'en') {
             continue;
         }
         $translated_content = array('name' => \Input::get($lang . '_name'), 'short_description' => \Input::get($lang . '_short_description'), 'long_description' => \Input::get($lang . '_long_description'));
         // Check if lang exist
         $translated_model = $newBundle->translations->where('lang', $lang)->first();
         if ($translated_model == null) {
             $translated_model = new Translation();
         }
         $translated_model->lang = $lang;
         $translated_model->content = json_encode($translated_content);
         $newBundle->translations()->save($translated_model);
     }
     if (!empty($tags)) {
         // Delete old tags
         $newBundle->tags()->detach();
         // Save tags
         foreach (explode(',', $tags) as $tagName) {
             Tag::addTag($newBundle, $tagName);
         }
     }
     if (Input::hasFile('image')) {
         //Upload the file
         $helper_image = new RImage();
         $filename = $helper_image->upload($image, 'bundles/' . $newBundle->id, true);
         if ($filename) {
             // create photo
             $newimage = new Image();
             $newimage->path = $filename;
             // save photo to the loaded model
             $newBundle->images()->save($newimage);
         }
     }
     return redirect('admin/bundles');
 }
Esempio n. 7
0
 public function getName()
 {
     $list = Tag::orderBy('name')->lists('name');
     return response()->json($list);
 }
 public function postStore()
 {
     $sid = \Input::get('id');
     /*
      * Validate
      */
     $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required|unique:medias,name' . (isset($sid) ? ',' . $sid : ''), 'short_description' => 'required', 'sku' => 'required|alpha_dash|unique:medias,sku' . (isset($sid) ? ',' . $sid : ''), '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');
         $media = isset($sid) ? Media::find($sid) : new Media();
         if ($media == null) {
             $errors = new \Illuminate\Support\MessageBag();
             $errors->add('editError', "The media cannot be found because it does not exist or may have been deleted.");
             return redirect('/admin/medias')->withErrors($errors);
         }
         $media->name = $name;
         $media->sku = $sku;
         $media->short_description = $short_description;
         $media->long_description = $long_description;
         $media->featured = $featured;
         $media->active = $active;
         if (isset($sid)) {
             // Check if category has changed
             if ($media->category_id != $category_id) {
                 $old_cat_folder = public_path() . '/assets/medias/' . $media->category_id . '/' . $sid;
                 $new_cat_folder = public_path() . '/assets/medias/' . $category_id . '/' . $sid;
                 // Create the directory
                 if (!file_exists($new_cat_folder)) {
                     mkdir($new_cat_folder, 0777, true);
                 }
                 // Copy existing media to new category
                 \File::copyDirectory($old_cat_folder, $new_cat_folder);
                 // Delete old media folder
                 \File::deleteDirectory($old_cat_folder);
             }
         } else {
             $media->path = 'broken.pdf';
         }
         $media->category_id = $category_id;
         // Create or save changes
         $media->save();
         // Save translations
         $translations = \Config::get('redminportal::translation');
         foreach ($translations as $translation) {
             $lang = $translation['lang'];
             if ($lang == 'en') {
                 continue;
             }
             $translated_content = array('name' => \Input::get($lang . '_name'), 'short_description' => \Input::get($lang . '_short_description'), 'long_description' => \Input::get($lang . '_long_description'));
             // Check if lang exist
             $translated_model = $media->translations->where('lang', $lang)->first();
             if ($translated_model == null) {
                 $translated_model = new Translation();
             }
             $translated_model->lang = $lang;
             $translated_model->content = json_encode($translated_content);
             $media->translations()->save($translated_model);
         }
         if (!empty($tags)) {
             // Delete old tags
             $media->tags()->detach();
             // Save tags
             foreach (explode(',', $tags) as $tagName) {
                 Tag::addTag($media, $tagName);
             }
         }
         if (\Input::hasFile('image')) {
             //Upload the file
             $helper_image = new RImage();
             $filename = $helper_image->upload($image, 'medias/' . $media->id, true);
             if ($filename) {
                 // create photo
                 $newimage = new Image();
                 $newimage->path = $filename;
                 // save photo to the loaded model
                 $media->images()->save($newimage);
             }
         }
         //if it validate
     } else {
         if (isset($sid)) {
             return redirect('admin/medias/edit/' . $sid)->withErrors($validation)->withInput();
         } else {
             return redirect('admin/medias/create')->withErrors($validation)->withInput();
         }
     }
     return redirect('admin/medias');
 }