BxTimelineFormPost.php 17 KB

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