BxDolStudioBuilderPage.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. bx_import('BxDolStudioUtils');
  10. define('BX_DOL_STUDIO_BP_SKELETONS', 'skeletons');
  11. define('BX_DOL_STUDIO_BP_TYPE_DEFAULT', BX_DOL_STUDIO_MODULE_SYSTEM);
  12. define('BX_DOL_STUDIO_BP_BLOCK_RAW', 'raw');
  13. define('BX_DOL_STUDIO_BP_BLOCK_HTML', 'html');
  14. define('BX_DOL_STUDIO_BP_BLOCK_CREATIVE', 'creative');
  15. define('BX_DOL_STUDIO_BP_BLOCK_RSS', 'rss');
  16. define('BX_DOL_STUDIO_BP_BLOCK_IMAGE', 'image');
  17. define('BX_DOL_STUDIO_BP_BLOCK_LANG', 'lang');
  18. define('BX_DOL_STUDIO_BP_BLOCK_MENU', 'menu');
  19. define('BX_DOL_STUDIO_BP_BLOCK_WIKI', 'wiki');
  20. define('BX_DOL_STUDIO_BP_BLOCK_CUSTOM', 'custom');
  21. define('BX_DOL_STUDIO_BP_BLOCK_SERVICE', 'service');
  22. class BxDolStudioBuilderPage extends BxTemplStudioWidget
  23. {
  24. protected $sType;
  25. protected $sPage;
  26. protected $sPageBaseUrl;
  27. protected $aPageRebuild;
  28. function __construct($sType = '', $sPage = '')
  29. {
  30. parent::__construct('builder_pages');
  31. $this->oDb = new BxDolStudioBuilderPageQuery();
  32. $this->sType = BX_DOL_STUDIO_BP_TYPE_DEFAULT;
  33. if(is_string($sType) && !empty($sType))
  34. $this->sType = $sType;
  35. $this->sPage = '';
  36. $this->sPageBaseUrl = 'page.php?i=';
  37. $this->aPageRebuild = array();
  38. if(is_string($sPage) && !empty($sPage)) {
  39. $this->sPage = $sPage;
  40. $this->aPageRebuild = $this->oDb->getPages(array('type' => 'by_object_full', 'value' => $this->sPage));
  41. if(empty($this->aPageRebuild) || !is_array($this->aPageRebuild)) {
  42. $this->sPage = '';
  43. $this->aPageRebuild = array();
  44. }
  45. }
  46. }
  47. public function checkAction()
  48. {
  49. $sAction = bx_get('bp_action');
  50. if($sAction === false)
  51. return false;
  52. $sAction = bx_process_input($sAction);
  53. $aResult = array('code' => 1, 'message' => _t('_adm_bp_err_cannot_process_action'));
  54. switch($sAction) {
  55. case 'reorder':
  56. if(empty($this->aPageRebuild) || !is_array($this->aPageRebuild))
  57. break;
  58. $bResult = false;
  59. for($i = 0; $i <= $this->aPageRebuild['layout_cells_number']; $i++) {
  60. $aItems = bx_get('bp_items_' . $i);
  61. $iItems = count($aItems);
  62. for($j = 0; $j < $iItems; $j++)
  63. $bResult |= $this->oDb->updateBlock((int)$aItems[$j], array(
  64. 'cell_id' => $i,
  65. 'order' => $j
  66. ));
  67. }
  68. $aResult = $bResult ? array('code' => 0, 'message' => _t('_adm_bp_scs_save')) : array('code' => 1, 'message' => _t('_adm_bp_err_nothing_changed'));
  69. break;
  70. default:
  71. $sMethod = 'action' . $this->getClassName($sAction);
  72. if(method_exists($this, $sMethod))
  73. $aResult = $this->$sMethod();
  74. }
  75. return $aResult;
  76. }
  77. function processAction($sAction)
  78. {
  79. $sMethod = 'action' . $this->getClassName($sAction);
  80. if (method_exists($this, $sMethod))
  81. return $this->$sMethod();
  82. else
  83. return array('code' => 1, 'message' => _t('_adm_bp_err_cannot_process_action'));
  84. }
  85. protected function onSaveBlock(&$oForm, &$aBlock)
  86. {
  87. $iDesignboxId = (int)str_replace($this->sSelectKeyPrefix, '', $oForm->getCleanValue('designbox_id'));
  88. BxDolForm::setSubmittedValue('designbox_id', $iDesignboxId, $oForm->aFormAttrs['method']);
  89. $iVisibleFor = BxDolStudioUtils::getVisibilityValue($oForm->getCleanValue('visible_for'), $oForm->getCleanValue('visible_for_levels'));
  90. BxDolForm::setSubmittedValue('visible_for_levels', $iVisibleFor, $oForm->aFormAttrs['method']);
  91. unset($oForm->aInputs['visible_for']);
  92. //--- Process Lang fields
  93. if($aBlock['type'] == BX_DOL_STUDIO_BP_BLOCK_LANG && isset($oForm->aInputs['content']))
  94. $this->onSaveBlockLang($oForm, $aBlock);
  95. //--- Process Image fields
  96. if($aBlock['type'] == BX_DOL_STUDIO_BP_BLOCK_IMAGE && isset($oForm->aInputs['image_file'], $oForm->aInputs['image_align']))
  97. $this->onSaveBlockImage($oForm, $aBlock);
  98. //--- Process RSS fields
  99. if($aBlock['type'] == BX_DOL_STUDIO_BP_BLOCK_RSS && isset($oForm->aInputs['rss_url'], $oForm->aInputs['rss_length']))
  100. $this->onSaveBlockRss($oForm, $aBlock);
  101. }
  102. protected function onSaveBlockLang(&$oForm, &$aBlock)
  103. {
  104. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  105. $sContentKey = '';
  106. $sContentValue = $oForm->getCleanValue('content');
  107. if($aBlock['content'] == '') {
  108. $sContentKey = '_sys_bpb_content_' . $aBlock['id'];
  109. $oLanguage->addLanguageString($sContentKey, $sContentValue);
  110. } else {
  111. $sContentKey = $aBlock['content'];
  112. $oLanguage->updateLanguageString($sContentKey, $sContentValue);
  113. }
  114. BxDolForm::setSubmittedValue('content', $sContentKey, $oForm->aFormAttrs['method']);
  115. }
  116. protected function onSaveBlockImage(&$oForm, &$aBlock)
  117. {
  118. $sInput = 'image_file';
  119. $iProfileId = bx_get_logged_profile_id();
  120. $iImageId = 0;
  121. if($aBlock['content'] != '')
  122. list($iImageId) = explode($this->sParamsDivider, $aBlock['content']);
  123. $iImageIdOld = $iImageId;
  124. $bValue = bx_get($sInput) !== false;
  125. $bFile = isset($_FILES[$sInput]) && !empty($_FILES[$sInput]['tmp_name']);
  126. if($bValue || $bFile) {
  127. $oStorage = BxDolStorage::getObjectInstance($oForm->aInputs[$sInput]['storage_object']);
  128. $iImageId = 0;
  129. if($bValue)
  130. $iImageId = (int)bx_get($sInput);
  131. else if($bFile) {
  132. $iImageId = $oStorage->storeFileFromForm($_FILES[$sInput], false, $iProfileId);
  133. $iImageId = $iImageId !== false ? (int)$iImageId : $oStorage->getErrorString();
  134. }
  135. if(is_int($iImageId) && $iImageId != 0 && $iImageId != $iImageIdOld)
  136. $oStorage->deleteFile($iImageIdOld);
  137. }
  138. if(is_string($iImageId) && !is_numeric($iImageId))
  139. return ['msg' => $iImageId];
  140. $sImageAlign = $oForm->getCleanValue('image_align');
  141. unset($oForm->aInputs['image_file'], $oForm->aInputs['image_align']);
  142. BxDolForm::setSubmittedValue('content', implode($this->sParamsDivider, [$iImageId, $sImageAlign]), $oForm->aFormAttrs['method']);
  143. }
  144. protected function onSaveBlockRss(&$oForm, &$aBlock)
  145. {
  146. $aRss = array(
  147. trim($oForm->getCleanValue('rss_url')),
  148. $oForm->getCleanValue('rss_length')
  149. );
  150. unset($oForm->aInputs['rss_url'], $oForm->aInputs['rss_length']);
  151. BxDolForm::setSubmittedValue('content', implode($this->sParamsDivider, $aRss), $oForm->aFormAttrs['method']);
  152. }
  153. protected function addInArray($aInput, $sKey, $aValues)
  154. {
  155. return BxDolStudioUtils::addInArray($aInput, $sKey, $aValues);
  156. }
  157. protected function getModuleIcon($sName, $sType = 'menu', $bReturnAsUrl = true)
  158. {
  159. $sResult = '';
  160. switch($sName) {
  161. case BX_DOL_STUDIO_MODULE_SYSTEM:
  162. $sResult = 'cog';
  163. break;
  164. case BX_DOL_STUDIO_MODULE_CUSTOM:
  165. $sResult = 'wrench';
  166. break;
  167. case BX_DOL_STUDIO_BP_SKELETONS:
  168. $sResult = 'qrcode';
  169. break;
  170. default:
  171. $sResult = parent::getModuleIcon($sName, $sType, $bReturnAsUrl);
  172. }
  173. return $sResult;
  174. }
  175. }
  176. /** @} */