Вложенные комментарии — различия между версиями
Материал из Umicms
VITL' (обсуждение | вклад) (Новая страница: «В модуле Блоги 2.0. есть возможность оставлять комментарии к комментарию. В этой статье буд…») |
(нет различий)
|
Версия 19:45, 29 июля 2010
В модуле Блоги 2.0. есть возможность оставлять комментарии к комментарию. В этой статье будет представлен пример кастомного макроса, который позволяет использовать практически идентичную функциональность в модуле "Комментарии". Пропишем необходимый код в файле /classes/modules/comments/__custom.php, после слов "//TODO: Write here your own macroses":
public function commentsList($postId = false, $template = 'default') {
list($sTemplateBlock, $sTemplateLine, $template_add_user, $template_add_guest, $template_smiles) = def_module::loadTemplates('tpls/comments/'.$template.'.tpl', 'comments_list_block', 'comments_list_line', "comments_block_add_user", "comments_block_add_guest", "smiles");
$oHierarchy = umiHierarchy::getInstance();
$iCommentHTypeId = umiHierarchyTypesCollection::getInstance()->getTypeByName("comments", "comment")->getId();
$aLines = $this->placeComments($postId, $sTemplateLine, $oHierarchy, $iCommentHTypeId);
$aBlockParam = array();
$oParent = $oHierarchy->getElement($postId);
$regedit = regedit::getInstance();
if(cmsController::getInstance()->getModule("users")->is_auth()) {
$template_add = $template_add_user;
} else {
$template_add = ($regedit->getVal("//modules/comments/allow_guest")) ? $template_add_guest : "";
}
$add_arr = Array();
$aBlockParam['void:smiles'] = $template_smiles;
$aBlockParam['action'] = "/comments/post/" . $postId . "/";
$template_add = def_module::parseTemplate($template_add, $aBlockParam, $postId);
if($oParent instanceof umiHierarchyElement) {
$aBlockParam['add_form'] = ($oParent->getValue('comments_disallow')) ? '' : $template_add;
} else {
$aBlockParam['add_form'] = $template_add;
}
$total= umiHierarchy::getInstance()->getChildsCount($postId,false,true,1,$iCommentHTypeId);
$aBlockParam['action'] = "/comments/post/" . $postId . "/";
$aBlockParam['subnodes:items'] = $aBlockParam['void:lines'] = $aLines;
$per_page = (int) $regedit->getVal("//modules/comments/per_page");
$aBlockParam['per_page'] = $per_page;
$aBlockParam['total'] = $total;
return def_module::parseTemplate($sTemplateBlock, $aBlockParam);
}
public function placeComments($parentId, $templateString, umiHierarchy $hierarchy, $commentHType) {
static $postHType = 0;
if(!$postHType) {
$postHType = umiHierarchyTypesCollection::getInstance()->getTypeByName('comments', 'comment')->getId();
}
$parent = $hierarchy->getElement($parentId, true);
if(!($parent instanceof umiHierarchyElement)) {
throw new publicException("Unknown parent element for comments");
}
$rootComments = ($parent->getTypeId() != $postHType);
$sel = new umiSelection;
$sel->addElementType($commentHType);
$sel->addHierarchyFilter($parentId);
if($rootComments) {
$regedit = regedit::getInstance();
$per_page = (int) $regedit->getVal("//modules/comments/per_page");
$page = (int)getRequest('p');
$sel->addLimit($per_page, $page);
$object_type_id = umiObjectTypesCollection::getInstance()->getBaseType("comments", "comment");
$object_type = umiObjectTypesCollection::getInstance()->getType($object_type_id);
$publish_time_field_id = $object_type->getFieldId('publish_time');
$sel->setOrderByProperty($publish_time_field_id, false);
}
$result = umiSelectionsParser::runSelection($sel);
$aLines = array();
foreach($result as $commentId) {
$oComment = $hierarchy->getElement($commentId, true);
$pubTime = $oComment->getValue('publish_time');
$aLineParam = array();
$aLineParam['attribute:cid'] = $commentId;
$aLineParam['name'] = $oComment->getName();
$aLineParam['content'] = $this->prepareContent( $oComment->getValue('message') );
$aLineParam['author_id'] = $oComment->getValue('author_id');
$aLineParam['publish_time'] = ($pubTime instanceof umiDate) ? $pubTime->getFormattedDate('U') : time() ;
$aLineParam['subnodes:subcomments'] = $this->placeComments($commentId, $templateString, $hierarchy, $commentHType);
$aLines[] = def_module::parseTemplate($templateString, $aLineParam, $commentId);
}
return $aLines;
}
public function prepareContent($content) {
$from = array('[b]', '[/b]', '[i]', '[/i]', '[s]', '[/s]', '[u]', '[/u]', '[quote]', '[/quote]', "\n", '[cut]', '[/cut]');
$to = array('<b>', '</b>', '<i>', '</i>', '<span style="text-decoration:line-through;">', '</span>', '<span style="text-decoration:underline;">', '</span>', '<div class="quote">', '</div>', "<br />\n", '', '');
$content = str_replace($from, $to, $content);
$content = preg_replace("@\[img\](.+?)\[\/img\]@i", "<img src=\"$1\" alt=\"\" />", $content);
$content = preg_replace("@\[url\](.+?)\[\/url\]@i", "<a href=\"$1\">[Link]</a>", $content);
$content = preg_replace("@\[url=(.+?)\]((.|\n)+?)\[\/url\]@i", "<a href=\"$1\" target=\"_blank\">$2</a>", $content);
$content = preg_replace("@\[code\]((.|\n)+?)\[\/code\]@i", "<tt>$1</tt>", $content);
$content = preg_replace("@\[color=([A-Za-z0-9#]+?)\]((.|\n)+?)\[\/color\]@i", "<span style=\"color:$1;\">$2</span>", $content);
$content = preg_replace("@\[smile:([0-9]+?)\]@i", "<img src=\"/images/forum/smiles/$1.gif\" alt=\"$1\">", $content);
return $content;
}
Создадим в той же папке файл permissions.custom.php с кратким содержанием:
<?php
$permissions = Array('insert' => Array('commentsList'));
?>
Изменим tpl шаблон /tpls/comments/default.tpl:
$FORMS = Array();
$FORMS['comments_list_block'] = <<<END
<script language="javascript">
<!--
function setCommentParent(parentId) {
var form = document.getElementById('comment_add_form');
var editor = document.getElementById('message');
if(form)
form.action = '/comments/post/' + parentId + '/';
if(editor)
editor.focus();
}
-->
</script>
<h3>Комментарии</h3>
<br />
%system numpages(%total%, %per_page%, 'default')%
<div style="padding: 10px 10px 0px 10px;">
%lines%
<br />
</div>
%add_form%
END;
$FORMS['comments_list_line'] = <<<END
<a name="%id%"></a>
<p><b umi:element-id="%id%" umi:field-name="name">%h1%</b>%users viewAuthor(%author_id%)%<br />
<i>%system convertDate(%publish_time%, 'Y-m-d в H:i')%</i></p>
<div style="clear:both;">
<p>
%content%
</p>
<p style="padding-bottom:20px;">
(<a href="#comment_add" onclick="javascript:setCommentParent(%cid%);">Ответить</a>)
</p>
<div style="padding-left:30px;">
%subcomments%
</div>
</div>
END;
$FORMS['comments_block_add_user'] = <<<ADD_FORM_USER
<form id="comment_add_form" method="post" action="%action%">
<table cellspacing="5" cellpadding="0" border="0">
<tr>
<td>
Заголовок комментария:<br />
<input type="text" name="title" size="50" style="width: 350px;" class="textinputs" />
</td>
</tr>
<tr>
<td>
Текст комментария:<br />
<textarea id="message" name="comment" style="width: 350px; height: 120px;" class="textinputs"></textarea>
</td>
</tr>
<tr>
<td>
%smiles%
</td>
</tr>
<tr>
<td>
<input type="submit" value="Добавить комментарий" />
</td>
</tr>
</table>
</form>
ADD_FORM_USER;
$FORMS['comments_block_add_guest'] = <<<ADD_FORM_GUEST
<form method="post" action="%action%">
<table cellspacing="5" cellpadding="0" border="0" width="100%">
<tr>
<td nowrap="nowrap">
Заголовок комментария:<br />
<input type="text" name="title" style="width: 350px;" class="textinputs" />
</td>
</tr>
<tr>
<td nowrap="nowrap">
Ваш ник:<br />
<input type="text" name="author_nick" style="width: 350px;" class="textinputs" />
</td>
</tr>
<tr>
<td nowrap="nowrap">
Ваш e-mail:<br />
<input type="text" name="author_email" style="width: 350px;" class="textinputs" />
</td>
</tr>
<tr>
<td nowrap="nowrap">
Текст комментария:<br />
<textarea id="message" name="comment" style="width: 350px;height:120px" class="textinputs"></textarea>
</td>
</tr>
<tr>
<td>
%system captcha('default')%
</td>
</tr>
<tr>
<td>
%smiles%
</td>
</tr>
<tr>
<td>
<input type="submit" value="Добавить комментарий" />
</td>
</tr>
</table>
</form>
ADD_FORM_GUEST;
$FORMS['smiles'] = <<<END
<div class="smiles">
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/1.gif" alt="1"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/2.gif" alt="2"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/3.gif" alt="3"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/4.gif" alt="4"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/5.gif" alt="5"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/6.gif" alt="6"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/7.gif" alt="7"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/8.gif" alt="8"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/9.gif" alt="9"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/10.gif" alt="10"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/11.gif" alt="11"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/12.gif" alt="12"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/13.gif" alt="13"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/14.gif" alt="14"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/15.gif" alt="15"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/16.gif" alt="16"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/17.gif" alt="17"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/18.gif" alt="18"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/19.gif" alt="19"></a>
<a href="#" onclick="javascript: forum_insert_smile(this, '%element%'); return false;"><img src="/images/forum/smiles/20.gif" alt="20"></a>
</div>
END;
Макрос commentsList вызывается следующим образом:
%comments commentsList(%pid%)%