BxDolStudioDesigner.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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_DSG_TYPE_GENERAL', 'general');
  10. define('BX_DOL_STUDIO_DSG_TYPE_LOGO', 'logo');
  11. define('BX_DOL_STUDIO_DSG_TYPE_ICON', 'icon');
  12. define('BX_DOL_STUDIO_DSG_TYPE_COVER', 'cover');
  13. define('BX_DOL_STUDIO_DSG_TYPE_SPLASH', 'splash');
  14. define('BX_DOL_STUDIO_DSG_TYPE_INJECTIONS', 'injections');
  15. define('BX_DOL_STUDIO_DSG_TYPE_PWA', 'pwa');
  16. define('BX_DOL_STUDIO_DSG_TYPE_SETTINGS', 'settings');
  17. define('BX_DOL_STUDIO_DSG_TYPE_DEFAULT', 'general');
  18. class BxDolStudioDesigner extends BxTemplStudioWidget
  19. {
  20. protected $sPage;
  21. protected $sManageUrl;
  22. protected $sParamPrefix;
  23. protected $sParamLogo;
  24. protected $sParamLogoDark;
  25. protected $sParamLogoInline;
  26. protected $sParamMark;
  27. protected $sParamMarkDark;
  28. protected $sParamMarkInline;
  29. protected $sParamLogoAlt;
  30. protected $aLogos;
  31. protected $aIcons;
  32. protected $aCovers;
  33. function __construct($sPage = "")
  34. {
  35. parent::__construct('designer');
  36. $this->oDb = new BxDolStudioDesignerQuery();
  37. $this->sPage = BX_DOL_STUDIO_DSG_TYPE_DEFAULT;
  38. if(is_string($sPage) && !empty($sPage))
  39. $this->sPage = $sPage;
  40. $this->sManageUrl = BX_DOL_URL_STUDIO . 'designer.php';
  41. $this->sParamPrefix = 'dsg';
  42. $this->sParamLogo = 'sys_site_logo';
  43. $this->sParamLogoDark = '';
  44. $this->sParamLogoInline = '';
  45. $this->sParamMark = '';
  46. $this->sParamMarkDark = '';
  47. $this->sParamMarkInline = '';
  48. $this->sParamLogoAlt = 'sys_site_logo_alt';
  49. $this->aLogos = [
  50. 'logo' => [
  51. 'param' => 'sParamLogo',
  52. 'storage' => 'sys_images_custom',
  53. 'transcoder' => 'sys_custom_images'
  54. ],
  55. 'logo_dark' => [
  56. 'param' => 'sParamLogoDark',
  57. 'storage' => 'sys_images_custom',
  58. 'transcoder' => 'sys_custom_images'
  59. ],
  60. 'logo_inline' => [
  61. 'param' => 'sParamLogoInline',
  62. ],
  63. 'mark' => [
  64. 'param' => 'sParamMark',
  65. 'storage' => 'sys_images_custom',
  66. 'transcoder' => 'sys_custom_images'
  67. ],
  68. 'mark_dark' => [
  69. 'param' => 'sParamMarkDark',
  70. 'storage' => 'sys_images_custom',
  71. 'transcoder' => 'sys_custom_images'
  72. ],
  73. 'mark_inline' => [
  74. 'param' => 'sParamMarkInline',
  75. ],
  76. ];
  77. $this->aIcons = [
  78. 'icon' => [
  79. 'storage' => BX_DOL_STORAGE_OBJ_FILES
  80. ],
  81. 'icon_svg' => [
  82. 'storage' => BX_DOL_STORAGE_OBJ_IMAGES
  83. ],
  84. 'icon_apple' => [
  85. 'storage' => BX_DOL_STORAGE_OBJ_IMAGES,
  86. 'transcoder' => BX_DOL_TRANSCODER_OBJ_ICON_APPLE
  87. ],
  88. 'icon_android' => [
  89. 'storage' => BX_DOL_STORAGE_OBJ_IMAGES,
  90. 'transcoder' => BX_DOL_TRANSCODER_OBJ_ICON_ANDROID
  91. ],
  92. 'icon_android_splash' => [
  93. 'storage' => BX_DOL_STORAGE_OBJ_IMAGES,
  94. 'transcoder' => BX_DOL_TRANSCODER_OBJ_ICON_ANDROID_SPLASH
  95. ]
  96. ];
  97. $this->aCovers = array(
  98. 'cover_common' => array(
  99. 'setting' => 'sys_site_cover_common',
  100. 'transcoder' => BX_DOL_TRANSCODER_OBJ_COVER,
  101. 'title' => '_adm_dsg_txt_cover_common',
  102. 'template' => 'dsr_cover_preview_page.html'
  103. ),
  104. 'cover_unit_profile' => array(
  105. 'setting' => 'sys_unit_cover_profile',
  106. 'transcoder' => BX_DOL_TRANSCODER_OBJ_COVER_UNIT_PROFILE,
  107. 'title' => '_adm_dsg_txt_cover_unit_profile',
  108. 'template' => 'dsr_cover_preview_unit_profile.html'
  109. )
  110. );
  111. }
  112. function checkAction()
  113. {
  114. $sAction = bx_get($this->sParamPrefix . '_action');
  115. if($sAction === false)
  116. return false;
  117. $sAction = bx_process_input($sAction);
  118. $aResult = array('code' => 1, 'message' => _t('_adm_dsg_err_cannot_process_action'));
  119. switch($sAction) {
  120. case 'delete_icon':
  121. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  122. $aResult = ['code' => 0, 'message' => ''];
  123. if(empty($sValue) || !$this->deleteIcon($sValue))
  124. $aResult = ['code' => 2, 'message' => _t('_adm_dsg_err_remove_old_icon')];
  125. break;
  126. case 'delete_cover':
  127. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  128. $aResult = array('code' => 0, 'message' => '');
  129. if(empty($sValue) || !$this->deleteCover($sValue))
  130. $aResult = array('code' => 2, 'message' => _t('_adm_dsg_err_remove_old_cover'));
  131. break;
  132. case 'make_default':
  133. $aResult = array('code' => 0, 'message' => '');
  134. if(!$this->makeDefault())
  135. $aResult = array('code' => 2, 'message' => _t('_adm_dsg_err_make_default'));
  136. break;
  137. case 'get-page-by-type':
  138. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  139. if(empty($sValue))
  140. break;
  141. $this->sPage = $sValue;
  142. $aResult = array('code' => 0, 'content' => $this->getPageCode());
  143. break;
  144. }
  145. return $aResult;
  146. }
  147. function setManageUrl($sUrl)
  148. {
  149. $this->sManageUrl = $sUrl;
  150. }
  151. function setParamPrefix($sPrefix)
  152. {
  153. $this->sParamPrefix = $sPrefix;
  154. }
  155. function setLogoParams($aParams)
  156. {
  157. switch(count($aParams)) {
  158. case 2:
  159. list($this->sParamLogo, $this->sParamLogoAlt) = $aParams;
  160. break;
  161. case 3:
  162. list($this->sParamLogo, $this->sParamMark, $this->sParamLogoAlt) = array_values($aParams);
  163. break;
  164. case 7:
  165. list(
  166. $this->sParamLogo,
  167. $this->sParamLogoDark,
  168. $this->sParamLogoInline,
  169. $this->sParamMark,
  170. $this->sParamMarkDark,
  171. $this->sParamMarkInline,
  172. $this->sParamLogoAlt
  173. ) = array_values($aParams);
  174. break;
  175. }
  176. }
  177. function makeDefault()
  178. {
  179. $sValue = bx_get($this->sParamPrefix . '_value');
  180. if($sValue === false)
  181. return false;
  182. $sValue = bx_process_input($sValue);
  183. return setParam('template', $sValue);
  184. }
  185. public function submitLogo(&$oForm)
  186. {
  187. $iProfileId = bx_get_logged_profile_id();
  188. foreach($this->aLogos as $sLogo => $aLogo) {
  189. if(empty($this->{$aLogo['param']}))
  190. continue;
  191. $mixedValue = $mixedValuePrior = false;
  192. if(isset($aLogo['storage'])) {
  193. $bValue = bx_get($sLogo) !== false;
  194. $bFile = isset($_FILES[$sLogo]) && !empty($_FILES[$sLogo]['tmp_name']);
  195. if(!$bValue && !$bFile)
  196. continue;
  197. $oStorage = BxDolStorage::getObjectInstance($aLogo['storage']);
  198. $iId = 0;
  199. if($bValue)
  200. $iId = (int)bx_get($sLogo);
  201. else if($bFile) {
  202. $iId = $oStorage->storeFileFromForm($_FILES[$sLogo], true, $iProfileId);
  203. if($iId === false)
  204. continue;
  205. }
  206. $mixedValue = $iId;
  207. $mixedValuePrior = (int)getParam($this->{$aLogo['param']});
  208. if($mixedValue != 0 && $mixedValue != $mixedValuePrior)
  209. $this->deleteLogo($sLogo);
  210. }
  211. else {
  212. $mixedValue = $oForm->getCleanValue($sLogo);
  213. $mixedValuePrior = getParam($this->{$aLogo['param']});
  214. }
  215. setParam($this->{$aLogo['param']}, $mixedValue);
  216. bx_alert('system', 'change_' . $sLogo, 0, 0, [
  217. 'option' => $aLogo['param'],
  218. 'value' => $mixedValue,
  219. 'value_prior' => $mixedValuePrior
  220. ]);
  221. }
  222. setParam($this->sParamLogoAlt, $oForm->getCleanValue('alt'));
  223. return $this->getJsResult('_adm_dsg_scs_save', true, true, bx_append_url_params($this->sManageUrl, array('page' => BX_DOL_STUDIO_DSG_TYPE_LOGO)));
  224. }
  225. public function deleteLogo($sLogo)
  226. {
  227. $iProfileId = getLoggedId();
  228. $sParamByType = $this->aLogos[$sLogo]['param'];
  229. $oStorage = BxDolStorage::getObjectInstance($this->aLogos[$sLogo]['storage']);
  230. $iId = (int)getParam($this->$sParamByType);
  231. if($iId != 0 && !$oStorage->deleteFile($iId, $iProfileId))
  232. return false;
  233. setParam($this->$sParamByType, 0);
  234. bx_alert('system', 'change_' . $sLogo, 0, 0, [
  235. 'option' => $this->$sParamByType,
  236. 'value' => 0,
  237. 'value_prior' => $iId
  238. ]);
  239. return true;
  240. }
  241. function submitIcon(&$oForm)
  242. {
  243. $iProfileId = bx_get_logged_profile_id();
  244. foreach($this->aIcons as $sIcon => $aIcon) {
  245. $bValue = bx_get($sIcon) !== false;
  246. $bFile = isset($_FILES[$sIcon]) && !empty($_FILES[$sIcon]['tmp_name']);
  247. if(!$bValue && !$bFile)
  248. continue;
  249. $sSetting = 'sys_site_' . $sIcon;
  250. $oStorage = BxDolStorage::getObjectInstance($aIcon['storage']);
  251. if(($bValue || $bFile) && (int)getParam($sSetting) != 0)
  252. $this->deleteIcon($sIcon);
  253. $iId = 0;
  254. if($bValue)
  255. $iId = (int)bx_get($sIcon);
  256. else if($bFile) {
  257. $iId = $oStorage->storeFileFromForm($_FILES[$sIcon], false, $iProfileId);
  258. if($iId === false)
  259. continue;
  260. }
  261. $oStorage->afterUploadCleanup($iId, $iProfileId);
  262. setParam($sSetting, $iId);
  263. }
  264. return $this->getJsResult('_adm_dsg_scs_save', true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_ICON);
  265. }
  266. function deleteIcon($sIcon)
  267. {
  268. $iProfile = bx_get_logged_profile_id();
  269. $sSetting = 'sys_site_' . $sIcon;
  270. $oStorage = BxDolStorage::getObjectInstance($this->aIcons[$sIcon]['storage']);
  271. $iId = (int)getParam($sSetting);
  272. if($iId != 0 && !$oStorage->deleteFile($iId, $iProfile) && $oStorage->getErrorCode() != BX_DOL_STORAGE_ERR_FILE_NOT_FOUND)
  273. return false;
  274. setParam($sSetting, 0);
  275. return true;
  276. }
  277. function submitCover(&$oForm)
  278. {
  279. $iProfile = bx_get_logged_profile_id();
  280. $oStorage = BxDolStorage::getObjectInstance($this->sCoverStorage);
  281. foreach($this->aCovers as $sCover => $aCover) {
  282. $iIdNew = $oForm->getCleanValue($sCover);
  283. if(empty($iIdNew))
  284. continue;
  285. $iIdOld = (int)getParam($aCover['setting']);
  286. if(!$this->deleteCover($sCover))
  287. return $this->getJsResult('_adm_dsg_err_remove_old_cover');
  288. setParam($aCover['setting'], $iIdNew);
  289. $oStorage->afterUploadCleanup($iIdNew, $iProfile);
  290. }
  291. setParam('sys_site_cover_disabled', $oForm->getCleanValue('disabled') == 'on' ? 'on' : '');
  292. return $this->getJsResult('_adm_dsg_scs_save', true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_COVER);
  293. }
  294. function deleteCover($sCover)
  295. {
  296. $iProfile = bx_get_logged_profile_id();
  297. $oStorage = BxDolStorage::getObjectInstance($this->sCoverStorage);
  298. $iId = (int)getParam($this->aCovers[$sCover]['setting']);
  299. if($iId != 0 && !$oStorage->deleteFile($iId, $iProfile) && $oStorage->getErrorCode() != BX_DOL_STORAGE_ERR_FILE_NOT_FOUND)
  300. return false;
  301. setParam($this->aCovers[$sCover]['setting'], 0);
  302. return true;
  303. }
  304. function submitSplash(&$oForm)
  305. {
  306. setParam('sys_site_splash_code', $oForm->getCleanValue('code'));
  307. setParam('sys_site_splash_enabled', $oForm->getCleanValue('enabled') == 'on' ? 'on' : '');
  308. return $this->getJsResult('_adm_dsg_scs_save', true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_SPLASH);
  309. }
  310. function submitInjections(&$oForm)
  311. {
  312. $sResult = '_adm_dsg_err_save_changes';
  313. $bResult = $this->oDb->updateInjection('sys_head', $oForm->getCleanValue('sys_head'));
  314. $bResult |= $this->oDb->updateInjection('sys_body', $oForm->getCleanValue('sys_body'));
  315. if($bResult) {
  316. if(getParam('sys_db_cache_enable'))
  317. $this->oDb->cleanCache('sys_injections.inc');
  318. $sResult = '_adm_dsg_scs_save';
  319. }
  320. return $this->getJsResult($sResult, true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_INJECTIONS);
  321. }
  322. }
  323. /** @} */