BxTimelineFormPost.php 16 KB

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