1
0

BxTimelineModule.php 30 KB

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