Restore event from existing historic data.

@param array $data
@param array $logextra the format is standardised by logging API
@return bool|\core\event\base
540789
public static final function restore(array $data, array $logextra) {
        $classname = $data['eventname'];
        $component = $data['component'];
        $action = $data['action'];
        $target = $data['target'];

        // Security: make 100% sure this really is an event class.
        if ($classname !== "\\{$component}\\event\\{$target}_{$action}") {
            return false;
        }

        if (!class_exists($classname)) {
            return self::restore_unknown($data, $logextra);
        }
        $event = new $classname();
        if (!($event instanceof \core\event\base)) {
            return false;
        }

        $event->init(); // Init method of events could be setting custom properties.
        $event->restored = true;
        $event->triggered = true;
        $event->dispatched = true;
        $event->logextra = $logextra;

        foreach (self::$fields as $key) {
            if (!array_key_exists($key, $data)) {
                debugging("Event restore data must contain key $key");
                $data[$key] = null;
            }
        }
        if (count($data) != count(self::$fields)) {
            foreach ($data as $key => $value) {
                if (!in_array($key, self::$fields)) {
                    debugging("Event restore data cannot contain key $key");
                    unset($data[$key]);
                }
            }
        }
        $event->data = $data;

        return $event;
    }
453919
public function recordExists($data = [])
    {

        if (empty($data)) {
            $data = $this->getData();
        }
        $ignorestate = $this->ignore404();
        $this->ignore404(true);
        $keyColumn   = $this->getKeyColumn();
        $res         = $this->getColumnsFromFlexibee([$keyColumn],
            is_array($data) ? $data : [$keyColumn => $data]);

        if (empty($res) || (isset($res['success']) && ($res['success'] == 'false'))
            || ((isset($res) && is_array($res)) && !isset($res[0]) )) {
            $found = false;
        } else {
            $found = true;
        }
        $this->ignore404($ignorestate);
        return $found;
    }
204287
public function setLoginData(array $data)
    {
        $data = Helpers::parseLoginData($data);
        $this->getUser()->setLoginData($data[User::ID_LOGIN], $data[User::ID_ROLE], $data[User::ID_UNIT], $data[User::LOGOUT_DATE]);
    }
270994
public function restore(string $data, int $timeout = 0): bool
    {
        return $this->handle->restore($this->name, $timeout, $data);
    }
1205611
public function saveRawData( array $data )
    {
        if ( ! parent::save( $data ) )
        {
            return null;
        }

        return isset( $data['id'] ) ? $data['id'] : null;
    }
1190695
public function accept(LogEvent $event) : bool
    {
        if (empty($event['origin']['class'])) {
            return false;
        }

        switch ($this->searchMethod) {
            case static::SEARCH_METHOD_STARTS_WITH:
                return (strpos($event['origin']['class'], $this->expectedNamespace) === 0);
            case static::SEARCH_METHOD_CONTAINS:
                $pos = strpos($event['origin']['class'], $this->expectedNamespace);
                if ($pos === false) {
                    return false;
                }

                return ($pos >= 0);
            case static::SEARCH_METHOD_REGEXP:
                return (preg_match($this->expectedNamespace, $event['origin']['class']) > 0);
        }
    // @codeCoverageIgnoreStart
    }
89102
public function delete(array $data = array())
    {
        $sql = 'DELETE FROM log';

        $conditions = array();

        if (empty($data['log_id'])) {
            $sql .= " WHERE log_id IS NOT NULL";
        } else {
            settype($data['log_id'], 'array');
            $placeholders = rtrim(str_repeat('?,', count($data['log_id'])), ',');
            $sql .= " WHERE log_id IN($placeholders)";
            $conditions = array_merge($conditions, $data['log_id']);
        }

        if (!empty($data['type'])) {
            settype($data['type'], 'array');
            $placeholders = rtrim(str_repeat('?,', count($data['type'])), ',');
            $sql .= " AND type IN($placeholders)";
            $conditions = array_merge($conditions, $data['type']);
        }

        $this->db->run($sql, $conditions);
        return true;
    }
243179
public function delete(DataAbstract $data)
    {
        foreach ($this->primaryKeys as $key) {
            $this->addWhere($key, $this->getDataValue($data, $key));
        }

        $where = $this->getQueryWhere();

        if (empty($where)) {
            throw new \LogicException(__METHOD__ . '| Where restriction is empty for current DELETE query !');
        }

        $query     = 'DELETE FROM ' . $this->getTable() . ' ' . $this->getQueryWhere();
        $statement = $this->db->prepare($query);
        $statement->execute($this->binds);

        //~ Reset some data
        $data->setExists(false);
        $data->resetUpdated();

        //~ Clear
        $this->clear();
        $this->deleteCache($data);

        return $this;
    }
1163072
protected static function saveExtraParams(array $data)
    {
        $final_data = array();
        if (count($data) > 0) {
            Logger::log('Saving extra configuration parameters');
            foreach ($data as $key => $value) {
                if (null !== $value || $value !== '') {
                    $final_data[$key] = $value;
                }
            }
        }
        return $final_data;
    }
1014677
public function addOrRestore(array $data): \Translations\Model\Entity\Language
    {
        if (empty($data['code'])) {
            throw new InvalidArgumentException("Language data is missing 'code' key");
        }

        if (empty($data['is_rtl'])) {
            $data['is_rtl'] = $this->isRtl($data['code']);
        }

        if (empty($data['name'])) {
            $data['name'] = $this->getName($data['code']);
        }

        /**
         * @var \Cake\Datasource\EntityInterface $deletedEntity
         */
        $deletedEntity = $this->find('onlyTrashed')
            ->where(['code' => $data['code']])
            ->first();

        if (!empty($deletedEntity)) {
            return $this->restoreTrash($deletedEntity);
        }

        $newEntity = $this->newEntity();
        $newEntity = $this->patchEntity($newEntity, $data);
        /**
         * @var \Translations\Model\Entity\Language $result
         */
        $result = $this->save($newEntity);

        return $result;
    }
368063
public function restoreFromArchive($bulk = false)
    {
        if ($this->getIsArchived()) {
            $this->connection->transact(function () use ($bulk) {
                $this->triggerEvent('on_before_restore_from_archive', [$bulk]);

                if ($bulk && method_exists($this, 'getOriginalIsArchived') && method_exists($this, 'setOriginalIsArchived')) {
                    $this->setIsArchived($this->getOriginalIsArchived());
                    $this->setOriginalIsArchived(false);
                } else {
                    $this->setIsArchived(false);
                }

                $this->save();

                $this->triggerEvent('on_after_restore_from_archive', [$bulk]);
            });
        }
    }
971496
protected function _update(array $data)
    {
        $data = $this->_setData($data);
        $pmk = $this->getPrimaryKey();
        unset($data[$pmk]);
        $sql = Sql::createSql($this->getAdapter())->update($this->getTableName());
        $event = new Save([
            'action' => Save::UPDATE,
            'data' => $data,
            'abort' => false
        ]);
        $this->getEventManager()->trigger(Save::BEFORE_SAVE, $this, $event);
        if ($event->abort) {
            return false;
        }
        $sql->set($event->data)
            ->where(["{$pmk} = :id" => [':id' => $this->$pmk]])
            ->execute();
        $this->getEventManager()->trigger(Save::AFTER_SAVE, $this, $event);
        return true;
    }
1040406
public function processBeforeSave(array $fieldData, array $trackData)
    {
        $this->changed = false;

        if (! $this->exists) {
            return null;
        }

        foreach ($this->_fields as $key => $field) {
            if ($field instanceof FieldInterface) {
                $field->calculationStart($trackData);
            }
        }

        $output = array();

        foreach ($this->_fields as $key => $field) {
            if ($field instanceof FieldInterface) {
                $inVal  = isset($fieldData[$key]) ? $fieldData[$key] : null;
                $outVal = $field->calculateFieldValue($inVal, $fieldData, $trackData);

                if (is_array($outVal) || is_array($inVal)) {
                   if (is_array($outVal) && is_array($inVal)) {
                        $changedNow = ($outVal != $inVal);
                    } else {
                        $changedNow = true;
                    }
                } else {
                    $changedNow = ((string) $inVal !== (string) $outVal);
                }
                $this->changed = $this->changed || $changedNow;

                $fieldData[$key] = $outVal; // Make sure the new value is available to the next field
                $output[$key]    = $outVal;
            }
        }

        return $output;
    }
1266124
public function restore_instance_data_from_backup(\restore_task $task, array $data) {
        $courseid = $task->get_courseid();
        $context = $this->get_instance_context($courseid);
        $editablefields = $this->get_editable_fields($courseid);
        $records = api::get_instance_fields_data($editablefields, $courseid);
        $target = $task->get_target();
        $override = ($target != \backup::TARGET_CURRENT_ADDING && $target != \backup::TARGET_EXISTING_ADDING);

        foreach ($records as $d) {
            $field = $d->get_field();
            if ($field->get('shortname') === $data['shortname'] && $field->get('type') === $data['type']) {
                if (!$d->get('id') || $override) {
                    $d->set($d->datafield(), $data['value']);
                    $d->set('value', $data['value']);
                    $d->set('valueformat', $data['valueformat']);
                    $d->set('contextid', $context->id);
                    $d->save();
                }
                return;
            }
        }
    }
70339
public function onLogEvent(LogEvent $event)
    {
        $context = $event->getContext();
        $context['event.name'] = $event->getEventNameContext();

        $eventSubscriberClassName = get_class($event->getEventSubscriberContext());
        if (strrpos($eventSubscriberClassName, '\\') !== false) {
            $eventSubscriberClassName = substr($eventSubscriberClassName, strrpos($eventSubscriberClassName, '\\') + 1);
        }
        $context['event.task.name'] = $eventSubscriberClassName;

        $this->logger->log($event->getLevel(), $event->getMessage(), $context);
    }
517372
private function captureReceiveMessageText(array $eventData)
    {
        $type = strtolower(@$eventData['message']['type']);

        switch ($type) {
            case Constants::REVEIVE_TYPE_MESSAGE_TEXT:
                return @$eventData['message']['text'];

            default:
                throw new \RuntimeException("Unknown message type `$type`.");
        }
    }
563251
public function trigger($event, $data = null) {
		$event = strtolower(trim($event));
		if (!isset($this->_events[$event])) return $data;

		foreach ($this->_events[$event] as $callback) {
			// we don't want to affect the data if someone forgets to return the original data in his callback. 
			$result = call_user_func($callback, $event, $data);
			if ($result !== null) $data = $result;
		}

		return $data;
	}