BxDolStudioOptions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. define('BX_DOL_STUDIO_STG_TYPE_SYSTEM', 'system');
  10. define('BX_DOL_STUDIO_STG_TYPE_DEFAULT', BX_DOL_STUDIO_STG_TYPE_SYSTEM);
  11. define('BX_DOL_STUDIO_STG_GROUP_SYSTEM', 'system');
  12. define('BX_DOL_STUDIO_STG_GROUP_MODULES', 'modules');
  13. define('BX_DOL_STUDIO_STG_GROUP_LANGUAGES', 'languages');
  14. define('BX_DOL_STUDIO_STG_GROUP_TEMPLATES', 'templates');
  15. define('BX_DOL_STUDIO_STG_CATEGORY_SYSTEM', 'system');
  16. define('BX_DOL_STUDIO_STG_CATEGORY_LANGUAGES', 'languages');
  17. define('BX_DOL_STUDIO_STG_CATEGORY_TEMPLATES', 'templates');
  18. define('BX_DOL_STUDIO_STG_CATEGORY_AUDIT', 'audit');
  19. define('BX_DOL_STUDIO_STG_MIX_SYSTEM', 'system');
  20. define('BX_DOL_STUDIO_STG_MIX_DEFAULT', BX_DOL_STUDIO_STG_MIX_SYSTEM);
  21. class BxDolStudioOptions extends BxDol
  22. {
  23. protected $oDb;
  24. protected $sParamPrefix;
  25. protected $sBaseUrl;
  26. protected $sType;
  27. protected $sCategory;
  28. protected $aCategories;
  29. protected $aCustomCategories;
  30. protected $bManage; //--- Enable/Disable Manage panel for system settings.
  31. protected $bReadOnly; //--- Enable/Disable ReadOnly mode for system settings. Note. It doesn't affect Editable mixes.
  32. protected $bMixes; //--- Enable/Disable mixes feature.
  33. protected $sMix;
  34. protected $aMix;
  35. protected $sStorage;
  36. protected $sTranscoder;
  37. protected $sErrorMessage;
  38. function __construct($sType = '', $mixedCategory = '', $sMix = '')
  39. {
  40. parent::__construct();
  41. $this->oDb = new BxDolStudioOptionsQuery();
  42. $this->sParamPrefix = 'opt';
  43. $this->sBaseUrl = BX_DOL_URL_STUDIO . 'options.php';
  44. $this->sType = BX_DOL_STUDIO_STG_TYPE_DEFAULT;
  45. if(!empty($sType) && is_string($sType))
  46. $this->sType = $sType;
  47. $this->sCategory = '';
  48. if(!empty($mixedCategory)) {
  49. if(is_array($mixedCategory))
  50. $this->sCategory = $mixedCategory;
  51. else if(is_string($mixedCategory)) {
  52. $this->sCategory = json_decode($mixedCategory);
  53. if(empty($this->sCategory))
  54. $this->sCategory = $mixedCategory;
  55. }
  56. }
  57. $this->bManage = false;
  58. $this->bReadOnly = false;
  59. $this->bMixes = false;
  60. $this->sMix = $sMix;
  61. $this->aMix = [];
  62. $this->sStorage = 'sys_images_custom';
  63. $this->sTranscoder = 'sys_custom_images';
  64. $this->sErrorMessage = '';
  65. }
  66. public function checkAction()
  67. {
  68. $sAction = bx_get($this->sParamPrefix . '_action');
  69. if($sAction === false)
  70. return false;
  71. $sAction = bx_process_input($sAction);
  72. $sValue = '';
  73. if(bx_get($this->sParamPrefix . '_value') !== false)
  74. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  75. $aResult = ['code' => 0, 'message' => ''];
  76. switch($sAction) {
  77. case 'import':
  78. $aResult = array_merge($aResult, $this->getPopupCodeImport());
  79. break;
  80. case 'export':
  81. $aResult = array_merge($aResult, $this->export());
  82. break;
  83. case 'download':
  84. $aResult = array_merge($aResult, $this->download());
  85. break;
  86. case 'select-mix':
  87. $aResult = array_merge($aResult, $this->selectMix($sValue));
  88. break;
  89. case 'create-mix':
  90. $aResult = array_merge($aResult, $this->getPopupCodeCreateMix());
  91. break;
  92. case 'import-mix':
  93. $aResult = array_merge($aResult, $this->getPopupCodeImportMix());
  94. break;
  95. case 'export-mix':
  96. $aResult = array_merge($aResult, $this->exportMix((int)$sValue));
  97. break;
  98. case 'download-mix':
  99. $aResult = array_merge($aResult, $this->downloadMix((int)$sValue));
  100. break;
  101. case 'publish-mix':
  102. $aResult = array_merge($aResult, $this->publishMix((int)$sValue));
  103. break;
  104. case 'hide-mix':
  105. $aResult = array_merge($aResult, $this->hideMix((int)$sValue));
  106. break;
  107. case 'delete-mix':
  108. $aResult = array_merge($aResult, $this->deleteMix((int)$sValue));
  109. break;
  110. case 'get-page-by-type':
  111. $this->sType = $sValue;
  112. $aResult['content'] = $this->getPageCode();
  113. break;
  114. }
  115. return $aResult;
  116. }
  117. public function getType()
  118. {
  119. return $this->sType;
  120. }
  121. public function enableManage($bManage = true)
  122. {
  123. $this->bManage = $bManage;
  124. }
  125. public function enableReadOnly($bReadOnly = true)
  126. {
  127. $this->bReadOnly = $bReadOnly;
  128. }
  129. public function enableMixes($bMixes = true)
  130. {
  131. $this->bMixes = $bMixes;
  132. }
  133. public function export()
  134. {
  135. return [
  136. 'url' => bx_append_url_params($this->sBaseUrl, [
  137. $this->sParamPrefix . '_action' => 'download',
  138. ]),
  139. 'eval' => $this->getJsObject() . '.onExport(oData);'
  140. ];
  141. }
  142. public function download()
  143. {
  144. $aTypes = [];
  145. $this->oDb->getTypes(['type' => 'all'], $aTypes, false);
  146. if(empty($aTypes) || !is_array($aTypes))
  147. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  148. $aListTypes = [];
  149. foreach($aTypes as $aType) {
  150. $aCategories = [];
  151. $this->oDb->getCategories(['type' => 'by_type_id_key_name', 'value' => $aType['id']], $aCategories, false);
  152. if(empty($aCategories) || !is_array($aCategories))
  153. continue;
  154. $aListCategories = [];
  155. foreach($aCategories as $aCategory) {
  156. $aOptions = [];
  157. $this->oDb->getOptions(['type' => 'by_category_id', 'value' => $aCategory['id'], 'for_export' => 1], $aOptions, false);
  158. if(empty($aOptions) || !is_array($aOptions))
  159. continue;
  160. $aListCategories[] = [
  161. 'name' => $aCategory['name'],
  162. 'options' => $aOptions
  163. ];
  164. }
  165. $aListTypes[] = [
  166. 'name' => $aType['name'],
  167. 'categories' => $aListCategories
  168. ];
  169. }
  170. $iNow = time();
  171. $sContent = json_encode([
  172. 'meta' => [
  173. 'version' => bx_get_ver(),
  174. 'date' => date('D, d M Y H:i:s', $iNow)
  175. ],
  176. 'types' => $aListTypes
  177. ]);
  178. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  179. header("Content-type: application/json");
  180. header("Content-Length: " . strlen($sContent));
  181. header("Content-Disposition: attachment; filename=\"settings_". date('d_m_Y', $iNow) . ".json\"");
  182. echo $sContent;
  183. exit;
  184. }
  185. public function selectMix($sName)
  186. {
  187. $this->oDb->updateMixes(['active' => 0], [
  188. 'type' => $this->sType,
  189. 'category' => is_string($this->sCategory) ? $this->sCategory : '',
  190. 'active' => 1
  191. ]);
  192. $aResult = [];
  193. if($sName == BX_DOL_STUDIO_STG_MIX_SYSTEM || $this->oDb->updateMixes(['active' => 1], ['name' => $sName])) {
  194. $this->clearCache();
  195. $aResult = ['eval' => $this->getJsObject() . '.onMixSelect(oData);'];
  196. }
  197. else
  198. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  199. return $aResult;
  200. }
  201. public function exportMix($iId)
  202. {
  203. $aMix = [];
  204. $this->oDb->getMixes(['type' => 'by_id', 'value' => $iId], $aMix, false);
  205. if(empty($aMix) || !is_array($aMix))
  206. return array('message' => _t('_adm_stg_err_cannot_perform'));
  207. return [
  208. 'url' => bx_append_url_params($this->sBaseUrl, [
  209. $this->sParamPrefix . '_action' => 'download-mix',
  210. $this->sParamPrefix . '_value' => $iId,
  211. ]),
  212. 'eval' => $this->getJsObject() . '.onMixExport(oData);'
  213. ];
  214. }
  215. public function downloadMix($iId)
  216. {
  217. $aMix = [];
  218. $this->oDb->getMixes(['type' => 'by_id', 'value' => $iId], $aMix, false);
  219. if(empty($aMix) || !is_array($aMix))
  220. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  221. $aOptions = [];
  222. $this->oDb->getMixesOptions(['type' => 'by_mix_id_pair_option_value', 'value' => $iId, 'for_export' => 1], $aOptions, false);
  223. $sContent = json_encode([
  224. 'mix' => [
  225. 'type' => $aMix['type'],
  226. 'category' => $aMix['category'],
  227. 'name' => $aMix['name'],
  228. 'title' => $aMix['title']
  229. ],
  230. 'options' => $aOptions
  231. ]);
  232. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  233. header("Content-type: application/json");
  234. header("Content-Length: " . strlen($sContent));
  235. header("Content-Disposition: attachment; filename=\"". strtolower($aMix['name']) . ".json\"");
  236. echo $sContent;
  237. exit;
  238. }
  239. public function publishMix($iId)
  240. {
  241. $aResult = [];
  242. if($this->oDb->updateMixes(['published' => 1], ['id' => $iId])) {
  243. $this->clearCache();
  244. $aResult = ['eval' => $this->getJsObject() . '.onMixPublish(oData);'];
  245. }
  246. else
  247. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  248. return $aResult;
  249. }
  250. public function hideMix($iId)
  251. {
  252. $aResult = [];
  253. if($this->oDb->updateMixes(['published' => 0], ['id' => $iId])) {
  254. $this->clearCache();
  255. $aResult = ['eval' => $this->getJsObject() . '.onMixHide(oData);'];
  256. }
  257. else
  258. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  259. return $aResult;
  260. }
  261. public function deleteMix($iId)
  262. {
  263. $aResult = [];
  264. if($this->oDb->deleteMixesOptions(['mix_id' => $iId]) && $this->oDb->deleteMixes(['id' => $iId])) {
  265. $this->clearCache();
  266. $aResult = ['eval' => $this->getJsObject() . '.onMixDelete(oData);'];
  267. }
  268. else
  269. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  270. return $aResult;
  271. }
  272. public function saveChanges(&$oForm)
  273. {
  274. $iMixId = $oForm->getCleanValue('mix_id');
  275. $aCategories = explode(',', $oForm->getCleanValue('categories'));
  276. $sEvalRenewToken = $this->getJsObject() . ".onSubmitted('" . $oForm->getId() . "', '" . $oForm->getCsrfToken() . "', oData);";
  277. foreach ($aCategories as $sCategory) {
  278. $aOptions = [];
  279. $iOptions = $this->oDb->getOptions(['type' => 'by_category_name_full', 'value' => $sCategory], $aOptions);
  280. $aData = [];
  281. foreach($aOptions as $aOption) {
  282. $aData[$aOption['name']] = $this->getSubmittedValue($aOption, $oForm);
  283. if($aData[$aOption['name']] === false && !empty($this->sErrorMessage)) {
  284. $this->sCategory = $sCategory;
  285. return bx_get_js_result([
  286. 'code' => 1,
  287. 'message' => _t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($this->sErrorMessage)),
  288. 'translate' => false,
  289. 'eval' => $sEvalRenewToken
  290. ]);
  291. }
  292. if(!empty($aOption['check'])) {
  293. $sCheckerHelper = '';
  294. if(!empty($aOption['type_name']) && BxDolRequest::serviceExists($aOption['type_name'], 'get_settings_checker_helper'))
  295. $sCheckerHelper = BxDolService::call($aOption['type_name'], 'get_settings_checker_helper');
  296. if($sCheckerHelper == '') {
  297. bx_import('BxDolStudioForm');
  298. $sCheckerHelper = 'BxDolStudioFormCheckerHelper';
  299. }
  300. $oChecker = new $sCheckerHelper();
  301. $aCheckFunction = [$oChecker, 'check' . bx_gen_method_name($aOption['check'])];
  302. $aCheckFunctionParams = [$aData[$aOption['name']]];
  303. if(!empty($aOption['check_params']))
  304. $aCheckFunctionParams = array_merge($aCheckFunctionParams, array_values(unserialize($aOption['check_params'])));
  305. if(is_callable($aCheckFunction) && !call_user_func_array($aCheckFunction, $aCheckFunctionParams)) {
  306. $this->sCategory = $sCategory;
  307. return bx_get_js_result([
  308. 'code' => 2,
  309. 'message' => _t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($aOption['check_error'])),
  310. 'translate' => false,
  311. 'eval' => $sEvalRenewToken
  312. ]);
  313. }
  314. }
  315. if(isset($aData[$aOption['name']]))
  316. $aData[$aOption['name']] = $this->getProcessedValue($aOption, $aData[$aOption['name']]);
  317. else
  318. $aData[$aOption['name']] = $this->getEmptyValue($aOption);
  319. $mixedValue = $this->oDb->getParam($aOption['name']);
  320. if($this->oDb->setParam($aOption['name'], $aData[$aOption['name']], $iMixId)) {
  321. $aCategoryInfo = [];
  322. $this->oDb->getCategories(['type' => 'by_name', 'value' => $sCategory], $aCategoryInfo, false);
  323. bx_alert('system', 'save_setting', 0, 0, [
  324. 'category' => $aCategoryInfo,
  325. 'option' => $aOption['name'],
  326. 'value' => $aData[$aOption['name']],
  327. 'value_prior' => $mixedValue
  328. ]);
  329. }
  330. }
  331. }
  332. $this->clearCache();
  333. return bx_get_js_result([
  334. 'code' => 0,
  335. 'message' => '_adm_stg_scs_save',
  336. 'eval' => $sEvalRenewToken
  337. ]);
  338. }
  339. protected function isReadOnly()
  340. {
  341. $bMix = !empty($this->aMix);
  342. return (!$bMix && $this->bReadOnly) || ($bMix && (int)$this->aMix['editable'] == 0);
  343. }
  344. protected function getSubmittedValue($aOption, &$oForm)
  345. {
  346. $mixedValue = '';
  347. switch($aOption['type']) {
  348. case 'image':
  349. $mixedValue = (int)getParam($aOption['name']);
  350. $mixedIds = $oForm->getCleanValue($aOption['name']);
  351. if(empty($mixedIds)) {
  352. if(!empty($mixedValue))
  353. $mixedValue = 0;
  354. break;
  355. }
  356. $oStorage = BxDolStorage::getObjectInstance($this->sStorage);
  357. if(!$oStorage)
  358. break;
  359. //--- Concatenation integer values as strings is required to get unique content id
  360. $iContentId = (int)($aOption['id'] . (int)$oForm->getCleanValue('mix_id'));
  361. $iImageId = is_array($mixedIds) ? (int)array_shift($mixedIds) : (int)$mixedIds;
  362. $oStorage->updateGhostsContentId($iImageId, false, $iContentId);
  363. $mixedValue = $iImageId;
  364. break;
  365. default:
  366. $mixedValue = $oForm->getCleanValue($aOption['name']);
  367. }
  368. return $mixedValue;
  369. }
  370. protected function getProcessedValue($aOption, $mixedValue)
  371. {
  372. $sMethod = 'processCustomValue' . bx_gen_method_name(trim(str_replace($this->sType, '', $aOption['name']), '_'));
  373. if(method_exists($this, $sMethod))
  374. return $this->$sMethod($aOption, $mixedValue);
  375. if(is_array($mixedValue))
  376. $mixedValue = implode(',', $mixedValue);
  377. return $mixedValue;
  378. }
  379. protected function getEmptyValue($aOption)
  380. {
  381. $mixedValue = '';
  382. switch($aOption['type']) {
  383. case 'digit':
  384. $mixedValue = 0;
  385. break;
  386. case 'select':
  387. if (BxDolService::isSerializedService($aOption['extra']))
  388. $aValues = BxDolService::callSerialized($aOption['extra']);
  389. else
  390. $aValues = explode(',', $aOption['extra']);
  391. $mixedValue = $aValues[0];
  392. break;
  393. case 'text':
  394. case 'checkbox':
  395. case 'file':
  396. $mixedValue = "";
  397. break;
  398. }
  399. return $mixedValue;
  400. }
  401. protected function clearCache()
  402. {
  403. BxDolDb::getInstance()->cacheParams(true, true);
  404. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  405. $oCacheUtilities->clear('db');
  406. $oCacheUtilities->clear('css');
  407. }
  408. protected function getCustomValueCurrencySign($aItem, $mixedValue)
  409. {
  410. return htmlspecialchars($mixedValue);
  411. }
  412. protected function processCustomValueCurrencySign($aItem, $mixedValue)
  413. {
  414. return htmlspecialchars_decode($mixedValue);
  415. }
  416. }
  417. /** @} */