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']
83 private function _loadComments()
86 $manageCommentsAccess = Yii::app()->user->checkAccess(
'manageComments');
99 $manageCommentsAccess,
100 $isEmailModerator ? Yii::app()->user->email : null
105 if (($comments = Yii::app()->cache->get($cacheId)) ===
false) {
106 $criteria =
new CDbCriteria();
107 $criteria->addCondition(
't.content_id=:content_id');
108 if (!$manageCommentsAccess) {
109 if ($isEmailModerator) {
110 $criteria->addCondition(
'(t.moderator_email=:moderator_email OR t.status=:status)');
111 $criteria->params[
':moderator_email'] = Yii::app()->user->email;
113 $criteria->addCondition(
't.status=:status');
115 $criteria->params[
':status'] = Comment::APPROVED;
117 $criteria->order =
't.createtime DESC';
118 $criteria->params[
':content_id'] = (string)$this->contentId;
122 $paginator =
new CPagination($commentsTotal);
123 $paginator->pageSize = $this->commentsPerPage;
124 $paginator->currentPage = $currentPage;
125 $paginator->applyLimit($criteria);
128 $childComments = array();
129 if ($this->oneLevelReply) {
131 foreach ($comments as $pc) {
134 $criteria->addInCondition(
'parent_id', $pIds);
135 $criteria->order =
't.createtime ASC';
136 $criteria->limit = -1;
137 $criteria->offset = -1;
139 $childrenByParents = array();
140 foreach ($childComments as $cc) {
141 $childrenByParents[$cc->parent_id][] = $cc;
143 foreach ($comments as $pc) {
144 $pc->childs = !empty($childrenByParents[$pc->id]) ? $childrenByParents[$pc->id] : array();
149 if (Yii::app()->db->getDriverName() ==
'mysql') {
151 $dependency =
new CDbCacheDependency(
"SELECT group_concat(id, ' - ' ,status) FROM " .
Comment::model()->tableName() .
" WHERE content_id=:content_id ");
152 $dependency->params[
'content_id'] = $this->contentId;
155 $dependency =
new CExpressionDependency(
156 'Yii::app()->db->createCommand()->select("id, status")->from(Comment::model()->tableName())->where("content_id=:content_id", array("content_id" => ' . $this->contentId .
'))->queryAll()'
160 'comments' => $comments,
161 'countWithChildren' => count($comments) + count($childComments),
162 'total' => $commentsTotal,
164 Yii::app()->cache->set($cacheId, $comments, Yii::app()->getModule(
'comments')->cacheTime, $dependency);
177 return Yii::app()->request->getParam(
"clw{$this->contentId}", 0);
187 return $this->oneLevelReply && $this->formWidget && ($this->formWidget->allowAnonymous || !Yii::app()->user->isGuest);
199 $url = Yii::app()->request->getUrl();
200 $name =
"clw{$this->contentId}";
201 $newParam =
"clw{$this->contentId}=$page";
202 if (($oldValue = Yii::app()->request->getParam($name)) !== null) {
203 $url = str_replace(
"$name=$oldValue", $newParam, $url);
205 if (strpos($url,
'?') !==
false) {
206 $url .=
'&' . $newParam;
208 $url .=
'?' . $newParam;
211 $url .=
'#list_' . $this->contentId;
225 $commentsAttributes = array();
226 foreach ($comments as $parent) {
229 'subject' => $parent->subject
231 foreach ($parent->childs as $child) {
232 $parentData[
'childs'][] = array(
234 'subject' => $child->subject
237 $commentsAttributes[] = $parentData;
239 return $commentsAttributes;
251 return Gravatar::getImage($comment->email, Yii::app()->request->isSecureConnection ?
'https' :
'http');