コード例 #1
0
 /**
  * Action for AJAX form validation
  *
  * @return array
  */
 public function actionIndex()
 {
     $model = new Comment(['scenario' => Yii::$app->user->isGuest ? Comment::SCENARIO_GUEST : Comment::SCENARIO_USER]);
     if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model);
     }
 }
コード例 #2
0
 public function run()
 {
     $orderDirection = $this->parent_id ? CommentsModule::getInstance()->nestedOrderDirection : CommentsModule::getInstance()->orderDirection;
     $pageSize = $this->parent_id ? 0 : CommentsModule::getInstance()->commentsPerPage;
     $dataProvider = new ActiveDataProvider(['query' => Comment::find()->where(['model' => $this->model, 'model_id' => $this->model_id, 'parent_id' => $this->parent_id, 'status' => Comment::STATUS_PUBLISHED]), 'pagination' => ['pageSize' => $pageSize, 'pageParam' => 'comment-page', 'pageSizeParam' => 'comments-per-page'], 'sort' => ['defaultOrder' => ['id' => $orderDirection]]]);
     return $this->render('list', ['dataProvider' => $dataProvider, 'comment' => $this->comment, 'nested_level' => $this->nested_level]);
 }
コード例 #3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Comment::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['status' => $this->status !== NULL ? $this->status : 1]);
     $query->andFilterWhere(['id' => $this->id, 'model_id' => $this->model_id, 'user_id' => $this->user_id, 'parent_id' => $this->parent_id, 'updated_at' => $this->updated_at]);
     switch ($this->created_at_operand) {
         case '=':
             $query->andFilterWhere(['>=', 'created_at', strtotime($this->created_at)]);
             $query->andFilterWhere(['<=', 'created_at', strtotime($this->created_at . ' 23:59:59')]);
             break;
         case '>':
             $query->andFilterWhere(['>', 'created_at', strtotime($this->created_at . ' 23:59:59')]);
             break;
         case '<':
             $query->andFilterWhere(['<', 'created_at', strtotime($this->created_at)]);
             break;
         default:
             break;
     }
     $query->andFilterWhere(['like', 'model', $this->model])->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'user_ip', $this->user_ip]);
     return $dataProvider;
 }
コード例 #4
0
 public function run()
 {
     $commentsAsset = CommentsAsset::register($this->getView());
     CommentModule::getInstance()->commentsAssetUrl = $commentsAsset->baseUrl;
     $model = $this->model;
     $model_id = $this->model_id;
     $comment = new Comment(compact('model', 'model_id'));
     $comment->scenario = Yii::$app->user->isGuest ? Comment::SCENARIO_GUEST : Comment::SCENARIO_USER;
     if ((!CommentModule::getInstance()->onlyRegistered || !Yii::$app->user->isGuest) && $comment->load(Yii::$app->getRequest()->post())) {
         if ($comment->validate() && Yii::$app->getRequest()->validateCsrfToken() && Yii::$app->getRequest()->getCsrfToken(true) && $comment->save()) {
             if (Yii::$app->user->isGuest) {
                 CommentsHelper::setCookies(['username' => $comment->username, 'email' => $comment->email]);
             }
             return Yii::$app->getResponse()->redirect(Yii::$app->request->referrer);
         }
     }
     return $this->render('comments', compact('model', 'model_id', 'comment'));
 }
コード例 #5
0
ファイル: Comments.php プロジェクト: yeesoft/yii2-comments
 public function run()
 {
     $commentsAsset = CommentsAsset::register($this->getView());
     CommentModule::getInstance()->commentsAssetUrl = $commentsAsset->baseUrl;
     $model = $this->model;
     $model_id = $this->model_id;
     $comment = new Comment(compact('model', 'model_id'));
     $comment->scenario = Yii::$app->user->isGuest ? Comment::SCENARIO_GUEST : Comment::SCENARIO_USER;
     if ((!CommentModule::getInstance()->onlyRegistered || !Yii::$app->user->isGuest) && $comment->load(Yii::$app->getRequest()->post())) {
         if ($comment->validate() && Yii::$app->getRequest()->validateCsrfToken() && Yii::$app->getRequest()->getCsrfToken(true) && $comment->checkSpam() && $comment->save()) {
             if (Yii::$app->user->isGuest) {
                 CommentsHelper::setCookies(['username' => $comment->username, 'email' => $comment->email]);
             }
             Yii::$app->getResponse()->redirect(Yii::$app->request->referrer);
             return;
         }
     }
     $dataProvider = new ActiveDataProvider(['query' => Comment::find(true)->where(['model' => $model, 'model_id' => $model_id, 'parent_id' => NULL, 'status' => Comment::STATUS_PUBLISHED]), 'pagination' => ['pageSize' => CommentsModule::getInstance()->commentsPerPage, 'pageParam' => 'comment-page', 'pageSizeParam' => 'comments-per-page'], 'sort' => ['defaultOrder' => ['created_at' => CommentsModule::getInstance()->orderDirection]]]);
     return $this->render('comments', compact('model', 'model_id', 'comment', 'dataProvider'));
 }
コード例 #6
0
ファイル: Comments.php プロジェクト: yeesoft/yii2-yee-comment
 public function run()
 {
     if (!$this->options) {
         $this->options = $this->getDefaultOptions();
     }
     if (User::hasPermission('viewComments')) {
         $searchModel = new CommentSearch();
         $formName = $searchModel->formName();
         $recentComments = Comment::find()->active()->orderBy(['id' => SORT_DESC])->limit($this->recentLimit)->all();
         foreach ($this->options as &$option) {
             $count = Comment::find()->filterWhere($option['filterWhere'])->count();
             $option['count'] = $count;
             $option['url'] = [$this->commentIndexAction, $formName => $option['filterWhere']];
         }
         return $this->render('comments', ['height' => $this->height, 'width' => $this->width, 'position' => $this->position, 'comments' => $this->options, 'recentComments' => $recentComments]);
     }
 }
コード例 #7
0
ファイル: comments.php プロジェクト: totojack/yii2-comments
/* @var $this yii\web\View */
/* @var $model yeesoft\comments\models\Comment */
$commentsPage = Yii::$app->getRequest()->get("comment-page", 1);
$cacheKey = 'comment' . $model . $model_id . $commentsPage . substr(\Yii::$app->language, 0, 2);
//SIMONE forse non indispensabile, ma logico
$cacheProperties = CommentsHelper::getCacheProperties($model, $model_id);
?>
<div class="comments">
    <?php 
if ($this->beginCache($cacheKey . '-count', $cacheProperties)) {
    ?>
        <h5><?php 
    echo Comments::t('comments', 'All Comments');
    ?>
 (<?php 
    echo Comment::activeCount($model, $model_id);
    ?>
)</h5>
        <?php 
    $this->endCache();
    ?>
    <?php 
}
?>

    <?php 
if (!Comments::getInstance()->onlyRegistered || !Yii::$app->user->isGuest) {
    ?>
        <div class="comments-main-form">
            <?php 
    echo CommentsForm::widget();
コード例 #8
0
ファイル: Comment.php プロジェクト: yeesoft/yii2-comments
 /**
  * Get count of active comments by $model and $model_id
  *
  * @param string $model
  * @param int $model_id
  * @return int
  */
 public static function activeCount($model, $model_id = NULL)
 {
     return Comment::find()->where(['model' => $model, 'model_id' => $model_id])->active()->count();
 }
コード例 #9
0
ファイル: Trip.php プロジェクト: yeesoft/yii2-yee-trip
 public function getCommentsCount()
 {
     try {
         return Comment::find()->where(['model' => Trip::className()])->andWhere(['model_id' => $this->primaryKey])->andWhere(['status' => Comment::STATUS_APPROVED])->count();
     } catch (Exception $exc) {
         return 0;
     }
 }
コード例 #10
0
ファイル: index.php プロジェクト: yeesoft/yii2-yee-comment
                    <?php 
echo GridPageSize::widget(['pjaxId' => 'comment-grid-pjax']);
?>
                </div>
            </div>

            <?php 
Pjax::begin(['id' => 'comment-grid-pjax']);
?>

            <?php 
echo GridView::widget(['id' => 'comment-grid', 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'bulkActionOptions' => ['gridId' => 'comment-grid', 'actions' => [Url::to(['bulk-activate']) => Comments::t('comments', 'Approve'), Url::to(['bulk-deactivate']) => Comments::t('comments', 'Unapprove'), Url::to(['bulk-spam']) => Comments::t('comments', 'Mark as Spam'), Url::to(['bulk-trash']) => Comments::t('comments', 'Move to Trash'), Url::to(['bulk-delete']) => Yii::t('yee', 'Delete')]], 'columns' => [['class' => 'yeesoft\\grid\\CheckboxColumn', 'options' => ['style' => 'width:10px']], ['class' => 'yeesoft\\grid\\columns\\TitleActionColumn', 'controller' => '/comment/default', 'attribute' => 'content', 'title' => function (Comment $model) {
    return Html::a(mb_substr($model->content, 0, 32) . '...', ['/comment/default/update', 'id' => $model->id], ['data-pjax' => 0]);
}, 'buttonsTemplate' => '{update} {delete}'], ['label' => Yii::t('yee', 'User'), 'value' => function (Comment $model) {
    return $model->getAuthor();
}, 'options' => ['style' => 'width:150px']], ['attribute' => 'model', 'value' => function (Comment $model) {
    return $model->model . ($model->model_id ? ' [' . $model->model_id . ']' : '');
}, 'options' => ['style' => 'width:120px']], ['class' => 'yeesoft\\grid\\columns\\StatusColumn', 'attribute' => 'status', 'optionsArray' => Comment::getStatusOptionsList(), 'options' => ['style' => 'width:60px']], ['class' => 'yeesoft\\grid\\columns\\DateFilterColumn', 'attribute' => 'created_at', 'value' => function (Comment $model) {
    return '<span style="font-size:85%;" class="label label-' . (time() >= $model->created_at ? 'primary' : 'default') . '">' . $model->createdDate . ' ' . $model->createdTime . '</span>';
}, 'format' => 'raw', 'options' => ['style' => 'width:150px']], ['attribute' => 'user_ip', 'options' => ['style' => 'width:100px']]]]);
?>

            <?php 
Pjax::end();
?>
        </div>
    </div>
</div>


コード例 #11
0
 /**
  * Generate settings for comments caching
  *
  * @param string $model comment's model name
  * @param int $model_id comment's model id
  * @param int $duration
  * @return array
  */
 public static function getCacheProperties($model, $model_id = '', $duration = 3600)
 {
     $tableName = Comment::tableName();
     return ['duration' => $duration, 'dependency' => ['class' => 'yii\\caching\\DbDependency', 'sql' => "SELECT COUNT(*) FROM {$tableName} " . "WHERE `model` = '{$model}' AND `model_id` = '{$model_id}'"]];
 }
コード例 #12
0
 public function run()
 {
     $recentComments = Comment::find()->active()->orderBy(['created_at' => SORT_DESC])->limit($this->recentLimit)->all();
     return $this->render($this->layout, ['recentComments' => $recentComments, 'commentTemplate' => $this->commentTemplate]);
 }
コード例 #13
0
ファイル: _form.php プロジェクト: yeesoft/yii2-yee-comment
?>

<div class="comment-form">

    <?php 
$form = ActiveForm::begin(['id' => 'comment-form', 'validateOnBlur' => false]);
?>

    <div class="row">
        <div class="col-md-9">

            <div class="panel panel-default">
                <div class="panel-body">

                    <?php 
echo $form->field($model, 'status')->dropDownList(Comment::getStatusList());
?>

                    <?php 
echo $form->field($model, 'content')->textarea(['rows' => 6]);
?>

                </div>

            </div>
        </div>

        <div class="col-md-3">

            <div class="panel panel-default">
                <div class="panel-body">