ElggEntity::enable PHP Method

enable() public method

Enable the entity
See also: access_show_hiden_entities()
public enable ( boolean $recursive = true ) : boolean
$recursive boolean Recursively enable all entities disabled with the entity?
return boolean
    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;
        // Override access only visible entities
        $old_access_status = access_get_show_hidden_status();
        access_show_hidden_entities(true);
        $result = $this->getDatabase()->updateData("UPDATE {$CONFIG->dbprefix}entities\n\t\t\tSET enabled = 'yes'\n\t\t\tWHERE 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;
    }

Usage Example

Example #1
0
 public function testElggEntityRecursiveDisableAndEnable()
 {
     global $CONFIG;
     $obj1 = new \ElggObject();
     $obj1->container_guid = $this->entity->getGUID();
     $obj1->save();
     $obj2 = new \ElggObject();
     $obj2->container_guid = $this->entity->getGUID();
     $obj2->save();
     // disable $obj2 before disabling the container
     $this->assertTrue($obj2->disable());
     // disable entities container by $this->entity
     $this->assertTrue($this->entity->disable());
     $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj1->guid}'");
     $this->assertIdentical($entity->enabled, 'no');
     // enable entities that were disabled with the container (but not $obj2)
     $this->assertTrue($this->entity->enable());
     $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj1->guid}'");
     $this->assertIdentical($entity->enabled, 'yes');
     $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj2->guid}'");
     $this->assertIdentical($entity->enabled, 'no');
     // cleanup
     $this->assertTrue($obj2->enable());
     $this->assertTrue($obj2->delete());
     $this->assertTrue($obj1->delete());
 }