1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560 |
- <?php
- abstract class ElggEntity extends \ElggData implements
- Notable, // Calendar interface (deprecated 1.9)
- Locatable, // Geocoding interface
- Importable // Allow import of data (deprecated 1.9)
- {
-
- protected $url_override;
-
- protected $icon_override;
-
- protected $temp_metadata = array();
-
- protected $temp_annotations = array();
-
- protected $temp_private_settings = array();
-
- protected $volatile = array();
-
-
- protected $tables_split;
-
-
- protected $tables_loaded;
-
-
- protected function initializeAttributes() {
- parent::initializeAttributes();
- $this->attributes['guid'] = null;
- $this->attributes['type'] = null;
- $this->attributes['subtype'] = null;
- $this->attributes['owner_guid'] = _elgg_services()->session->getLoggedInUserGuid();
- $this->attributes['container_guid'] = _elgg_services()->session->getLoggedInUserGuid();
- $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";
-
-
- $this->tables_split = 1;
- $this->tables_loaded = 0;
- }
-
- public function __clone() {
- $orig_entity = get_entity($this->guid);
- if (!$orig_entity) {
- _elgg_services()->logger->error("Failed to clone entity with GUID $this->guid");
- return;
- }
- $metadata_array = elgg_get_metadata(array(
- 'guid' => $this->guid,
- 'limit' => 0
- ));
- $this->attributes['guid'] = "";
- $this->attributes['subtype'] = $orig_entity->getSubtype();
-
-
- if (is_array($metadata_array)) {
-
- $metadata_names = array();
- foreach ($metadata_array as $metadata) {
- $metadata_names[] = $metadata['name'];
- }
-
- $metadata_names = array_unique($metadata_names);
-
- foreach ($metadata_names as $name) {
- $this->__set($name, $orig_entity->$name);
- }
- }
- }
-
- public function __set($name, $value) {
- if ($this->$name === $value) {
-
- return;
- }
- if (array_key_exists($name, $this->attributes)) {
-
- switch ($name) {
- case 'guid':
- case 'time_updated':
- case 'last_action':
- return;
- break;
- case 'access_id':
- case 'owner_guid':
- case 'container_guid':
- if ($value !== null) {
- $this->attributes[$name] = (int)$value;
- } else {
- $this->attributes[$name] = null;
- }
- break;
- default:
- $this->attributes[$name] = $value;
- break;
- }
- } else {
- $this->setMetadata($name, $value);
- }
- }
-
- public function set($name, $value) {
- elgg_deprecated_notice("Use -> instead of set()", 1.9);
- $this->__set($name, $value);
- return true;
- }
-
- public function __get($name) {
- if (array_key_exists($name, $this->attributes)) {
- if ($name === 'subtype' && $this->attributes['guid']) {
-
- elgg_deprecated_notice("Use getSubtype()", 1.9);
- }
- return $this->attributes[$name];
- }
- return $this->getMetadata($name);
- }
-
- public function get($name) {
- elgg_deprecated_notice("Use -> instead of get()", 1.9);
- return $this->__get($name);
- }
-
- abstract public function getDisplayName();
-
- abstract public function setDisplayName($displayName);
-
- public function getMetadata($name) {
- $guid = $this->getGUID();
- if (!$guid) {
- if (isset($this->temp_metadata[$name])) {
-
- if (count($this->temp_metadata[$name]) == 1) {
- return $this->temp_metadata[$name][0];
- } else {
- return $this->temp_metadata[$name];
- }
- } else {
- return null;
- }
- }
-
-
- $cache = _elgg_services()->metadataCache;
- if ($cache->isLoaded($guid)) {
- return $cache->getSingle($guid, $name);
- } else {
- $cache->populateFromEntities(array($guid));
-
- if ($cache->isLoaded($guid)) {
- return $cache->getSingle($guid, $name);
- }
- }
- $md = elgg_get_metadata(array(
- 'guid' => $guid,
- 'metadata_name' => $name,
- 'limit' => 0,
- 'distinct' => false,
- ));
- $value = null;
- if ($md && !is_array($md)) {
- $value = $md->value;
- } elseif (count($md) == 1) {
- $value = $md[0]->value;
- } else if ($md && is_array($md)) {
- $value = metadata_array_to_values($md);
- }
- return $value;
- }
-
- public function __unset($name) {
- if (array_key_exists($name, $this->attributes)) {
- $this->attributes[$name] = "";
- } else {
- $this->deleteMetadata($name);
- }
- }
-
- public function setMetadata($name, $value, $value_type = '', $multiple = false, $owner_guid = 0, $access_id = null) {
-
-
- if (is_array($value)) {
- $value = array_values($value);
- } else {
- $value = array($value);
- }
-
- if ($this->guid) {
-
- if (!$multiple) {
- $options = array(
- 'guid' => $this->getGUID(),
- 'metadata_name' => $name,
- 'limit' => 0
- );
-
-
-
-
-
- $ia = elgg_set_ignore_access(true);
- if (false === elgg_delete_metadata($options)) {
- return false;
- }
- elgg_set_ignore_access($ia);
- }
- $owner_guid = (int)$owner_guid;
- $access_id = ($access_id === null) ? $this->getAccessId() : (int)$access_id;
- $owner_guid = $owner_guid ? $owner_guid : $this->getOwnerGUID();
-
- $result = true;
- foreach ($value as $value_tmp) {
-
- $md_id = create_metadata($this->getGUID(), $name, $value_tmp, $value_type,
- $owner_guid, $access_id, true);
- if (!$md_id) {
- return false;
- }
- }
- return $result;
- } else {
-
-
-
- if ($owner_guid != 0 || $access_id !== null) {
- $msg = "owner guid and access id cannot be used in \ElggEntity::setMetadata() until entity is saved.";
- throw new \InvalidArgumentException($msg);
- }
-
- if (!$multiple || !isset($this->temp_metadata[$name])) {
- $this->temp_metadata[$name] = array();
- }
-
- $this->temp_metadata[$name] = array_merge($this->temp_metadata[$name], $value);
- return true;
- }
- }
-
- public function deleteMetadata($name = null) {
- if (!$this->guid) {
- return false;
- }
- $options = array(
- 'guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['metadata_name'] = $name;
- }
- return elgg_delete_metadata($options);
- }
-
- public function deleteOwnedMetadata($name = null) {
-
-
- $ia = elgg_set_ignore_access(true);
- $options = array(
- 'metadata_owner_guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['metadata_name'] = $name;
- }
- $r = elgg_delete_metadata($options);
- elgg_set_ignore_access($ia);
- return $r;
- }
-
- public function clearMetadata($name = '') {
- elgg_deprecated_notice('\ElggEntity->clearMetadata() is deprecated by ->deleteMetadata()', 1.8);
- return $this->deleteMetadata($name);
- }
-
- public function disableMetadata($name = '') {
- $options = array(
- 'guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['metadata_name'] = $name;
- }
- return elgg_disable_metadata($options);
- }
-
- public function enableMetadata($name = '') {
- $options = array(
- 'guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['metadata_name'] = $name;
- }
- return elgg_enable_metadata($options);
- }
-
- 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;
- }
- }
-
- public function setVolatileData($name, $value) {
- if (!is_array($this->volatile)) {
- $this->volatile = array();
- }
- $this->volatile[$name] = $value;
- }
-
- public function deleteRelationships($relationship = null) {
- $relationship = (string)$relationship;
- $result = remove_entity_relationships($this->getGUID(), $relationship);
- return $result && remove_entity_relationships($this->getGUID(), $relationship, true);
- }
-
- public function clearRelationships() {
- elgg_deprecated_notice('\ElggEntity->clearRelationships() is deprecated by ->deleteRelationships()', 1.8);
- return $this->deleteRelationships();
- }
-
- public function addRelationship($guid_two, $relationship) {
- return add_entity_relationship($this->getGUID(), $relationship, $guid_two);
- }
-
- public function removeRelationship($guid_two, $relationship) {
- return remove_entity_relationship($this->getGUID(), $relationship, $guid_two);
- }
-
- public 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;
- }
- }
-
- public 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;
- }
-
- public function removePrivateSetting($name) {
- return remove_private_setting($this->getGUID(), $name);
- }
-
- public function deleteAnnotations($name = null) {
- $options = array(
- 'guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['annotation_name'] = $name;
- }
- return elgg_delete_annotations($options);
- }
-
- public function deleteOwnedAnnotations($name = null) {
-
-
- $ia = elgg_set_ignore_access(true);
- $options = array(
- 'annotation_owner_guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['annotation_name'] = $name;
- }
- $r = elgg_delete_annotations($options);
- elgg_set_ignore_access($ia);
- return $r;
- }
-
- public function disableAnnotations($name = '') {
- $options = array(
- 'guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['annotation_name'] = $name;
- }
- return elgg_disable_annotations($options);
- }
-
- public function enableAnnotations($name = '') {
- $options = array(
- 'guid' => $this->guid,
- 'limit' => 0
- );
- if ($name) {
- $options['annotation_name'] = $name;
- }
- return elgg_enable_annotations($options);
- }
-
- private function getAnnotationCalculation($name, $calculation) {
- $options = array(
- 'guid' => $this->getGUID(),
- 'distinct' => false,
- 'annotation_name' => $name,
- 'annotation_calculation' => $calculation
- );
- return elgg_get_annotations($options);
- }
-
- public function annotate($name, $value, $access_id = ACCESS_PRIVATE, $owner_guid = 0, $vartype = "") {
- if ((int) $this->guid > 0) {
- return create_annotation($this->getGUID(), $name, $value, $vartype, $owner_guid, $access_id);
- } else {
- $this->temp_annotations[$name] = $value;
- }
- return true;
- }
-
- public function getAnnotations($options = array(), $limit = 50, $offset = 0, $order = "asc") {
- if (!is_array($options)) {
- elgg_deprecated_notice("\ElggEntity::getAnnotations() takes an array of options.", 1.9);
- }
- if ((int) ($this->guid) > 0) {
- if (!is_array($options)) {
- $options = array(
- 'guid' => $this->guid,
- 'annotation_name' => $options,
- 'limit' => $limit,
- 'offset' => $offset,
- );
- if ($order != 'asc') {
- $options['reverse_order_by'] = true;
- }
- } else {
- $options['guid'] = $this->guid;
- }
- return elgg_get_annotations($options);
- } else {
- if (!is_array($options)) {
- $name = $options;
- } else {
- $name = elgg_extract('annotation_name', $options, '');
- }
- if (isset($this->temp_annotations[$name])) {
- return array($this->temp_annotations[$name]);
- }
- }
- return array();
- }
-
- public function clearAnnotations($name = "") {
- elgg_deprecated_notice('\ElggEntity->clearAnnotations() is deprecated by ->deleteAnnotations()', 1.8);
- return $this->deleteAnnotations($name);
- }
-
- public function countAnnotations($name = "") {
- return $this->getAnnotationCalculation($name, 'count');
- }
-
- public function getAnnotationsAvg($name) {
- return $this->getAnnotationCalculation($name, 'avg');
- }
-
- public function getAnnotationsSum($name) {
- return $this->getAnnotationCalculation($name, 'sum');
- }
-
- public function getAnnotationsMin($name) {
- return $this->getAnnotationCalculation($name, 'min');
- }
-
- public function getAnnotationsMax($name) {
- return $this->getAnnotationCalculation($name, 'max');
- }
-
- public function countComments() {
- $params = array('entity' => $this);
- $num = _elgg_services()->hooks->trigger('comments:count', $this->getType(), $params);
- if (is_int($num)) {
- return $num;
- } else {
- return elgg_get_entities(array(
- 'type' => 'object',
- 'subtype' => 'comment',
- 'container_guid' => $this->getGUID(),
- 'count' => true,
- 'distinct' => false,
- ));
- }
- }
-
- public function getEntitiesFromRelationship($options = array(), $inverse = false, $limit = 50, $offset = 0) {
- if (is_array($options)) {
- $options['relationship_guid'] = $this->getGUID();
- return elgg_get_entities_from_relationship($options);
- } else {
- elgg_deprecated_notice("\ElggEntity::getEntitiesFromRelationship takes an options array", 1.9);
- return elgg_get_entities_from_relationship(array(
- 'relationship' => $options,
- 'relationship_guid' => $this->getGUID(),
- 'inverse_relationship' => $inverse,
- 'limit' => $limit,
- 'offset' => $offset
- ));
- }
- }
-
- public 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
- ));
- }
-
- public function canEdit($user_guid = 0) {
- $user_guid = (int)$user_guid;
- $user = get_entity($user_guid);
- if (!$user) {
- $user = _elgg_services()->session->getLoggedInUser();
- }
- $return = false;
-
- if ($user) {
- if ($this->getOwnerGUID() == $user->getGUID()) {
- $return = true;
- }
-
- if ($this->getContainerGUID() == $user->getGUID()) {
- $return = true;
- }
-
- if ($this->getGUID() == $user->getGUID()) {
- $return = true;
- }
- $container = $this->getContainerEntity();
- if ($container && $container->canEdit($user->getGUID())) {
- $return = true;
- }
- }
- $params = array('entity' => $this, 'user' => $user);
- return _elgg_services()->hooks->trigger('permissions_check', $this->type, $params, $return);
- }
-
- public function canDelete($user_guid = 0) {
- $user_guid = (int) $user_guid;
- if (!$user_guid) {
- $user_guid = _elgg_services()->session->getLoggedInUserGuid();
- }
-
- $ia = elgg_set_ignore_access(true);
- $show_hidden = access_show_hidden_entities(true);
-
- $user = _elgg_services()->entityTable->get($user_guid, 'user');
-
- elgg_set_ignore_access($ia);
- access_show_hidden_entities($show_hidden);
-
- if ($user_guid & !$user) {
-
- $message = _elgg_services()->translator->translate('entity:can_delete:invaliduser', array($user_guid));
- _elgg_services()->logger->warning($message);
-
- return false;
- }
-
- $return = $this->canEdit($user_guid);
- $params = array('entity' => $this, 'user' => $user);
- return _elgg_services()->hooks->trigger('permissions_check:delete', $this->type, $params, $return);
- }
-
- public function canEditMetadata($metadata = null, $user_guid = 0) {
- if (!$this->guid) {
-
- return false;
- }
- if ($user_guid) {
- $user = get_user($user_guid);
- if (!$user) {
- return false;
- }
- } else {
- $user = _elgg_services()->session->getLoggedInUser();
- $user_guid = $user->guid;
- }
- $return = null;
-
- if ($metadata && ($metadata->owner_guid == 0 || $metadata->owner_guid == $user_guid)) {
- $return = true;
- }
- if (is_null($return)) {
- $return = $this->canEdit($user_guid);
- }
-
- $params = array('entity' => $this, 'user' => $user, 'metadata' => $metadata);
- return _elgg_services()->hooks->trigger('permissions_check:metadata', $this->type, $params, $return);
- }
-
- public function canWriteToContainer($user_guid = 0, $type = 'all', $subtype = 'all') {
- return can_write_to_container($user_guid, $this->guid, $type, $subtype);
- }
-
- public function canComment($user_guid = 0) {
- if ($user_guid == 0) {
- $user_guid = _elgg_services()->session->getLoggedInUserGuid();
- }
- $user = get_entity($user_guid);
-
-
- $params = array('entity' => $this, 'user' => $user);
- return _elgg_services()->hooks->trigger('permissions_check:comment', $this->type, $params, null);
- }
-
- public function canAnnotate($user_guid = 0, $annotation_name = '') {
- if ($user_guid == 0) {
- $user_guid = _elgg_services()->session->getLoggedInUserGuid();
- }
- $user = get_entity($user_guid);
- $return = true;
- if (!$user) {
- $return = false;
- }
- $hooks = _elgg_services()->hooks;
- $params = array(
- 'entity' => $this,
- 'user' => $user,
- 'annotation_name' => $annotation_name,
- );
- if ($annotation_name !== '') {
- $return = $hooks->trigger("permissions_check:annotate:$annotation_name", $this->type, $params, $return);
- }
- $return = $hooks->trigger('permissions_check:annotate', $this->type, $params, $return);
- return $return;
- }
-
- public function getAccessID() {
- return $this->access_id;
- }
-
- public function getGUID() {
- return $this->guid;
- }
-
- public function getType() {
- return $this->type;
- }
-
- public function getSubtype() {
-
- if ($this->attributes['guid']) {
- return get_subtype_from_id($this->attributes['subtype']);
- }
- return $this->attributes['subtype'];
- }
-
- public function getOwnerGUID() {
- return (int)$this->owner_guid;
- }
-
- public function getOwner() {
- elgg_deprecated_notice("\ElggEntity::getOwner deprecated for \ElggEntity::getOwnerGUID", 1.8);
- return $this->getOwnerGUID();
- }
-
- public function getOwnerEntity() {
- return get_entity($this->owner_guid);
- }
-
- public function setContainerGUID($container_guid) {
- return $this->container_guid = (int)$container_guid;
- }
-
- public function setContainer($container_guid) {
- elgg_deprecated_notice("\ElggObject::setContainer deprecated for \ElggEntity::setContainerGUID", 1.8);
- return $this->setContainerGUID('container_guid', $container_guid);
- }
-
- public function getContainerGUID() {
- return (int)$this->container_guid;
- }
-
- public function getContainer() {
- elgg_deprecated_notice("\ElggObject::getContainer deprecated for \ElggEntity::getContainerGUID", 1.8);
- return $this->getContainerGUID();
- }
-
- public function getContainerEntity() {
- return get_entity($this->getContainerGUID());
- }
-
- public function getTimeUpdated() {
- return $this->time_updated;
- }
-
- public function getURL() {
-
- $url = "";
-
- if ($this->guid) {
- global $CONFIG;
- if (isset($CONFIG->entity_url_handler[$this->getType()][$this->getSubtype()])) {
- $function = $CONFIG->entity_url_handler[$this->getType()][$this->getSubtype()];
- if (is_callable($function)) {
- $url = call_user_func($function, $this);
- }
- } elseif (isset($CONFIG->entity_url_handler[$this->getType()]['all'])) {
- $function = $CONFIG->entity_url_handler[$this->getType()]['all'];
- if (is_callable($function)) {
- $url = call_user_func($function, $this);
- }
- } elseif (isset($CONFIG->entity_url_handler['all']['all'])) {
- $function = $CONFIG->entity_url_handler['all']['all'];
- if (is_callable($function)) {
- $url = call_user_func($function, $this);
- }
- }
- if ($url) {
- $url = elgg_normalize_url($url);
- }
- }
- $type = $this->getType();
- $params = array('entity' => $this);
- $url = _elgg_services()->hooks->trigger('entity:url', $type, $params, $url);
-
- if (!empty($this->url_override)) {
- $url = $this->url_override;
- }
- return elgg_normalize_url($url);
- }
-
- public function setURL($url) {
- elgg_deprecated_notice('\ElggEntity::setURL() has been replaced by the "entity:url" plugin hook', 1.9);
- $this->url_override = $url;
- return $url;
- }
-
- public function getIconURL($params = array()) {
- if (is_array($params)) {
- $size = elgg_extract('size', $params, 'medium');
- } else {
- $size = is_string($params) ? $params : 'medium';
- $params = array();
- }
- $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];
- }
- $params['entity'] = $this;
- $params['size'] = $size;
- $type = $this->getType();
- $url = _elgg_services()->hooks->trigger('entity:icon:url', $type, $params, null);
- if ($url == null) {
- $url = "_graphics/icons/default/$size.png";
- }
- return elgg_normalize_url($url);
- }
-
- public function getIcon($size = 'medium') {
- elgg_deprecated_notice("getIcon() deprecated by getIconURL()", 1.8);
- return $this->getIconURL($size);
- }
-
- 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;
- }
-
- public function addToSite($site) {
- if (!elgg_instanceof($site, 'site')) {
- return false;
- }
- return $site->addEntity($this);
- }
-
- public function removeFromSite($site) {
- if (!elgg_instanceof($site, 'site')) {
- return false;
- }
- return $site->removeEntity($this);
- }
-
- public function getSites($options = array()) {
- $options['relationship'] = 'member_of_site';
- $options['relationship_guid'] = $this->guid;
- $options['inverse_relationship'] = false;
- if (!isset($options['site_guid']) || !isset($options['site_guids'])) {
- $options['site_guids'] = ELGG_ENTITIES_ANY_VALUE;
- }
- return elgg_get_entities_from_relationship($options);
- }
-
- public function isFullyLoaded() {
- return ! ($this->tables_loaded < $this->tables_split);
- }
-
- public function save() {
- $guid = $this->getGUID();
- if ($guid > 0) {
- return $this->update();
- } else {
- $guid = $this->create();
- if ($guid) {
- if (_elgg_services()->events->trigger('create', $this->type, $this)) {
- return $guid;
- } else {
-
- $ia = elgg_set_ignore_access(true);
- $this->delete();
- elgg_set_ignore_access($ia);
- }
- }
- }
-
- return false;
- }
-
-
- protected function create() {
- global $CONFIG;
-
- $type = $this->getDatabase()->sanitizeString($this->attributes['type']);
- if ($type == "") {
- throw new \InvalidParameterException("Entity type must be set.");
- }
-
- $subtype = $this->attributes['subtype'];
- $subtype_id = add_subtype($type, $subtype);
- $owner_guid = (int)$this->attributes['owner_guid'];
- $access_id = (int)$this->attributes['access_id'];
- $now = (string)time();
- $time_created = isset($this->attributes['time_created']) ? (int)$this->attributes['time_created'] : $now;
- $site_guid = $this->attributes['site_guid'];
- if ($site_guid == 0) {
- $site_guid = $CONFIG->site_guid;
- }
- $site_guid = (int)$site_guid;
-
- $container_guid = $this->attributes['container_guid'];
- if ($container_guid == 0) {
- $container_guid = $owner_guid;
- $this->attributes['container_guid'] = $container_guid;
- }
- $container_guid = (int)$container_guid;
- if ($access_id == ACCESS_DEFAULT) {
- throw new \InvalidParameterException('ACCESS_DEFAULT is not a valid access level. See its documentation in elgglib.h');
- }
- $owner = $this->getOwnerEntity();
- if ($owner && !$owner->canWriteToContainer(0, $type, $subtype)) {
- return false;
- }
-
- if ($owner_guid != $container_guid) {
- $container = $this->getContainerEntity();
- if ($container && !$container->canWriteToContainer(0, $type, $subtype)) {
- return false;
- }
- }
-
- $result = $this->getDatabase()->insertData("INSERT into {$CONFIG->dbprefix}entities
- (type, subtype, owner_guid, site_guid, container_guid,
- access_id, time_created, time_updated, last_action)
- values
- ('$type', $subtype_id, $owner_guid, $site_guid, $container_guid,
- $access_id, $time_created, $now, $now)");
- if (!$result) {
- throw new \IOException("Unable to save new object's base entity information!");
- }
-
-
- $this->attributes['subtype'] = (int)$subtype_id;
- $this->attributes['guid'] = (int)$result;
- $this->attributes['time_created'] = (int)$time_created;
- $this->attributes['time_updated'] = (int)$now;
- $this->attributes['last_action'] = (int)$now;
- $this->attributes['site_guid'] = (int)$site_guid;
- $this->attributes['container_guid'] = (int)$container_guid;
-
- if (sizeof($this->temp_metadata) > 0) {
- foreach ($this->temp_metadata as $name => $value) {
- $this->$name = $value;
- }
-
- $this->temp_metadata = array();
- }
-
- if (sizeof($this->temp_annotations) > 0) {
- foreach ($this->temp_annotations as $name => $value) {
- $this->annotate($name, $value);
- }
-
- $this->temp_annotations = array();
- }
-
- if (sizeof($this->temp_private_settings) > 0) {
- foreach ($this->temp_private_settings as $name => $value) {
- $this->setPrivateSetting($name, $value);
- }
-
- $this->temp_private_settings = array();
- }
- _elgg_cache_entity($this);
-
- return $result;
- }
-
- protected function update() {
- global $CONFIG;
-
-
- _elgg_disable_caching_for_entity($this->guid);
- $persisted_entity = get_entity($this->guid);
- if (!$persisted_entity) {
-
-
- return false;
- }
- $allow_edit = $persisted_entity->canEdit();
- unset($persisted_entity);
- if ($allow_edit) {
-
- $allow_edit = _elgg_services()->events->trigger('update', $this->type, $this);
- }
- _elgg_enable_caching_for_entity($this->guid);
- if (!$allow_edit) {
- return false;
- }
-
- $guid = (int)$this->guid;
- $owner_guid = (int)$this->owner_guid;
- $access_id = (int)$this->access_id;
- $container_guid = (int)$this->container_guid;
- $time_created = (int)$this->time_created;
- $time = time();
- if ($access_id == ACCESS_DEFAULT) {
- throw new \InvalidParameterException('ACCESS_DEFAULT is not a valid access level. See its documentation in elgglib.php');
- }
-
- $ret = $this->getDatabase()->updateData("UPDATE {$CONFIG->dbprefix}entities
- set owner_guid='$owner_guid', access_id='$access_id',
- container_guid='$container_guid', time_created='$time_created',
- time_updated='$time' WHERE guid=$guid");
-
- elgg_trigger_after_event('update', $this->type, $this);
-
- if ($this instanceof \ElggObject) {
- update_river_access_by_object($guid, $access_id);
- }
-
- static $newentity_cache;
- if ((!$newentity_cache) && (is_memcache_available())) {
- $newentity_cache = new \ElggMemcache('new_entity_cache');
- }
- if ($newentity_cache) {
- $newentity_cache->delete($guid);
- }
- if ($ret !== false) {
- $this->attributes['time_updated'] = $time;
- }
- _elgg_cache_entity($this);
-
- return $ret !== false;
- }
-
- protected function load($guid) {
- if ($guid instanceof \stdClass) {
- $row = $guid;
- } else {
- $row = get_entity_as_row($guid);
- }
- if ($row) {
-
- if (!is_array($this->attributes)) {
- $this->attributes = array();
- }
-
- $objarray = (array) $row;
- foreach ($objarray as $key => $value) {
- $this->attributes[$key] = $value;
- }
-
- if (!$this->isFullyLoaded()) {
- $this->tables_loaded++;
- }
-
- $this->attributes['guid'] = (int)$this->attributes['guid'];
-
- $this->attributes['subtype'] = (int)$this->attributes['subtype'];
-
- if ($this->attributes['guid']) {
- _elgg_cache_entity($this);
- }
- return true;
- }
- return false;
- }
-
- protected function loadAdditionalSelectValues(array $data) {
- foreach ($data as $name => $value) {
- $this->setVolatileData("select:$name", $value);
- }
- }
-
-
- public function refresh(\stdClass $row) {
- if ($row instanceof \stdClass) {
- return $this->load($row);
- }
- return false;
- }
-
- public function disable($reason = "", $recursive = true) {
- if (!$this->guid) {
- return false;
- }
-
- if (!_elgg_services()->events->trigger('disable', $this->type, $this)) {
- return false;
- }
-
- if (!$this->canEdit()) {
- return false;
- }
- _elgg_invalidate_cache_for_entity($this->guid);
-
- if ($reason) {
- $this->disable_reason = $reason;
- }
- global $CONFIG;
- $guid = (int)$this->guid;
-
- if ($recursive) {
- $hidden = access_get_show_hidden_status();
- access_show_hidden_entities(true);
- $ia = elgg_set_ignore_access(true);
-
- $sub_entities = $this->getDatabase()->getData("SELECT * FROM {$CONFIG->dbprefix}entities
- WHERE (
- container_guid = $guid
- OR owner_guid = $guid
- OR site_guid = $guid
- ) AND enabled='yes'", 'entity_row_to_elggstar');
- if ($sub_entities) {
- foreach ($sub_entities as $e) {
- add_entity_relationship($e->guid, 'disabled_with', $this->guid);
- $e->disable($reason);
- }
- }
-
- access_show_hidden_entities($hidden);
- elgg_set_ignore_access($ia);
- }
- $this->disableMetadata();
- $this->disableAnnotations();
- $res = $this->getDatabase()->updateData("UPDATE {$CONFIG->dbprefix}entities
- SET enabled = 'no'
- WHERE guid = $guid");
- if ($res) {
- $this->attributes['enabled'] = 'no';
- _elgg_services()->events->trigger('disable:after', $this->type, $this);
- }
- return $res;
- }
-
- public function enable($recursive = true) {
- $guid = (int)$this->guid;
- if (!$guid) {
- return false;
- }
-
- if (!_elgg_services()->events->trigger('enable', $this->type, $this)) {
- return false;
- }
-
- if (!$this->canEdit()) {
- return false;
- }
-
- global $CONFIG;
-
-
- $old_access_status = access_get_show_hidden_status();
- access_show_hidden_entities(true);
-
- $result = $this->getDatabase()->updateData("UPDATE {$CONFIG->dbprefix}entities
- SET enabled = 'yes'
- WHERE guid = $guid");
- $this->deleteMetadata('disable_reason');
- $this->enableMetadata();
- $this->enableAnnotations();
- if ($recursive) {
- $disabled_with_it = elgg_get_entities_from_relationship(array(
- 'relationship' => 'disabled_with',
- 'relationship_guid' => $guid,
- 'inverse_relationship' => true,
- 'limit' => 0,
- ));
- foreach ($disabled_with_it as $e) {
- $e->enable();
- remove_entity_relationship($e->guid, 'disabled_with', $guid);
- }
- }
-
- access_show_hidden_entities($old_access_status);
-
- if ($result) {
- $this->attributes['enabled'] = 'yes';
- _elgg_services()->events->trigger('enable:after', $this->type, $this);
- }
- return $result;
- }
-
- public function isEnabled() {
- return $this->enabled == 'yes';
- }
-
- public function delete($recursive = true) {
- global $CONFIG;
- $guid = $this->guid;
- if (!$guid) {
- return false;
- }
-
-
-
-
- if (!$this->canDelete()) {
- return false;
- }
-
-
- if (!_elgg_services()->events->trigger('delete', $this->type, $this)) {
- return false;
- }
-
- _elgg_invalidate_cache_for_entity($guid);
-
-
- static $newentity_cache;
- if ((!$newentity_cache) && (is_memcache_available())) {
- $newentity_cache = new \ElggMemcache('new_entity_cache');
- }
- if ($newentity_cache) {
- $newentity_cache->delete($guid);
- }
-
- if ($recursive) {
-
- $entity_disable_override = access_get_show_hidden_status();
- access_show_hidden_entities(true);
- $ia = elgg_set_ignore_access(true);
-
-
-
- $options = array(
- 'wheres' => array(
- "((container_guid = $guid OR owner_guid = $guid OR site_guid = $guid)"
- . " AND guid != $guid)"
- ),
- 'limit' => 0
- );
- $batch = new \ElggBatch('elgg_get_entities', $options);
- $batch->setIncrementOffset(false);
- foreach ($batch as $e) {
- $e->delete(true);
- }
- access_show_hidden_entities($entity_disable_override);
- elgg_set_ignore_access($ia);
- }
- $entity_disable_override = access_get_show_hidden_status();
- access_show_hidden_entities(true);
- $ia = elgg_set_ignore_access(true);
-
-
- $this->deleteMetadata();
- $this->deleteOwnedMetadata();
- $this->deleteAnnotations();
- $this->deleteOwnedAnnotations();
- $this->deleteRelationships();
- $this->deleteAccessCollectionMemberships();
- $this->deleteOwnedAccessCollections();
- access_show_hidden_entities($entity_disable_override);
- elgg_set_ignore_access($ia);
- elgg_delete_river(array('subject_guid' => $guid));
- elgg_delete_river(array('object_guid' => $guid));
- elgg_delete_river(array('target_guid' => $guid));
- remove_all_private_settings($guid);
- $res = $this->getDatabase()->deleteData("DELETE FROM {$CONFIG->dbprefix}entities WHERE guid = $guid");
- if ($res) {
- $sub_table = "";
-
- switch ($this->type) {
- case 'object' :
- $sub_table = $CONFIG->dbprefix . 'objects_entity';
- break;
- case 'user' :
- $sub_table = $CONFIG->dbprefix . 'users_entity';
- break;
- case 'group' :
- $sub_table = $CONFIG->dbprefix . 'groups_entity';
- break;
- case 'site' :
- $sub_table = $CONFIG->dbprefix . 'sites_entity';
- break;
- }
- if ($sub_table) {
- $this->getDatabase()->deleteData("DELETE FROM $sub_table WHERE guid = $guid");
- }
- }
-
- _elgg_clear_entity_files($this);
- return (bool)$res;
- }
-
- public function toObject() {
- $object = $this->prepareObject(new \stdClass());
- $params = array('entity' => $this);
- $object = _elgg_services()->hooks->trigger('to:object', 'entity', $params, $object);
- return $object;
- }
-
- protected function prepareObject($object) {
- $object->guid = $this->guid;
- $object->type = $this->getType();
- $object->subtype = $this->getSubtype();
- $object->owner_guid = $this->getOwnerGUID();
- $object->container_guid = $this->getContainerGUID();
- $object->site_guid = (int)$this->site_guid;
- $object->time_created = date('c', $this->getTimeCreated());
- $object->time_updated = date('c', $this->getTimeUpdated());
- $object->url = $this->getURL();
- $object->read_access = (int)$this->access_id;
- return $object;
- }
-
-
- public function getLocation() {
- return $this->location;
- }
-
- public function setLocation($location) {
- $this->location = $location;
- }
-
- public function setLatLong($lat, $long) {
- $this->{"geo:lat"} = $lat;
- $this->{"geo:long"} = $long;
- }
-
- public function getLatitude() {
- return (float)$this->{"geo:lat"};
- }
-
- public function getLongitude() {
- return (float)$this->{"geo:long"};
- }
-
-
- public function setCalendarTimeAndDuration($hour = null, $minute = null, $second = null,
- $day = null, $month = null, $year = null, $duration = null) {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
- $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;
- }
-
- public function getCalendarStartTime() {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
- return (int)$this->calendar_start;
- }
-
- public function getCalendarEndTime() {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
- return (int)$this->calendar_end;
- }
-
-
- public function getExportableValues() {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated by toObject()', 1.9);
- return array(
- 'guid',
- 'type',
- 'subtype',
- 'time_created',
- 'time_updated',
- 'container_guid',
- 'owner_guid',
- 'site_guid'
- );
- }
-
- public function export() {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
- $tmp = array();
-
- $uuid = guid_to_uuid($this->getGUID());
-
- $odd = new ODDEntity(
- $uuid,
- $this->attributes['type'],
- get_subtype_from_id($this->attributes['subtype'])
- );
- $tmp[] = $odd;
- $exportable_values = $this->getExportableValues();
-
- foreach ($this->attributes as $k => $v) {
- $meta = null;
- if (in_array($k, $exportable_values)) {
- switch ($k) {
- case 'guid':
- case 'type':
- case 'subtype':
- break;
- case 'time_created':
- $odd->setAttribute('published', date("r", $v));
- break;
- case 'site_guid':
- $k = 'site_uuid';
- $v = guid_to_uuid($v);
- $meta = new ODDMetaData($uuid . "attr/$k/", $uuid, $k, $v);
- break;
- case 'container_guid':
- $k = 'container_uuid';
- $v = guid_to_uuid($v);
- $meta = new ODDMetaData($uuid . "attr/$k/", $uuid, $k, $v);
- break;
- case 'owner_guid':
- $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);
- }
-
- if ($meta) {
- $meta->setAttribute('published', date("r", $this->time_created));
- $tmp[] = $meta;
- }
- }
- }
-
-
- elgg_set_viewtype('default');
- $view = elgg_view_entity($this, array('full_view' => true));
- elgg_set_viewtype();
- $tmp[] = new ODDMetaData($uuid . "volatile/renderedentity/", $uuid,
- 'renderedentity', $view, 'volatile');
- return $tmp;
- }
-
-
- public function import(ODD $data) {
- elgg_deprecated_notice(__METHOD__ . ' has been deprecated', 1.9);
- if (!($data instanceof ODDEntity)) {
- throw new \InvalidParameterException("import() passed an unexpected ODD class");
- }
-
- $this->attributes['type'] = $data->getAttribute('class');
- $this->attributes['subtype'] = $data->getAttribute('subclass');
-
- $this->attributes['owner_guid'] = _elgg_services()->session->getLoggedInUserGuid();
-
- $this->attributes['time_created'] = strtotime($data->getAttribute('published'));
- $this->attributes['time_updated'] = time();
- return true;
- }
-
-
- public function getSystemLogID() {
- return $this->getGUID();
- }
-
- public function getObjectFromID($id) {
- return get_entity($id);
- }
-
- public function getTags($tag_names = null) {
- 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 (is_array($tags)) {
- $entity_tags = array_merge($entity_tags, $tags);
- } else {
- $entity_tags[] = $tags;
- }
- }
- }
- return $entity_tags;
- }
-
-
- public function deleteAccessCollectionMemberships() {
-
- if (!$this->guid) {
- return false;
- }
-
- if ($this->type !== 'user') {
- return true;
- }
-
- $ac = _elgg_services()->accessCollections;
-
- $collections = $ac->getCollectionsByMember($this->guid);
- if (empty($collections)) {
- return true;
- }
-
- $result = true;
- foreach ($collections as $collection) {
- $result = $result & $ac->removeUser($this->guid, $collection->id);
- }
-
- return $result;
- }
-
-
- public function deleteOwnedAccessCollections() {
-
- if (!$this->guid) {
- return false;
- }
-
- $ac = _elgg_services()->accessCollections;
-
- $collections = $ac->getEntityCollections($this->guid);
- if (empty($collections)) {
- return true;
- }
-
- $result = true;
- foreach ($collections as $collection) {
- $result = $result & $ac->delete($collection->id);
- }
-
- return $result;
- }
- }
|