|
| 1 | +<?php |
| 2 | +/*========================================================================= |
| 3 | + MIDAS Server |
| 4 | + Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE |
| 5 | + All rights reserved. |
| 6 | + More information http://www.kitware.com |
| 7 | +
|
| 8 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + you may not use this file except in compliance with the License. |
| 10 | + You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0.txt |
| 13 | +
|
| 14 | + Unless required by applicable law or agreed to in writing, software |
| 15 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + See the License for the specific language governing permissions and |
| 18 | + limitations under the License. |
| 19 | +=========================================================================*/ |
| 20 | + |
| 21 | +/** Admin controller for the batchmake module. */ |
| 22 | +class Batchmake_AdminController extends Batchmake_AppController |
| 23 | +{ |
| 24 | + /** @var array */ |
| 25 | + public $_moduleComponents = array('KWBatchmake'); |
| 26 | + |
| 27 | + /** Index action */ |
| 28 | + public function indexAction() |
| 29 | + { |
| 30 | + $this->requireAdminPrivileges(); |
| 31 | + |
| 32 | + $this->view->pageTitle = 'BatchMake Module Configuration'; |
| 33 | + $config = $this->ModuleComponent->KWBatchmake->loadConfigProperties(null, false); |
| 34 | + $form = new Batchmake_Form_Admin(); |
| 35 | + |
| 36 | + if ($this->getRequest()->isPost()) { |
| 37 | + $data = $this->getRequest()->getPost(); |
| 38 | + |
| 39 | + if ($form->isValid($data)) { |
| 40 | + $values = $form->getValues(); |
| 41 | + |
| 42 | + foreach ($values as $key => $value) { |
| 43 | + if ($key !== MIDAS_BATCHMAKE_CSRF_TOKEN && !is_null($value)) { |
| 44 | + $config[MIDAS_BATCHMAKE_GLOBAL_CONFIG_NAME][$this->moduleName.'.'.$key] = $value; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + UtilityComponent::createInitFile(MIDAS_BATCHMAKE_MODULE_LOCAL_CONFIG, $config); |
| 49 | + } |
| 50 | + |
| 51 | + $form->populate($data); |
| 52 | + } else { |
| 53 | + $elements = $form->getElements(); |
| 54 | + |
| 55 | + foreach ($elements as $element) { |
| 56 | + $batchMakeConfig = $this->ModuleComponent->KWBatchmake->filterBatchmakeConfigProperties($config); |
| 57 | + $defaultConfig = $this->createDefaultConfig($batchMakeConfig); |
| 58 | + $name = $element->getName(); |
| 59 | + |
| 60 | + if ($name !== MIDAS_BATCHMAKE_CSRF_TOKEN && $name !== MIDAS_BATCHMAKE_SUBMIT_CONFIG) { |
| 61 | + $value = $defaultConfig[$name]; |
| 62 | + |
| 63 | + if (!is_null($value)) { |
| 64 | + $form->setDefault($name, $value); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $this->view->form = $form; |
| 71 | + session_start(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * will create default paths in the temporary directory |
| 76 | + * for any properties not already set, except for the |
| 77 | + * condor bin dir; imposing a firmer hand on the user |
| 78 | + * |
| 79 | + * @param array $currentConfig current configuration |
| 80 | + * @return array |
| 81 | + */ |
| 82 | + protected function createDefaultConfig($currentConfig) |
| 83 | + { |
| 84 | + $defaultConfigDirs = array( |
| 85 | + MIDAS_BATCHMAKE_TMP_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_TMP_DIR, |
| 86 | + MIDAS_BATCHMAKE_BIN_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_BIN_DIR, |
| 87 | + MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_SCRIPT_DIR, |
| 88 | + MIDAS_BATCHMAKE_APP_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_APP_DIR, |
| 89 | + MIDAS_BATCHMAKE_DATA_DIR_PROPERTY => MIDAS_BATCHMAKE_DEFAULT_DATA_DIR, |
| 90 | + ); |
| 91 | + |
| 92 | + $returnedConfig = array(); |
| 93 | + |
| 94 | + foreach ($currentConfig as $configProp => $configDir) { |
| 95 | + if ((!isset($configProp) || !isset($configDir) || empty($configDir)) && array_key_exists( |
| 96 | + $configProp, |
| 97 | + $defaultConfigDirs |
| 98 | + ) |
| 99 | + ) { |
| 100 | + $returnedConfig[$configProp] = UtilityComponent::getTempDirectory($defaultConfigDirs[$configProp]); |
| 101 | + } else { |
| 102 | + $returnedConfig[$configProp] = $configDir; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $returnedConfig; |
| 107 | + } |
| 108 | +} |
0 commit comments