Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typehead search button and improve article excerpt #342

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions common/helpers/StringHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace common\helpers;

use yii\helpers\BaseStringHelper;
/**
* StringHelper
*
* @author Qiang Xue <[email protected]>
* @author Alex Makarov <[email protected]>
* @since 2.0
*/
class StringHelper extends BaseStringHelper
{

public static function Excerpt($string, $lines, $suffix, $wordsPerLine=80, $encoding = false)
{
return static::truncateHtml($string, $lines, $suffix, $wordsPerLine, $encoding);
}

protected static function truncateHtml($string, $count, $suffix, $wordsPerLine, $encoding)
{
$config = \HTMLPurifier_Config::create(null);
$config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
$lexer = \HTMLPurifier_Lexer::create($config);
$tokens = $lexer->tokenizeHTML($string, $config, null);
$openTokens = 0;
$totalCount = 0;
$truncated = [];
foreach ($tokens as $token) {
if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
$openTokens++;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
if (false === $encoding) {
$token->data = self::truncateWords($token->data, ($count - $totalCount)*$wordsPerLine, '');
$currentWords = str_word_count($token->data);
} else {
$token->data = self::truncate($token->data, ($count - $totalCount)*$wordsPerLine, '', $encoding) . ' ';
$currentWords = mb_strlen($token->data, $encoding);
}
//$totalCount += $currentWords;
if(!$token->is_whitespace)
$totalCount += intval(ceil($currentWords/$wordsPerLine));//turn into lines
if (1 === $currentWords) {
$token->data = ' ' . $token->data;
}
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
$openTokens--;
$truncated[] = $token;
} elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
if($token->name=='img')
{
//filter img tag
}
else
$truncated[] = $token;
}
if (0 === $openTokens && $totalCount >= $count) {
break;
}
}
$context = new \HTMLPurifier_Context();
$generator = new \HTMLPurifier_Generator($config, $context);
return $generator->generateFromTokens($truncated) . $suffix;
}

}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
"bower-asset/font-awesome": "^4.0",
"bower-asset/html5shiv": "^3.0",
"bower-asset/jquery-slimscroll": "^1.3",
"bower-asset/flot": "^0.8"
"bower-asset/flot": "^0.8",

"kartik-v/yii2-widgets": "*",
},
"require-dev": {
"yiisoft/yii2-debug": "^2.0.0",
Expand Down
20 changes: 18 additions & 2 deletions frontend/controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use frontend\models\search\ArticleSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

use yii\helpers\Json;
/**
* @author Eugene Terentev <[email protected]>
*/
Expand All @@ -25,7 +25,7 @@ public function actionIndex()
];
return $this->render('index', ['dataProvider'=>$dataProvider]);
}

/**
* @param $slug
* @return string
Expand Down Expand Up @@ -54,4 +54,20 @@ public function actionAttachmentDownload($id)
$model->name
);
}

public function actionSearchList($q = null) {

$out = [];
if($q){
$query = Article::find()->where(['LIKE', 'title', $q])->orWhere(['LIKE', 'body', $q]);
$data=$query->limit(10)->all();
}

foreach ($data as $d) {
$out[] = ['value' => $d['title']];
}
echo Json::encode($out);
}


}
13 changes: 13 additions & 0 deletions frontend/models/search/ArticleSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public function search($params)
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

//q=word
if(!empty($params['q']))
{
$q=$params['q'];
//$q="��";
//$query2=Article::find();
//$query2->andWhere(['LIKE', 'title', $q]);
//$t=$query2->all();

$query->andWhere(['LIKE', 'title', $q])->orWhere(['LIKE', 'body', $q]);
return $dataProvider;
}

if (!($this->load($params) && $this->validate())) {
return $dataProvider;
Expand Down
8 changes: 6 additions & 2 deletions frontend/views/article/_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @var $model common\models\Article
*/
use yii\helpers\Html;

use Symfony\Component\DomCrawler\Crawler;
use common\helpers\StringHelper;
?>
<hr/>
<div class="article-item row">
Expand Down Expand Up @@ -35,7 +36,10 @@
) ?>
<?php endif; ?>
<div class="article-text">
<?php echo \yii\helpers\StringHelper::truncate($model->body, 150, '...', null, true) ?>
<?php
//excerpt 5 lines and filter img
echo StringHelper::Excerpt($model->body, 5, '...', 90, Yii::$app->charset);
?>
</div>
</div>
</div>
Expand Down
43 changes: 43 additions & 0 deletions frontend/views/article/_typehead.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
use yii\helpers\Html;
//use yii\bootstrap\ActiveForm;
use kartik\form\ActiveForm;
use kartik\widgets\Typeahead;
use yii\helpers\Url;
?>

<div class="system-article-search">
<?php
$form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'type' => ActiveForm::TYPE_INLINE,
]);
?>

<div class="input-group pull-right">
<span class="input-group-btn">
<?php
echo Typeahead::widget([
'name' => 'q',
'options' => ['placeholder' => 'search articles...'],
'scrollable' => true,
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
'display' => 'value',
//'prefetch' => $baseUrl . '/samples/countries.json',
'remote' => [
'url' => Url::to(['article/search-list']) . '?q=%QUERY',
'wildcard' => '%QUERY'
]
]
]
]);
?>
</span>
<?php echo Html::submitButton(Yii::t('backend', 'Search'), ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
1 change: 1 addition & 0 deletions frontend/views/article/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* @var $this yii\web\View */
$this->title = Yii::t('frontend', 'Articles')
?>
<?php echo $this->render('_typehead');?>
<div id="article-index">
<h1><?php echo Yii::t('frontend', 'Articles') ?></h1>
<?php echo \yii\widgets\ListView::widget([
Expand Down
3 changes: 3 additions & 0 deletions frontend/views/article/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
$this->params['breadcrumbs'][] = ['label' => Yii::t('frontend', 'Articles'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<?php echo $this->render('_typehead'); ?>

<div class="content">
<article class="article-item">
<h1><?php echo $model->title ?></h1>
Expand Down