Пример #1
0
 public static function insert($data)
 {
     try {
         $props = ['code' => Model_Service_Util::gen_code(), 'product_name' => $data['product_name'], 'product_description' => $data['product_description'], 'product_info' => $data['product_info'], 'status' => 2, 'created_at' => date('Y-m-d H:i:s', Date::forge()->get_timestamp())];
         $new = Model_Product::forge($props);
         $new->save();
         return $new->id;
     } catch (Exception $e) {
         Log::write('ERROR', $e->getMessage());
         return false;
     }
 }
Пример #2
0
 /**
  * Creates a new product.
  *
  * @param string        $name   The name of the product.
  * @param Model_Seller  $seller The seller the product belongs to.
  * @param array         $data   Optional data.
  *
  * @return Model_Product
  */
 public static function create($name, Model_Seller $seller, array $data = array())
 {
     $product = Model_Product::forge();
     $product->name = $name;
     $product->seller = $seller;
     $product->populate($data);
     try {
         $product->save();
     } catch (FuelException $e) {
         Log::error($e);
         return false;
     }
     Service_Event::trigger('product.create', $product->seller, $product->to_array());
     return $product;
 }
Пример #3
0
 public function action_addProduct()
 {
     $validator = $this->addModifyValidator();
     $message = "";
     $categories_db = Model_Category::find('all');
     foreach ($categories_db as $category) {
         $categories[$category->id] = $category->name;
     }
     $category_id = Input::post('category_id');
     $description = Input::post('description');
     $image = Input::post('image');
     $doit = Input::post('doit');
     if (!is_null($doit)) {
         try {
             if (!$validator->run(Input::post())) {
                 throw new Exception();
             }
             $valid = (object) $validator->validated();
             $product = Model_Product::forge();
             $product->name = $valid->name;
             $product->price = $valid->price;
             $product->category_id = $category_id;
             $product->description = $description;
             $product->image = $image;
             $product->save();
             return Response::redirect("/home/productInfo/{$product->id}");
             /* 
                 if (strlen($name) < 3) {
                   throw new Exception("name must have at least 3 chars");
                 }
                 if (!preg_match($pattern, $price)) {
                   throw new Exception("illegal price format");
                 }*/
         } catch (Database_Exception $ex) {
             // this gets the message without the extra info
             list(, , $message) = Database_Connection::instance()->error_info();
         } catch (Exception $ex) {
             $message = $ex->getMessage();
         }
     }
     $data = ['message' => $message, 'categories' => $categories];
     $view = View::forge('admin/addProduct.tpl', $data);
     $view->set('validator', $validator, false);
     return Response::forge($view);
 }
 public function action_create()
 {
     if (Input::method() == 'POST') {
         $val = Model_Product::validate('create');
         if ($val->run()) {
             $product = Model_Product::forge(array('description' => Input::post('description'), 'barcode' => Input::post('barcode'), 'short_code' => Input::post('short_code'), 'cost_price' => Input::post('cost_price'), 'retail_price' => Input::post('retail_price'), 'is_taxable' => Input::post('is_taxable'), 'discontinued' => Input::post('discontinued'), 'picture' => Input::post('picture'), 'product_details' => Input::post('product_details')));
             if ($product and $product->save()) {
                 Session::set_flash('success', e('Added product #' . $product->id . '.'));
                 Response::redirect('admin/products');
             } else {
                 Session::set_flash('error', e('Could not save product.'));
             }
         } else {
             Session::set_flash('error', $val->error());
         }
     }
     $this->template->title = "Products";
     $this->template->content = View::forge('admin/products/create');
 }
Пример #5
0
 public function action_create()
 {
     \View::set_global('title', 'Add New');
     if (\Input::post()) {
         $val = Model_Product::validate('create');
         // Upload image and display errors if there are any
         $image = $this->upload_image();
         if (!$image['exists'] && \Config::get('details.image.required', false) && empty($item->image)) {
             // No previous images and image is not selected and it is required
             \Messages::error('<strong>There was an error while trying to upload product image</strong>');
             \Messages::error('You have to select image');
         } elseif ($image['errors']) {
             \Messages::error('<strong>There was an error while trying to upload product image</strong>');
             foreach ($image['errors'] as $error) {
                 \Messages::error($error);
             }
         }
         if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false))) {
             // Get POST values
             $insert = \Input::post();
             // Prepare some values
             //				$insert['published_at'] = !empty($insert['published_at']) ? strtotime($insert['published_at']) : \Date::forge()->get_timestamp();
             $insert['active_from'] = !empty($insert['active_from']) ? strtotime($insert['active_from']) : NULL;
             $insert['active_to'] = !empty($insert['active_to']) ? strtotime($insert['active_to']) : NULL;
             if ($insert['status'] != 2) {
                 unset($insert['active_from']);
                 unset($insert['active_to']);
             }
             $item = Model_Product::forge($insert);
             try {
                 $item->save();
                 // Validate and insert product slug into SEO database table
                 $val_seo = Model_Product_Seo::validate('create_seo');
                 $insert_seo = array('content_id' => $item->id, 'slug' => \Inflector::friendly_title($item->title, '-', true));
                 while (!$val_seo->run($insert_seo)) {
                     $insert_seo['slug'] = \Str::increment($insert_seo['slug'], 1, '-');
                 }
                 $item_seo = Model_Product_Seo::forge($insert_seo);
                 $item_seo->save();
                 // END OF: SEO
                 // Insert product categories
                 foreach ($insert['cat_ids'] as $parent_category) {
                     $item_categories = Model_Product_To_Categories::forge(array('product_id' => $item->id, 'category_id' => $parent_category));
                     $item_categories->save();
                 }
                 /**  PROPERTY GROUPS  **/
                 // First delete old groups
                 $tmp_categories = \Product\Model_Product_To_Groups::find_by_product_id($item->id);
                 if ($tmp_categories) {
                     foreach ($tmp_categories as $tmp_cat) {
                         $tmp_cat->delete();
                     }
                 }
                 // Insert product property groups
                 if (isset($insert['group_ids'])) {
                     foreach ($insert['group_ids'] as $parent_group) {
                         $related = \Product\Model_Product_To_Groups::forge(array('group_id' => $parent_group, 'product_id' => $item->id));
                         $related->save();
                     }
                 }
                 /**  END OF: PROPERTY GROUPS  **/
                 /**  PRICING GROUPS  **/
                 // Insert product pricing group
                 if (\Input::post('group', false)) {
                     $pricing = \Product\Model_Product_To_Groups::forge(array('group_id' => \Input::post('group'), 'product_id' => $item->id));
                     $pricing->save();
                 }
                 /**  END OF: PRICING GROUPS  **/
                 // Insert product images
                 if ($this->_image_data) {
                     chmod($this->_image_data[0]['saved_to'] . $this->_image_data[0]['saved_as'], 0755);
                     chmod($this->_image_data[0]['saved_to'] . 'large/' . $this->_image_data[0]['saved_as'], 0755);
                     chmod($this->_image_data[0]['saved_to'] . 'medium/' . $this->_image_data[0]['saved_as'], 0755);
                     chmod($this->_image_data[0]['saved_to'] . 'thumbs/' . $this->_image_data[0]['saved_as'], 0755);
                     $item_image = array(array('id' => 0, 'data' => array('content_id' => $item->id, 'image' => $this->_image_data[0]['saved_as'], 'alt_text' => \Input::post('alt_text', ''), 'cover' => 1, 'sort' => 1)));
                     Model_Product::bind_images($item_image);
                 }
                 \Messages::success('Product successfully created.');
                 \Response::redirect(\Input::post('update', false) ? \Uri::create('admin/product/update/' . $item->id) : \Uri::create('admin/product/create/' . $item_categories->category_id));
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to create product</strong>');
                 // Uncomment lines below to show database errors
                 $errors = $e->getMessage();
                 \Messages::error($errors);
             }
         } else {
             if ($val->error() != array()) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to create product</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
         // Delete uploaded image if there is product saving error
         if (isset($this->_image_data)) {
             $this->delete_image($this->_image_data[0]['saved_as']);
         }
     }
     \Theme::instance()->set_partial('content', $this->view_dir . 'create');
 }