1
0

BxDolStudioBuilderPage.php 7.9 KB

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