BxDolStudioBuilderPage.php 7.6 KB

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