コード例 #1
0
/**
 * Create or update the extras table for a given object.
 * Call create_entity first.
 *
 * @param int    $guid        The guid of the entity you're creating (as obtained by create_entity)
 * @param string $title       The title of the object
 * @param string $description The object's description
 *
 * @return bool
 */
function create_object_entity($guid, $title, $description)
{
    global $CONFIG;
    $guid = (int) $guid;
    $title = sanitise_string($title);
    $description = sanitise_string($description);
    $row = get_entity_as_row($guid);
    if ($row) {
        // Core entities row exists and we have access to it
        $query = "SELECT guid from {$CONFIG->dbprefix}objects_entity where guid = {$guid}";
        if ($exists = get_data_row($query)) {
            $query = "UPDATE {$CONFIG->dbprefix}objects_entity\n\t\t\t\tset title='{$title}', description='{$description}' where guid={$guid}";
            $result = update_data($query);
            if ($result != false) {
                // Update succeeded, continue
                $entity = get_entity($guid);
                elgg_trigger_event('update', $entity->type, $entity);
                return $guid;
            }
        } else {
            // Update failed, attempt an insert.
            $query = "INSERT into {$CONFIG->dbprefix}objects_entity\n\t\t\t\t(guid, title, description) values ({$guid}, '{$title}','{$description}')";
            $result = insert_data($query);
            if ($result !== false) {
                $entity = get_entity($guid);
                if (elgg_trigger_event('create', $entity->type, $entity)) {
                    return $guid;
                } else {
                    $entity->delete();
                }
            }
        }
    }
    return false;
}
コード例 #2
0
 public function __construct($guid = null)
 {
     if ($guid && !is_object($guid)) {
         $guid = get_entity_as_row($guid);
     }
     parent::__construct($guid);
 }
コード例 #3
0
ファイル: Enqueue.php プロジェクト: coldtrick/tag_tools
 /**
  * Check if an entity_guid is valid for sending tag notifications
  *
  * @param int $entity_guid the entity GUID
  *
  * @return bool
  */
 protected static function validateEntity($entity_guid)
 {
     $entity_guid = sanitize_int($entity_guid, false);
     if (empty($entity_guid)) {
         return false;
     }
     // cache plugin
     self::cachePlugin();
     if (check_entity_relationship(self::$plugin->getGUID(), 'tag_tools:notification', $entity_guid)) {
         // already enqueued
         return false;
     }
     // can't use elgg get entity because cache is not correctly updated
     $entity_row = get_entity_as_row($entity_guid);
     if ($entity_row === false) {
         // invalid entity
         return false;
     }
     $entity_access = sanitise_int($entity_row->access_id);
     if ($entity_access === ACCESS_PRIVATE) {
         // private entity
         return false;
     }
     if (!tag_tools_is_notification_entity($entity_guid)) {
         // not supported entity type/subtype
         return false;
     }
     return true;
 }
コード例 #4
0
ファイル: events.php プロジェクト: coldtrick/tag_tools
/**
 * Listen to the creation of metadata
 *
 * @param string       $event    the name of the event
 * @param string       $type     the type of the event
 * @param ElggMetadata $metadata supplied metadata
 *
 * @return void
 */
function tag_tools_create_metadata_event_handler($event, $type, $metadata)
{
    if (empty($metadata) || !$metadata instanceof ElggMetadata) {
        return;
    }
    // is it a tag
    if ($metadata->name != 'tags') {
        return;
    }
    // get the entity for further use
    $ia = elgg_set_ignore_access(true);
    $entity_guid = $metadata->entity_guid;
    // can't use elgg get entity because cache is not correctly updated
    $entity_row = get_entity_as_row($entity_guid);
    elgg_set_ignore_access($ia);
    // shortcut for private entities
    if ($entity_row->access_id == ACCESS_PRIVATE) {
        return;
    }
    // only send notifications on creation of the entity
    $time_created_treshold = 5;
    if ($entity_row->time_created < time() - $time_created_treshold) {
        // assume it is an update
        return;
    }
    // check of the entity is allowed for notifications
    if (!tag_tools_is_notification_entity($entity_row->guid)) {
        return;
    }
    $tag = $metadata->value;
    $options = ['type' => 'user', 'annotation_name_value_pairs' => ['name' => 'follow_tag', 'value' => $tag], 'limit' => false];
    $ia = elgg_set_ignore_access(true);
    $dbprefix = elgg_get_config('dbprefix');
    $entities = new ElggBatch('elgg_get_entities_from_annotations', $options);
    foreach ($entities as $user) {
        // check if not trying to notify the owner
        if ($user->getGUID() == $entity_row->owner_guid) {
            continue;
        }
        // force a correct access bit
        elgg_set_ignore_access(false);
        // check access for the user, can't use has_access_to_entity
        // because that requires a full entity
        $access_bit = _elgg_get_access_where_sql(['user_guid' => $user->getGUID()]);
        // ignore access to get the correct next user
        elgg_set_ignore_access(true);
        // build correct query to check access
        $query = "SELECT guid FROM {$dbprefix}entities e\n\t\t\t WHERE e.guid = {$entity_guid}\n\t\t\t AND {$access_bit}";
        if (get_data($query)) {
            // regsiter shutdown function because we need the full entity
            // this is a workaround and should be reviewed in the near future
            register_shutdown_function('tag_tools_notify_user', $user->getGUID(), $entity_row->guid, $tag);
        }
    }
    elgg_set_ignore_access($ia);
}
コード例 #5
0
ファイル: Drawmap.php プロジェクト: nlybe/elgg-sharemaps
 public function __construct($guid = null)
 {
     if ($guid && !is_object($guid)) {
         // Loading entities via __construct(GUID) is deprecated, so we give it the entity row and the
         // attribute loader will finish the job. This is necessary due to not using a custom
         // subtype (see above).
         $guid = get_entity_as_row($guid);
     }
     parent::__construct($guid);
 }
コード例 #6
0
ファイル: Userpoint.php プロジェクト: iionly/elggx_userpoints
 /**
  * Class constructor
  *
  * @param integer  $guid The object guid
  * @param integer  $user_guid The users guid
  * @param string   $description The description (reason) for these points
  */
 public function __construct($guid = null, $user_guid = null, $description = null)
 {
     if ($guid && !is_object($guid)) {
         $guid = get_entity_as_row($guid);
     }
     parent::__construct($guid);
     if ($guid) {
         return true;
     }
     if (!($user = get_entity($user_guid))) {
         return false;
     }
     $this->attributes['owner_guid'] = $user_guid;
     $this->attributes['container_guid'] = $user_guid;
     $this->attributes['description'] = $description;
 }
コード例 #7
0
ファイル: group.php プロジェクト: nogsus/Elgg
/**
 * Create or update the entities table for a given group.
 * Call create_entity first.
 *
 * @param int    $guid        GUID
 * @param string $name        Name
 * @param string $description Description
 *
 * @return bool
 */
function create_group_entity($guid, $name, $description)
{
    global $CONFIG;
    $guid = (int) $guid;
    $name = sanitise_string($name);
    $description = sanitise_string($description);
    $row = get_entity_as_row($guid);
    if ($row) {
        // Exists and you have access to it
        $exists = get_data_row("SELECT guid from {$CONFIG->dbprefix}groups_entity WHERE guid = {$guid}");
        if ($exists) {
        } else {
        }
    }
    return false;
}
コード例 #8
0
ファイル: users.php プロジェクト: rasul/Elgg
/**
 * Create or update the entities table for a given user.
 * Call create_entity first.
 *
 * @param int    $guid     The user's GUID
 * @param string $name     The user's display name
 * @param string $username The username
 * @param string $password The password
 * @param string $salt     A salt for the password
 * @param string $email    The user's email address
 * @param string $language The user's default language
 * @param string $code     A code
 *
 * @return bool
 */
function create_user_entity($guid, $name, $username, $password, $salt, $email, $language, $code)
{
    global $CONFIG;
    $guid = (int) $guid;
    $name = sanitise_string($name);
    $username = sanitise_string($username);
    $password = sanitise_string($password);
    $salt = sanitise_string($salt);
    $email = sanitise_string($email);
    $language = sanitise_string($language);
    $code = sanitise_string($code);
    $row = get_entity_as_row($guid);
    if ($row) {
        // Exists and you have access to it
        $query = "SELECT guid from {$CONFIG->dbprefix}users_entity where guid = {$guid}";
        if ($exists = get_data_row($query)) {
            $query = "UPDATE {$CONFIG->dbprefix}users_entity\n\t\t\t\tset name='{$name}', username='******', password='******', salt='{$salt}',\n\t\t\t\temail='{$email}', language='{$language}', code='{$code}', last_action = " . time() . " where guid = {$guid}";
            $result = update_data($query);
            if ($result != false) {
                // Update succeeded, continue
                $entity = get_entity($guid);
                if (elgg_trigger_event('update', $entity->type, $entity)) {
                    return $guid;
                } else {
                    $entity->delete();
                }
            }
        } else {
            // Update failed, attempt an insert.
            $query = "INSERT into {$CONFIG->dbprefix}users_entity\n\t\t\t\t(guid, name, username, password, salt, email, language, code)\n\t\t\t\tvalues ({$guid}, '{$name}', '{$username}', '{$password}', '{$salt}', '{$email}', '{$language}', '{$code}')";
            $result = insert_data($query);
            if ($result !== false) {
                $entity = get_entity($guid);
                if (elgg_trigger_event('create', $entity->type, $entity)) {
                    return $guid;
                } else {
                    $entity->delete();
                    //delete_entity($guid);
                }
            }
        }
    }
    return false;
}
コード例 #9
0
ファイル: sites.php プロジェクト: duanhv/mdg-social
/**
 * Create or update the entities table for a given site.
 * Call create_entity first.
 *
 * @param int    $guid        Site GUID
 * @param string $name        Site name
 * @param string $description Site Description
 * @param string $url         URL of the site
 *
 * @return bool
 * @access private
 */
function create_site_entity($guid, $name, $description, $url)
{
    global $CONFIG;
    $guid = (int) $guid;
    $name = sanitise_string($name);
    $description = sanitise_string($description);
    $url = sanitise_string($url);
    $row = get_entity_as_row($guid);
    if ($row) {
        // Exists and you have access to it
        $query = "SELECT guid from {$CONFIG->dbprefix}sites_entity where guid = {$guid}";
        if ($exists = get_data_row($query)) {
            $query = "UPDATE {$CONFIG->dbprefix}sites_entity\n\t\t\t\tset name='{$name}', description='{$description}', url='{$url}' where guid={$guid}";
            $result = update_data($query);
            if ($result != false) {
                // Update succeeded, continue
                $entity = get_entity($guid);
                if (elgg_trigger_event('update', $entity->type, $entity)) {
                    return $guid;
                } else {
                    $entity->delete();
                    //delete_entity($guid);
                }
            }
        } else {
            // Update failed, attempt an insert.
            $query = "INSERT into {$CONFIG->dbprefix}sites_entity\n\t\t\t\t(guid, name, description, url) values ({$guid}, '{$name}', '{$description}', '{$url}')";
            $result = insert_data($query);
            if ($result !== false) {
                $entity = get_entity($guid);
                if (elgg_trigger_event('create', $entity->type, $entity)) {
                    return $guid;
                } else {
                    $entity->delete();
                    //delete_entity($guid);
                }
            }
        }
    }
    return false;
}
コード例 #10
0
ファイル: users.php プロジェクト: riggo/Elgg
/**
 * Create or update the entities table for a given user.
 * Call create_entity first.
 *
 * @param int    $guid     The user's GUID
 * @param string $name     The user's display name
 * @param string $username The username
 * @param string $password The password
 * @param string $salt     A salt for the password
 * @param string $email    The user's email address
 * @param string $language The user's default language
 * @param string $code     A code
 *
 * @return bool
 */
function create_user_entity($guid, $name, $username, $password, $salt, $email, $language, $code)
{
    global $CONFIG;
    $guid = (int) $guid;
    $name = sanitise_string($name);
    $username = sanitise_string($username);
    $password = sanitise_string($password);
    $salt = sanitise_string($salt);
    $email = sanitise_string($email);
    $language = sanitise_string($language);
    $code = sanitise_string($code);
    $row = get_entity_as_row($guid);
    if ($row) {
        // Exists and you have access to it
        $query = "SELECT guid from {$CONFIG->dbprefix}users_entity where guid = {$guid}";
        if ($exists = get_data_row($query)) {
        } else {
            // Exists query failed, attempt an insert.
        }
    }
    return false;
}
コード例 #11
0
ファイル: entities.php プロジェクト: gzachos/elgg_ellak
/**
 * Loads and returns an entity object from a guid.
 *
 * @param int $guid The GUID of the entity
 *
 * @return ElggEntity The correct Elgg or custom object based upon entity type and subtype
 */
function get_entity($guid)
{
    // This should not be a static local var. Notice that cache writing occurs in a completely
    // different instance outside this function.
    // @todo We need a single Memcache instance with a shared pool of namespace wrappers. This function would pull an instance from the pool.
    static $shared_cache;
    // We could also use: if (!(int) $guid) { return false },
    // but that evaluates to a false positive for $guid = true.
    // This is a bit slower, but more thorough.
    if (!is_numeric($guid) || $guid === 0 || $guid === '0') {
        return false;
    }
    // Check local cache first
    $new_entity = _elgg_retrieve_cached_entity($guid);
    if ($new_entity) {
        return $new_entity;
    }
    // Check shared memory cache, if available
    if (null === $shared_cache) {
        if (is_memcache_available()) {
            $shared_cache = new ElggMemcache('new_entity_cache');
        } else {
            $shared_cache = false;
        }
    }
    // until ACLs in memcache, DB query is required to determine access
    $entity_row = get_entity_as_row($guid);
    if (!$entity_row) {
        return false;
    }
    if ($shared_cache) {
        $cached_entity = $shared_cache->load($guid);
        // @todo store ACLs in memcache https://github.com/elgg/elgg/issues/3018#issuecomment-13662617
        if ($cached_entity) {
            // @todo use ACL and cached entity access_id to determine if user can see it
            return $cached_entity;
        }
    }
    // don't let incomplete entities cause fatal exceptions
    try {
        $new_entity = entity_row_to_elggstar($entity_row);
    } catch (IncompleteEntityException $e) {
        return false;
    }
    if ($new_entity) {
        _elgg_cache_entity($new_entity);
    }
    return $new_entity;
}
コード例 #12
0
ファイル: entities.php プロジェクト: nhunaro/Elgg
/**
 * Loads and returns an entity object from a guid.
 *
 * @param int $guid The GUID of the entity
 *
 * @return ElggEntity The correct Elgg or custom object based upon entity type and subtype
 * @link http://docs.elgg.org/DataModel/Entities
 */
function get_entity($guid)
{
    static $newentity_cache;
    $new_entity = false;
    // We could also use: if (!(int) $guid) { return FALSE },
    // but that evaluates to a false positive for $guid = TRUE.
    // This is a bit slower, but more thorough.
    if (!is_numeric($guid) || $guid === 0 || $guid === '0') {
        return FALSE;
    }
    if (!$newentity_cache && is_memcache_available()) {
        $newentity_cache = new ElggMemcache('new_entity_cache');
    }
    if ($newentity_cache) {
        $new_entity = $newentity_cache->load($guid);
    }
    if ($new_entity) {
        return $new_entity;
    }
    return entity_row_to_elggstar(get_entity_as_row($guid));
}
コード例 #13
0
ファイル: ElggEntity.php プロジェクト: rasul/Elgg
 /**
  * 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;
 }
コード例 #14
0
ファイル: hooks.php プロジェクト: pleio/subsite_manager
/**
 * Create metadata on the correct site
 *
 * @param string $hook
 * @param string $type
 * @param int $returnvalue
 * @param mixed $params
 * @return int $site_guid
 */
function subsite_manager_create_metadata_hook($hook, $type, $returnvalue, $params)
{
    $result = $returnvalue;
    $entity_guid = elgg_extract("entity_guid", $params);
    $metadata_name = elgg_extract("metadata_name", $params);
    $site_guid = elgg_extract("site_guid", $params);
    if (!empty($entity_guid)) {
        if ($entity_row = get_entity_as_row($entity_guid)) {
            if ($entity_row->type != "user") {
                // default set metadata to the site of the entity
                $result = (int) $entity_row->site_guid;
            } elseif (subsite_manager_on_subsite()) {
                global $SUBSITE_MANAGER_MAIN_PROFILE_FIELDS;
                $global_metadata_fields = array("validated", "validation_method", "validated_official", "icontime", "x1", "x2", "y1", "y2");
                if (!empty($SUBSITE_MANAGER_MAIN_PROFILE_FIELDS) && is_array($SUBSITE_MANAGER_MAIN_PROFILE_FIELDS)) {
                    $global_metadata_fields = array_merge($global_metadata_fields, array_keys($SUBSITE_MANAGER_MAIN_PROFILE_FIELDS));
                }
                if (in_array($metadata_name, $global_metadata_fields)) {
                    $result = elgg_get_site_entity()->getOwnerGUID();
                }
            }
        }
    }
    return $result;
}
コード例 #15
0
ファイル: functions.php プロジェクト: coldtrick/tag_tools
/**
 * Check is notifications for this entity are allowed
 *
 * @param int $entity_guid the entity guid
 *
 * @return bool
 */
function tag_tools_is_notification_entity($entity_guid)
{
    $entity_guid = sanitise_int($entity_guid);
    $entity_row = get_entity_as_row($entity_guid);
    if (empty($entity_row)) {
        return false;
    }
    $type_subtypes = tag_tools_get_notification_type_subtypes();
    if (empty($type_subtypes) || !is_array($type_subtypes)) {
        return false;
    }
    $type = $entity_row->type;
    if (empty($type) || !isset($type_subtypes[$type])) {
        return false;
    }
    $subtype = get_subtype_from_id($entity_row->subtype);
    if (empty($subtype)) {
        // user, group, site
        return true;
    }
    return in_array($subtype, elgg_extract($type, $type_subtypes));
}
コード例 #16
0
 /**
  * Loads attributes from the entities table into the object.
  *
  * @param mixed $guid GUID of entity or stdClass object from entities table
  *
  * @return bool
  */
 protected function load($guid)
 {
     if ($guid instanceof stdClass) {
         $row = $guid;
     } else {
         $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']++;
         }
         // guid needs to be an int  http://trac.elgg.org/ticket/4111
         $this->attributes['guid'] = (int) $this->attributes['guid'];
         // Cache object handle
         if ($this->attributes['guid']) {
             cache_entity($this);
         }
         return true;
     }
     return false;
 }
コード例 #17
0
/**
 * Deletes all private settings for an entity.
 *
 * @param int $entity_guid The Entity GUID
 *
 * @return bool
 * @see get_private_setting()
 * @see get_all_private_settings()
 * @see set_private_setting()
 * @see remove_private_settings()
 * @link http://docs.elgg.org/DataModel/Entities/PrivateSettings
 */
function remove_all_private_settings($entity_guid)
{
    global $PRIVATE_SETTINGS_CACHE;
    static $private_setting_memcache;
    $dbprefix = elgg_get_config("dbprefix");
    $entity_guid = (int) $entity_guid;
    // check if you have access to the entity
    if (!get_entity_as_row($entity_guid)) {
        return false;
    }
    if (!isset($private_setting_memcache) && is_memcache_available()) {
        $private_setting_memcache = new ElggMemcache("private_settings");
    }
    $query = "DELETE FROM {$dbprefix}private_settings";
    $query .= " WHERE entity_guid = {$entity_guid}";
    $result = delete_data($query);
    if ($result !== false) {
        // unset local cache
        if (isset($PRIVATE_SETTINGS_CACHE[$entity_guid])) {
            unset($PRIVATE_SETTINGS_CACHE[$entity_guid]);
        }
        // unset memcache
        if ($private_setting_memcache) {
            // invalidate the settings in Memcache
            $private_setting_memcache->delete($entity_guid);
        }
    }
    return $result !== false;
}
コード例 #18
0
ファイル: entities.php プロジェクト: nachopavon/Elgg
/**
 * Loads and returns an entity object from a guid.
 *
 * @param int $guid The GUID of the entity
 *
 * @return ElggEntity The correct Elgg or custom object based upon entity type and subtype
 * @link http://docs.elgg.org/DataModel/Entities
 */
function get_entity($guid)
{
    static $newentity_cache;
    $new_entity = false;
    if (!is_numeric($guid)) {
        return FALSE;
    }
    if (!$newentity_cache && is_memcache_available()) {
        $newentity_cache = new ElggMemcache('new_entity_cache');
    }
    if ($newentity_cache) {
        $new_entity = $newentity_cache->load($guid);
    }
    if ($new_entity) {
        return $new_entity;
    }
    return entity_row_to_elggstar(get_entity_as_row($guid));
}
コード例 #19
0
 /**
  * Overridden from ElggEntity and ElggObject::load(). Core always inits plugins with
  * a query joined to the objects_entity table, so all the info is there.
  *
  * @param mixed $guid GUID of an ElggObject or the stdClass object from entities table
  *
  * @return bool
  * @throws InvalidClassException
  */
 protected function load($guid)
 {
     $expected_attributes = $this->attributes;
     unset($expected_attributes['tables_split']);
     unset($expected_attributes['tables_loaded']);
     // this was loaded with a full join
     $needs_loaded = false;
     if ($guid instanceof stdClass) {
         $row = (array) $guid;
         $missing_attributes = array_diff_key($expected_attributes, $row);
         if ($missing_attributes) {
             $needs_loaded = true;
             $old_guid = $guid;
             $guid = $row['guid'];
         } else {
             $this->attributes = $row;
         }
     } else {
         $needs_loaded = true;
     }
     if ($needs_loaded) {
         $entity = (array) get_entity_as_row($guid);
         $object = (array) get_object_entity_as_row($guid);
         if (!$entity || !$object) {
             return false;
         }
         $this->attributes = array_merge($this->attributes, $entity, $object);
     }
     $this->attributes['tables_loaded'] = 2;
     // Check the type
     if ($this->attributes['type'] != 'object') {
         $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class()));
         throw new InvalidClassException($msg);
     }
     // guid needs to be an int  http://trac.elgg.org/ticket/4111
     $this->attributes['guid'] = (int) $this->attributes['guid'];
     // cache the entity
     if ($this->attributes['guid']) {
         cache_entity($this);
     }
     return true;
 }
コード例 #20
0
ファイル: ElggEntity.php プロジェクト: elgg/elgg
 /**
  * Loads attributes from the entities table into the object.
  *
  * @param mixed $guid GUID of entity or \stdClass object from entities table
  *
  * @return bool
  */
 protected function load($guid)
 {
     if ($guid instanceof \stdClass) {
         $row = $guid;
     } else {
         $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;
         }
         // guid needs to be an int  https://github.com/elgg/elgg/issues/4111
         $this->attributes['guid'] = (int) $this->attributes['guid'];
         // for BC with 1.8, ->subtype always returns ID, ->getSubtype() the string
         $this->attributes['subtype'] = (int) $this->attributes['subtype'];
         // Cache object handle
         if ($this->attributes['guid']) {
             _elgg_services()->entityCache->set($this);
         }
         return true;
     }
     return false;
 }
コード例 #21
0
ファイル: group.php プロジェクト: eokyere/elgg
/**
 * Create or update the extras table for a given group.
 * Call create_entity first.
 * 
 * @param int $guid
 * @param string $name
 * @param string $description
 */
function create_group_entity($guid, $name, $description)
{
    global $CONFIG;
    $guid = (int) $guid;
    $name = sanitise_string($name);
    $description = sanitise_string($description);
    $row = get_entity_as_row($guid);
    if ($row) {
        // Exists and you have access to it
        if ($exists = get_data_row("SELECT guid from {$CONFIG->dbprefix}groups_entity WHERE guid = {$guid}")) {
            $result = update_data("UPDATE {$CONFIG->dbprefix}groups_entity set name='{$name}', description='{$description}' where guid={$guid}");
            if ($result != false) {
                // Update succeeded, continue
                $entity = get_entity($guid);
                if (trigger_elgg_event('update', $entity->type, $entity)) {
                    return $guid;
                } else {
                    $entity->delete();
                    //delete_entity($guid);
                }
            }
        } else {
            // Update failed, attempt an insert.
            $result = insert_data("INSERT into {$CONFIG->dbprefix}groups_entity (guid, name, description) values ({$guid}, '{$name}','{$description}')");
            if ($result !== false) {
                $entity = get_entity($guid);
                if (trigger_elgg_event('create', $entity->type, $entity)) {
                    return $guid;
                } else {
                    $entity->delete();
                    //delete_entity($guid);
                }
            }
        }
    }
    return false;
}