Example #1
0
 public function search($params, $whereParams = null)
 {
     $query = Note::find()->joinWith(User::tableName());
     $isAdminPanel = $this->getScenario() === 'admin';
     if ($whereParams) {
         $query->where($whereParams);
     }
     if ($this->load($params) && $this->validate()) {
         $query->andFilterWhere(['like', 'note.name', $this->name])->andFilterWhere(['like', 'user.name', $this['user.name']]);
         if ($isAdminPanel) {
             $query->andFilterWhere(['note.id' => $this->id])->andFilterWhere(['visibility' => $this->visibility]);
             if (preg_match('/[\\d]{2}-[\\d]{2}-[\\d]{4}/', $this->created_at)) {
                 $query->andFilterWhere(['AND', ['>', 'note.created_at', \DateTime::createFromFormat('d-m-Y H:i:s', $this->created_at . ' 00:00:00')->getTimestamp()], ['<', 'note.created_at', \DateTime::createFromFormat('d-m-Y H:i:s', $this->created_at . ' 23:59:59')->getTimestamp()]]);
             }
         } else {
             $query->andFilterWhere(['like', 'description', $this->description]);
         }
     }
     $noteSortAttributes = ['name' => ['asc' => ['note.name' => SORT_ASC], 'desc' => ['note.name' => SORT_DESC], 'label' => $this->getAttributeLabel('name')], 'created_at' => ['default' => SORT_DESC, 'label' => $this->getAttributeLabel('created_at')]];
     if ($isAdminPanel) {
         array_push($noteSortAttributes, 'id', 'user.name', 'visibility');
     } else {
         $noteSortAttributes['description'] = ['label' => $this->getAttributeLabel('description')];
     }
     $noteProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['attributes' => $noteSortAttributes, 'defaultOrder' => ['created_at' => SORT_DESC]], 'pagination' => ['pageSize' => $isAdminPanel ? 10 : 9, 'defaultPageSize' => $isAdminPanel ? 10 : 9]]);
     return $noteProvider;
 }
Example #2
0
 public function behaviors()
 {
     return ['access' => ['class' => AccessControl::className(), 'only' => ['index', 'view', 'create', 'update', 'delete'], 'rules' => [['actions' => ['index', 'view', 'create', 'update', 'delete'], 'allow' => true, 'roles' => ['@']]]], 'verbs' => ['class' => VerbFilter::className(), 'actions' => ['delete' => ['post'], 'send-message' => ['post']]], ['class' => 'yii\\filters\\HttpCache', 'only' => ['index'], 'lastModified' => function ($action, $params) {
         $q = new Query();
         $res = $q->from(User::tableName())->max('create_date');
         return strtotime($res);
     }]];
 }
Example #3
0
 /**
  * Find by my ID and my friends.
  *
  * @param $id
  * @return $this
  */
 public function meAndMyFriends($id)
 {
     $modelClass = $this->modelClass;
     $statusTableName = $modelClass::tableName();
     $relationTableName = Relation::tableName();
     $userTableName = User::tableName();
     return $this->innerJoin(Relation::tableName(), $relationTableName . '.following_id = ' . $statusTableName . '.author_id OR ' . $relationTableName . '.follower_id = ' . $statusTableName . '.author_id')->innerJoin(User::tableName(), $relationTableName . '.following_id = ' . $userTableName . '.id OR ' . $relationTableName . '.follower_id = ' . $userTableName . '.id')->andWhere([User::tableName() . '.id' => $id])->orderBy($statusTableName . '.created_at DESC');
 }
 public function up()
 {
     $this->alterColumn(User::tableName(), 'role', 'smallint(6) NOT NULL DEFAULT 1');
     $this->alterColumn(User::tableName(), 'status', 'smallint(6) NOT NULL DEFAULT 1');
     $this->update(User::tableName(), ['status' => User::STATUS_ADMIN_ACTIVE], 'status = 10 and role = 1');
     $this->update(User::tableName(), ['status' => User::STATUS_ACTIVE], 'status = 0 or (status = 10 and role > 1)');
     $this->update(User::tableName(), ['role' => 3], 'role=1');
     $this->update(User::tableName(), ['role' => User::ROLE_DEV], 'role=2');
     $this->update(User::tableName(), ['role' => User::ROLE_ADMIN], 'role=3');
 }
 /**
  * @param array $params
  * @param bool $filterByUser
  * @return \yii\db\Query
  */
 public static function getQuery($params = [], $filterByUser = true)
 {
     $portionCaloriesSql = self::getPortionCaloriesQuery('`d`.`id`')->createCommand()->sql;
     $recipeCaloriesSql = self::getRecipeCaloriesQuery('`d`.`id`')->createCommand()->sql;
     $productCaloriesSql = self::getProductCaloriesQuery('`d`.`id`')->createCommand()->sql;
     $portWeightSql = self::getPortionsWeightQuery('`d`.`id`')->createCommand()->sql;
     $recWeightSql = self::getRecipesWeightQuery('`d`.`id`')->createCommand()->sql;
     $prodWeightSql = self::getProductsWeightQuery('`d`.`id`')->createCommand()->sql;
     $query = Diary::find()->select(['`d`.*', '`u`.`weighing_day` AS `weighingDay`', "COALESCE(({$portionCaloriesSql}), 0) + COALESCE(({$recipeCaloriesSql}), 0) + COALESCE(({$productCaloriesSql}), 0) AS `calories`", "COALESCE(({$prodWeightSql}), 0) + COALESCE(({$recWeightSql}), 0) + COALESCE(({$portWeightSql}), 0) AS `weight`"])->from(self::tableName() . ' `d`')->leftJoin(User::tableName() . ' `u`', '`u`.`id` = `d`.`user_id`');
     if ($filterByUser) {
         $query->where(['`d`.`user_id`' => Yii::$app->user->id]);
     }
     return $query;
 }
Example #6
0
 function run()
 {
     // && $model->contact(Yii::$app->params['adminEmail'])
     $this->enable_form = $this->allow_comments();
     $comment = new Comments();
     $comment->parent_id = 0;
     if ($this->enable_form && $comment->load(Yii::$app->request->post()) && $comment->validate()) {
         Yii::$app->session->setFlash('commentFormSubmitted');
         $comment->active = true;
         $comment->user_id = Yii::$app->user->isGuest ? null : Yii::$app->user->identity->id;
         $comment->ip = ip2long(Yii::$app->request->getUserIP());
         $comment->agent = Yii::$app->request->getUserAgent();
         $comment->saveUploadedImage('file');
         $comment->save();
         return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl());
     }
     $query = new Query();
     $query->addSelect('c.*, f.filename, f.thumb_filename, f.size, u.username')->from([Comments::tableName() . ' c'])->leftJoin(User::tableName() . ' u', 'u.id = c.user_id')->leftJoin(Files::tableName() . ' f', 'f.id = c.image_id')->where(['c.active' => true])->andWhere(['c.post_id' => $this->post_id]);
     $comment->post_id = $this->post_id;
     return $this->render('comments/comments_box', ['comments' => $this->buildTree($query->all()), 'model' => $comment, 'enable' => $this->enable_form]);
 }
 /**
  * Все
  *
  * @param null $q
  * @param null $id
  * @return array
  */
 public function actionIndex($q = null, $id = null)
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $out = ['results' => ['id' => '', 'text' => '']];
     if (!is_null($q)) {
         $query = new Query();
         $query->select('id, name, surname')->from(User::tableName())->where(['like', 'name', $q])->orWhere(['like', 'surname', $q])->limit(20);
         $command = $query->createCommand();
         $data = $command->queryAll();
         $users = [];
         foreach ($data as &$user) {
             $users[] = ['id' => $user['id'], 'text' => $user['name'] . ' ' . $user['surname']];
         }
         $out['results'] = $users;
     } else {
         if ($id > 0) {
             $user = User::findIdentity($id);
             if ($user) {
                 $out['results'] = ['id' => $user->id, 'text' => $user->name . ' ' . $user->surname];
             }
         }
     }
     return $out;
 }
Example #8
0
 public function actionIndex()
 {
     $dataProvider = new ActiveDataProvider(['query' => User::findBySql('select * from ' . User::tableName() . ' where isadmin = 0'), 'pagination' => ['pagesize' => '20']]);
     return $this->render('index', array('dataProvider' => $dataProvider));
 }
 public function down()
 {
     $this->dropTable('group');
     $this->dropColumn(\app\models\User::tableName(), 'avatar');
     return true;
 }
 public function down()
 {
     $this->dropColumn(User::tableName(), 'options');
 }
 public function down()
 {
     $this->dropTable(User::tableName());
 }
Example #12
0
 public function testTableName()
 {
     $this->assertEquals('user', User::tableName());
 }
Example #13
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getReceiver()
 {
     return $this->hasOne(User::className(), ['id' => 'receiver_id'])->from(User::tableName() . ' receiver');
 }
 public function down()
 {
     echo "m150926_151034_init_user reverted.\n";
     $this->delete(User::tableName(), ['id' => [1, 2]]);
     return true;
 }
Example #15
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCalendarEvents()
 {
     return $this->hasMany(CalendarEvent::className(), ['calendar_id' => 'calendar_id'])->viaTable(User::tableName(), ['id' => 'user_id']);
 }
Example #16
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getCreatedBy()
 {
     return $this->hasOne(User::className(), ['id' => 'created_by'])->from(User::tableName() . ' createdBy');
 }
Example #17
0
 /**
  * @return array|ActiveQuery users
  */
 public function getSearchUser($fullname)
 {
     $query = new Query();
     $query->select('id, full_name AS text')->from(User::tableName())->where(['like', 'full_name', $fullname])->limit(20);
     return $query->createCommand()->queryAll();
 }