BxTimelineModule.php 30 KB

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