BxDolStudioDesigner.php 11 KB

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