/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $user = Yii::$app->getModule("user")->model("User"); $query = Album::find(); $postTable = Album::tableName(); // set up query with relation to `user.username` $userTable = $user::tableName(); $query->joinWith(['user' => function ($query) use($userTable) { $query->from(['user' => $userTable]); }]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]); // enable sorting for the related columns $addSortAttributes = ["user.username"]; foreach ($addSortAttributes as $addSortAttribute) { $dataProvider->sort->attributes[$addSortAttribute] = ['asc' => [$addSortAttribute => SORT_ASC], 'desc' => [$addSortAttribute => SORT_DESC]]; } if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(["{$postTable}.id" => $this->id, 'user_id' => $this->user_id, 'is_public' => $this->is_public]); if ($this->created_at) { $createdTime = strtotime($this->created_at); $startDay = date("Y-m-d 00:00:00", $createdTime); $endDay = date("Y-m-d 00:00:00", $createdTime + 60 * 60 * 24); $query->andFilterWhere(['between', 'created_at', $startDay, $endDay]); } if ($this->updated_at) { $updatedTime = strtotime($this->updated_at); $startDay = date("Y-m-d 00:00:00", $updatedTime); $endDay = date("Y-m-d 00:00:00", $updatedTime + 60 * 60 * 24); $query->andFilterWhere(['between', 'updated_at', $startDay, $endDay]); } $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'slug', $this->slug])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'cached_tag_list', $this->cached_tag_list])->andFilterWhere(['like', 'user.username', $this->getAttribute('user.username')]); return $dataProvider; }
public function afterSave($insert, $changedAttributes) { switch ($this->commentable_type) { case self::COMMENTABLE_POST: case self::COMMENTABLE_TRANSFER: case self::COMMENTABLE_MATCH: case self::COMMENTABLE_VIDEO: case self::COMMENTABLE_INQUIRER: $commentCount = CommentCount::find()->where(['commentable_id' => $this->commentable_id, 'commentable_type' => $this->commentable_type])->one(); if (empty($commentCount->id)) { $commentCount = new CommentCount(); $commentCount->commentable_id = $this->commentable_id; $commentCount->commentable_type = $this->commentable_type; } $count = Comment::find()->where(['commentable_type' => $this->commentable_type, 'commentable_id' => $this->commentable_id])->count(); $commentCount->count = $count; $commentCount->save(false); break; case self::COMMENTABLE_PHOTO: case self::COMMENTABLE_ALBUM: if ($this->commentable_type == self::COMMENTABLE_ALBUM) { $albumID = $this->commentable_id; } else { $albumTable = Album::tableName(); $assetTable = Asset::tableName(); $album = (new \yii\db\Query())->select("{$albumTable}.id")->from($albumTable)->innerJoin($assetTable, "{$assetTable}.assetable_id = {$albumTable}.id")->where(["{$assetTable}.id" => $this->commentable_id, "{$assetTable}.assetable_type" => Asset::ASSETABLE_ALBUM])->one(); if (isset($album['id'])) { $albumID = $album['id']; } } if (isset($albumID)) { $commentCount = CommentCount::find()->where(['commentable_id' => $albumID, 'commentable_type' => self::COMMENTABLE_ALBUM])->one(); if (empty($commentCount->id)) { $commentCount = new CommentCount(); $commentCount->commentable_id = $albumID; $commentCount->commentable_type = self::COMMENTABLE_ALBUM; $commentCount->count = 0; } // $commentCount->count++; $albumCount = Comment::find()->where(['commentable_type' => $this->commentable_type, 'commentable_id' => $this->commentable_id])->count(); $assetTable = Asset::tableName(); $albumTable = self::tableName(); $ids = (new \yii\db\Query())->select("{$assetTable}.id")->from($assetTable)->innerJoin($albumTable, "{$albumTable}.id = {$assetTable}.assetable_id")->where(["{$albumTable}.id" => $albumID, "{$assetTable}.assetable_type" => Asset::ASSETABLE_ALBUM])->all(); $assetIDs = []; foreach ($ids as $id) { $assetIDs[] = (int) $id['id']; } $assetsCount = Comment::find()->where(['commentable_type' => self::COMMENTABLE_PHOTO, 'commentable_id' => $assetIDs])->count(); $commentCount->count = $albumCount + $assetsCount; $commentCount->save(false); } break; default: break; } parent::afterSave($insert, $changedAttributes); }
/** * Url: /match/{$id}/photos * @param $id Match id * @return mixed * @throws NotFoundHttpException */ public function actionMatchPhotos($id) { $match = Match::findOne($id); if (!isset($match)) { throw new NotFoundHttpException('The requested page does not exist.'); } $title = "Фото матча " . $match->name; $albumTable = Album::tableName(); $relationTable = Relation::tableName(); $album = Album::find()->innerJoin($relationTable, "{$albumTable}.id = {$relationTable}.relationable_id")->where(['is_public' => 1, "{$relationTable}.relationable_type" => Relation::RELATIONABLE_ALBUM, "{$relationTable}.parent_id" => $match->id])->one(); $columnData = []; $columnData['menu'] = ['view' => '@frontend/views/translation/menu', 'data' => compact('match')]; if (!isset($album)) { $columnData['content'] = ['view' => '@frontend/views/site/empty', 'data' => ['message' => 'Фотографий к матчу не найдено'], 'weight' => 10]; } else { $contentImagesCount = Asset::find()->where(['assetable_id' => $album->id, 'assetable_type' => Asset::ASSETABLE_ALBUM, 'thumbnail' => Asset::THUMBNAIL_CONTENT])->count(); if ($contentImagesCount > 0) { $imageCount = $contentImagesCount; $bigImages = Asset::find()->where(['assetable_id' => $album->id, 'assetable_type' => Asset::ASSETABLE_ALBUM, 'thumbnail' => Asset::THUMBNAIL_CONTENT])->limit(12)->offset(0)->orderBy(['id' => SORT_ASC])->all(); $smallImages = Asset::find()->where(['assetable_id' => $album->id, 'assetable_type' => Asset::ASSETABLE_ALBUM, 'thumbnail' => Asset::THUMBNAIL_SMALL])->limit(12)->offset(0)->orderBy(['id' => SORT_ASC])->all(); } else { $imageCount = Asset::find()->where(['assetable_id' => $album->id, 'assetable_type' => Asset::ASSETABLE_ALBUM, 'parent_id' => NULL])->count(); $bigImages = Asset::find()->where(['assetable_id' => $album->id, 'assetable_type' => Asset::ASSETABLE_ALBUM])->limit(12)->offset(0)->orderBy(['id' => SORT_ASC])->all(); $smallImages = $bigImages; } $columnData['content'] = ['view' => '@frontend/views/site/album', 'data' => compact('album', 'bigImages', 'smallImages', 'imageCount'), 'weight' => 10]; $columnData['comments'] = Comment::getCommentsBlock($album->id, Comment::COMMENTABLE_ALBUM); $columnData['comments']['weight'] = 20; } usort($columnData, 'self::cmp'); return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | ' . $title, 'columnFirst' => $columnData, 'columnSecond' => ['short_news' => SiteBlock::getshortNews(50)]]); }