Esempio n. 1
0
 public function edit($id)
 {
     $this->respondTo('html', function () use($id) {
         $product = Product::find($id);
         if ($product == null) {
             $this->getResponse()->redirect('App\\Admin\\Controllers\\ProductController', 'index');
             return;
         }
         $categories = Category::findAll();
         $session = $this->getSession();
         $inputs = $session->getOldInputBag();
         $errors = $session->getErrorBag();
         $error_msgs = array_flatten($errors->all());
         $data = compact('error_msgs', 'product', 'inputs', 'errors', 'categories');
         $data['page_title'] = "{$product->name} - Edit";
         $data['method'] = 'edit';
         $this->render(new TwigView('admin/product/edit_main.html', $data));
     });
 }
Esempio n. 2
0
 /**
  * @throw Markzero\Http\Exception\ResourceNotFoundException
  *        Markzero\Validation\Exception\ValidationException
  */
 public static function update($id, ParameterBag $params)
 {
     $em = self::getEntityManager();
     $product = Product::find($id);
     self::validateParams($params, $product);
     if ($product === null) {
         throw new ResourceNotFoundException();
     }
     $product->name = $params->get('product[name]', '', true);
     $product->short_desc = $params->get('product[short_desc]', '', true);
     $product->description = $params->get('product[description]', '', true);
     $product->price = floatval($params->get('product[price]', 0, true));
     $product->updated_at = new \DateTime("now");
     $isbn10 = $params->get('product[isbn_10]', null, true);
     $isbn13 = $params->get('product[isbn_13]', null, true);
     $issn = $params->get('product[issn]', null, true);
     $product->updateBarcode(Barcode::ISBN_10, $isbn10);
     $product->updateBarcode(Barcode::ISBN_13, $isbn13);
     $product->updateBarcode(Barcode::ISSN, $issn);
     $catid = $params->get('product[category_id]', null, true);
     $cat = Category::find($catid);
     if ($cat === null) {
         throw new ResourceNotFoundException('Category cannot be found');
     }
     $product->category = $cat;
     $em->persist($product);
     $em->flush();
     return $product;
 }
 function doDelete($id)
 {
     $this->respondTo('html', function () use($id) {
         $response = $this->getResponse();
         $flash = $this->getSession()->getFlashBag();
         try {
             Category::delete($id);
             $response->redirect('App\\Admin\\Controllers\\CategoryController', 'index');
         } catch (ResourceNotFoundException $e) {
             $flash->add('errors', "Cannot find Category #{$id}");
             $response->redirect('App\\Admin\\Controllers\\CategoryController', 'index');
         } catch (\Exception $e) {
             $flash->add('errors', 'Cannot delete Category #' . $id . ' (' . $e->getMessage() . ')');
             $response->redirect('App\\Admin\\Controllers\\CategoryController', 'index');
         }
     });
 }