BxDolStudioSettings.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 'publish-mix':
  82. $aResult = array_merge($aResult, $this->publishMix((int)$sValue));
  83. break;
  84. case 'hide-mix':
  85. $aResult = array_merge($aResult, $this->hideMix((int)$sValue));
  86. break;
  87. case 'delete-mix':
  88. $aResult = array_merge($aResult, $this->deleteMix((int)$sValue));
  89. break;
  90. case 'get-page-by-type':
  91. $this->sType = $sValue;
  92. $aResult['content'] = $this->getPageCode();
  93. break;
  94. }
  95. echo json_encode($aResult);
  96. }
  97. exit;
  98. }
  99. }
  100. public function enableReadOnly($bReadOnly = true)
  101. {
  102. $this->bReadOnly = $bReadOnly;
  103. }
  104. public function enableMixes($bMixes = true)
  105. {
  106. $this->bMixes = $bMixes;
  107. }
  108. public function selectMix($sName)
  109. {
  110. $this->oDb->updateMixes(array('active' => 0), array(
  111. 'type' => $this->sType,
  112. 'category' => is_string($this->sCategory) ? $this->sCategory : '',
  113. 'active' => 1
  114. ));
  115. $aResult = array();
  116. if($sName == BX_DOL_STUDIO_STG_MIX_SYSTEM || $this->oDb->updateMixes(array('active' => 1), array('name' => $sName))) {
  117. $this->clearCache();
  118. $aResult = array('eval' => $this->getPageJsObject() . '.onMixSelect(oData);');
  119. }
  120. else
  121. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  122. return $aResult;
  123. }
  124. public function exportMix($iId)
  125. {
  126. $aMix = array();
  127. $this->oDb->getMixes(array('type' => 'by_id', 'value' => $iId), $aMix, false);
  128. if(empty($aMix) || !is_array($aMix))
  129. return array('message' => _t('_adm_stg_err_cannot_perform'));
  130. return array(
  131. 'url' => bx_append_url_params(BX_DOL_URL_STUDIO . 'settings.php', array(
  132. 'stg_action' => 'download-mix',
  133. 'stg_value' => $iId,
  134. )),
  135. 'eval' => $this->getPageJsObject() . '.onMixExport(oData);'
  136. );
  137. }
  138. public function downloadMix($iId)
  139. {
  140. $aMix = array();
  141. $this->oDb->getMixes(array('type' => 'by_id', 'value' => $iId), $aMix, false);
  142. if(empty($aMix) || !is_array($aMix))
  143. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  144. $aOptions = array();
  145. $this->oDb->getMixesOptions(array('type' => 'by_mix_id_pair_option_value', 'value' => $iId, 'for_export' => 1), $aOptions, false);
  146. $aContent = array(
  147. 'mix' => array(
  148. 'type' => $aMix['type'],
  149. 'category' => $aMix['category'],
  150. 'name' => $aMix['name'],
  151. 'title' => $aMix['title']
  152. ),
  153. 'options' => $aOptions
  154. );
  155. $sContent = json_encode($aContent);
  156. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  157. header("Content-type: application/json");
  158. header("Content-Length: " . strlen($sContent));
  159. header("Content-Disposition: attachment; filename=\"". strtolower($aMix['name']) . ".json\"");
  160. echo $sContent;
  161. exit;
  162. }
  163. public function publishMix($iId)
  164. {
  165. $aResult = array();
  166. if($this->oDb->updateMixes(array('published' => 1), array('id' => $iId))) {
  167. $this->clearCache();
  168. $aResult = array('eval' => $this->getPageJsObject() . '.onMixPublish(oData);');
  169. }
  170. else
  171. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  172. return $aResult;
  173. }
  174. public function hideMix($iId)
  175. {
  176. $aResult = array();
  177. if($this->oDb->updateMixes(array('published' => 0), array('id' => $iId))) {
  178. $this->clearCache();
  179. $aResult = array('eval' => $this->getPageJsObject() . '.onMixHide(oData);');
  180. }
  181. else
  182. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  183. return $aResult;
  184. }
  185. public function deleteMix($iId)
  186. {
  187. $aResult = array();
  188. if($this->oDb->deleteMixesOptions(array('mix_id' => $iId)) && $this->oDb->deleteMixes(array('id' => $iId))) {
  189. $this->clearCache();
  190. $aResult = array('eval' => $this->getPageJsObject() . '.onMixDelete(oData);');
  191. }
  192. else
  193. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  194. return $aResult;
  195. }
  196. function saveChanges(&$oForm)
  197. {
  198. $iMixId = $oForm->getCleanValue('mix_id');
  199. $aCategories = explode(',', $oForm->getCleanValue('categories'));
  200. $sEvalRenewToken = '$("#' . $oForm->getId() . ' input[name=csrf_token]").val("' . $oForm->getCsrfToken() . '");';
  201. foreach ($aCategories as $sCategory) {
  202. $aOptions = array();
  203. $iOptions = $this->oDb->getOptions(array('type' => 'by_category_name_full', 'value' => $sCategory), $aOptions);
  204. $aData = array();
  205. foreach($aOptions as $aOption) {
  206. $aData[$aOption['name']] = $this->getSubmittedValue($aOption, $oForm);
  207. if($aData[$aOption['name']] === false && !empty($this->sErrorMessage)) {
  208. $this->sCategory = $sCategory;
  209. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($this->sErrorMessage)), false, false, '', $sEvalRenewToken);
  210. }
  211. if(!empty($aOption['check'])) {
  212. $sCheckerHelper = '';
  213. if(!empty($aOption['type_name']) && BxDolRequest::serviceExists($aOption['type_name'], 'get_settings_checker_helper'))
  214. $sCheckerHelper = BxDolService::call($aOption['type_name'], 'get_settings_checker_helper');
  215. if($sCheckerHelper == '') {
  216. bx_import('BxDolStudioForm');
  217. $sCheckerHelper = 'BxDolStudioFormCheckerHelper';
  218. }
  219. $oChecker = new $sCheckerHelper();
  220. $aCheckFunction = array($oChecker, 'check' . bx_gen_method_name($aOption['check']));
  221. $aCheckFunctionParams = array($aData[$aOption['name']]);
  222. if(!empty($aOption['check_params']))
  223. $aCheckFunctionParams = array_merge($aCheckFunctionParams, unserialize($aOption['check_params']));
  224. if(is_callable($aCheckFunction) && !call_user_func_array($aCheckFunction, $aCheckFunctionParams)) {
  225. $this->sCategory = $sCategory;
  226. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($aOption['check_error'])), false, false, '', $sEvalRenewToken);
  227. }
  228. }
  229. if(isset($aData[$aOption['name']]))
  230. $aData[$aOption['name']] = $this->getProcessedValue($aOption, $aData[$aOption['name']]);
  231. else
  232. $aData[$aOption['name']] = $this->getEmptyValue($aOption);
  233. if($this->oDb->setParam($aOption['name'], $aData[$aOption['name']], $iMixId)) {
  234. $aCategoryInfo = array();
  235. $this->oDb->getCategories(array('type' => 'by_name', 'value' => $sCategory), $aCategoryInfo, false);
  236. bx_alert('system', 'save_setting', 0, 0, array('category' => $aCategoryInfo, 'option' => $aOption['name'], 'value' => $aData[$aOption['name']]));
  237. }
  238. }
  239. }
  240. return $this->getJsResult('_adm_stg_scs_save', true, false, '', $sEvalRenewToken);
  241. }
  242. protected function isReadOnly()
  243. {
  244. $bMix = !empty($this->aMix);
  245. return (!$bMix && $this->bReadOnly) || ($bMix && (int)$this->aMix['editable'] == 0);
  246. }
  247. protected function getSubmittedValue($aOption, &$oForm)
  248. {
  249. $mixedValue = '';
  250. switch($aOption['type']) {
  251. case 'image':
  252. $mixedValue = (int)getParam($aOption['name']);
  253. $aIds = $oForm->getCleanValue($aOption['name']);
  254. if(empty($aIds))
  255. break;
  256. $oStorage = BxDolStorage::getObjectInstance($this->sStorage);
  257. if(!$oStorage)
  258. break;
  259. //--- Concatenation integer values as strings is required to get unique content id
  260. $iContentId = (int)($aOption['id'] . (int)$oForm->getCleanValue('mix_id'));
  261. foreach($aIds as $iId) {
  262. $oStorage->updateGhostsContentId($iId, false, $iContentId);
  263. $mixedValue = $iId;
  264. }
  265. break;
  266. default:
  267. $mixedValue = $oForm->getCleanValue($aOption['name']);
  268. }
  269. return $mixedValue;
  270. }
  271. protected function getProcessedValue($aOption, $mixedValue)
  272. {
  273. if(is_array($mixedValue))
  274. $mixedValue = implode(',', $mixedValue);
  275. return $mixedValue;
  276. }
  277. protected function getEmptyValue($aOption)
  278. {
  279. $mixedValue = '';
  280. switch($aOption['type']) {
  281. case 'digit':
  282. $mixedValue = 0;
  283. break;
  284. case 'select':
  285. if (BxDolService::isSerializedService($aOption['extra']))
  286. $aValues = BxDolService::callSerialized($aOption['extra']);
  287. else
  288. $aValues = explode(',', $aOption['extra']);
  289. $mixedValue = $aValues[0];
  290. break;
  291. case 'text':
  292. case 'checkbox':
  293. case 'file':
  294. $mixedValue = "";
  295. break;
  296. }
  297. return $mixedValue;
  298. }
  299. protected function clearCache()
  300. {
  301. BxDolDb::getInstance()->cacheParams(true, true);
  302. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  303. $oCacheUtilities->clear('db');
  304. $oCacheUtilities->clear('css');
  305. }
  306. }
  307. /** @} */