BxBaseModTextFormEntry.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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('modules/base/text/js/|polls.js', '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. public function insert ($aValsToAdd = array(), $isIgnore = false)
  70. {
  71. $CNF = $this->_oModule->_oConfig->CNF;
  72. $bMulticatEnabled = $this->_isMulticatEnabled();
  73. if ($bMulticatEnabled)
  74. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  75. $iContentId = parent::insert($aValsToAdd, $isIgnore);
  76. if(!empty($iContentId)){
  77. if ($bMulticatEnabled)
  78. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  79. }
  80. return $iContentId;
  81. }
  82. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  83. {
  84. $CNF = $this->_oModule->_oConfig->CNF;
  85. $bMulticatEnabled = $this->_isMulticatEnabled();
  86. if ($bMulticatEnabled)
  87. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  88. $iResult = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  89. if ($bMulticatEnabled)
  90. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  91. return $iResult;
  92. }
  93. protected function genCustomInputAttachments ($aInput)
  94. {
  95. return '__attachments_menu__';
  96. }
  97. protected function genCustomInputPolls ($aInput)
  98. {
  99. return $this->_oModule->_oTemplate->getPollField(!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0);
  100. }
  101. public function processPolls ($sFieldPoll, $iContentId = 0)
  102. {
  103. $CNF = &$this->_oModule->_oConfig->CNF;
  104. if (!isset($this->aInputs[$sFieldPoll]))
  105. return true;
  106. $aPollIds = $this->getCleanValue($sFieldPoll);
  107. if(empty($aPollIds) || !is_array($aPollIds))
  108. return true;
  109. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  110. $aPollsDbIds = $this->_oModule->_oDb->getPolls(array('type' => 'content_id_ids', 'content_id' => $iContentId));
  111. //--- Remove deleted
  112. $this->_oModule->_oDb->deletePollsByIds(array_diff($aPollsDbIds, $aPollIds));
  113. //--- Add new
  114. if($iContentId) {
  115. $aPollsAddIds = array_diff($aPollIds, $aPollsDbIds);
  116. foreach($aPollsAddIds as $iPollId)
  117. $this->_oModule->_oDb->updatePolls(array($CNF['FIELD_POLL_CONTENT_ID'] => $iContentId), array($CNF['FIELD_POLL_ID'] => $iPollId, $CNF['FIELD_POLL_CONTENT_ID'] => 0));
  118. }
  119. return true;
  120. }
  121. /**
  122. *
  123. * MultiCategories related methods.
  124. *
  125. */
  126. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  127. if (isset($this->aInputs[$sFieldName])){
  128. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  129. return trim($sTmp);
  130. }));
  131. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  132. }
  133. }
  134. protected function processMulticatAfter($sFieldName, $iContentId){
  135. $CNF = &$this->_oModule->_oConfig->CNF;
  136. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  137. $oCategories = BxDolCategories::getInstance();
  138. if (isset($this->aInputs[$sFieldName])){
  139. $oCategories->delete($this->_oModule->getName(), $iContentId);
  140. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  141. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  142. }
  143. }
  144. }
  145. protected function genCustomViewRowValueMulticat(&$aInput)
  146. {
  147. $oCategories = BxDolCategories::getInstance();
  148. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  149. if (count($aValues) > 0){
  150. $aVars = array('bx_repeat:cats' => array());
  151. foreach ($aValues as $sValue) {
  152. $aVars['bx_repeat:cats'][] = array(
  153. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  154. 'name' => _t($sValue),
  155. 'bx_if:more' => array(
  156. 'condition' => $sValue === end($aValues) ? false : true,
  157. 'content' => array('1')
  158. ),
  159. );
  160. }
  161. if (!$aVars['bx_repeat:cats'])
  162. return '';
  163. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  164. }
  165. return '';
  166. }
  167. protected function genCustomInputMulticat(&$aInput)
  168. {
  169. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  170. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  171. $aSelectedItems = array();
  172. if (isset($aInput['value']) && is_array($aInput['value']))
  173. $aInput['value'] = array_filter($aInput['value']);
  174. if(!empty($aInput['value'])) {
  175. if (!is_array($aInput['value']))
  176. $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 )));
  177. else
  178. $aValues = $aInput['value'];
  179. $aValues = array_filter($aValues);
  180. foreach($aValues as $sValue) {
  181. if (!array_key_exists($sValue, $aValuesForSelect)){
  182. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  183. }
  184. }
  185. foreach($aValues as $sValue) {
  186. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  187. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  188. }
  189. }
  190. else{
  191. $aSelectedItems = array(
  192. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  193. );
  194. }
  195. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  196. 'bx_repeat:items' => $aSelectedItems,
  197. 'js_object' => $sJsObject,
  198. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  199. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  200. 'js_object' => $sJsObject,
  201. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  202. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  203. ));
  204. }
  205. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  206. {
  207. foreach($aValues as $sKey => $aValue) {
  208. $aValues[$sKey]['value'] = _t($aValue['key']);
  209. }
  210. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  211. $aInput['type'] = 'select';
  212. $aInput['name'] .= '[]';
  213. $aInput['value'] = $mixedValue;
  214. $aInput['values'] = $aValues;
  215. return $this->genInput($aInput);
  216. }
  217. protected function genCustomInputMulticatInput($aInput)
  218. {
  219. $aInput['type'] = 'text';
  220. $aInput['name'] .= '[]';
  221. $aInput['value'] = '';
  222. return $this->genInput($aInput);
  223. }
  224. protected function genCustomInputMulticatButton($aInput)
  225. {
  226. $sName = $aInput['name'];
  227. $aInput['type'] = 'button';
  228. $aInput['name'] .= '_add';
  229. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  230. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  231. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  232. return $this->genInputButton($aInput);
  233. }
  234. protected function genCustomInputMulticatButtonNew($aInput)
  235. {
  236. $sName = $aInput['name'];
  237. $aInput['type'] = 'button';
  238. $aInput['name'] .= '_add';
  239. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  240. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  241. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  242. return $this->genInputButton($aInput);
  243. }
  244. protected function _getVideoGhostTmplVars($aContentInfo = array())
  245. {
  246. $CNF = &$this->_oModule->_oConfig->CNF;
  247. return array (
  248. 'name' => $this->aInputs[$CNF['FIELD_VIDEO']]['name'],
  249. 'content_id' => (int)$this->aInputs[$CNF['FIELD_VIDEO']]['content_id'],
  250. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  251. 'embed_url' => BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'file_embed_video/',
  252. );
  253. }
  254. protected function _getFileGhostTmplVars($aContentInfo = array())
  255. {
  256. $CNF = &$this->_oModule->_oConfig->CNF;
  257. return array (
  258. 'name' => $this->aInputs[$CNF['FIELD_FILE']]['name'],
  259. 'content_id' => (int)$this->aInputs[$CNF['FIELD_FILE']]['content_id'],
  260. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : ''
  261. );
  262. }
  263. protected function _isMulticatEnabled(){
  264. $CNF = $this->_oModule->_oConfig->CNF;
  265. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  266. }
  267. }
  268. /** @} */