modxmodx-revolutionmodx-resources

Multiple Contexts: One ReadOnly, One FullControl


Multi-Context site has two (in reality more) contexts. We need to have a user (group) where every user can edit in one context and only VIEW the other context.

The site is designed that there is no single root node in each context (because of friendly urls and logic that home is not parent of any other page).

Resource-Groups seems to be not working, because you have to add each resource manually in the group (there are tons of documents in).

Any Ideas how to handle this?


Solution

  • You have to use resource groups for this and a plugin, that sets the resource group on context base during saving the resource. The existing resources could be set by a one time running snippet.

    The code for the plugin is quite simple, it has to run in onDocFormSave:

    <?php
    /**
     * SetResourceGroup
     *
     * @package setresourcegroup
     */
    
    /** @var $modx modX */
    /** @var $scriptProperties array */
    
    /* only operate on new resources */
    if ($mode != modSystemEvent::MODE_NEW) { 
        $modx->log(xPDO::LOG_LEVEL_INFO, 'Old Resource ' . $resource->get('id'));
        return;
    } else {
        $modx->log(xPDO::LOG_LEVEL_INFO, 'New Resource ' . $resource->get('id'));
    
        switch($resource->get('context_key')) {
            case 'en':
                $group = 'Translator (en)';
                if (!$resource->joinGroup($group)) {
                    $modx->log(xPDO::LOG_LEVEL_ERROR, 'Can\'t add the resource ' . $resource->get('id') . 'to the Resource Group "'. $group . '"');
                }
                break;
        }
    
        $group = 'Administrator';
        if (!$resource->joinGroup($group)) {
            $modx->log(xPDO::LOG_LEVEL_ERROR, 'Can\'t add the resource ' . $resource->get('id') . 'to the Resource Group "'. $group . '"');
        }
    }
    
    return;