<commit_before><?php
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2006-2013 <NAME>
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version. 
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace Xibo\Controller;
use Respect\Validation\Validator as v;
use Stash\Interfaces\PoolInterface;
use Xibo\Exception\AccessDeniedException;
use Xibo\Factory\LayoutFactory;
use Xibo\Factory\SettingsFactory;
use Xibo\Service\ConfigServiceInterface;
use Xibo\Service\DateServiceInterface;
use Xibo\Service\LogServiceInterface;
use Xibo\Service\SanitizerServiceInterface;

/**
 * Class Settings
 * @package Xibo\Controller
 */
class Settings extends Base
{
    /**
     * @var PoolInterface
     */
    private $pool;

    /**
     * @var SettingsFactory
     */
    private $settingsFactory;

    /** @var  LayoutFactory */
    private $layoutFactory;

    /**
     * Set common dependencies.
     * @param LogServiceInterface $log
     * @param SanitizerServiceInterface $sanitizerService
     * @param \Xibo\Helper\ApplicationState $state
     * @param \Xibo\Entity\User $user
     * @param \Xibo\Service\HelpServiceInterface $help
     * @param DateServiceInterface $date
     * @param ConfigServiceInterface $config
     * @param PoolInterface $pool
     * @param SettingsFactory $settingsFactory
     * @param LayoutFactory $layoutFactory
     */
    public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $pool, $settingsFactory, $layoutFactory)
    {
        $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);

        $this->pool = $pool;
        $this->settingsFactory = $settingsFactory;
        $this->layoutFactory = $layoutFactory;

        // Initialise extra validation rules
        v::with('Xibo\\Validation\\Rules\\');
    }

    /**
     *
     */
    function displayPage()
    {
        // Get all of the settings in an array
        $settings = $this->settingsFactory->query(null, ['userSee' => 1]);

        $currentCategory = '';
        $categories = array();
        $formFields = array();

        // Should we hide other themes?
        $hideThemes = $this->getConfig()->getThemeConfig('hide_others');

        // Go through each setting, validate it and add it to the array
        foreach ($settings as $setting) {

            $options = [];

            if ($currentCategory != $setting['cat']) {
                $currentCategory = $setting['cat'];
                $categories[] = array('tabId' => $setting['cat'], 'tabName' => ucfirst($setting['cat']));
            }

            // Special handling for the theme selector
            if (!$hideThemes && $setting['setting'] == 'GLOBAL_THEME_NAME') {
                // Convert to a drop down containing the theme names that are available
                $setting['fieldType'] = 'dropdown';

                $directory = new \RecursiveDirectoryIterator(PROJECT_ROOT . '/web/theme', \FilesystemIterator::SKIP_DOTS);
                $filter = new \RecursiveCallbackFilterIterator($directory, function($current, $key, $iterator) {

                    if ($current->isDir()) {
                        return true;
                    }

                    return strpos($current->getFilename(), 'config.php') === 0;
                });

                $iterator = new \RecursiveIteratorIterator($filter);

                // Add options for all themes installed
                $options = [];
                foreach($iterator as $file) {
                    /* @var \SplFileInfo $file */
                    $this->getLog()->debug('Found %s', $file->getPath());

                    // Include the config file
                    include $file->getPath() . '/' . $file->getFilename();

                    $options[] = ['id' => basename($file->getPath()), 'value' => $config['theme_name']];
                }

            } else if ($setting['setting'] == 'ELEVATE_LOG_UNTIL') {

                // If we are less that the current date, then show as empty
                if (intval($setting['value']) >= time()) {
                    $setting['value'] = $this->getDate()->getLocalDate($setting['value']);
                } else if ($setting['userChange'] == 0) {
                    // Set to be now, plus 1 hour
                    $setting['value'] = $this->getDate()->getLocalDate($this->getDate()->parse()->addHour(1));
                } else {
                    $setting['value'] = null;
                }

            }  else if ($setting['setting'] == 'DEFAULT_LAYOUT') {

                // Show a list of all layouts in the system
                // convert to a dropdown
                $setting['fieldType'] = 'dropdown';

                foreach ($this->layoutFactory->query(null, ['disableUserCheck' => 1]) as $layout) {
                    /** @var \Xibo\Entity\Layout $layout */
                    $options[] = ['id' => $layout->layoutId, 'value' => $layout->layout];
                }

            } else {
                // Are there any options
                $options = NULL;
                if (!empty($setting['options'])) {
                    // Change to an id=>value array
                    foreach (explode('|', $setting['options']) as $tempOption)
                        $options[] = array('id' => $tempOption, 'value' => $tempOption);
                }
            }

            // Validate the current setting
            if ($setting['type'] == 'checkbox' && isset($setting['value']))
                $validated = $setting['value'];
            else if (isset($setting['value']))
                $validated = $setting['value'];
            else
                $validated = $setting['default'];

            // Time zone type requires special handling.
            if ($setting['fieldType'] == 'timezone') {
                $options = [];
                foreach ($this->getDate()->timezoneList() as $key => $value) {
                    $options[] = ['id' => $key, 'value' => $value];
                }
            }

            // Get a list of settings and assign them to the settings field
            $formFields[] = array(
                'name' => $setting['setting'],
                'type' => $setting['type'],
                'fieldType' => $setting['fieldType'],
                'helpText' => __($setting['helptext']),
                'title' => __($setting['title']),
                'options' => $options,
                'validation' => $setting['validation'],
                'value' => $validated,
                'enabled' => $setting['userChange'],
                'catId' => $setting['cat'],
                'cat' => ucfirst($setting['cat'])
            );
        }

        $data = [
            'categories' => $categories,
            'fields' => $formFields
        ];

        // Render the Theme and output
        $this->getState()->template = 'settings-page';<commit_msg>Fix <br /> in settings page. xibosignage/xibo#1168<commit_after><?php
/*
 * Xibo - Digital Signage - http://www.xibo.org.uk
 * Copyright (C) 2006-2013 <NAME>
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version. 
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace Xibo\Controller;
use Respect\Validation\Validator as v;
use Stash\Interfaces\PoolInterface;
use Xibo\Exception\AccessDeniedException;
use Xibo\Factory\LayoutFactory;
use Xibo\Factory\SettingsFactory;
use Xibo\Service\ConfigServiceInterface;
use Xibo\Service\DateServiceInterface;
use Xibo\Service\LogServiceInterface;
use Xibo\Service\SanitizerServiceInterface;

/**
 * Class Settings
 * @package Xibo\Controller
 */
class Settings extends Base
{
    /**
     * @var PoolInterface
     */
    private $pool;

    /**
     * @var SettingsFactory
     */
    private $settingsFactory;

    /** @var  LayoutFactory */
    private $layoutFactory;

    /**
     * Set common dependencies.
     * @param LogServiceInterface $log
     * @param SanitizerServiceInterface $sanitizerService
     * @param \Xibo\Helper\ApplicationState $state
     * @param \Xibo\Entity\User $user
     * @param \Xibo\Service\HelpServiceInterface $help
     * @param DateServiceInterface $date
     * @param ConfigServiceInterface $config
     * @param PoolInterface $pool
     * @param SettingsFactory $settingsFactory
     * @param LayoutFactory $layoutFactory
     */
    public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $pool, $settingsFactory, $layoutFactory)
    {
        $this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);

        $this->pool = $pool;
        $this->settingsFactory = $settingsFactory;
        $this->layoutFactory = $layoutFactory;

        // Initialise extra validation rules
        v::with('Xibo\\Validation\\Rules\\');
    }

    /**
     *
     */
    function displayPage()
    {
        // Get all of the settings in an array
        $settings = $this->settingsFactory->query(null, ['userSee' => 1]);

        $currentCategory = '';
        $categories = array();
        $formFields = array();

        // Should we hide other themes?
        $hideThemes = $this->getConfig()->getThemeConfig('hide_others');

        // Go through each setting, validate it and add it to the array
        foreach ($settings as $setting) {

            $options = [];

            if ($currentCategory != $setting['cat']) {
                $currentCategory = $setting['cat'];
                $categories[] = array('tabId' => $setting['cat'], 'tabName' => ucfirst($setting['cat']));
            }

            // Special handling for the theme selector
            if (!$hideThemes && $setting['setting'] == 'GLOBAL_THEME_NAME') {
                // Convert to a drop down containing the theme names that are available
                $setting['fieldType'] = 'dropdown';

                $directory = new \RecursiveDirectoryIterator(PROJECT_ROOT . '/web/theme', \FilesystemIterator::SKIP_DOTS);
                $filter = new \RecursiveCallbackFilterIterator($directory, function($current, $key, $iterator) {

                    if ($current->isDir()) {
                        return true;
                    }

                    return strpos($current->getFilename(), 'config.php') === 0;
                });

                $iterator = new \RecursiveIteratorIterator($filter);

                // Add options for all themes installed
                $options = [];
                foreach($iterator as $file) {
                    /* @var \SplFileInfo $file */
                    $this->getLog()->debug('Found %s', $file->getPath());

                    // Include the config file
                    include $file->getPath() . '/' . $file->getFilename();

                    $options[] = ['id' => basename($file->getPath()), 'value' => $config['theme_name']];
                }

            } else if ($setting['setting'] == 'ELEVATE_LOG_UNTIL') {

                // If we are less that the current date, then show as empty
                if (intval($setting['value']) >= time()) {
                    $setting['value'] = $this->getDate()->getLocalDate($setting['value']);
                } else if ($setting['userChange'] == 0) {
                    // Set to be now, plus 1 hour
                    $setting['value'] = $this->getDate()->getLocalDate($this->getDate()->parse()->addHour(1));
                } else {
                    $setting['value'] = null;
                }

            }  else if ($setting['setting'] == 'DEFAULT_LAYOUT') {

                // Show a list of all layouts in the system
                // convert to a dropdown
                $setting['fieldType'] = 'dropdown';

                foreach ($this->layoutFactory->query(null, ['disableUserCheck' => 1]) as $layout) {
                    /** @var \Xibo\Entity\Layout $layout */
                    $options[] = ['id' => $layout->layoutId, 'value' => $layout->layout];
                }

            } else {
                // Are there any options
                $options = NULL;
                if (!empty($setting['options'])) {
                    // Change to an id=>value array
                    foreach (explode('|', $setting['options']) as $tempOption)
                        $options[] = array('id' => $tempOption, 'value' => $tempOption);
                }
            }

            // Validate the current setting
            if ($setting['type'] == 'checkbox' && isset($setting['value']))
                $validated = $setting['value'];
            else if (isset($setting['value']))
                $validated = $setting['value'];
            else
                $validated = $setting['default'];

            // Time zone type requires special handling.
            if ($setting['fieldType'] == 'timezone') {
                $options = [];
                foreach ($this->getDate()->timezoneList() as $key => $value) {
                    $options[] = ['id' => $key, 'value' => $value];
                }
            }

            // Get a list of settings and assign them to the settings field
            $formFields[] = array(
                'name' => $setting['setting'],
                'type' => $setting['type'],
                'fieldType' => $setting['fieldType'],
                'helpText' => str_replace('<br />', PHP_EOL, __($setting['helptext'])),
                'title' => __($setting['title']),
                'options' => $options,
                'validation' => $setting['validation'],
                'value' => $validated,
                'enabled' => $setting['userChange'],
                'catId' => $setting['cat'],
                'cat' => ucfirst($setting['cat'])
            );
        }

        $data = [
            'categories' => $categories,
            'fields' => $formFields
        ];

        // Render the Theme and output
        $this->getState()->template = 'settings-page';
{'fraction_non_alphanumeric': 0.13954881213815132, 'fraction_numerical': 0.0022625939974712183, 'mean_word_length': 1.9328649492583918, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3682975', 'n_tokens_mistral': 4028, 'n_tokens_neox': 3772, 'n_words': 1052}