<commit_before><?php

namespace Core\Controller;

/**
 * Controller for actions on pages
 *
 * @package     CMS
 * @subpackage  Core
 * @category    Controller
 * @copyright   Copyright (c) 2009-2010 Modo Design Group (http://mododesigngroup.com)
 * @license     <license>
 */
class PageController extends \Zend_Controller_Action
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $_em;

    /**
     * @var \Core\Model\Page
     */
    protected $_page;

    public function init()
    {
        $this->_em = \Zend_Registry::get('doctrine');
        if ($this->getRequest()->getActionName() != 'add') {
            if (!$pageId = $this->getRequest()->getParam('id', false)) {
                throw new \Exception('Page not set.');
            }

            if (!$this->_page = $this->_em->getRepository('Core\Model\Page')->getPageForRender($pageId)) {
                throw new \Exception('Page does not exist.');
            }
        }
    }

    public function viewAction()
    {
        if (!\Core\Auth\Auth::getInstance()->getIdentity()->isAllowed($this->_page, 'view')) {
            throw new \Exception('Not allowed to view page.');
        }

        // Initialize blocks
        foreach ($this->_page->getBlocks() as $block) {
            if ($block instanceof \Core\Model\Block\DynamicBlock) {
                // Initialize the dynamic block
                $block->setRequest($this->getRequest());
                $block->setEntityManager($this->_em);
                $block->init();
            }
        }

        // Render blocks into block wrapper
        $blockActions = array();
        foreach ($this->_page->getBlocks() as $block) {
            if ($block->canView(\Core\Auth\Auth::getInstance()->getIdentity())) {
                $view = new \Zend_View();
                $view->assign('content', $block->render());
                $view->assign('block', $block);
                $view->assign('page', $this->_page);
                $edit = $this->getRequest()->getParam('edit', true);
                $view->assign('edit', $edit);
                $view->setBasePath(APPLICATION_ROOT . '/themes/default/layouts');
                $block->getLocation()->addContent($view->render('partials/block.phtml'));
            }
        }

        // Set the layout
        $this->_page->getLayout()->assign('page', $this->_page);
        $this->getResponse()->setBody($this->_page->getLayout()->render());
    }

    /**
     * @todo refactor the crap out of this
     */
    public function addBlockAction()
    {
        if (!\Core\Auth\Auth::getInstance()->getIdentity()->isAllowed($this->_page, 'edit')) {
            throw new \Exception('Not allowed to edit page.');
        }
        if (!($location = $this->getRequest()->getParam('location'))){
            throw new \Exception('Invalid location.');
        }
        if (!($location = $this->_em->getRepository('Core\Model\Layout\Location')->findOneBySysname($location))) {
            throw new \Exception('Invalid location.');
        }
        $type = $this->getRequest()->getParam('type');

        switch($type) {
            case 'standard':
                $controller = new \Core\Controller\Content\Text();
                $controller->setEntityManager($this->_em);
                $controller->setRequest($this->getRequest());
                $controller->setResponse($this->getResponse());

                $frontend = $controller->addAction();

                $block = null;
                if ($this->getRequest()->isPost() && $frontend->code->id <= 0) {
                    $text = $frontend->html;
                    $frontend = new \Core\Model\Frontend\BlockInfo();
                    $view = \Core\Module\Registry::getInstance()->getDatabaseStorage()->getModule('Core')->getContentType('Text')->getView('default');
                    $block = new \Core\Model\Block\StaticBlock($text, $view);
                    $this->_page->addBlock($block, $location);
                    $this->_em->persist($block);
                    $this->_em->flush();
                    $frontend->success($block);
                    $frontend->html = $block->render();
                } else {
                    $block = new \stdClass();
                    $block->id = 'new';
                }
                $view = new \Zend_View();
                $view->assign('content', $frontend->html);
                $view->assign('block', $block);
                $edit = $this->getRequest()->getParam('edit', true);
                $view->assign('edit', $edit);
                $view->setBasePath(APPLICATION_ROOT . '/themes/default/layouts');
                $frontend->html = $view->render('partials/block.phtml');
                echo $frontend;
                return;
            case 'shared':
                $types = $this->_em->getRepository('Core\Model\Content\Text')->findSharedText();
                $view = new \Core\Model\View('Core', 'Block/addShared');
                $view->assign('types', $types);
                $view->assign('type', $type);
                $view->assign('id', $this->_page->id);
                $view->assign('location', $location->sysname);
                $frontend = new \Core\Model\Frontend\Simple();
                $frontend->html = $view->render();
                if ($contentId = $this->getRequest()->getParam('content', null)) {
                    $content = $this->_em->getReference('Core\Model\Content', $contentId);
                    $view = \Core\Module\Registry::getInstance()->getDatabaseStorage()->getModule('Core')->getContentType('Text')->getView('default');
                    $block = new \Core\Model\Block\StaticBlock($content, $view);
                    $this->_page->addBlock($block, $location);
                    $this->_em->persist($block);
                    $this->_em->flush();
                    echo new \Core\Model\Frontend\Simple();
                    return;
                }
                echo $frontend;
                return;
            case 'dynamic':
                $types = $this->_em->getRepository('Core\Model\Module\BlockType')->findAddableBlockTypes();
                $view = new \Core\Model\View('Core', 'Block/addDynamic');
                $view->assign('types', $types);
                $view->assign('type', $type);
                $view->assign('id', $this->_page->id);
                $view->assign('location', $location->sysname);
                $frontend = new \Core\Model\Frontend\Simple();
                $frontend->html = $view->render($view->getFile());
                if ($blockId = $this->getRequest()->getParam('blockType', null)) {
                    $blockType = $this->_em->find('Core\Model\Module\BlockType', $blockId);
                    $view = $blockType->getView('default');
                    $block = $blockType->createInstance(array($view));
                    $this->_page->addBlock($block, $location);
                    $this->_em->persist($block);
                    $this->_em->flush();
                    echo new \Core\Model\Frontend\Simple();
                    return;
                }
                echo $frontend;
                break;
            default:
                throw new \Exception('Invalid block type.');
        }
    }

    public function editAction()
    {<commit_msg>restoring the lost addAction in page controller<commit_after><?php

namespace Core\Controller;

/**
 * Controller for actions on pages
 *
 * @package     CMS
 * @subpackage  Core
 * @category    Controller
 * @copyright   Copyright (c) 2009-2010 Modo Design Group (http://mododesigngroup.com)
 * @license     <license>
 */
class PageController extends \Zend_Controller_Action
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $_em;

    /**
     * @var \Core\Model\Page
     */
    protected $_page;

    public function init()
    {
        $this->_em = \Zend_Registry::get('doctrine');
        if ($this->getRequest()->getActionName() != 'add') {
            if (!$pageId = $this->getRequest()->getParam('id', false)) {
                throw new \Exception('Page not set.');
            }

            if (!$this->_page = $this->_em->getRepository('Core\Model\Page')->getPageForRender($pageId)) {
                throw new \Exception('Page does not exist.');
            }
        }
    }

    public function viewAction()
    {
        if (!\Core\Auth\Auth::getInstance()->getIdentity()->isAllowed($this->_page, 'view')) {
            throw new \Exception('Not allowed to view page.');
        }

        // Initialize blocks
        foreach ($this->_page->getBlocks() as $block) {
            if ($block instanceof \Core\Model\Block\DynamicBlock) {
                // Initialize the dynamic block
                $block->setRequest($this->getRequest());
                $block->setEntityManager($this->_em);
                $block->init();
            }
        }

        // Render blocks into block wrapper
        $blockActions = array();
        foreach ($this->_page->getBlocks() as $block) {
            if ($block->canView(\Core\Auth\Auth::getInstance()->getIdentity())) {
                $view = new \Zend_View();
                $view->assign('content', $block->render());
                $view->assign('block', $block);
                $view->assign('page', $this->_page);
                $edit = $this->getRequest()->getParam('edit', true);
                $view->assign('edit', $edit);
                $view->setBasePath(APPLICATION_ROOT . '/themes/default/layouts');
                $block->getLocation()->addContent($view->render('partials/block.phtml'));
            }
        }

        // Set the layout
        $this->_page->getLayout()->assign('page', $this->_page);
        $this->getResponse()->setBody($this->_page->getLayout()->render());
    }

    /**
     * @todo refactor the crap out of this
     */
    public function addBlockAction()
    {
        if (!\Core\Auth\Auth::getInstance()->getIdentity()->isAllowed($this->_page, 'edit')) {
            throw new \Exception('Not allowed to edit page.');
        }
        if (!($location = $this->getRequest()->getParam('location'))){
            throw new \Exception('Invalid location.');
        }
        if (!($location = $this->_em->getRepository('Core\Model\Layout\Location')->findOneBySysname($location))) {
            throw new \Exception('Invalid location.');
        }
        $type = $this->getRequest()->getParam('type');

        switch($type) {
            case 'standard':
                $controller = new \Core\Controller\Content\Text();
                $controller->setEntityManager($this->_em);
                $controller->setRequest($this->getRequest());
                $controller->setResponse($this->getResponse());

                $frontend = $controller->addAction();

                $block = null;
                if ($this->getRequest()->isPost() && $frontend->code->id <= 0) {
                    $text = $frontend->html;
                    $frontend = new \Core\Model\Frontend\BlockInfo();
                    $view = \Core\Module\Registry::getInstance()->getDatabaseStorage()->getModule('Core')->getContentType('Text')->getView('default');
                    $block = new \Core\Model\Block\StaticBlock($text, $view);
                    $this->_page->addBlock($block, $location);
                    $this->_em->persist($block);
                    $this->_em->flush();
                    $frontend->success($block);
                    $frontend->html = $block->render();
                } else {
                    $block = new \stdClass();
                    $block->id = 'new';
                }
                $view = new \Zend_View();
                $view->assign('content', $frontend->html);
                $view->assign('block', $block);
                $edit = $this->getRequest()->getParam('edit', true);
                $view->assign('edit', $edit);
                $view->setBasePath(APPLICATION_ROOT . '/themes/default/layouts');
                $frontend->html = $view->render('partials/block.phtml');
                echo $frontend;
                return;
            case 'shared':
                $types = $this->_em->getRepository('Core\Model\Content\Text')->findSharedText();
                $view = new \Core\Model\View('Core', 'Block/addShared');
                $view->assign('types', $types);
                $view->assign('type', $type);
                $view->assign('id', $this->_page->id);
                $view->assign('location', $location->sysname);
                $frontend = new \Core\Model\Frontend\Simple();
                $frontend->html = $view->render();
                if ($contentId = $this->getRequest()->getParam('content', null)) {
                    $content = $this->_em->getReference('Core\Model\Content', $contentId);
                    $view = \Core\Module\Registry::getInstance()->getDatabaseStorage()->getModule('Core')->getContentType('Text')->getView('default');
                    $block = new \Core\Model\Block\StaticBlock($content, $view);
                    $this->_page->addBlock($block, $location);
                    $this->_em->persist($block);
                    $this->_em->flush();
                    echo new \Core\Model\Frontend\Simple();
                    return;
                }
                echo $frontend;
                return;
            case 'dynamic':
                $types = $this->_em->getRepository('Core\Model\Module\BlockType')->findAddableBlockTypes();
                $view = new \Core\Model\View('Core', 'Block/addDynamic');
                $view->assign('types', $types);
                $view->assign('type', $type);
                $view->assign('id', $this->_page->id);
                $view->assign('location', $location->sysname);
                $frontend = new \Core\Model\Frontend\Simple();
                $frontend->html = $view->render($view->getFile());
                if ($blockId = $this->getRequest()->getParam('blockType', null)) {
                    $blockType = $this->_em->find('Core\Model\Module\BlockType', $blockId);
                    $view = $blockType->getView('default');
                    $block = $blockType->createInstance(array($view));
                    $this->_page->addBlock($block, $location);
                    $this->_em->persist($block);
                    $this->_em->flush();
                    echo new \Core\Model\Frontend\Simple();
                    return;
                }
                echo $frontend;
                break;
            default:
                throw new \Exception('Invalid block type.');
        }
    }

    /**
    * This function presents a form to add a new page. Upon valid submission of the form, a new page is created.
    */
    public function addAction()
    {
        if (!\Core\Auth\Auth::getInstance()->getIdentity()->isAllowed($this->_page, 'add')) {
            throw new \Exception('Not allowed to add page.');
        }

        $frontend = new \Core\Model\Frontend\Simple();

        $form = new \Core\Form\Page();
        $form->setAction('/direct/page/add');

        $page = null;
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            if ($form->isValid($data)) {

                $layout = $this->_em->getRepository('Core\Model\Layout')->findBy(array('sysname' => $data['layout']));
                unset($data['layout']);

                $page = new \Core\Model\Page($layout);
                $page->setData($data);
                $this->_em->flush();

                $frontend->success();
                header('Location: ' . $this->_page->getUrl());
            } else {
                $frontend->fail();
            }
        }

        if($page)
        {
            $form->setObject($page);
        }
        $frontend->html = (string)$form;

        $html = $this->getRequest()->getParam('html');
        if (isset($html)) {
            $this->_page->getLayout()->getLocation('main')->addContent($frontend->html);
            $this->_page->getLayout()->assign('page', $this->_page);
            echo $this->_page->getLayout()->render();
        } else {
            echo $frontend;
        }
    }

    public function editAction()
    {
{'fraction_non_alphanumeric': 0.17149699128085472, 'fraction_numerical': 0.0011052437676531991, 'mean_word_length': 1.7553713415665708, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 321, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '125285', 'n_tokens_mistral': 4359, 'n_tokens_neox': 4153, 'n_words': 727}