public function delete() { // Remove original file $filename = $this->path; $this->deleteFiles($filename); $imagine = new RImage(); // Remove all dimenions $defaultDimensions = \Config::get('redminportal::image.dimensions'); foreach (array_keys($defaultDimensions) as $key) { $filename_variant = $imagine->getUrl($filename, $key); $this->deleteFiles($filename_variant); } return parent::delete(); }
/** * Run the migrations. * Upgrade images table from Redminportal 0.1 to 0.2/0.3 * * @return void */ public function up() { // Check if the images table exists if (Schema::hasTable('images')) { if (Schema::hasColumn('images', 'imageable_type')) { $images = DB::select('select * from images'); foreach ($images as $image) { // Get the model name $new_type_array = explode('\\', $image->imageable_type); $last_model = array_pop($new_type_array); // Move image to new folder $image_folder = Config::get('redminportal::image.upload_dir'); switch ($last_model) { case 'Category': $move_file = $this->moveFileNewToOld($image_folder, $image, 'categories'); break; case 'Media': $move_file = $this->moveFileNewToOld($image_folder, $image, 'medias'); break; case 'Module': $move_file = $this->moveFileNewToOld($image_folder, $image, 'modules'); break; case 'Promotion': $move_file = $this->moveFileNewToOld($image_folder, $image, 'promotions'); break; default: $move_file = false; break; } // Create dimensions if ($move_file) { $img_helper = new RImage(); $img_helper->createDimensions($move_file); } // Points to new namespace Redooor\\Redminportal\\App\\Models\\ $new_type = 'Redooor\\Redminportal\\App\\Models\\' . $last_model; DB::table('images')->where('id', $image->id)->update(['imageable_type' => $new_type, 'path' => $move_file ? $move_file : $image->path]); } } } }
public function postStore() { $sid = \Input::get('id'); $validation = Announcement::validate(\Input::all()); if ($validation->passes()) { $title = \Input::get('title'); $content = \Input::get('content'); $image = \Input::file('image'); $private = \Input::get('private') == '' ? false : true; $announcement = isset($sid) ? Announcement::find($sid) : new Announcement(); if ($announcement == null) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('editError', "The Announcement cannot be found because it does not exist or may have been deleted."); return redirect('/admin/announcements')->withErrors($errors); } $announcement->title = $title; $announcement->content = $content; $announcement->private = $private; $announcement->save(); if (\Input::hasFile('image')) { //Upload the file $helper_image = new RImage(); $filename = $helper_image->upload($image, 'announcements/' . $announcement->id, true); if ($filename) { // create photo $newimage = new Image(); $newimage->path = $filename; // save photo to the loaded model $announcement->images()->save($newimage); } } //if it validate } else { if (isset($sid)) { return redirect('admin/announcements/edit/' . $sid)->withErrors($validation)->withInput(); } else { return redirect('admin/announcements/create')->withErrors($validation)->withInput(); } } return redirect('admin/announcements'); }
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'); }
public function postStore() { $sid = \Input::get('id'); /* * Validate */ $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->passes()) { $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'); $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; } $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 it validate } else { if (isset($sid)) { return redirect('admin/posts/edit/' . $sid)->withErrors($validation)->withInput(); } else { return redirect('admin/posts/create')->withErrors($validation)->withInput(); } } return redirect('admin/posts'); }
public function postStore() { $sid = \Input::get('id'); $return_path = isset($sid) ? 'admin/categories/edit/' . $sid : 'admin/categories/create'; /* * Validate */ $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required', 'short_description' => 'required', 'order' => 'required|min:0'); $validation = \Validator::make(\Input::all(), $rules); if ($validation->fails()) { return redirect($return_path)->withErrors($validation)->withInput(); } $name = \Input::get('name'); $short_description = \Input::get('short_description'); $long_description = \Input::get('long_description'); $image = \Input::file('image'); $active = \Input::get('active') == '' ? false : true; $order = \Input::get('order'); $parent_id = \Input::get('parent_id'); $category = isset($sid) ? Category::find($sid) : new Category(); if ($category == null) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('editError', "The category cannot be found because it does not exist or may have been deleted."); return redirect('/admin/categories')->withErrors($errors); } // Check if there's an existing category with the same name under the same parent if (isset($sid)) { $checkSameName = Category::where('name', $name)->where('category_id', $parent_id == 0 ? null : $parent_id)->whereNotIn('id', [$sid])->count(); } else { $checkSameName = Category::where('name', $name)->where('category_id', $parent_id == 0 ? null : $parent_id)->count(); } if ($checkSameName > 0) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('nameError', "The category cannot be added because there's an existing category with the same name."); return redirect($return_path)->withErrors($errors)->withInput(); } $category->name = $name; $category->short_description = $short_description; $category->long_description = $long_description; $category->active = $active; $category->order = $order; // Check if parent_id is equal to this->id. If 0, save as null $category->category_id = $sid == $parent_id ? null : ($parent_id == 0 ? null : $parent_id); $category->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 = $category->translations->where('lang', $lang)->first(); if ($translated_model == null) { $translated_model = new Translation(); } $translated_model->lang = $lang; $translated_model->content = json_encode($translated_content); $category->translations()->save($translated_model); } if (\Input::hasFile('image')) { //Upload the file $helper_image = new RImage(); $filename = $helper_image->upload($image, 'categories/' . $category->id, true); if ($filename) { // create photo $newimage = new Image(); $newimage->path = $filename; // save photo to the loaded model $category->images()->save($newimage); } } return redirect('admin/categories'); }
public function postStore() { $sid = \Input::get('id'); /* * Validate */ $rules = array('image' => 'mimes:jpg,jpeg,png,gif|max:500', 'name' => 'required', 'short_description' => 'required', 'category_id' => 'required|numeric|min:1'); $messages = ['category_id.min' => 'The category field is required.']; $validation = \Validator::make(\Input::all(), $rules, $messages); if ($validation->passes()) { $name = \Input::get('name'); $short_description = \Input::get('short_description'); $long_description = \Input::get('long_description'); $image = \Input::file('image'); $active = \Input::get('active') == '' ? false : true; $category_id = \Input::get('category_id'); $portfolio = isset($sid) ? Portfolio::find($sid) : new Portfolio(); if ($portfolio == null) { $errors = new \Illuminate\Support\MessageBag(); $errors->add('editError', "The portfolio cannot be found because it does not exist or may have been deleted."); return redirect('/admin/portfolios')->withErrors($errors); } $portfolio->name = $name; $portfolio->short_description = $short_description; $portfolio->long_description = $long_description; $portfolio->active = $active; $portfolio->category_id = $category_id; $portfolio->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 = $portfolio->translations->where('lang', $lang)->first(); if ($translated_model == null) { $translated_model = new Translation(); } $translated_model->lang = $lang; $translated_model->content = json_encode($translated_content); $portfolio->translations()->save($translated_model); } if (\Input::hasFile('image')) { //Upload the file $helper_image = new RImage(); $filename = $helper_image->upload($image, 'portfolios/' . $portfolio->id, true); if ($filename) { // create photo $newimage = new Image(); $newimage->path = $filename; // save photo to the loaded model $portfolio->images()->save($newimage); } } //if it validate } else { if (isset($sid)) { return redirect('admin/portfolios/edit/' . $sid)->withErrors($validation)->withInput(); } else { return redirect('admin/portfolios/create')->withErrors($validation)->withInput(); } } return redirect('admin/portfolios'); }
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); }
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'); }
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'); }
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'); }