public function addProduct($productName, $productBrief, $productLogo, $sellerId, $categoryId, $productStock, $productPrice, $productStatus) { $model = new Product(); $model->productName = $productName; $model->productBrief = $productBrief; $model->productLogo = $productLogo; $model->sellerId = $sellerId; $model->categoryId = $categoryId; $model->productStock = $productStock; $model->productPrice = $productPrice; $model->productStatus = $productStatus; if (in_array($productStatus, GlobalArray::$productStatusArray) == 1) { $model->onSaleTime = date('Y-m-d H:i:s', time()); } $model->addTime = date('Y-m-d H:i:s', time()); $model->addUser = Yii::$app->session->get(Variable::$session_userId_str); $model->productNo = 0; if ($model->save()) { $model->productNo = date('ymd', time()) . $sellerId . '' . $categoryId . '' . $model->id; if ($model->save()) { return true; } } return false; }
public function actionUpload_product() { if (Yii::$app->request->isAjax) { $model = new ProductCategory(); $Product = new Product(); $ProductImageRel = new ProductImageRel(); $ProductCategoryRel = new ProductCategoryRel(); $model_cat_title = UploadedFile::getInstance($model, 'cat_title'); $time = time(); $model_cat_title->saveAs('product_uploads/' . $time . $model_cat_title->baseName . '.' . $model_cat_title->extension); if ($model_cat_title) { $response = []; $Product->title = $_POST['title']; $Product->desc = $_POST['desc']; $Product->status = 1; if ($Product->save()) { $ProductCategoryRel->category_id = $_POST['id']; $ProductCategoryRel->product_id = $Product->id; $ProductImageRel->product_id = $Product->id; $ProductImageRel->image = $time . $model_cat_title->baseName . '.' . $model_cat_title->extension; if ($ProductCategoryRel->save() && $ProductImageRel->save()) { $response['files'][] = ['name' => $time . $model_cat_title->name, 'type' => $model_cat_title->type, 'size' => $model_cat_title->size, 'url' => Url::base() . '/product_uploads/' . $time . $model_cat_title->baseName . '.' . $model_cat_title->extension, 'deleteUrl' => Url::to(['delete_uploaded_file', 'file' => $model_cat_title->baseName . '.' . $model_cat_title->extension]), 'deleteType' => 'DELETE']; $response['base'] = $time . $model_cat_title->baseName; $response['view'] = $this->renderAjax('uploaded_product', ['url' => Url::base() . '/product_uploads/' . $time . $model_cat_title->baseName . '.' . $model_cat_title->extension, 'basename' => $time . $model_cat_title->baseName, 'id' => $ProductImageRel->id, 'model' => $Product]); } } else { $response['errors'] = $product->getErrors(); } return json_encode($response); } } }
/** * 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(['index']); } 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()) { //gets the instance of the uploaded file $ImageName = $model->name; $model->file = UploadedFile::getInstance($model, 'file'); $model->file->saveAs('uploads/' . $ImageName . '.' . $model->file->extension); //saves db column path $model->logo = 'uploads/' . $ImageName . '.' . $model->file->extension; $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()) { $imageName = $model->prod_id; $model->file = UploadedFile::getInstance($model, 'file'); if ($model->validate() && $model->file) { $model->file->saveAs('uploads/' . $imageName . '.' . $model->file->extension); $model->prod_pic = 'uploads/' . $imageName . '.' . $model->file->extension; $model->save(); } return $this->redirect(['view', 'id' => $model->prod_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * function ::create ($data) */ public static function create($data) { $now = strtotime('now'); $username = Yii::$app->user->identity->username; $model = new Product(); if ($model->load($data)) { if ($log = new UserLog()) { $log->username = $username; $log->action = "Create"; $log->object_class = "Product"; $log->created_at = $now; $log->is_success = 0; $log->save(); } $model->created_at = $now; $model->created_by = $username; $model->published_at = strtotime($model->published_at); do { $path = FileUtils::generatePath($now); } while (file_exists(Yii::$app->params['images_folder'] . $path)); $model->image_path = $path; $targetFolder = Yii::$app->params['images_folder'] . $model->image_path; $targetUrl = Yii::$app->params['images_url'] . $model->image_path; if (!empty($data['product-image'])) { $copyResult = FileUtils::copyImage(['imageName' => $model->image, 'fromFolder' => Yii::$app->params['uploads_folder'], 'toFolder' => $targetFolder, 'resize' => array_values(Product::$image_resizes), 'removeInputImage' => true]); if ($copyResult['success']) { $model->image = $copyResult['imageName']; } } if (!empty($data['product-banner'])) { $copyResult = FileUtils::copyImage(['imageName' => $model->banner, 'fromFolder' => Yii::$app->params['uploads_folder'], 'toFolder' => $targetFolder, 'removeInputImage' => true]); if ($copyResult['success']) { $model->banner = $copyResult['imageName']; } } if ($model->save()) { if ($log) { $log->object_pk = $model->id; $log->is_success = 1; $log->save(); } return $model; } $model->getErrors(); return $model; } return false; }
/** * 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(); $ProductCategoryRel = new ProductCategoryRel(); if ($model->load(Yii::$app->request->post()) && $ProductCategoryRel->load(Yii::$app->request->post())) { if ($model->save()) { $ProductCategoryRel->product_id = $model->id; if ($ProductCategoryRel->category_id == '') { $ProductCategoryRel->category_id = array(0); } if (Product::update_product_category_rel($ProductCategoryRel)) { return $this->redirect(['view', 'id' => $model->id]); } else { var_dump($ProductCategoryRel->getErrors()); } return $this->redirect(['view', 'id' => $model->id]); } else { var_dump($model->getErrors()); } } else { return $this->render('create', ['model' => $model, 'ProductCategoryRel' => $ProductCategoryRel]); } }