1: <?php
2: /**
3: * The base class for ElggEntity extenders.
4: *
5: * Extenders allow you to attach extended information to an
6: * ElggEntity. Core supports two: ElggAnnotation and ElggMetadata.
7: *
8: * Saving the extender data to database is handled by the child class.
9: *
10: * @tip Plugin authors would probably want to extend either ElggAnnotation
11: * or ElggMetadata instead of this class.
12: *
13: * @package Elgg.Core
14: * @subpackage DataModel.Extender
15: * @link http://docs.elgg.org/DataModel/Extenders
16: * @see ElggAnnotation
17: * @see ElggMetadata
18: *
19: * @property string $type annotation or metadata (read-only after save)
20: * @property int $id The unique identifier (read-only)
21: * @property int $entity_guid The GUID of the entity that this extender describes
22: * @property int $access_id Specifies the visibility level of this extender
23: * @property string $name The name of this extender
24: * @property mixed $value The value of the extender (int or string)
25: * @property int $time_created A UNIX timestamp of when the extender was created (read-only, set on first save)
26: */
27: abstract class ElggExtender extends ElggData {
28:
29: /**
30: * (non-PHPdoc)
31: *
32: * @see ElggData::initializeAttributes()
33: *
34: * @return void
35: */
36: protected function initializeAttributes() {
37: parent::initializeAttributes();
38:
39: $this->attributes['type'] = NULL;
40: }
41:
42: /**
43: * Returns an attribute
44: *
45: * @param string $name Name
46: *
47: * @return mixed
48: */
49: protected function get($name) {
50: if (array_key_exists($name, $this->attributes)) {
51: // Sanitise value if necessary
52: if ($name == 'value') {
53: switch ($this->attributes['value_type']) {
54: case 'integer' :
55: return (int)$this->attributes['value'];
56: break;
57:
58: //case 'tag' :
59: //case 'file' :
60: case 'text' :
61: return ($this->attributes['value']);
62: break;
63:
64: default :
65: $msg = elgg_echo('InstallationException:TypeNotSupported', array(
66: $this->attributes['value_type']));
67:
68: throw new InstallationException($msg);
69: break;
70: }
71: }
72:
73: return $this->attributes[$name];
74: }
75: return null;
76: }
77:
78: /**
79: * Set an attribute
80: *
81: * @param string $name Name
82: * @param mixed $value Value
83: * @param string $value_type Value type
84: *
85: * @return boolean
86: */
87: protected function set($name, $value, $value_type = "") {
88: $this->attributes[$name] = $value;
89: if ($name == 'value') {
90: $this->attributes['value_type'] = detect_extender_valuetype($value, $value_type);
91: }
92:
93: return true;
94: }
95:
96: /**
97: * Get the GUID of the extender's owner entity.
98: *
99: * @return int The owner GUID
100: */
101: public function getOwnerGUID() {
102: return $this->owner_guid;
103: }
104:
105: /**
106: * Return the guid of the entity's owner.
107: *
108: * @return int The owner GUID
109: * @deprecated 1.8 Use getOwnerGUID
110: */
111: public function getOwner() {
112: elgg_deprecated_notice("ElggExtender::getOwner deprecated for ElggExtender::getOwnerGUID", 1.8);
113: return $this->getOwnerGUID();
114: }
115:
116: /**
117: * Get the entity that owns this extender
118: *
119: * @return ElggEntity
120: */
121: public function getOwnerEntity() {
122: return get_entity($this->owner_guid);
123: }
124:
125: /**
126: * Get the entity this describes.
127: *
128: * @return ElggEntity The entity
129: */
130: public function getEntity() {
131: return get_entity($this->entity_guid);
132: }
133:
134: /**
135: * Returns if a user can edit this extended data.
136: *
137: * @param int $user_guid The GUID of the user (defaults to currently logged in user)
138: *
139: * @return bool
140: */
141: public function canEdit($user_guid = 0) {
142: return can_edit_extender($this->id, $this->type, $user_guid);
143: }
144:
145: /*
146: * EXPORTABLE INTERFACE
147: */
148:
149: /**
150: * Return an array of fields which can be exported.
151: *
152: * @return array
153: */
154: public function getExportableValues() {
155: return array(
156: 'id',
157: 'entity_guid',
158: 'name',
159: 'value',
160: 'value_type',
161: 'owner_guid',
162: 'type',
163: );
164: }
165:
166: /**
167: * Export this object
168: *
169: * @return array
170: */
171: public function export() {
172: $uuid = get_uuid_from_object($this);
173:
174: $meta = new ODDMetaData($uuid, guid_to_uuid($this->entity_guid), $this->attributes['name'],
175: $this->attributes['value'], $this->attributes['type'], guid_to_uuid($this->owner_guid));
176: $meta->setAttribute('published', date("r", $this->time_created));
177:
178: return $meta;
179: }
180:
181: /*
182: * SYSTEM LOG INTERFACE
183: */
184:
185: /**
186: * Return an identification for the object for storage in the system log.
187: * This id must be an integer.
188: *
189: * @return int
190: */
191: public function getSystemLogID() {
192: return $this->id;
193: }
194:
195: /**
196: * Return a type of extension.
197: *
198: * @return string
199: */
200: public function getType() {
201: return $this->type;
202: }
203:
204: /**
205: * Return a subtype. For metadata & annotations this is the 'name' and
206: * for relationship this is the relationship type.
207: *
208: * @return string
209: */
210: public function getSubtype() {
211: return $this->name;
212: }
213:
214: }
215: