Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/ElggRiverItem.php (2 issues)

1
<?php
2
3
/**
4
 * River item class.
5
 *
6
 * @package    Elgg.Core
7
 * @subpackage Core
8
 *
9
 * @property-read int    $id            The unique identifier (read-only)
10
 * @property-read int    $subject_guid  The GUID of the actor
11
 * @property-read int    $object_guid   The GUID of the object
12
 * @property-read int    $target_guid   The GUID of the object's container
13
 * @property-read int    $annotation_id The ID of the annotation involved in the action
14
 * @property-read string $type          The type of one of the entities involved in the action
15
 * @property-read string $subtype       The subtype of one of the entities involved in the action
16
 * @property-read string $action_type   The name of the action
17
 * @property-read string $view          The view for displaying this river item
18
 * @property-read int    $access_id     The visibility of the river item
19
 * @property-read int    $posted        UNIX timestamp when the action occurred
20
 * @property-read string $enabled       Is the river item enabled yes|no
21
 */
22
class ElggRiverItem {
23
	public $id;
24
	public $subject_guid;
25
	public $object_guid;
26
	public $target_guid;
27
	public $annotation_id;
28
	public $type;
29
	public $subtype;
30
	public $action_type;
31
	public $access_id;
32
	public $view;
33
	public $posted;
34
	public $enabled;
35
36
	/**
37
	 * Construct a river item object given a database row.
38
	 *
39
	 * @param \stdClass $object Object obtained from database
40
	 */
41 22
	public function __construct($object) {
42 22
		if (!($object instanceof \stdClass)) {
43
			throw new \InvalidParameterException("Invalid input to \ElggRiverItem constructor");
44
		}
45
46
		// the casting is to support typed serialization like json
47 22
		$int_types = ['id', 'subject_guid', 'object_guid', 'target_guid', 'annotation_id', 'access_id', 'posted'];
48 22
		foreach ($object as $key => $value) {
49 22
			if (in_array($key, $int_types)) {
50 22
				$this->$key = (int) $value;
51
			} else {
52 22
				$this->$key = $value;
53
			}
54
		}
55 22
	}
56
57
	/**
58
	 * Get the subject of this river item
59
	 *
60
	 * @return \ElggEntity
61
	 */
62 6
	public function getSubjectEntity() {
63 6
		return get_entity($this->subject_guid);
64
	}
65
66
	/**
67
	 * Get the object of this river item
68
	 *
69
	 * @return \ElggEntity
70
	 */
71 8
	public function getObjectEntity() {
72 8
		return get_entity($this->object_guid);
73
	}
74
75
	/**
76
	 * Get the target of this river item
77
	 *
78
	 * @return \ElggEntity
79
	 */
80
	public function getTargetEntity() {
81
		return get_entity($this->target_guid);
82
	}
83
84
	/**
85
	 * Get the Annotation for this river item
86
	 *
87
	 * @return \ElggAnnotation
88
	 */
89
	public function getAnnotation() {
90
		return elgg_get_annotation_from_id($this->annotation_id);
91
	}
92
93
	/**
94
	 * Get the view used to display this river item
95
	 *
96
	 * @return string
97
	 */
98
	public function getView() {
99
		return $this->view;
100
	}
101
102
	/**
103
	 * Get the time this activity was posted
104
	 *
105
	 * @return int
106
	 */
107 4
	public function getTimePosted() {
108 4
		return (int) $this->posted;
109
	}
110
111
	/**
112
	 * Get the type of the object
113
	 *
114
	 * This is required for elgg_view_list_item(). All the other data types
115
	 * (entities, extenders, relationships) have a type/subtype.
116
	 *
117
	 * @return string 'river'
118
	 */
119
	public function getType() {
120
		return 'river';
121
	}
122
123
	/**
124
	 * Get the subtype of the object
125
	 *
126
	 * This is required for elgg_view_list_item().
127
	 *
128
	 * @return string 'item'
129
	 */
130
	public function getSubtype() {
131
		return 'item';
132
	}
133
134
	/**
135
	 * Can a user delete this river item?
136
	 *
137
	 * @tip Can be overridden by registering for the "permissions_check:delete", "river" plugin hook.
138
	 *
139
	 * @param int $user_guid The user GUID, optionally (default: logged in user)
140
	 *
141
	 * @return bool Whether this river item should be considered deletable by the given user.
142
	 * @since 2.3
143
	 */
144 11
	public function canDelete($user_guid = 0) {
145 11
		return _elgg_services()->userCapabilities->canDeleteRiverItem($this, $user_guid);
146
	}
147
148
	/**
149
	 * Delete the river item
150
	 *
151
	 * @return bool False if the user lacks permission or the before event is cancelled
152
	 * @since 2.3
153
	 */
154 11
	public function delete() {
155 11
		if (!$this->canDelete()) {
156 1
			return false;
157
		}
158
159 11
		$events = _elgg_services()->hooks->getEvents();
160 11
		if (!$events->triggerBefore('delete', 'river', $this)) {
0 ignored issues
show
$this of type ElggRiverItem is incompatible with the type string expected by parameter $object of Elgg\EventsService::triggerBefore(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
		if (!$events->triggerBefore('delete', 'river', /** @scrutinizer ignore-type */ $this)) {
Loading history...
161 1
			return false;
162
		}
163
164 11
		$db = _elgg_services()->db;
165 11
		$prefix = $db->prefix;
166 11
		_elgg_services()->db->deleteData("DELETE FROM {$prefix}river WHERE id = ?", [$this->id]);
167
168 11
		$events->triggerAfter('delete', 'river', $this);
0 ignored issues
show
$this of type ElggRiverItem is incompatible with the type string expected by parameter $object of Elgg\EventsService::triggerAfter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
		$events->triggerAfter('delete', 'river', /** @scrutinizer ignore-type */ $this);
Loading history...
169
170 11
		return true;
171
	}
172
173
	/**
174
	 * Get a plain old object copy for public consumption
175
	 *
176
	 * @return \stdClass
177
	 */
178
	public function toObject() {
179
		$object = new \stdClass();
180
		$object->id = $this->id;
181
		$object->subject_guid = $this->subject_guid;
182
		$object->object_guid = $this->object_guid;
183
		$object->annotation_id = $this->annotation_id;
184
		$object->read_access = $this->access_id;
185
		$object->action = $this->action_type;
186
		$object->time_posted = date('c', $this->getTimePosted());
187
		$object->enabled = $this->enabled;
188
		$params = ['item' => $this];
189
		return _elgg_services()->hooks->trigger('to:object', 'river_item', $params, $object);
190
	}
191
192
}
193