BxDolStudioBuilderPage.php 7.7 KB

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