/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ProductImage::find(); if (!empty($params['product_id'])) { $query->where(['product_id' => $params['product_id']]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'product_id' => $this->product_id]); $query->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'image_path', $this->image_path])->andFilterWhere(['like', 'color_code', $this->color_code]); return $dataProvider; }
/** * function ::create ($data) */ public static function create($data) { $now = strtotime('now'); $username = Yii::$app->user->identity->username; $model = new ProductImage(); if ($model->load($data)) { if ($log = new UserLog()) { $log->username = $username; $log->action = 'Create'; $log->object_class = 'ProductImage'; $log->created_at = $now; $log->is_success = 0; $log->save(); } 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['productimage-image'])) { $copyResult = FileUtils::copyImage(['imageName' => $model->image, 'fromFolder' => Yii::$app->params['uploads_folder'], 'toFolder' => $targetFolder, 'resize' => array_values(ProductImage::$image_resizes), 'removeInputImage' => true]); if ($copyResult['success']) { $model->image = $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; }
/** * @return \yii\db\ActiveQuery */ public function getProductImages() { return $this->hasMany(ProductImage::className(), ['product_id' => 'id']); }
/** * Deletes an existing Product model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { if ($model = $this->findModel($id)) { foreach (ProductTranslation::findAll(['product_id' => $model->id]) as $item) { $item->delete(); } foreach (ProductImage::findAll(['product_id' => $model->id]) as $item) { $item->delete(); } foreach (ProductCategoryToProduct::findAll(['product_id' => $model->id]) as $item) { $item->delete(); } foreach (ProductCollectionToProduct::findAll(['product_id' => $model->id]) as $item) { $item->delete(); } $model->delete(); return $this->goBack(Url::previous()); } else { throw new NotFoundHttpException(); } }
/** * Finds the ProductImage model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ProductImage the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ProductImage::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function updateUpload() { $tokenImage = date("dmYHis") . bin2hex(openssl_random_pseudo_bytes(8)); if ($this->validate() and UploadedFile::getInstance($this, 'file')) { unlink('images/products/titles/' . $this->image); if ($this->file = UploadedFile::getInstance($this, 'file')) { $imageName = $this->file->baseName . '.' . $this->file->extension; $goodImage = $tokenImage . $imageName; $this->file->saveAs('images/products/titles/' . $goodImage); $this->image = $goodImage; } } // multiFiles upload if ($this->validate() and $this->multiFiles = UploadedFile::getInstances($this, 'multiFiles')) { foreach ($this->multiFiles as $file) { $goodImage = $tokenImage . $file->baseName . '.' . $file->extension; $file->saveAs('images/products/galleries/' . $goodImage); $image = new Image(); $image->name = $goodImage; $image->alt = $this->title; // $image->alt = $goodImage; $image->save(); $pi = new ProductImage(); $pi->product_id = $this->id; $pi->image_id = $image->id; $pi->save(); } } }