17 public $oneLevelReply =
false;
32 public $commentsPerPage = 5;
37 public $paginationPagesCount = 5;
47 $assetsUrl = Yii::app()->getModule(
'comments')->getAssetsUrl();
48 Yii::app()->clientScript->registerCssFile($assetsUrl .
'/css/pagination.css');
49 Yii::app()->clientScript->registerScriptFile($assetsUrl .
'/js/initCommentsReply.js');
52 'contentId' => $this->contentId
54 $this->hash = md5(serialize($options) . Yii::app()->getModule(
'comments')->hashSalt);
55 if (Yii::app()->cache->get($this->hash) != $options) {
56 Yii::app()->cache->set($this->hash, $options);
67 $comments = $this->_loadComments();
71 'comments' => $comments[
'comments'],
72 'commentsTotal' => $comments[
'total'],
73 'commentsCountWithChildren' => $comments[
'countWithChildren'],
74 'pages' => $comments[
'pages']
84 private function _loadComments()
87 $manageCommentsAccess = Yii::app()->user->checkAccess(
'manageComments');
100 $manageCommentsAccess,
101 $isEmailModerator ? Yii::app()->user->email : null
106 if (($comments = Yii::app()->cache->get($cacheId)) ===
false) {
107 $criteria =
new CDbCriteria();
108 $criteria->addCondition(
't.content_id=:content_id');
109 if (!$manageCommentsAccess) {
110 if ($isEmailModerator) {
111 $criteria->addCondition(
'(t.moderator_email=:moderator_email OR t.status=:status)');
112 $criteria->params[
':moderator_email'] = Yii::app()->user->email;
114 $criteria->addCondition(
't.status=:status');
116 $criteria->params[
':status'] = Comment::APPROVED;
118 $criteria->order =
't.createtime DESC';
119 $criteria->params[
':content_id'] = (string)$this->contentId;
123 $paginator =
new CPagination($commentsTotal);
124 $paginator->pageSize = $this->commentsPerPage;
125 $paginator->currentPage = $currentPage;
126 $paginator->applyLimit($criteria);
129 $childComments = array();
130 if ($this->oneLevelReply) {
132 foreach ($comments as $pc) {
135 $criteria->addInCondition(
'parent_id', $pIds);
136 $criteria->order =
't.createtime ASC';
137 $criteria->limit = -1;
138 $criteria->offset = -1;
140 $childrenByParents = array();
141 foreach ($childComments as $cc) {
142 $childrenByParents[$cc->parent_id][] = $cc;
144 foreach ($comments as $pc) {
145 $pc->childs = !empty($childrenByParents[$pc->id]) ? $childrenByParents[$pc->id] : array();
150 if (Yii::app()->db->getDriverName() ==
'mysql') {
152 $dependency =
new CDbCacheDependency(
"SELECT group_concat(id, ' - ' ,status) FROM " .
Comment::model()->tableName() .
" WHERE content_id=:content_id ");
153 $dependency->params[
'content_id'] = $this->contentId;
156 $dependency =
new CExpressionDependency(
157 'Yii::app()->db->createCommand()->select("id, status")->from(Comment::model()->tableName())->where("content_id=:content_id", array("content_id" => ' . $this->contentId .
'))->queryAll()'
161 'comments' => $comments,
162 'countWithChildren' => count($comments) + count($childComments),
163 'total' => $commentsTotal,
164 'pages' => $paginator
166 Yii::app()->cache->set($cacheId, $comments, Yii::app()->getModule(
'comments')->cacheTime, $dependency);
179 return Yii::app()->request->getParam(
"clw{$this->contentId}", 0);
189 return $this->oneLevelReply && $this->formWidget && ($this->formWidget->allowAnonymous || !Yii::app()->user->isGuest);
201 $url = Yii::app()->request->getUrl();
202 $name =
"clw{$this->contentId}";
203 $newParam =
"clw{$this->contentId}=$page";
204 if (($oldValue = Yii::app()->request->getParam($name)) !== null) {
205 $url = str_replace(
"$name=$oldValue", $newParam, $url);
207 if (strpos($url,
'?') !==
false) {
208 $url .=
'&' . $newParam;
210 $url .=
'?' . $newParam;
213 $url .=
'#list_' . $this->contentId;
227 $commentsAttributes = array();
228 foreach ($comments as $parent) {
231 'subject' => $parent->subject
233 foreach ($parent->childs as $child) {
234 $parentData[
'childs'][] = array(
236 'subject' => $child->subject
239 $commentsAttributes[] = $parentData;
241 return $commentsAttributes;
253 return Gravatar::getImage($comment->email, Yii::app()->request->isSecureConnection ?
'https' :
'http');