1: <?php
2: /**
3: * Elgg Annotations
4: *
5: * Annotations allow you to attach bits of information to entities.
6: * They are essentially the same as metadata, but with additional
7: * helper functions.
8: *
9: * @internal Annotations are stored in the annotations table.
10: *
11: * @package Elgg.Core
12: * @subpackage DataModel.Annotations
13: * @link http://docs.elgg.org/DataModel/Annotations
14: *
15: * @property string $value_type
16: * @property string $enabled
17: */
18: class ElggAnnotation extends ElggExtender {
19:
20: /**
21: * (non-PHPdoc)
22: *
23: * @see ElggData::initializeAttributes()
24: *
25: * @return void
26: */
27: protected function initializeAttributes() {
28: parent::initializeAttributes();
29:
30: $this->attributes['type'] = 'annotation';
31: }
32:
33: /**
34: * Construct a new annotation object
35: *
36: * @param mixed $id The annotation ID or a database row as stdClass object
37: */
38: function __construct($id = null) {
39: $this->initializeAttributes();
40:
41: if (!empty($id)) {
42: // Create from db row
43: if ($id instanceof stdClass) {
44: $annotation = $id;
45:
46: $objarray = (array) $annotation;
47: foreach ($objarray as $key => $value) {
48: $this->attributes[$key] = $value;
49: }
50: } else {
51: // get an ElggAnnotation object and copy its attributes
52: $annotation = elgg_get_annotation_from_id($id);
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: 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(elgg_echo('IOException:UnableToSaveNew', array(get_class())));
75: }
76: return $this->id;
77: }
78: }
79:
80: /**
81: * Delete the annotation.
82: *
83: * @return bool
84: */
85: function delete() {
86: elgg_delete_river(array('annotation_id' => $this->id));
87: return elgg_delete_metastring_based_object_by_id($this->id, 'annotations');
88: }
89:
90: /**
91: * Disable the annotation.
92: *
93: * @return bool
94: * @since 1.8
95: */
96: function disable() {
97: return elgg_set_metastring_based_object_enabled_by_id($this->id, 'no', 'annotations');
98: }
99:
100: /**
101: * Enable the annotation.
102: *
103: * @return bool
104: * @since 1.8
105: */
106: function enable() {
107: return elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'annotations');
108: }
109:
110: /**
111: * Get a url for this annotation.
112: *
113: * @return string
114: */
115: public function getURL() {
116: return get_annotation_url($this->id);
117: }
118:
119: // SYSTEM LOG INTERFACE
120:
121: /**
122: * For a given ID, return the object associated with it.
123: * This is used by the river functionality primarily.
124: * This is useful for checking access permissions etc on objects.
125: *
126: * @param int $id An annotation ID.
127: *
128: * @return ElggAnnotation
129: */
130: public function getObjectFromID($id) {
131: return elgg_get_annotation_from_id($id);
132: }
133: }
134: