BxTimelineFormPost.php 15 KB

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