178 lines
4.4 KiB
PHP
178 lines
4.4 KiB
PHP
<?php
|
|
|
|
class cvIngCfg
|
|
{
|
|
|
|
public $config;
|
|
public $structure;
|
|
public $route;
|
|
public $culture;
|
|
|
|
public function __construct($configName, $structure, $route = null)
|
|
{
|
|
$this->structure = $structure;
|
|
$this->loadConfigOrCreate($configName);
|
|
$this->culture = $this->config->getCulture();
|
|
$this->route = $route;
|
|
}
|
|
|
|
public function loadConfigOrCreate($configName)
|
|
{
|
|
if(SF_APP == 'backend') {
|
|
$culture = sfContext::getInstance()->getRequest()->getParameter('culture', stLanguage::getOptLanguage());
|
|
} else {
|
|
$culture = sfContext::getInstance()->getUser()->getCulture();
|
|
}
|
|
$this->config = stConfig::getInstance($configName, array('culture' => $culture));
|
|
|
|
if($this->config->isEmpty()) {
|
|
$this->createConfig();
|
|
}
|
|
}
|
|
|
|
public function createConfig()
|
|
{
|
|
foreach($this->structure as $fieldsetName => $fieldset) {
|
|
foreach($fieldset as $cfgname => $labels) {
|
|
foreach($labels as $labelName => $label) {
|
|
$default = isset($label['default'])
|
|
? $label['default']
|
|
: '';
|
|
$this->config->set($labelName, $default);
|
|
}
|
|
}
|
|
}
|
|
$this->config->save();
|
|
}
|
|
|
|
public function getContent()
|
|
{
|
|
$html = '';
|
|
foreach($this->structure as $fieldsetName => $fieldset) {
|
|
$formFields = $this->getFormFields($fieldset);
|
|
$name = content_tag('h2', $fieldsetName);
|
|
$html .= content_tag('fieldset', $name . implode('', $formFields));
|
|
}
|
|
|
|
return $html;
|
|
}
|
|
|
|
public function getFormFields($fieldset)
|
|
{
|
|
$formFields = array();
|
|
foreach($fieldset as $cfgname => $labels) {
|
|
foreach($labels as $labelName => $label) {
|
|
$i18n = isset($label['i18n']) && $label['i18n']
|
|
? true
|
|
: false;
|
|
$value = $this->config->get($labelName, '', $i18n);
|
|
$params = $label['params'];
|
|
$this->modParams($params, $value);
|
|
|
|
switch($label['type']) {
|
|
case 'plain':
|
|
{
|
|
$value = $params['value'];
|
|
unset($params['value']);
|
|
$formFields[] = st_admin_get_form_field($cfgname . '[' . $labelName . ']', $label['name'], $value, $label['type'], $params);
|
|
}
|
|
break;
|
|
case 'checkbox_tag':
|
|
{
|
|
$params = array_merge($params, array('checked' => $value));
|
|
$formFields[] = st_admin_get_form_field($cfgname . '[' . $labelName . ']', $label['name'], 1, $label['type'], $params);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
{
|
|
$formFields[] = st_admin_get_form_field($cfgname . '[' . $labelName . ']', $label['name'], $value, $label['type'], $params);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $formFields;
|
|
}
|
|
|
|
public function modParams(&$params, &$value)
|
|
{
|
|
if(isset($params['truncate'])) {
|
|
if(strlen($value) > $params['truncate']) {
|
|
$value = substr($value, 0, $params['truncate']) . '...';
|
|
}
|
|
unset($params['truncate']);
|
|
}
|
|
|
|
if(isset($params['warn_if_empty'])) {
|
|
if(empty($value)) {
|
|
$value = 'brak';
|
|
$params['style'] = 'color: red;';
|
|
}
|
|
unset($params['warn_if_empty']);
|
|
}
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
if(sfContext::getInstance()->getRequest()->getMethod() == sfRequest::POST) {
|
|
foreach($this->structure as $fieldsetName => $fieldset) {
|
|
foreach($fieldset as $cfgname => $labels) {
|
|
$parameter = sfContext::getInstance()->getRequest()->getParameter($cfgname, array());
|
|
|
|
foreach($labels as $labelName => $label) {
|
|
if(isset($parameter[$labelName])) {
|
|
$value = $parameter[$labelName];
|
|
} else {
|
|
$value = '';
|
|
}
|
|
$i18n = isset($label['i18n']) && $label['i18n']
|
|
? true
|
|
: false;
|
|
$this->config->set($labelName, $value, $i18n);
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!sfContext::getInstance()->getRequest()->hasErrors()) {
|
|
$this->config->save();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
#PARAMS MOD
|
|
|
|
public function validate()
|
|
{
|
|
if(sfContext::getInstance()->getRequest()->getMethod() == sfRequest::POST) {
|
|
foreach($this->structure as $fieldsetName => $fieldset) {
|
|
foreach($fieldset as $cfgname => $labels) {
|
|
$config = sfContext::getInstance()->getRequest()->getParameter($cfgname);
|
|
foreach($labels as $labelName => $label) {
|
|
if(isset($label['validate']['method']) && isset($label['validate']['message'])) {
|
|
$result = $this->{$label['validate']['method']}($config[$labelName]);
|
|
if($result) {
|
|
sfContext::getInstance()->getRequest()->setError($cfgname . '{' . $labelName . '}', $label['validate']['message']);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return !sfContext::getInstance()->getRequest()->hasErrors();
|
|
}
|
|
|
|
#VALIDATE
|
|
|
|
public function isEmpty($v)
|
|
{
|
|
return empty($v);
|
|
}
|
|
}
|