/** * * @param $action string * @param $productId int * @return nothing * @author Tremor */ public function productAction($action, $productId = 0) { if (isset($productId) && !empty($productId) && !is_numeric($productId)) { return Redirect::to('admin/product'); } switch ($action) { case 'add': $product = new Product(); $product->save(); $newId = $product->id; return Redirect::to('admin/product/' . $newId); break; case 'edit': $post = Input::except('_token'); $sizeList = Input::only('size'); $groupList = Input::only('group'); // product $product = Product::find($productId); foreach ($post as $key => $val) { if (isset($product->{$key})) { $product->{$key} = $key == 'alias' ? camel_case($val) : $val; } } $product->save(); if (empty($product->alias)) { $product->alias = camel_case($post['name']); $product->save(); } // product sizes if (!empty($sizeList['size'])) { foreach ($sizeList['size'] as $key => $val) { ProductSize::find($key)->update($val); } } // product groups if (!empty($groupList['group'])) { foreach ($groupList['group'] as $key => $val) { ProductGroup::find($key)->update($val); } } return Redirect::to('admin/product/' . $productId); break; case 'delete': $product = Product::find($productId); $product->delete(); $productSizeList = ProductSize::where('product_id', $productId)->get(); if ($productSizeList) { foreach ($productSizeList as $size) { $size->delete(); } } $productGroupList = ProductGroup::where('product_id', $productId)->get(); if ($productGroupList) { foreach ($productGroupList as $group) { $group->delete(); } } break; case 'addPriceSize': $this->data['productSize'] = ProductSize::create(array('product_id' => $productId)); $this->data['sizeList'] = Size::all(); $answer['content'] = View::make('admin.product.priceSizeTr')->with($this->data)->render(); return json_encode($answer); break; case 'removePriceSize': $priceSizeId = Input::get('priceSizeId', false); if ($priceSizeId != false) { ProductSize::find($priceSizeId)->delete(); return json_encode($answer['true'] = true); } break; case 'addGroup': $this->data['productGroup'] = ProductGroup::create(array('product_id' => $productId)); $this->data['groupList'] = Group::all(); $answer['content'] = View::make('admin.product.groupTr')->with($this->data)->render(); return json_encode($answer); break; case 'removeGroup': $groupId = Input::get('groupId', false); if ($groupId != false) { ProductGroup::find($groupId)->delete(); return json_encode($answer['true'] = true); } break; default: break; } return Redirect::to('admin/product'); }