BxDolStudioSettings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_MIX_SYSTEM', 'system');
  19. define('BX_DOL_STUDIO_STG_MIX_DEFAULT', BX_DOL_STUDIO_STG_MIX_SYSTEM);
  20. class BxDolStudioSettings extends BxTemplStudioPage
  21. {
  22. protected $sType;
  23. protected $sCategory;
  24. protected $aCategories;
  25. protected $aCustomCategories;
  26. protected $bReadOnly; //--- Enable/Disable ReadOnly mode for system settings. Note. It doesn't affect Editable mixes.
  27. protected $bMixes; //--- Enable/Disable mixes feature.
  28. protected $sMix;
  29. protected $aMix;
  30. protected $sStorage;
  31. protected $sTranscoder;
  32. protected $sErrorMessage;
  33. function __construct($sType = '', $mixedCategory = '')
  34. {
  35. parent::__construct('settings');
  36. $this->oDb = new BxDolStudioSettingsQuery();
  37. $this->sType = BX_DOL_STUDIO_STG_TYPE_DEFAULT;
  38. if(!empty($sType) && is_string($sType))
  39. $this->sType = $sType;
  40. $this->sCategory = '';
  41. if(!empty($mixedCategory)) {
  42. if(is_array($mixedCategory))
  43. $this->sCategory = $mixedCategory;
  44. else if(is_string($mixedCategory)) {
  45. $this->sCategory = json_decode($mixedCategory);
  46. if(empty($this->sCategory))
  47. $this->sCategory = $mixedCategory;
  48. }
  49. }
  50. $this->bReadOnly = false;
  51. $this->bMixes = false;
  52. $this->sMix = '';
  53. $this->aMix = array();
  54. $this->sStorage = 'sys_images_custom';
  55. $this->sTranscoder = 'sys_custom_images';
  56. $this->sErrorMessage = '';
  57. //--- Check actions ---//
  58. if(($sAction = bx_get('stg_action')) !== false) {
  59. $sAction = bx_process_input($sAction);
  60. $sValue = '';
  61. if(bx_get('stg_value') !== false)
  62. $sValue = bx_process_input(bx_get('stg_value'));
  63. $aResult = array('code' => 0, 'message' => '');
  64. if(!empty($sAction)) {
  65. switch($sAction) {
  66. case 'select-mix':
  67. $aResult = array_merge($aResult, $this->selectMix($sValue));
  68. break;
  69. case 'create-mix':
  70. $aResult = array_merge($aResult, $this->getPopupCodeCreateMix());
  71. break;
  72. case 'import-mix':
  73. $aResult = array_merge($aResult, $this->getPopupCodeImportMix());
  74. break;
  75. case 'export-mix':
  76. $aResult = array_merge($aResult, $this->exportMix((int)$sValue));
  77. break;
  78. case 'download-mix':
  79. $aResult = array_merge($aResult, $this->downloadMix((int)$sValue));
  80. break;
  81. case 'delete-mix':
  82. $aResult = array_merge($aResult, $this->deleteMix((int)$sValue));
  83. break;
  84. case 'get-page-by-type':
  85. $this->sType = $sValue;
  86. $aResult['content'] = $this->getPageCode();
  87. break;
  88. }
  89. echo json_encode($aResult);
  90. }
  91. exit;
  92. }
  93. }
  94. public function enableReadOnly($bReadOnly = true)
  95. {
  96. $this->bReadOnly = $bReadOnly;
  97. }
  98. public function enableMixes($bMixes = true)
  99. {
  100. $this->bMixes = $bMixes;
  101. }
  102. public function selectMix($sName)
  103. {
  104. $this->oDb->updateMixes(array('active' => 0), array(
  105. 'type' => $this->sType,
  106. 'category' => is_string($this->sCategory) ? $this->sCategory : '',
  107. 'active' => 1
  108. ));
  109. $aResult = array();
  110. if($sName == BX_DOL_STUDIO_STG_MIX_SYSTEM || $this->oDb->updateMixes(array('active' => 1), array('name' => $sName))) {
  111. $this->clearCache();
  112. $aResult = array('eval' => $this->getPageJsObject() . '.onMixSelect(oData);');
  113. }
  114. else
  115. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  116. return $aResult;
  117. }
  118. public function exportMix($iId)
  119. {
  120. $aMix = array();
  121. $this->oDb->getMixes(array('type' => 'by_id', 'value' => $iId), $aMix, false);
  122. if(empty($aMix) || !is_array($aMix))
  123. return array('message' => _t('_adm_stg_err_cannot_perform'));
  124. return array(
  125. 'url' => bx_append_url_params(BX_DOL_URL_STUDIO . 'settings.php', array(
  126. 'stg_action' => 'download-mix',
  127. 'stg_value' => $iId,
  128. )),
  129. 'eval' => $this->getPageJsObject() . '.onMixExport(oData);'
  130. );
  131. }
  132. public function downloadMix($iId)
  133. {
  134. $aMix = array();
  135. $this->oDb->getMixes(array('type' => 'by_id', 'value' => $iId), $aMix, false);
  136. if(empty($aMix) || !is_array($aMix))
  137. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  138. $aOptions = array();
  139. $this->oDb->getMixesOptions(array('type' => 'by_mix_id_pair_option_value', 'value' => $iId, 'for_export' => 1), $aOptions, false);
  140. $aContent = array(
  141. 'mix' => array(
  142. 'type' => $aMix['type'],
  143. 'category' => $aMix['category'],
  144. 'name' => $aMix['name'],
  145. 'title' => $aMix['title']
  146. ),
  147. 'options' => $aOptions
  148. );
  149. $sContent = json_encode($aContent);
  150. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  151. header("Content-type: application/json");
  152. header("Content-Length: " . strlen($sContent));
  153. header("Content-Disposition: attachment; filename=\"". strtolower($aMix['name']) . ".json\"");
  154. echo $sContent;
  155. exit;
  156. }
  157. public function deleteMix($iId)
  158. {
  159. $aResult = array();
  160. if($this->oDb->deleteMixesOptions(array('mix_id' => $iId)) && $this->oDb->deleteMixes(array('id' => $iId))) {
  161. $this->clearCache();
  162. $aResult = array('eval' => $this->getPageJsObject() . '.onMixDelete(oData);');
  163. }
  164. else
  165. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  166. return $aResult;
  167. }
  168. function saveChanges(&$oForm)
  169. {
  170. $iMixId = $oForm->getCleanValue('mix_id');
  171. $aCategories = explode(',', $oForm->getCleanValue('categories'));
  172. $sEvalRenewToken = '$("#' . $oForm->getId() . ' input[name=csrf_token]").val("' . $oForm->getCsrfToken() . '");';
  173. foreach ($aCategories as $sCategory) {
  174. $aOptions = array();
  175. $iOptions = $this->oDb->getOptions(array('type' => 'by_category_name_full', 'value' => $sCategory), $aOptions);
  176. $aData = array();
  177. foreach($aOptions as $aOption) {
  178. $aData[$aOption['name']] = $this->getSubmittedValue($aOption, $oForm);
  179. if($aData[$aOption['name']] === false && !empty($this->sErrorMessage)) {
  180. $this->sCategory = $sCategory;
  181. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($this->sErrorMessage)), false, false, '', $sEvalRenewToken);
  182. }
  183. if(!empty($aOption['check'])) {
  184. $sCheckerHelper = '';
  185. if(!empty($aOption['type_name']) && BxDolRequest::serviceExists($aOption['type_name'], 'get_settings_checker_helper'))
  186. $sCheckerHelper = BxDolService::call($aOption['type_name'], 'get_settings_checker_helper');
  187. if($sCheckerHelper == '') {
  188. bx_import('BxDolStudioForm');
  189. $sCheckerHelper = 'BxDolStudioFormCheckerHelper';
  190. }
  191. $oChecker = new $sCheckerHelper();
  192. $aCheckFunction = array($oChecker, 'check' . bx_gen_method_name($aOption['check']));
  193. $aCheckFunctionParams = array($aData[$aOption['name']]);
  194. if(!empty($aOption['check_params']))
  195. $aCheckFunctionParams = array_merge($aCheckFunctionParams, unserialize($aOption['check_params']));
  196. if(is_callable($aCheckFunction) && !call_user_func_array($aCheckFunction, $aCheckFunctionParams)) {
  197. $this->sCategory = $sCategory;
  198. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($aOption['check_error'])), false, false, '', $sEvalRenewToken);
  199. }
  200. }
  201. if(isset($aData[$aOption['name']]))
  202. $aData[$aOption['name']] = $this->getProcessedValue($aOption, $aData[$aOption['name']]);
  203. else
  204. $aData[$aOption['name']] = $this->getEmptyValue($aOption);
  205. if($this->oDb->setParam($aOption['name'], $aData[$aOption['name']], $iMixId)) {
  206. $aCategoryInfo = array();
  207. $this->oDb->getCategories(array('type' => 'by_name', 'value' => $sCategory), $aCategoryInfo, false);
  208. bx_alert('system', 'save_setting', 0, 0, array('category' => $aCategoryInfo, 'option' => $aOption['name'], 'value' => $aData[$aOption['name']]));
  209. }
  210. }
  211. }
  212. return $this->getJsResult('_adm_stg_scs_save', true, false, '', $sEvalRenewToken);
  213. }
  214. protected function isReadOnly()
  215. {
  216. $bMix = !empty($this->aMix);
  217. return (!$bMix && $this->bReadOnly) || ($bMix && (int)$this->aMix['editable'] == 0);
  218. }
  219. protected function getSubmittedValue($aOption, &$oForm)
  220. {
  221. $mixedValue = '';
  222. switch($aOption['type']) {
  223. case 'image':
  224. $mixedValue = (int)getParam($aOption['name']);
  225. $aIds = $oForm->getCleanValue($aOption['name']);
  226. if(empty($aIds))
  227. break;
  228. $oStorage = BxDolStorage::getObjectInstance($this->sStorage);
  229. if(!$oStorage)
  230. break;
  231. //--- Concatenation integer values as strings is required to get unique content id
  232. $iContentId = (int)($aOption['id'] . (int)$oForm->getCleanValue('mix_id'));
  233. foreach($aIds as $iId) {
  234. $oStorage->updateGhostsContentId($iId, false, $iContentId);
  235. $mixedValue = $iId;
  236. }
  237. break;
  238. default:
  239. $mixedValue = $oForm->getCleanValue($aOption['name']);
  240. }
  241. return $mixedValue;
  242. }
  243. protected function getProcessedValue($aOption, $mixedValue)
  244. {
  245. if(is_array($mixedValue))
  246. $mixedValue = implode(',', $mixedValue);
  247. return $mixedValue;
  248. }
  249. protected function getEmptyValue($aOption)
  250. {
  251. $mixedValue = '';
  252. switch($aOption['type']) {
  253. case 'digit':
  254. $mixedValue = 0;
  255. break;
  256. case 'select':
  257. if (BxDolService::isSerializedService($aOption['extra']))
  258. $aValues = BxDolService::callSerialized($aOption['extra']);
  259. else
  260. $aValues = explode(',', $aOption['extra']);
  261. $mixedValue = $aValues[0];
  262. break;
  263. case 'text':
  264. case 'checkbox':
  265. case 'file':
  266. $mixedValue = "";
  267. break;
  268. }
  269. return $mixedValue;
  270. }
  271. protected function clearCache()
  272. {
  273. BxDolDb::getInstance()->cacheParams(true, true);
  274. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  275. $oCacheUtilities->clear('db');
  276. $oCacheUtilities->clear('css');
  277. }
  278. }
  279. /** @} */