2010-11-30 11:06:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper Class for storing the configuration options. Retains the mapping between
|
|
|
|
* objects which have comments attached and the related configuration options.
|
|
|
|
*
|
|
|
|
* Also handles adding the Commenting extension to the {@link DataObject} on behalf
|
|
|
|
* of the user.
|
|
|
|
*
|
|
|
|
* For documentation on how to use this class see docs/en/Configuration.md
|
|
|
|
*
|
|
|
|
* @package comments
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Commenting {
|
2015-03-23 17:59:05 +01:00
|
|
|
|
2010-11-30 11:06:22 +01:00
|
|
|
/**
|
|
|
|
* @var array map of enabled {@link DataObject} and related configuration
|
|
|
|
*/
|
|
|
|
private static $enabled_classes = array();
|
2015-03-23 17:59:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var bool Whether to enable commenting on SiteTree objects by default
|
|
|
|
*/
|
|
|
|
private static $sitetree_comments = true;
|
|
|
|
|
2010-11-30 11:06:22 +01:00
|
|
|
/**
|
|
|
|
* @var array default configuration values
|
|
|
|
*/
|
2010-12-03 02:01:38 +01:00
|
|
|
private static $default_config = array(
|
2010-12-11 06:37:47 +01:00
|
|
|
'require_login' => false, // boolean, whether a user needs to login
|
|
|
|
'required_permission' => false, // required permission to comment (or array of permissions)
|
2013-03-05 15:37:08 +01:00
|
|
|
'include_js' => true, // Enhance operation by ajax behaviour on moderation links
|
2013-03-06 10:09:32 +01:00
|
|
|
'use_gravatar' => false, // set to true to show gravatar icons,
|
|
|
|
'gravatar_size' => 80, // size of gravatar in pixels. This is the same as the standard default
|
2013-03-26 17:15:56 +01:00
|
|
|
'gravatar_default' => 'identicon', // theme for 'not found' gravatar (see http://gravatar.com/site/implement/images/)
|
|
|
|
'gravatar_rating' => 'g', // gravatar rating. This is the same as the standard default
|
2010-12-11 06:37:47 +01:00
|
|
|
'show_comments_when_disabled' => false, // when comments are disabled should we show older comments (if available)
|
|
|
|
'order_comments_by' => "\"Created\" DESC",
|
|
|
|
'comments_per_page' => 10,
|
|
|
|
'comments_holder_id' => "comments-holder", // id for the comments holder
|
|
|
|
'comment_permalink_prefix' => "comment-", // id prefix for each comment. If needed make this different
|
2013-02-21 16:39:57 +01:00
|
|
|
'require_moderation' => false,
|
2013-06-04 22:12:16 +02:00
|
|
|
'require_moderation_nonmembers' => false, // requires moderation for comments posted by non-members. 'require_moderation' overrides this if set.
|
2013-02-21 16:39:57 +01:00
|
|
|
'html_allowed' => false, // allow for sanitized HTML in comments
|
2013-03-05 09:37:00 +01:00
|
|
|
'html_allowed_elements' => array('a', 'img', 'i', 'b'),
|
2013-03-05 15:37:08 +01:00
|
|
|
'use_preview' => false, // preview formatted comment (when allowing HTML). Requires include_js=true
|
2010-11-30 11:06:22 +01:00
|
|
|
);
|
|
|
|
|
2010-12-03 02:01:38 +01:00
|
|
|
/**
|
|
|
|
* Adds commenting to a {@link DataObject}
|
|
|
|
*
|
|
|
|
* @param string classname to add commenting to
|
|
|
|
* @param array $setting Settings. See {@link self::$default_config} for
|
|
|
|
* available settings
|
2010-12-03 02:16:35 +01:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
2010-12-03 02:01:38 +01:00
|
|
|
*/
|
2010-12-03 02:16:35 +01:00
|
|
|
public static function add($class, $settings = false) {
|
|
|
|
if($settings && !is_array($settings)) {
|
|
|
|
throw new InvalidArgumentException('$settings needs to be an array or null', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2010-12-03 02:01:38 +01:00
|
|
|
self::$enabled_classes[$class] = $settings;
|
2010-12-06 11:09:04 +01:00
|
|
|
|
2013-03-20 10:53:28 +01:00
|
|
|
$class::add_extension('CommentsExtension');
|
2010-11-30 11:06:22 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 02:01:38 +01:00
|
|
|
/**
|
|
|
|
* Removes commenting from a {@link DataObject}. Does not remove existing comments
|
|
|
|
* but does remove the extension.
|
|
|
|
*
|
|
|
|
* @param string $class Class to remove {@link CommentsExtension} from
|
|
|
|
*/
|
|
|
|
public static function remove($class) {
|
|
|
|
if(isset(self::$enabled_classes[$class])) {
|
|
|
|
unset(self::$enabled_classes[$class]);
|
|
|
|
}
|
2010-11-30 11:06:22 +01:00
|
|
|
|
2013-03-20 10:53:28 +01:00
|
|
|
$class::remove_extension('CommentsExtension');
|
2010-11-30 11:06:22 +01:00
|
|
|
}
|
2012-07-22 03:30:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a given class name has commenting enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function has_commenting($class) {
|
2013-06-04 22:12:16 +02:00
|
|
|
return (isset(self::$enabled_classes[$class]));
|
2012-07-22 03:30:33 +02:00
|
|
|
}
|
|
|
|
|
2010-12-03 02:01:38 +01:00
|
|
|
/**
|
2010-12-03 03:07:47 +01:00
|
|
|
* Sets a value for a class of a given config setting. Passing 'all' as the class
|
|
|
|
* sets it for everything
|
2010-12-03 02:01:38 +01:00
|
|
|
*
|
2010-12-03 03:07:47 +01:00
|
|
|
* @param string $class Class to set the value on. Passing 'all' will set it to all
|
|
|
|
* active mappings
|
2010-12-03 02:01:38 +01:00
|
|
|
* @param string $key setting to change
|
|
|
|
* @param mixed $value value of the setting
|
|
|
|
*/
|
|
|
|
public static function set_config_value($class, $key, $value = false) {
|
2010-12-03 03:07:47 +01:00
|
|
|
if($class == "all") {
|
|
|
|
if($enabledClasses = self::$enabled_classes) {
|
|
|
|
foreach($enabledClasses as $enabled) {
|
|
|
|
if(!is_array($enabled)) $enabled = array();
|
|
|
|
|
|
|
|
$enabled[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(isset(self::$enabled_classes[$class])) {
|
2010-12-03 02:01:38 +01:00
|
|
|
if(!is_array(self::$enabled_classes[$class])) self::$enabled_classes[$class] = array();
|
|
|
|
|
|
|
|
self::$enabled_classes[$class][$key] = $value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception("$class does not have commenting enabled", E_USER_ERROR);
|
|
|
|
}
|
2010-11-30 11:06:22 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 02:01:38 +01:00
|
|
|
/**
|
|
|
|
* Returns a given config value for a commenting class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param string $key config value to return
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-07-31 10:45:29 +02:00
|
|
|
public static function get_config_value($class = null, $key) {
|
|
|
|
if(!$class || isset(self::$enabled_classes[$class])) {
|
2010-12-03 02:01:38 +01:00
|
|
|
// custom configuration
|
|
|
|
if(isset(self::$enabled_classes[$class][$key])) return self::$enabled_classes[$class][$key];
|
|
|
|
|
|
|
|
// default configuration
|
|
|
|
if(isset(self::$default_config[$key])) return self::$default_config[$key];
|
|
|
|
|
|
|
|
// config value doesn't exist
|
|
|
|
throw new Exception("Config ($key) is not a valid configuration value", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception("$class does not have commenting enabled", E_USER_ERROR);
|
|
|
|
}
|
2010-11-30 11:06:22 +01:00
|
|
|
}
|
|
|
|
|
2010-12-03 02:01:38 +01:00
|
|
|
/**
|
|
|
|
* Determines whether a config value on the commenting extension
|
|
|
|
* matches a given value.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value Expected value
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function config_value_equals($class, $key, $value) {
|
|
|
|
try {
|
|
|
|
$check = self::get_config_value($class, $key);
|
|
|
|
|
|
|
|
if($check && ($check == $value)) return true;
|
|
|
|
}
|
|
|
|
catch(Exception $e) {}
|
2010-11-30 11:06:22 +01:00
|
|
|
|
2010-12-03 02:01:38 +01:00
|
|
|
return false;
|
2010-11-30 11:06:22 +01:00
|
|
|
}
|
2010-12-06 11:09:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether a user can post on a given commenting instance
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
*/
|
|
|
|
public static function can_member_post($class) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$login = self::get_config_value($class, 'require_login');
|
|
|
|
$permission = self::get_config_value($class, 'required_permission');
|
|
|
|
|
|
|
|
if($permission && !Permission::check($permission)) return false;
|
|
|
|
|
|
|
|
if($login && !$member) return false;
|
|
|
|
}
|
|
|
|
catch(Exception $e) {}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-26 17:15:56 +01:00
|
|
|
}
|