1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Elgg Annotations |
4
|
|
|
* |
5
|
|
|
* Annotations allow you to attach bits of information to entities. They are |
6
|
|
|
* essentially the same as metadata, but with additional helper functions for |
7
|
|
|
* performing calculations. |
8
|
|
|
* |
9
|
|
|
* @note Internal: Annotations are stored in the annotations table. |
10
|
|
|
* |
11
|
|
|
* @package Elgg.Core |
12
|
|
|
* @subpackage DataModel.Annotations |
13
|
|
|
*/ |
14
|
|
|
class ElggAnnotation extends \ElggExtender { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* (non-PHPdoc) |
18
|
|
|
* |
19
|
|
|
* @see \ElggData::initializeAttributes() |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
protected function initializeAttributes() { |
24
|
|
|
parent::initializeAttributes(); |
25
|
|
|
|
26
|
|
|
$this->attributes['type'] = 'annotation'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Construct a new annotation object |
31
|
|
|
* |
32
|
|
|
* Plugin developers will probably never use the constructor. |
33
|
|
|
* See \ElggEntity for its API for adding annotations. |
34
|
|
|
* |
35
|
|
|
* @param \stdClass $row Database row as \stdClass object |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function __construct($row = null) { |
|
|
|
|
38
|
|
|
$this->initializeAttributes(); |
39
|
|
|
|
40
|
|
|
if (!empty($row)) { |
41
|
|
|
// Create from db row |
42
|
|
|
if ($row instanceof \stdClass) { |
43
|
|
|
$annotation = $row; |
44
|
|
|
|
45
|
|
|
$objarray = (array) $annotation; |
46
|
|
|
foreach ($objarray as $key => $value) { |
47
|
|
|
$this->attributes[$key] = $value; |
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
// get an \ElggAnnotation object and copy its attributes |
51
|
|
|
elgg_deprecated_notice('Passing an ID to constructor is deprecated. Use elgg_get_annotation_from_id()', 1.9); |
52
|
|
|
$annotation = elgg_get_annotation_from_id($row); |
53
|
|
|
$this->attributes = $annotation->attributes; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Save this instance |
60
|
|
|
* |
61
|
|
|
* @return int an object id |
62
|
|
|
* |
63
|
|
|
* @throws IOException |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function save() { |
|
|
|
|
66
|
|
|
if ($this->id > 0) { |
67
|
|
|
return update_annotation($this->id, $this->name, $this->value, $this->value_type, |
|
|
|
|
68
|
|
|
$this->owner_guid, $this->access_id); |
69
|
|
|
} else { |
70
|
|
|
$this->id = create_annotation($this->entity_guid, $this->name, $this->value, |
71
|
|
|
$this->value_type, $this->owner_guid, $this->access_id); |
72
|
|
|
|
73
|
|
|
if (!$this->id) { |
|
|
|
|
74
|
|
|
throw new \IOException("Unable to save new " . get_class()); |
75
|
|
|
} |
76
|
|
|
return $this->id; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Delete the annotation. |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function delete() { |
86
|
|
|
$result = _elgg_delete_metastring_based_object_by_id($this->id, 'annotation'); |
87
|
|
|
if ($result) { |
88
|
|
|
elgg_delete_river(array('annotation_id' => $this->id)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Disable the annotation. |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
* @since 1.8 |
99
|
|
|
*/ |
100
|
|
|
public function disable() { |
101
|
|
|
return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'annotations'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Enable the annotation. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
* @since 1.8 |
109
|
|
|
*/ |
110
|
|
|
public function enable() { |
111
|
|
|
return _elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'annotations'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Determines whether or not the user can edit this annotation |
116
|
|
|
* |
117
|
|
|
* @param int $user_guid The GUID of the user (defaults to currently logged in user) |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
* @see elgg_set_ignore_access() |
121
|
|
|
*/ |
122
|
|
|
public function canEdit($user_guid = 0) { |
123
|
|
|
|
124
|
|
View Code Duplication |
if ($user_guid) { |
125
|
|
|
$user = get_user($user_guid); |
126
|
|
|
if (!$user) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
|
|
$user = _elgg_services()->session->getLoggedInUser(); |
131
|
|
|
$user_guid = _elgg_services()->session->getLoggedInUserGuid(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$result = false; |
135
|
|
|
if ($user) { |
136
|
|
|
// If the owner of annotation is the specified user, they can edit. |
137
|
|
|
if ($this->getOwnerGUID() == $user_guid) { |
138
|
|
|
$result = true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// If the user can edit the entity this is attached to, they can edit. |
142
|
|
|
$entity = $this->getEntity(); |
143
|
|
|
if ($result == false && $entity->canEdit($user->getGUID())) { |
|
|
|
|
144
|
|
|
$result = true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Trigger plugin hook - note that $user may be null |
149
|
|
|
$params = array('entity' => $entity, 'user' => $user, 'annotation' => $this); |
|
|
|
|
150
|
|
|
return _elgg_services()->hooks->trigger('permissions_check', 'annotation', $params, $result); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// SYSTEM LOG INTERFACE |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* For a given ID, return the object associated with it. |
157
|
|
|
* This is used by the river functionality primarily. |
158
|
|
|
* This is useful for checking access permissions etc on objects. |
159
|
|
|
* |
160
|
|
|
* @param int $id An annotation ID. |
161
|
|
|
* |
162
|
|
|
* @return \ElggAnnotation |
163
|
|
|
*/ |
164
|
|
|
public function getObjectFromID($id) { |
165
|
|
|
return elgg_get_annotation_from_id($id); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.