BxDolStudioSettings.php 11 KB

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