BxDolStudioSettings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. foreach ($aCategories as $sCategory) {
  173. $aOptions = array();
  174. $iOptions = $this->oDb->getOptions(array('type' => 'by_category_name_full', 'value' => $sCategory), $aOptions);
  175. $aData = array();
  176. foreach($aOptions as $aOption) {
  177. $aData[$aOption['name']] = $this->getSubmittedValue($aOption, $oForm);
  178. if($aData[$aOption['name']] === false && !empty($this->sErrorMessage)) {
  179. $this->sCategory = $sCategory;
  180. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($this->sErrorMessage)), false);
  181. }
  182. if(!empty($aOption['check'])) {
  183. $sCheckerHelper = '';
  184. if(!empty($aOption['type_name']) && BxDolRequest::serviceExists($aOption['type_name'], 'get_settings_checker_helper'))
  185. $sCheckerHelper = BxDolService::call($aOption['type_name'], 'get_settings_checker_helper');
  186. if($sCheckerHelper == '') {
  187. bx_import('BxDolStudioForm');
  188. $sCheckerHelper = 'BxDolStudioFormCheckerHelper';
  189. }
  190. $oChecker = new $sCheckerHelper();
  191. $aCheckFunction = array($oChecker, 'check' . bx_gen_method_name($aOption['check']));
  192. $aCheckFunctionParams = array($aData[$aOption['name']]);
  193. if(!empty($aOption['check_params']))
  194. $aCheckFunctionParams = array_merge($aCheckFunctionParams, unserialize($aOption['check_params']));
  195. if(is_callable($aCheckFunction) && !call_user_func_array($aCheckFunction, $aCheckFunctionParams)) {
  196. $this->sCategory = $sCategory;
  197. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($aOption['check_error'])), false);
  198. }
  199. }
  200. if(isset($aData[$aOption['name']]))
  201. $aData[$aOption['name']] = $this->getProcessedValue($aOption, $aData[$aOption['name']]);
  202. else
  203. $aData[$aOption['name']] = $this->getEmptyValue($aOption);
  204. if($this->oDb->setParam($aOption['name'], $aData[$aOption['name']], $iMixId)) {
  205. $aCategoryInfo = array();
  206. $this->oDb->getCategories(array('type' => 'by_name', 'value' => $sCategory), $aCategoryInfo, false);
  207. bx_alert('system', 'save_setting', 0, 0, array('category' => $aCategoryInfo, 'option' => $aOption['name'], 'value' => $aData[$aOption['name']]));
  208. }
  209. }
  210. }
  211. return $this->getJsResult('_adm_stg_scs_save');
  212. }
  213. protected function isReadOnly()
  214. {
  215. $bMix = !empty($this->aMix);
  216. return (!$bMix && $this->bReadOnly) || ($bMix && (int)$this->aMix['editable'] == 0);
  217. }
  218. protected function getSubmittedValue($aOption, &$oForm)
  219. {
  220. $mixedValue = '';
  221. switch($aOption['type']) {
  222. case 'image':
  223. $mixedValue = (int)getParam($aOption['name']);
  224. $aIds = $oForm->getCleanValue($aOption['name']);
  225. if(empty($aIds))
  226. break;
  227. $oStorage = BxDolStorage::getObjectInstance($this->sStorage);
  228. if(!$oStorage)
  229. break;
  230. //--- Concatenation integer values as strings is required to get unique content id
  231. $iContentId = (int)($aOption['id'] . (int)$oForm->getCleanValue('mix_id'));
  232. foreach($aIds as $iId) {
  233. $oStorage->updateGhostsContentId($iId, false, $iContentId);
  234. $mixedValue = $iId;
  235. }
  236. break;
  237. default:
  238. $mixedValue = $oForm->getCleanValue($aOption['name']);
  239. }
  240. return $mixedValue;
  241. }
  242. protected function getProcessedValue($aOption, $mixedValue)
  243. {
  244. if(is_array($mixedValue))
  245. $mixedValue = implode(',', $mixedValue);
  246. return $mixedValue;
  247. }
  248. protected function getEmptyValue($aOption)
  249. {
  250. $mixedValue = '';
  251. switch($aOption['type']) {
  252. case 'digit':
  253. $mixedValue = 0;
  254. break;
  255. case 'select':
  256. if (BxDolService::isSerializedService($aOption['extra']))
  257. $aValues = BxDolService::callSerialized($aOption['extra']);
  258. else
  259. $aValues = explode(',', $aOption['extra']);
  260. $mixedValue = $aValues[0];
  261. break;
  262. case 'text':
  263. case 'checkbox':
  264. case 'file':
  265. $mixedValue = "";
  266. break;
  267. }
  268. return $mixedValue;
  269. }
  270. protected function clearCache()
  271. {
  272. BxDolDb::getInstance()->cacheParams(true);
  273. BxDolCacheUtilities::getInstance()->clear('css');
  274. }
  275. }
  276. /** @} */