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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
|
<?php
/**
* The parent class for all Elgg Entities.
*
* An ElggEntity is one of the basic data models in Elgg. It is the primary
* means of storing and retrieving data from the database. An ElggEntity
* represents one row of the entities table.
*
* The ElggEntity class handles CRUD operations for the entities table.
* ElggEntity should always be extended by another class to handle CRUD
* operations on the type-specific table.
*
* ElggEntity uses magic methods for get and set, so any property that isn't
* declared will be assumed to be metadata and written to the database
* as metadata on the object. All children classes must declare which
* properties are columns of the type table or they will be assumed
* to be metadata. See ElggObject::initialise_entities() for examples.
*
* Core supports 4 types of entities: ElggObject, ElggUser, ElggGroup, and
* ElggSite.
*
* @tip Most plugin authors will want to extend the ElggObject class
* instead of this class.
*
* @package Elgg.Core
* @subpackage DataModel.Entities
* @link http://docs.elgg.org/DataModel/ElggEntity
*/
abstract class ElggEntity extends ElggData implements
Notable, // Calendar interface
Locatable, // Geocoding interface
Importable // Allow import of data
{
/**
* If set, overrides the value of getURL()
*/
protected $url_override;
/**
* Icon override, overrides the value of getIcon().
*/
protected $icon_override;
/**
* Holds metadata until entity is saved. Once the entity is saved,
* metadata are written immediately to the database.
*/
protected $temp_metadata = array();
/**
* Holds annotations until entity is saved. Once the entity is saved,
* annotations are written immediately to the database.
*/
protected $temp_annotations = array();
/**
* Holds private settings until entity is saved. Once the entity is saved,
* private settings are written immediately to the database.
*/
protected $temp_private_settings = array();
/**
* Volatile data structure for this object, allows for storage of data
* in-memory that isn't sync'd back to the metadata table.
*/
protected $volatile = array();
/**
* Initialize the attributes array.
*
* This is vital to distinguish between metadata and base parameters.
*
* @return void
*/
protected function initializeAttributes() {
parent::initializeAttributes();
$this->attributes['guid'] = NULL;
$this->attributes['type'] = NULL;
$this->attributes['subtype'] = NULL;
$this->attributes['owner_guid'] = elgg_get_logged_in_user_guid();
$this->attributes['container_guid'] = elgg_get_logged_in_user_guid();
$this->attributes['site_guid'] = NULL;
$this->attributes['access_id'] = ACCESS_PRIVATE;
$this->attributes['time_updated'] = NULL;
$this->attributes['last_action'] = NULL;
$this->attributes['enabled'] = "yes";
// There now follows a bit of a hack
/* Problem: To speed things up, some objects are split over several tables,
* this means that it requires n number of database reads to fully populate
* an entity. This causes problems for caching and create events
* since it is not possible to tell whether a subclassed entity is complete.
*
* Solution: We have two counters, one 'tables_split' which tells whatever is
* interested how many tables are going to need to be searched in order to fully
* populate this object, and 'tables_loaded' which is how many have been
* loaded thus far.
*
* If the two are the same then this object is complete.
*
* Use: isFullyLoaded() to check
*/
$this->attributes['tables_split'] = 1;
$this->attributes['tables_loaded'] = 0;
}
/**
* Clone an entity
*
* Resets the guid so that the entity can be saved as a distinct entity from
* the original. Creation time will be set when this new entity is saved.
* The owner and container guids come from the original entity. The clone
* method copies metadata but does not copy annotations or private settings.
*
* @note metadata will have its owner and access id set when the entity is saved
* and it will be the same as that of the entity.
*
* @return void
*/
public function __clone() {
$orig_entity = get_entity($this->guid);
if (!$orig_entity) {
elgg_log("Failed to clone entity with GUID $this->guid", "ERROR");
return;
}
$metadata_array = elgg_get_metadata(array(
'guid' => $this->guid,
'limit' => 0
));
$this->attributes['guid'] = "";
$this->attributes['subtype'] = $orig_entity->getSubtype();
// copy metadata over to new entity - slightly convoluted due to
// handling of metadata arrays
if (is_array($metadata_array)) {
// create list of metadata names
$metadata_names = array();
foreach ($metadata_array as $metadata) {
$metadata_names[] = $metadata['name'];
}
// arrays are stored with multiple enties per name
$metadata_names = array_unique($metadata_names);
// move the metadata over
foreach ($metadata_names as $name) {
$this->set($name, $orig_entity->$name);
}
}
}
/**
* Return the value of a property.
*
* If $name is defined in $this->attributes that value is returned, otherwise it will
* pull from the entity's metadata.
*
* Q: Why are we not using __get overload here?
* A: Because overload operators cause problems during subclassing, so we put the code here and
* create overloads in subclasses.
*
* @todo What problems are these?
*
* @warning Subtype is returned as an id rather than the subtype string. Use getSubtype()
* to get the subtype string.
*
* @param string $name Name
*
* @return mixed Returns the value of a given value, or null.
*/
public function get($name) {
// See if its in our base attributes
if (array_key_exists($name, $this->attributes)) {
return $this->attributes[$name];
}
// No, so see if its in the meta data for this entity
$meta = $this->getMetaData($name);
// getMetaData returns NULL if $name is not found
return $meta;
}
/**
* Sets the value of a property.
*
* If $name is defined in $this->attributes that value is set, otherwise it will
* set the appropriate item of metadata.
*
* @warning It is important that your class populates $this->attributes with keys
* for all base attributes, anything not in their gets set as METADATA.
*
* Q: Why are we not using __set overload here?
* A: Because overload operators cause problems during subclassing, so we put the code here and
* create overloads in subclasses.
*
* @todo What problems?
*
* @param string $name Name
* @param mixed $value Value
*
* @return bool
*/
public function set($name, $value) {
if (array_key_exists($name, $this->attributes)) {
// Certain properties should not be manually changed!
switch ($name) {
case 'guid':
case 'time_created':
case 'time_updated':
case 'last_action':
return FALSE;
break;
default:
$this->attributes[$name] = $value;
break;
}
} else {
return $this->setMetaData($name, $value);
}
return TRUE;
}
/**
* Return the value of a piece of metadata.
*
* @param string $name Name
*
* @return mixed The value, or NULL if not found.
*/
public function getMetaData($name) {
if ((int) ($this->guid) > 0) {
$md = elgg_get_metadata(array(
'guid' => $this->getGUID(),
'metadata_name' => $name
));
} else {
if (isset($this->temp_metadata[$name])) {
return $this->temp_metadata[$name];
}
}
if ($md && !is_array($md)) {
return $md->value;
} elseif (count($md) == 1) {
return $md[0]->value;
} else if ($md && is_array($md)) {
return metadata_array_to_values($md);
}
return null;
}
/**
* Unset a property from metadata or attribute.
*
* @warning If you use this to unset an attribute, you must save the object!
*
* @param string $name The name of the attribute or metadata.
*
* @return void
*/
function __unset($name) {
if (array_key_exists($name, $this->attributes)) {
$this->attributes[$name] = "";
} else {
$this->clearMetaData($name);
}
}
/**
* Set a piece of metadata.
*
* @tip Plugin authors should use the magic methods.
*
* @access private
*
* @param string $name Name of the metadata
* @param mixed $value Value of the metadata
* @param string $value_type Types supported: integer and string. Will auto-identify if not set
* @param bool $multiple Allow multiple values for a single name (doesn't support assoc arrays)
*
* @return bool
*/
public function setMetaData($name, $value, $value_type = "", $multiple = false) {
if (is_array($value)) {
unset($this->temp_metadata[$name]);
remove_metadata($this->getGUID(), $name);
foreach ($value as $v) {
if ((int) $this->guid > 0) {
$multiple = true;
if (!create_metadata($this->getGUID(), $name, $v, $value_type,
$this->getOwnerGUID(), $this->getAccessID(), $multiple)) {
return false;
}
} else {
if (($multiple) && (isset($this->temp_metadata[$name]))) {
if (!is_array($this->temp_metadata[$name])) {
$tmp = $this->temp_metadata[$name];
$this->temp_metadata[$name] = array();
$this->temp_metadata[$name][] = $tmp;
}
$this->temp_metadata[$name][] = $value;
} else {
$this->temp_metadata[$name] = $value;
}
}
}
return true;
} else {
unset($this->temp_metadata[$name]);
if ((int) $this->guid > 0) {
$result = create_metadata($this->getGUID(), $name, $value, $value_type,
$this->getOwnerGUID(), $this->getAccessID(), $multiple);
return (bool)$result;
} else {
if (($multiple) && (isset($this->temp_metadata[$name]))) {
if (!is_array($this->temp_metadata[$name])) {
$tmp = $this->temp_metadata[$name];
$this->temp_metadata[$name] = array();
$this->temp_metadata[$name][] = $tmp;
}
$this->temp_metadata[$name][] = $value;
} else {
$this->temp_metadata[$name] = $value;
}
return true;
}
}
}
/**
* Remove metadata
*
* @warning Calling this with no or empty arguments will clear all metadata on the entity.
*
* @param string $name The name of the metadata to clear
*
* @return mixed bool
*/
public function clearMetaData($name = "") {
if (empty($name)) {
return clear_metadata($this->getGUID());
} else {
return remove_metadata($this->getGUID(), $name);
}
}
/**
* Get a piece of volatile (non-persisted) data on this entity.
*
* @param string $name The name of the volatile data
*
* @return mixed The value or NULL if not found.
*/
public function getVolatileData($name) {
if (!is_array($this->volatile)) {
$this->volatile = array();
}
if (array_key_exists($name, $this->volatile)) {
return $this->volatile[$name];
} else {
return NULL;
}
}
/**
* Set a piece of volatile (non-persisted) data on this entity
*
* @param string $name Name
* @param mixed $value Value
*
* @return void
*/
public function setVolatileData($name, $value) {
if (!is_array($this->volatile)) {
$this->volatile = array();
}
$this->volatile[$name] = $value;
}
/**
* Remove all relationships to and from this entity.
*
* @return true
* @todo This should actually return if it worked.
* @see ElggEntity::addRelationship()
* @see ElggEntity::removeRelationship()
*/
public function clearRelationships() {
remove_entity_relationships($this->getGUID());
remove_entity_relationships($this->getGUID(), "", true);
return true;
}
/**
* Add a relationship between this an another entity.
*
* @tip Read the relationship like "$guid is a $relationship of this entity."
*
* @param int $guid Entity to link to.
* @param string $relationship The type of relationship.
*
* @return bool
* @see ElggEntity::removeRelationship()
* @see ElggEntity::clearRelationships()
*/
public function addRelationship($guid, $relationship) {
return add_entity_relationship($this->getGUID(), $relationship, $guid);
}
/**
* Remove a relationship
*
* @param int $guid GUID of the entity to make a relationship with
* @param str $relationship Name of relationship
*
* @return bool
* @see ElggEntity::addRelationship()
* @see ElggEntity::clearRelationships()
*/
public function removeRelationship($guid, $relationship) {
return remove_entity_relationship($this->getGUID(), $relationship, $guid);
}
/**
* Adds a private setting to this entity.
*
* Private settings are similar to metadata but will not
* be searched and there are fewer helper functions for them.
*
* @param string $name Name of private setting
* @param mixed $value Value of private setting
*
* @return bool
* @link http://docs.elgg.org/DataModel/Entities/PrivateSettings
*/
function setPrivateSetting($name, $value) {
if ((int) $this->guid > 0) {
return set_private_setting($this->getGUID(), $name, $value);
} else {
$this->temp_private_settings[$name] = $value;
return true;
}
}
/**
* Returns a private setting value
*
* @param string $name Name of the private setting
*
* @return mixed
*/
function getPrivateSetting($name) {
if ((int) ($this->guid) > 0) {
return get_private_setting($this->getGUID(), $name);
} else {
if (isset($this->temp_private_settings[$name])) {
return $this->temp_private_settings[$name];
}
}
return null;
}
/**
* Removes private setting
*
* @param string $name Name of the private setting
*
* @return bool
*/
function removePrivateSetting($name) {
return remove_private_setting($this->getGUID(), $name);
}
/**
* Adds an annotation to an entity.
*
* @warning By default, annotations are private.
*
* @param string $name Annotation name
* @param mixed $value Annotation value
* @param int $access_id Access ID
* @param int $owner_id GUID of the annotation owner
* @param string $vartype The type of annotation value
*
* @return bool
*
* @link http://docs.elgg.org/DataModel/Annotations
*/
function annotate($name, $value, $access_id = ACCESS_PRIVATE, $owner_id = 0, $vartype = "") {
if ((int) $this->guid > 0) {
return create_annotation($this->getGUID(), $name, $value, $vartype, $owner_id, $access_id);
} else {
$this->temp_annotations[$name] = $value;
}
return true;
}
/**
* Returns an array of annotations.
*
* @param string $name Annotation name
* @param int $limit Limit
* @param int $offset Offset
* @param string $order Order by time: asc or desc
*
* @return array
*/
function getAnnotations($name, $limit = 50, $offset = 0, $order = "asc") {
if ((int) ($this->guid) > 0) {
$options = array(
'guid' => $this->guid,
'annotation_name' => $name,
'limit' => $limit,
'offset' => $offset,
);
if ($order == 'desc') {
$options['order_by'] = 'n_table.time_created desc';
}
return elgg_get_annotations($options);
} else {
return $this->temp_annotations[$name];
}
}
/**
* Remove an annotation or all annotations for this entity.
*
* @warning Calling this method with no or an empty argument will remove
* all annotations on the entity.
*
* @param string $name Annotation name
*
* @return bool
*/
function clearAnnotations($name = "") {
return clear_annotations($this->getGUID(), $name);
}
/**
* Count annotations.
*
* @param string $name The type of annotation.
*
* @return int
*/
function countAnnotations($name = "") {
return count_annotations($this->getGUID(), "", "", $name);
}
/**
* Get the average of an integer type annotation.
*
* @param string $name Annotation name
*
* @return int
*/
function getAnnotationsAvg($name) {
return get_annotations_avg($this->getGUID(), "", "", $name);
}
/**
* Get the sum of integer type annotations of a given name.
*
* @param string $name Annotation name
*
* @return int
*/
function getAnnotationsSum($name) {
return get_annotations_sum($this->getGUID(), "", "", $name);
}
/**
* Get the minimum of integer type annotations of given name.
*
* @param string $name Annotation name
*
* @return int
*/
function getAnnotationsMin($name) {
return get_annotations_min($this->getGUID(), "", "", $name);
}
/**
* Get the maximum of integer type annotations of a given name.
*
* @param string $name Annotation name
*
* @return int
*/
function getAnnotationsMax($name) {
return get_annotations_max($this->getGUID(), "", "", $name);
}
/**
* Count the number of comments attached to this entity.
*
* @return int Number of comments
* @since 1.8.0
*/
function countComments() {
$type = $this->getType();
$params = array('entity' => $this);
$number = elgg_trigger_plugin_hook('comments:count', $type, $params, false);
if ($number) {
return $number;
} else {
return count_annotations($this->getGUID(), "", "", "generic_comment");
}
}
/**
* Count how many people have liked this entity.
*
* @return int Number of likes
* @since 1.8.0
*/
function countLikes() {
$type = $this->getType();
$params = array('entity' => $this);
$number = elgg_trigger_plugin_hook('likes:count', $type, $params, false);
if ($number) {
return $number;
} else {
return count_annotations($this->getGUID(), "", "", "likes");
}
}
/**
* Gets an array of entities with a relationship to this entity.
*
* @param string $relationship Relationship type (eg "friends")
* @param bool $inverse Is this an inverse relationship?
* @param int $limit Number of elements to return
* @param int $offset Indexing offset
*
* @return array|false An array of entities or false on failure
*/
function getEntitiesFromRelationship($relationship, $inverse = false, $limit = 50, $offset = 0) {
return elgg_get_entities_from_relationship(array(
'relationship' => $relationship,
'relationship_guid' => $this->getGUID(),
'inverse_relationship' => $inverse,
'limit' => $limit,
'offset' => $offset
));
}
/**
* Gets the number of of entities from a specific relationship type
*
* @param string $relationship Relationship type (eg "friends")
* @param bool $inverse_relationship Invert relationship
*
* @return int|false The number of entities or false on failure
*/
function countEntitiesFromRelationship($relationship, $inverse_relationship = FALSE) {
return elgg_get_entities_from_relationship(array(
'relationship' => $relationship,
'relationship_guid' => $this->getGUID(),
'inverse_relationship' => $inverse_relationship,
'count' => TRUE
));
}
/**
* Can a user edit this entity.
*
* @param int $user_guid The user GUID, optionally (default: logged in user)
*
* @return bool
*/
function canEdit($user_guid = 0) {
return can_edit_entity($this->getGUID(), $user_guid);
}
/**
* Can a user edit metadata on this entity
*
* @param ElggMetadata $metadata The piece of metadata to specifically check
* @param int $user_guid The user GUID, optionally (default: logged in user)
*
* @return true|false
*/
function canEditMetadata($metadata = null, $user_guid = 0) {
return can_edit_entity_metadata($this->getGUID(), $user_guid, $metadata);
}
/**
* Can a user write to this entity
*
* @param int $user_guid The user.
* @param string $type The type of entity we're looking to write
* @param string $subtype The subtype of the entity we're looking to write
*
* @return bool
*/
public function canWriteToContainer($user_guid = 0, $type = 'all', $subtype = 'all') {
return can_write_to_container($user_guid, $this->guid, $type, $subtype);
}
/**
* Returns the access_id.
*
* @return int The access ID
*/
public function getAccessID() {
return $this->get('access_id');
}
/**
* Returns the guid.
*
* @return int GUID
*/
public function getGUID() {
return $this->get('guid');
}
/**
* Returns the entity type
*
* @return string Entity type
*/
public function getType() {
return $this->get('type');
}
/**
* Returns the entity subtype string
*
* @note This returns a string. If you want the id, use ElggEntity::subtype.
*
* @return string The entity subtype
*/
public function getSubtype() {
// If this object hasn't been saved, then return the subtype string.
if (!((int) $this->guid > 0)) {
return $this->get('subtype');
}
return get_subtype_from_id($this->get('subtype'));
}
/**
* Get the guid of the entity's owner.
*
* @return int The owner GUID
*/
public function getOwnerGUID() {
return $this->owner_guid;
}
/**
* Return the guid of the entity's owner.
*
* @return int The owner GUID
* @deprecated 1.8 Use getOwnerGUID()
*/
public function getOwner() {
elgg_deprecated_notice("ElggEntity::getOwner deprecated for ElggEntity::getOwnerGUID", 1.8);
return $this->getOwnerGUID();
}
/**
* Gets the ElggEntity that owns this entity.
*
* @return ElggEntity The owning entity
*/
public function getOwnerEntity() {
return get_entity($this->owner_guid);
}
/**
* Set the container for this object.
*
* @param int $container_guid The ID of the container.
*
* @return bool
*/
public function setContainerGUID($container_guid) {
$container_guid = (int)$container_guid;
return $this->set('container_guid', $container_guid);
}
/**
* Set the container for this object.
*
* @param int $container_guid The ID of the container.
*
* @return bool
* @deprecated 1.8 use setContainerGUID()
*/
public function setContainer($container_guid) {
elgg_deprecated_notice("ElggObject::setContainer deprecated for ElggEntity::setContainerGUID", 1.8);
$container_guid = (int)$container_guid;
return $this->set('container_guid', $container_guid);
}
/**
* Gets the container GUID for this entity.
*
* @return int
*/
public function getContainerGUID() {
return $this->get('container_guid');
}
/**
* Gets the container GUID for this entity.
*
* @return int
* @deprecated 1.8 Use getContainerGUID()
*/
public function getContainer() {
elgg_deprecated_notice("ElggObject::getContainer deprecated for ElggEntity::getContainerGUID", 1.8);
return $this->get('container_guid');
}
/**
* Get the container entity for this object.
*
* @return ElggEntity
* @since 1.8.0
*/
public function getContainerEntity() {
return get_entity($this->getContainerGUID());
}
/**
* Returns the UNIX epoch time that this entity was last updated
*
* @return int UNIX epoch time
*/
public function getTimeUpdated() {
return $this->get('time_updated');
}
/**
* Returns the URL for this entity
*
* @return string The URL
* @see register_entity_url_handler()
* @see ElggEntity::setURL()
*/
public function getURL() {
if (!empty($this->url_override)) {
return $this->url_override;
}
return get_entity_url($this->getGUID());
}
/**
* Overrides the URL returned by getURL()
*
* @warning This override exists only for the life of the object.
*
* @param string $url The new item URL
*
* @return string The URL
*/
public function setURL($url) {
$this->url_override = $url;
return $url;
}
/**
* Get the URL for this entity's icon
*
* Plugins can register for the 'entity:icon:url', <type> plugin hook
* to customize the icon for an entity.
*
* @param string $size Size of the icon: tiny, small, medium, large
*
* @return string The URL
* @since 1.8.0
*/
public function getIconURL($size = 'medium') {
$size = elgg_strtolower($size);
if (isset($this->icon_override[$size])) {
elgg_deprecated_notice("icon_override on an individual entity is deprecated", 1.8);
return $this->icon_override[$size];
}
$url = "_graphics/icons/default/$size.png";
$url = elgg_normalize_url($url);
$type = $this->getType();
$params = array(
'entity' => $this,
'size' => $size,
);
$url = elgg_trigger_plugin_hook('entity:icon:url', $type, $params, $url);
return elgg_normalize_url($url);
}
/**
* Returns a URL for the entity's icon.
*
* @param string $size Either 'large', 'medium', 'small' or 'tiny'
*
* @return string The url or false if no url could be worked out.
* @deprecated Use getIconURL()
*/
public function getIcon($size = 'medium') {
elgg_deprecated_notice("getIcon() deprecated by getIconURL()", 1.8);
return $this->getIconURL($size);
}
/**
* Set an icon override for an icon and size.
*
* @warning This override exists only for the life of the object.
*
* @param string $url The url of the icon.
* @param string $size The size its for.
*
* @return bool
* @deprecated 1.8 See getIconURL() for the plugin hook to use
*/
public function setIcon($url, $size = 'medium') {
elgg_deprecated_notice("icon_override on an individual entity is deprecated", 1.8);
$url = sanitise_string($url);
$size = sanitise_string($size);
if (!$this->icon_override) {
$this->icon_override = array();
}
$this->icon_override[$size] = $url;
return true;
}
/**
* Tests to see whether the object has been fully loaded.
*
* @return bool
*/
public function isFullyLoaded() {
return ! ($this->attributes['tables_loaded'] < $this->attributes['tables_split']);
}
/**
* Save an entity.
*
* @return bool/int
* @throws IOException
*/
public function save() {
$guid = (int) $this->guid;
if ($guid > 0) {
cache_entity($this);
return update_entity(
$this->get('guid'),
$this->get('owner_guid'),
$this->get('access_id'),
$this->get('container_guid')
);
} else {
// Create a new entity (nb: using attribute array directly
// 'cos set function does something special!)
$this->attributes['guid'] = create_entity($this->attributes['type'],
$this->attributes['subtype'], $this->attributes['owner_guid'],
$this->attributes['access_id'], $this->attributes['site_guid'],
$this->attributes['container_guid']);
if (!$this->attributes['guid']) {
throw new IOException(elgg_echo('IOException:BaseEntitySaveFailed'));
}
// Save any unsaved metadata
// @todo How to capture extra information (access id etc)
if (sizeof($this->temp_metadata) > 0) {
foreach ($this->temp_metadata as $name => $value) {
$this->$name = $value;
unset($this->temp_metadata[$name]);
}
}
// Save any unsaved annotations.
if (sizeof($this->temp_annotations) > 0) {
foreach ($this->temp_annotations as $name => $value) {
$this->annotate($name, $value);
unset($this->temp_annotations[$name]);
}
}
// Save any unsaved private settings.
if (sizeof($this->temp_private_settings) > 0) {
foreach ($this->temp_private_settings as $name => $value) {
$this->setPrivateSetting($name, $value);
unset($this->temp_private_settings[$name]);
}
}
// set the subtype to id now rather than a string
$this->attributes['subtype'] = get_subtype_id($this->attributes['type'],
$this->attributes['subtype']);
// Cache object handle
if ($this->attributes['guid']) {
cache_entity($this);
}
return $this->attributes['guid'];
}
}
/**
* Loads attributes from the entities table into the object.
*
* @param int $guid GUID of Entity
*
* @return bool
*/
protected function load($guid) {
$row = get_entity_as_row($guid);
if ($row) {
// Create the array if necessary - all subclasses should test before creating
if (!is_array($this->attributes)) {
$this->attributes = array();
}
// Now put these into the attributes array as core values
$objarray = (array) $row;
foreach ($objarray as $key => $value) {
$this->attributes[$key] = $value;
}
// Increment the portion counter
if (!$this->isFullyLoaded()) {
$this->attributes['tables_loaded']++;
}
// Cache object handle
if ($this->attributes['guid']) {
cache_entity($this);
}
return true;
}
return false;
}
/**
* Disable this entity.
*
* Disabled entities are not returned by getter functions.
* To enable an entity, use {@link enable_entity()}.
*
* Recursively disabling an entity will disable all entities
* owned or contained by the parent entity.
*
* @internal Disabling an entity sets the 'enabled' column to 'no'.
*
* @param string $reason Optional reason
* @param bool $recursive Recursively disable all contained entities?
*
* @return bool
* @see enable_entity()
* @see ElggEntity::enable()
*/
public function disable($reason = "", $recursive = true) {
if ($r = disable_entity($this->get('guid'), $reason, $recursive)) {
$this->attributes['enabled'] = 'no';
}
return $r;
}
/**
* Enable an entity
*
* @warning Disabled entities can't be loaded unless
* {@link access_show_hidden_entities(true)} has been called.
*
* @see enable_entity()
* @see access_show_hiden_entities()
* @return bool
*/
public function enable() {
if ($r = enable_entity($this->get('guid'))) {
$this->attributes['enabled'] = 'yes';
}
return $r;
}
/**
* Is this entity enabled?
*
* @return boolean
*/
public function isEnabled() {
if ($this->enabled == 'yes') {
return true;
}
return false;
}
/**
* Delete this entity.
*
* @param bool $recursive Whether to delete all the entities contained by this entity
*
* @return bool
*/
public function delete($recursive = true) {
return delete_entity($this->get('guid'), $recursive);
}
/*
* LOCATABLE INTERFACE
*/
/**
* Gets the 'location' metadata for the entity
*
* @return string The location
*/
public function getLocation() {
return $this->location;
}
/**
* Sets the 'location' metadata for the entity
*
* @todo Unimplemented
*
* @param string $location String representation of the location
*
* @return true
*/
public function setLocation($location) {
$location = sanitise_string($location);
$this->location = $location;
return true;
}
/**
* Set latitude and longitude metadata tags for a given entity.
*
* @param float $lat Latitude
* @param float $long Longitude
*
* @return true
* @todo Unimplemented
*/
public function setLatLong($lat, $long) {
$lat = sanitise_string($lat);
$long = sanitise_string($long);
$this->set('geo:lat', $lat);
$this->set('geo:long', $long);
return true;
}
/**
* Return the entity's latitude.
*
* @return int
* @todo Unimplemented
*/
public function getLatitude() {
return $this->get('geo:lat');
}
/**
* Return the entity's longitude
*
* @return Int
*/
public function getLongitude() {
return $this->get('geo:long');
}
/*
* NOTABLE INTERFACE
*/
/**
* Set the time and duration of an object
*
* @param int $hour If ommitted, now is assumed.
* @param int $minute If ommitted, now is assumed.
* @param int $second If ommitted, now is assumed.
* @param int $day If ommitted, now is assumed.
* @param int $month If ommitted, now is assumed.
* @param int $year If ommitted, now is assumed.
* @param int $duration Duration of event, remainder of the day is assumed.
*
* @return true
* @todo Unimplemented
*/
public function setCalendarTimeAndDuration($hour = NULL, $minute = NULL, $second = NULL,
$day = NULL, $month = NULL, $year = NULL, $duration = NULL) {
$start = mktime($hour, $minute, $second, $month, $day, $year);
$end = $start + abs($duration);
if (!$duration) {
$end = get_day_end($day, $month, $year);
}
$this->calendar_start = $start;
$this->calendar_end = $end;
return true;
}
/**
* Returns the start timestamp.
*
* @return int
* @todo Unimplemented
*/
public function getCalendarStartTime() {
return (int)$this->calendar_start;
}
/**
* Returns the end timestamp.
*
* @todo Unimplemented
*
* @return int
*/
public function getCalendarEndTime() {
return (int)$this->calendar_end;
}
/*
* EXPORTABLE INTERFACE
*/
/**
* Returns an array of fields which can be exported.
*
* @return array
*/
public function getExportableValues() {
return array(
'guid',
'type',
'subtype',
'time_created',
'time_updated',
'container_guid',
'owner_guid',
'site_guid'
);
}
/**
* Export this class into an array of ODD Elements containing all necessary fields.
* Override if you wish to return more information than can be found in
* $this->attributes (shouldn't happen)
*
* @return array
*/
public function export() {
$tmp = array();
// Generate uuid
$uuid = guid_to_uuid($this->getGUID());
// Create entity
$odd = new ODDEntity(
$uuid,
$this->attributes['type'],
get_subtype_from_id($this->attributes['subtype'])
);
$tmp[] = $odd;
$exportable_values = $this->getExportableValues();
// Now add its attributes
foreach ($this->attributes as $k => $v) {
$meta = NULL;
if (in_array( $k, $exportable_values)) {
switch ($k) {
case 'guid' : // Dont use guid in OpenDD
case 'type' : // Type and subtype already taken care of
case 'subtype' :
break;
case 'time_created' : // Created = published
$odd->setAttribute('published', date("r", $v));
break;
case 'site_guid' : // Container
$k = 'site_uuid';
$v = guid_to_uuid($v);
$meta = new ODDMetaData($uuid . "attr/$k/", $uuid, $k, $v);
break;
case 'container_guid' : // Container
$k = 'container_uuid';
$v = guid_to_uuid($v);
$meta = new ODDMetaData($uuid . "attr/$k/", $uuid, $k, $v);
break;
case 'owner_guid' : // Convert owner guid to uuid, this will be stored in metadata
$k = 'owner_uuid';
$v = guid_to_uuid($v);
$meta = new ODDMetaData($uuid . "attr/$k/", $uuid, $k, $v);
break;
default :
$meta = new ODDMetaData($uuid . "attr/$k/", $uuid, $k, $v);
}
// set the time of any metadata created
if ($meta) {
$meta->setAttribute('published', date("r", $this->time_created));
$tmp[] = $meta;
}
}
}
// Now we do something a bit special.
/*
* This provides a rendered view of the entity to foreign sites.
*/
elgg_set_viewtype('default');
$view = elgg_view_entity($this, true);
elgg_set_viewtype();
$tmp[] = new ODDMetaData($uuid . "volatile/renderedentity/", $uuid,
'renderedentity', $view, 'volatile');
return $tmp;
}
/*
* IMPORTABLE INTERFACE
*/
/**
* Import data from an parsed ODD xml data array.
*
* @param array $data XML data
*
* @return true
*/
public function import(ODD $data) {
if (!($data instanceof ODDEntity)) {
throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnexpectedODDClass'));
}
// Set type and subtype
$this->attributes['type'] = $data->getAttribute('class');
$this->attributes['subtype'] = $data->getAttribute('subclass');
// Set owner
$this->attributes['owner_guid'] = elgg_get_logged_in_user_guid(); // Import as belonging to importer.
// Set time
$this->attributes['time_created'] = strtotime($data->getAttribute('published'));
$this->attributes['time_updated'] = time();
return true;
}
/*
* SYSTEM LOG INTERFACE
*/
/**
* Return an identification for the object for storage in the system log.
* This id must be an integer.
*
* @return int
*/
public function getSystemLogID() {
return $this->getGUID();
}
/**
* For a given ID, return the object associated with it.
* This is used by the river functionality primarily.
*
* This is useful for checking access permissions etc on objects.
*
* @param int $id GUID.
*
* @todo How is this any different or more useful than get_entity($guid)
* or new ElggEntity($guid)?
*
* @return int GUID
*/
public function getObjectFromID($id) {
return get_entity($id);
}
/**
* Returns tags for this entity.
*
* @warning Tags must be registered by {@link elgg_register_tag_metadata_name()}.
*
* @param array $tag_names Optionally restrict by tag metadata names.
*
* @return array
*/
public function getTags($tag_names = NULL) {
global $CONFIG;
if ($tag_names && !is_array($tag_names)) {
$tag_names = array($tag_names);
}
$valid_tags = elgg_get_registered_tag_metadata_names();
$entity_tags = array();
foreach ($valid_tags as $tag_name) {
if (is_array($tag_names) && !in_array($tag_name, $tag_names)) {
continue;
}
if ($tags = $this->$tag_name) {
// if a single tag, metadata returns a string.
// if multiple tags, metadata returns an array.
if (is_array($tags)) {
$entity_tags = array_merge($entity_tags, $tags);
} else {
$entity_tags[] = $tags;
}
}
}
return $entity_tags;
}
}
|