<commit_before><?php
class Vps_Controller_Action_Component_PagesController extends Vps_Controller_Action_Auto_Tree
{
    protected $_textField = 'name';
    protected $_rootVisible = false;
    protected $_buttons = array();
    protected $_hasPosition = true;
    protected $_modelName = 'Vps_Component_Model';

    private $_componentConfigs = array();

    protected function _init()
    {
        $this->_filters->add(new Vps_Controller_Action_Auto_Filter_Text())
            ->setQueryFields(array('name'));
    }

    public function indexAction()
    {
        $this->view->xtype = 'vps.component.pages';
    }

    protected function _formatNode($row)
    {
        $component = $row->getData();
        $data = parent::_formatNode($row);
        $data['uiProvider'] = 'Vps.Component.PagesNode';

        $user = Zend_Registry::get('userModel')->getAuthedUser();
        $acl = Vps_Registry::get('acl')->getComponentAcl();

        $nodeConfig = self::getNodeConfig($component, $user, $acl, $this->_componentConfigs);
        if (is_null($nodeConfig)) return null;
        $data = array_merge($data, $nodeConfig);

        if (!$data['expanded']) {
            $openedNodes = $this->_saveSessionNodeOpened(null, null);
            if ($data['disabled'] && !array_key_exists($row->id, $openedNodes)) {
                $data['expanded'] = true;
            }
        }

        if ($data['loadChildren'] || $data['expanded'] || $data['disabled']) {
            $data['children'] = $this->_formatNodes($component->componentId);
        }

        return $data;
    }

    //public static zum testen
    public static function getNodeConfig($component, $user, $acl, array &$componentConfigs = array())
    {
        $data = array();
        $enabled = $acl->isAllowed($user, $component);
        if (!$enabled) {
//             static $allowedComponents;
//             if (!isset($allowedComponents)) {
                //TODO kann das wirklich gecached werden?
                $allowedComponents = $acl->getAllowedRecursiveChildComponents($user, $component);
//             }
            $allowed = false;
            foreach ($allowedComponents as $allowedComponent) {
                $c = $allowedComponent;
                while ($c) {
                    if ($c->componentId == $component->componentId) {
                        $allowed = true;
                        break 2;
                    }
                    $c = $c->parent;
                }
            }
            //wenn gar keine unterkomponente bearbeitet werden kann seite ausblenden
            if (!$allowed) return null;
        }
        if (!$enabled) {
            //wenn eine unterkomponente (nicht seite!) bearbeitet werden kann seite nicht ausgrauen
            $editComponents = $acl->getAllowedChildComponents($user, $component);
            if ($editComponents) $enabled = true;
        } else {
            $editComponents = array($component);
        }
        $data['actions'] = array();
        $data['allowDrop'] = false;
        $data['disabled'] = !$enabled;
        $data['editControllerUrl'] = '';
        if ($component->componentId == 'root') { // Root hat keinen Generator
            $data['bIcon'] = new Vps_Asset('world');
            $data['bIcon'] = $data['bIcon']->__toString();
            $data['expanded'] = true;
            $data['loadChildren'] = true;
        } else {
            $config = $component->generator->getPagesControllerConfig($component);
            $data = array_merge($data, $config);
            if (!$enabled) $data['iconEffects'][] = 'forbidden';
            $icon = $data['icon'];
            if (is_string($icon)) {
                $icon = new Vps_Asset($icon);
            }
            $data['bIcon'] = $icon->toString($data['iconEffects']);
            if (isset($data['icon'])) unset($data['icon']);
        }

        //wenn *unter* der seite eine page möglich ist (pageGenerator vorhanden) dann
        //hinzufügen + drop erlauben
        //das kann nicht im Generator ermittelt werden, der macht nur sich selbst
        if ($acl->isAllowed($user, $component)) {
            $pageGenerator = Vps_Component_Generator_Abstract::getInstances($component, array(
                'pageGenerator' => true
            ));
            if ($pageGenerator) {
                $data['addControllerUrl'] = Vpc_Admin::getInstance($pageGenerator[0]->getClass())
                    ->getControllerUrl('Generator');
                $data['actions']['add'] = true;
                $data['allowDrop'] = true;
            }
        }

        $data['actions']['preview'] = (bool)$component->isPage;


        //default werte
        $data['actions'] = array_merge(array(
            'properties' => false,
            'delete' => false,
            'visible' => false,
            'makeHome' => false,
            'add' => false,
        ), $data['actions']);
        $data = array_merge(array(
            'allowDrag' => false,
            'allowDrop' => false
        ), $data);


        // EditComponents
        $ec = array();
        foreach ($editComponents as $editComponent) {
            foreach (self::getEditComponents($editComponent) as $c) {
                $ec = array_merge($ec, self::_formatEditComponents($c->componentClass, $c, Vps_Component_Abstract_ExtConfig_Abstract::TYPE_DEFAULT, $componentConfigs));
            }
            foreach (self::getMenuEditComponents($editComponent) as $c) {
                $ec = array_merge($ec, self::_formatEditComponents($c->componentClass, $c, Vps_Component_Abstract_ExtConfig_Abstract::TYPE_DEFAULT, $componentConfigs));
            }
            foreach (self::getSharedComponents($editComponent) as $componentClass => $c) {
                $ec = array_merge($ec, self::_formatEditComponents($componentClass, $c, Vps_Component_Abstract_ExtConfig_Abstract::TYPE_SHARED, $componentConfigs));
            }
        }
        $data['editComponents'] = $ec;

        return $data;
    }

    protected function _getParentId($row)
    {
        $parent = $row->parent;
        if (!$parent) return null;
        return $parent->componentId;
    }

    private static function _formatEditComponents($componentClass, Vps_Component_Data $component, $configType, $componentConfigs)
    {
        $ret = array();
        $cfg = Vpc_Admin::getInstance($componentClass)->getExtConfig($configType);
        if (isset($cfg['xtype'])) { //test for legacy
            throw new Vps_Exception("getExtConfig for $componentClass doesn't return an array of configs");
        }
        foreach ($cfg as $type=>$c) {
            $k = $componentClass.'-'.$type;
            if (!isset($componentConfigs[$k])) {
                $componentConfigs[$k] = $c;
            }
            $ret[] = array(
                'componentClass' => $componentClass,
                'type' => $type,
                'componentId' => $component->dbId
            );
        }
        foreach ($component->generator->getGeneratorPlugins() as $generatorPlugin) {
            $admin = Vpc_Admin::getInstance(get_class($generatorPlugin));
            $cfg = $admin->getExtConfig($configType);
            foreach ($cfg as $type=>$c) {
                $k = get_class($generatorPlugin).'-'.$type;
                if (!isset($componentConfigs[$k])) {<commit_msg>das muss eine reference sein, das ist ein rueckgabeparameter<commit_after><?php
class Vps_Controller_Action_Component_PagesController extends Vps_Controller_Action_Auto_Tree
{
    protected $_textField = 'name';
    protected $_rootVisible = false;
    protected $_buttons = array();
    protected $_hasPosition = true;
    protected $_modelName = 'Vps_Component_Model';

    private $_componentConfigs = array();

    protected function _init()
    {
        $this->_filters->add(new Vps_Controller_Action_Auto_Filter_Text())
            ->setQueryFields(array('name'));
    }

    public function indexAction()
    {
        $this->view->xtype = 'vps.component.pages';
    }

    protected function _formatNode($row)
    {
        $component = $row->getData();
        $data = parent::_formatNode($row);
        $data['uiProvider'] = 'Vps.Component.PagesNode';

        $user = Zend_Registry::get('userModel')->getAuthedUser();
        $acl = Vps_Registry::get('acl')->getComponentAcl();

        $nodeConfig = self::getNodeConfig($component, $user, $acl, $this->_componentConfigs);
        if (is_null($nodeConfig)) return null;
        $data = array_merge($data, $nodeConfig);

        if (!$data['expanded']) {
            $openedNodes = $this->_saveSessionNodeOpened(null, null);
            if ($data['disabled'] && !array_key_exists($row->id, $openedNodes)) {
                $data['expanded'] = true;
            }
        }

        if ($data['loadChildren'] || $data['expanded'] || $data['disabled']) {
            $data['children'] = $this->_formatNodes($component->componentId);
        }

        return $data;
    }

    //public static zum testen
    public static function getNodeConfig($component, $user, $acl, array &$componentConfigs = array())
    {
        $data = array();
        $enabled = $acl->isAllowed($user, $component);
        if (!$enabled) {
//             static $allowedComponents;
//             if (!isset($allowedComponents)) {
                //TODO kann das wirklich gecached werden?
                $allowedComponents = $acl->getAllowedRecursiveChildComponents($user, $component);
//             }
            $allowed = false;
            foreach ($allowedComponents as $allowedComponent) {
                $c = $allowedComponent;
                while ($c) {
                    if ($c->componentId == $component->componentId) {
                        $allowed = true;
                        break 2;
                    }
                    $c = $c->parent;
                }
            }
            //wenn gar keine unterkomponente bearbeitet werden kann seite ausblenden
            if (!$allowed) return null;
        }
        if (!$enabled) {
            //wenn eine unterkomponente (nicht seite!) bearbeitet werden kann seite nicht ausgrauen
            $editComponents = $acl->getAllowedChildComponents($user, $component);
            if ($editComponents) $enabled = true;
        } else {
            $editComponents = array($component);
        }
        $data['actions'] = array();
        $data['allowDrop'] = false;
        $data['disabled'] = !$enabled;
        $data['editControllerUrl'] = '';
        if ($component->componentId == 'root') { // Root hat keinen Generator
            $data['bIcon'] = new Vps_Asset('world');
            $data['bIcon'] = $data['bIcon']->__toString();
            $data['expanded'] = true;
            $data['loadChildren'] = true;
        } else {
            $config = $component->generator->getPagesControllerConfig($component);
            $data = array_merge($data, $config);
            if (!$enabled) $data['iconEffects'][] = 'forbidden';
            $icon = $data['icon'];
            if (is_string($icon)) {
                $icon = new Vps_Asset($icon);
            }
            $data['bIcon'] = $icon->toString($data['iconEffects']);
            if (isset($data['icon'])) unset($data['icon']);
        }

        //wenn *unter* der seite eine page möglich ist (pageGenerator vorhanden) dann
        //hinzufügen + drop erlauben
        //das kann nicht im Generator ermittelt werden, der macht nur sich selbst
        if ($acl->isAllowed($user, $component)) {
            $pageGenerator = Vps_Component_Generator_Abstract::getInstances($component, array(
                'pageGenerator' => true
            ));
            if ($pageGenerator) {
                $data['addControllerUrl'] = Vpc_Admin::getInstance($pageGenerator[0]->getClass())
                    ->getControllerUrl('Generator');
                $data['actions']['add'] = true;
                $data['allowDrop'] = true;
            }
        }

        $data['actions']['preview'] = (bool)$component->isPage;


        //default werte
        $data['actions'] = array_merge(array(
            'properties' => false,
            'delete' => false,
            'visible' => false,
            'makeHome' => false,
            'add' => false,
        ), $data['actions']);
        $data = array_merge(array(
            'allowDrag' => false,
            'allowDrop' => false
        ), $data);


        // EditComponents
        $ec = array();
        foreach ($editComponents as $editComponent) {
            foreach (self::getEditComponents($editComponent) as $c) {
                $ec = array_merge($ec, self::_formatEditComponents($c->componentClass, $c, Vps_Component_Abstract_ExtConfig_Abstract::TYPE_DEFAULT, $componentConfigs));
            }
            foreach (self::getMenuEditComponents($editComponent) as $c) {
                $ec = array_merge($ec, self::_formatEditComponents($c->componentClass, $c, Vps_Component_Abstract_ExtConfig_Abstract::TYPE_DEFAULT, $componentConfigs));
            }
            foreach (self::getSharedComponents($editComponent) as $componentClass => $c) {
                $ec = array_merge($ec, self::_formatEditComponents($componentClass, $c, Vps_Component_Abstract_ExtConfig_Abstract::TYPE_SHARED, $componentConfigs));
            }
        }
        $data['editComponents'] = $ec;

        return $data;
    }

    protected function _getParentId($row)
    {
        $parent = $row->parent;
        if (!$parent) return null;
        return $parent->componentId;
    }

    private static function _formatEditComponents($componentClass, Vps_Component_Data $component, $configType, &$componentConfigs)
    {
        $ret = array();
        $cfg = Vpc_Admin::getInstance($componentClass)->getExtConfig($configType);
        if (isset($cfg['xtype'])) { //test for legacy
            throw new Vps_Exception("getExtConfig for $componentClass doesn't return an array of configs");
        }
        foreach ($cfg as $type=>$c) {
            $k = $componentClass.'-'.$type;
            if (!isset($componentConfigs[$k])) {
                $componentConfigs[$k] = $c;
            }
            $ret[] = array(
                'componentClass' => $componentClass,
                'type' => $type,
                'componentId' => $component->dbId
            );
        }
        foreach ($component->generator->getGeneratorPlugins() as $generatorPlugin) {
            $admin = Vpc_Admin::getInstance(get_class($generatorPlugin));
            $cfg = $admin->getExtConfig($configType);
            foreach ($cfg as $type=>$c) {
                $k = get_class($generatorPlugin).'-'.$type;
                if (!isset($componentConfigs[$k])) {
{'fraction_non_alphanumeric': 0.1427986348122867, 'fraction_numerical': 0.00027303754266211604, 'mean_word_length': 2.1954198473282442, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6608217', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3907, 'n_words': 794}