/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Main::find(); $query->joinWith('amount'); $query->joinWith('meal'); $query->joinWith('user'); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['main_id' => SORT_DESC], 'enableMultiSort' => true]]); $dataProvider->sort->attributes['amount_text'] = ['asc' => ['amount_text' => SORT_ASC], 'desc' => ['amount_text' => SORT_DESC]]; $dataProvider->sort->attributes['meal_text'] = ['asc' => ['meal_text' => SORT_ASC], 'desc' => ['meal_text' => SORT_DESC]]; $dataProvider->sort->attributes['username'] = ['asc' => ['username' => SORT_ASC], 'desc' => ['username' => 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; } $user_role = \common\models\User::roleName(Yii::$app->user->getId()); $query->andFilterWhere(['main_id' => $this->main_id, 'amount_id' => $this->amount_id, 'time_bit' => $this->time_bit, 'meal_id' => $this->meal_id, 'user_id' => $user_role == 'root' ? $this->user_id : Yii::$app->user->identity->id, 'created' => $this->created, 'updated' => $this->updated]); $query->andFilterWhere(['like', 'profile_name', $this->profile_name])->andFilterWhere(['like', 'drug_name', $this->drug_name])->andFilterWhere(['like', 'amount_text', $this->amount_text])->andFilterWhere(['like', 'meal_text', $this->meal_text])->andFilterWhere(['like', 'username', $this->username]); return $dataProvider; }
<?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'formatter' => ['class' => 'yii\\i18n\\Formatter', 'nullDisplay' => '<span class="not-set">' . Yii::t('backend', '(empty)') . '</span>'], 'columns' => [['class' => 'yii\\grid\\ActionColumn', 'template' => '{update} {delete}', 'buttons' => ['update' => function ($url, $model, $key) { return Html::a('<span class="glyphicon glyphicon-pencil btn btn-warning btn-sm"></span>', $url, ['title' => Yii::t('backend', 'Update'), 'aria-label' => Yii::t('backend', 'Update'), 'data-pjax' => '0']); }, 'delete' => function ($url, $model, $key) { return Html::a('<span class="glyphicon glyphicon-trash btn btn-danger btn-sm"></span>', $url, ['title' => Yii::t('backend', 'Delete'), 'aria-label' => Yii::t('backend', 'Delete'), 'data-confirm' => Yii::t('backend', 'Are you sure you want to delete this item?'), 'data-method' => 'post', 'data-pjax' => '0']); }], 'options' => ['style' => 'word-break: keep-all; word-wrap: normal; width: 1px;'], 'contentOptions' => ['noWrap' => true]], ['attribute' => 'main_id', 'contentOptions' => ['style' => 'text-align: right; width: 1px;']], 'profile_name', 'drug_name', ['attribute' => 'amount_text', 'value' => function ($data) { return !empty($data->amount->amount_text) ? $data->amount->amount_text : $data->amount_id; }, 'label' => Yii::t('backend', 'Amount')], ['attribute' => 'meal_text', 'value' => function ($data) { return !empty($data->meal->meal_text) ? $data->meal->meal_text : $data->meal_id; }, 'label' => Yii::t('backend', 'Meal')], ['attribute' => 'time_bit', 'value' => function ($data) { $time_data = backend\models\Time::find()->select(['time_id', 'time_text'])->orderBy('time_id')->all(); if (!empty($time_data)) { $time_list = \yii\helpers\ArrayHelper::map($time_data, 'time_id', 'time_text'); // Convert bit to array $time_array = \backend\models\Main::decodeBit($data->time_bit, \backend\models\Main::getTimeID()); foreach ($time_array as $time_array_key => $time_array_value) { foreach ($time_list as $time_list_key => $time_list_value) { if ($time_list_key == $time_array_value) { $time_array[$time_array_key] = $time_list_value; } } } return implode(', ', $time_array); } else { return $data->time_bit; } }, 'enableSorting' => false, 'filter' => false], ['attribute' => 'username', 'value' => function ($data) { return !empty($data->user->username) ? $data->user->username : $data->user_id; }, 'label' => Yii::t('backend', 'Owner'), 'enableSorting' => $user_role == 'root' ? true : false, 'filter' => $user_role == 'root' ? true : false]]]); ?>
/** * Deletes an existing Amount model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $main_data = \backend\models\Main::find()->select(['amount_id'])->andWhere('amount_id = :id', [':id' => $id])->groupBy('amount_id')->one(); $model = $this->findModel($id); // Look-up Unit $unit_data = \backend\models\Unit::find()->select(['unit_text'])->andWhere('unit_id = :unit_id', [':unit_id' => $model->unit_id])->one(); if (empty($main_data->amount_id)) { if (file_exists($model->amount_img)) { unlink($model->amount_img); } $model->delete(); Yii::$app->getSession()->setFlash('amount_delete_success', ['type' => Growl::TYPE_SUCCESS, 'title' => '<b>' . Yii::t('backend', 'Success') . '</b>', 'icon' => 'glyphicon glyphicon-ok-sign', 'body' => '<strong>' . $model->amount_text . ' ' . $unit_data->unit_text . '</strong> ' . Yii::t('backend', 'has been deleted.')]); } else { Yii::$app->getSession()->setFlash('amount_delete_failed', ['type' => \kartik\growl\Growl::TYPE_DANGER, 'title' => '<b>' . Yii::t('backend', 'Error') . '</b>', 'icon' => 'glyphicon glyphicon-remove-sign', 'body' => '<strong>' . $model->amount_text . ' ' . $unit_data->unit_text . '</strong> ' . Yii::t('backend', 'is currently being used and cannot be deleted.')]); } return $this->redirect(['index']); }
/** * @return \yii\db\ActiveQuery */ public function getMains() { return $this->hasMany(Main::className(), ['amount_id' => 'amount_id']); }
public function actionProfileDetail($name) { $user_id = Yii::$app->user->identity->id; $data = Main::find()->andWhere('profile_name like :name', [':name' => $name])->andWhere('user_id = :user_id', [':user_id' => $user_id])->one(); $data->time_array = $data->decodeBit($data->time_bit, $data->getTimeID()); return Json::encode(['main_id' => $data->main_id, 'drug_name' => $data->drug_name, 'amount_id' => $data->amount_id, 'time_array' => $data->time_array, 'meal_id' => $data->meal_id]); }