BxPostsFormEntry.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 Posts Posts
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit entry form
  13. */
  14. class BxPostsFormEntry extends BxBaseModTextFormEntry
  15. {
  16. protected $_sGhostTemplateCover = 'form_ghost_template_cover.html';
  17. public function __construct($aInfo, $oTemplate = false)
  18. {
  19. $this->MODULE = 'bx_posts';
  20. parent::__construct($aInfo, $oTemplate);
  21. $CNF = &$this->_oModule->_oConfig->CNF;
  22. if(isset($CNF['FIELD_COVER']) && isset($this->aInputs[$CNF['FIELD_COVER']])) {
  23. if($this->_oModule->checkAllowedSetThumb() === CHECK_ACTION_RESULT_ALLOWED) {
  24. $this->aInputs[$CNF['FIELD_COVER']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  25. $this->aInputs[$CNF['FIELD_COVER']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_COVER']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_COVER']]['value']) : $CNF['OBJECT_UPLOADERS'];
  26. $this->aInputs[$CNF['FIELD_COVER']]['upload_buttons_titles'] = array(
  27. 'Simple' => _t('_bx_posts_form_entry_input_covers_uploader_simple_title'),
  28. 'HTML5' => _t('_bx_posts_form_entry_input_covers_uploader_html5_title')
  29. );
  30. $this->aInputs[$CNF['FIELD_COVER']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  31. $this->aInputs[$CNF['FIELD_COVER']]['storage_private'] = 0;
  32. $this->aInputs[$CNF['FIELD_COVER']]['multiple'] = false;
  33. $this->aInputs[$CNF['FIELD_COVER']]['content_id'] = 0;
  34. $this->aInputs[$CNF['FIELD_COVER']]['ghost_template'] = '';
  35. }
  36. else
  37. unset($this->aInputs[$CNF['FIELD_COVER']]);
  38. }
  39. if(isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  40. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE_PHOTOS'];
  41. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  42. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW_PHOTOS'];
  43. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  44. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  45. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  46. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  47. $this->aInputs[$CNF['FIELD_PHOTO']]['tr_attrs'] = array('class'=> 'bx-base-text-attachment-item');
  48. }
  49. if(isset($this->aInputs[$CNF['FIELD_VIDEO']])) {
  50. $this->aInputs[$CNF['FIELD_VIDEO']]['storage_object'] = $CNF['OBJECT_STORAGE_VIDEOS'];
  51. $this->aInputs[$CNF['FIELD_VIDEO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_VIDEO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_VIDEO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  52. $this->aInputs[$CNF['FIELD_VIDEO']]['images_transcoder'] = $CNF['OBJECT_VIDEOS_TRANSCODERS']['poster_preview'];
  53. $this->aInputs[$CNF['FIELD_VIDEO']]['storage_private'] = 0;
  54. $this->aInputs[$CNF['FIELD_VIDEO']]['multiple'] = true;
  55. $this->aInputs[$CNF['FIELD_VIDEO']]['content_id'] = 0;
  56. $this->aInputs[$CNF['FIELD_VIDEO']]['ghost_template'] = '';
  57. $this->aInputs[$CNF['FIELD_VIDEO']]['tr_attrs'] = array('class'=> 'bx-base-text-attachment-item');
  58. }
  59. if (isset($CNF['FIELD_FILE']) && isset($this->aInputs[$CNF['FIELD_FILE']])) {
  60. $this->aInputs[$CNF['FIELD_FILE']]['storage_object'] = $CNF['OBJECT_STORAGE_FILES'];
  61. $this->aInputs[$CNF['FIELD_FILE']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_FILE']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_FILE']]['value']) : $CNF['OBJECT_UPLOADERS'];
  62. $this->aInputs[$CNF['FIELD_FILE']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW_FILES'];
  63. $this->aInputs[$CNF['FIELD_FILE']]['storage_private'] = 0;
  64. $this->aInputs[$CNF['FIELD_FILE']]['multiple'] = true;
  65. $this->aInputs[$CNF['FIELD_FILE']]['content_id'] = 0;
  66. $this->aInputs[$CNF['FIELD_FILE']]['ghost_template'] = '';
  67. $this->aInputs[$CNF['FIELD_FILE']]['tr_attrs'] = array('class'=> 'bx-base-text-attachment-item');
  68. }
  69. if(isset($this->aInputs[$CNF['FIELD_POLL']])) {
  70. $this->aInputs[$CNF['FIELD_POLL']]['tr_attrs'] = array('class'=> 'bx-base-text-attachment-item');
  71. }
  72. }
  73. public function getCode($bDynamicMode = false)
  74. {
  75. $sJs = $this->_oModule->_oTemplate->addJs(array('categories.js'), $bDynamicMode);
  76. $sJs = $this->_oModule->_oTemplate->addCss(array('categories.css'));
  77. $sCode = '';
  78. if($bDynamicMode)
  79. $sCode .= $sJs;
  80. $sCode .= $this->_oModule->_oTemplate->getJsCode('categories');
  81. $sCode .= parent::getCode($bDynamicMode);
  82. return $sCode;
  83. }
  84. function initChecker ($aValues = array (), $aSpecificValues = array())
  85. {
  86. $CNF = &$this->_oModule->_oConfig->CNF;
  87. $bValues = $aValues && !empty($aValues['id']);
  88. $aContentInfo = $bValues ? $this->_oModule->_oDb->getContentInfoById($aValues['id']) : false;
  89. if($this->aParams['display'] == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'] && isset($CNF['FIELD_PUBLISHED']) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  90. if($bValues && in_array($aValues[$CNF['FIELD_STATUS']], array('active', 'hidden')))
  91. unset($this->aInputs[$CNF['FIELD_PUBLISHED']]);
  92. if (isset($CNF['FIELD_COVER']) && isset($this->aInputs[$CNF['FIELD_COVER']])) {
  93. if($bValues)
  94. $this->aInputs[$CNF['FIELD_COVER']]['content_id'] = $aValues['id'];
  95. $this->aInputs[$CNF['FIELD_COVER']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplateCover, $this->_getCoverGhostTmplVars($aContentInfo));
  96. }
  97. parent::initChecker ($aValues, $aSpecificValues);
  98. }
  99. public function insert ($aValsToAdd = array(), $isIgnore = false)
  100. {
  101. $CNF = &$this->_oModule->_oConfig->CNF;
  102. if(isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']])) {
  103. $iAdded = 0;
  104. if(isset($this->aInputs[$CNF['FIELD_ADDED']]))
  105. $iAdded = $this->getCleanValue($CNF['FIELD_ADDED']);
  106. if(empty($iAdded))
  107. $iAdded = time();
  108. $aValsToAdd[$CNF['FIELD_ADDED']] = $iAdded;
  109. }
  110. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']])) {
  111. $iPublished = 0;
  112. if(isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  113. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  114. if(empty($iPublished))
  115. $iPublished = time();
  116. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  117. }
  118. $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? 'awaiting' : 'active';
  119. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  120. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  121. if(!empty($iContentId)){
  122. $this->processFiles($CNF['FIELD_COVER'], $iContentId, true);
  123. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  124. }
  125. return $iContentId;
  126. }
  127. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  128. {
  129. $CNF = &$this->_oModule->_oConfig->CNF;
  130. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']]) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']])) {
  131. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  132. if(empty($iPublished))
  133. $iPublished = time();
  134. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  135. }
  136. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  137. $iResult = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  138. $this->processFiles($CNF['FIELD_COVER'], $iContentId, false);
  139. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  140. return $iResult;
  141. }
  142. protected function _getCoverGhostTmplVars($aContentInfo = array())
  143. {
  144. $CNF = &$this->_oModule->_oConfig->CNF;
  145. return array (
  146. 'name' => $this->aInputs[$CNF['FIELD_COVER']]['name'],
  147. 'content_id' => $this->aInputs[$CNF['FIELD_COVER']]['content_id'],
  148. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  149. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  150. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : ''
  151. );
  152. }
  153. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  154. {
  155. $CNF = &$this->_oModule->_oConfig->CNF;
  156. return array (
  157. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  158. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  159. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : ''
  160. );
  161. }
  162. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  163. if (isset($this->aInputs[$sFieldName])){
  164. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  165. return trim($sTmp);
  166. }));
  167. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  168. }
  169. }
  170. protected function processMulticatAfter($sFieldName, $iContentId){
  171. if (isset($this->aInputs[$sFieldName])){
  172. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  173. $this->_oModule->_oDb->addCategory($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue);
  174. }
  175. }
  176. }
  177. protected function genCustomViewRowValueMulticat(&$aInput)
  178. {
  179. $aCats = explode(',', $aInput['value']);
  180. if (count($aCats) > 0){
  181. $aVars = array('bx_repeat:cats' => array());
  182. foreach ($aCats as $sKey => $sValue) {
  183. $aVars['bx_repeat:cats'][] = array(
  184. 'url' => $this->_oModule->getCategoriesMultiUrl($sValue),
  185. 'name' => $sValue,
  186. 'bx_if:more' => array(
  187. 'condition' => $sValue === end($aCats) ? false : true,
  188. 'content' => array('1')
  189. ),
  190. );
  191. }
  192. if (!$aVars['bx_repeat:cats'])
  193. return '';
  194. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  195. }
  196. return '';
  197. }
  198. protected function genCustomInputMulticat(&$aInput)
  199. {
  200. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  201. $aValuesList = $this->_oModule->_oDb->getCategories($this->_oModule->getName(), bx_get_logged_profile_id());
  202. if(!empty($aInput['value'])) {
  203. if (!is_array($aInput['value']))
  204. $aValues = explode(',', $aInput['value']);
  205. else
  206. $aValues = $aInput['value'];
  207. $aTmplVarsSubentries = array();
  208. foreach($aValues as $sValue) {
  209. if (!array_key_exists($sValue, $aValuesList)){
  210. $aValuesList[$sValue] = array('key' => $sValue, 'value' => $sValue);
  211. }
  212. }
  213. foreach($aValues as $sValue) {
  214. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesList, $sValue);
  215. $aTmplVarsSubentries[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  216. }
  217. }
  218. else{
  219. $aTmplVarsSubentries = array(
  220. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesList))
  221. );
  222. }
  223. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  224. 'bx_repeat:items' => $aTmplVarsSubentries,
  225. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  226. 'js_object' => $sJsObject,
  227. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  228. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  229. ));
  230. }
  231. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  232. {
  233. $aInput['type'] = 'select';
  234. $aInput['name'] .= '[]';
  235. $aInput['value'] = $mixedValue;
  236. $aInput['values'] = $aValues;
  237. return $this->genInput($aInput);
  238. }
  239. protected function genCustomInputMulticatInput($aInput)
  240. {
  241. $aInput['type'] = 'text';
  242. $aInput['name'] .= '[]';
  243. $aInput['value'] = '';
  244. return $this->genInput($aInput);
  245. }
  246. protected function genCustomInputMulticatButton($aInput)
  247. {
  248. $sName = $aInput['name'];
  249. $aInput['type'] = 'button';
  250. $aInput['name'] .= '_add';
  251. $aInput['value'] = _t('_bx_posts_form_entry_input_category_add');
  252. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  253. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  254. return $this->genInputButton($aInput);
  255. }
  256. protected function genCustomInputMulticatButtonNew($aInput)
  257. {
  258. $sName = $aInput['name'];
  259. $aInput['type'] = 'button';
  260. $aInput['name'] .= '_add';
  261. $aInput['value'] = _t('_bx_posts_form_entry_input_category_add_new');
  262. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  263. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  264. return $this->genInputButton($aInput);
  265. }
  266. }
  267. /** @} */