<?php
/** @var xPDOTransport $transport */
/** @var array $options */
/** @var modX $modx */
use MODX\Revolution\modX;
use MODX\Revolution\modContext;
use MODX\Revolution\modAccessContext;
use xPDO\Transport\xPDOTransport;

$jkcontext = [
    'key' => 'justknit',
    'name' => 'JustKnit',
    'description' => 'Kontext für die PWA von JustKnit'
];

if ($transport->xpdo) {
    $modx = &$transport->xpdo;

    switch ($options[xPDOTransport::PACKAGE_ACTION]) {
        case xPDOTransport::ACTION_INSTALL:
        case xPDOTransport::ACTION_UPGRADE:
            // Commonly similar actions for install and upgrade
            $upd = true;
            $context = $modx->getObject(modContext::class, ['key' => $jkcontext['key']]);
            if (!is_object($context)) {
                $upd = false;
                $context = $modx->newObject(modContext::class);
            }
            if (is_object($context)) {
                $modx->log(
                    modX::LOG_LEVEL_INFO,
                    '-> context is Object: ' . $jkcontext['name']
                );
            } else {
                $modx->log(
                    modX::LOG_LEVEL_INFO,
                    '-> context is NOT Object: ' . $jkcontext['name']
                );
            }
//            $context->fromArray($jkcontext);
            $context->set('key',$jkcontext['key']);
            $context->set('name',$jkcontext['name']);
            $context->set('description',$jkcontext['description']);

            if ($context->save()) {
                $modx->log(
                    modX::LOG_LEVEL_INFO,
                    '-> installed context: ' . $jkcontext['name']
                );
            } else {
                $modx->log(
                    modX::LOG_LEVEL_ERROR,
                    '-> could not install context: ' . $jkcontext['name']
                );
            }
            // set ACL (Load Only - 3) for anonymous usergroup to new context
            $access = $modx->newObject(modAccessContext::class);
            $access->set('target',$context->get('key'));
            $access->set('principal_class','MODX\\Revolution\\modUserGroup');
            $access->set('principal',0);
            $access->set('authority',9999);
            $access->set('policy',3);
            if ($access->save()) {
                $modx->log(
                    modX::LOG_LEVEL_INFO,
                    '-> anonymous user has access to "Load Only" new context: ' . $jkcontext['name']
                );
            } else {
                $modx->log(
                    modX::LOG_LEVEL_ERROR,
                    '-> could not install ACL "Load Only" for anonymous user for context: ' . $jkcontext['name']
                );
            }
            break;
        case xPDOTransport::ACTION_UNINSTALL:
            // Any uninstall cleanup needed?
            $context = $modx->getObject(modContext::class, ['key' => $jkcontext['key']]);
            if (is_object($context)) {
                $context->remove();
                $modx->log(
                    modX::LOG_LEVEL_INFO,
                    '-> removed context: ' . $jkcontext['name']
                );
            }
            break;
    }
}

return true;
