00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Adminhtml_Block_System_Config_Form extends Mage_Adminhtml_Block_Widget_Form
00036 {
00037
00038 const SCOPE_DEFAULT = 'default';
00039 const SCOPE_WEBSITES = 'websites';
00040 const SCOPE_STORES = 'stores';
00041
00042
00043
00044
00045
00046
00047 protected $_configData;
00048
00049
00050
00051
00052
00053
00054 protected $_configRoot;
00055
00056
00057
00058
00059
00060
00061 protected $_configFields;
00062
00063
00064
00065
00066
00067
00068 protected $_defaultFieldsetRenderer;
00069
00070
00071
00072
00073
00074
00075 protected $_defaultFieldRenderer;
00076
00077
00078
00079
00080
00081
00082 protected $_fieldsets = array();
00083
00084
00085
00086
00087
00088 public function __construct()
00089 {
00090 parent::__construct();
00091 }
00092
00093
00094
00095
00096
00097
00098 protected function _initObjects()
00099 {
00100 $this->_configRoot = Mage::getConfig()->getNode(null, $this->getScope(), $this->getScopeCode());
00101
00102 $this->_configData = Mage::getModel('adminhtml/config_data')
00103 ->setSection($this->getSectionCode())
00104 ->setWebsite($this->getWebsiteCode())
00105 ->setStore($this->getStoreCode())
00106 ->load();
00107
00108 $this->_configFields = Mage::getSingleton('adminhtml/config');
00109
00110 $this->_defaultFieldsetRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_fieldset');
00111 $this->_defaultFieldRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_field');
00112 return $this;
00113 }
00114
00115
00116
00117
00118
00119
00120 public function initForm()
00121 {
00122 $this->_initObjects();
00123
00124 $form = new Varien_Data_Form();
00125
00126 $sections = $this->_configFields->getSection($this->getSectionCode(), $this->getWebsiteCode(), $this->getStoreCode());
00127 if (empty($sections)) {
00128 $sections = array();
00129 }
00130 foreach ($sections as $section) {
00131
00132 if (!$this->_canShowField($section)) {
00133 continue;
00134 }
00135 foreach ($section->groups as $groups){
00136
00137 $groups = (array)$groups;
00138 usort($groups, array($this, '_sortForm'));
00139
00140 foreach ($groups as $group){
00141
00142 if (!$this->_canShowField($group)) {
00143 continue;
00144 }
00145
00146 if ($group->frontend_model) {
00147 $fieldsetRenderer = Mage::getBlockSingleton((string)$group->frontend_model);
00148 } else {
00149 $fieldsetRenderer = $this->_defaultFieldsetRenderer;
00150 }
00151
00152 $fieldsetRenderer->setForm($this);
00153 $fieldsetRenderer->setConfigData($this->_configData);
00154 $fieldsetRenderer->setGroup($group);
00155
00156 if ($this->_configFields->hasChildren($group, $this->getWebsiteCode(), $this->getStoreCode())) {
00157
00158 $helperName = $this->_configFields->getAttributeModule($section, $group);
00159
00160 $fieldsetConfig = array('legend' => Mage::helper($helperName)->__((string)$group->label));
00161 if (!empty($group->comment)) {
00162 $fieldsetConfig['comment'] = (string)$group->comment;
00163 }
00164
00165 $fieldset = $form->addFieldset(
00166 $section->getName() . '_' . $group->getName(), $fieldsetConfig)
00167 ->setRenderer($fieldsetRenderer);
00168 $this->_addElementTypes($fieldset);
00169
00170 if ($group->clone_fields) {
00171 if ($group->clone_model) {
00172 $cloneModel = Mage::getModel((string)$group->clone_model);
00173 } else {
00174 Mage::throwException('Config form fieldset clone model required to be able to clone fields');
00175 }
00176 foreach ($cloneModel->getPrefixes() as $prefix) {
00177 $this->initFields($fieldset, $group, $section, $prefix['field'], $prefix['label']);
00178 }
00179 } else {
00180 $this->initFields($fieldset, $group, $section);
00181 }
00182
00183 $this->_fieldsets[$group->getName()] = $fieldset;
00184
00185 }
00186 }
00187 }
00188 }
00189
00190 $this->setForm($form);
00191 return $this;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 public function initFields($fieldset, $group, $section, $fieldPrefix='', $labelPrefix='')
00205 {
00206 foreach ($group->fields as $elements) {
00207
00208 $elements = (array)$elements;
00209 usort($elements, array($this, '_sortForm'));
00210
00211 foreach ($elements as $e) {
00212 if (!$this->_canShowField($e)) {
00213 continue;
00214 }
00215 $path = $section->getName() . '/' . $group->getName() . '/' . $fieldPrefix . $e->getName();
00216 $id = $section->getName() . '_' . $group->getName() . '_' . $fieldPrefix . $e->getName();
00217
00218 if (isset($this->_configData[$path])) {
00219 $data = $this->_configData[$path];
00220 $inherit = false;
00221 } else {
00222 $data = $this->_configRoot->descend($path);
00223 $inherit = true;
00224 }
00225 if ($e->frontend_model) {
00226 $fieldRenderer = Mage::getBlockSingleton((string)$e->frontend_model);
00227 } else {
00228 $fieldRenderer = $this->_defaultFieldRenderer;
00229 }
00230
00231 $fieldRenderer->setForm($this);
00232 $fieldRenderer->setConfigData($this->_configData);
00233
00234 $helperName = $this->_configFields->getAttributeModule($section, $group, $e);
00235 $fieldType = (string)$e->frontend_type ? (string)$e->frontend_type : 'text';
00236 $name = 'groups['.$group->getName().'][fields]['.$fieldPrefix.$e->getName().'][value]';
00237 $label = Mage::helper($helperName)->__($labelPrefix).' '.Mage::helper($helperName)->__((string)$e->label);
00238 $comment = (string)$e->comment ? Mage::helper($helperName)->__((string)$e->comment) : '';
00239
00240 if ($e->backend_model) {
00241 $model = Mage::getModel((string)$e->backend_model);
00242 if (!$model instanceof Mage_Core_Model_Config_Data) {
00243 Mage::throwException('Invalid config field backend model: '.(string)$e->backend_model);
00244 }
00245 $model->setPath($path)->setValue($data)->afterLoad();
00246 $data = $model->getValue();
00247 }
00248 $field = $fieldset->addField($id, $fieldType, array(
00249 'name' => $name,
00250 'label' => $label,
00251 'comment' => $comment,
00252 'value' => $data,
00253 'inherit' => $inherit,
00254 'class' => $e->frontend_class,
00255 'field_config' => $e,
00256 'scope' => $this->getScope(),
00257 'scope_id' => $this->getScopeId(),
00258 'can_use_default_value' => $this->canUseDefaultValue((int)$e->show_in_default),
00259 'can_use_website_value' => $this->canUseWebsiteValue((int)$e->show_in_website),
00260 ));
00261
00262 if (isset($e->validate)) {
00263 $field->addClass($e->validate);
00264 }
00265
00266 if (isset($e->frontend_type) && 'multiselect' === (string)$e->frontend_type && isset($e->can_be_empty)) {
00267 $field->setCanBeEmpty(true);
00268 }
00269
00270 $field->setRenderer($fieldRenderer);
00271
00272 if ($e->source_model) {
00273 $sourceModel = Mage::getSingleton((string)$e->source_model);
00274 if ($sourceModel instanceof Varien_Object) {
00275 $sourceModel->setPath($path);
00276 }
00277 $field->setValues($sourceModel->toOptionArray($fieldType == 'multiselect'));
00278 }
00279 }
00280 }
00281 return $this;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 protected function _sortForm($a, $b)
00293 {
00294 return (int)$a->sort_order < (int)$b->sort_order ? -1 : ((int)$a->sort_order > (int)$b->sort_order ? 1 : 0);
00295
00296 }
00297
00298
00299
00300
00301
00302
00303
00304 public function canUseDefaultValue($field)
00305 {
00306 if ($this->getScope() == self::SCOPE_STORES && $field) {
00307 return true;
00308 }
00309 if ($this->getScope() == self::SCOPE_WEBSITES && $field) {
00310 return true;
00311 }
00312 return false;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321 public function canUseWebsiteValue($field)
00322 {
00323 if ($this->getScope() == self::SCOPE_STORES && $field) {
00324 return true;
00325 }
00326 return false;
00327 }
00328
00329
00330
00331
00332
00333
00334
00335 protected function _canShowField($field)
00336 {
00337 switch ($this->getScope()) {
00338 case self::SCOPE_DEFAULT:
00339 return (int)$field->show_in_default;
00340 break;
00341 case self::SCOPE_WEBSITES:
00342 return (int)$field->show_in_website;
00343 break;
00344 case self::SCOPE_STORES:
00345 return (int)$field->show_in_store;
00346 break;
00347 }
00348 return true;
00349 }
00350
00351
00352
00353
00354
00355
00356 public function getScope()
00357 {
00358 $scope = $this->getData('scope');
00359 if (is_null($scope)) {
00360 if ($this->getStoreCode()) {
00361 $scope = self::SCOPE_STORES;
00362 } elseif ($this->getWebsiteCode()) {
00363 $scope = self::SCOPE_WEBSITES;
00364 } else {
00365 $scope = self::SCOPE_DEFAULT;
00366 }
00367 $this->setScope($scope);
00368 }
00369
00370 return $scope;
00371 }
00372
00373
00374
00375
00376
00377
00378 public function getScopeCode()
00379 {
00380 $scopeCode = $this->getData('scope_code');
00381 if (is_null($scopeCode)) {
00382 if ($this->getStoreCode()) {
00383 $scopeCode = $this->getStoreCode();
00384 } elseif ($this->getWebsiteCode()) {
00385 $scopeCode = $this->getWebsiteCode();
00386 } else {
00387 $scopeCode = '';
00388 }
00389 $this->setScopeCode($scopeCode);
00390 }
00391
00392 return $scopeCode;
00393 }
00394
00395
00396
00397
00398
00399
00400 public function getScopeId()
00401 {
00402 $scopeId = $this->getData('scope_id');
00403 if (is_null($scopeId)) {
00404 if ($this->getStoreCode()) {
00405 $scopeId = Mage::app()->getStore($this->getStoreCode())->getId();
00406 } elseif ($this->getWebsiteCode()) {
00407 $scopeId = Mage::app()->getWebsite($this->getWebsiteCode())->getId();
00408 } else {
00409 $scopeId = '';
00410 }
00411 $this->setScopeId($scopeId);
00412 }
00413 return $scopeId;
00414 }
00415
00416
00417
00418
00419
00420
00421 protected function _getAdditionalElementTypes()
00422 {
00423 return array(
00424 'export' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_export'),
00425 'import' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_import'),
00426 'allowspecific' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_select_allowspecific'),
00427 'image' => Mage::getConfig()->getBlockClassName('adminhtml/system_config_form_field_image'),
00428 );
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 public function getSectionCode()
00442 {
00443 return $this->getRequest()->getParam('section', '');
00444 }
00445
00446
00447
00448
00449
00450
00451
00452 public function getWebsiteCode()
00453 {
00454 return $this->getRequest()->getParam('website', '');
00455 }
00456
00457
00458
00459
00460
00461
00462
00463 public function getStoreCode()
00464 {
00465 return $this->getRequest()->getParam('store', '');
00466 }
00467
00468 }