BxTimelineFormPost.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 $_sType; //--- Is needed for Edit form. Determines which timeline the form was called from.
  20. protected $_bVisibilityAutoselect;
  21. protected $_aUploadersInfo;
  22. public function __construct($aInfo, $oTemplate = false)
  23. {
  24. $this->MODULE = 'bx_timeline';
  25. parent::__construct($aInfo, $oTemplate);
  26. $this->_sGhostTemplate = 'uploader_nfw.html';
  27. $this->_resetMode();
  28. if(isset($this->aParams['display']))
  29. switch($this->aParams['display']) {
  30. case $this->_oModule->_oConfig->getObject('form_display_post_add'):
  31. $this->setModeAccount();
  32. break;
  33. case $this->_oModule->_oConfig->getObject('form_display_post_add_public'):
  34. $this->setModePublic();
  35. break;
  36. case $this->_oModule->_oConfig->getObject('form_display_post_add_profile'):
  37. $this->setModeProfile();
  38. break;
  39. }
  40. $this->_sType = '';
  41. $this->_bVisibilityAutoselect = true;
  42. $this->_aUploadersInfo = [];
  43. }
  44. public function getUploadersInfo($sField = '')
  45. {
  46. if(empty($sField))
  47. return $this->_aUploadersInfo;
  48. $aUploaders = !empty($this->aInputs[$sField]['value']) ? unserialize($this->aInputs[$sField]['value']) : $this->_oModule->_oConfig->getUploaders($sField);
  49. return $this->_aUploadersInfo[array_shift($aUploaders)];
  50. }
  51. public function initChecker($aValues = array (), $aSpecificValues = array())
  52. {
  53. $CNF = &$this->_oModule->_oConfig->CNF;
  54. $bEdit = $this->aParams['display'] == $this->_oModule->_oConfig->getObject('form_display_post_edit');
  55. $iUserId = $this->_oModule->getUserId();
  56. $bValueId = $aValues && !empty($aValues['id']);
  57. $iValueId = $bValueId ? (int)$aValues['id'] : 0;
  58. if($bEdit && isset($aValues[$CNF['FIELD_OWNER_ID']]) && (int)$aValues[$CNF['FIELD_OWNER_ID']] === 0)
  59. $this->setType(BX_BASE_MOD_NTFS_TYPE_PUBLIC);
  60. if(isset($this->aInputs[$CNF['FIELD_ATTACHMENTS']]) && ($oMenu = $this->_oModule->getAttachmentsMenuObject()) !== false) {
  61. $oMenu->setEventById($iValueId);
  62. $oMenu->setUploadersInfo($this->_aUploadersInfo);
  63. $this->aInputs['attachments']['content'] = $oMenu->getCode() . $this->_oModule->_oTemplate->parseHtmlByName('uploader_progress.html', []);;
  64. }
  65. if(isset($this->aInputs[$CNF['FIELD_LINK']]))
  66. $this->aInputs[$CNF['FIELD_LINK']]['content'] = $this->_oModule->_oTemplate->getAttachLinkField($iUserId, $iValueId);
  67. foreach(['FIELD_VIDEO', 'FIELD_FILE'] as $sSetting){
  68. if(!isset($CNF[$sSetting]) || !isset($this->aInputs[$CNF[$sSetting]]))
  69. continue;
  70. $aContentInfo = false;
  71. if($bValueId) {
  72. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($iValueId);
  73. $this->aInputs[$CNF[$sSetting]]['content_id'] = $iValueId;
  74. }
  75. $this->aInputs[$CNF[$sSetting]]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getGhostTmplVars($CNF[$sSetting], $aContentInfo));
  76. }
  77. if($bEdit && isset($CNF['FIELD_PUBLISHED']) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  78. if(isset($aValues[$CNF['FIELD_STATUS']]) && in_array($aValues[$CNF['FIELD_STATUS']], array(BX_TIMELINE_STATUS_ACTIVE, BX_TIMELINE_STATUS_HIDDEN)))
  79. unset($this->aInputs[$CNF['FIELD_PUBLISHED']]);
  80. if(isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($aValues[$CNF['FIELD_OBJECT_ID']]))
  81. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_OBJECT_ID']] < 0;
  82. parent::initChecker ($aValues, $aSpecificValues);
  83. }
  84. public function insert($aValsToAdd = array(), $isIgnore = false)
  85. {
  86. $CNF = &$this->_oModule->_oConfig->CNF;
  87. $aValsToAdd[$CNF['FIELD_SYSTEM']] = 0;
  88. $aValsToAdd[$CNF['FIELD_OBJECT_ID']] *= isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  89. if(isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']])) {
  90. $iAdded = 0;
  91. if(isset($this->aInputs[$CNF['FIELD_ADDED']]))
  92. $iAdded = $this->getCleanValue($CNF['FIELD_ADDED']);
  93. if(empty($iAdded))
  94. $iAdded = time();
  95. $aValsToAdd[$CNF['FIELD_ADDED']] = $iAdded;
  96. if(isset($CNF['FIELD_REACTED']))
  97. $aValsToAdd[$CNF['FIELD_REACTED']] = $iAdded;
  98. }
  99. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']])) {
  100. $iPublished = 0;
  101. if(isset($this->aInputs[$CNF['FIELD_PUBLISHED']]))
  102. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  103. if(empty($iPublished))
  104. $iPublished = time();
  105. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  106. }
  107. $aValsToAdd[$CNF['FIELD_STATUS']] = $aValsToAdd[$CNF['FIELD_PUBLISHED']] > $aValsToAdd[$CNF['FIELD_ADDED']] ? BX_TIMELINE_STATUS_AWAITING : BX_TIMELINE_STATUS_ACTIVE;
  108. return parent::insert ($aValsToAdd, $isIgnore);
  109. }
  110. function update($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  111. {
  112. $CNF = &$this->_oModule->_oConfig->CNF;
  113. if (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']])) {
  114. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  115. $aValsToAdd[$CNF['FIELD_OBJECT_ID']] = ($this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * abs($aContentInfo[$CNF['FIELD_OBJECT_ID']]);
  116. }
  117. if(empty($aValsToAdd[$CNF['FIELD_PUBLISHED']]) && isset($this->aInputs[$CNF['FIELD_PUBLISHED']])) {
  118. $iPublished = $this->getCleanValue($CNF['FIELD_PUBLISHED']);
  119. if(empty($iPublished))
  120. $iPublished = time();
  121. $aValsToAdd[$CNF['FIELD_PUBLISHED']] = $iPublished;
  122. }
  123. return parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  124. }
  125. public function addHtmlEditor($iViewMode, &$aInput, $sUniq)
  126. {
  127. $CNF = &$this->_oModule->_oConfig->CNF;
  128. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  129. if (!$oEditor)
  130. return false;
  131. if(in_array($this->aParams['display'], array($this->_oModule->_oConfig->getObject('form_display_post_add'), $this->_oModule->_oConfig->getObject('form_display_post_add_public'))))
  132. $oEditor->setCustomConf("
  133. placeholderText: '" . _t('_bx_timeline_txt_some_text_here') . "',
  134. initOnClick: false,
  135. ");
  136. if(!$this->_oModule->_oConfig->isEditorToolbar())
  137. $oEditor->setCustomToolbarButtons('');
  138. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name=' . $aInput['name'] . ']', $iViewMode, $this->_bDynamicMode, ['form_id' => $this->aFormAttrs['id'], 'element_name' => $aInput['name'], 'query_params' => $this->getHtmlEditorQueryParams($aInput), 'uniq' => $sUniq]);
  139. return true;
  140. }
  141. public function init()
  142. {
  143. $CNF = &$this->_oModule->_oConfig->CNF;
  144. $iUserId = $this->_oModule->getUserId();
  145. $iOwnerId = $this->_oModule->getOwnerId();
  146. $this->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oModule->_oConfig->getBaseUri() . 'post/';
  147. $this->aInputs['owner_id']['value'] = $iOwnerId;
  148. if(isset($this->aInputs['text'])) {
  149. if(empty($this->aInputs['text']['attrs']) || !is_array($this->aInputs['text']['attrs']))
  150. $this->aInputs['text']['attrs'] = array();
  151. $this->aInputs['text']['attrs']['id'] = $this->_oModule->_oConfig->getHtmlIds('post', 'textarea') . time();
  152. }
  153. $this->_initPrivacyFields();
  154. if(isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  155. $sStorage = $this->_oModule->_oConfig->getObject('storage_photos');
  156. $sUploadersId = genRndPwd(8, false);
  157. $aUploaders = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $this->_oModule->_oConfig->getUploaders($CNF['FIELD_PHOTO']);
  158. foreach($aUploaders as $sUploader)
  159. $this->_aUploadersInfo[$sUploader] = array(
  160. 'id' => $sUploadersId,
  161. 'name' => $sUploader,
  162. 'js_object' => BxDolUploader::getObjectInstance($sUploader, $sStorage, $sUploadersId)->getNameJsInstanceUploader()
  163. );
  164. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $sStorage;
  165. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  166. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $this->_oModule->_oConfig->getObject('transcoder_photos_preview');
  167. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders_id'] = $sUploadersId;
  168. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = $aUploaders;
  169. $this->aInputs[$CNF['FIELD_PHOTO']]['upload_buttons_titles'] = array('Simple' => 'camera');
  170. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  171. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  172. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  173. }
  174. if(isset($this->aInputs[$CNF['FIELD_VIDEO']])) {
  175. $sStorage = $this->_oModule->_oConfig->getObject('storage_videos');
  176. $sUploadersId = genRndPwd(8, false);
  177. $aUploaders = !empty($this->aInputs[$CNF['FIELD_VIDEO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_VIDEO']]['value']) : $this->_oModule->_oConfig->getUploaders($CNF['FIELD_VIDEO']);
  178. foreach($aUploaders as $sUploader)
  179. $this->_aUploadersInfo[$sUploader] = array(
  180. 'id' => $sUploadersId,
  181. 'name' => $sUploader,
  182. 'js_object' => BxDolUploader::getObjectInstance($sUploader, $sStorage, $sUploadersId)->getNameJsInstanceUploader()
  183. );
  184. $this->aInputs[$CNF['FIELD_VIDEO']]['storage_object'] = $sStorage;
  185. $this->aInputs[$CNF['FIELD_VIDEO']]['storage_private'] = 0;
  186. $this->aInputs[$CNF['FIELD_VIDEO']]['images_transcoder'] = $this->_oModule->_oConfig->getObject('transcoder_videos_preview');
  187. $this->aInputs[$CNF['FIELD_VIDEO']]['uploaders_id'] = $sUploadersId;
  188. $this->aInputs[$CNF['FIELD_VIDEO']]['uploaders'] = $aUploaders;
  189. $this->aInputs[$CNF['FIELD_VIDEO']]['upload_buttons_titles'] = array('Simple' => 'video');
  190. $this->aInputs[$CNF['FIELD_VIDEO']]['multiple'] = true;
  191. $this->aInputs[$CNF['FIELD_VIDEO']]['content_id'] = 0;
  192. $this->aInputs[$CNF['FIELD_VIDEO']]['ghost_template'] = '';
  193. }
  194. if(isset($this->aInputs[$CNF['FIELD_FILE']])) {
  195. $sStorage = $this->_oModule->_oConfig->getObject('storage_files');
  196. $sUploadersId = genRndPwd(8, false);
  197. $aUploaders = !empty($this->aInputs[$CNF['FIELD_FILE']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_FILE']]['value']) : $this->_oModule->_oConfig->getUploaders($CNF['FIELD_FILE']);
  198. foreach($aUploaders as $sUploader){
  199. $this->_aUploadersInfo[$sUploader] = array(
  200. 'id' => $sUploadersId,
  201. 'name' => $sUploader,
  202. 'js_object' => BxDolUploader::getObjectInstance($sUploader, $sStorage, $sUploadersId)->getNameJsInstanceUploader()
  203. );
  204. }
  205. $this->aInputs[$CNF['FIELD_FILE']]['storage_object'] = $sStorage;
  206. $this->aInputs[$CNF['FIELD_FILE']]['storage_private'] = 0;
  207. $this->aInputs[$CNF['FIELD_FILE']]['images_transcoder'] = '';
  208. $this->aInputs[$CNF['FIELD_FILE']]['uploaders_id'] = $sUploadersId;
  209. $this->aInputs[$CNF['FIELD_FILE']]['uploaders'] = $aUploaders;
  210. $this->aInputs[$CNF['FIELD_FILE']]['upload_buttons_titles'] = array('Simple' => 'file');
  211. $this->aInputs[$CNF['FIELD_FILE']]['multiple'] = true;
  212. $this->aInputs[$CNF['FIELD_FILE']]['content_id'] = 0;
  213. $this->aInputs[$CNF['FIELD_FILE']]['ghost_template'] = '';
  214. }
  215. if (isset($this->_oModule->_oConfig->CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$this->_oModule->_oConfig->CNF['FIELD_LOCATION_PREFIX']]))
  216. $this->aInputs[$this->_oModule->_oConfig->CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = false;
  217. }
  218. public function setModePublic()
  219. {
  220. $this->_resetMode();
  221. $this->_bPublicMode = true;
  222. }
  223. public function setModeProfile()
  224. {
  225. $this->_resetMode();
  226. $this->_bProfileMode = true;
  227. }
  228. public function setModeAccount()
  229. {
  230. $this->_resetMode();
  231. $this->_bAccountMode = true;
  232. }
  233. public function setType($sType)
  234. {
  235. $this->_sType = $sType;
  236. switch($this->_sType) {
  237. case BX_BASE_MOD_NTFS_TYPE_PUBLIC:
  238. $this->setModePublic();
  239. break;
  240. case BX_BASE_MOD_NTFS_TYPE_OWNER:
  241. $this->setModeProfile();
  242. break;
  243. case BX_TIMELINE_TYPE_OWNER_AND_CONNECTIONS:
  244. case BX_TIMELINE_TYPE_CONNECTED_CONTEXTS:
  245. case BX_TIMELINE_TYPE_FEED:
  246. case BX_TIMELINE_TYPE_FEED_AND_HOT:
  247. $this->setModeAccount();
  248. break;
  249. }
  250. $this->_initPrivacyFields();
  251. }
  252. public function setVisibilityAutoselect($bVisibilityAutoselect)
  253. {
  254. $this->_bVisibilityAutoselect = $bVisibilityAutoselect;
  255. }
  256. protected function genCustomRowObjectCf(&$aInput)
  257. {
  258. return parent::genCustomRowCf($aInput);
  259. }
  260. protected function _getPrivacyFields($aKeysF2O = array())
  261. {
  262. if(empty($aKeysF2O))
  263. $aKeysF2O = array(
  264. 'FIELD_OBJECT_PRIVACY_VIEW' => 'OBJECT_PRIVACY_VIEW'
  265. );
  266. return parent::_getPrivacyFields($aKeysF2O);
  267. }
  268. protected function _initPrivacyFields()
  269. {
  270. $aPrivacyFields = $this->_getPrivacyFields();
  271. foreach($aPrivacyFields as $sField => $sPrivacyObject)
  272. $this->_initPrivacyField($sField, $sPrivacyObject);
  273. }
  274. /**
  275. * The method inherited from BxBaseModGeneralFormEntry class is disabled
  276. * because own variant (method BxTimelineFormPost::_initPrivacyField) is
  277. * used instead.
  278. */
  279. protected function _preparePrivacyField($sField, $sPrivacyObject)
  280. {
  281. return;
  282. }
  283. protected function _initPrivacyField($sField, $sPrivacyObject)
  284. {
  285. $CNF = &$this->_oModule->_oConfig->CNF;
  286. $iUserId = $this->_oModule->getUserId();
  287. $iOwnerId = $this->_oModule->getOwnerId();
  288. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], BxDolPrivacy::getGroupChooser($sPrivacyObject, 0, array(
  289. 'object' => $this->aParams['object'],
  290. 'display' => $this->aParams['display'],
  291. 'title' => _t($CNF['T']['form_input_title_' . $sField]),
  292. 'dynamic_mode' => $this->_bDynamicMode
  293. )));
  294. //--- Show field as is when 'Autoselect' is disabled.
  295. if(!$this->_bVisibilityAutoselect)
  296. return;
  297. //--- Show default privacy groups and followed contexts on Account (Profile + Connections) post form.
  298. if($this->_bAccountMode)
  299. return;
  300. //--- Preselect Context and hide privacy selector when posting in some context (profile, group, space, etc).
  301. if($this->_bProfileMode && $iOwnerId != $iUserId) {
  302. $this->aInputs[$sField]['type'] = 'hidden';
  303. $this->aInputs[$sField]['value'] = -$iOwnerId;
  304. return;
  305. }
  306. $bProfileModeOwner = $this->_bProfileMode && $iOwnerId == $iUserId;
  307. if($this->_bPublicMode || $bProfileModeOwner) {
  308. $iGc = 0;
  309. $iKeyGh = false;
  310. foreach($this->aInputs[$sField]['values'] as $iKey => $aValue) {
  311. if(isset($aValue['type']) && in_array($aValue['type'], array('group_header', 'group_end'))) {
  312. if($iKeyGh !== false && $iGc == 0) {
  313. unset($this->aInputs[$sField]['values'][$iKeyGh]);
  314. $iKeyGh = false;
  315. if($aValue['type'] == 'group_end')
  316. unset($this->aInputs[$sField]['values'][$iKey]);
  317. }
  318. if($aValue['type'] == 'group_header') {
  319. $iGc = 0;
  320. $iKeyGh = $iKey;
  321. }
  322. continue;
  323. }
  324. //--- Show 'Public' privacy group only in Public post form.
  325. if($this->_bPublicMode && $aValue['key'] == BX_DOL_PG_ALL) {
  326. $iGc += 1;
  327. continue;
  328. }
  329. //--- Show a default privacy groups in Profile (for Owner) post form.
  330. if($bProfileModeOwner && (int)$aValue['key'] >= 0) {
  331. $iGc += 1;
  332. continue;
  333. }
  334. unset($this->aInputs[$sField]['values'][$iKey]);
  335. }
  336. }
  337. }
  338. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  339. {
  340. $CNF = &$this->_oModule->_oConfig->CNF;
  341. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  342. if(!$oPrivacy)
  343. return;
  344. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  345. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  346. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  347. if(!isset($this->aInputs[$sField]['content']))
  348. $this->aInputs[$sField]['content'] = '';
  349. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, array(
  350. 'content_id' => $iContentId,
  351. 'group_id' => $iGroupId,
  352. 'html_ids' => array(
  353. 'form' => $this->getId()
  354. )
  355. ));
  356. }
  357. protected function _getGhostTmplVars($sName, $aContentInfo = [])
  358. {
  359. return [
  360. 'js_object' => $this->_oModule->_oConfig->getJsObject('post'),
  361. 'name' => $this->aInputs[$sName]['name'],
  362. 'content_id' => (int)$this->aInputs[$sName]['content_id'],
  363. ];
  364. }
  365. protected function _getPhotoGhostTmplVars($aContentInfo = [])
  366. {
  367. return array_merge(parent::_getPhotoGhostTmplVars($aContentInfo), [
  368. 'js_object' => $this->_oModule->_oConfig->getJsObject('post'),
  369. ]);
  370. }
  371. protected function _resetMode()
  372. {
  373. $this->_bPublicMode = false;
  374. $this->_bProfileMode = false;
  375. $this->_bAccountMode = false;
  376. }
  377. }
  378. /** @} */