BxTimelineFormPost.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 Timeline Timeline
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit entry form
  13. */
  14. class BxTimelineFormPost extends BxBaseModGeneralFormEntry
  15. {
  16. protected $_bAccountMode;
  17. protected $_bPublicMode;
  18. protected $_bProfileMode;
  19. protected $_bVisibilityAutoselect;
  20. public function __construct($aInfo, $oTemplate = false)
  21. {
  22. $this->MODULE = 'bx_timeline';
  23. parent::__construct($aInfo, $oTemplate);
  24. $this->_bAccountMode = isset($this->aParams['display']) && $this->aParams['display'] == $this->_oModule->_oConfig->getObject('form_display_post_add');
  25. $this->_bPublicMode = isset($this->aParams['display']) && $this->aParams['display'] == $this->_oModule->_oConfig->getObject('form_display_post_add_public');
  26. $this->_bProfileMode = isset($this->aParams['display']) && $this->aParams['display'] == $this->_oModule->_oConfig->getObject('form_display_post_add_profile');
  27. $this->_bVisibilityAutoselect = true;
  28. }
  29. public function getCode($bDynamicMode = false)
  30. {
  31. $sResult = parent::getCode($bDynamicMode);
  32. return $this->_oModule->_oTemplate->parseHtmlByContent($sResult, array(
  33. 'attachments_menu' => $this->_oModule->getAttachmentsMenuObject()->getCode()
  34. ));
  35. }
  36. function initChecker($aValues = array (), $aSpecificValues = array())
  37. {
  38. $CNF = &$this->_oModule->_oConfig->CNF;
  39. if($this->aParams['display'] == $this->_oModule->_oConfig->getObject('form_display_post_edit') && isset($CNF['FIELD_PUBLISHED']) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  40. if(isset($aValues[$CNF['FIELD_STATUS']]) && in_array($aValues[$CNF['FIELD_STATUS']], array(BX_TIMELINE_STATUS_ACTIVE, BX_TIMELINE_STATUS_HIDDEN)))
  41. unset($this->aInputs[$CNF['FIELD_PUBLISHED']]);
  42. if(isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($aValues[$CNF['FIELD_OBJECT_ID']]))
  43. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_OBJECT_ID']] < 0;
  44. parent::initChecker ($aValues, $aSpecificValues);
  45. }
  46. public function insert($aValsToAdd = array(), $isIgnore = false)
  47. {
  48. $CNF = &$this->_oModule->_oConfig->CNF;
  49. $aValsToAdd[$CNF['FIELD_SYSTEM']] = 0;
  50. $aValsToAdd[$CNF['FIELD_OBJECT_ID']] *= isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  51. if(isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']])) {
  52. $iAdded = 0;
  53. if(isset($this->aInputs[$CNF['FIELD_ADDED']]))
  54. $iAdded = $this->getCleanValue($CNF['FIELD_ADDED']);
  55. if(empty($iAdded))
  56. $iAdded = time();
  57. $aValsToAdd[$CNF['FIELD_ADDED']] = $iAdded;
  58. }
  59. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']])) {
  60. $iPublished = 0;
  61. if(isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  62. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  63. if(empty($iPublished))
  64. $iPublished = time();
  65. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  66. }
  67. $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? BX_TIMELINE_STATUS_AWAITING : BX_TIMELINE_STATUS_ACTIVE;
  68. return parent::insert ($aValsToAdd, $isIgnore);
  69. }
  70. function update($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  71. {
  72. $CNF = &$this->_oModule->_oConfig->CNF;
  73. if (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']])) {
  74. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  75. $aValsToAdd[$CNF['FIELD_OBJECT_ID']] = ($this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * abs($aContentInfo[$CNF['FIELD_OBJECT_ID']]);
  76. }
  77. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']]) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']])) {
  78. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  79. if(empty($iPublished))
  80. $iPublished = time();
  81. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  82. }
  83. return parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  84. }
  85. public function addHtmlEditor($iViewMode, &$aInput)
  86. {
  87. $CNF = &$this->_oModule->_oConfig->CNF;
  88. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  89. if (!$oEditor)
  90. return false;
  91. if(in_array($this->aParams['display'], array($this->_oModule->_oConfig->getObject('form_display_post_add'), $this->_oModule->_oConfig->getObject('form_display_post_add_public'))))
  92. $oEditor->setCustomConf("
  93. placeholderText: '" . _t('_bx_timeline_txt_some_text_here') . "',
  94. initOnClick: false,
  95. ");
  96. if(!$this->_oModule->_oConfig->isEditorToolbar())
  97. $oEditor->setCustomToolbarButtons('');
  98. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode);
  99. return true;
  100. }
  101. public function init()
  102. {
  103. $iUserId = $this->_oModule->getUserId();
  104. $iOwnerId = $this->_oModule->getOwnerId();
  105. $this->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'post/';
  106. $this->aInputs['owner_id']['value'] = $iOwnerId;
  107. if(isset($this->aInputs['text'])) {
  108. if(empty($this->aInputs['text']['attrs']) || !is_array($this->aInputs['text']['attrs']))
  109. $this->aInputs['text']['attrs'] = array();
  110. $this->aInputs['text']['attrs']['id'] = $this->_oModule->_oConfig->getHtmlIds('post', 'textarea') . time();
  111. }
  112. $aPrivacyFields = $this->_getPrivacyFields();
  113. foreach($aPrivacyFields as $sFieldKey => $sObject)
  114. $this->_initPrivacyField($sFieldKey, $sObject);
  115. if(isset($this->aInputs['link']))
  116. $this->aInputs['link']['content'] = $this->_oModule->_oTemplate->getAttachLinkField($iUserId);
  117. if(isset($this->aInputs['photo'])) {
  118. $aFormNested = array(
  119. 'params' =>array(
  120. 'nested_form_template' => 'uploader_nfw.html'
  121. ),
  122. 'inputs' => array(),
  123. );
  124. $oFormNested = new BxDolFormNested('photo', $aFormNested, 'tlb_do_submit', $this->_oModule->_oTemplate);
  125. $this->aInputs['photo']['storage_object'] = $this->_oModule->_oConfig->getObject('storage_photos');
  126. $this->aInputs['photo']['images_transcoder'] = $this->_oModule->_oConfig->getObject('transcoder_photos_preview');
  127. $this->aInputs['photo']['uploaders'] = !empty($this->aInputs['photo']['value']) ? unserialize($this->aInputs['photo']['value']) : $this->_oModule->_oConfig->getUploaders('photo');
  128. $this->aInputs['photo']['upload_buttons_titles'] = array('Simple' => 'camera');
  129. $this->aInputs['photo']['multiple'] = true;
  130. $this->aInputs['photo']['ghost_template'] = $oFormNested;
  131. }
  132. if(isset($this->aInputs['video'])) {
  133. $aFormNested = array(
  134. 'params' =>array(
  135. 'nested_form_template' => 'uploader_nfw.html'
  136. ),
  137. 'inputs' => array(),
  138. );
  139. $oFormNested = new BxDolFormNested('video', $aFormNested, 'tlb_do_submit', $this->_oModule->_oTemplate);
  140. $this->aInputs['video']['storage_object'] = $this->_oModule->_oConfig->getObject('storage_videos');
  141. $this->aInputs['video']['images_transcoder'] = $this->_oModule->_oConfig->getObject('transcoder_videos_poster');
  142. $this->aInputs['video']['uploaders'] = !empty($this->aInputs['video']['value']) ? unserialize($this->aInputs['video']['value']) : $this->_oModule->_oConfig->getUploaders('video');
  143. $this->aInputs['video']['upload_buttons_titles'] = array('Simple' => 'video');
  144. $this->aInputs['video']['multiple'] = true;
  145. $this->aInputs['video']['ghost_template'] = $oFormNested;
  146. }
  147. if(isset($this->aInputs['attachments']))
  148. $this->aInputs['attachments']['content'] = '__attachments_menu__';
  149. if (isset($this->_oModule->_oConfig->CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$this->_oModule->_oConfig->CNF['FIELD_LOCATION_PREFIX']]))
  150. $this->aInputs[$this->_oModule->_oConfig->CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = false;
  151. }
  152. public function setVisibilityAutoselect($bVisibilityAutoselect)
  153. {
  154. $this->_bVisibilityAutoselect = $bVisibilityAutoselect;
  155. }
  156. protected function _getPrivacyFields()
  157. {
  158. $CNF = &$this->_oModule->_oConfig->CNF;
  159. $aFields = array(
  160. 'FIELD_OBJECT_PRIVACY_VIEW' => 'OBJECT_PRIVACY_VIEW'
  161. );
  162. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  163. $aFields = $CNF['PRIVACY_FIELD_TO_OBJECT'];
  164. return $aFields;
  165. }
  166. /**
  167. * The method inherited from BxBaseModGeneralFormEntry class is disabled
  168. * because own variant (method BxTimelineFormPost::_initPrivacyField) is
  169. * used instead.
  170. */
  171. protected function _preparePrivacyField($sFieldKey, $sObject)
  172. {
  173. return;
  174. }
  175. protected function _initPrivacyField($sFieldKey, $sObject)
  176. {
  177. $CNF = &$this->_oModule->_oConfig->CNF;
  178. if(!isset($CNF[$sFieldKey]) || !isset($this->aInputs[$CNF[$sFieldKey]]) || !isset($CNF[$sObject]))
  179. return;
  180. $sField = $CNF[$sFieldKey];
  181. $iUserId = $this->_oModule->getUserId();
  182. $iOwnerId = $this->_oModule->getOwnerId();
  183. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], BxDolPrivacy::getGroupChooser($CNF[$sObject], 0, array(
  184. 'title' => _t($CNF['T']['form_input_title_' . $sField]),
  185. 'dynamic_mode' => $this->_bDynamicMode
  186. )));
  187. //--- Show field as is when 'Autoselect' is disabled.
  188. if(!$this->_bVisibilityAutoselect)
  189. return;
  190. //--- Show default privacy groups and followed contexts on Account (Profile + Connections) post form.
  191. if($this->_bAccountMode)
  192. return;
  193. //--- Preselect Context and hide privacy selector when posting in some context (profile, group, space, etc).
  194. if($this->_bProfileMode && $iOwnerId != $iUserId) {
  195. $this->aInputs[$sField]['type'] = 'hidden';
  196. $this->aInputs[$sField]['value'] = -$iOwnerId;
  197. return;
  198. }
  199. $bProfileModeOwner = $this->_bProfileMode && $iOwnerId == $iUserId;
  200. if($this->_bPublicMode || $bProfileModeOwner) {
  201. $iGc = 0;
  202. $iKeyGh = false;
  203. foreach($this->aInputs[$sField]['values'] as $iKey => $aValue) {
  204. if(isset($aValue['type']) && in_array($aValue['type'], array('group_header', 'group_end'))) {
  205. if($iKeyGh !== false && $iGc == 0) {
  206. unset($this->aInputs[$sField]['values'][$iKeyGh]);
  207. $iKeyGh = false;
  208. if($aValue['type'] == 'group_end')
  209. unset($this->aInputs[$sField]['values'][$iKey]);
  210. }
  211. if($aValue['type'] == 'group_header') {
  212. $iGc = 0;
  213. $iKeyGh = $iKey;
  214. }
  215. continue;
  216. }
  217. //--- Show 'Public' privacy group only in Public post form.
  218. if($this->_bPublicMode && $aValue['key'] == BX_DOL_PG_ALL) {
  219. $iGc += 1;
  220. continue;
  221. }
  222. //--- Show a default privacy groups in Profile (for Owner) post form.
  223. if($bProfileModeOwner && (int)$aValue['key'] >= 0) {
  224. $iGc += 1;
  225. continue;
  226. }
  227. unset($this->aInputs[$sField]['values'][$iKey]);
  228. }
  229. }
  230. }
  231. protected function _preloadPrivacyField($sFieldKey, $sObject, $aValues)
  232. {
  233. $CNF = &$this->_oModule->_oConfig->CNF;
  234. if(!isset($CNF[$sFieldKey]) || !isset($this->aInputs[$CNF[$sFieldKey]]) || !isset($CNF[$sObject]))
  235. return;
  236. $sPrivacy = $CNF[$sObject];
  237. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacy);
  238. if(!$oPrivacy)
  239. return;
  240. $sField = $CNF[$sFieldKey];
  241. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  242. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  243. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  244. if(!isset($this->aInputs[$sField]['content']))
  245. $this->aInputs[$sField]['content'] = '';
  246. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacy, $iProfileId, array(
  247. 'content_id' => $iContentId,
  248. 'group_id' => $iGroupId,
  249. 'html_ids' => array(
  250. 'form' => $this->getId()
  251. )
  252. ));
  253. }
  254. }
  255. /** @} */