BxBaseModTextFormEntry.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 BaseText Base classes for text modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit entry form
  13. */
  14. class BxBaseModTextFormEntry extends BxBaseModGeneralFormEntry
  15. {
  16. protected $_sGhostTemplateVideo = 'form_ghost_template_video.html';
  17. protected $_sGhostTemplateFile = 'form_ghost_template_file.html';
  18. public function __construct($aInfo, $oTemplate = false)
  19. {
  20. parent::__construct($aInfo, $oTemplate);
  21. }
  22. function getCode($bDynamicMode = false)
  23. {
  24. $CNF = &$this->_oModule->_oConfig->CNF;
  25. $sResult = parent::getCode($bDynamicMode);
  26. if(!empty($CNF['OBJECT_MENU_ENTRY_ATTACHMENTS']))
  27. $sResult = $this->_oModule->_oTemplate->parseHtmlByContent($sResult, array(
  28. 'attachments_menu' => BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_ENTRY_ATTACHMENTS'])->getCode()
  29. ));
  30. if(isset($CNF['PARAM_POLL_ENABLED']) && $CNF['PARAM_POLL_ENABLED'] === true) {
  31. $sInclude = '';
  32. $sInclude .= $this->_oModule->_oTemplate->addCss(array('polls.css'), $bDynamicMode);
  33. $sInclude .= $this->_oModule->_oTemplate->addJs(array('polls.js'), $bDynamicMode);
  34. $sResult .= ($bDynamicMode ? $sInclude : '') . $this->_oModule->_oTemplate->getJsCode('poll');
  35. }
  36. if(isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true) {
  37. $sInclude2 = '';
  38. $sInclude2 .= $this->_oModule->_oTemplate->addJs(array('categories.js'), $bDynamicMode);
  39. $sInclude2 .= $this->_oModule->_oTemplate->addCss(array('categories.css'), $bDynamicMode);
  40. $sResult .= ($bDynamicMode ? $sInclude2 : '') . $this->_oModule->_oTemplate->getJsCode('categories');
  41. }
  42. return $sResult;
  43. }
  44. function initChecker ($aValues = array (), $aSpecificValues = array())
  45. {
  46. $CNF = &$this->_oModule->_oConfig->CNF;
  47. $bValues = $aValues && !empty($aValues['id']);
  48. $aContentInfo = $bValues ? $this->_oModule->_oDb->getContentInfoById($aValues['id']) : false;
  49. if (isset($CNF['FIELD_VIDEO']) && isset($this->aInputs[$CNF['FIELD_VIDEO']])) {
  50. if ($bValues)
  51. $this->aInputs[$CNF['FIELD_VIDEO']]['content_id'] = $aValues['id'];
  52. $this->aInputs[$CNF['FIELD_VIDEO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplateVideo, $this->_getVideoGhostTmplVars($aContentInfo));
  53. }
  54. if (isset($CNF['FIELD_FILE']) && isset($this->aInputs[$CNF['FIELD_FILE']])) {
  55. if ($bValues)
  56. $this->aInputs[$CNF['FIELD_FILE']]['content_id'] = $aValues['id'];
  57. $this->aInputs[$CNF['FIELD_FILE']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplateFile, $this->_getFileGhostTmplVars($aContentInfo));
  58. }
  59. if (isset($CNF['FIELD_POLL']) && isset($this->aInputs[$CNF['FIELD_POLL']])) {
  60. if ($bValues)
  61. $this->aInputs[$CNF['FIELD_POLL']]['content_id'] = $aValues['id'];
  62. }
  63. if (isset($CNF['FIELD_MULTICAT']) && isset($this->aInputs[$CNF['FIELD_MULTICAT']]) && $CNF['PARAM_MULTICAT_ENABLED']) {
  64. if ($bValues)
  65. $this->aInputs[$CNF['FIELD_MULTICAT']]['content_id'] = $aValues['id'];
  66. }
  67. parent::initChecker ($aValues, $aSpecificValues);
  68. }
  69. protected function genCustomInputAttachments ($aInput)
  70. {
  71. return '__attachments_menu__';
  72. }
  73. protected function genCustomInputPolls ($aInput)
  74. {
  75. return $this->_oModule->_oTemplate->getPollField(!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0);
  76. }
  77. public function processPolls ($sFieldPoll, $iContentId = 0)
  78. {
  79. $CNF = &$this->_oModule->_oConfig->CNF;
  80. if (!isset($this->aInputs[$sFieldPoll]))
  81. return true;
  82. $aPollIds = $this->getCleanValue($sFieldPoll);
  83. if(empty($aPollIds) || !is_array($aPollIds))
  84. return true;
  85. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  86. $aPollsDbIds = $this->_oModule->_oDb->getPolls(array('type' => 'content_id_ids', 'content_id' => $iContentId));
  87. //--- Remove deleted
  88. $this->_oModule->_oDb->deletePollsByIds(array_diff($aPollsDbIds, $aPollIds));
  89. //--- Add new
  90. if($iContentId) {
  91. $aPollsAddIds = array_diff($aPollIds, $aPollsDbIds);
  92. foreach($aPollsAddIds as $iPollId)
  93. $this->_oModule->_oDb->updatePolls(array($CNF['FIELD_POLL_CONTENT_ID'] => $iContentId), array($CNF['FIELD_POLL_ID'] => $iPollId, $CNF['FIELD_POLL_CONTENT_ID'] => 0));
  94. }
  95. return true;
  96. }
  97. /**
  98. *
  99. * MultiCategories related methods.
  100. *
  101. */
  102. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  103. if (isset($this->aInputs[$sFieldName])){
  104. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  105. return trim($sTmp);
  106. }));
  107. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  108. }
  109. }
  110. protected function processMulticatAfter($sFieldName, $iContentId){
  111. $CNF = &$this->_oModule->_oConfig->CNF;
  112. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  113. $oCategories = BxDolCategories::getInstance();
  114. if (isset($this->aInputs[$sFieldName])){
  115. $oCategories->delete($this->_oModule->getName(), $iContentId);
  116. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  117. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  118. }
  119. }
  120. }
  121. protected function genCustomViewRowValueMulticat(&$aInput)
  122. {
  123. $oCategories = BxDolCategories::getInstance();
  124. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  125. if (count($aValues) > 0){
  126. $aVars = array('bx_repeat:cats' => array());
  127. foreach ($aValues as $sValue) {
  128. $aVars['bx_repeat:cats'][] = array(
  129. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  130. 'name' => _t($sValue),
  131. 'bx_if:more' => array(
  132. 'condition' => $sValue === end($aValues) ? false : true,
  133. 'content' => array('1')
  134. ),
  135. );
  136. }
  137. if (!$aVars['bx_repeat:cats'])
  138. return '';
  139. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  140. }
  141. return '';
  142. }
  143. protected function genCustomInputMulticat(&$aInput)
  144. {
  145. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  146. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  147. $aSelectedItems = array();
  148. if (isset($aInput['value']) && is_array($aInput['value']))
  149. $aInput['value'] = array_filter($aInput['value']);
  150. if(!empty($aInput['value'])) {
  151. if (!is_array($aInput['value']))
  152. $aValues = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  153. else
  154. $aValues = $aInput['value'];
  155. $aValues = array_filter($aValues);
  156. foreach($aValues as $sValue) {
  157. if (!array_key_exists($sValue, $aValuesForSelect)){
  158. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  159. }
  160. }
  161. foreach($aValues as $sValue) {
  162. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  163. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  164. }
  165. }
  166. else{
  167. $aSelectedItems = array(
  168. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  169. );
  170. }
  171. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  172. 'bx_repeat:items' => $aSelectedItems,
  173. 'js_object' => $sJsObject,
  174. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  175. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  176. 'js_object' => $sJsObject,
  177. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  178. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  179. ));
  180. }
  181. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  182. {
  183. foreach($aValues as $sKey => $aValue) {
  184. $aValues[$sKey]['value'] = _t($aValue['key']);
  185. }
  186. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  187. $aInput['type'] = 'select';
  188. $aInput['name'] .= '[]';
  189. $aInput['value'] = $mixedValue;
  190. $aInput['values'] = $aValues;
  191. return $this->genInput($aInput);
  192. }
  193. protected function genCustomInputMulticatInput($aInput)
  194. {
  195. $aInput['type'] = 'text';
  196. $aInput['name'] .= '[]';
  197. $aInput['value'] = '';
  198. return $this->genInput($aInput);
  199. }
  200. protected function genCustomInputMulticatButton($aInput)
  201. {
  202. $sName = $aInput['name'];
  203. $aInput['type'] = 'button';
  204. $aInput['name'] .= '_add';
  205. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  206. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  207. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  208. return $this->genInputButton($aInput);
  209. }
  210. protected function genCustomInputMulticatButtonNew($aInput)
  211. {
  212. $sName = $aInput['name'];
  213. $aInput['type'] = 'button';
  214. $aInput['name'] .= '_add';
  215. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  216. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  217. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  218. return $this->genInputButton($aInput);
  219. }
  220. protected function _getVideoGhostTmplVars($aContentInfo = array())
  221. {
  222. $CNF = &$this->_oModule->_oConfig->CNF;
  223. return array (
  224. 'name' => $this->aInputs[$CNF['FIELD_VIDEO']]['name'],
  225. 'content_id' => (int)$this->aInputs[$CNF['FIELD_VIDEO']]['content_id'],
  226. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  227. 'embed_url' => BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'file_embed_video/',
  228. );
  229. }
  230. protected function _getFileGhostTmplVars($aContentInfo = array())
  231. {
  232. $CNF = &$this->_oModule->_oConfig->CNF;
  233. return array (
  234. 'name' => $this->aInputs[$CNF['FIELD_FILE']]['name'],
  235. 'content_id' => (int)$this->aInputs[$CNF['FIELD_FILE']]['content_id'],
  236. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : ''
  237. );
  238. }
  239. }
  240. /** @} */