BxTimelineModule.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Timeline Timeline
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. bx_import('BxDolModule');
  13. define('BX_TIMELINE_TYPE_OWNER', 'owner');
  14. define('BX_TIMELINE_TYPE_CONNECTIONS', 'connections');
  15. define('BX_TIMELINE_TYPE_ITEM', 'view_item');
  16. define('BX_TIMELINE_HANDLER_TYPE_INSERT', 'insert');
  17. define('BX_TIMELINE_HANDLER_TYPE_UPDATE', 'update');
  18. define('BX_TIMELINE_HANDLER_TYPE_DELETE', 'delete');
  19. define('BX_TIMELINE_FILTER_ALL', 'all');
  20. define('BX_TIMELINE_FILTER_OWNER', 'owner');
  21. define('BX_TIMELINE_FILTER_OTHER', 'other');
  22. define('BX_TIMELINE_PARSE_TYPE_TEXT', 'text');
  23. define('BX_TIMELINE_PARSE_TYPE_LINK', 'link');
  24. define('BX_TIMELINE_PARSE_TYPE_PHOTO', 'photo');
  25. define('BX_TIMELINE_PARSE_TYPE_POST', 'post');
  26. define('BX_TIMELINE_PARSE_TYPE_SHARE', 'share');
  27. define('BX_TIMELINE_PARSE_TYPE_DEFAULT', BX_TIMELINE_PARSE_TYPE_POST);
  28. class BxTimelineModule extends BxDolModule
  29. {
  30. public $_iOwnerId;
  31. protected $_sJsPostObject;
  32. protected $_sJsViewObject;
  33. protected $_aPostElements;
  34. protected $_sJsOutlineObject;
  35. protected $_sDividerTemplate;
  36. protected $_sBalloonTemplate;
  37. protected $_sCmtPostTemplate;
  38. protected $_sCmtViewTemplate;
  39. protected $_sCmtTemplate;
  40. /**
  41. * Constructor
  42. */
  43. function __construct($aModule)
  44. {
  45. parent::__construct($aModule);
  46. $this->_oConfig->init($this->_oDb);
  47. $this->_oTemplate->init();
  48. $this->_iOwnerId = 0;
  49. }
  50. /**
  51. * ACTION METHODS
  52. */
  53. public function actionPost()
  54. {
  55. $sType = bx_process_input($_POST['type']);
  56. $sMethod = 'getForm' . ucfirst($sType);
  57. if(!method_exists($this, $sMethod)) {
  58. $this->_echoResultJson(array());
  59. return;
  60. }
  61. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  62. $mixedAllowed = $this->isAllowedPost(true);
  63. if($mixedAllowed !== true) {
  64. $this->_echoResultJson(array('msg' => strip_tags($mixedAllowed)));
  65. return;
  66. }
  67. $aResult = $this->$sMethod();
  68. $this->_echoResultJson($aResult);
  69. }
  70. function actionDelete()
  71. {
  72. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  73. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  74. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
  75. $mixedAllowed = $this->isAllowedDelete($aEvent, true);
  76. if($mixedAllowed !== true) {
  77. $this->_echoResultJson(array('code' => 1, 'msg' => strip_tags($mixedAllowed)));
  78. return;
  79. }
  80. if(!$this->_oDb->deleteEvent(array('id' => $iId))) {
  81. $this->_echoResultJson(array('code' => 2));
  82. return;
  83. }
  84. $this->onDelete($aEvent);
  85. $this->_echoResultJson(array('code' => 0, 'id' => $iId));
  86. }
  87. public function actionShare()
  88. {
  89. $iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  90. $aContent = array(
  91. 'type' => bx_process_input(bx_get('type'), BX_DATA_TEXT),
  92. 'action' => bx_process_input(bx_get('action'), BX_DATA_TEXT),
  93. 'object_id' => bx_process_input(bx_get('object_id'), BX_DATA_INT),
  94. );
  95. $aShared = $this->_oDb->getShared($aContent['type'], $aContent['action'], $aContent['object_id']);
  96. if(empty($aShared) || !is_array($aShared)) {
  97. $this->_echoResultJson(array('code' => 1, 'msg' => _t('_bx_timeline_txt_err_cannot_share')));
  98. return;
  99. }
  100. $mixedAllowed = $this->isAllowedShare($aShared, true);
  101. if($mixedAllowed !== true) {
  102. $this->_echoResultJson(array('code' => 2, 'msg' => strip_tags($mixedAllowed)));
  103. return;
  104. }
  105. $iId = $this->_oDb->insertEvent(array(
  106. 'owner_id' => $iOwnerId,
  107. 'type' => $this->_oConfig->getPrefix('common_post') . 'share',
  108. 'action' => '',
  109. 'object_id' => $this->getUserId(),
  110. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
  111. 'content' => serialize($aContent),
  112. 'title' => '',
  113. 'description' => ''
  114. ));
  115. if(!empty($iId)) {
  116. $this->onShare($iId, $aShared);
  117. $sCounter = $this->_oTemplate->getShareCounter($aContent['type'], $aContent['action'], $aContent['object_id']);
  118. $this->_echoResultJson(array('code' => 0, 'counter' => $sCounter, 'msg' => _t('_bx_timeline_txt_msg_success_share')));
  119. return;
  120. }
  121. $this->_echoResultJson(array('code' => 3, 'msg' => _t('_bx_timeline_txt_err_cannot_share')));
  122. }
  123. function actionGetPost()
  124. {
  125. $this->_oConfig->setJsMode(true);
  126. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  127. $iEvent = bx_process_input(bx_get('id'), BX_DATA_INT);
  128. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iEvent));
  129. $this->_echoResultJson(array('item' => $this->_oTemplate->getPost($aEvent, array('type' => 'owner', 'owner_id' => $this->_iOwnerId))));
  130. }
  131. function actionGetPosts()
  132. {
  133. $this->_oConfig->setJsMode(true);
  134. $aParams = $this->_prepareParamsGet();
  135. list($sItems, $sLoadMore, $sBack) = $this->_oTemplate->getPosts($aParams);
  136. $this->_echoResultJson(array('items' => $sItems, 'load_more' => $sLoadMore, 'back' => $sBack));
  137. }
  138. public function actionGetPostForm($sType)
  139. {
  140. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  141. $sMethod = 'getForm' . ucfirst($sType);
  142. if(!method_exists($this, $sMethod)) {
  143. $this->_echoResultJson(array());
  144. return;
  145. }
  146. $aResult = $this->$sMethod();
  147. $this->_echoResultJson($aResult);
  148. }
  149. public function actionGetComments()
  150. {
  151. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  152. $sSystem = bx_process_input(bx_get('system'), BX_DATA_TEXT);
  153. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  154. $sComments = $this->_oTemplate->getComments($sSystem, $iId);
  155. $this->_echoResultJson(array('content' => $sComments));
  156. }
  157. public function actionGetPostPopup()
  158. {
  159. $iItemId = bx_process_input(bx_get('id'), BX_DATA_INT);
  160. if(!$iItemId)
  161. return;
  162. echo $this->_oTemplate->getViewItemPopup($iItemId);
  163. }
  164. /*
  165. TODO: Remove if it's not needed.
  166. public function actionGetPhotoPopup()
  167. {
  168. $iEventId = bx_process_input(bx_get('event_id'), BX_DATA_INT);
  169. $iStart = bx_process_input(bx_get('start'), BX_DATA_INT);
  170. if(!$iEventId)
  171. return;
  172. echo $this->_oTemplate->getPhotoPopup($iEventId, $iStart);
  173. }
  174. */
  175. public function actionAddAttachLink()
  176. {
  177. $aResult = $this->getFormAttachLink();
  178. $this->_echoResultJson($aResult);
  179. }
  180. public function actionDeleteAttachLink()
  181. {
  182. $iLinkId = bx_process_input(bx_get('id'), BX_DATA_INT);
  183. if(empty($iLinkId)) {
  184. $this->_echoResultJson(array());
  185. return;
  186. }
  187. $aResult = array();
  188. if($this->_oDb->deleteUnusedLinks($this->getUserId(), $iLinkId))
  189. $aResult = array('code' => 0);
  190. else
  191. $aResult = array('code' => 1, 'msg' => _t('_bx_timeline_form_post_input_link_err_delete'));
  192. $this->_echoResultJson($aResult);
  193. }
  194. public function actionGetAttachLinkForm()
  195. {
  196. echo $this->_oTemplate->getAttachLinkForm();
  197. }
  198. public function actionGetSharedBy()
  199. {
  200. $iSharedId = bx_process_input(bx_get('id'), BX_DATA_INT);
  201. echo $this->_oTemplate->getSharedBy($iSharedId);
  202. }
  203. function actionRss($iOwnerId)
  204. {
  205. list($sUserName) = $this->getUserInfo($iOwnerId);
  206. $sRssCaption = _t('_bx_timeline_txt_rss_caption', $sUserName);
  207. $sRssLink = $this->_oConfig->getViewUrl($iOwnerId);
  208. $aParams = $this->_prepareParams('owner', $iOwnerId, 0, $this->_oConfig->getRssLength(), '', array(), 0);
  209. $aEvents = $this->_oDb->getEvents($aParams);
  210. $aRssData = array();
  211. foreach($aEvents as $aEvent) {
  212. if(empty($aEvent['title'])) continue;
  213. $aRssData[$aEvent['id']] = array(
  214. 'UnitID' => $aEvent['id'],
  215. 'UnitTitle' => $aEvent['title'],
  216. 'UnitLink' => $this->_oConfig->getItemViewUrl($aEvent),
  217. 'UnitDesc' => $aEvent['description'],
  218. 'UnitDateTimeUTS' => $aEvent['date'],
  219. );
  220. }
  221. bx_import('BxDolRssFactory');
  222. $oRss = new BxDolRssFactory();
  223. header('Content-Type: application/xml; charset=utf-8');
  224. echo $oRss->GenRssByData($aRssData, $sRssCaption, $sRssLink);
  225. }
  226. /**
  227. * SERVICE METHODS
  228. */
  229. public function serviceAddHandlers($sModuleUri = 'all')
  230. {
  231. $this->_updateModuleData('add_handlers', $sModuleUri);
  232. }
  233. public function serviceDeleteHandlers($sModuleUri = 'all')
  234. {
  235. $this->_updateModuleData('delete_handlers', $sModuleUri);
  236. }
  237. public function serviceDeleteModuleEvents($sModuleUri = 'all')
  238. {
  239. $this->_updateModuleData('delete_module_events', $sModuleUri);
  240. }
  241. function serviceGetActionsChecklist()
  242. {
  243. $aHandlers = $this->_oConfig->getHandlers();
  244. $aResults = array();
  245. foreach($aHandlers as $aHandler) {
  246. if($aHandler['type'] != BX_TIMELINE_HANDLER_TYPE_INSERT)
  247. continue;
  248. $aModule = $this->_oDb->getModuleByName($aHandler['module_name']);
  249. if(empty($aModule))
  250. $aModule['title'] = _t('_bx_timeline_alert_module_' . $aHandler['alert_unit']);
  251. $aResults[$aHandler['id']] = $aModule['title'] . ' (' . _t('_bx_timeline_alert_action_' . $aHandler['alert_action']) . ')';
  252. }
  253. asort($aResults);
  254. return $aResults;
  255. }
  256. /*
  257. * Get Post block for a separate page.
  258. */
  259. public function serviceGetBlockPost($sProfileModule = 'bx_persons', $iProfileId = 0)
  260. {
  261. return $this->serviceGetBlockPostProfile($sProfileModule, $iProfileId);
  262. }
  263. public function serviceGetBlockPostProfile($sProfileModule = 'bx_persons', $iProfileId = 0)
  264. {
  265. if(empty($iProfileId) && !empty($sProfileModule) && bx_get('id') !== false) {
  266. $oProfile = BxDolProfile::getInstanceByContentAndType(bx_process_input(bx_get('id'), BX_DATA_INT), $sProfileModule);
  267. if(!empty($oProfile))
  268. $iProfileId = $oProfile->id();
  269. }
  270. if(!$iProfileId)
  271. return array();
  272. $this->_iOwnerId = $iProfileId;
  273. if($this->isAllowedPost() !== true)
  274. return array();
  275. return array(
  276. 'content' => $this->_oTemplate->getPostBlock($this->_iOwnerId)
  277. );
  278. }
  279. /*
  280. * Get View block for a separate page. Will return a block with "Empty" message if nothing found.
  281. */
  282. public function serviceGetBlockView($sProfileModule = 'bx_persons', $iProfileId = 0)
  283. {
  284. $aBlock = $this->serviceGetBlockViewProfile($sProfileModule, $iProfileId);
  285. if(!empty($aBlock))
  286. return $aBlock;
  287. return array('content' => MsgBox(_t('_bx_timeline_txt_msg_no_results')));
  288. }
  289. public function serviceGetBlockViewProfile($sProfileModule = 'bx_persons', $iProfileId = 0, $iStart = -1, $iPerPage = -1, $sFilter = '', $aModules = array(), $iTimeline = -1)
  290. {
  291. if(empty($iProfileId) && !empty($sProfileModule) && bx_get('id') !== false) {
  292. $oProfile = BxDolProfile::getInstanceByContentAndType(bx_process_input(bx_get('id'), BX_DATA_INT), $sProfileModule);
  293. if(!empty($oProfile))
  294. $iProfileId = $oProfile->id();
  295. }
  296. if (!$iProfileId)
  297. return array();
  298. $sJsObject = $this->_oConfig->getJsObject('view');
  299. $aParams = $this->_prepareParams(BX_TIMELINE_TYPE_OWNER, $iProfileId, $iStart, $iPerPage, $sFilter, $aModules, $iTimeline);
  300. $this->_iOwnerId = $aParams['owner_id'];
  301. list($sUserName, $sUserUrl) = $this->getUserInfo($aParams['owner_id']);
  302. $aMenu = array(
  303. array('id' => 'timeline-view-all', 'name' => 'timeline-view-all', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.changeFilter(this)', 'target' => '_self', 'title' => _t('_bx_timeline_menu_item_view_all'), 'active' => 1),
  304. array('id' => 'timeline-view-owner', 'name' => 'timeline-view-owner', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.changeFilter(this)', 'target' => '_self', 'title' => _t('_bx_timeline_menu_item_view_owner', $sUserName)),
  305. array('id' => 'timeline-view-other', 'name' => 'timeline-view-other', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.changeFilter(this)', 'target' => '_self', 'title' => _t('_bx_timeline_menu_item_view_other')),
  306. array('id' => 'timeline-get-rss', 'name' => 'timeline-get-rss', 'class' => '', 'link' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'rss/' . $iProfileId . '/', 'target' => '_blank', 'title' => _t('_bx_timeline_menu_item_get_rss')),
  307. );
  308. bx_import('BxTemplMenuInteractive');
  309. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> 'timeline-view-all', 'menu_items' => $aMenu));
  310. $oMenu->setSelected('', 'timeline-view-all');
  311. $sContent = $this->_oTemplate->getViewBlock($aParams);
  312. return array('content' => $sContent, 'menu' => $oMenu);
  313. }
  314. public function serviceGetBlockViewAccount($iProfileId = 0, $iStart = -1, $iPerPage = -1, $iTimeline = -1, $sFilter = '', $aModules = array())
  315. {
  316. $aParams = $this->_prepareParams(BX_TIMELINE_TYPE_CONNECTIONS, $iProfileId, $iStart, $iPerPage, $sFilter, $aModules, $iTimeline);
  317. $this->_iOwnerId = $aParams['owner_id'];
  318. $sContent = $this->_oTemplate->getViewBlock($aParams);
  319. return array('content' => $sContent);
  320. }
  321. public function serviceGetBlockItem()
  322. {
  323. $iItemId = bx_process_input(bx_get('id'), BX_DATA_INT);
  324. if(!$iItemId)
  325. return array();
  326. return array('content' => $this->_oTemplate->getItemBlock($iItemId));
  327. }
  328. public function serviceGetShareElementBlock($iOwnerId, $sType, $sAction, $iObjectId, $aParams = array())
  329. {
  330. if(!$this->isEnabled())
  331. return '';
  332. $aParams = array_merge($this->_oConfig->getShareDefaults(), $aParams);
  333. return $this->_oTemplate->getShareElement($iOwnerId, $sType, $sAction, $iObjectId, $aParams);
  334. }
  335. public function serviceGetShareCounter($sType, $sAction, $iObjectId)
  336. {
  337. if(!$this->isEnabled())
  338. return '';
  339. return $this->_oTemplate->getShareCounter($sType, $sAction, $iObjectId);
  340. }
  341. public function serviceGetShareJsScript()
  342. {
  343. if(!$this->isEnabled())
  344. return '';
  345. return $this->_oTemplate->getShareJsScript();
  346. }
  347. public function serviceGetShareJsClick($iOwnerId, $sType, $sAction, $iObjectId)
  348. {
  349. if(!$this->isEnabled())
  350. return '';
  351. return $this->_oTemplate->getShareJsClick($iOwnerId, $sType, $sAction, $iObjectId);
  352. }
  353. public function serviceGetMenuItemAddonComment($sSystem, $iObjectId)
  354. {
  355. if(empty($sSystem) || empty($iObjectId))
  356. return '';
  357. $oCmts = $this->getCmtsObject($sSystem, $iObjectId);
  358. if($oCmts === false)
  359. return '';
  360. $iCounter = (int)$oCmts->getCommentsCount();
  361. return $this->_oTemplate->parseHtmlByName('bx_a.html', array(
  362. 'href' => 'javascript:void(0)',
  363. 'title' => _t('_bx_timeline_menu_item_title_item_comment'),
  364. 'bx_repeat:attrs' => array(
  365. array('key' => 'onclick', 'value' => "javascript:" . $this->_oConfig->getJsObject('view') . ".commentItem(this, '" . $sSystem . "', " . $iObjectId . ")")
  366. ),
  367. 'content' => $iCounter > 0 ? $iCounter : ''
  368. ));
  369. }
  370. public function serviceGetMenuItemAddonVote($sSystem, $iObjectId)
  371. {
  372. if(empty($sSystem) || empty($iObjectId))
  373. return '';
  374. $oVote = $this->getVoteObject($sSystem, $iObjectId);
  375. if($oVote === false)
  376. return '';
  377. return $oVote->getCounter();
  378. }
  379. public function serviceGetMenuItemAddonShare($sType, $sAction, $iObjectId)
  380. {
  381. if(empty($sType) || empty($iObjectId))
  382. return '';
  383. return $this->serviceGetShareCounter($sType, $sAction, $iObjectId);
  384. }
  385. public function serviceGetSettingsCheckerHelper()
  386. {
  387. bx_import('FormCheckerHelper', $this->_aModule);
  388. return 'BxTimelineFormCheckerHelper';
  389. }
  390. /*
  391. * COMMON METHODS
  392. */
  393. public function getFormAttachLink()
  394. {
  395. $iUserId = $this->getUserId();
  396. bx_import('BxDolForm');
  397. $oForm = BxDolForm::getObjectInstance('mod_tml_attach_link', 'mod_tml_attach_link_add');
  398. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'add_attach_link/';
  399. $oForm->aInputs['url']['checker']['params']['preg'] = $this->_oConfig->getPregPattern('url');
  400. $oForm->initChecker();
  401. if($oForm->isSubmittedAndValid()) {
  402. $sLink = $oForm->getCleanValue('url');
  403. $sLinkContent = bx_file_get_contents($sLink);
  404. $aMatch = array();
  405. preg_match($this->_oConfig->getPregPattern('meta_title'), $sLinkContent, $aMatch);
  406. $sLinkTitle = $aMatch ? $aMatch[1] : '';
  407. preg_match($this->_oConfig->getPregPattern('meta_description'), $sLinkContent, $aMatch);
  408. $sLinkDescription = $aMatch ? $aMatch[1] : '';
  409. $iId = (int)$oForm->insert(array('profile_id' => $iUserId, 'title' => $sLinkTitle, 'text' => $sLinkDescription, 'added' => time()));
  410. if(!empty($iId))
  411. return array('item' => $this->_oTemplate->getAttachLinkItem($iUserId, $iId));
  412. return array('msg' => _t('_adm_nav_err_menus_create'));
  413. }
  414. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  415. }
  416. public function getFormPost()
  417. {
  418. $iUserId = $this->getUserId();
  419. bx_import('BxDolForm');
  420. $oForm = BxDolForm::getObjectInstance('mod_tml_post', 'mod_tml_post_add');
  421. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/';
  422. $oForm->aInputs['owner_id']['value'] = $this->_iOwnerId;
  423. if(isset($oForm->aInputs['link']))
  424. $oForm->aInputs['link']['content'] = $this->_oTemplate->getAttachLinkField($iUserId);
  425. if(isset($oForm->aInputs['photo'])) {
  426. $aFormNested = array(
  427. 'params' =>array(
  428. 'nested_form_template' => 'uploader_nfw.html'
  429. ),
  430. 'inputs' => array(),
  431. );
  432. bx_import('BxDolFormNested');
  433. $oFormNested = new BxDolFormNested('photo', $aFormNested, 'do_submit', $this->_oTemplate);
  434. $oForm->aInputs['photo']['storage_object'] = $this->_oConfig->getObject('storage');
  435. $oForm->aInputs['photo']['images_transcoder'] = $this->_oConfig->getObject('transcoder_preview');
  436. $oForm->aInputs['photo']['uploaders'] = $this->_oConfig->getUploaders('image');
  437. $oForm->aInputs['photo']['upload_buttons_titles'] = array('Simple' => 'camera');
  438. $oForm->aInputs['photo']['multiple'] = true;
  439. $oForm->aInputs['photo']['ghost_template'] = $oFormNested;
  440. }
  441. $oForm->initChecker();
  442. if($oForm->isSubmittedAndValid()) {
  443. list($sUserName) = $this->getUserInfo($iUserId);
  444. $sType = $oForm->getCleanValue('type');
  445. $sType = $this->_oConfig->getPrefix('common_post') . $sType;
  446. BxDolForm::setSubmittedValue('type', $sType, $oForm->aFormAttrs['method']);
  447. $aContent = array();
  448. //--- Process Text ---//
  449. $sText = $oForm->getCleanValue('text');
  450. unset($oForm->aInputs['text']);
  451. $aContent['text'] = $this->_prepareTextForSave($sText);
  452. //--- Process Link ---//
  453. $aLinkIds = $oForm->getCleanValue('link');
  454. unset($oForm->aInputs['link']);
  455. //--- Process Photos ---//
  456. $aPhotoIds = $oForm->getCleanValue('photo');
  457. unset($oForm->aInputs['photo']);
  458. $sTitle = _t('_bx_timeline_txt_user_added_sample', $sUserName, _t('_bx_timeline_txt_sample'));
  459. $sDescription = !empty($aContent['text']) ? $aContent['text'] : '';
  460. $iId = $oForm->insert(array(
  461. 'object_id' => $iUserId,
  462. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
  463. 'content' => serialize($aContent),
  464. 'title' => $sTitle,
  465. 'description' => $sDescription,
  466. 'date' => time()
  467. ));
  468. if(!empty($iId)) {
  469. if(!empty($aLinkIds) && is_array($aLinkIds))
  470. foreach($aLinkIds as $iLinkId)
  471. $this->_oDb->saveLink($iId, $iLinkId);
  472. if(!empty($aPhotoIds) && is_array($aPhotoIds)) {
  473. bx_import('BxDolStorage');
  474. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->getObject('storage'));
  475. foreach($aPhotoIds as $iPhotoId)
  476. if($this->_oDb->savePhoto($iId, $iPhotoId))
  477. $oStorage->afterUploadCleanup($iPhotoId, $iUserId);
  478. }
  479. $this->onPost($iId);
  480. return array('id' => $iId);
  481. }
  482. return array('msg' => _t('_bx_timeline_txt_err_cannot_perform_action'));
  483. }
  484. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  485. }
  486. public function getCmtsObject($sSystem, $iId)
  487. {
  488. if(empty($sSystem) || (int)$iId == 0)
  489. return false;
  490. bx_import('BxDolCmts');
  491. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iId);
  492. if(!$oCmts->isEnabled())
  493. return false;
  494. return $oCmts;
  495. }
  496. public function getVoteObject($sSystem, $iId)
  497. {
  498. if(empty($sSystem) || (int)$iId == 0)
  499. return false;
  500. bx_import('BxDolVote');
  501. $oVote = BxDolVote::getObjectInstance($sSystem, $iId);
  502. if(!$oVote->isEnabled())
  503. return false;
  504. return $oVote;
  505. }
  506. public function getUserId()
  507. {
  508. return isLogged() ? bx_get_logged_profile_id() : 0;
  509. }
  510. public function getUserIp()
  511. {
  512. return getVisitorIP();
  513. }
  514. public function getUserInfo($iUserId = 0)
  515. {
  516. bx_import('BxDolProfile');
  517. $oProfile = BxDolProfile::getInstance($iUserId);
  518. if (!$oProfile) {
  519. bx_import('BxDolProfileUndefined');
  520. $oProfile = BxDolProfileUndefined::getInstance();
  521. }
  522. return array(
  523. $oProfile->getDisplayName(),
  524. $oProfile->getUrl(),
  525. $oProfile->getThumb(),
  526. $oProfile->getUnit()
  527. );
  528. }
  529. //--- Check permissions methods ---//
  530. public function isAllowedPost($bPerform = false)
  531. {
  532. if(isAdmin())
  533. return true;
  534. $iUserId = $this->getUserId();
  535. if($this->_iOwnerId == $this->getUserId())
  536. return true;
  537. $aCheckResult = checkActionModule($iUserId, 'post', $this->getName(), $bPerform);
  538. return $aCheckResult[CHECK_ACTION_RESULT] != CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  539. }
  540. public function isAllowedDelete($aEvent, $bPerform = false)
  541. {
  542. if(isAdmin())
  543. return true;
  544. $iUserId = (int)$this->getUserId();
  545. if((int)$aEvent['owner_id'] == $iUserId && $this->_oConfig->isAllowDelete())
  546. return true;
  547. $aCheckResult = checkActionModule($iUserId, 'delete', $this->getName(), $bPerform);
  548. return $aCheckResult[CHECK_ACTION_RESULT] != CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  549. }
  550. public function isAllowedComment($aEvent, $bPerform = false)
  551. {
  552. $mixedComments = $this->getCommentsData($aEvent['comments']);
  553. if($mixedComments === false)
  554. return false;
  555. list($sSystem, $iObjectId) = $mixedComments;
  556. $oCmts = $this->getCmtsObject($sSystem, $iObjectId);
  557. $oCmts->addCssJs();
  558. $iUserId = (int)$this->getUserId();
  559. if($iUserId == 0)
  560. return false;
  561. if(isAdmin())
  562. return true;
  563. return $oCmts->isPostReplyAllowed($bPerform);
  564. }
  565. public function isAllowedVote($aEvent, $bPerform = false)
  566. {
  567. $mixedVotes = $this->getVotesData($aEvent['votes']);
  568. if($mixedVotes === false)
  569. return false;
  570. list($sSystem, $iObjectId) = $mixedVotes;
  571. $oVote = $this->getVoteObject($sSystem, $iObjectId);
  572. $oVote->addCssJs();
  573. $iUserId = (int)$this->getUserId();
  574. if($iUserId == 0)
  575. return false;
  576. if(isAdmin())
  577. return true;
  578. return $oVote->isAllowedVote($bPerform);
  579. }
  580. public function isAllowedShare($aEvent, $bPerform = false)
  581. {
  582. if(isAdmin())
  583. return true;
  584. $iUserId = (int)$this->getUserId();
  585. if($iUserId == 0)
  586. return false;
  587. $aCheckResult = checkActionModule($iUserId, 'share', $this->getName(), $bPerform);
  588. return $aCheckResult[CHECK_ACTION_RESULT] != CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  589. }
  590. public function onPost($iId)
  591. {
  592. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
  593. if($this->_oConfig->isSystem($aEvent['type'], $aEvent['action'])) {
  594. $sPostType = 'system';
  595. $iSenderId = $aEvent['owner_id'];
  596. } else {
  597. $sPostType = 'common';
  598. $iSenderId = $aEvent['object_id'];
  599. }
  600. //--- Event -> Post for Alerts Engine ---//
  601. bx_import('BxDolAlerts');
  602. $oAlert = new BxDolAlerts($this->_oConfig->getSystemName('alert'), 'post_' . $sPostType, $iId, $iSenderId);
  603. $oAlert->alert();
  604. //--- Event -> Post for Alerts Engine ---//
  605. }
  606. public function onShare($iId, $aShared = array())
  607. {
  608. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
  609. if(empty($aShared)) {
  610. $aContent = unserialize($aEvent['content']);
  611. $aShared = $this->_oDb->getShared($aContent['type'], $aContent['action'], $aContent['object_id']);
  612. if(empty($aShared) || !is_array($aShared))
  613. return;
  614. }
  615. $iUserId = $this->getUserId();
  616. $this->_oDb->insertShareTrack($aEvent['id'], $iUserId, $this->getUserIp(), $aShared['id']);
  617. $this->_oDb->updateShareCounter($aShared['id'], $aShared['shares']);
  618. //--- Timeline -> Update for Alerts Engine ---//
  619. bx_import('BxDolAlerts');
  620. $oAlert = new BxDolAlerts($this->_oConfig->getSystemName('alert'), 'share', $aShared['id'], $iUserId);
  621. $oAlert->alert();
  622. //--- Timeline -> Update for Alerts Engine ---//
  623. }
  624. public function onDelete($aEvent)
  625. {
  626. $aPhotos = $this->_oDb->getPhotos($aEvent['id']);
  627. if(!empty($aPhotos) && is_array($aPhotos)) {
  628. bx_import('BxDolStorage');
  629. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->getObject('storage'));
  630. foreach($aPhotos as $iPhotoId)
  631. $oStorage->deleteFile($iPhotoId);
  632. $this->_oDb->deletePhotos($aEvent['id']);
  633. }
  634. $this->_oDb->deleteLinks($aEvent['id']);
  635. if($aEvent['type'] == $this->_oConfig->getPrefix('common_post') . BX_TIMELINE_PARSE_TYPE_SHARE) {
  636. $this->_oDb->deleteShareTrack($aEvent['id']);
  637. $aContent = unserialize($aEvent['content']);
  638. $aShared = $this->_oDb->getShared($aContent['type'], $aContent['action'], $aContent['object_id']);
  639. if(!empty($aShared) && is_array($aShared))
  640. $this->_oDb->updateShareCounter($aShared['id'], $aShared['shares'], -1);
  641. }
  642. //--- Event -> Delete for Alerts Engine ---//
  643. bx_import('BxDolAlerts');
  644. $oAlert = new BxDolAlerts($this->_oConfig->getSystemName('alert'), 'delete', $aEvent['id'], $this->getUserId());
  645. $oAlert->alert();
  646. //--- Event -> Delete for Alerts Engine ---//
  647. }
  648. public function getVotesData(&$aVotes)
  649. {
  650. if(empty($aVotes) || !is_array($aVotes))
  651. return false;
  652. $sSystem = isset($aVotes['system']) ? $aVotes['system'] : '';
  653. $iObjectId = isset($aVotes['object_id']) ? (int)$aVotes['object_id'] : 0;
  654. $iCount = isset($aVotes['count']) ? (int)$aVotes['count'] : 0;
  655. if($sSystem == '' || $iObjectId == 0)
  656. return false;
  657. return array($sSystem, $iObjectId, $iCount);
  658. }
  659. public function getCommentsData(&$aComments)
  660. {
  661. if(empty($aComments) || !is_array($aComments))
  662. return false;
  663. $sSystem = isset($aComments['system']) ? $aComments['system'] : '';
  664. $iObjectId = isset($aComments['object_id']) ? (int)$aComments['object_id'] : 0;
  665. $iCount = isset($aComments['count']) ? (int)$aComments['count'] : 0;
  666. if($sSystem == '' || $iObjectId == 0 || ($iCount == 0 && !isLogged()))
  667. return false;
  668. return array($sSystem, $iObjectId, $iCount);
  669. }
  670. protected function _prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $sFilter, $aModules, $iTimeline)
  671. {
  672. $aParams = array();
  673. $aParams['browse'] = 'list';
  674. $aParams['type'] = !empty($sType) ? $sType : BX_TIMELINE_TYPE_OWNER;
  675. $aParams['owner_id'] = (int)$iOwnerId != 0 ? $iOwnerId : $this->getUserId();
  676. $aParams['start'] = (int)$iStart > 0 ? $iStart : 0;
  677. $aParams['per_page'] = (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage();
  678. $aParams['filter'] = !empty($sFilter) ? $sFilter : BX_TIMELINE_FILTER_ALL;
  679. $aParams['modules'] = is_array($aModules) && !empty($aModules) ? $aModules : array();
  680. $aParams['timeline'] = (int)$iTimeline > 0 ? $iTimeline : 0;
  681. return $aParams;
  682. }
  683. protected function _prepareParamsGet()
  684. {
  685. $aParams = array();
  686. $aParams['browse'] = 'list';
  687. $sType = bx_get('type');
  688. $aParams['type'] = $sType !== false ? bx_process_input($sType, BX_DATA_TEXT) : BX_TIMELINE_TYPE_OWNER;
  689. $aParams['owner_id'] = $sType !== false ? bx_process_input(bx_get('owner_id'), BX_DATA_INT) : $this->getUserId();
  690. $iStart = bx_get('start');
  691. $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
  692. $iPerPage = bx_get('per_page');
  693. $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
  694. $sFilter = bx_get('filter');
  695. $aParams['filter'] = $sFilter !== false ? bx_process_input($sFilter, BX_DATA_TEXT) : BX_TIMELINE_FILTER_ALL;
  696. $aModules = bx_get('modules');
  697. $aParams['modules'] = $aModules !== false ? bx_process_input($aModules, BX_DATA_TEXT) : array();
  698. $iTimeline = bx_get('timeline');
  699. $aParams['timeline'] = $iTimeline !== false ? bx_process_input($iTimeline, BX_DATA_INT) : 0;
  700. return $aParams;
  701. }
  702. protected function _prepareTextForSave($s)
  703. {
  704. return bx_process_input($s, BX_DATA_TEXT_MULTILINE);
  705. }
  706. protected function _updateModuleData($sAction, $sModuleUri = 'all')
  707. {
  708. $aModules = $sModuleUri == 'all' ? $this->_oDb->getModules() : array($this->_oDb->getModuleByUri($sModuleUri));
  709. foreach($aModules as $aModule) {
  710. if(!BxDolRequest::serviceExists($aModule, 'get_timeline_data'))
  711. continue;
  712. $aData = BxDolService::call($aModule['name'], 'get_timeline_data');
  713. if(empty($aData) || !is_array($aData))
  714. continue;
  715. switch($sAction) {
  716. case 'add_handlers':
  717. $this->_oDb->insertData($aData);
  718. BxDolAlerts::cacheInvalidate();
  719. break;
  720. case 'delete_handlers':
  721. $this->_oDb->deleteData($aData);
  722. BxDolAlerts::cacheInvalidate();
  723. break;
  724. case 'delete_module_events':
  725. $this->_oDb->deleteModuleEvents($aData);
  726. break;
  727. }
  728. }
  729. }
  730. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  731. {
  732. header('Content-type: text/html; charset=utf-8');
  733. $s = json_encode($a);
  734. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  735. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  736. echo $s;
  737. }
  738. }
  739. /** @} */