Browse Source

Dashboard search

Search Performance
Frontend blog tags showing fix
master
Egorka 6 years ago
parent
commit
c9513f031a
  1. 41
      backend/controllers/SiteController.php
  2. 1
      backend/messages/ru/main.php
  3. 43
      backend/views/layouts/left.php
  4. 41
      backend/views/site/search.php
  5. 5
      common/bootstrap/SetUp.php
  6. 7
      common/modules/blog/BlogModule.php
  7. 4
      common/modules/blog/urls/BlogTagUrlRule.php
  8. 1
      common/modules/blog/views/post/post.php
  9. 20
      console/controllers/SearchInitController.php
  10. 19
      core/components/SearchPerformance.php
  11. 26
      core/entities/Search.php
  12. 21
      core/forms/SearchForm.php
  13. 2
      frontend/web/themes/start/modules/blog/views/post/post.php

41
backend/controllers/SiteController.php

@ -1,7 +1,10 @@
<?php
namespace backend\controllers;
use core\entities\Search;
use core\forms\SearchForm;
use Yii;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
@ -19,14 +22,14 @@ class SiteController extends Controller
{
return [
'access' => [
'class' => AccessControl::className(),
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['error'],
'allow' => true,
],
[
'actions' => ['index'],
'actions' => ['index', 'search'],
'allow' => true,
'roles' => ['Dashboard'],
],
@ -37,7 +40,7 @@ class SiteController extends Controller
],
],
'verbs' => [
'class' => VerbFilter::className(),
'class' => VerbFilter::class,
'actions' => [
'logout' => ['post'],
],
@ -67,6 +70,38 @@ class SiteController extends Controller
return $this->render('index');
}
public function actionSearch()
{
$form = new SearchForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
$query = Search::find()
->andWhere(['LIKE', 'title', $form->query])
->orWhere(['LIKE', 'content', $form->query]);
$provider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
'sort' => [],
]);
return $this->render('search', [
'provider' => $provider,
'form' => $form
]);
//$page = $this->service->create($form);
//return $this->redirect(['view', 'id' => $page->id]);
} catch (\DomainException $e) {
//Yii::$app->errorHandler->logException($e);
//Yii::$app->session->setFlash('error', $e->getMessage());
}
}
}
public function beforeAction($action)
{
if ($action->id === 'error') {

1
backend/messages/ru/main.php

@ -25,4 +25,5 @@ return [
'Editing' => 'Редактирование',
'Change at your own risk' => 'Редактируйте на свой страх и риск',
'Online' => 'В сети',
'Search results' => 'Поиск',
];

43
backend/views/layouts/left.php

@ -1,3 +1,13 @@
<?php
//use yii\widgets\ActiveForm;
use kartik\form\ActiveForm;
use core\forms\SearchForm;
use yii\helpers\Html;
$model = new SearchForm();
?>
<aside class="main-sidebar">
<section class="sidebar">
@ -15,15 +25,30 @@
</div>
<!-- search form -->
<form action="#" method="get" class="sidebar-form">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search..."/>
<span class="input-group-btn">
<button type='submit' name='search' id='search-btn' class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
<?php $form = ActiveForm::begin([
'action' => \yii\helpers\Url::to(['/site/search']),
'options' => [
'class' => 'sidebar-form'
],
'enableClientValidation' => false,
'fieldConfig' => [
'options' => [
'tag' => false,
],
],
]); ?>
<?= $form->field($model, 'query', [
'addon' => [
'append' => [
'content' => Html::button('<i class="fa fa-search"></i>', ['class'=>'btn btn-flat', 'id' => 'search-btn']),
'asButton' => true
]
],
'template' => "{input}",
])->textInput(['placeholder' => 'Search...'])->label(false)->hint(false); ?>
<?php ActiveForm::end(); ?>
<!-- /.search form -->
<?= dmstr\widgets\Menu::widget(

41
backend/views/site/search.php

@ -0,0 +1,41 @@
<?php
use core\entities\Search;
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\StringHelper;
/* @var $this yii\web\View */
/* @var $provider yii\data\ActiveDataProvider */
/* @var $form \core\forms\SearchForm */
$title = Yii::t('main', 'Search results');
$this->title = $title . ' > ' . Html::encode($form->query);
$this->params['breadcrumbs'][] = $title;
?>
<div class="blog-post-index">
<div class="box">
<div class="box-body">
<?= GridView::widget([
'dataProvider' => $provider,
'showHeader' => false,
'options' => [
'style' => 'word-wrap:break-word;',
],
'tableOptions' => [
'class' => 'table',
],
'columns' => [
[
'attribute' => 'title',
'value' => function (Search $model) {
return Html::a(Html::tag('h3', $model->title), [$model->url]) . StringHelper::truncateWords(strip_tags($model->content), 60, '...');
},
'format' => 'raw',
],
],
]); ?>
</div>
</div>
</div>

5
common/bootstrap/SetUp.php

@ -146,5 +146,10 @@ class SetUp implements BootstrapInterface
Yii::$app->getModule($module->name)->bootstrap(Yii::$app);
}
}
// todo move that to pages module
// pages search
$app->params['search_rules'][] = "SELECT title, content, CONCAT('/page/', id) AS url FROM {{pages}}";
}
}

7
common/modules/blog/BlogModule.php

@ -27,6 +27,9 @@ class BlogModule extends \yii\base\Module implements ModuleInterface
public function bootstrap($app)
{
// add search rules
$app->params['search_rules'][] = "SELECT title, content, CONCAT('/blog/manage/post/view/', id) AS url FROM {{blog_posts}}";
// add rules
$app->getUrlManager()->addRules([
'blog' => 'blog/post/index',
@ -38,6 +41,10 @@ class BlogModule extends \yii\base\Module implements ModuleInterface
['class' => 'common\modules\blog\urls\BlogTagUrlRule'],
]);
$app->getUrlManager()->addRules([
'blog/manage/post/view/<id:\d+>' => 'blog/manage/post/view',
]);
// add languages
$app->getI18n()->translations = ArrayHelper::merge($app->getI18n()->translations, [
'blog' => [

4
common/modules/blog/urls/BlogTagUrlRule.php

@ -56,7 +56,7 @@ class BlogTagUrlRule extends BaseObject implements UrlRuleInterface
{
if ($route == 'blog/post/tag') {
if (empty($params['id'])) {
throw new InvalidParamException('Empty id.');
throw new \InvalidArgumentException('Empty id.');
}
$id = $params['id'];
@ -69,7 +69,7 @@ class BlogTagUrlRule extends BaseObject implements UrlRuleInterface
}, null, new TagDependency(['tags' => ['blog']]));
if (!$url) {
throw new InvalidParamException('Undefined id.');
throw new \InvalidArgumentException('Undefined id.');
}
$url = $this->prefix . '/' . $url;

1
common/modules/blog/views/post/post.php

@ -104,7 +104,6 @@ $this->registerJs($js, $this::POS_READY);
'post' => $post,
]) ?>
<ul class="list-tags" style="margin-top:20px"><?= implode(', ', $tagLinks) ?></ul>
<?= CommentsWidget::widget([

20
console/controllers/SearchInitController.php

@ -0,0 +1,20 @@
<?php
/**
* Created by Error202
* Date: 05.07.2018
*
* Init / Update search mysql performance
*/
namespace console\controllers;
use core\components\SearchPerformance;
use yii\console\Controller;
class SearchInitController extends Controller {
public function actionInit() {
SearchPerformance::init();
echo "Search performance complete" . PHP_EOL;
}
}

19
core/components/SearchPerformance.php

@ -0,0 +1,19 @@
<?php
/**
* Created by Error202
* Date: 05.07.2018
*/
namespace core\components;
use Yii;
class SearchPerformance {
public static function init()
{
$connection = Yii::$app->getDb();
$command = $connection->createCommand("
CREATE OR REPLACE VIEW view_search AS " . implode(' UNION ', Yii::$app->params['search_rules']));
$command->execute();
}
}

26
core/entities/Search.php

@ -0,0 +1,26 @@
<?php
/**
* Created by Error202
* Date: 05.07.2018
*/
namespace core\entities;
use yii\db\ActiveRecord;
/**
* Class Search
* @package core\entities
*
* @property string $title
* @property string $content
* @property string $url
*/
class Search extends ActiveRecord
{
public static function tableName(): string
{
return '{{view_search}}';
}
}

21
core/forms/SearchForm.php

@ -0,0 +1,21 @@
<?php
/**
* Created by Error202
* Date: 10.02.2018
*/
namespace core\forms;
use yii\base\Model;
class SearchForm extends Model
{
public $query;
public function rules() {
return [
['query', 'required'],
['query', 'string'],
];
}
}

2
frontend/web/themes/start/modules/blog/views/post/post.php

@ -20,7 +20,7 @@ $this->params['active_category'] = $post->category;
$tagLinks = [];
foreach ($post->tags as $tag) {
$tagLinks[] = '<li>' . Html::a(Html::encode($tag->name), ['tag', 'slug' => $tag->slug]) . '</li>';
$tagLinks[] = '<li>' . Html::a(Html::encode($tag->name), ['tag', 'id' => $tag->id]) . '</li>';
}
$url = Url::to(['/blog/post/post', 'id' =>$post->id]);

Loading…
Cancel
Save