BxDolStudioDesigner.php 11 KB

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