File indexing completed on 2024-10-13 13:29:13

0001 <?php
0002 /**
0003  *  ocs-apiserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-apiserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  *
0022  * Created: 01.12.2017
0023  */
0024 
0025 class Application_Model_ClientConfig
0026 {
0027     /** @var string */
0028     protected $_clientName;
0029     /** @var  array */
0030     protected $_clientConfigData;
0031     protected $defaultConfigLoaded;
0032 
0033     /**
0034      * PHP 5 allows developers to declare constructor methods for classes.
0035      * Classes which have a constructor method call this method on each newly-created object,
0036      * so it is suitable for any initialization that the object may need before it is used.
0037      *
0038      * Note: Parent constructors are not called implicitly if the child class defines a constructor.
0039      * In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
0040      *
0041      * param [ mixed $args [, $... ]]
0042      * @link http://php.net/manual/en/language.oop5.decon.php
0043      * @param string $clientName
0044      */
0045     function __construct($clientName)
0046     {
0047         $this->_clientName = $clientName;
0048     }
0049 
0050     public function loadClientConfig()
0051     {
0052         $clientFileId = '';
0053         if (false == empty($this->_clientName)) {
0054             $clientFileId = '_' . $this->_clientName;
0055         }
0056         $clientConfigPath = Zend_Registry::get('config')->settings->client->config->path;
0057         $clientConfigFileName = "client{$clientFileId}.ini.php";
0058         if (file_exists($clientConfigPath . $clientConfigFileName)) {
0059             $this->_clientConfigData = require $clientConfigPath . $clientConfigFileName;
0060         } else {
0061             // load default config
0062             $this->_clientConfigData = require $clientConfigPath . "default.ini.php";
0063             $this->defaultConfigLoaded = true;
0064         }
0065     }
0066 
0067     /**
0068      * @return null|array
0069      */
0070     public function getConfig()
0071     {
0072         return $this->_clientConfigData;
0073     }
0074 
0075     /**
0076      * @return null|string
0077      */
0078     public function getClientName()
0079     {
0080         return $this->_clientName;
0081     }
0082 
0083     /**
0084      * @return Zend_Form
0085      */
0086     public function getForm()
0087     {
0088         $form = new Backend_Form_Other_Config();
0089         $form->addElementPrefixPath('Backend_Form_Element', APPLICATION_PATH . '/backend/forms/elements');
0090         $form->addElement('hidden', 'client-name');
0091         $form->getElement('client-name')->setValue($this->_clientName);
0092         foreach ($this->_clientConfigData as $key => $value) {
0093             $form = $this->generateSubForm($key, $value, $form);
0094         }
0095 
0096         return $form;
0097     }
0098 
0099     /**
0100      * @param string $key
0101      * @param mixed $value
0102      * @param Zend_Form $form
0103      * @return mixed
0104      */
0105     private function generateSubForm($key, $value, $form)
0106     {
0107         if (false === is_object($form->getSubForm($key))) {
0108             $newSubForm = new Backend_Form_Other_SubFormConfig();
0109             $form->addSubForm($newSubForm, $key);
0110             $form->getSubForm($key)->setLegend($key);
0111         }
0112 
0113         if (false === is_array($value)) {
0114             $form->addElement('text', $key);
0115             $form->$key->setValue($value);
0116             $form->$key->setLabel($key);
0117             return $form;
0118         }
0119 
0120         foreach ($value as $itemKey => $itemValue) {
0121             if (false === is_array($itemValue)) {
0122                 $element = new Backend_Form_Element_Config($itemKey);
0123                 $form->getSubForm($key)->addElement($element, $itemKey);
0124                 $form->getSubForm($key)->$itemKey->setValue($itemValue);
0125                 $form->getSubForm($key)->$itemKey->setLabel($itemKey);
0126                 continue;
0127             }
0128             $subForm = $this->generateSubForm($itemKey, $itemValue, $form->getSubForm($key));
0129             $form->addSubForm($subForm, $key);
0130         }
0131 
0132         return $form;
0133     }
0134 
0135     public function saveClientConfig($getAllParams, $clientName = null)
0136     {
0137         $clientFileId = '';
0138         if (false == empty($clientName)) {
0139             $clientFileId = '_' . $clientName;
0140         }
0141         $clientConfigPath = Zend_Registry::get('config')->settings->client->config->path;
0142         $clientConfigFileName = "client{$clientFileId}.ini.php";
0143 
0144         file_put_contents($clientConfigPath.$clientConfigFileName, '<?php
0145 
0146 return '.var_export($getAllParams, true).';'
0147         );
0148     }
0149 
0150     /**
0151      * @return array
0152      */
0153     public function getClientConfigData()
0154     {
0155         return $this->_clientConfigData;
0156     }
0157 
0158     /**
0159      * @return mixed
0160      */
0161     public function getDefaultConfigLoaded()
0162     {
0163         return $this->defaultConfigLoaded;
0164     }
0165 
0166 }