Skip to content

Commit

Permalink
Replaced the static method by a specific hook to allow to add comment…
Browse files Browse the repository at this point in the history
…s on any page and for any record.
  • Loading branch information
Daniel-KM committed Sep 25, 2014
1 parent 60d59d8 commit a4eceb9
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 86 deletions.
109 changes: 77 additions & 32 deletions CommentingPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CommentingPlugin extends Omeka_Plugin_AbstractPlugin
'admin_head',
'public_items_show',
'public_collections_show',
'commenting_comments',
'after_delete_record',
'define_acl',
);
Expand All @@ -46,6 +47,7 @@ class CommentingPlugin extends Omeka_Plugin_AbstractPlugin
*/
protected $_options = array(
// serialize(array()) = 'a:0:{}'.
'commenting_pages' => 'a:2:{i:0;s:16:"collections/show";i:1;s:10:"items/show";}',
'commenting_comment_roles' => 'a:0:{}',
'commenting_moderate_roles' => 'a:0:{}',
'commenting_reqapp_comment_roles' => 'a:0:{}',
Expand Down Expand Up @@ -158,6 +160,7 @@ public function hookUpgrade($args)
if (version_compare($old, '2.1', '<')) {
delete_option('commenting_noapp_comment_roles');
set_option('commenting_reqapp_comment_roles', serialize(array()));
set_option('commenting_pages', $this->_options('commenting_pages'));
$sql = "ALTER TABLE `$db->Comment` CHANGE `flagged` `flagged` TINYINT( 1 ) NOT NULL DEFAULT '0'";
$db->query($sql);
}
Expand All @@ -175,17 +178,38 @@ public function hookUninstall()
$this->_uninstallOptions();
}

public function hookPublicHead()
public function hookPublicHead($args)
{
queue_css_file('commenting');
queue_js_file('commenting');
queue_js_file('tiny_mce', 'javascripts/vendor/tiny_mce');
queue_js_string("Commenting.pluginRoot = '" . WEB_ROOT . "/commenting/comment/'");
if ($this->_isCommentingEnabled()) {
queue_css_file('commenting');
queue_js_file('commenting');
queue_js_file('tiny_mce', 'javascripts/vendor/tiny_mce');
queue_js_string("Commenting.pluginRoot = '" . WEB_ROOT . "/commenting/comment/'");
}
}

public function hookAdminHead($args)
{
if ($this->_isCommentingEnabled()) {
queue_css_file('commenting');
}
}

public function hookAdminHead()
/**
* Helper to determine if comments are enabled on current page or not.
*/
private function _isCommentingEnabled()
{
queue_css_file('commenting');
static $isEnabled = null;
if (is_null($isEnabled)) {
$request = Zend_Controller_Front::getInstance()->getRequest();
$controller = $request->getControllerName();
$action = $request->getActionName();
$pages = get_option('commenting_pages');
$pages = empty($pages) ? array() : unserialize($pages);
$isEnabled = in_array($controller . '/' . $action, $pages);
}
return $isEnabled;
}

public function hookAfterDeleteRecord($args)
Expand All @@ -201,43 +225,63 @@ public function hookAfterDeleteRecord($args)
/**
* Helper to append comments and comment form to a page.
*/
public static function showComments($args = array())
protected function _showComments($args = array())
{
$view = isset($args['view']) ? $args['view'] : get_view();
echo "<div id='comments-container'>";
if ((get_option('commenting_allow_public') == 1)
|| (get_option('commenting_allow_public_view') == 1)
|| is_allowed('Commenting_Comment', 'show')
) {
$options = array(
'threaded' => get_option('commenting_threaded'),
'approved' => true,
);

$comments = isset($args['comments']) ? $args['comments'] : $view->getComments($options);
echo $view->partial('common/comments.php', array(
'comments' => $comments,
'threaded' => $options['threaded'],
));
// This option allows to display comments and comment form separately.
$display = isset($args['display']) ? array($args['display']) : array('comments', 'comment_form');
$record = isset($args['record']) ? $args['record'] : null;

$html = '<div id="comments-container">';

if (in_array('comments', $display)) {
if ((get_option('commenting_allow_public') == 1)
|| (get_option('commenting_allow_public_view') == 1)
|| is_allowed('Commenting_Comment', 'show')
) {
$options = array(
'threaded' => get_option('commenting_threaded'),
'approved' => true,
);
$comments = isset($args['comments']) ? $args['comments'] : $view->getComments($options, $record);
$html .= $view->partial('common/comments.php', array(
'comments' => $comments,
'threaded' => $options['threaded'],
));
}
}

if ((get_option('commenting_allow_public') == 1)
|| is_allowed('Commenting_Comment', 'add')) {
echo "<div id='comment-main-container'>";
echo $view->getCommentForm();
echo "</div>";
if (in_array('comment_form', $display)) {
if ((get_option('commenting_allow_public') == 1)
|| is_allowed('Commenting_Comment', 'add')
) {
$html .= '<div id="comment-main-container">';
$html .= $view->getCommentForm($record);
$html .= '</div>';
}
}
echo "</div>";

$html .= '</div>';
echo $html;
}

public function hookPublicItemsShow($args)
{
self::showComments($args);
$this->_showComments($args);
}

public function hookPublicCollectionsShow($args)
{
self::showComments($args);
$this->_showComments($args);
}

/**
* This hook can be used in place of view helpers GetComments() and
* GetCommentForm().
*/
public function hookCommentingComments($args)
{
$this->_showComments($args);
}

public function hookConfigForm()
Expand All @@ -251,6 +295,7 @@ public function hookConfig($args)
{
$post = $args['post'];
foreach (array(
'commenting_pages',
'commenting_comment_roles',
'commenting_moderate_roles',
'commenting_view_roles',
Expand Down Expand Up @@ -368,7 +413,7 @@ private function _countComments($record)
{
$params = array(
'record_type' => get_class($record),
'record_id' => $record->id
'record_id' => $record->id,
);
return get_db()->getTable('Comment')->count($params);
}
Expand Down
84 changes: 76 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,88 @@ See USE CASES below for examples of configuration combinations
DISPLAYING COMMENTS
===================

Commenting will automatically add commenting options to Item and Collection show pages. To enable commenting on other
record types from modules (e.g. SimplePages or ExhibitBuilder), you will have to add the following lines
Commenting will automatically add commenting options to Item and Collection show
pages via default public hooks:

```php
fire_plugin_hook('public_items_show', array('view' => $this, 'item' => $item));
```php

or

```php
fire_plugin_hook('public_collections_show', array('view' => $this, 'collection' => $collection));
```

For flexibility and to enable commenting on other record types from modules
(e.g. SimplePages or ExhibitBuilder), you will have to add the following lines
to the appropriate place in the plugin's public view script:

```php
<?php CommentingPlugin::showComments(); ?>
fire_plugin_hook('commenting_comments');
```

or with options:

```php
fire_plugin_hook('commenting_comments', array(
'view' => $this,
'display' => array('comments', 'comment_form'),
'comments' => $comments,
));
```

For example, to show comments on exhibit sections and pages, the file `/plugins/ExhibitBuilder/views/public/exhibits/show.php`
could look like:

```php
<?php
echo head(array(
'title' => metadata('exhibit_page', 'title') . ' &middot; ' . metadata('exhibit', 'title'),
'bodyclass' => 'exhibits show'));
?>

<nav id="exhibit-pages">
<?php echo exhibit_builder_page_nav(); ?>
</nav>

<h1><span class="exhibit-page"><?php echo metadata('exhibit_page', 'title'); ?></h1>

<nav id="exhibit-child-pages">
<?php echo exhibit_builder_child_page_nav(); ?>
</nav>

<?php exhibit_builder_render_exhibit_page(); ?>

<?php fire_plugin_hook('commenting_comments'); ?>

<div id="exhibit-page-navigation">
<?php if ($prevLink = exhibit_builder_link_to_previous_page()): ?>
<div id="exhibit-nav-prev">
<?php echo $prevLink; ?>
</div>
<?php endif; ?>
<?php if ($nextLink = exhibit_builder_link_to_next_page()): ?>
<div id="exhibit-nav-next">
<?php echo $nextLink; ?>
</div>
<?php endif; ?>
<div id="exhibit-nav-up">
<?php echo exhibit_builder_page_trail(); ?>
</div>
</div>

<?php echo foot(); ?>
```

Keep in mind that updating themes or plugins will clobber your addition of the commenting functions.
Keep in mind that updating themes or plugins will clobber your addition of the
commenting functions.

The Commenting plugin knows how to work with SimplePages and ExhibitBuilder. Due to variability
in how plugins store their data in the database, other record types and views supplied by other plugins
might or might not work out of the box. Please ask on the forums or dev list if you would like Commenting
to work with other plugins.
The Commenting plugin knows how to work with SimplePages and ExhibitBuilder. Due
to variability in how plugins store their data in the database, other record
types and views supplied by other plugins might or might not work out of the
box. Please ask on the forums or dev list if you would like Commenting to work
with other plugins.


USE CASES
Expand Down
Loading

0 comments on commit a4eceb9

Please sign in to comment.