BxDolStudioSettings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. $aResult = array('eval' => $this->getPageJsObject() . '.onMixSelect(oData);');
  112. else
  113. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  114. return $aResult;
  115. }
  116. public function exportMix($iId)
  117. {
  118. $aMix = array();
  119. $this->oDb->getMixes(array('type' => 'by_id', 'value' => $iId), $aMix, false);
  120. if(empty($aMix) || !is_array($aMix))
  121. return array('message' => _t('_adm_stg_err_cannot_perform'));
  122. return array(
  123. 'url' => bx_append_url_params(BX_DOL_URL_STUDIO . 'settings.php', array(
  124. 'stg_action' => 'download-mix',
  125. 'stg_value' => $iId,
  126. )),
  127. 'eval' => $this->getPageJsObject() . '.onMixExport(oData);'
  128. );
  129. }
  130. public function downloadMix($iId)
  131. {
  132. $aMix = array();
  133. $this->oDb->getMixes(array('type' => 'by_id', 'value' => $iId), $aMix, false);
  134. if(empty($aMix) || !is_array($aMix))
  135. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  136. $aOptions = array();
  137. $this->oDb->getMixesOptions(array('type' => 'by_mix_id_pair_option_value', 'value' => $iId, 'for_export' => 1), $aOptions, false);
  138. $aContent = array(
  139. 'mix' => array(
  140. 'type' => $aMix['type'],
  141. 'category' => $aMix['category'],
  142. 'name' => $aMix['name'],
  143. 'title' => $aMix['title']
  144. ),
  145. 'options' => $aOptions
  146. );
  147. $sContent = json_encode($aContent);
  148. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  149. header("Content-type: application/json");
  150. header("Content-Length: " . strlen($sContent));
  151. header("Content-Disposition: attachment; filename=\"". strtolower($aMix['name']) . ".json\"");
  152. echo $sContent;
  153. exit;
  154. }
  155. public function deleteMix($iId)
  156. {
  157. $aResult = array();
  158. if($this->oDb->deleteMixesOptions(array('mix_id' => $iId)) && $this->oDb->deleteMixes(array('id' => $iId)))
  159. $aResult = array('eval' => $this->getPageJsObject() . '.onMixDelete(oData);');
  160. else
  161. $aResult = array('message' => _t('_adm_stg_err_cannot_perform'));
  162. return $aResult;
  163. }
  164. function saveChanges(&$oForm)
  165. {
  166. $iMixId = $oForm->getCleanValue('mix_id');
  167. $aCategories = explode(',', $oForm->getCleanValue('categories'));
  168. foreach ($aCategories as $sCategory) {
  169. $aOptions = array();
  170. $iOptions = $this->oDb->getOptions(array('type' => 'by_category_name_full', 'value' => $sCategory), $aOptions);
  171. $aData = array();
  172. foreach($aOptions as $aOption) {
  173. $aData[$aOption['name']] = $this->getSubmittedValue($aOption, $oForm);
  174. if($aData[$aOption['name']] === false && !empty($this->sErrorMessage)) {
  175. $this->sCategory = $sCategory;
  176. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($this->sErrorMessage)), false);
  177. }
  178. if(!empty($aOption['check'])) {
  179. $sCheckerHelper = '';
  180. if(!empty($aOption['type_name']) && BxDolRequest::serviceExists($aOption['type_name'], 'get_settings_checker_helper'))
  181. $sCheckerHelper = BxDolService::call($aOption['type_name'], 'get_settings_checker_helper');
  182. if($sCheckerHelper == '') {
  183. bx_import('BxDolStudioForm');
  184. $sCheckerHelper = 'BxDolStudioFormCheckerHelper';
  185. }
  186. $oChecker = new $sCheckerHelper();
  187. $aCheckFunction = array($oChecker, 'check' . bx_gen_method_name($aOption['check']));
  188. $aCheckFunctionParams = array($aData[$aOption['name']]);
  189. if(!empty($aOption['check_params']))
  190. $aCheckFunctionParams = array_merge($aCheckFunctionParams, unserialize($aOption['check_params']));
  191. if(is_callable($aCheckFunction) && !call_user_func_array($aCheckFunction, $aCheckFunctionParams)) {
  192. $this->sCategory = $sCategory;
  193. return $this->getJsResult(_t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($aOption['check_error'])), false);
  194. }
  195. }
  196. if(isset($aData[$aOption['name']]))
  197. $aData[$aOption['name']] = $this->getProcessedValue($aOption, $aData[$aOption['name']]);
  198. else
  199. $aData[$aOption['name']] = $this->getEmptyValue($aOption);
  200. if($this->oDb->setParam($aOption['name'], $aData[$aOption['name']], $iMixId)) {
  201. $aCategoryInfo = array();
  202. $this->oDb->getCategories(array('type' => 'by_name', 'value' => $sCategory), $aCategoryInfo, false);
  203. bx_alert('system', 'save_setting', 0, 0, array('category' => $aCategoryInfo, 'option' => $aOption['name'], 'value' => $aData[$aOption['name']]));
  204. }
  205. }
  206. }
  207. return $this->getJsResult('_adm_stg_scs_save');
  208. }
  209. protected function isReadOnly()
  210. {
  211. $bMix = !empty($this->aMix);
  212. return (!$bMix && $this->bReadOnly) || ($bMix && (int)$this->aMix['editable'] == 0);
  213. }
  214. protected function getSubmittedValue($aOption, &$oForm)
  215. {
  216. $mixedValue = '';
  217. switch($aOption['type']) {
  218. case 'image':
  219. $mixedValue = (int)getParam($aOption['name']);
  220. $aIds = $oForm->getCleanValue($aOption['name']);
  221. if(empty($aIds))
  222. break;
  223. $oStorage = BxDolStorage::getObjectInstance($this->sStorage);
  224. if(!$oStorage)
  225. break;
  226. //--- Concatenation integer values as strings is required to get unique content id
  227. $iContentId = (int)($aOption['id'] . (int)$oForm->getCleanValue('mix_id'));
  228. foreach($aIds as $iId) {
  229. $oStorage->updateGhostsContentId($iId, false, $iContentId);
  230. $mixedValue = $iId;
  231. }
  232. break;
  233. default:
  234. $mixedValue = $oForm->getCleanValue($aOption['name']);
  235. }
  236. return $mixedValue;
  237. }
  238. protected function getProcessedValue($aOption, $mixedValue)
  239. {
  240. if(is_array($mixedValue))
  241. $mixedValue = implode(',', $mixedValue);
  242. return $mixedValue;
  243. }
  244. protected function getEmptyValue($aOption)
  245. {
  246. $mixedValue = '';
  247. switch($aOption['type']) {
  248. case 'digit':
  249. $mixedValue = 0;
  250. break;
  251. case 'select':
  252. if (BxDolService::isSerializedService($aOption['extra']))
  253. $aValues = BxDolService::callSerialized($aOption['extra']);
  254. else
  255. $aValues = explode(',', $aOption['extra']);
  256. $mixedValue = $aValues[0];
  257. break;
  258. case 'text':
  259. case 'checkbox':
  260. case 'file':
  261. $mixedValue = "";
  262. break;
  263. }
  264. return $mixedValue;
  265. }
  266. }
  267. /** @} */