public function actionDetail($id) { $model = School::findOne($id); if (!$model) { throw new BadRequestHttpException('未找到该学校!'); } $data['name'] = $model->name; $data['stores'] = $model->getStores()->select(['id', 'name', 'status', 'address'])->all(); $data['stores'] = array_chunk($data['stores'], 2); return $data; }
public function actionIndex($id) { $model = School::findOne($id); if (!$model) { throw new NotFoundHttpException('未找到该学校!'); } $cookieSchool = new Cookie(['name' => 'schoolId', 'value' => $id, 'expire' => time() + 86400 * 30]); Yii::$app->response->cookies->add($cookieSchool); Yii::$app->params['schoolModel'] = $model; return $this->render('index', ['model' => $model]); }
/** * @inheritdoc */ public function init() { $schoolId = Yii::$app->request->cookies['schoolId']; $storeId = Yii::$app->request->cookies['storeId']; if ($schoolId) { Yii::$app->params['schoolModel'] = School::findOne($schoolId); } if ($storeId) { Yii::$app->params['storeModel'] = Store::findOne($storeId); } }
public function actionNameFilter($q = null, $id = null) { Yii::$app->response->format = Response::FORMAT_JSON; $output = ['results' => ['id' => '', 'text' => '']]; if (!is_null($q)) { $sql = 'SELECT id, name AS text FROM ' . School::tableName() . ' WHERE name LIKE :like ORDER BY name ASC LIMIT 25'; $output['results'] = Yii::$app->db->createCommand($sql, [':like' => "%{$q}%"])->queryAll(); } elseif ($id > 0 && ($school = School::findOne($id))) { $output['results'] = ['id' => $id, 'text' => $school->name]; } return $output; }
$this->title = '营业点列表'; ?> <p> <?php echo Html::a('<i class="fa fa-plus"></i> 添加营业点', ['store/add'], ['class' => 'btn btn-primary']); ?> </p> <div class="row"> <div class="col-lg-12"> <?php Pjax::begin(); ?> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'tableOptions' => ['class' => 'table table-striped table-bordered table-center'], 'summaryOptions' => ['tag' => 'p', 'class' => 'text-right text-info'], 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'headerOptions' => ['class' => 'col-md-1']], ['attribute' => 'name', 'headerOptions' => ['class' => 'col-md-2'], 'filterInputOptions' => ['class' => 'form-control input-sm']], ['attribute' => 'school_id', 'headerOptions' => ['class' => 'col-md-3'], 'filterInputOptions' => ['class' => 'form-control input-sm'], 'value' => function ($model, $key, $index, $column) { return $model->school->name; }, 'filter' => Select2::widget(['model' => $searchModel, 'initValueText' => ($school = School::findOne($searchModel->school_id)) ? $school->name : '', 'attribute' => 'school_id', 'size' => Select2::SMALL, 'theme' => Select2::THEME_KRAJEE, 'options' => ['placeholder' => '搜索学校名称...'], 'pluginOptions' => ['allowClear' => true, 'minimumInputLength' => 3, 'ajax' => ['url' => Url::to(['/school/name-filter']), 'dataType' => 'json', 'data' => new JsExpression('function(params) { return {q:params.term}; }')], 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), 'templateResult' => new JsExpression('function (store) { return store.text; }'), 'templateSelection' => new JsExpression('function (store) { return store.text; }')]])], ['attribute' => 'address', 'headerOptions' => ['class' => 'col-md-2'], 'filterInputOptions' => ['class' => 'form-control input-sm']], ['attribute' => 'status', 'format' => 'raw', 'filter' => Store::getStatusList(), 'filterInputOptions' => ['class' => 'form-control input-sm'], 'headerOptions' => ['class' => 'col-md-1'], 'value' => function ($model, $key, $index, $column) { return Html::dropDownList('status', $model->status, Store::getStatusList(), ['data-id' => $model->id]); }], ['attribute' => 'created_at', 'format' => ['date', 'php:Y-m-d H:i'], 'filter' => DatePicker::widget(['model' => $searchModel, 'type' => DatePicker::TYPE_COMPONENT_APPEND, 'attribute' => 'date', 'options' => ['class' => 'input-sm'], 'pluginOptions' => ['autoclose' => true, 'format' => 'yyyy-mm-dd']]), 'headerOptions' => ['class' => 'col-md-2']], ['class' => 'yii\\grid\\ActionColumn', 'header' => '操作', 'headerOptions' => ['class' => 'col-md-1'], 'template' => '{update} {view}']]]); ?> <?php Pjax::end(); ?> </div> </div> <?php $url = Url::to(['/store/status']); $js = <<<JS var handle = function () { var id = \$(this).attr('data-id'); var status = \$(this).val(); \$.ajax({
/** * Finds the School model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return School the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = School::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public static function addVote($entity, $id, $voteAdd) { $user = User::thisUser(); $vote = Vote::findOne(['entity' => $entity, 'entity_id' => $id, 'user_id' => $user->id]); if (empty($vote)) { $vote = new Vote(); $vote->entity = $entity; $vote->entity_id = $id; $vote->user_id = $user->id; } /** @var VoteModel $model */ $model = null; if ($entity == self::ENTITY_ITEM) { $model = Item::findOne($id); if ($user->reputation < Item::MIN_REPUTATION_ITEM_VOTE) { // Если только пользователь не отменяет свои дизлайки if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) { return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')]; } } } else { if ($entity == self::ENTITY_EVENT) { $model = Event::findOne($id); if ($user->reputation < Event::MIN_REPUTATION_EVENT_VOTE) { // Если только пользователь не отменяет свои дизлайки if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) { return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')]; } } } else { if ($entity == self::ENTITY_SCHOOL) { $model = School::findOne($id); if ($user->reputation < School::MIN_REPUTATION_SCHOOL_VOTE) { // Если только пользователь не отменяет свои дизлайки if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) { return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')]; } } } else { if ($entity == self::ENTITY_COMMENT) { $model = Comment::findOne($id); if ($user->reputation < Comment::MIN_REPUTATION_COMMENT_VOTE) { // Если только пользователь не отменяет свои дизлайки if (!($vote->vote == self::VOTE_DOWN && $voteAdd == self::VOTE_DOWN)) { return ['vote' => 0, 'count' => $model->getVoteCount(), 'error' => Lang::t('ajax', 'noReputationVote')]; } } } } } } if (!empty($model)) { if ($vote->vote == self::VOTE_UP) { if ($voteAdd == self::VOTE_UP) { // убираем up $vote->vote = self::VOTE_NONE; $model->addVote(-1); $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_UP); } else { // ставим down $vote->vote = self::VOTE_DOWN; $model->addVote(-2); $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_UP); $model->addReputation(VoteModel::ADD_REPUTATION_DOWN); } } elseif ($vote->vote == self::VOTE_DOWN) { if ($voteAdd == self::VOTE_UP) { // ставим up $vote->vote = self::VOTE_UP; $model->addVote(2); $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_DOWN); $model->addReputation(VoteModel::ADD_REPUTATION_UP); } else { // убираем down $vote->vote = self::VOTE_NONE; $model->addVote(1); $model->addReputation(VoteModel::ADD_REPUTATION_CANCEL_DOWN); } } else { if ($voteAdd == self::VOTE_UP) { // ставим up $vote->vote = self::VOTE_UP; $model->addVote(1); $model->addReputation(VoteModel::ADD_REPUTATION_UP); } else { // ставим down $vote->vote = self::VOTE_DOWN; $model->addVote(-1); $model->addReputation(VoteModel::ADD_REPUTATION_DOWN); } } } if ($vote->save()) { if (!empty($model)) { $model->save(); } } return ['vote' => $vote->vote, 'count' => $model->getVoteCount()]; }
public function actionAlarm() { $id = Yii::$app->request->post('id'); $msg = Yii::$app->request->post('msg'); $school = School::findOne($id); if ($school && !empty($msg)) { if (Alarm::addAlarm(Alarm::ENTITY_SCHOOL, $school->id, $msg)) { $resultMsg = Lang::t('main/dialogs', 'modalAlarm_msgAlarmResultTrue'); Yii::$app->session->setFlash('success', Lang::t('main/dialogs', 'modalAlarm_msgAlarmResultTrue')); } else { $resultMsg = Lang::t('main/dialogs', 'modalAlarm_msgAlarmResultFalse'); Yii::$app->session->setFlash('success', Lang::t('main/dialogs', 'modalAlarm_msgAlarmResultFalse')); } return json_encode(['msg' => $resultMsg]); } }