/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(['scenario' => 'create']); $model->date = date('Y-m-d H:i'); if ($model->load(Yii::$app->request->post())) { $model->image = UploadedFile::getInstance($model, 'image'); $model->images = UploadedFile::getInstances($model, 'images'); if ($model->validate() && $model->save() && $model->image) { // Working directory $dir = Yii::getAlias('@frontend/web/uploads/product/' . $model->id); FileHelper::createDirectory($dir); // Save main image $model->image->saveAs($dir . '/main.jpg'); // Save images if ($model->images) { foreach ($model->images as $image) { $imageModel = new Image(); $imageModel->model_id = $model->id; $imageModel->save(); $image->saveAs($dir . '/' . $imageModel->id . '.jpg'); } } } return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', ['model' => $model]); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->session->setFlash('success', 'Товар успешно создан!'); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function actionCreate() { $model = new Product(); if ($model->load(\Yii::$app->request->post())) { if ($model->validate() && $model->save()) { return $this->redirect(['index']); } } return $this->render('form', ['model' => $model]); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $categories = Category::find()->all(); $model = new Product(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'categories' => $categories]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); $categoryModel = new CategoryProduct(); $treeParents = TreeHelper::build($categoryModel->find()->addOrderBy('tree')->addOrderBy('lft')->all()); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'treeParents' => $treeParents]); } }
public function insert(ProductDto $productDto) { $product = new Product(); if ($product->load(['Product' => ArrayHelper::toArray($productDto)])) { $product->type = ArrayUtil::arrayToInt($productDto->type); $product->save(); return ArrayHelper::toArray($product); } else { return []; } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $currUser = Yii::$app->user->getIdentity(); $currMb = MerchantBrand::find()->where(['merchant_brand_id' => $currUser->username])->one(); $mbFk = $currMb->_id; $model = new Product(); $model->merchant_brand_fk = $mbFk; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => (string) $model->_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { //if(!Yii::$app->user->can('createYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth')); $model = new Product(); $model->loadDefaultValues(); if ($model->load(Yii::$app->request->post())) { $model->type = ProductType::arrayToInt($model->type); if ($model->save()) { return $this->redirect(['update', 'id' => $model->id]); } } return $this->render('create', ['model' => $model]); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if (!is_numeric($model['agreement_id'])) { $agreement = Agreement::find()->where(["default_flag" => 1])->asArray()->one(); $model['agreement_id'] = $agreement['id']; } if ($model->load(Yii::$app->request->post()) && $model->save()) { $redirect = yii::$app->request->post('goto') == 'list' ? ['index'] : ['update', 'id' => $model->id]; return $this->redirect($redirect); } return $this->render('create', ['model' => $model]); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); $categoryModel = new ProductCategory(); $companyModel = new ProductCompany(); $treeParents = TreeHelper::build($categoryModel->find()->addOrderBy('tree')->addOrderBy('lft')->all()); $treeCompany = ArrayHelper::map($companyModel->findAll(['status' => 10]), 'id', 'title'); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('create', ['model' => $model, 'treeParents' => $treeParents, 'treeCompany' => $treeCompany]); } }
/** * Экшен '/admin/catalog/products' - список товаров * @return string * @throws InvalidParamException */ public function actionProducts() { $filterModel = new Product(); $filterModel->scenario = 'search'; $filterModel->load(Yii::$app->request->get()); $dataProvider = $filterModel->search(); if ($filterModel->parents === 0) { $category = new Category(); $category->title = 'Все товары'; } else { $category = Category::findOne($filterModel->parents); } return $this->render('products', ['category' => $category, 'dataProvider' => $dataProvider, 'filterModel' => $filterModel]); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post())) { $imageName = $model->product_name; $path = Yii::getAlias('@common') . '/web/uploads/'; $model->file = UploadedFile::getInstance($model, 'file'); $model->file->saveAs($path . $imageName . '.' . $model->file->extension); $model->product_main_picture = $path . $imageName . '.' . $model->file->extension; $model->admin_id = Yii::$app->user->getId(); $model->product_created_date = date('Y-m-d'); $model->save(); return $this->redirect(['view', 'id' => $model->product_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); $parents = []; $root = Category::findOne(['parent_id' => null, 'title' => 'product', 'module' => 'product']); if (empty($root)) { throw new NotFoundHttpException('Module không tồn tại'); } $parents = $root->children()->all(); $parent_id = $this->buildTree($parents); if ($model->load(Yii::$app->request->post())) { $model['slug'] = $this->slugAlias($model); if ($model->save()) { return $this->redirect(['index']); } } else { return $this->render('create', ['model' => $model, 'parent_id' => $parent_id, 'statestock' => $this->getStateStock(), 'hotline' => $this->getHotline(), 'state' => $this->getStatus()]); } }
/** * Creates a new EntpProduct model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post())) { $model->loadDefaultValues(); $model->save(); if ($model->save()) { $produk = new EntpProduct(); $produk->product_id = $this->id; $produk->entrepreneur_user_id = Yii::$app->user->id; $produk->link('product', $model); } Yii::$app->session->setFlash('success', 'Create New Product Success'); return $this->redirect(['index']); //return $this->redirect(['view', 'entrepreneur_id' => $model->entrepreneur_id, 'product_id' => $model->product_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Product(); if ($model->load(Yii::$app->request->post())) { $model->file = UploadedFile::getInstance($model, 'file'); if ($model->validate()) { $filename = 'uploads/' . md5($model->file->baseName) . '.' . $model->file->extension; if ($model->file->saveAs($filename)) { $model->image = Url::to($filename, true); $model->save(false); foreach ($model->categories as $categoryId) { $category = Category::findOne($categoryId); $model->link('categories', $category); } return $this->redirect(['view', 'id' => $model->id]); } } } return $this->render('create', ['model' => $model]); }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { // echo Yii::getAlias('@common'); exit; $model = new Product(); $modelProdMeta = new ProductMeta(); $stores = []; if (\Yii::$app->user->identity->id == 1) { $stores = \common\models\UserStore::find()->select('store_id')->where(['user_id' => Yii::$app->user->identity->id])->all(); $stores = \yii\helpers\ArrayHelper::getColumn($stores, 'store_id'); $storeList = \common\models\Store::find()->where(['IN', 'store_id', $stores])->all(); } else { $storeList = \common\models\Store::find()->all(); } if ($postData = Yii::$app->request->post()) { if ($model->load($postData) && $model->save()) { $prod_id = $model->prod_id; foreach ($model->cat_id as $cat) { $catModel = new ProdCat(); $catModel->cat_id = $cat; $catModel->prod_id = $prod_id; $catModel->save(); } $file = UploadedFile::getInstances($model, 'images'); $model->file = $file[0]; $image_path = Yii::getAlias('@frontend') . '/web/uploads/product/' . $model->file->baseName . '_' . $prod_id . '.' . $model->file->extension; $model->file->saveAs($image_path); $this->updateProductMeta($id, 'prod_img', $model->file->baseName . '_' . $prod_id . '.' . $model->file->extension); foreach ($postData['PMeta'] as $key => $meta_value) { $this->updateProductMeta($id, $key, $meta_value); } return $this->redirect(['view', 'id' => $model->prod_id]); } } else { return $this->render('create', ['model' => $model, 'modelProdMeta' => $modelProdMeta, 'storeList' => $storeList]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $product = new Product(['scenario' => 'product-create']); $ingredients = [new Ingredients()]; if ($product->load(Yii::$app->request->post())) { $ingredients = Model::createMultiple(Ingredients::classname()); Model::loadMultiple($ingredients, Yii::$app->request->post()); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validateMultiple($ingredients), ActiveForm::validate($product)); } $product->seen = 0; $product->sold = 0; $product->user_id = 0; // change to admin id or user id $product->created_by = Yii::$app->user->identity->username; $product->created_date = date('Y-m-d h:m:s'); $product->modified_by = Yii::$app->user->identity->username; $product->modified_date = date('Y-m-d h:m:s'); $product->status = Status::STATUS_ACTIVE; // validate all models $valid = $product->validate(); foreach ($ingredients as $detail) { $fields = array('ingredient'); $valid = $detail->validate($fields) && $valid; } if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $product->save(false)) { foreach ($ingredients as $ingredient) { $ingredient->created_by = Yii::$app->user->identity->username; $ingredient->created_date = date('Y-m-d h:m:s'); $ingredient->modified_by = Yii::$app->user->identity->username; $ingredient->modified_date = date('Y-m-d h:m:s'); $ingredient->status = Status::STATUS_ACTIVE; $ingredient->product_id = $product->id; if (!($flag = $ingredient->save(false))) { $transaction->rollBack(); break; } } $product->files = UploadedFile::getInstances($product, 'files'); if ($product->files) { foreach ($product->files as $file) { $productPhoto = new ProductPhoto(); $imageName = $file->baseName . substr(md5(rand()), 0, 7); $productPhoto->caption = $imageName; $productPhoto->product_id = $product->id; $productPhoto->product_photo_order = 0; $productPhoto->created_by = Yii::$app->user->identity->username; $productPhoto->created_date = date('Y-m-d h:m:s'); $productPhoto->modified_by = Yii::$app->user->identity->username; $productPhoto->modified_date = date('Y-m-d h:m:s'); $productPhoto->status = Status::STATUS_ACTIVE; $productPhoto->image_path = 'uploads/product/' . $imageName . '.' . $file->extension; if (!($flag = $productPhoto->save(false))) { $transaction->rollBack(); break; } else { $file->saveAs('uploads/product/' . $imageName . '.' . $file->extension); } } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $product->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } else { return $this->render('create', ['product' => $product, 'ingredients' => empty($ingredients) ? [new Ingredients()] : $ingredients]); } }
/** * Creates a new Product model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed * @throws Exception * @throws \yii\base\Exception */ public function actionCreate() { //check for categories if (!Category::find()->active()->all()) { Yii::$app->session->setFlash('error', "You don't have any active categories to add product"); return $this->redirect('index'); } //check for brand if (!Brand::find()->all()) { Yii::$app->session->setFlash('error', "You don't have any brands to add product"); return $this->redirect('index'); } $model = new Product(); $model->scenario = $model::SCENARIO_CREATE; $productVideo = new ProductVideo(['scenario' => ProductVideo::SCENARIO_PRODUCT_ADD]); $videos = [$productVideo]; $productProperty = new ProductProperty(['scenario' => ProductProperty::SCENARIO_PRODUCT_ADD]); $properties = [$productProperty]; if ($model->load($post = Yii::$app->request->post())) { $properties = Model::createMultiple(ProductProperty::classname()); $videos = Model::createMultiple(ProductVideo::classname()); Model::setScenarios($properties, ProductProperty::SCENARIO_PRODUCT_ADD); Model::setScenarios($videos, ProductVideo::SCENARIO_PRODUCT_ADD); Model::loadMultiple($properties, Yii::$app->request->post()); Model::loadMultiple($videos, Yii::$app->request->post()); Model::validateDuplicates($properties, 'property_id'); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validate($model), ActiveForm::validateMultiple($properties, false), ActiveForm::validateMultiple($videos)); } if ($model->validate() && Model::validateMultiple($properties) && Model::validateMultiple($videos)) { $transaction = $model->getDb()->beginTransaction(); $model->main_photo = UploadedFile::getInstance($model, 'main_photo'); try { if (!($category = Category::findOne(['id' => $model->category_id]))) { throw new \yii\base\Exception(); } if ($model->save(false)) { Model::linkAll($model, $properties, 'productProperties'); Model::unsetEmpty($videos, 'video'); Model::linkAll($model, $videos, 'videos'); $model->savePhotos(); $model->upload(); $transaction->commit(); Yii::$app->session->setFlash('success', 'Product have been created.'); } else { $transaction->rollBack(); Yii::$app->session->setFlash('error', 'Failed to create product.'); } return $this->redirect('index'); } catch (Exception $e) { Yii::$app->session->setFlash('error', 'Failed to create product.'); $transaction->rollBack(); return $this->redirect('index'); } } } $model->clearTmpPhotos(); $model->setAttribute('active', true); return $this->render('create', ['model' => $model, 'videos' => $videos, 'properties' => $properties]); }