PageRenderTime 71ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/classes/ConfigReader/Base.php

https://github.com/kjavitz/SalesIgniter1
PHP | 401 lines | 338 code | 48 blank | 15 comment | 60 complexity | f4e4e6c49b7d57a619021704bc0b1ed8 MD5 | raw file
  1. <?php
  2. /*
  3. * Sales Igniter E-Commerce System
  4. * Version: 2.0
  5. *
  6. * I.T. Web Experts
  7. * http://www.itwebexperts.com
  8. *
  9. * Copyright (c) 2011 I.T. Web Experts
  10. *
  11. * This script and its source are not distributable without the written conscent of I.T. Web Experts
  12. */
  13. require(__DIR__ . '/Config.php');
  14. class ConfigurationReader
  15. {
  16. public $configData = array();
  17. public $mappings = array();
  18. public $compareData = array();
  19. public $title = '';
  20. public $description = '';
  21. public $key = '';
  22. public function loadConfiguration($configFile, $parseConfig = true) {
  23. $xmlObj = simplexml_load_file(
  24. $configFile,
  25. 'SimpleXMLElement',
  26. LIBXML_NOCDATA
  27. );
  28. if (!$xmlObj){
  29. die('Config Error::' . $configFile);
  30. }
  31. $this->title = (string)$xmlObj->title;
  32. $this->description = (string)$xmlObj->description;
  33. $this->key = (string)$xmlObj->key;
  34. if ($parseConfig === true){
  35. $this->parseXmlConfig($xmlObj);
  36. }
  37. }
  38. public function getTitle() {
  39. return $this->title;
  40. }
  41. public function getDescription() {
  42. return $this->description;
  43. }
  44. public function getKey() {
  45. return $this->key;
  46. }
  47. public function loadCompareData(SimpleXMLElement $xmlObj) {
  48. return array();
  49. }
  50. public function loadToSystem() {
  51. foreach($this->configData as $tInfo){
  52. foreach($tInfo['config'] as $cfg){
  53. sysConfig::set($cfg->getKey(), $cfg->getValue());
  54. }
  55. }
  56. }
  57. public function check(SimpleXMLElement $xmlObj) {
  58. return true;
  59. }
  60. public function parseXmlConfig(SimpleXMLElement $xmlObj) {
  61. global $App;
  62. if ($this->check($xmlObj) === false) {
  63. return;
  64. }
  65. if (empty($this->compareData)){
  66. $this->compareData = $this->loadCompareData($xmlObj);
  67. }
  68. foreach($xmlObj->tabs->children() as $TabKey => $TabInfo){
  69. $configurations = array();
  70. $configCount = 0;
  71. foreach($TabInfo->configurations->children() as $ConfigKey => $ConfigInfo){
  72. $cfgKey = (string)$ConfigKey;
  73. if (isset($this->compareData[$cfgKey])){
  74. $configVal = $this->compareData[$cfgKey]['configuration_value'];
  75. }
  76. else {
  77. $configVal = (string)$ConfigInfo->value;
  78. }
  79. $configArr = array(
  80. 'key' => $cfgKey,
  81. 'value' => $configVal
  82. );
  83. if (isset($ConfigInfo->value_glue)){
  84. $configArr['value_glue'] = (string)$ConfigInfo->value_glue;
  85. $configArr['value'] = explode($configArr['value_glue'], $configArr['value']);
  86. }
  87. if (
  88. $App && (
  89. ($App->getEnv() == 'admin' && $App->getAppName() == 'extensions' && isset($_GET['action'])) ||
  90. ($App->getEnv() == 'admin' && $App->getAppName() == 'modules' && isset($_GET['action'])) ||
  91. ($App->getEnv() == 'admin' && $App->getAppName() == 'configuration')
  92. )
  93. ){
  94. $configArr['is_editable'] = true;
  95. $configArr['title'] = (string)$ConfigInfo->title;
  96. $configArr['description'] = (string)$ConfigInfo->description;
  97. $configArr['set_function'] = null;
  98. $configArr['use_function'] = null;
  99. $configArr['is_deprecated'] = false;
  100. if (isset($ConfigInfo['editable'])){
  101. $configArr['is_editable'] = ($ConfigInfo['editable'] == 'true' ? true : false);
  102. }
  103. if (isset($ConfigInfo['deprecated'])){
  104. $configArr['is_deprecated'] = ($ConfigInfo['deprecated'] == 'true' ? true : false);
  105. }
  106. if (isset($ConfigInfo->set_function)){
  107. if (isset($ConfigInfo->set_function->type)){
  108. $setFunction = array(
  109. 'type' => (string)$ConfigInfo->set_function->type
  110. );
  111. if (isset($ConfigInfo->set_function->data_function)){
  112. $args = array();
  113. if (isset($ConfigInfo->set_function->data_function->name)){
  114. $function = (string)$ConfigInfo->set_function->data_function->name;
  115. if (isset($ConfigInfo->set_function->data_function->args)){
  116. foreach($ConfigInfo->set_function->data_function->args->children() as $arg){
  117. $argStr = (string)$arg;
  118. if (substr($argStr, 0, 7) == 'CONFIG:'){
  119. $argStr = sysConfig::get(substr($argStr, 7));
  120. }
  121. $args[] = $argStr;
  122. }
  123. }
  124. }
  125. else {
  126. $function = (string)$ConfigInfo->set_function->data_function;
  127. }
  128. $data = call_user_func_array($function, $args);
  129. foreach($data as $dInfo){
  130. $setFunction['values'][] = array(
  131. 'id' => $dInfo['id'],
  132. 'text' => $dInfo['text']
  133. );
  134. }
  135. }
  136. elseif (isset($ConfigInfo->set_function->values)) {
  137. foreach($ConfigInfo->set_function->values->children() as $value){
  138. $id = (string)$value;
  139. if (isset($value['id'])){
  140. $id = $value['id'];
  141. }
  142. $setFunction['values'][] = array(
  143. 'id' => (string)$id,
  144. 'text' => (string)$value
  145. );
  146. }
  147. }
  148. if (isset($ConfigInfo->set_function->attributes)){
  149. $setFunction['attributes'] = array();
  150. foreach($ConfigInfo->set_function->attributes->children() as $k => $v){
  151. $setFunction['attributes'][$k] = $v;
  152. }
  153. }
  154. if (!isset($setFunction['values']) || empty($setFunction['values'])){
  155. if (isset($ConfigInfo->set_function['use_default_if_no_data']) && $ConfigInfo->set_function['use_default_if_no_data'] == 'true'){
  156. $setFunction = null;
  157. }
  158. }
  159. }
  160. else {
  161. $setFunction = (string)$ConfigInfo->set_function;
  162. }
  163. $configArr['set_function'] = $setFunction;
  164. }
  165. if (isset($ConfigInfo->use_function)){
  166. $configArr['use_function'] = (string)$ConfigInfo->use_function;
  167. }
  168. }
  169. $configurations[$configCount] = new Config($configArr);
  170. $this->mappings[$configArr['key']] = array((string)$TabKey, $configCount);
  171. $configCount++;
  172. }
  173. if (!isset($this->configData[(string)$TabKey])){
  174. $this->configData[(string)$TabKey] = array(
  175. 'title' => (string)$TabInfo->title,
  176. 'description' => (string)$TabInfo->description,
  177. 'config' => $configurations
  178. );
  179. }
  180. else {
  181. $this->configData[(string)$TabKey]['config'] = array_merge_recursive($this->configData[(string)$TabKey]['config'], $configurations);
  182. }
  183. }
  184. }
  185. /**
  186. * @param bool $key
  187. * @return Config
  188. */
  189. public function getConfig($key = false) {
  190. if ($key === false){
  191. return $this->configData;
  192. }
  193. if (
  194. isset($this->mappings[$key]) &&
  195. isset($this->configData[$this->mappings[$key][0]])
  196. ){
  197. return $this->configData[$this->mappings[$key][0]]['config'][$this->mappings[$key][1]];
  198. }
  199. return null;
  200. }
  201. public function getInputField(Config $Config, $disable = false, $fieldName = 'configuration') {
  202. $fieldHtml = 'NOTHING WORKED';
  203. if ($Config->isEditable() === false){
  204. $fieldHtml = $Config->getValue();
  205. }
  206. elseif ($Config->hasSetFunction() === true) {
  207. $function = $Config->getSetFunction();
  208. if (is_array($function)){
  209. switch($function['type']){
  210. case 'textarea':
  211. $inputField = htmlBase::newElement('textarea')
  212. ->setName($fieldName . '[' . $Config->getKey() . ']')
  213. ->html($Config->getValue());
  214. if (isset($function['attributes'])){
  215. foreach($function['attributes'] as $k => $v){
  216. $inputField->attr($k, $v);
  217. }
  218. }
  219. $fieldHtml = $inputField->draw();
  220. break;
  221. case 'selectbox':
  222. $inputField = htmlBase::newElement('selectbox')
  223. ->addClass('notEdited')
  224. ->setName($fieldName . '[' . $Config->getKey() . ']')
  225. ->selectOptionByValue($Config->getValue());
  226. foreach($function['values'] as $vInfo){
  227. $inputField->addOption($vInfo['id'], $vInfo['text']);
  228. }
  229. $fieldHtml = $inputField->draw();
  230. break;
  231. case 'radio':
  232. $data = array();
  233. foreach($function['values'] as $vInfo){
  234. $data[] = array(
  235. 'label' => $vInfo['text'],
  236. 'value' => $vInfo['id']
  237. );
  238. }
  239. $inputField = htmlBase::newElement('radio')
  240. ->addGroup(array(
  241. 'name' => $fieldName . '[' . $Config->getKey() . ']',
  242. 'separator' => '<br>',
  243. 'labelPosition' => 'after',
  244. 'checked' => $Config->getValue(),
  245. 'data' => $data
  246. ));
  247. $fieldHtml = $inputField->draw();
  248. break;
  249. case 'checkbox':
  250. $data = array();
  251. foreach($function['values'] as $vInfo){
  252. $data[] = array(
  253. 'label' => $vInfo['text'],
  254. 'value' => $vInfo['id']
  255. );
  256. }
  257. $inputField = htmlBase::newElement('checkbox')
  258. ->addGroup(array(
  259. 'name' => $fieldName . '[' . $Config->getKey() . '][]',
  260. 'separator' => '<br>',
  261. 'labelPosition' => 'after',
  262. 'checked' => $Config->getValue(),
  263. 'data' => $data
  264. ));
  265. $fieldHtml = $inputField->draw();
  266. break;
  267. }
  268. }
  269. else {
  270. switch(true){
  271. case (stristr($function, 'tep_cfg_select_option')):
  272. $type = 'radio';
  273. $function = str_replace(
  274. 'tep_cfg_select_option',
  275. 'tep_cfg_select_option_elements',
  276. $function
  277. );
  278. break;
  279. case (stristr($function, 'tep_cfg_pull_down_order_statuses')):
  280. $type = 'drop';
  281. $function = str_replace(
  282. 'tep_cfg_pull_down_order_statuses',
  283. 'tep_cfg_pull_down_order_statuses_element',
  284. $function
  285. );
  286. break;
  287. case (stristr($function, 'tep_cfg_pull_down_zone_classes')):
  288. $type = 'drop';
  289. $function = str_replace(
  290. 'tep_cfg_pull_down_zone_classes',
  291. 'tep_cfg_pull_down_zone_classes_element',
  292. $function
  293. );
  294. break;
  295. case (stristr($function, 'tep_cfg_select_multioption')):
  296. case (stristr($function, '_selectOptions')):
  297. $type = 'checkbox';
  298. $function = str_replace(
  299. array(
  300. 'tep_cfg_select_multioption',
  301. '_selectOptions'
  302. ),
  303. 'tep_cfg_select_multioption_element',
  304. $function
  305. );
  306. break;
  307. }
  308. eval('$inputField = ' . $function . "'" . $Config->getValue() . "', '" . $Config->getKey() . "');");
  309. if (is_object($inputField)){
  310. $inputField->addClass('notEdited');
  311. if ($type == 'checkbox'){
  312. $inputField->setName($fieldName . '[' . $Config->getKey() . '][]');
  313. }
  314. else {
  315. $inputField->setName($fieldName . '[' . $Config->getKey() . ']');
  316. }
  317. if ($disable === true){
  318. $inputField->disable(true);
  319. }
  320. $fieldHtml = $inputField->draw();
  321. }
  322. elseif (substr($inputField, 0, 3) == '<br') {
  323. $fieldHtml = substr($inputField, 4) . '<span style="display:none">' . $function . '</span>';
  324. }
  325. else {
  326. $fieldHtml = $inputField . '<span style="display:none">' . $function . '</span>';
  327. }
  328. }
  329. }
  330. else {
  331. $inputField = htmlBase::newElement('input')
  332. ->addClass('notEdited')
  333. ->setName($fieldName . '[' . $Config->getKey() . ']')
  334. ->val($Config->getValue())
  335. ->css(array(
  336. 'width' => '100%'
  337. ));
  338. if ($disable === true){
  339. $inputField->disable(true);
  340. }
  341. $fieldHtml = $inputField->draw();
  342. }
  343. if ($fieldHtml == 'NOTHING WORKED'){
  344. ob_start();
  345. echo '<pre>';
  346. print_r($Config);
  347. echo '</pre>';
  348. $fieldHtml .= '<br>' . ob_get_contents();
  349. ob_end_clean();
  350. }
  351. return $fieldHtml;
  352. }
  353. }