BxDolStudioDesigner.php 11 KB

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