aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/objects.php
blob: 1541c8dd5a2fd6df4ab7f8856e6f3f8c750a8355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php

	/**
	 * Elgg objects
	 * Forms the basis of object storage and retrieval
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Curverider Ltd
	 * @copyright Curverider Ltd 2008
	 * @link http://elgg.org/
	 */

	/**
	 * Get object reverse ordered by publish time, optionally filtered by user and/or type
	 *
	 * @param int $user_id The ID of the publishing user; set to 0 for all users
	 * @param string $type The type of the object; set to blank for all types
	 * @param string $metadata_type The type of metadata that we're searching on (blank for none)
	 * @param string $metadata_value The value of metadata that we're searching on (blank for none)
	 * @param int $limit The number of objects (default 10)
	 * @param int $offset The offset of the return, for pagination
	 * @param int $site_id The site the objects belong to (leave blank for default site)
	 * @return unknown
	 */
		function get_objects($user_id = 0, $type = "", $metadata_type = "", $metadata_value = "", $limit = 10, $offset = 0, $site_id = 0) {
			
			global $CONFIG;
			
			$user_id = (int) $user_id;
			$type = sanitise_string($type);
			$limit = (int) $limit;
			$offset = (int) $offset;
			$site_id = (int) $site_id;
			if ($site_id == 0) $site_id = $CONFIG->site_id;
			$access = get_access_list();
			
			$query = "select o.*, ot.name as typename from {$CONFIG->dbprefix}objects o ";
			if (!empty($type)) $query .= " left join {$CONFIG->dbprefix}object_types ot on ot.id = o.type_id ";
			if (!empty($metadata_type) && !empty($metadata_value)) {
				$metadata_type = sanitise_string($metadata_type);
				$metadata_value = sanitise_string($metadata_value);
				$query .= " left join {$CONFIG->dbprefix}object_metadata om on om.object_id = o.id ";
				$query .= " left join {$CONFIG->dbprefix}metadata_value mv on mv.id = om.value_id ";
				$query .= " left join {$CONFIG->dbprefix}metadata_type mt on mt.id = om.metadata_type_id "; 
			}
			$query .= " where o.site_id = {$site_id} ";
			$query .= " and (o.access_id in {$access} or (o.access_id = 0 and o.owner_id = {$_SESSION['id']}))";
			if (!empty($type)) $query .= " and ot.name = '{$type}'";
			if ($user_id > 0) $query .= " and o.owner_id = {$user_id} ";
			if (!empty($metadata_type) && !empty($metadata_value)) {
				$query .= " and mv.value = '{$metadata_value}' and mt.name = '{$metadata_type}' ";
				$query .= " and (om.access_id in {$access} or (om.access_id = 0 and o.owner_id = {$_SESSION['id']}))";
			}
			$query .= " order by o.time_created desc ";
			if ($limit > 0 || $offset > 0) $query .= " limit {$offset}, {$limit}";
			
			return get_data($query);
			
		}

	/**
	 * Retrieves details about an object, if the current user is allowed to see it
	 *
	 * @param int $object_id The ID of the object to load
	 * @return object A database representation of the object
	 */
		function get_object($object_id) {
			
			global $CONFIG;
			
			$object_id = (int) $object_id;
			$access = get_access_list();
			
			return get_data_row("select o.*, ot.name as typename from {$CONFIG->dbprefix}objects left join {$CONFIG->dbprefix}object_types ot on ot.id = o.type_id where (o.access_id in {$access} or (o.access_id = 0 and o.owner_id = {$_SESSION['id']}))");			
			
		}
		
	/**
	 * Deletes an object and all accompanying metadata
	 *
	 * @param int $object_id The ID of the object
	 * @return true|false Depending on success
	 */
		function delete_object($object_id) {
			
			global $CONFIG;
			
			$object_id = (int) $object_id;
			$access = get_access_list();
			
			if (delete_data("delete from {$CONFIG->dbprefix}objects where o.id = {$object_id} and o.owner_id = {$_SESSION['id']}")) {
				remove_object_metadata($object_id);
				return true;
			}
			
			return false;
		}
		
	/**
	 * Creates an object
	 *
	 * @param string $title Object title
	 * @param string $description A description of the object
	 * @param string $type The textual type of the object (eg "blog")
	 * @param int $owner The owner of the object (defaults to currently logged in user)
	 * @param int $access_id The access restriction on the object (defaults to private)
	 * @param int $site_id The site the object belongs to
	 * @return int The ID of the newly-inserted object
	 */
		function create_object($title, $description, $type, $owner = 0, $access_id = 0, $site_id = 0) {
			
			global $CONFIG;
			
			$title = sanitise_string($title);
			$description = sanitise_string($description);
			$owner = (int) $owner;
			$site_id = (int) $site_id;
			$access_id = (int) $access_id;
			if ($site_id == 0) $site_id = $CONFIG->site_id;
			if ($owner == 0) $owner = $_SESSION['id'];
			
			// We can't let non-logged in users create data
			// We also need the access restriction to be valid
			if ($owner > 0 && in_array($access_id,get_access_array())) {
				
				$type_id = get_object_type_id($type);
				
				$query = " insert into {$CONFIG->dbprefix}objects ";
				$query .= "(`title`,`description`,`type_id`,`owner_id`,`site_id`,`access_id`) values ";
				$query .= "('{$title}','{$description}', {$type_id}, {$owner}, {$site_id}, {$access_id}";
				return insert_data($query);
				
			}
			return false;
			
		}

	/**
	 * Update an object
	 * Note that to write to an object, you must be logged in as the owner
	 *
	 * @param int $id The ID of the object
	 * @param string $title Object title
	 * @param string $description A description of the object
	 * @param string $type The textual type of the object (eg "blog")
	 * @param int $owner The owner of the object (defaults to currently logged in user)
	 * @param int $access_id The access restriction on the object (defaults to private)
	 * @param int $site_id The site the object belongs to
	 * @return int|false Either 1 or 0 (the number of objects updated) or false on failure
	 */
		
		function update_object($id, $title = null, $description = null, $type = null, $owner_id = null, $access_id = null, $site_id = null) {

			global $CONFIG;
			
			$id = (int) $id;
			if ($title != null) $title = sanitise_string($title);
			if ($description != null) $description = sanitise_string($description);
			if ($owner_id != null) $owner_id = (int) $owner_id;
			if ($site_id != null) $site_id = (int) $site_id;
			if ($access_id != null) $access_id = (int) $access_id;
			if ($site_id != null) if ($site_id == 0) $site_id = $CONFIG->site_id;
			if ($owner_id != null) if ($owner_id == 0) $owner = $_SESSION['id'];
			
			// We can't let non-logged in users create data
			// We also need the access restriction to be valid
			if ($owner > 0 && in_array($access_id,get_access_array())) {
			
				$params = array();
				foreach(array('title','description','owner','site_id','access_id','site_id','owner') as $param) {
					if ($$param != null) {
						$params[] = "{$param} = '{$$param}'";
					}
				}
				
				return update_data("update {$CONFIG->prefix}objects set " . implode(",",$params) . " where id = {$id} and owner_id = {$_SESSION['id']}");
				
			}
			return false;
			
		}
		
	/**
	 * Gets the ID of an object type in the database, setting it if necessary 
	 *
	 * @param string $type The name of the object type
	 * @return int|false The database ID of the object type, or false if the given type was invalid
	 */
		function get_object_type_id($type) {
			
			global $CONFIG;
			
			$type = strtolower(trim(sanitise_string($type)));
			if (!empty($type) && $dbtype = get_data_row("select id from {$CONFIG->dbprefix}object_types where name = '{$type}'")) {
				return $dbtype->id;
			} else if (!empty($type)) {
				return insert_data("insert into {$CONFIG->dbprefix}object_types set name = '{$type}'");
			}
			return false;
			
		}
		
	/**
	 * Gets the ID of an object metadata type in the database, setting it if necessary 
	 *
	 * @param string $type The name of the metadata type
	 * @return int|false The database ID of the metadata type, or false if the given type was invalid
	 */
		function get_metadata_type_id($type) {
			
			global $CONFIG;
			$type = strtolower(trim(sanitise_string($type)));
			if (!empty($type) && $dbtype = get_data_row("select id from {$CONFIG->dbprefix}metadata_type where name = '{$type}'")) {
				return $dbtype->id;
			} else if (!empty($type)) {
				return insert_data("insert into {$CONFIG->dbprefix}metadata_type set name = '{$type}'");
			}
			return false;
			
		}

	/**
	 * Gets the ID of an object metadata value in the database, setting it if necessary 
	 *
	 * @param string $type The metadata value
	 * @return int|false The database ID of the metadata value, or false if the given value was invalid
	 */
		function get_metadata_value_id($value) {
			
			global $CONFIG;
			$type = strtolower(trim(sanitise_string($value)));
			if (!empty($value) && $dbtype = get_data_row("select id from {$CONFIG->dbprefix}metadata_value where value = '{$value}'")) {
				return $dbtype->id;
			} else if (!empty($value)) {
				return insert_data("insert into {$CONFIG->dbprefix}metadata_value set value = '{$value}'");
			}
			return false;
			
		}
		
	/**
	 * Sets a piece of metadata for a particular object.
	 *
	 * @param string $metadata_name The type of metadata
	 * @param string $metadata_value Its value
	 * @param int $access_id The access level of the metadata
	 * @param int $object_id The ID of the object
	 * @return true|false depending on success
	 */
		function set_object_metadata($metadata_name, $metadata_value, $access_id, $object_id, $site_id = 0) {
			global $CONFIG;
			$object_id = (int) $object_id;
			if ($object = get_object($object_id)) {
				if ($object->owner_id == $_SESSION['id']) {
					
					$access_id = (int) $access_id;
					if ($site_id == 0) $site_id = $CONFIG->site_id;
					$site_id = (int) $site_id;
					
					if ($type_id = get_object_metadata_type_id($metadata_name)
						&& $value_id = get_object_metadata_value_id($metadata_value)
						&& in_array($access_id,get_access_array())) {
						delete_data("delete from {$CONFIG->dbprefix}object_metadata where metadata_type_id = {$type_id} and object_id = {$object_id}");
						return insert_data("insert into {$CONFIG->dbprefix}object_metadata set object_id = {$object_id}, access_id = {$access_id}, metadata_type_id = {$type_id}, value_id = {$value_id}, site_id = {$site_id}");						
					} else {
						return false;
					}
					
				}
			} else {
				return false;
			}
		}

	/**
	 * Returns the value of a particular piece of metadata on an object
	 *
	 * @param string $metadata_name The name of the metadata
	 * @param int $object_id The object ID
	 * @param int $site_id The site ID, optionally
	 * @return mixed The value of the metadata
	 */
		function get_object_metadata($metadata_name, $object_id, $site_id = 0) {
			
			if ($type_id = get_metadata_type_id($metadata_name)) {
				
				$accesslist = get_access_list();
				$object_id = (int) $objet_id;
				if ($site_id == 0) $site_id = $CONFIG->site_id;
				$site_id = (int) $site_id;
				
				if ($result = get_data_row("select mv.value from object_metadata om left join metadata_value mv on mv.id = om.value_id where om.object_id = {$object_id} and om.site_id = {$site_id} and om.metadata_type_id = {$type_id}")) {
					return $result->value;
				}
				return false;
				
			}
			
		}
		
	/**
	 * Removes a piece of (or all) metadata for a particular object.
	 *
	 * @param int $object_id The ID of the object
	 * @param string $metadata_name The type of metadata; blank for all metadata
	 * @return true|false depending on success
	 */
		function remove_object_metadata($object_id, $metadata_name = "") {
			global $CONFIG;
			$object_id = (int) $object_id;
			if ($object = get_object($object_id)) {
				if ($object->owner_id == $_SESSION['id']) {
					
					if ($type_id = get_object_metadata_type_id($metadata_name)) {
						return delete_data("delete from {$CONFIG->dbprefix}object_metadata where metadata_type_id = {$type_id} and object_id = {$object_id}");						
					} else {
						return false;
					}
					
				}
			} else {
				return false;
			}
			return true;
		}
		
	/**
	 * This class represents an Elgg object.
	 *
	 */
		class ElggObject {
			
			private $attributes = array();
			
			function __get($name) {
				
				if (isset($this->attributes[$name])) {
					return $this->attributes[$name];
				}
				return null;
			}
			
			function __set($name, $value) {
				$this->attributes[$name] = $value;
				return true;
			}
			
			function __construct($id = null) {
				
				$this->attributes = array();
				
				if (!empty($id)) {
					if ($object = get_object($id)) {
						$objarray = (array) $object;
						foreach($objarray as $key => $value) {
							$this->attributes[$key] = $value;
						}
					}
				}
			}

			
		/**
		 * Obtains the parent site
		 *
		 * @return ElggSite The parent site
		 */
			function getSite() {
				// TODO: gets the parent site
			}
			
		
			
		/**
		 * Returns the value of a particular piece of metadata
		 *
		 * @param string $name The name of the metadata
		 * @return mixed|false The metadata value; false on failure
		 */
			function getMetadata($name) {
				if (!empty($this->id)) {
					return get_object_metadata($name, $this->id, $this->site_id);
				}
				return false;
			}
			
		/**
		 * Adds metadata for this object
		 *
		 * @uses set_object_metadata
		 * @param string $name The name of the metadata type
		 * @param string $value The value for the metadata to set
		 * @param int $access_id The access level for this piece of metadata (default: private)
		 * @return true|false Depending on success
		 */
			function setMetadata($name, $value, $access_id = 0) {
				if (!empty($this->id)) {
					return set_object_metadata($name, $value, $access_id, $this->id, $this->site_id);
				}
				return false;
			}
			
		/**
		 * Clears metadata for this object, either for a particular type or across the board
		 *
		 * @uses remove_object_metadata
		 * @param string $name Optionally, the name of the metadata to remove
		 * @return true|false Depending on success
		 */
			function clearMetadata($name = "") {
				if (!empty($this->id)) {
					return remove_object_metadata($this->id, $name);
				}
				return false;
			}
			
		/**
		 * Adds an annotation to an object
		 *
		 * @param string $name Name of the annotation type
		 * @param string|int $value The annotation value
		 * @param int $access_id The access level for the annotation
		 * @param int $owner_id The annotation owner
		 * @param string $vartype Optionally, the variable type of the annotation (eg "int")
		 */
			function annotate($name, $value, $access_id = 0, $owner_id = 0, $vartype = "") {
				// TODO: add annotation
			}
			
		/**
		 * Returns the object's annotations of a particular type (eg "comment")
		 *
		 * @param string $name The type of annotation
		 * @param int $limit Number of annotations to get
		 * @param int $offset Any offset
		 */
			function getAnnotations($name, $limit = 50, $offset = 0) {
				// TODO: get annotations
			}
			
		/**
		 * Gets the average of the integer annotations on this object
		 *
		 * @param string $name Optionally, the type of annotation
		 */
			function getAnnotationsAvg($name) {
			}

		/**
		 * Gets the sum of the integer annotations on this object
		 *
		 * @param string $name Optionally, the type of annotation
		 */
			function getAnnotationsSum($name) {
			}
			
		/**
		 * Gets the minimum value of the integer annotations on this object
		 *
		 * @param string $name Optionally, the type of annotation
		 */
			function getAnnotationsMin($name) {
			}
			
		/**
		 * Gets the maximum value of the integer annotations on this object
		 *
		 * @param string $name Optionally, the type of annotation
		 */
			function getAnnotationsMax($name) {
			}
			
		/**
		 * Inserts or updates the object, depending on whether it's new or not
		 *
		 * @return true|false Depending on success
		 */
			function save() {
				if (!empty($this->id)) {
					return update_object($this->id, $this->title, $this->description, $this->type, $this->owner_id, $this->access_id, $this->site_id);				
				} else if ($id = create_object($this->title,$this->description,$this->type,$this->owner_id,$this->access_id,$this->site_id)) {
					$this->id = $id;
					return true;
				}
			}
			
		/**
		 * Deletes this object
		 *
		 * @uses delete_object
		 * @return int|false The number of objects deleted, or false on failure
		 */
			function delete() {
				if (!empty($this->id)) {
					return delete_object($this->id);
				}
				return false;
			}

		}
		
?>