Completed
Branch master (2665c2)
by Pierre-Henry
58:43 queued 23:39
created

app/system/core/forms/ConfigFileCoreForm.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @title          Generate a dynamic form from INI files
4
 *
5
 * @author         Pierre-Henry Soria <[email protected]>
6
 * @copyright      (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
7
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
8
 * @package        PH7 / App / System / Core / Form
9
 */
10
namespace PH7;
11
12
defined('PH7') or exit('Restricted access');
13
14
use PH7\Framework\Str\Str, PH7\Framework\Registry\Registry;
15
16
class ConfigFileCoreForm
17
{
18
    const CONFIG_FILE = 'config.ini';
19
20
    /**
21
     * @param string $sConfigVar Specify the variable in the INI file where module options. Default: module.setting
22
     * @param string $sConfigPath Specify the path of INI file configuration WITHOUT "config.ini". The default value is the current configuration module file. Default: NULL
23
     * @return void
24
     */
25
    public static function display($sConfigVar = 'module.setting', $sConfigPath = null)
0 ignored issues
show
display uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
    {
27
        $sIniFile = (empty($sConfigPath)) ? Registry::getInstance()->path_module_config . static::CONFIG_FILE : $sConfigPath . static::CONFIG_FILE;
28
29
        if (isset($_POST['submit_config'])) {
30
            if (\PFBC\Form::isValid($_POST['submit_config']))
31
                new ConfigFileCoreFormProcess($sConfigVar, $sIniFile);
32
33
            Framework\Url\Header::redirect();
34
        }
35
36
        $oForm = new \PFBC\Form('form_config');
37
        $oForm->configure(array('action' => ''));
38
        $oForm->addElement(new \PFBC\Element\Hidden('submit_config', 'form_config'));
39
        $oForm->addElement(new \PFBC\Element\Token('config'));
40
41
        $aData = parse_ini_file($sIniFile, true);
42
        foreach ($aData[$sConfigVar] as $sKey => $sVal) {
43
            $sLabel = str_replace(array('.', '_'), ' ', $sKey);
44
            $sLabel = (new Str)->upperFirstWords($sLabel);
45
46
            if (false !== strpos($sKey, 'enable'))
47
                $oForm->addElement(new \PFBC\Element\Select($sLabel, 'config[' . $sKey . ']', array(1 => t('Enable'), 0 => t('Disable')), array('value' => $sVal)));
48
            elseif (false !== strpos($sKey, 'email'))
49
                $oForm->addElement(new \PFBC\Element\Email($sLabel, 'config[' . $sKey . ']', array('value' => $sVal)));
50
            elseif (false !== strpos($sKey, 'environment'))
51
                $oForm->addElement(new \PFBC\Element\Select($sLabel, 'config[' . $sKey . ']', array('production' => t('Production'), 'development' => t('Development')), array('description' => t('If you see "Internal Server Error" message on your site, please set to "development" mode in order to see the details of the error. If your site is on production (and visible by everyone) please set it to the production mode for security reason.'), 'value' => $sVal)));
52
            elseif (ctype_digit($sVal))
53
                $oForm->addElement(new \PFBC\Element\Number($sLabel, 'config[' . $sKey . ']', array('step' => 'any', 'value' => $sVal)));
54
            else
55
                $oForm->addElement(new \PFBC\Element\Textbox($sLabel, 'config[' . $sKey . ']', array('value' => $sVal)));
56
        }
57
        unset($aData);
58
59
        $oForm->addElement(new \PFBC\Element\Button);
60
        $oForm->render();
61
    }
62
}
63