Here you are my first approach to this matter. This solutions is quite specific to what I need at the moment, but could be easly tailored to any other .
From functional point of view we need:
From technical point following tasks needs to be done:
-
Reading and parsing given Ini file
-
Fetching all settings set
-
Retrieving setting stored in fetched set
-
Creating form to change settings values and save them in the same ini file.
So, lets to the work.
First lets extend CFormModel, put class attributes, validation rules and meaningful labels. Next lets implement load method for reading INI file and save for update it. Finally make this class implementing singleton pattern (optionally, but recommended).
<?php
class EasySettingsForm extends CFormModel {
/**
* According to CFormModel convention all public attributes will be treated as modelled one.
*/
public $pageTitle;
public $itemsPerPage;
public $topMenu; // 1 - will be displayed, 0 - will not
public $footer; // 1 - will be displayed, 0 - will not
private static $instance;
public function getInstance(){
Yii::trace('getInstance() request','org.maziarz.test');
if (is_null(EasySettingsForm::$instance)) {
EasySettingsForm::$instance = new EasySettingsForm;
EasySettingsForm::$instance->load();
Yii::trace('New instance has been created', 'org.maziarz.test');
}
return EasySettingsForm::$instance;
}
public function rules(){
return array(
array('pageTitle, itemsPerPage', 'required'),
array('itemsPerPage, topMenu, footer' , 'CNumberValidator'),
);
}
public function attributeLabels(){
return array(
'itemsPerPage' => 'Items per page',
'topMenu' => 'Display top menu',
'footer' => 'Display footer items',
);
}
public function load(){
$ini_array = parse_ini_file(Yii::getPathOfAlias('webroot')."/protected/config/site.ini");
$this->setAttributes($ini_array);
}
public function save(){
$configFile = Yii::getPathOfAlias('webroot')."/protected/config/site.ini";
file_put_contents($configFile, ";config.ini");
foreach ($this->getAttributes() as $name=>$value) {
file_put_contents($configFile, "\n".$name." = ".$value, FILE_APPEND);
}
}
}
Now lets create some action for displaying and editing settings:
public function actionSettings(){
$settings = new EasySettingsForm;
$settings->load();
if (isset($_POST['EasySettingsForm'])){
$settings->attributes=$_POST['EasySettingsForm'];
if ($settings->validate()){
$settings->save();
Yii::app()->user->setFlash('settings', 'Site settings has been successfully saved.');
}
}
$this->render('easySettings', array('settings'=>$settings));
}
and relevant view:
<?php if(Yii::app()->user->hasFlash('settings')): ?>
<div class="confirmation">
<?php echo Yii::app()->user->getFlash('settings'); ?>
</div>
<?php endif;?>
<?php echo CHtml::errorSummary($settings); ?>
<?php echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data')); ?>
<div class="row">
<?php echo CHtml::activeLabel($settings,'pageTitle'); ?>
<?php echo CHtml::activeTextField($settings,'pageTitle'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($settings,'itemsPerPage'); ?>
<?php echo CHtml::activeTextField($settings,'itemsPerPage'); ?>
<?php echo CHtml::error($settings,'itemsPerPage'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($settings,'topMenu'); ?> <br></br>
<?php echo CHtml::activeRadioButtonList($settings, 'topMenu', array("1"=>"Yes", "0"=>"No"));?>
<?php echo CHtml::error($settings,'topMenu'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($settings,'footer'); ?> <br></br>
<?php echo CHtml::activeRadioButtonList($settings, 'footer', array("1"=>"Yes", "0"=>"No"));?>
<?php echo CHtml::error($settings,'footer'); ?>
</div>
<div class="row">
<?php echo CHtml::submitButton('Save'); ?>
</div>
<?php echo CHtml::endForm(); ?>
Finally we can use our settings object wherever we want. Almost wherever - at least we can use it in view files -> in particular layout view is good example where you will can easily disabling or enabling different stuff (menus, portlets, stylesheets, etc.).
Example:
..
<title><?php echo EasySettingsForm::getInstance()->pageTitle ?></title>
</head>
<body>
<div id="container" class="c">
<div id="header" class="c is_container">
<div id="headerTop"></div>
<div id="headerBody"></div>
<?php if (EasySettingsForm::getInstance()->topMenu): ?>
<div id="headerMenu">
<?php $this->widget('EWTopMenu'); ?>
</div>
<?php endif; ?>
</div>
..
I hope some of you found this solution helpful or even interesting. I am also sure some will find here a field for enhancements - you welcome.