BxTimelineModule.php 32 KB

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