public function actionSave() { if (Yii::$app->user->can('updateResource')) { $post = Yii::$app->request->post('Scanner'); if ($post['id']) { $model = Scanner::findOne(['id' => $post['id']]); $model->attributes = $post; if ($model->validate()) { if ($model->update()) { Yii::$app->getSession()->setFlash('success', 'Scanner #' . $post['id'] . ' updated.'); } else { Yii::$app->getSession()->setFlash('error', 'Failed to update Scanner #' . $post['id'] . '.'); } } } else { $model = new Scanner(); $model->attributes = $post; if ($model->validate()) { if ($model->insert()) { Yii::$app->getSession()->setFlash('success', 'Scanner created.'); } else { Yii::$app->getSession()->setFlash('error', 'Failed to create scanner.'); } } } } else { Yii::$app->getSession()->setFlash('error', 'Not allowed.'); } return $this->redirect(['index']); }
<div class="box"> <?php $form = ActiveForm::begin(['id' => 'edit-job-form', 'action' => 'save']); echo Html::activeHiddenInput($model, 'id'); ?> <div class="box-body"> <div class="form-group"> <?php echo $form->field($model->firmware, 'description')->label('Firmware')->textInput(['readonly' => true]); ?> <?php echo $form->field($model, 'description')->textInput(); ?> <?php echo $form->field($model, 'scanner_id')->dropdownList(Scanner::find()->select(['name', 'id'])->indexBy('id')->column(), ['prompt' => 'Select scanner']); ?> </div> <div class="box-footer"> <?php echo Button::widget(['label' => 'Save', 'options' => ['class' => 'btn btn-primary']]); ?> <?php echo Html::a('Cancel', Yii::$app->request->referrer, ['class' => 'btn btn-default']); ?> </div> </div> <?php ActiveForm::end(); ?> </div>
<?php use yii\grid\GridView; use yii\helpers\Html; use yii\data\ActiveDataProvider; use common\models\Scanner; use common\models\User; /* @var $this yii\web\View */ $this->title = 'Scanners'; $this->params['breadcrumbs'][] = $this->title; $dataProvider = new ActiveDataProvider(['query' => Scanner::find(), 'pagination' => ['pageSize' => 20]]); ?> <div class="box"> <div class="box-body"> <?php echo Html::a('Create', 'create', ['class' => 'btn btn-primary']); ?> </div> </div> <div class="box"> <div class="box-body"> <?php if (Yii::$app->user->can('listResources')) { echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => ['id', 'name', 'description', ['label' => 'Registered API users', 'value' => function ($model, $index, $widget) { return count(User::findAllByScannerId($model->id)); }], ['attribute' => 'created_at', 'format' => ['datetime', 'php:Y-m-d H:i:s']], ['attribute' => 'updated_at', 'format' => ['datetime', 'php:Y-m-d H:i:s']], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{update}']]]); } ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getScanner() { return $this->hasOne(Scanner::className(), ['id' => 'scanner_id']); }