1
0

BxDolStudioOptions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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('settings'));
  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. $sType = 'settings';
  145. $mixedFull = false;
  146. $aOptionsExclude = ['sys_revision', 'sys_cron_time', 'sys_eq_time', 'sys_push_queue_time'];
  147. $aTypes = [];
  148. $this->oDb->getTypes(['type' => 'all'], $aTypes, false);
  149. if(empty($aTypes) || !is_array($aTypes))
  150. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  151. $aListTypes = [];
  152. foreach($aTypes as $aType) {
  153. $aCategories = [];
  154. $this->oDb->getCategories(['type' => 'by_type_id_key_name', 'value' => $aType['id']], $aCategories, false);
  155. if(empty($aCategories) || !is_array($aCategories))
  156. continue;
  157. $aListCategories = [];
  158. foreach($aCategories as $aCategory) {
  159. $aOptions = [];
  160. $this->oDb->getOptions(['type' => 'by_category_id', 'value' => $aCategory['id'], 'for_export' => 1, 'exclude' => $aOptionsExclude], $aOptions, false);
  161. if(empty($aOptions) || !is_array($aOptions))
  162. continue;
  163. $aListCategories[] = [
  164. 'name' => $aCategory['name'],
  165. 'options' => $aOptions
  166. ];
  167. }
  168. $aListTypes[] = [
  169. 'name' => $aType['name'],
  170. 'categories' => $aListCategories
  171. ];
  172. }
  173. $iNow = time();
  174. $sContent = json_encode([
  175. 'meta' => [
  176. 'version' => bx_get_ver(),
  177. 'type' => $sType,
  178. 'full' => $mixedFull,
  179. 'date' => date('D, d M Y H:i:s', $iNow),
  180. ],
  181. 'data' => [
  182. 'types' => $aListTypes
  183. ]
  184. ]);
  185. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  186. header("Content-type: application/json");
  187. header("Content-Length: " . strlen($sContent));
  188. header("Content-Disposition: attachment; filename=\"" . $sType . ($mixedFull ? "_f" : "") . "_" . date('d_m_Y', $iNow) . ".json\"");
  189. echo $sContent;
  190. exit;
  191. }
  192. public function selectMix($sName)
  193. {
  194. $this->oDb->updateMixes(['active' => 0], [
  195. 'type' => $this->sType,
  196. 'category' => is_string($this->sCategory) ? $this->sCategory : '',
  197. 'active' => 1
  198. ]);
  199. $aResult = [];
  200. if($sName == BX_DOL_STUDIO_STG_MIX_SYSTEM || $this->oDb->updateMixes(['active' => 1], ['name' => $sName])) {
  201. $this->clearCache();
  202. $aResult = ['eval' => $this->getJsObject() . '.onMixSelect(oData);'];
  203. }
  204. else
  205. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  206. return $aResult;
  207. }
  208. public function exportMix($iId)
  209. {
  210. $aMix = [];
  211. $this->oDb->getMixes(['type' => 'by_id', 'value' => $iId], $aMix, false);
  212. if(empty($aMix) || !is_array($aMix))
  213. return array('message' => _t('_adm_stg_err_cannot_perform'));
  214. return [
  215. 'url' => bx_append_url_params($this->sBaseUrl, [
  216. $this->sParamPrefix . '_action' => 'download-mix',
  217. $this->sParamPrefix . '_value' => $iId,
  218. ]),
  219. 'eval' => $this->getJsObject() . '.onMixExport(oData);'
  220. ];
  221. }
  222. public function downloadMix($iId)
  223. {
  224. $aMix = [];
  225. $this->oDb->getMixes(['type' => 'by_id', 'value' => $iId], $aMix, false);
  226. if(empty($aMix) || !is_array($aMix))
  227. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  228. $aOptions = [];
  229. $this->oDb->getMixesOptions(['type' => 'by_mix_id_pair_option_value', 'value' => $iId, 'for_export' => 1], $aOptions, false);
  230. $sContent = json_encode([
  231. 'mix' => [
  232. 'type' => $aMix['type'],
  233. 'category' => $aMix['category'],
  234. 'name' => $aMix['name'],
  235. 'title' => $aMix['title']
  236. ],
  237. 'options' => $aOptions
  238. ]);
  239. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  240. header("Content-type: application/json");
  241. header("Content-Length: " . strlen($sContent));
  242. header("Content-Disposition: attachment; filename=\"". strtolower($aMix['name']) . ".json\"");
  243. echo $sContent;
  244. exit;
  245. }
  246. public function publishMix($iId)
  247. {
  248. $aResult = [];
  249. if($this->oDb->updateMixes(['published' => 1], ['id' => $iId])) {
  250. $this->clearCache();
  251. $aResult = ['eval' => $this->getJsObject() . '.onMixPublish(oData);'];
  252. }
  253. else
  254. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  255. return $aResult;
  256. }
  257. public function hideMix($iId)
  258. {
  259. $aResult = [];
  260. if($this->oDb->updateMixes(['published' => 0], ['id' => $iId])) {
  261. $this->clearCache();
  262. $aResult = ['eval' => $this->getJsObject() . '.onMixHide(oData);'];
  263. }
  264. else
  265. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  266. return $aResult;
  267. }
  268. public function deleteMix($iId)
  269. {
  270. $aResult = [];
  271. if($this->oDb->deleteMixesOptions(['mix_id' => $iId]) && $this->oDb->deleteMixes(['id' => $iId])) {
  272. $this->clearCache();
  273. $aResult = ['eval' => $this->getJsObject() . '.onMixDelete(oData);'];
  274. }
  275. else
  276. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  277. return $aResult;
  278. }
  279. public function saveChanges(&$oForm)
  280. {
  281. $iMixId = $oForm->getCleanValue('mix_id');
  282. $aCategories = explode(',', $oForm->getCleanValue('categories'));
  283. $sEvalRenewToken = $this->getJsObject() . ".onSubmitted('" . $oForm->getId() . "', '" . $oForm->getCsrfToken() . "', oData);";
  284. foreach ($aCategories as $sCategory) {
  285. $aOptions = [];
  286. $iOptions = $this->oDb->getOptions(['type' => 'by_category_name_full', 'value' => $sCategory], $aOptions);
  287. $aData = [];
  288. foreach($aOptions as $aOption) {
  289. $aData[$aOption['name']] = $this->getSubmittedValue($aOption, $oForm);
  290. if($aData[$aOption['name']] === false && !empty($this->sErrorMessage)) {
  291. $this->sCategory = $sCategory;
  292. return bx_get_js_result([
  293. 'code' => 1,
  294. 'message' => _t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($this->sErrorMessage)),
  295. 'translate' => false,
  296. 'eval' => $sEvalRenewToken
  297. ]);
  298. }
  299. if(!empty($aOption['check'])) {
  300. $sCheckerHelper = '';
  301. if(!empty($aOption['type_name']) && BxDolRequest::serviceExists($aOption['type_name'], 'get_settings_checker_helper'))
  302. $sCheckerHelper = BxDolService::call($aOption['type_name'], 'get_settings_checker_helper');
  303. if($sCheckerHelper == '') {
  304. bx_import('BxDolStudioForm');
  305. $sCheckerHelper = 'BxDolStudioFormCheckerHelper';
  306. }
  307. $oChecker = new $sCheckerHelper();
  308. $aCheckFunction = [$oChecker, 'check' . bx_gen_method_name($aOption['check'])];
  309. $aCheckFunctionParams = [$aData[$aOption['name']]];
  310. if(!empty($aOption['check_params']))
  311. $aCheckFunctionParams = array_merge($aCheckFunctionParams, array_values(unserialize($aOption['check_params'])));
  312. if(is_callable($aCheckFunction) && !call_user_func_array($aCheckFunction, $aCheckFunctionParams)) {
  313. $this->sCategory = $sCategory;
  314. return bx_get_js_result([
  315. 'code' => 2,
  316. 'message' => _t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($aOption['check_error'])),
  317. 'translate' => false,
  318. 'eval' => $sEvalRenewToken
  319. ]);
  320. }
  321. }
  322. if(isset($aData[$aOption['name']]))
  323. $aData[$aOption['name']] = $this->getProcessedValue($aOption, $aData[$aOption['name']]);
  324. else
  325. $aData[$aOption['name']] = $this->getEmptyValue($aOption);
  326. $mixedValue = $this->oDb->getParam($aOption['name']);
  327. if($this->oDb->setParam($aOption['name'], $aData[$aOption['name']], $iMixId)) {
  328. $aCategoryInfo = [];
  329. $this->oDb->getCategories(['type' => 'by_name', 'value' => $sCategory], $aCategoryInfo, false);
  330. bx_alert('system', 'save_setting', 0, 0, [
  331. 'category' => $aCategoryInfo,
  332. 'option' => $aOption['name'],
  333. 'value' => $aData[$aOption['name']],
  334. 'value_prior' => $mixedValue
  335. ]);
  336. }
  337. }
  338. }
  339. $this->clearCache();
  340. return bx_get_js_result([
  341. 'code' => 0,
  342. 'message' => '_adm_stg_scs_save',
  343. 'eval' => $sEvalRenewToken
  344. ]);
  345. }
  346. protected function isReadOnly()
  347. {
  348. $bMix = !empty($this->aMix);
  349. return (!$bMix && $this->bReadOnly) || ($bMix && (int)$this->aMix['editable'] == 0);
  350. }
  351. protected function getSubmittedValue($aOption, &$oForm)
  352. {
  353. $mixedValue = '';
  354. switch($aOption['type']) {
  355. case 'image':
  356. $mixedValue = (int)getParam($aOption['name']);
  357. $mixedIds = $oForm->getCleanValue($aOption['name']);
  358. if(empty($mixedIds)) {
  359. if(!empty($mixedValue))
  360. $mixedValue = 0;
  361. break;
  362. }
  363. $oStorage = BxDolStorage::getObjectInstance($this->sStorage);
  364. if(!$oStorage)
  365. break;
  366. //--- Concatenation integer values as strings is required to get unique content id
  367. $iContentId = (int)($aOption['id'] . (int)$oForm->getCleanValue('mix_id'));
  368. $iImageId = is_array($mixedIds) ? (int)array_shift($mixedIds) : (int)$mixedIds;
  369. $oStorage->updateGhostsContentId($iImageId, false, $iContentId);
  370. $mixedValue = $iImageId;
  371. break;
  372. default:
  373. $mixedValue = $oForm->getCleanValue($aOption['name']);
  374. }
  375. return $mixedValue;
  376. }
  377. protected function getProcessedValue($aOption, $mixedValue)
  378. {
  379. $sMethod = 'processCustomValue' . bx_gen_method_name(trim(str_replace($this->sType, '', $aOption['name']), '_'));
  380. if(method_exists($this, $sMethod))
  381. return $this->$sMethod($aOption, $mixedValue);
  382. if(is_array($mixedValue))
  383. $mixedValue = implode(',', $mixedValue);
  384. return $mixedValue;
  385. }
  386. protected function getEmptyValue($aOption)
  387. {
  388. $mixedValue = '';
  389. switch($aOption['type']) {
  390. case 'digit':
  391. $mixedValue = 0;
  392. break;
  393. case 'select':
  394. if (BxDolService::isSerializedService($aOption['extra']))
  395. $aValues = BxDolService::callSerialized($aOption['extra']);
  396. else
  397. $aValues = explode(',', $aOption['extra']);
  398. $mixedValue = $aValues[0];
  399. break;
  400. case 'text':
  401. case 'checkbox':
  402. case 'file':
  403. $mixedValue = "";
  404. break;
  405. }
  406. return $mixedValue;
  407. }
  408. protected function clearCache()
  409. {
  410. BxDolDb::getInstance()->cacheParams(true, true);
  411. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  412. $oCacheUtilities->clear('db');
  413. $oCacheUtilities->clear('css');
  414. }
  415. protected function getCustomValueCurrencySign($aItem, $mixedValue)
  416. {
  417. return htmlspecialchars($mixedValue);
  418. }
  419. protected function processCustomValueCurrencySign($aItem, $mixedValue)
  420. {
  421. return htmlspecialchars_decode($mixedValue);
  422. }
  423. }
  424. /** @} */