jacmaes
4/30/2015 - 5:21 PM

HideUneditablePages.module

<?php

/**
 * This module is build by Adrian, tnx Adrian.
 *
 *
 * Module to hide pages that are not editable by the logged in user.
 *
 * ProcessWire 2.x
 * Copyright (C) 2010 by Ryan Cramer
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 *
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */

class HideUneditablePages extends WireData implements Module {

    public static function getModuleInfo() {

        return array(
            'title' => 'HideUneditablePages',
            'version' => 1,
            'summary' => 'Module to hide pages that are not editable by the logged in user.',
            'href' => '',
            'singular' => true,
            'autoload' => true
            );
    }


    public function init() {
        // only add hook only if the render parameter is set
        // (as used by ProcessPageList)
        // or if superuser, also exit
        if(!isset($_GET['render']) || $this->user->isSuperuser()) return;

        $this->addHookAfter('ProcessPageList::execute', $this, 'pageListHiddenPages');
    }


    public function pageListHiddenPages(HookEvent $event){

        // make sure it's an ajax request
        if($this->config->ajax){
            // manipulate the json returned and remove any pages found from array
            $json = json_decode($event->return, true);
            foreach($json['children'] as $key => $child){
                $c = $this->pages->get($child['id']);
                $pagetemplate = $c->template;
                $uroles = $this->user->roles->implode(',', 'id');
                $uroles = explode(',', $uroles);
                if(is_array($pagetemplate->editRoles) || is_array($pagetemplate->addRoles)) {
                    if(!array_intersect($uroles, $pagetemplate->editRoles) && !array_intersect($uroles, $pagetemplate->addRoles)) unset($json['children'][$key]);
                }
            }
            $json['children'] = array_values($json['children']);
            $event->return = json_encode($json);
        }

    }

}