Completed
Push — master ( b1bf62...c2fee6 )
by Damian
01:51
created

code/Commenting.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Helper Class for storing the configuration options. Retains the mapping between
5
 * objects which have comments attached and the related configuration options.
6
 *
7
 * Also handles adding the Commenting extension to the {@link DataObject} on behalf
8
 * of the user.
9
 *
10
 * For documentation on how to use this class see docs/en/Configuration.md
11
 *
12
 * @deprecated since version 2.0
13
 *
14
 * @package comments
15
 */
16
class Commenting {
17
18
	/**
19
	 * Adds commenting to a {@link DataObject}
20
	 *
21
	 * @deprecated since version 2.0
22
	 *
23
	 * @param string classname to add commenting to
24
	 * @param array $settings Settings. See {@link self::$default_config} for
25
	 *			available settings
26
	 * 
27
	 * @throws InvalidArgumentException
28
	 */
29
	public static function add($class, $settings = false) {
30
		Deprecation::notice('2.0', 'Using Commenting::add is deprecated. Please use the config API instead');
31
		Config::inst()->update($class, 'extensions', array('CommentsExtension'));
32
33
		// Check if settings must be customised
34
		if($settings === false) return;
35
		if(!is_array($settings)) {
36
			throw new InvalidArgumentException('$settings needs to be an array or null');
37
		}
38
		Config::inst()->update($class, 'comments', $settings);
39
	}
40
41
	/**
42
	 * Removes commenting from a {@link DataObject}. Does not remove existing comments
43
	 * but does remove the extension.
44
	 *
45
	 * @deprecated since version 2.0
46
	 *
47
	 * @param string $class Class to remove {@link CommentsExtension} from
48
	 */
49
	public static function remove($class) {
50
		Deprecation::notice('2.0', 'Using Commenting::remove is deprecated. Please use the config API instead');
51
		$class::remove_extension('CommentsExtension');
52
	}
53
54
	/**
55
	 * Returns whether a given class name has commenting enabled
56
	 *
57
	 * @deprecated since version 2.0
58
	 *
59
	 * @return bool
60
	 */
61
	public static function has_commenting($class) {
62
		Deprecation::notice('2.0', 'Using Commenting::has_commenting is deprecated. Please use the config API instead');
63
		return $class::has_extension('CommentsExtension');
64
	}
65
66
	/**
67
	 * Sets a value for a class of a given config setting. Passing 'all' as the class
68
	 * sets it for everything
69
	 *
70
	 * @deprecated since version 2.0
71
	 *
72
	 * @param string $class Class to set the value on. Passing 'all' will set it to all 
73
	 *			active mappings
74
	 * @param string $key setting to change
75
	 * @param mixed $value value of the setting
76
	 */
77
	public static function set_config_value($class, $key, $value = false) {
78
		Deprecation::notice('2.0', 'Commenting::set_config_value is deprecated. Use the config api instead');
79
		if($class === "all") $class = 'CommentsExtension';
80
		Config::inst()->update($class, 'comments', array($key => $value));
81
	}
82
83
	/**
84
	 * Returns a given config value for a commenting class
85
	 *
86
	 * @deprecated since version 2.0
87
	 * 
88
	 * @param string $class
89
	 * @param string $key config value to return
90
	 *
91
	 * @throws Exception
92
	 * @return mixed
93
	 */
94
	public static function get_config_value($class, $key) {
95
		Deprecation::notice(
96
			'2.0',
97
			'Using Commenting::get_config_value is deprecated. Please use $parent->getCommentsOption() or '
98
			. 'CommentingController::getOption() instead'
99
		);
100
101
		// Get settings
102
		if(!$class) {
103
			$class = 'CommentsExtension';
104
		} elseif(!$class::has_extension('CommentsExtension')) {
105
			throw new InvalidArgumentException("$class does not have commenting enabled");
106
		}
107
		return singleton($class)->getCommentsOption($key);
108
	}
109
110
	/**
111
	 * Determines whether a config value on the commenting extension
112
	 * matches a given value.
113
	 *
114
	 * @deprecated since version 2.0
115
	 *
116
	 * @param string $class
117
	 * @param string $key
118
	 * @param string $value Expected value
119
	 * @return boolean
120
	 */
121
	public static function config_value_equals($class, $key, $value) {
122
		$check = self::get_config_value($class, $key);
0 ignored issues
show
Deprecated Code introduced by
The method Commenting::get_config_value() has been deprecated with message: since version 2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
123
		if($check && ($check == $value)) return true;
124
	}
125
126
	/**
127
	 * Return whether a user can post on a given commenting instance
128
	 *
129
	 * @deprecated since version 2.0
130
	 * 
131
	 * @param string $class
132
	 * @return boolean true
133
	 */
134
	public static function can_member_post($class) {
135
		Deprecation::notice('2.0',  'Use $instance->canPostComment() directly instead');
136
		$member = Member::currentUser();
137
138
		// Check permission
139
		$permission = self::get_config_value($class, 'required_permission');
0 ignored issues
show
Deprecated Code introduced by
The method Commenting::get_config_value() has been deprecated with message: since version 2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
140
		if($permission && !Permission::check($permission)) return false;
141
142
		// Check login required
143
		$requireLogin = self::get_config_value($class, 'require_login');
0 ignored issues
show
Deprecated Code introduced by
The method Commenting::get_config_value() has been deprecated with message: since version 2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
144
		return !$requireLogin || $member;
145
	}
146
}
147