Deletes an actor and all of the necessary cleanup. It also dispatches all the apps to
clean up after the deleted actor.

@param AnCommandContext $context Context parameter

@return AnDomainEntityAbstract
11619
protected function _actionDelete(AnCommandContext $context)
    {
        $this->getService('repos:components')
             ->fetchSet()
             ->registerEventDispatcher($this->getService('anahita:event.dispatcher'));

        $result = parent::_actionDelete($context);

        $this->getService('anahita:event.dispatcher')
             ->dispatchEvent('onDeleteActor', array(
                'actor_id' => $this->getItem()->id,
                ));

        return $result;
    }
745095
protected static function cache_remove(context $context) {
        if (!isset(self::$cache_contextsbyid[$context->id])) {
            // not cached, no need to do anything - this is relatively cheap, we do all this because count() is slow
            return;
        }
        unset(self::$cache_contexts[$context->contextlevel][$context->instanceid]);
        unset(self::$cache_contextsbyid[$context->id]);

        self::$cache_count--;

        if (self::$cache_count < 0) {
            self::$cache_count = 0;
        }
    }
444683
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        // When we process user deletions and expiries, we always delete from the user context.
        // As a result the cohort role assignments would be deleted, which has a knock-on effect with courses
        // as roles may change and data may be removed earlier than it should be.

        $allowedcontextlevels = [
            CONTEXT_SYSTEM,
            CONTEXT_COURSECAT
        ];

        if (!in_array($context->contextlevel, $allowedcontextlevels)) {
            return;
        }

        $cohortids = $DB->get_fieldset_select('cohort', 'id', 'contextid = :contextid',
            ['contextid' => $context->id]);

        // Delete the tool_cohortroles records created in the specific context.
        $DB->delete_records_list('tool_cohortroles', 'cohortid', $cohortids);
    }
156626
protected function _afterEntityDelete(AnCommandContext $context)
    {
        if (!empty($this->__sets)) {
            foreach ($this->__sets as $set) {
                if ($set->photos->getTotal() == 0) {
                    $set->delete();
                }
            }
        }
    }
1334462
protected function _actionUnlead(AnCommandContext $context)
    {
        $this->getResponse()->status = AnHttpResponse::RESET_CONTENT;
        $this->getItem()->removeLeader($this->actor);

        return $this->getItem();
    }
1064928
protected function _authorizeRemoveAdmin(AnCommandContext $context)
    {
        if ($this->_entity->isAdministrable()) {
            return ($this->_entity->administratorIds->count() >= 2);
        }

        return false;
    }
438297
public function redirect(AnCommandContext $context)
    {
        $url = null;

        if ($context->action == 'delete') {
            $url = 'option=com_'.$this->getIdentifier()->package.'&view='.AnInflector::pluralize($this->getIdentifier()->name);
        } elseif ($context->action == 'add') {
            $url = $context->result->getURL().'&get=settings';
        }

        if ($url) {
            $context->response->setRedirect(route($url));
        }
    }
652699
protected function _actionGetcount(AnCommandContext $context)
    {
        $count = $this->actor->numOfNewNotifications();
        return $this->getView()->newNotifications($count)->display();
    }
557849
protected function _actionRemoveapp(AnCommandContext $context)
    {
        $data = $context->data;
        $this->getItem()->components->extract($data->app);
        $this->commit();
    }
1088446
protected function _actionAdd(AnCommandContext $context)
    {
        $data = $context->data;
        $email = $data->email;
        //an email
        $person = $this->getService('repos:people.person')
                      ->getQuery()
                      ->email($email)
                      ->where('IF(!@col(enabled),@col(activationCode) <> \'\',1)')
                      ->disableChain()
                      ->fetch();

        if ($person) {
            $person->requiresReactivation()->save();
            $this->getResponse()->status = AnHttpResponse::CREATED;
            $this->person = $person;
        } else {
            throw new LibBaseControllerExceptionNotFound('Email Not Found');
        }
    }
1319256
protected function _fetchOwner(AnCommandContext $context)
    {
        $actor = pick($this->getActor(), $this->_default);
        $value = $this->{$this->getIdentifiableKey()};

        if ($value) {
            if ($value == 'viewer') {
                $actor = get_viewer();
            } elseif (!is_numeric($value)) {
                $actor = $this->getService('repos:people.person')->fetch(array('username' => $value));
            } else {
                $actor = $this->getService('repos:actors.actor')->fetch((int) $value);
            }

            //guest actor can never be a context actor
            if (is_person($actor) && $actor->guest()) {
                $actor = null;
            }

            //set the data owner to actor.
            $context->data['owner'] = $actor;

            if (!$actor) {
                throw new LibBaseControllerExceptionNotFound('Owner Not Found');
            }
        }

        $this->setActor($actor);
    }
1145764
public static function delete_data_for_all_users_in_context(\context $context) {
        global $DB;

        // Sanity check that context is at the User context level, then get the userid.
        if ($context->contextlevel !== CONTEXT_USER) {
            return;
        }
        $userid = $context->instanceid;

        // Delete the repository_instances records created for the userid.
        $DB->delete_records('repository_instances', ['userid' => $userid]);
    }
323463
protected function _actionAdd(AnCommandContext $context)
    {
        $context->response->status = AnHttpResponse::CREATED;
        $entity = $this->getRepository()->getEntity()->setData($context['data']);
        $this->setItem($entity);

        return $this->getItem();
    }
561667
protected function _actionDispatch(AnCommandContext $context)
    {
        if ($this->action === 'confirm' && $this->token) {
            $context->data->append(array(
               '_action' => 'confirm',
               'token' => $this->token,
            ));

            return $this->execute('post', $context);
        }

        return parent::_actionDispatch($context);
    }
418694
protected function _afterEntityDelete(AnCommandContext $context)
    {
        $this->getSpace()->setEntityState($context->entity, AnDomain::STATE_DESTROYED);
    }
512103
protected function _afterEntityInsert(AnCommandContext $context)
    {
        $actorIds = $this->package->getActorIds();

        if (count($actorIds)) {
            $actors = $this->getService('repos:actors.actor')
                           ->getQuery(true)
                           ->where('id', 'IN', $actorIds)
                           ->fetchSet();

            foreach ($actors as $actor) {
                $actor->addFollower($this->person);
            }
        }
    }
1075459
public function deleteContext($context)
    {

        $id = $context->getRecordId();

// Delete any outstanding share keys for resource links for this context
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . ' ' .
               "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
               "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_SHARE_KEY_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Delete any users in resource links for this context
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . ' ' .
               "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
               "WHERE ({$this->dbTableNamePrefix}" . DataConnector::USER_RESULT_TABLE_NAME . '.resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Update any resource links for which this consumer is acting as a primary resource link
        $sql = "UPDATE {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
               'SET primary_resource_link_pk = null, share_approved = null ' .
               "WHERE EXISTS (SELECT * FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' rl ' .
               "WHERE ({$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . '.primary_resource_link_pk = rl.resource_link_pk) AND (rl.context_pk = :id))';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Delete any resource links for this consumer
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::RESOURCE_LINK_TABLE_NAME . ' ' .
               'WHERE context_pk = :id';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $query->execute();

// Delete context
        $sql = "DELETE FROM {$this->dbTableNamePrefix}" . DataConnector::CONTEXT_TABLE_NAME . ' ' .
               'WHERE context_pk = :id';
        $query = $this->db->prepare($sql);
        $query->bindValue('id', $id, PDO::PARAM_INT);
        $ok = $query->execute();

        if ($ok) {
            $context->initialize();
        }

        return $ok;

    }