/** * After save update cache of blocks * @inheritdoc */ public function afterSave($insert, $changedAttributes) { $machineName = 'lastBlogPosts'; $cacheBlock = CacheBlock::find()->where(['machine_name' => $machineName])->one(); if (!isset($cacheBlock)) { $cacheBlock = new CacheBlock(); $cacheBlock->machine_name = $machineName; } $cacheBlock->content = SiteBlock::getBlogPosts(false); $cacheBlock->save(); return parent::afterSave($insert, $changedAttributes); }
/** * Url: /post/edit/{$id} * @param $id int Post id * @return mixed * @throws ForbiddenHttpException * @throws NotFoundHttpException */ public function actionPostEdit($id) { $model = Post::findOne($id); if (!isset($model)) { throw new NotFoundHttpException('Страница не найдена.'); } if ($model->content_category_id != Post::CATEGORY_BLOG || empty(Yii::$app->user) || $model->user_id != Yii::$app->user->id) { throw new ForbiddenHttpException("Вы не можете выполнить это действие."); } $image = $model->getAsset(); $tags = $model->getTags(); $assets = $model->getAssets(); $model->tags = []; foreach ($tags as $tag) { $model->tags[] = $tag->id; } $model->title = html_entity_decode($model->title); $model->content = html_entity_decode($model->content); if ($model->load(Yii::$app->request->post()) && $model->validate()) { // Set slug $model->slug = $model->genSlug($model->title); // Set image $uploadedFile = UploadedFile::getInstance($model, 'image'); if ($uploadedFile) { // Remove old assets foreach ($assets as $asset) { $asset->delete(); } // Save origionals $asset = new Asset(); $asset->assetable_type = Asset::ASSETABLE_POST; $asset->assetable_id = $model->id; $asset->uploadedFile = $uploadedFile; $asset->saveAsset(); // Save thumbnails $imageID = $asset->id; $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_POST); foreach ($thumbnails as $thumbnail) { $asset = new Asset(); $asset->parent_id = $imageID; $asset->thumbnail = $thumbnail; $asset->assetable_type = Asset::ASSETABLE_POST; $asset->assetable_id = $model->id; $asset->uploadedFile = $uploadedFile; $asset->saveAsset(); } } $existingTags = []; // Remove tags foreach ($tags as $tag) { if (!is_array($model->tags) || !in_array($tag->id, $model->tags)) { $model->removeTag($tag->id); } else { $existingTags[] = $tag->id; } } // Adding new tags if (is_array($model->tags)) { foreach ($model->tags as $id) { if (!in_array($id, $existingTags)) { $model->addTag($id); } } } $cached_tag_list = []; $newTags = $model->getTags(); foreach ($newTags as $newTag) { $cached_tag_list[] = $newTag->name; } $model->cached_tag_list = implode(', ', $cached_tag_list); $model->save(); return $this->redirect($model->getUrl()); } $title = 'Изменить запись в блоге'; return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | ' . $title, 'columnFirst' => ['blog_form' => ['view' => '@frontend/views/forms/blog_form', 'data' => compact('model', 'tags', 'image', 'title')]], 'columnSecond' => ['blog' => SiteBlock::getBlogPosts(), 'banner1' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner2' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner3' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner4' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner5' => SiteBlock::getBanner(Banner::REGION_NEWS)]]); }
/** * Reset password */ public function actionReset($key) { /** @var \common\modules\user\models\User $user */ /** @var \common\modules\user\models\UserKey $userKey */ // check for valid userKey $userKey = Yii::$app->getModule("user")->model("UserKey"); $userKey = $userKey::findActiveByKey($key, $userKey::TYPE_PASSWORD_RESET); if (!$userKey) { return $this->render('reset', ["invalidKey" => true]); } // get user and set "reset" scenario $success = false; $user = Yii::$app->getModule("user")->model("User"); $user = $user::findOne($userKey->user_id); $user->setScenario("reset"); // load post data and reset user password if ($user->load(Yii::$app->request->post()) && $user->save()) { // consume userKey and set success = true $userKey->consume(); $success = true; } // render // return $this->render('reset', compact("user", "success")); return $this->render('@frontend/views/site/index', ['templateType' => 'col3', 'title' => Yii::t('user', 'Вход'), 'columnFirst' => ['top3News' => SiteBlock::getTop3News(), 'top6News' => SiteBlock::getTop6News(), 'subscribing' => SiteBlock::getSubscribingForm(), 'blog_column' => SiteBlock::getBlogPosts()], 'columnSecond' => ['forgot_block' => ['view' => 'reset', 'data' => compact('user', 'success')], 'short_news' => SiteBlock::getShortNews()], 'columnThird' => ['reviewNews' => SiteBlock::getPhotoVideoNews(), 'questionBlock' => SiteBlock::getQuestionBlock(), 'tournament' => SiteBlock::getTournamentTable()]]); }
/** * After save and before delete update cache of blocks */ public function updateCacheBlocks($changedAttributes) { $newsPosts50 = Post::find()->where(['is_public' => 1, 'content_category_id' => Post::CATEGORY_NEWS])->orderBy(['created_at' => SORT_DESC])->limit(50)->all(); $newsPosts20 = array_slice($newsPosts50, 0, 20); $cacheBlocksData = CacheBlock::find()->all(); $cacheBlocks = []; foreach ($cacheBlocksData as $block) { $cacheBlocks[$block->machine_name] = $block; } $cacheStatus = false; if (isset($changedAttributes['content_category_id']) && $changedAttributes['content_category_id'] == self::CATEGORY_NEWS && $this->content_category_id != self::CATEGORY_NEWS || $this->content_category_id == self::CATEGORY_NEWS) { $machineName = 'shortNews50'; $enableBanners = false; if (!isset($cacheBlocks[$machineName])) { $cacheBlock = new CacheBlock(); $cacheBlock->machine_name = $machineName; } else { $cacheBlock = $cacheBlocks[$machineName]; } $cacheBlock->content = SiteBlock::getShortNews(50, $enableBanners, $cacheStatus, $newsPosts50); $cacheBlock->save(); $machineName = 'shortNews50banners'; $enableBanners = true; SiteBlock::$postedBannerIds = []; if (!isset($cacheBlocks[$machineName])) { $cacheBlock = new CacheBlock(); $cacheBlock->machine_name = $machineName; } else { $cacheBlock = $cacheBlocks[$machineName]; } $cacheBlock->content = SiteBlock::getShortNews(50, $enableBanners, $cacheStatus, $newsPosts50); $cacheBlock->save(); } if (isset($changedAttributes['is_index']) && $this->is_index != $changedAttributes['is_index'] || isset($changedAttributes['is_top']) && $this->is_top != $changedAttributes['is_top'] || $this->is_top || $this->is_index) { $machineName = 'top3News'; if (!isset($cacheBlocks[$machineName])) { $cacheBlock = new CacheBlock(); $cacheBlock->machine_name = $machineName; } else { $cacheBlock = $cacheBlocks[$machineName]; } $cacheBlock->content = SiteBlock::getTop3News($cacheStatus); $cacheBlock->save(); $machineName = 'top6News'; if (!isset($cacheBlocks[$machineName])) { $cacheBlock = new CacheBlock(); $cacheBlock->machine_name = $machineName; } else { $cacheBlock = $cacheBlocks[$machineName]; } $cacheBlock->content = SiteBlock::getTop6News($cacheStatus); $cacheBlock->save(); } if ($this->content_category_id == self::CATEGORY_BLOG) { $machineName = 'lastBlogPosts'; if (!isset($cacheBlocks[$machineName])) { $cacheBlock = new CacheBlock(); $cacheBlock->machine_name = $machineName; } else { $cacheBlock = $cacheBlocks[$machineName]; } $cacheBlock->content = SiteBlock::getBlogPosts($cacheStatus); $cacheBlock->save(); $machineName = 'blogPostsByRating'; if (!isset($cacheBlocks[$machineName])) { $cacheBlock = new CacheBlock(); $cacheBlock->machine_name = $machineName; } else { $cacheBlock = $cacheBlocks[$machineName]; } $cacheBlock->content = SiteBlock::getBlogPostsByRating($cacheStatus); $cacheBlock->save(); } }