BxDolStudioDesigner.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 $sParamLogoAlt;
  24. protected $sParamLogoWidth;
  25. protected $sParamLogoHeight;
  26. protected $aCovers;
  27. function __construct($sPage = "")
  28. {
  29. parent::__construct('designer');
  30. $this->oDb = new BxDolStudioDesignerQuery();
  31. $this->sPage = BX_DOL_STUDIO_DSG_TYPE_DEFAULT;
  32. if(is_string($sPage) && !empty($sPage))
  33. $this->sPage = $sPage;
  34. $this->sManageUrl = BX_DOL_URL_STUDIO . 'designer.php';
  35. $this->sParamPrefix = 'dsg';
  36. $this->sParamLogo = 'sys_site_logo';
  37. $this->sParamLogoAlt = 'sys_site_logo_alt';
  38. $this->sParamLogoWidth = 'sys_site_logo_width';
  39. $this->sParamLogoHeight = 'sys_site_logo_height';
  40. $this->aCovers = array(
  41. 'cover_common' => array(
  42. 'setting' => 'sys_site_cover_common',
  43. 'transcoder' => BX_DOL_TRANSCODER_OBJ_COVER,
  44. 'title' => '_adm_dsg_txt_cover_common',
  45. 'template' => 'dsr_cover_preview_page.html'
  46. ),
  47. 'cover_unit_profile' => array(
  48. 'setting' => 'sys_unit_cover_profile',
  49. 'transcoder' => BX_DOL_TRANSCODER_OBJ_COVER_UNIT_PROFILE,
  50. 'title' => '_adm_dsg_txt_cover_unit_profile',
  51. 'template' => 'dsr_cover_preview_unit_profile.html'
  52. )
  53. );
  54. }
  55. function checkAction()
  56. {
  57. $sAction = bx_get($this->sParamPrefix . '_action');
  58. if($sAction === false)
  59. return false;
  60. $sAction = bx_process_input($sAction);
  61. $aResult = array('code' => 1, 'message' => _t('_adm_dsg_err_cannot_process_action'));
  62. switch($sAction) {
  63. case 'delete_logo':
  64. $aResult = array('code' => 0, 'message' => '');
  65. if(!$this->deleteLogo())
  66. $aResult = array('code' => 2, 'message' => _t('_adm_dsg_err_remove_old_logo'));
  67. break;
  68. case 'delete_cover':
  69. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  70. $aResult = array('code' => 0, 'message' => '');
  71. if(empty($sValue) || !$this->deleteCover($sValue))
  72. $aResult = array('code' => 2, 'message' => _t('_adm_dsg_err_remove_old_cover'));
  73. break;
  74. case 'make_default':
  75. $aResult = array('code' => 0, 'message' => '');
  76. if(!$this->makeDefault())
  77. $aResult = array('code' => 2, 'message' => _t('_adm_dsg_err_make_default'));
  78. break;
  79. case 'get-page-by-type':
  80. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  81. if(empty($sValue))
  82. break;
  83. $this->sPage = $sValue;
  84. $aResult = array('code' => 0, 'content' => $this->getPageCode());
  85. break;
  86. }
  87. return $aResult;
  88. }
  89. function setManageUrl($sUrl)
  90. {
  91. $this->sManageUrl = $sUrl;
  92. }
  93. function setParamPrefix($sPrefix)
  94. {
  95. $this->sParamPrefix = $sPrefix;
  96. }
  97. function setLogoParams($aParams)
  98. {
  99. list($this->sParamLogo, $this->sParamLogoAlt, $this->sParamLogoWidth, $this->sParamLogoHeight) = $aParams;
  100. }
  101. function makeDefault()
  102. {
  103. $sValue = bx_get($this->sParamPrefix . '_value');
  104. if($sValue === false)
  105. return false;
  106. $sValue = bx_process_input($sValue);
  107. return $this->oDb->setParam('template', $sValue);
  108. }
  109. function submitLogo(&$oForm)
  110. {
  111. $iProfileId = getLoggedId();
  112. if(!empty($_FILES['image']['tmp_name'])) {
  113. $oStorage = BxDolStorage::getObjectInstance('sys_images_custom');
  114. if(!$this->deleteLogo())
  115. return $this->getJsResult('_adm_dsg_err_remove_old_logo');
  116. $iId = $oStorage->storeFileFromForm($_FILES['image'], false, $iProfileId);
  117. if($iId === false) {
  118. $this->oDb->setParam($this->sParamLogo, 0);
  119. return $this->getJsResult(_t('_adm_dsg_err_save') . $oStorage->getErrorString(), false);
  120. }
  121. $this->oDb->setParam($this->sParamLogo, $iId);
  122. $oStorage->afterUploadCleanup($iId, $iProfileId);
  123. }
  124. $this->oDb->setParam($this->sParamLogoAlt, $oForm->getCleanValue('alt'));
  125. $this->oDb->setParam($this->sParamLogoWidth, $oForm->getCleanValue('width'));
  126. $this->oDb->setParam($this->sParamLogoHeight, $oForm->getCleanValue('height'));
  127. return $this->getJsResult('_adm_dsg_scs_save', true, true, bx_append_url_params($this->sManageUrl, array('page' => BX_DOL_STUDIO_DSG_TYPE_LOGO)));
  128. }
  129. function deleteLogo()
  130. {
  131. $iProfileId = getLoggedId();
  132. $oStorage = BxDolStorage::getObjectInstance('sys_images_custom');
  133. $iId = (int)getParam($this->sParamLogo);
  134. if($iId != 0 && !$oStorage->deleteFile($iId, $iProfileId))
  135. return false;
  136. $this->oDb->setParam($this->sParamLogo, 0);
  137. return true;
  138. }
  139. function submitIcon(&$oForm)
  140. {
  141. $iProfileId = getLoggedId();
  142. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  143. $iId = (int)getParam('sys_site_icon');
  144. if($iId != 0 && !$oStorage->deleteFile($iId, $iProfileId))
  145. return $this->getJsResult('_adm_dsg_err_remove_old_icon');
  146. $iId = $oStorage->storeFileFromForm($_FILES['image'], true, $iProfileId);
  147. if($iId === false) {
  148. $this->oDb->setParam('sys_site_icon', 0);
  149. return $this->getJsResult(_t('_adm_dsg_err_save') . $oStorage->getErrorString(), false);
  150. }
  151. $this->oDb->setParam('sys_site_icon', $iId);
  152. $oStorage->afterUploadCleanup($iId, $iProfileId);
  153. return $this->getJsResult('_adm_dsg_scs_save', true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_ICON);
  154. }
  155. function submitCover(&$oForm)
  156. {
  157. $iProfile = bx_get_logged_profile_id();
  158. $oStorage = BxDolStorage::getObjectInstance($this->sCoverStorage);
  159. foreach($this->aCovers as $sCover => $aCover) {
  160. $iIdNew = $oForm->getCleanValue($sCover);
  161. if(empty($iIdNew))
  162. continue;
  163. $iIdOld = (int)getParam($aCover['setting']);
  164. if(!$this->deleteCover($sCover))
  165. return $this->getJsResult('_adm_dsg_err_remove_old_cover');
  166. $this->oDb->setParam($aCover['setting'], $iIdNew);
  167. $oStorage->afterUploadCleanup($iIdNew, $iProfile);
  168. }
  169. $this->oDb->setParam('sys_site_cover_disabled', $oForm->getCleanValue('disabled') == 'on' ? 'on' : '');
  170. return $this->getJsResult('_adm_dsg_scs_save', true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_COVER);
  171. }
  172. function deleteCover($sCover)
  173. {
  174. $iProfile = bx_get_logged_profile_id();
  175. $oStorage = BxDolStorage::getObjectInstance($this->sCoverStorage);
  176. $iId = (int)getParam($this->aCovers[$sCover]['setting']);
  177. if($iId != 0 && !$oStorage->deleteFile($iId, $iProfile) && $oStorage->getErrorCode() != BX_DOL_STORAGE_ERR_FILE_NOT_FOUND)
  178. return false;
  179. $this->oDb->setParam($this->aCovers[$sCover]['setting'], 0);
  180. return true;
  181. }
  182. function submitSplash(&$oForm)
  183. {
  184. $this->oDb->setParam('sys_site_splash_code', $oForm->getCleanValue('code'));
  185. $this->oDb->setParam('sys_site_splash_enabled', $oForm->getCleanValue('enabled') == 'on' ? 'on' : '');
  186. return $this->getJsResult('_adm_dsg_scs_save', true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_SPLASH);
  187. }
  188. function submitInjections(&$oForm)
  189. {
  190. $bResult = $this->oDb->updateInjection('sys_head', $oForm->getCleanValue('sys_head'));
  191. $bResult |= $this->oDb->updateInjection('sys_body', $oForm->getCleanValue('sys_body'));
  192. if($bResult) {
  193. if(getParam('sys_db_cache_enable'))
  194. $this->oDb->cleanCache('sys_injections.inc');
  195. return $this->getJsResult('_adm_dsg_scs_save', true, true, BX_DOL_URL_STUDIO . 'designer.php?page=' . BX_DOL_STUDIO_DSG_TYPE_INJECTIONS);
  196. }
  197. return $this->getJsResult('_adm_dsg_err_save_changes');
  198. }
  199. }
  200. /** @} */