public function search($params) { $query = ProductType::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'cat_id' => $this->cat_id]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'body', $this->body]); return $dataProvider; }
/** * Страница категории */ public function actionCat($id) { $query = Product::find()->joinWith('type')->where([ProductType::TableName() . '.cat_id' => $id]); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]); $productList = $query->offset($pages->offset)->limit($pages->limit)->with(['type'])->with(['organization'])->asArray()->all(); Product::format($productList); $randProduct = Product::getRandom(1); $randOrg = Organization::getRandom(1); $randSpec = Specialist::getRandom(1, [], ['specTypes']); return $this->render('cat', ['item' => ProductCat::find()->where(['id' => $id])->asArray()->one(), 'productList' => $productList, 'productPages' => $pages, 'randProduct' => $randProduct, 'randOrg' => $randOrg, 'randSpec' => $randSpec]); }
?> <p> <?php echo Html::a(Yii::t('app', 'Create ') . Yii::t('app', 'Product'), ['create'], ['class' => 'btn btn-success']); ?> <?php echo Html::a(Yii::t('app', 'Batch ') . Yii::t('app', 'Delete'), '#', ['class' => 'btn btn-danger', 'id' => 'batchDelete']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\CheckboxColumn'], 'id', ['attribute' => 'category_id', 'value' => function ($model) { return $model->category->name; }, 'filter' => Html::activeDropDownList($searchModel, 'category_id', ArrayHelper::map(Category::get(0, Category::find()->asArray()->all()), 'id', 'label'), ['class' => 'form-control', 'prompt' => Yii::t('app', 'Please Filter')])], 'name', 'sku', 'stock', 'market_price', 'price', ['attribute' => 'type', 'format' => 'html', 'value' => function ($model) { return \common\models\ProductType::labels($model->type); }], ['attribute' => 'brand_id', 'value' => function ($model) { return $model->brand ? $model->brand->name : '-'; }, 'filter' => Html::activeDropDownList($searchModel, 'type', ArrayHelper::map(\common\models\Brand::find()->all(), 'id', 'name'), ['class' => 'form-control', 'prompt' => Yii::t('app', 'Please Filter')])], ['attribute' => 'status', 'format' => 'html', 'value' => function ($model) { if ($model->status === Status::STATUS_ACTIVE) { $class = 'label-success'; } elseif ($model->status === Status::STATUS_INACTIVE) { $class = 'label-warning'; } else { $class = 'label-danger'; } return '<span class="label ' . $class . '">' . Status::labels($model->status) . '</span>'; }, 'filter' => Html::activeDropDownList($searchModel, 'status', Status::labels(), ['class' => 'form-control', 'prompt' => Yii::t('app', 'PROMPT_STATUS')])], 'created_at:date', ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>
/** * Получаем тип */ public function getType() { return $this->hasOne(ProductType::className(), ['id' => 'type_id']); }
?> <?php echo $form->field($model, 'content')->widget(CKEditor::className(), ['editorOptions' => ['preset' => 'full', 'inline' => false]]); ?> <?php echo $form->field($model, 'keywords')->textInput(['maxlength' => 255]); ?> <?php echo $form->field($model, 'description')->textarea(['rows' => 6]); ?> <?php echo $form->field($model, 'type')->checkboxList(\common\models\ProductType::labels()); ?> <?php echo $form->field($model, 'brand_id')->dropDownList(ArrayHelper::map(\common\models\Brand::find()->all(), 'id', 'name'), ['prompt' => Yii::t('app', 'Please Select')]); ?> <?php echo $form->field($model, 'status')->dropDownList(\common\models\Status::labels()); ?> <?php if (!$model->isNewRecord) { ?> <div class="form-group"> <?php
/** * Finds the ProductType model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ProductType the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ProductType::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Updates an existing Product model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { //if(!Yii::$app->user->can('updateYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth')); $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $model->type = ProductType::arrayToInt($model->type); if ($model->save()) { if (isset(Yii::$app->request->post()['imageSort'])) { foreach (Yii::$app->request->post()['imageSort'] as $key => $sortOrder) { ProductImage::updateAll(['sort_order' => $sortOrder], ['id' => $key]); } } $productImage = ProductImage::find()->where(['product_id' => $id])->orderBy(['sort_order' => SORT_ASC])->one(); if ($productImage) { $model->image = $productImage->image; $model->thumb = $productImage->thumb; $model->save(); } return $this->redirect(['view', 'id' => $model->id]); } } $model->type = ProductType::intToArray($model->type); return $this->render('update', ['model' => $model]); }
/** * @return \yii\db\ActiveQuery */ public function getProductTypes() { return $this->hasMany(ProductType::className(), ['id' => 'product_type_id'])->viaTable('tbl_product_type_has_diagnos', ['diagnos_id' => 'id']); }
<style> td img{width:100px;} </style> <div class="product-view"> <p> <?php echo Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']); ?> <?php echo Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'method' => 'post']]); ?> </p> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', ['attribute' => 'category_id', 'value' => $model->category ? $model->category->name : '-'], 'name', 'sku', 'stock', 'weight', 'market_price', 'price', 'brief', 'content:ntext', ['attribute' => 'thumb', 'format' => 'image', 'value' => isset($data) ? "data:" . $fileType . ";base64," . $data . "" : ($model->thumb ? $model->thumb : ''), 'options' => ['style' => 'width:100px'], 'visible' => isset($model->thumb)], 'image', 'keywords', 'description:ntext', ['attribute' => 'type', 'value' => \common\models\ProductType::labels($model->type)], ['attribute' => 'brand_id', 'value' => $model->brand ? $model->brand->name : '-'], ['attribute' => 'status', 'value' => \common\models\Status::labels($model->status)], 'created_at:datetime', 'updated_at:datetime', ['attribute' => 'created_by', 'value' => $model->createdBy->username], ['attribute' => 'updated_by', 'value' => $model->updatedBy->username]]]); ?> </div> <?php foreach ($model->productImagesSort as $item) { if (strpos($model->thumb, 'http://') === null) { $file = Yii::getAlias('@frontend/web' . $item->thumb); $fileType = \yii\helpers\FileHelper::getMimeType($file); $data = base64_encode(file_get_contents($file)); echo "<img src='data:" . $fileType . ";base64," . $data . "' width=100>"; } else { echo "<img src='{$item->thumb}' width=100>"; } }
/** * @return \yii\db\ActiveQuery */ public function getProductTypes() { return $this->hasMany(ProductType::className(), ['cat_id' => 'id']); }