BxTimelineModule.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. require_once( BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php' );
  14. define('BX_TIMELINE_TYPE_OWNER', 'owner');
  15. define('BX_TIMELINE_TYPE_CONNECTIONS', 'connections');
  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_SHARE', 'share');
  26. define('BX_TIMELINE_PARSE_TYPE_DEFAULT', BX_TIMELINE_PARSE_TYPE_TEXT);
  27. class BxTimelineModule extends BxDolModule
  28. {
  29. var $_iOwnerId;
  30. var $_sJsPostObject;
  31. var $_sJsViewObject;
  32. var $_aPostElements;
  33. var $_sJsOutlineObject;
  34. var $_sDividerTemplate;
  35. var $_sBalloonTemplate;
  36. var $_sCmtPostTemplate;
  37. var $_sCmtViewTemplate;
  38. var $_sCmtTemplate;
  39. /**
  40. * Constructor
  41. */
  42. function __construct($aModule)
  43. {
  44. parent::BxDolModule($aModule);
  45. $this->_oConfig->init($this->_oDb);
  46. $this->_oTemplate->init();
  47. $this->_iOwnerId = 0;
  48. }
  49. /**
  50. * ACTION METHODS
  51. */
  52. public function actionPost()
  53. {
  54. $sType = bx_process_input($_POST['type']);
  55. $sMethod = 'getForm' . ucfirst($sType);
  56. if(!method_exists($this, $sMethod)) {
  57. $this->_echoResultJson(array());
  58. return;
  59. }
  60. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  61. if (!$this->isAllowedPost(true)) {
  62. $this->_echoResultJson(array('msg' => bx_js_string(_t('_bx_timeline_txt_msg_not_allowed_post'))));
  63. return;
  64. }
  65. $aResult = $this->$sMethod();
  66. $this->_echoResultJson($aResult);
  67. }
  68. function actionDelete()
  69. {
  70. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  71. if(!$this->isAllowedDelete(true)) {
  72. $this->_echoResultJson(array('code' => 1));
  73. return;
  74. }
  75. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  76. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
  77. if(!$this->_oDb->deleteEvent(array('id' => $iId))) {
  78. $this->_echoResultJson(array('code' => 2));
  79. return;
  80. }
  81. $this->onDelete($aEvent);
  82. $this->_echoResultJson(array('code' => 0, 'id' => $iId));
  83. }
  84. public function actionShare()
  85. {
  86. $iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  87. $aContent = array(
  88. 'type' => bx_process_input(bx_get('type'), BX_DATA_TEXT),
  89. 'action' => bx_process_input(bx_get('action'), BX_DATA_TEXT),
  90. 'object_id' => bx_process_input(bx_get('object_id'), BX_DATA_INT),
  91. );
  92. $iId = $this->_oDb->insertEvent(array(
  93. 'owner_id' => $iOwnerId,
  94. 'type' => $this->_oConfig->getPrefix('common_post') . 'share',
  95. 'action' => '',
  96. 'object_id' => $this->getUserId(),
  97. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
  98. 'content' => serialize($aContent),
  99. 'title' => '',
  100. 'description' => ''
  101. ));
  102. if(!empty($iId)) {
  103. $this->onShare($iId);
  104. $this->_echoResultJson(array('msg' => _t('_bx_timeline_txt_msg_success_share')));
  105. return;
  106. }
  107. $this->_echoResultJson(array('msg' => _t('_bx_timeline_txt_err_cannot_share')));
  108. }
  109. function actionGetPost()
  110. {
  111. $this->_oConfig->setJsMode(true);
  112. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  113. $iEvent = bx_process_input(bx_get('id'), BX_DATA_INT);
  114. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iEvent));
  115. $this->_echoResultJson(array('item' => $this->_oTemplate->getPost($aEvent, array('type' => 'owner', 'owner_id' => $this->_iOwnerId))));
  116. }
  117. function actionGetPosts()
  118. {
  119. $this->_oConfig->setJsMode(true);
  120. $aParams = $this->_prepareParamsGet();
  121. list($sItems, $sLoadMore, $sBack) = $this->_oTemplate->getPosts($aParams);
  122. $this->_echoResultJson(array('items' => $sItems, 'load_more' => $sLoadMore, 'back' => $sBack));
  123. }
  124. public function actionGetPostForm($sType)
  125. {
  126. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  127. $sMethod = 'getForm' . ucfirst($sType);
  128. if(!method_exists($this, $sMethod)) {
  129. $this->_echoResultJson(array());
  130. return;
  131. }
  132. $aResult = $this->$sMethod();
  133. $this->_echoResultJson($aResult);
  134. }
  135. public function actionGetComments()
  136. {
  137. $this->_iOwnerId = bx_process_input(bx_get('owner_id'), BX_DATA_INT);
  138. $sSystem = bx_process_input(bx_get('system'), BX_DATA_TEXT);
  139. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  140. $sComments = $this->_oTemplate->getComments($sSystem, $iId);
  141. $this->_echoResultJson(array('content' => $sComments));
  142. }
  143. public function actionGetPostPopup()
  144. {
  145. $iItemId = bx_process_input(bx_get('id'), BX_DATA_INT);
  146. if(!$iItemId) {
  147. $this->_echoResultJson(array());
  148. return;
  149. }
  150. $sContent = $this->_oTemplate->getViewItemPopup($iItemId);
  151. $this->_echoResultJson(array('popup' => $sContent));
  152. }
  153. function actionRss($iOwnerId)
  154. {
  155. list($sUserName) = $this->getUserInfo($iOwnerId);
  156. $sRssCaption = _t('_bx_timeline_txt_rss_caption', $sUserName);
  157. $sRssLink = $this->_oConfig->getViewUrl($iOwnerId);
  158. $aParams = $this->_prepareParams('owner', $iOwnerId, 0, $this->_oConfig->getRssLength(), '', array(), 0);
  159. $aEvents = $this->_oDb->getEvents($aParams);
  160. $aRssData = array();
  161. foreach($aEvents as $aEvent) {
  162. if(empty($aEvent['title'])) continue;
  163. $aRssData[$aEvent['id']] = array(
  164. 'UnitID' => $aEvent['id'],
  165. 'UnitTitle' => $aEvent['title'],
  166. 'UnitLink' => $this->_oConfig->getItemViewUrl($aEvent),
  167. 'UnitDesc' => $aEvent['description'],
  168. 'UnitDateTimeUTS' => $aEvent['date'],
  169. );
  170. }
  171. bx_import('BxDolRssFactory');
  172. $oRss = new BxDolRssFactory();
  173. header('Content-Type: text/html; charset=utf-8');
  174. echo $oRss->GenRssByData($aRssData, $sRssCaption, $sRssLink);
  175. }
  176. /**
  177. * SERVICE METHODS
  178. */
  179. public function serviceAddHandlers($sModuleUri = 'all')
  180. {
  181. $this->_updateHandlers($sModuleUri, true);
  182. }
  183. public function serviceDeleteHandlers($sModuleUri = 'all')
  184. {
  185. $this->_updateHandlers($sModuleUri, false);
  186. }
  187. function serviceGetActionsChecklist()
  188. {
  189. $aHandlers = $this->_oConfig->getHandlers();
  190. $aResults = array();
  191. foreach($aHandlers as $aHandler) {
  192. if($aHandler['type'] != BX_TIMELINE_HANDLER_TYPE_INSERT)
  193. continue;
  194. $aModule = $this->_oDb->getModuleByName($aHandler['module_name']);
  195. if(empty($aModule))
  196. $aModule['title'] = _t('_bx_timeline_alert_module_' . $aHandler['alert_unit']);
  197. $aResults[$aHandler['id']] = $aModule['title'] . ' (' . _t('_bx_timeline_alert_action_' . $aHandler['alert_action']) . ')';
  198. }
  199. asort($aResults);
  200. return $aResults;
  201. }
  202. public function serviceResponse($oAlert)
  203. {
  204. bx_import('Response', $this->_aModule);
  205. $oResponse = new BxTimelineResponse($this);
  206. $oResponse->response($oAlert);
  207. }
  208. public function serviceGetBlockPost($iProfileId = 0)
  209. {
  210. return $this->serviceGetBlockPostProfile($iProfileId);
  211. }
  212. public function serviceGetBlockPostProfile($iProfileId = 0)
  213. {
  214. if(!$iProfileId && bx_get('id') !== false) {
  215. $oProfile = BxDolProfile::getInstanceByContentAndType(bx_process_input(bx_get('id'), BX_DATA_INT), 'bx_persons');
  216. if(!empty($oProfile))
  217. $iProfileId = $oProfile->id();
  218. }
  219. if (!$iProfileId)
  220. return array();
  221. $this->_iOwnerId = $iProfileId;
  222. list($sUserName, $sUserUrl) = $this->getUserInfo($iProfileId);
  223. if($this->_iOwnerId != $this->getUserId() && !$this->isAllowedPost())
  224. return array();
  225. bx_import('BxDolMenu');
  226. $oMenu = BxDolMenu::getObjectInstance($this->_oConfig->getObject('menu_post'));
  227. $oMenu->setMenuId('timeline-post-menu');
  228. $oMenu->setSelected($this->getName(), 'post-text');
  229. $sContent = $this->_oTemplate->getPostBlock($this->_iOwnerId);
  230. return array('content' => $sContent, 'menu' => $oMenu);
  231. }
  232. public function serviceGetBlockView($iProfileId = 0)
  233. {
  234. $aBlock = $this->serviceGetBlockViewProfile($iProfileId);
  235. if(!empty($aBlock))
  236. return $aBlock;
  237. return array('content' => MsgBox(_t('_bx_timeline_txt_msg_no_results')));
  238. }
  239. public function serviceGetBlockViewProfile($iProfileId = 0, $iStart = -1, $iPerPage = -1, $sFilter = '', $aModules = array(), $iTimeline = -1)
  240. {
  241. if(!$iProfileId && bx_get('id') !== false) {
  242. $oProfile = BxDolProfile::getInstanceByContentAndType(bx_process_input(bx_get('id'), BX_DATA_INT), 'bx_persons');
  243. if(!empty($oProfile))
  244. $iProfileId = $oProfile->id();
  245. }
  246. if (!$iProfileId)
  247. return array();
  248. $sJsObject = $this->_oConfig->getJsObject('view');
  249. $aParams = $this->_prepareParams('owner', $iProfileId, $iStart, $iPerPage, $sFilter, $aModules, $iTimeline);
  250. $this->_iOwnerId = $aParams['owner_id'];
  251. list($sUserName, $sUserUrl) = $this->getUserInfo($aParams['owner_id']);
  252. $aMenu = array(
  253. 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),
  254. 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)),
  255. 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')),
  256. 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')),
  257. );
  258. bx_import('BxTemplMenuInteractive');
  259. $oMenu = new BxTemplMenuInteractive(array('template' => 'menu_interactive_vertical.html', 'menu_id'=> 'timeline-view-all', 'menu_items' => $aMenu));
  260. $oMenu->setSelected('', 'timeline-view-all');
  261. $sContent = $this->_oTemplate->getViewBlock($aParams);
  262. return array('content' => $sContent, 'menu' => $oMenu);
  263. }
  264. public function serviceGetBlockViewAccount($iProfileId = 0, $iStart = -1, $iPerPage = -1, $iTimeline = -1, $sFilter = '', $aModules = array())
  265. {
  266. $aParams = $this->_prepareParams('connections', $iProfileId, $iStart, $iPerPage, $sFilter, $aModules, $iTimeline);
  267. $this->_iOwnerId = $aParams['owner_id'];
  268. $sContent = $this->_oTemplate->getViewBlock($aParams);
  269. return array('content' => $sContent);
  270. }
  271. public function serviceGetBlockItem()
  272. {
  273. $iItemId = bx_process_input(bx_get('id'), BX_DATA_INT);
  274. if(!$iItemId)
  275. return array();
  276. return array('content' => $this->_oTemplate->getViewItemBlock($iItemId));
  277. }
  278. public function serviceGetShareOnclick($iOwnerId, $sType, $sAction, $iObjectId)
  279. {
  280. $sJsObject = $this->_oConfig->getJsObject('view');
  281. $sFormat = "%s.shareItem(this, %d, '%s', '%s', %d);";
  282. $iOwnerId = !empty($iOwnerId) ? (int)$iOwnerId : $this->getUserId(); //--- in whose timeline the content will be shared
  283. return sprintf($sFormat, $sJsObject, $iOwnerId, $sType, $sAction, (int)$iObjectId);
  284. }
  285. /*
  286. * COMMON METHODS
  287. */
  288. public function getFormText()
  289. {
  290. bx_import('BxDolForm');
  291. $oForm = BxDolForm::getObjectInstance('mod_tml_text', 'mod_tml_text_add');
  292. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/';
  293. $oForm->aInputs['owner_id']['value'] = $this->_iOwnerId;
  294. $oForm->initChecker();
  295. if($oForm->isSubmittedAndValid()) {
  296. $iUserId = $this->getUserId();
  297. list($sUserName) = $this->getUserInfo($iUserId);
  298. $sType = $oForm->getCleanValue('type');
  299. $sType = $this->_oConfig->getPrefix('common_post') . $sType;
  300. BxDolForm::setSubmittedValue('type', $sType, $oForm->aFormAttrs['method']);
  301. $sContent = $sDescription = $oForm->getCleanValue('content');
  302. $sContent = serialize(array('text' => $sContent));
  303. BxDolForm::setSubmittedValue('content', $sContent, $oForm->aFormAttrs['method']);
  304. $iId = $oForm->insert(array(
  305. 'object_id' => $iUserId,
  306. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
  307. 'title' => bx_process_input($sUserName . ' ' . _t('_bx_timeline_txt_wrote')),
  308. 'description' => $sDescription,
  309. 'date' => time()
  310. ));
  311. if(!empty($iId)) {
  312. $this->onPost($iId);
  313. return array('id' => $iId);
  314. }
  315. return array('msg' => _t('_bx_timeline_txt_err_cannot_perform_action'));
  316. }
  317. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  318. }
  319. public function getFormLink()
  320. {
  321. bx_import('BxDolForm');
  322. $oForm = BxDolForm::getObjectInstance('mod_tml_link', 'mod_tml_link_add');
  323. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/';
  324. $oForm->aInputs['owner_id']['value'] = $this->_iOwnerId;
  325. $oForm->initChecker();
  326. if($oForm->isSubmittedAndValid()) {
  327. $iUserId = $this->getUserId();
  328. list($sUserName) = $this->getUserInfo($iUserId);
  329. $sType = $oForm->getCleanValue('type');
  330. $sType = $this->_oConfig->getPrefix('common_post') . $sType;
  331. BxDolForm::setSubmittedValue('type', $sType, $oForm->aFormAttrs['method']);
  332. $sUrl = $oForm->getCleanValue('content');
  333. $sContent = bx_file_get_contents($sUrl);
  334. preg_match("/<title>(.*)<\/title>/", $sContent, $aMatch);
  335. $sTitle = $aMatch ? $aMatch[1] : '';
  336. preg_match("/<meta.*name[='\" ]+description['\"].*content[='\" ]+(.*)['\"].*><\/meta>/", $sContent, $aMatch);
  337. $sDescription = $aMatch ? $aMatch[1] : '';
  338. $sContent = serialize(array(
  339. 'url' => strpos($sUrl, 'http://') === false && strpos($sUrl, 'https://') === false ? 'http://' . $sUrl : $sUrl,
  340. 'title' => $sTitle,
  341. 'text' => $sDescription
  342. ));
  343. BxDolForm::setSubmittedValue('content', $sContent, $oForm->aFormAttrs['method']);
  344. $iId = $oForm->insert(array(
  345. 'object_id' => $iUserId,
  346. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
  347. 'title' => bx_process_input($sUserName . ' ' . _t('_bx_timeline_txt_shared_link')),
  348. 'description' => bx_process_input($sUrl . ' - ' . $sTitle),
  349. 'date' => time()
  350. ));
  351. if(!empty($iId)) {
  352. $this->onPost($iId);
  353. return array('id' => $iId);
  354. }
  355. return array('msg' => _t('_bx_timeline_txt_err_cannot_perform_action'));
  356. }
  357. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  358. }
  359. public function getFormPhoto()
  360. {
  361. $aFormNested = array(
  362. 'inputs' => array(
  363. 'file_title' => array(
  364. 'type' => 'text',
  365. 'name' => 'file_title[]',
  366. 'value' => '{file_title}',
  367. 'caption' => _t('_bx_timeline_form_photo_input_title'),
  368. 'required' => true,
  369. 'checker' => array(
  370. 'func' => 'length',
  371. 'params' => array(1, 150),
  372. 'error' => _t('_bx_timeline_form_photo_input_err_title')
  373. ),
  374. 'db' => array (
  375. 'pass' => 'Xss',
  376. ),
  377. ),
  378. 'file_text' => array(
  379. 'type' => 'textarea',
  380. 'name' => 'file_text[]',
  381. 'caption' => _t('_bx_timeline_form_photo_input_description'),
  382. 'required' => true,
  383. 'checker' => array(
  384. 'func' => 'length',
  385. 'params' => array(10, 5000),
  386. 'error' => _t('_bx_timeline_form_photo_input_err_description')
  387. ),
  388. 'db' => array (
  389. 'pass' => 'Xss',
  390. ),
  391. ),
  392. ),
  393. );
  394. bx_import('BxDolForm');
  395. $oForm = BxDolForm::getObjectInstance('mod_tml_photo', 'mod_tml_photo_add');
  396. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'post/';
  397. $oForm->aInputs['owner_id']['value'] = $this->_iOwnerId;
  398. bx_import('BxDolFormNested');
  399. $oFormNested = new BxDolFormNested('content', $aFormNested, 'do_submit');
  400. $oForm->aInputs['content']['storage_object'] = $this->_oConfig->getObject('storage');
  401. $oForm->aInputs['content']['images_transcoder'] = $this->_oConfig->getObject('transcoder_preview');
  402. $oForm->aInputs['content']['uploaders'] = $this->_oConfig->getUploaders('image');
  403. $oForm->aInputs['content']['multiple'] = false;
  404. $oForm->aInputs['content']['ghost_template'] = $oFormNested;
  405. $oForm->initChecker();
  406. if($oForm->isSubmittedAndValid()) {
  407. $iUserId = $this->getUserId();
  408. list($sUserName) = $this->getUserInfo($iUserId);
  409. $sType = $oForm->getCleanValue('type');
  410. $sType = $this->_oConfig->getPrefix('common_post') . $sType;
  411. BxDolForm::setSubmittedValue('type', $sType, $oForm->aFormAttrs['method']);
  412. $aPhIds = $oForm->getCleanValue('content');
  413. BxDolForm::setSubmittedValue('content', serialize(array()), $oForm->aFormAttrs['method']);
  414. $iPhIds = count($aPhIds);
  415. $iId = $oForm->insert(array(
  416. 'object_id' => $iUserId,
  417. 'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
  418. 'title' => bx_process_input($sUserName . ' ' . _t('_bx_timeline_txt_added_photo' . ($iPhIds > 1 ? 's' : ''))),
  419. 'description' => '',
  420. 'date' => time()
  421. ));
  422. if(!empty($iId)) {
  423. if($iPhIds > 0) {
  424. $aPhTitles = $oForm->getCleanValue('file_title');
  425. $aPhTexts = $oForm->getCleanValue('file_text');
  426. bx_import('BxDolStorage');
  427. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->getObject('storage'));
  428. for($i = 0; $i < $iPhIds; $i++)
  429. if($this->_oDb->savePhoto($iId, $aPhIds[$i], $aPhTitles[$i], $aPhTexts[$i]))
  430. $oStorage->afterUploadCleanup($aPhIds[$i], $iUserId);
  431. }
  432. $this->onPost($iId);
  433. return array('id' => $iId);
  434. }
  435. return array('msg' => _t('_bx_timeline_txt_err_cannot_perform_action'));
  436. }
  437. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  438. }
  439. public function getCmtsObject($sSystem, $iId)
  440. {
  441. if(empty($sSystem) || (int)$iId == 0)
  442. return false;
  443. bx_import('BxDolCmts');
  444. $oCmts = BxDolCmts::getObjectInstance($sSystem, $iId);
  445. if(!$oCmts->isEnabled())
  446. return false;
  447. return $oCmts;
  448. }
  449. public function getUserId()
  450. {
  451. return isLogged() ? bx_get_logged_profile_id() : 0;
  452. }
  453. public function getUserInfo($iUserId = 0)
  454. {
  455. bx_import('BxDolProfile');
  456. $oProfile = BxDolProfile::getInstance($iUserId);
  457. if (!$oProfile) {
  458. bx_import('BxDolProfileUndefined');
  459. $oProfile = BxDolProfileUndefined::getInstance();
  460. }
  461. return array(
  462. $oProfile->getDisplayName(),
  463. $oProfile->getUrl(),
  464. $oProfile->getThumb(),
  465. $oProfile->getUnit()
  466. );
  467. }
  468. public function isAllowedPost($bPerform = false)
  469. {
  470. if(isAdmin())
  471. return true;
  472. $iUserId = $this->getUserId();
  473. if($iUserId == 0 && $this->_oConfig->isAllowGuestComments())
  474. return true;
  475. $aCheckResult = checkActionModule($iUserId, 'post', $this->getName(), $bPerform);
  476. return $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED;
  477. }
  478. public function isAllowedDelete($aEvent, $bPerform = false)
  479. {
  480. if(isAdmin())
  481. return true;
  482. $iUserId = (int)$this->getUserId();
  483. if((int)$aEvent['owner_id'] == $iUserId)
  484. return true;
  485. $aCheckResult = checkActionModule($iUserId, 'delete', $this->getName(), $bPerform);
  486. return $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED;
  487. }
  488. public function isAllowedComment($aEvent, $bPerform = false)
  489. {
  490. $mixedComments = $this->getCommentsData($aEvent['comments']);
  491. if($mixedComments === false)
  492. return false;
  493. list($sSystem, $iObjectId) = $mixedComments;
  494. $oCmts = $this->getCmtsObject($sSystem, $iObjectId);
  495. $oCmts->addCssJs();
  496. $iUserId = (int)$this->getUserId();
  497. if($iUserId == 0)
  498. return false;
  499. if(isAdmin())
  500. return true;
  501. return $oCmts->isPostReplyAllowed($bPerform);
  502. }
  503. public function isAllowedVote($aEvent, $bPerform = false)
  504. {
  505. $iUserId = (int)$this->getUserId();
  506. if($iUserId == 0)
  507. return false;
  508. if(isAdmin())
  509. return true;
  510. $aCheckResult = checkActionModule($iUserId, 'vote', $this->getName(), $bPerform);
  511. return $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED;
  512. }
  513. public function isAllowedShare($aEvent, $bPerform = false)
  514. {
  515. $iUserId = (int)$this->getUserId();
  516. if($iUserId == 0)
  517. return false;
  518. if(isAdmin())
  519. return true;
  520. $aCheckResult = checkActionModule($iUserId, 'share', $this->getName(), $bPerform);
  521. return $aCheckResult[CHECK_ACTION_RESULT] == CHECK_ACTION_RESULT_ALLOWED;
  522. }
  523. public function onPost($iId)
  524. {
  525. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
  526. if($this->_oConfig->isSystem($aEvent['type'], $aEvent['action'])) {
  527. $sPostType = 'system';
  528. $iSenderId = $aEvent['owner_id'];
  529. }
  530. else {
  531. $sPostType = 'common';
  532. $iSenderId = $aEvent['object_id'];
  533. }
  534. //--- Event -> Post for Alerts Engine ---//
  535. bx_import('BxDolAlerts');
  536. $oAlert = new BxDolAlerts($this->_oConfig->getSystemName('alert'), 'post_' . $sPostType, $iId, $iSenderId);
  537. $oAlert->alert();
  538. //--- Event -> Post for Alerts Engine ---//
  539. }
  540. public function onShare($iId)
  541. {
  542. $aEvent = $this->_oDb->getEvents(array('browse' => 'id', 'value' => $iId));
  543. $aContent = unserialize($aEvent['content']);
  544. $iSharedId = $this->_oDb->updateSharesCounter($aContent['type'], $aContent['action'], $aContent['object_id']);
  545. //--- Timeline -> Update for Alerts Engine ---//
  546. bx_import('BxDolAlerts');
  547. $oAlert = new BxDolAlerts($this->_oConfig->getSystemName('alert'), 'share', $iSharedId, $aEvent['object_id']);
  548. $oAlert->alert();
  549. //--- Timeline -> Update for Alerts Engine ---//
  550. }
  551. public function onDelete($aEvent)
  552. {
  553. $aPhotos = $this->_oDb->getPhotos($aEvent['id']);
  554. if(is_array($aPhotos) && !empty($aPhotos)) {
  555. bx_import('BxDolStorage');
  556. $oStorage = BxDolStorage::getObjectInstance($this->_oConfig->getObject('storage'));
  557. foreach($aPhotos as $aPhoto)
  558. $oStorage->deleteFile($aPhoto['id']);
  559. $this->_oDb->deletePhotos($aEvent['id']);
  560. }
  561. if($aEvent['type'] == $this->_oConfig->getPrefix('common_post') . BX_TIMELINE_PARSE_TYPE_SHARE) {
  562. $aContent = unserialize($aEvent['content']);
  563. $this->_oDb->updateSharesCounter($aContent['type'], $aContent['action'], $aContent['object_id'], -1);
  564. }
  565. //--- Event -> Delete for Alerts Engine ---//
  566. bx_import('BxDolAlerts');
  567. $oAlert = new BxDolAlerts($this->_oConfig->getSystemName('alert'), 'delete', $aEvent['id'], $this->getUserId());
  568. $oAlert->alert();
  569. //--- Event -> Delete for Alerts Engine ---//
  570. }
  571. public function getCommentsData(&$aComments)
  572. {
  573. if(empty($aComments) || !is_array($aComments))
  574. return false;
  575. $sSystem = isset($aComments['system']) ? $aComments['system'] : '';
  576. $iObjectId = isset($aComments['object_id']) ? (int)$aComments['object_id'] : 0;
  577. $iCount = isset($aComments['count']) ? (int)$aComments['count'] : 0;
  578. if($sSystem == '' || $iObjectId == 0 || ($iCount == 0 && !isLogged()))
  579. return false;
  580. return array($sSystem, $iObjectId, $iCount);
  581. }
  582. protected function _prepareParams($sType, $iOwnerId, $iStart, $iPerPage, $sFilter, $aModules, $iTimeline)
  583. {
  584. $aParams = array();
  585. $aParams['browse'] = 'list';
  586. $aParams['type'] = !empty($sType) ? $sType : BX_TIMELINE_TYPE_OWNER;
  587. $aParams['owner_id'] = (int)$iOwnerId != 0 ? $iOwnerId : $this->getUserId();
  588. $aParams['start'] = (int)$iStart > 0 ? $iStart : 0;
  589. $aParams['per_page'] = (int)$iPerPage > 0 ? $iPerPage : $this->_oConfig->getPerPage();
  590. $aParams['filter'] = !empty($sFilter) ? $sFilter : BX_TIMELINE_FILTER_ALL;
  591. $aParams['modules'] = is_array($aModules) && !empty($aModules) ? $aModules : array();
  592. $aParams['timeline'] = (int)$iTimeline > 0 ? $iTimeline : 0;
  593. return $aParams;
  594. }
  595. protected function _prepareParamsGet()
  596. {
  597. $aParams = array();
  598. $aParams['browse'] = 'list';
  599. $sType = bx_get('type');
  600. $aParams['type'] = $sType !== false ? bx_process_input($sType, BX_DATA_TEXT) : BX_TIMELINE_TYPE_OWNER;
  601. $iOwnerId = bx_get('owner_id');
  602. $aParams['owner_id'] = $sType !== false ? bx_process_input(bx_get('owner_id'), BX_DATA_INT) : $this->getUserId();
  603. $iStart = bx_get('start');
  604. $aParams['start'] = $iStart !== false ? bx_process_input($iStart, BX_DATA_INT) : 0;
  605. $iPerPage = bx_get('per_page');
  606. $aParams['per_page'] = $iPerPage !== false ? bx_process_input($iPerPage, BX_DATA_INT) : $this->_oConfig->getPerPage();
  607. $sFilter = bx_get('filter');
  608. $aParams['filter'] = $sFilter !== false ? bx_process_input($sFilter, BX_DATA_TEXT) : BX_TIMELINE_FILTER_ALL;
  609. $aModules = bx_get('modules');
  610. $aParams['modules'] = $aModules !== false ? bx_process_input($aModules, BX_DATA_TEXT) : array();
  611. $iTimeline = bx_get('timeline');
  612. $aParams['timeline'] = $iTimeline !== false ? bx_process_input($iTimeline, BX_DATA_INT) : 0;
  613. return $aParams;
  614. }
  615. protected function _updateHandlers($sModuleUri = 'all', $bInstall = true)
  616. {
  617. $aModules = $sModuleUri == 'all' ? $this->_oDb->getModules() : array($this->_oDb->getModuleByUri($sModuleUri));
  618. foreach($aModules as $aModule) {
  619. if(!BxDolRequest::serviceExists($aModule, 'get_timeline_data'))
  620. continue;
  621. $aData = BxDolService::call($aModule['name'], 'get_timeline_data');
  622. if(empty($aData) || !is_array($aData))
  623. continue;
  624. if($bInstall)
  625. $this->_oDb->insertData($aData);
  626. else
  627. $this->_oDb->deleteData($aData);
  628. }
  629. BxDolAlerts::cache();
  630. }
  631. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false) {
  632. header('Content-type: text/html; charset=utf-8');
  633. require_once(BX_DIRECTORY_PATH_PLUGINS . 'Services_JSON.php');
  634. $oParser = new Services_JSON();
  635. $s = $oParser->encode($a);
  636. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  637. $s = '<textarea>' . $s . '</textarea>'; // http://jquery.malsup.com/form/#file-upload
  638. echo $s;
  639. }
  640. }
  641. /** @} */