File indexing completed on 2024-12-22 05:33:28

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