<commit_before><?php
namespace Concrete\Controller\SinglePage\Dashboard\Blocks;

use Concrete\Core\Multilingual\Page\Section\Section;
use Concrete\Core\Page\Collection\Version\Version;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Page\Stack\StackCategory;
use Concrete\Core\Support\Facade\StackFolder;
use Config;
use Concrete\Core\Page\Stack\StackList;
use Stack;
use Page;
use Permissions;
use User;
use Concrete\Core\Workflow\Request\DeletePageRequest;
use Concrete\Core\Workflow\Request\ApproveStackRequest;
use View;
use Exception;
use Redirect;
use Symfony\Component\HttpFoundation\JsonResponse;

class Stacks extends DashboardPageController
{
    public function view_global_areas()
    {
        $stm = new StackList();
        $stm->filterByGlobalAreas();
        $this->deliverStackList($stm);
        $this->set('breadcrumb', $this->getBreadcrumb());
    }

    /**
     * @return Section[]
     */
    protected function getMultilingualSections()
    {
        $result = array();
        if ($this->app->make('multilingual/detector')->isEnabled()) {
            foreach (Section::getList() as $section) {
                /* @var Section $section */
                $result[$section->getLocale()] = $section;
            }
            uasort($result, function (Section $a, Section $b) {
                $r = strcasecmp($a->getLanguageText(), $a->getLanguageText());
                if ($r === 0) {
                    $r = strcasecmp($a->getLocale(), $b->getLocale());
                }
                return $r;
            });
        }

        return $result;
    }

    public function view_details($cID, $msg = false)
    {
        if (strpos($cID, '@') !== false) {
            list($cID, $locale) = explode('@', $cID, 2);
        } else {
            $locale = '';
        }
        $s = Stack::getByID($cID);
        if (is_object($s)) {
            $isGlobalArea = $s->getStackType() == Stack::ST_TYPE_GLOBAL_AREA;
            if ($isGlobalArea || $s->isNeutralStack()) {
                $neutralStack = $s;
                $stackToEdit = $s;
            } else {
                $neutralStack = $s->getNeutralStack();
                $stackToEdit = $s;
            }
            $sections = $isGlobalArea ? array() : $this->getMultilingualSections();
            $breadcrumb = $this->getBreadcrumb($neutralStack);
            if (!empty($sections)) {
                if ($stackToEdit !== $neutralStack) {
                    $section = $stackToEdit->getMultilingualSection();
                    if ($section !== null) {
                        $locale = $section->getLocale();
                    }
                }
                if (!isset($sections[$locale])) {
                    $locale = '';
                }
                if ($locale !== '') {
                    $this->set('localeCode', $locale);
                    $this->set('localeName', $sections[$locale]->getLanguageText());
                    $stackToEdit = $neutralStack->getLocalizedStack($sections[$locale]);
                }
                $localeCrumbs = array();
                $localeCrumbs[] = array(
                    'id' => $neutralStack->getCollectionID(),
                    'active' => $locale === '',
                    'name' => h(tc('Locale', 'default')),
                    'url' => \URL::to('/dashboard/blocks/stacks', 'view_details', $neutralStack->getCollectionID()),
                );
                $mif = $this->app->make('multilingual/interface/flag');
                /* @var \Concrete\Core\Multilingual\Service\UserInterface\Flag $mif */
                foreach ($sections as $sectionLocale => $section) {
                    /* @var Section $section */
                    $localeCrumbs[] = array(
                        'id' => $neutralStack->getCollectionID().'@'.$sectionLocale,
                        'active' => $locale === $sectionLocale,
                        'name' => $mif->getSectionFlagIcon($section).' '.h($section->getLanguageText()).' <span class="text-muted">'.h($sectionLocale).'</span>',
                        'url' => \URL::to('/dashboard/blocks/stacks', 'view_details', $neutralStack->getCollectionID().rawurlencode('@'.$sectionLocale)),
                    );
                }
                foreach ($localeCrumbs as $localeCrumb) {
                    if ($localeCrumb['active']) {
                        $localeCrumb['children'] = array_filter($localeCrumbs, function ($child) {
                           return !$child['active'];
                        });
                        $breadcrumb[] = $localeCrumb;
                        break;
                    }
                }
            }
            if ($stackToEdit !== null) {
                $blocks = $stackToEdit->getBlocks('Main');
                $view = View::getInstance();
                foreach ($blocks as $b1) {
                    $btc = $b1->getInstance();
                    // now we inject any custom template CSS and JavaScript into the header
                    if ($btc instanceof \Concrete\Core\Block\BlockController) {
                        $btc->outputAutoHeaderItems();
                    }
                    $btc->runTask('on_page_view', array($view));
                }
                $this->addHeaderItem($stackToEdit->outputCustomStyleHeaderItems(true));
                $this->set('blocks', $blocks);
            }
            $this->set('neutralStack', $neutralStack);
            $this->set('stackToEdit', $stackToEdit);
            $this->set('breadcrumb', $breadcrumb);
            $this->set('isGlobalArea', $isGlobalArea);
            switch ($msg) {
                case 'stack_added':
                    $this->set('message', t('Stack added successfully.'));
                    break;
                case 'localized_stack_added':
                    $this->set('message', t('Localized version of stack added successfully.'));
                    break;
                case 'stack_approved':
                    $this->set('message', t('Stack approved successfully'));
                    break;
                case 'approve_saved':
                    $this->set('message', t('Approve request saved. You must complete the approval workflow before these changes are publicly accessible.'));
                    break;
                case 'delete_saved':
                    $this->set('message', t('Delete request saved. You must complete the delete workflow before this stack can be deleted.'));
                    break;
                case 'rename_saved':
                    $this->set('message', t('Rename request saved. You must complete the approval workflow before the name of the stack will be updated.'));
                    break;
                case 'stack_deleted':
                    $this->set('message', t('Stack deleted successfully'));
                    break;
                case 'localized_stack_deleted':
                    $this->set('message', t('Localized version of stack deleted successfully'));
                    break;
            }
        } else {
            $folder = StackFolder::getByID($cID);
            if (is_object($folder)) {
                $stm = new StackList();
                $stm->filterByFolder($folder);
                $this->set('currentStackFolderID', $folder->getPage()->getCollectionID());
                $this->set('breadcrumb', $this->getBreadcrumb($folder->getPage()));
                $this->set('current', $current);
                $this->deliverStackList($stm);
                $this->set('canMoveStacks', $this->canMoveStacks($folder));
            } else {
                $root = Page::getByPath(STACKS_PAGE_PATH);
                if ($root->getCollectionID() != $cID) {
                    $this->error->add(t('Invalid stack'));
                }
                $this->view();
            }
        }
    }

    /**
     * Check if stacks in a Page or StackFolder can be moved.
     *
     * @param Page|StackFolder $parent
     *
     * @return bool
     */
    protected function canMoveStacks($parent)
    {
        $page = ($parent instanceof \Concrete\Core\Page\Page) ? $parent : $parent->getPage();
        $cpc = new Permissions($page);
        return (bool) $cpc->canMoveOrCopyPage();
    }

    protected function getBreadcrumb(\Concrete\Core\Page\Page $page = null)
    {
        $breadcrumb = [[
            'active' => false,
            'name' => t('Stacks'),
            'url' => \URL::to('/dashboard/blocks/stacks'),
        ]];
        $nav = $this->app->make('helper/navigation');
        if ($page === null) {
            $breadcrumb[] = [
                'id' => '',<commit_msg>Enable localized versions of global areas<commit_after><?php
namespace Concrete\Controller\SinglePage\Dashboard\Blocks;

use Concrete\Core\Multilingual\Page\Section\Section;
use Concrete\Core\Page\Collection\Version\Version;
use Concrete\Core\Page\Controller\DashboardPageController;
use Concrete\Core\Page\Stack\StackCategory;
use Concrete\Core\Support\Facade\StackFolder;
use Config;
use Concrete\Core\Page\Stack\StackList;
use Stack;
use Page;
use Permissions;
use User;
use Concrete\Core\Workflow\Request\DeletePageRequest;
use Concrete\Core\Workflow\Request\ApproveStackRequest;
use View;
use Exception;
use Redirect;
use Symfony\Component\HttpFoundation\JsonResponse;

class Stacks extends DashboardPageController
{
    public function view_global_areas()
    {
        $stm = new StackList();
        $stm->filterByGlobalAreas();
        $this->deliverStackList($stm);
        $this->set('breadcrumb', $this->getBreadcrumb());
    }

    /**
     * @return Section[]
     */
    protected function getMultilingualSections()
    {
        $result = array();
        if ($this->app->make('multilingual/detector')->isEnabled()) {
            foreach (Section::getList() as $section) {
                /* @var Section $section */
                $result[$section->getLocale()] = $section;
            }
            uasort($result, function (Section $a, Section $b) {
                $r = strcasecmp($a->getLanguageText(), $a->getLanguageText());
                if ($r === 0) {
                    $r = strcasecmp($a->getLocale(), $b->getLocale());
                }
                return $r;
            });
        }

        return $result;
    }

    public function view_details($cID, $msg = false)
    {
        if (strpos($cID, '@') !== false) {
            list($cID, $locale) = explode('@', $cID, 2);
        } else {
            $locale = '';
        }
        $s = Stack::getByID($cID);
        if (is_object($s)) {
            $isGlobalArea = $s->getStackType() == Stack::ST_TYPE_GLOBAL_AREA;
            if ($s->isNeutralStack()) {
                $neutralStack = $s;
                $stackToEdit = $s;
            } else {
                $neutralStack = $s->getNeutralStack();
                $stackToEdit = $s;
            }
            $sections = $this->getMultilingualSections();
            $breadcrumb = $this->getBreadcrumb($neutralStack);
            if (!empty($sections)) {
                if ($stackToEdit !== $neutralStack) {
                    $section = $stackToEdit->getMultilingualSection();
                    if ($section !== null) {
                        $locale = $section->getLocale();
                    }
                }
                if (!isset($sections[$locale])) {
                    $locale = '';
                }
                if ($locale !== '') {
                    $this->set('localeCode', $locale);
                    $this->set('localeName', $sections[$locale]->getLanguageText());
                    $stackToEdit = $neutralStack->getLocalizedStack($sections[$locale]);
                }
                $localeCrumbs = array();
                $localeCrumbs[] = array(
                    'id' => $neutralStack->getCollectionID(),
                    'active' => $locale === '',
                    'name' => h(tc('Locale', 'default')),
                    'url' => \URL::to('/dashboard/blocks/stacks', 'view_details', $neutralStack->getCollectionID()),
                );
                $mif = $this->app->make('multilingual/interface/flag');
                /* @var \Concrete\Core\Multilingual\Service\UserInterface\Flag $mif */
                foreach ($sections as $sectionLocale => $section) {
                    /* @var Section $section */
                    $localeCrumbs[] = array(
                        'id' => $neutralStack->getCollectionID().'@'.$sectionLocale,
                        'active' => $locale === $sectionLocale,
                        'name' => $mif->getSectionFlagIcon($section).' '.h($section->getLanguageText()).' <span class="text-muted">'.h($sectionLocale).'</span>',
                        'url' => \URL::to('/dashboard/blocks/stacks', 'view_details', $neutralStack->getCollectionID().rawurlencode('@'.$sectionLocale)),
                    );
                }
                foreach ($localeCrumbs as $localeCrumb) {
                    if ($localeCrumb['active']) {
                        $localeCrumb['children'] = array_filter($localeCrumbs, function ($child) {
                           return !$child['active'];
                        });
                        $breadcrumb[] = $localeCrumb;
                        break;
                    }
                }
            }
            if ($stackToEdit !== null) {
                $blocks = $stackToEdit->getBlocks('Main');
                $view = View::getInstance();
                foreach ($blocks as $b1) {
                    $btc = $b1->getInstance();
                    // now we inject any custom template CSS and JavaScript into the header
                    if ($btc instanceof \Concrete\Core\Block\BlockController) {
                        $btc->outputAutoHeaderItems();
                    }
                    $btc->runTask('on_page_view', array($view));
                }
                $this->addHeaderItem($stackToEdit->outputCustomStyleHeaderItems(true));
                $this->set('blocks', $blocks);
            }
            $this->set('neutralStack', $neutralStack);
            $this->set('stackToEdit', $stackToEdit);
            $this->set('breadcrumb', $breadcrumb);
            $this->set('isGlobalArea', $isGlobalArea);
            switch ($msg) {
                case 'stack_added':
                    $this->set('message', t('Stack added successfully.'));
                    break;
                case 'localized_stack_added':
                    $this->set('message', t('Localized version of stack added successfully.'));
                    break;
                case 'stack_approved':
                    $this->set('message', t('Stack approved successfully'));
                    break;
                case 'approve_saved':
                    $this->set('message', t('Approve request saved. You must complete the approval workflow before these changes are publicly accessible.'));
                    break;
                case 'delete_saved':
                    $this->set('message', t('Delete request saved. You must complete the delete workflow before this stack can be deleted.'));
                    break;
                case 'rename_saved':
                    $this->set('message', t('Rename request saved. You must complete the approval workflow before the name of the stack will be updated.'));
                    break;
                case 'stack_deleted':
                    $this->set('message', t('Stack deleted successfully'));
                    break;
                case 'localized_stack_deleted':
                    $this->set('message', t('Localized version of stack deleted successfully'));
                    break;
            }
        } else {
            $folder = StackFolder::getByID($cID);
            if (is_object($folder)) {
                $stm = new StackList();
                $stm->filterByFolder($folder);
                $this->set('currentStackFolderID', $folder->getPage()->getCollectionID());
                $this->set('breadcrumb', $this->getBreadcrumb($folder->getPage()));
                $this->set('current', $current);
                $this->deliverStackList($stm);
                $this->set('canMoveStacks', $this->canMoveStacks($folder));
            } else {
                $root = Page::getByPath(STACKS_PAGE_PATH);
                if ($root->getCollectionID() != $cID) {
                    $this->error->add(t('Invalid stack'));
                }
                $this->view();
            }
        }
    }

    /**
     * Check if stacks in a Page or StackFolder can be moved.
     *
     * @param Page|StackFolder $parent
     *
     * @return bool
     */
    protected function canMoveStacks($parent)
    {
        $page = ($parent instanceof \Concrete\Core\Page\Page) ? $parent : $parent->getPage();
        $cpc = new Permissions($page);
        return (bool) $cpc->canMoveOrCopyPage();
    }

    protected function getBreadcrumb(\Concrete\Core\Page\Page $page = null)
    {
        $breadcrumb = [[
            'active' => false,
            'name' => t('Stacks & Global Areas'),
            'url' => \URL::to('/dashboard/blocks/stacks'),
        ]];
        $nav = $this->app->make('helper/navigation');
        if ($page === null) {
            $breadcrumb[] = [
                'id' => '',
{'fraction_non_alphanumeric': 0.14450966850828728, 'fraction_numerical': 0.00046040515653775324, 'mean_word_length': 1.6509534706331046, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 171, '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': '1115329', 'n_tokens_mistral': 4662, 'n_tokens_neox': 4421, 'n_words': 870}