/** * Finds user by id. * * @return User|null User instance */ protected function getUser() { if ($this->_user === null) { $this->_user = User::find()->where(['id' => Yii::$app->user->identity->id])->active()->one(); } return $this->_user; }
protected function getUsersFoundCod() { $user = User::find()->where(['cod' => $this->codUser])->one(); if (isset($user)) { return $user->id; } else { return 0; } }
/** * Searches users. * @param array $params search query data * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['like', 'id', $this->id])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'password', $this->password])->andFilterWhere(['like', 'DATE_FORMAT(FROM_UNIXTIME(created_at), "%Y-%m-%d")', $this->created_at])->andFilterWhere(['like', 'DATE_FORMAT(FROM_UNIXTIME(updated_at), "%Y-%m-%d")', $this->updated_at]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'role' => $this->role, 'status' => $this->status, 'FROM_UNIXTIME(created_at, "%d.%m.%Y")' => $this->created_at]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'auth_key', $this->auth_key])->andFilterWhere(['like', 'password_hash', $this->password_hash])->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])->andFilterWhere(['like', 'role', $this->role])->andFilterWhere(['like', 'email', $this->email]); return $dataProvider; }
/** * @return array */ public function actionAjaxUser() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $result = ['more' => false, 'results' => []]; $search = \Yii::$app->request->get('search', []); if (!empty($search['term'])) { $query = User::find()->select('id, username, first_name, email')->where(['like', 'username', trim($search['term'])])->orWhere(['like', 'email', trim($search['term'])])->orWhere(['like', 'first_name', trim($search['term'])])->asArray(); $result['results'] = array_values($query->all()); } return $result; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'followers' => $this->followers, 'following' => $this->following, 'photos' => $this->photos, 'avatar_sid' => $this->avatar_sid, 'status' => $this->status, 'last_visit_at' => $this->last_visit_at, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'auth_key', $this->auth_key])->andFilterWhere(['like', 'password_hash', $this->password_hash])->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])->andFilterWhere(['like', 'last_login_ip', $this->last_login_ip]); return $dataProvider; }
public function actionRemoveOverdue() { foreach (User::find()->overdue($this->module->emailConfirmUserExpire)->each() as $user) { /** @var User $user */ $this->stdout($user->username . ' - ' . $user->email); $user->delete(); $this->stdout('OK', Console::FG_GREEN, Console::BOLD); $this->stdout(PHP_EOL); } $this->stdout('Done!', Console::FG_GREEN, Console::BOLD); $this->stdout(PHP_EOL); }
/** * Removes non-activated expired users */ public function actionRemoveOverdue() { foreach (User::find()->overdue(Yii::$app->params['user.emailConfirmUserExpire'])->each() as $user) { /** @var User $user */ $this->stdout($user->username); $user->delete(); $this->stdout(' OK', Console::FG_GREEN, Console::BOLD); $this->stdout(PHP_EOL); } $this->stdout('Done!', Console::FG_GREEN, Console::BOLD); $this->stdout(PHP_EOL); }
/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (is_numeric($id)) { $model = User::findOne($id); } else { $model = User::find()->where(['username' => $id])->one(); } if ($model !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'auth_key', $this->auth_key])->andFilterWhere(['like', 'password_hash', $this->password_hash])->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'marital' => $this->marital, 'birthday' => $this->birthday, 'post_id' => $this->post_id, 'department_id' => $this->department_id, 'user_id' => $this->user_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by, 'visited_at' => $this->visited_at, 'views' => $this->views, 'weight' => $this->weight, 'status_id' => $this->status_id]); $query->andFilterWhere(['like', 'last_name', $this->last_name])->andFilterWhere(['like', 'first_name', $this->first_name])->andFilterWhere(['like', 'patronymic', $this->patronymic])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'alias', $this->alias])->andFilterWhere(['like', 'gender', $this->gender])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'password_hash', $this->password_hash])->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])->andFilterWhere(['like', 'auth_key', $this->auth_key])->andFilterWhere(['like', 'token', $this->token])->andFilterWhere(['like', 'email_confirm_token', $this->email_confirm_token])->andFilterWhere(['like', 'phone_work', $this->phone_work])->andFilterWhere(['like', 'phone_private', $this->phone_private])->andFilterWhere(['like', 'email_work', $this->email_work])->andFilterWhere(['like', 'email_private', $this->email_private]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['status' => $this->status]); $query->andFilterWhere(['like', 'email', $this->email]); $query->andFilterWhere(['like', 'username', $this->username]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); // add conditions that should always apply here $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; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'status' => $this->status]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['>=', 'created_at', $this->created_at ? strtotime($this->created_at . ' 00:00:00') : null])->andFilterWhere(['<=', 'created_at', $this->created_at ? strtotime($this->created_at . ' 23:59:59') : null]); return $dataProvider; }
/** * Removes non-activated expired users */ public function actionRemoveOverdue() { foreach (User::find()->overdue($this->module->emailConfirmTokenExpire)->each() as $user) { /** @var User $user */ $this->stdout($user->username); if ($user->delete()) { Yii::info('Remove expired user ' . $user->username); $this->stdout(' OK', Console::FG_GREEN, Console::BOLD); } else { Yii::warning('Cannot remove expired user ' . $user->username); $this->stderr(' FAIL', Console::FG_RED, Console::BOLD); } $this->stdout(PHP_EOL); } $this->stdout('Done!', Console::FG_GREEN, Console::BOLD); $this->stdout(PHP_EOL); }
/*$listdata = ArrayHelper::map(Post::find()->all(), 'id', 'title'); ?> <?= $form->field($model, 'post_id')->dropDownList($listdata, [ 'class' => 'form-control select', 'prompt' => Yii::t('app', 'Select from list') ]); */ ?> <?php $listdata = ArrayHelper::map(Department::find()->all(), 'id', 'title'); ?> <?php echo $form->field($model, 'department_id')->dropDownList($listdata, ['class' => 'form-control select', 'prompt' => Yii::t('app', 'Select from list')]); ?> <?php $listdata = ArrayHelper::map(User::find()->all(), 'id', 'last_name'); ?> <?php echo $form->field($model, 'user_id')->dropDownList($listdata, ['class' => 'form-control select', 'prompt' => Yii::t('app', 'Select from list')]); ?> <?php $listdata = ArrayHelper::map(Address::find()->all(), 'id', 'title'); ?> <?php echo $form->field($model, 'address_id')->dropDownList($listdata, ['class' => 'form-control select', 'prompt' => Yii::t('app', 'Select from list')]); ?> <?php echo $form->field($model, 'place_id')->widget(DepDrop::classname(), ['options' => ['id' => 'place_id'], 'pluginOptions' => ['depends' => ['user-address_id'], 'placeholder' => 'Select...', 'url' => Url::to(['/place/list'])]]);
<div class="order-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'currency_code')->widget(EntityDropDown::className(), ['items' => ArrayHelper::map(Currency::find()->all(), 'code', 'name')]); ?> <?php echo $form->field($model, 'country_id')->widget(EntityDropDown::className(), array('items' => ArrayHelper::map(Country::find()->all(), 'id', 'name'))); ?> <?php echo $form->field($model, 'user_id')->widget(EntityDropDown::className(), ['items' => ArrayHelper::map(User::find()->all(), 'id', 'name')]); ?> <?php echo $form->field($model, 'status')->widget(EntityDropDown::className(), ['items' => [Yii::t('app', 'New'), Yii::t('app', 'Closed'), Yii::t('app', 'Canceled')]]); ?> <?php echo $form->field($model, 'discount'); ?> <?php echo $form->field($model, 'name'); ?> <?php
public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => User::find()->where(['status' => 1]), 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => 10]]); return $this->render('index', ['dataProvider' => $dataProvider]); }
private function findModel() { return User::find()->withProfile()->current(); }
/** * @param Connection $db * @return array */ protected function getUserStatistics(Connection $db) { $usersIds = User::find()->select('id')->column($db); $activeUsers = Order::find()->select('user_id')->innerJoin(OrderTransaction::tableName(), Order::tableName() . '.id = ' . OrderTransaction::tableName() . '.order_id')->where(['status' => 5, 'user_id' => $usersIds])->count('*', $db); $lastRegistered = User::find()->where(['>=', 'create_time', $this->getPeriodTs($this->userDate)])->count('*', $db); $userData = [Yii::t('app', 'Registered customers') => count($usersIds), Yii::t('app', 'Active registered customers') => $activeUsers, Yii::t('app', 'New registered customers for last {period}', ['period' => Yii::t('app', $this->userDate)]) => $lastRegistered]; return $userData; }
/** * @return array */ public static function userList() { return User::find()->orderBy('username')->indexBy('id')->select('username')->column(); }
public function actionRemoveAll() { $items = Yii::$app->request->post('items', []); if (!empty($items)) { $items = User::find()->where(['in', 'id', $items])->all(); foreach ($items as $item) { $item->scenario = 'admin'; $item->status = User::STATUS_DELETED; $item->save(true, ['status']); } } return $this->redirect(['index']); }
use app\modules\blog\models\Blog; /* @var $this yii\web\View */ /* @var $searchModel app\modules\admin\models\search\BlogSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = 'Blogs'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="blog-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?php echo Html::a('Create Blog', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['attribute' => 'id', 'options' => ['width' => '50']], ['filter' => ArrayHelper::map(BlogCategory::find()->all(), 'id', 'name'), 'attribute' => 'category_id', 'value' => 'category.name'], ['filter' => ArrayHelper::map(User::find()->all(), 'id', 'username'), 'attribute' => 'user_id', 'value' => 'user.username'], ['class' => LinkColumn::className(), 'attribute' => 'title', 'value' => function ($data) { return \yii\helpers\StringHelper::truncate($data->title, 30, '...'); }, 'options' => ['width' => '250', 'title' => 'title']], 'slug', ['class' => SetColumn::className(), 'filter' => Blog::getStatusesArray(), 'attribute' => 'status', 'name' => 'statusName', 'cssCLasses' => [Blog::STATUS_PUBLISH => 'success', Blog::STATUS_UNPUBLISH => 'default']], ['attribute' => 'publication_at', 'format' => ['date', 'd MMMM yyyy HH:mm']], ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>
public function onAuthSuccess($client) { $attributes = $client->getUserAttributes(); /* @var $auth Auth */ $auth = Auth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one(); if (Yii::$app->user->isGuest) { if ($auth) { // login $user = $auth->user; Yii::$app->user->login($user); } else { // signup if (isset($attributes['email']) && User::find()->where(['email' => $attributes['email']])->exists()) { Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]); } else { $password = Yii::$app->security->generateRandomString(6); $user = new User(['username' => $attributes['screen_name'], 'password' => $password, 'role' => 'user', 'email' => $attributes['email']]); if (!empty($attributes['photo'])) { $file = file_get_contents($attributes['photo']); $avatarPath = \app\modules\user\Module::$avatarPath; file_put_contents($avatarPath . '/1.jpg', $file); } $profile = new Profile(['firstname' => $attributes['first_name'], 'lastname' => $attributes['last_name'], 'country' => $attributes['country']]); $user->generateAuthKey(); $user->generatePasswordResetToken(); $transaction = $user->getDb()->beginTransaction(); if ($user->save()) { // Сохраняем профиль $profile->user_id = $user->id; if (!$profile->save()) { print_r($profile->getErrors()); die; } $auth = new Auth(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]); if ($auth->save()) { $transaction->commit(); Yii::$app->user->login($user); } else { print_r($auth->getErrors()); } } else { print_r($user->getErrors()); } } } } else { // user already logged in if (!$auth) { // add auth provider $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]); $auth->save(); } } }
/** * Delete an existing User model. If deletion is successful, the browser * will be redirected to the 'index' page. * * @param string $id * @return mixed */ public function actionDelete($id) { // delete profile and userkeys first to handle foreign key constraint $user = $this->findModel($id); // If user has admin role, check if the one if ($user->role->id === Role::ROLE_ADMIN && User::find()->where(['role_id' => Role::ROLE_ADMIN])->count() < 2) { Yii::$app->session->setFlash('danger', Yii::t('app', "You need to create another user with 'Admin' role in order to delete your account.")); } else { $profile = $user->profile; UserKey::deleteAll(['user_id' => $user->id]); UserAuth::deleteAll(['user_id' => $user->id]); FormUser::deleteAll(["user_id" => $user->id]); $profile->delete(); $user->delete(); Yii::$app->getSession()->setFlash('success', Yii::t('app', 'The user has been successfully deleted.')); } return $this->redirect(['index']); }
use app\models\Product; use app\modules\admin\models\CommentSearch; use app\modules\user\models\User; use app\widgets\EntityDropDown; use app\widgets\Rating; use yii\bootstrap\Html; use yii\data\ActiveDataProvider; use yii\helpers\ArrayHelper; use yii\web\View; /** * @var CommentSearch $searchModel * @var ActiveDataProvider $dataProvider * @var View $this */ $this->title = Yii::t('app', 'Comments'); ?> <?php echo \yii\grid\GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['attribute' => 'product_id', 'filter' => false, 'format' => 'text'], ['label' => Yii::t('app', 'Product name'), 'filter' => false, 'format' => 'html', 'value' => function ($comment) { /** @var Product $product */ $product = Product::findOne($comment->product_id); return Html::a($product->name, ['/admin/product/update', 'id' => $product->id]); }], ['attribute' => 'user_id', 'format' => 'html', 'filter' => EntityDropDown::widget(['model' => $searchModel, 'attribute' => 'user_id', 'items' => ArrayHelper::map(User::find()->all(), 'id', 'name')]), 'value' => function ($comment) { return User::findOne($comment->user_id)->name; }], ['attribute' => 'body', 'format' => 'text', 'value' => function ($comment) { return \yii\helpers\StringHelper::truncateWords($comment->body, 10); }], 'created_at:datetime', ['attribute' => 'rating', 'format' => 'raw', 'filter' => false, 'value' => function ($comment) { return Rating::widget(['readonly' => true, 'name' => "rating[{$comment->id}]", 'value' => $comment->rating]); }], ['attribute' => 'status', 'format' => 'html', 'filter' => EntityDropDown::widget(['model' => $searchModel, 'attribute' => 'user_id', 'items' => [Yii::t('app', 'Inactive'), Yii::t('app', 'Active')]]), 'value' => function ($comment) { return $comment->status == Comment::STATUS_ACTIVE ? Yii::t('app', 'Active') : Yii::t('app', 'Inactive'); }], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{update} {delete}']]]);
<?php use app\modules\admin\models\PageSearch; use app\modules\user\models\User; use app\widgets\EntityDropDown; use yii\bootstrap\Nav; use yii\data\ActiveDataProvider; use yii\grid\GridView; use yii\helpers\ArrayHelper; use yii\helpers\Url; /** * @var PageSearch $searchModel * @var ActiveDataProvider $dataProvider */ $this->title = Yii::t('app', 'News items'); echo Nav::widget(['encodeLabels' => false, 'items' => [['label' => Yii::t('app', 'Create'), 'url' => Url::to(['create']), 'active' => true]], 'options' => ['class' => 'nav-pills']]); echo GridView::widget(['filterModel' => $searchModel, 'dataProvider' => $dataProvider, 'columns' => [['attribute' => 'id', 'filter' => false], 'slug', ['attribute' => 'name', 'format' => 'text'], ['attribute' => 'author_id', 'format' => 'html', 'filter' => EntityDropDown::widget(['model' => $searchModel, 'attribute' => 'author_id', 'items' => ArrayHelper::map(User::find()->all(), 'id', 'name')]), 'value' => function ($newsItem) { return User::findOne($newsItem->author_id)->name; }], ['attribute' => 'status', 'filter' => EntityDropDown::widget(['model' => $searchModel, 'attribute' => 'status', 'items' => [Yii::t('app', 'No'), Yii::t('app', 'Yes')]]), 'value' => function ($data) { return $data->status ? Yii::t('app', 'Yes') : Yii::t('app', 'No'); }], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{update} {delete}']]]);
public function findUser($id) { return User::find($id)->joinWith('profile')->where(['profile.user_id' => $id])->one(); }
/** * Lists all User models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => User::find()]); return $this->render('index', ['dataProvider' => $dataProvider]); }
public static function voteAfterInsertHandler(Event $event) { /** @var Vote $vote */ $vote = $event->sender; $question = $vote->question; $userCount = User::find()->count(); $votes = $question->getVotes()->joinWith('answer')->select('answer.value')->column(); $voteCount = count($votes); $voteSum = array_sum($votes); $voteLeft = $userCount - $voteCount; $voteDiff = $voteCount - $voteSum; if ($voteSum > $voteDiff + $voteLeft) { // negative voices can't become more than positive $question->status = $question::STATUS_SUCCESS; } elseif ($voteSum < $voteDiff - $voteLeft) { // positive votes can't become more than negative $question->status = $question::STATUS_FAIL; } else { return; } $question->close(); }
protected function getCurrentUser($id) { $currentUser = User::find($id); return $currentUser; }