Comments in Popup prototype

This commit is contained in:
Lucas Bartholemy 2014-06-18 08:58:20 +02:00
parent 0a925fa3c4
commit 6ba5326fa3
12 changed files with 355 additions and 185 deletions

View File

@ -6,14 +6,16 @@
* @package humhub.modules_core.comment
* @since 0.5
*/
class CommentModule extends CWebModule {
class CommentModule extends CWebModule
{
/**
* On content deletion make sure to delete all its comments
*
* @param CEvent $event
*/
public static function onContentDelete($event) {
public static function onContentDelete($event)
{
foreach (Comment::model()->findAllByAttributes(array('object_model' => get_class($event->sender), 'object_id' => $event->sender->id)) as $comment) {
$comment->delete();
@ -25,7 +27,8 @@ class CommentModule extends CWebModule {
*
* @param CEvent $event
*/
public static function onUserDelete($event) {
public static function onUserDelete($event)
{
foreach (Comment::model()->findAllByAttributes(array('created_by' => $event->sender->id)) as $comment) {
$comment->delete();
@ -38,7 +41,8 @@ class CommentModule extends CWebModule {
*
* @param CEvent $event
*/
public static function onIntegrityCheck($event) {
public static function onIntegrityCheck($event)
{
$integrityChecker = $event->sender;
$integrityChecker->showTestHeadline("Validating Comment Module (" . Comment::model()->count() . " entries)");
@ -59,13 +63,9 @@ class CommentModule extends CWebModule {
*
* @param CEvent $event
*/
public static function onWallEntryLinksInit($event) {
$event->sender->addWidget('application.modules_core.comment.widgets.CommentLinkWidget', array(
'modelName' => $event->sender->object->content->object_model,
'modelId' => $event->sender->object->content->object_id,
), array('sortOrder' => 10)
);
public static function onWallEntryLinksInit($event)
{
$event->sender->addWidget('application.modules_core.comment.widgets.CommentLinkWidget', array('object' => $event->sender->object), array('sortOrder' => 10));
}
/**
@ -73,13 +73,9 @@ class CommentModule extends CWebModule {
*
* @param CEvent $event
*/
public static function onWallEntryAddonInit($event) {
$event->sender->addWidget('application.modules_core.comment.widgets.CommentsWidget', array(
'modelName' => $event->sender->object->content->object_model,
'modelId' => $event->sender->object->content->object_id,
), array('sortOrder' => 20)
);
public static function onWallEntryAddonInit($event)
{
$event->sender->addWidget('application.modules_core.comment.widgets.CommentsWidget', array('object' => $event->sender->object), array('sortOrder' => 20));
}
}

View File

@ -6,7 +6,8 @@
* @package humhub.modules_core.comment.controllers
* @since 0.5
*/
class CommentController extends Controller {
class CommentController extends Controller
{
// Used by loadTargetModel() to avoid multiple loading
private $cachedLoadedTarget = null;
@ -14,7 +15,8 @@ class CommentController extends Controller {
/**
* @return array action filters
*/
public function filters() {
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
@ -25,7 +27,8 @@ class CommentController extends Controller {
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules() {
public function accessRules()
{
return array(
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'users' => array('@'),
@ -42,7 +45,8 @@ class CommentController extends Controller {
*
* @return type
*/
private function loadTargetModel() {
private function loadTargetModel()
{
// Fast lane
if ($this->cachedLoadedTarget != null)
@ -83,7 +87,8 @@ class CommentController extends Controller {
/**
* Returns a List of all Comments belong to this Model
*/
public function actionShow() {
public function actionShow()
{
$target = $this->loadTargetModel();
@ -101,10 +106,31 @@ class CommentController extends Controller {
Yii::app()->end();
}
public function actionShowPopup()
{
$target = $this->loadTargetModel();
$output = "";
// Get new current comments
$comments = Comment::model()->findAllByAttributes(array('object_model' => get_class($target), 'object_id' => $target->id));
foreach ($comments as $comment) {
$output .= $this->widget('application.modules_core.comment.widgets.ShowCommentWidget', array('comment' => $comment), true);
}
$id = get_class($target) . "_" . $target->id;
$this->renderPartial('show', array('object' => $target, 'output' => $output, 'id' => $id), false, true);
}
/**
* Handles AJAX Post Request to submit new Comment
*/
public function actionPost() {
public function actionPost()
{
$this->forcePostRequest();
$target = $this->loadTargetModel();
@ -151,7 +177,8 @@ class CommentController extends Controller {
* Handles AJAX Request for Comment Deletion.
* Currently this is only allowed for the Comment Owner.
*/
public function actionDelete() {
public function actionDelete()
{
$this->forcePostRequest();
$target = $this->loadTargetModel();

View File

@ -0,0 +1,47 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title"
id="myModalLabel">
<?php echo Yii::t('CommentModule.base', 'Comments'); ?>
</h4>
</div>
<div id="userlist-content">
<div class="well well-small" id="comment_<?php echo $id; ?>">
<div class="comment" id="comments_area_<?php echo $id; ?>">
<?php echo $output; ?>
</div>
<?php $this->widget('application.modules_core.comment.widgets.CommentFormWidget', array('object' => $object)); ?>
</div>
</div>
</div>
<script type="text/javascript">
/*
* Modal handling by close event
*/
$('#globalModal').on('hidden.bs.modal', function(e) {
// Reload whole page (to see changes on it)
//window.location.reload();
// just close modal and reset modal content to default (shows the loader)
$('#globalModal').html('<div class="modal-dialog"><div class="modal-content"><div class="modal-body"><div class="loader"></div></div></div></div>');
})
</script>
<script type="text/javascript">
// scroll to top of list
$(".modal-body").animate({scrollTop: 0}, 200);
</script>

View File

@ -0,0 +1,57 @@
<?php
/**
* HumHub
* Copyright © 2014 The HumHub Project
*
* The texts of the GNU Affero General Public License with an additional
* permission and of our proprietary license can be found at and
* in the LICENSE file you have received along with this program.
*
* According to our dual licensing model, this program can be used either
* under the terms of the GNU Affero General Public License, version 3,
* or under a proprietary license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*/
/**
* This widget is used include the comments functionality to a wall entry.
*
* Normally it shows a excerpt of all comments, but provides the functionality
* to show all comments.
*
* @package humhub.modules_core.comment
* @since 0.5
*/
class CommentFormWidget extends HWidget
{
/**
* Content Object
*/
public $object;
/**
* Executes the widget.
*/
public function run()
{
$modelName = $this->object->content->object_model;
$modelId = $this->object->content->object_id;
$id = $modelName . "_" . $modelId;
$this->render('form', array(
'modelName' => $modelName,
'modelId' => $modelId,
'id' => $modelName . "_" . $modelId,
));
}
}
?>

View File

@ -6,27 +6,52 @@
* @package humhub.modules_core.comment
* @since 0.5
*/
class CommentLinkWidget extends HWidget {
class CommentLinkWidget extends HWidget
{
const MODE_INLINE = 'inline';
const MODE_POPUP = 'popup';
/**
* Model Name (e.g. Post) to identify which posts we shall show
*
* @var String
* Content Object
*/
public $modelName = "";
public $object;
/**
* The primary key of the model
* Mode
*
* @var String
* inline: Show comments on the same page with CommentsWidget (default)
* popup: Open comments popup, display only link
*
* @var type
*/
public $modelId = "";
public $mode;
/**
* Executes the widget.
*/
public function run() {
$this->render('commentsLink', array('id' => $this->modelName . "_" . $this->modelId));
public function run()
{
if ($this->mode == "")
$this->mode = self::MODE_INLINE;
$this->render('link', array(
'id' => $this->object->content->object_model . "_" . $this->object->content->object_id,
'mode' => $this->mode,
'objectModel' => $this->object->content->object_model,
'objectId' => $this->object->content->object_id,
));
}
/**
* Returns count of existing comments
*
* @return Int Comment Count
*/
public function getCommentsCount()
{
return Comment::GetCommentCount(get_class($this->object), $this->object->getPrimaryKey());
}
}

View File

@ -9,42 +9,40 @@
* @package humhub.modules_core.comment
* @since 0.5
*/
class CommentsWidget extends HWidget {
class CommentsWidget extends HWidget
{
/**
* Model Name (e.g. Post) to identify which posts we shall show
*
* @var String
* Content Object
*/
public $modelName = "";
/**
* The primary key of the Model
*
* @var Integer
*/
public $modelId = "";
public $object;
/**
* Executes the widget.
*/
public function run() {
public function run()
{
$modelName = $this->object->content->object_model;
$modelId = $this->object->content->object_id;
// Indicates that the number of comments was limited
$isLimited = false;
// Count all Comments
$commentCount = Comment::GetCommentCount($this->modelName, $this->modelId);
$comments = Comment::GetCommentsLimited($this->modelName, $this->modelId, 2);
$commentCount = Comment::GetCommentCount($modelName, $modelId);
$comments = Comment::GetCommentsLimited($modelName, $modelId, 2);
if ($commentCount > 2)
$isLimited = true;
$this->render('comments', array(
'object' => $this->object,
'comments' => $comments,
'modelName' => $this->modelName,
'modelId' => $this->modelId,
'id' => $this->modelName . "_" . $this->modelId,
'modelName' => $modelName,
'modelId' => $modelId,
'id' => $modelName . "_" . $modelId,
'isLimited' => $isLimited,
'total' => $commentCount
)

View File

@ -25,7 +25,7 @@
$reloadUrl = CHtml::normalizeUrl(Yii::app()->createUrl('comment/comment/show', array('model' => $modelName, 'id' => $modelId)));
echo HHtml::ajaxLink($showAllLabel, $reloadUrl, array(
'success' => "function(html) { $('#comments_area_" . $id . "').html(html); }",
), array('id' => $id . "_showAllLink", 'class' => 'show show-all-link'));
), array('id' => $id . "_showAllLink", 'class' => 'show show-all-link'));
?>
<hr>
<?php endif; ?>
@ -35,87 +35,16 @@
<?php endforeach; ?>
</div>
<?php /* BEGIN: Comment Create Form */ ?>
<div id="comment_create_form_<?php echo $id; ?>">
<?php echo CHtml::form("#"); ?>
<?php echo CHtml::hiddenField('model', $modelName); ?>
<?php echo CHtml::hiddenField('id', $modelId); ?>
<?php $this->widget('application.modules_core.comment.widgets.CommentFormWidget', array('object' => $object)); ?>
<?php echo CHtml::textArea("message", Yii::t('CommentModule.base', ""), array('id' => 'newCommentForm_' . $id, 'rows' => '1', 'class' => 'form-control autosize commentForm', 'placeholder' => 'Write a new comment...')); ?>
<?php
/* Modify textarea for mention input */
$this->widget('application.widgets.MentionWidget', array(
'element' => '#newCommentForm_' . $id,
));
?>
<?php
echo HHtml::ajaxSubmitButton(Yii::t('base', 'Post'), CHtml::normalizeUrl(array('/comment/comment/post')), array(
'beforeSend' => "function() {
$('#newCommentForm_" . $id . "').blur();
}",
'success' => "function(html) {
$('#comments_area_" . $id . "').html(html);
$('#newCommentForm_" . $id . "').val('').trigger('autosize.resize');
$.fn.mention.reset('#newCommentForm_" . $id . "');
}",
), array(
'id' => "comment_create_post_" . $id,
'class' => 'btn btn-small btn-primary',
'style' => 'position: absolute; top: -3000px; left: -3000px;',
)
);
?>
<?php echo Chtml::endForm(); ?>
</div>
</div>
<?php /* END: Comment Create Form */ ?>
<script type="text/javascript">
<?php if (count($comments) != 0) { ?>
// make comments visible at this point to fixing autoresizing issue for textareas in Firefox
$('#comment_<?php echo $id; ?>').show();
<?php } ?>
// Fire click event for comment button by typing enter
$('#newCommentForm_<?php echo $id; ?>').keydown(function (event) {
if (event.keyCode == 13) {
if ($.fn.mention.defaults.stateUserList == false) {
event.cancelBubble = true;
event.returnValue = false;
$('#comment_create_post_<?php echo $id; ?>').focus();
$('#comment_create_post_<?php echo $id; ?>').click();
// empty input
//$(this).val('');
}
}
return event.returnValue;
});
// set the size for one row (Firefox)
$('#newCommentForm_<?php echo $id; ?>').css({height: '36px'});
// add autosize function to input
$('.autosize').autosize();
<?php if (count($comments) != 0) { ?>
// make comments visible at this point to fixing autoresizing issue for textareas in Firefox
$('#comment_<?php echo $id; ?>').show();
<?php } ?>
</script>

View File

@ -1,16 +0,0 @@
<?php
/**
* This view is used by the CommentLinkWidget to inject a link to the
* Wall Entry Controls.
*
* The primary goal is to show the new comment input when clicking it.
* The Input Form is defined in comments.php
*
* @property String $id is a unique Id on Model and PK e.g. (Post_1)
*
* @package humhub.modules_core.comment
* @since 0.5
*/
echo CHtml::link(Yii::t('CommentModule.base', "Comment") . "", "#", array('onClick' => "$('#comment_" . $id . "').show();$('#newCommentForm_" . $id . "').focus();return false;"));
?>

View File

@ -0,0 +1,81 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
?>
<?php /* BEGIN: Comment Create Form */ ?>
<div id="comment_create_form_<?php echo $id; ?>">
<?php echo CHtml::form("#"); ?>
<?php echo CHtml::hiddenField('model', $modelName); ?>
<?php echo CHtml::hiddenField('id', $modelId); ?>
<?php echo CHtml::textArea("message", Yii::t('CommentModule.base', ""), array('id' => 'newCommentForm_' . $id, 'rows' => '1', 'class' => 'form-control autosize commentForm', 'placeholder' => 'Write a new comment...')); ?>
<?php
/* Modify textarea for mention input */
$this->widget('application.widgets.MentionWidget', array(
'element' => '#newCommentForm_' . $id,
));
?>
<?php
echo HHtml::ajaxSubmitButton(Yii::t('base', 'Post'), CHtml::normalizeUrl(array('/comment/comment/post')), array(
'beforeSend' => "function() {
$('#newCommentForm_" . $id . "').blur();
}",
'success' => "function(html) {
$('#comments_area_" . $id . "').html(html);
$('#newCommentForm_" . $id . "').val('').trigger('autosize.resize');
$.fn.mention.reset('#newCommentForm_" . $id . "');
}",
), array(
'id' => "comment_create_post_" . $id,
'class' => 'btn btn-small btn-primary',
'style' => 'position: absolute; top: -3000px; left: -3000px;',
)
);
?>
<?php echo Chtml::endForm(); ?>
</div>
<script>
// Fire click event for comment button by typing enter
$('#newCommentForm_<?php echo $id; ?>').keydown(function(event) {
if (event.keyCode == 13) {
if ($.fn.mention.defaults.stateUserList == false) {
event.cancelBubble = true;
event.returnValue = false;
$('#comment_create_post_<?php echo $id; ?>').focus();
$('#comment_create_post_<?php echo $id; ?>').click();
// empty input
//$(this).val('');
}
}
return event.returnValue;
});
// set the size for one row (Firefox)
$('#newCommentForm_<?php echo $id; ?>').css({height: '36px'});
// add autosize function to input
$('.autosize').autosize();
</script>

View File

@ -0,0 +1,23 @@
<?php
/**
* This view is used by the CommentLinkWidget to inject a link to the
* Wall Entry Controls.
*
* The primary goal is to show the new comment input when clicking it.
* The Input Form is defined in comments.php
*
* @property String $id is a unique Id on Model and PK e.g. (Post_1)
*
* @package humhub.modules_core.comment
* @since 0.5
*/
?>
<?php if ($mode == CommentLinkWidget::MODE_POPUP): ?>
<a href="<?php echo $this->createUrl('//comment/comment/showPopup', array('model' => $objectModel, 'id' => $objectId)); ?>"
class="tt" data-toggle="modal"
data-placement="top" title="" data-target="#globalModal"
data-original-title="Comments">Comments (<?php echo $this->getCommentsCount(); ?>)</a>
<?php else: ?>
<?php echo CHtml::link(Yii::t('CommentModule.base', "Comment") . "", "#", array('onClick' => "$('#comment_" . $id . "').show();$('#newCommentForm_" . $id . "').focus();return false;")); ?>
<?php endif; ?>

View File

@ -9,47 +9,40 @@
* @since 0.5
*/
?>
<div class="media">
<a href="<?php echo $user->getUrl(); ?>" class="pull-left">
<img class="media-object img-rounded" src="<?php echo $user->getProfileImage()->getUrl(); ?>" width="40"
height="40" alt="40x40" data-src="holder.js/40x40" style="width: 40px; height: 40px;"/>
</a>
<div class="media">
<a href="<?php echo $user->getUrl(); ?>" class="pull-left">
<img class="media-object img-rounded" src="<?php echo $user->getProfileImage()->getUrl(); ?>" width="40"
height="40" alt="40x40" data-src="holder.js/40x40" style="width: 40px; height: 40px;"/>
</a>
<div class="media-body">
<h4 class="media-heading"><a href="<?php echo $user->getProfileUrl(); ?>"><?php echo $user->displayName; ?></a> <small><?php echo HHtml::timeago($comment->created_at); ?></small></h4>
<span class="content">
<?php
print HHtml::enrichText($comment->message);
//print nl2br($comment->message);
?>
</span>
<div class="media-body">
<h4 class="media-heading"><a href="<?php echo $user->getProfileUrl(); ?>"><?php echo $user->displayName; ?></a> <small><?php echo HHtml::timeago($comment->created_at); ?></small></h4>
<span class="content">
<?php
print HHtml::enrichText($comment->message);
?>
</span>
<?php //echo CHtml::link(Yii::t('base', "Delete"), '#'); ?>
<?php //echo CHtml::link(Yii::t('base', "Delete"), '#'); ?>
<div class="wall-entry-controls">
<?php
if ($comment->canDelete()) {
$deleteUrl = CHtml::normalizeUrl(array('//comment/comment/delete', 'model' => $comment->object_model, 'id' => $comment->object_id, 'cid' => $comment->id));
echo HHtml::ajaxLink(Yii::t('base', 'Delete'), $deleteUrl, array(
'type' => 'POST',
'data' => array(Yii::app()->request->csrfTokenName => Yii::app()->request->csrfToken),
'success' => "function(html) {
$('#comments_area_" . $comment->object_model . "_" . $comment->object_id . "').html(html);
}",
<div class="wall-entry-controls">
<?php
if ($comment->canDelete()) {
$deleteUrl = CHtml::normalizeUrl(array('//comment/comment/delete', 'model' => $comment->object_model, 'id' => $comment->object_id, 'cid' => $comment->id));
echo HHtml::ajaxLink(Yii::t('base', 'Delete'), $deleteUrl, array(
'type' => 'POST',
'data' => array(Yii::app()->request->csrfTokenName => Yii::app()->request->csrfToken),
'success' => "function(html) { $('#comments_area_" . $comment->object_model . "_" . $comment->object_id . "').html(html); }",
), array(
'id' => "comment_delete_link" . $comment->id
'id' => "comment_delete_link" . $comment->id
)
);
echo " - ";
}
?>
);
echo " - ";
}
?>
<?php Yii::app()->getController()->widget('application.modules_core.like.widgets.LikeLinkWidget', array('object' => $comment)); ?>
</div>
<?php Yii::app()->getController()->widget('application.modules_core.like.widgets.LikeLinkWidget', array('object' => $comment)); ?>
</div>
</div>
<hr>
<!-- Maybe use another, smaller version here? -->
<?php
//Yii::app()->getController()->widget('application.modules_core.like.widgets.ShowLikesWidget', array('object' => $comment));
?>
</div>
<hr>

View File

@ -69,6 +69,16 @@ class SpaceControllerBehavior extends CBehavior {
return $space;
}
public function createContainerUrl($route, $params = array(), $ampersand = '&') {
if (!isset($params['sguid'])) {
$params['sguid'] = $this->getSpace()->guid;
}
return $this->owner->createUrl($route, $params, $ampersand);
}
}
?>