BxNtfsMenuPreview.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Notifications Notifications
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxNtfsMenuPreview extends BxTemplMenuCustom
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sTmplContentPreviewItem;
  16. protected $_iOwnerId;
  17. protected $_aBrowseParams;
  18. public function __construct($aObject, $oTemplate = false)
  19. {
  20. $this->_sModule = 'bx_notifications';
  21. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  22. if(!$oTemplate)
  23. $oTemplate = $this->_oModule->_oTemplate;
  24. parent::__construct($aObject, $oTemplate);
  25. if(empty($this->_sTmplContentPreviewItem))
  26. $this->_sTmplContentPreviewItem = $this->_oTemplate->getHtml('menu_preview_item.html');
  27. $this->_iOwnerId = $this->_oModule->getUserId();
  28. $this->_aBrowseParams = array(
  29. 'last_read' => $this->_oModule->_oDb->getLastRead($this->_iOwnerId)
  30. );
  31. }
  32. public function getMenuItems ()
  33. {
  34. $aItems = parent::getMenuItems();
  35. if(empty($aItems) || !is_array($aItems))
  36. return MsgBox(_t('_Empty'));
  37. return $aItems;
  38. }
  39. public function getCode ()
  40. {
  41. return $this->_oTemplate->getJsCode('view', [], true, true) . $this->_oTemplate->parseHtmlByName('menu_preview.html', array(
  42. 'content' => parent::getCode()
  43. ));
  44. }
  45. protected function getMenuItemsRaw ()
  46. {
  47. $iPerPage = $this->_oModule->_oConfig->getPerPage('preview');
  48. $aItems = $this->_oModule->serviceGetNotifications($this->_iOwnerId, ['per_page' => 3 * $iPerPage]);
  49. if(empty($aItems) || !is_array($aItems))
  50. return array();
  51. $aResults = array();
  52. foreach($aItems as $iKey => $aItem) {
  53. if(!$this->_oModule->_oTemplate->getPost($aItem))
  54. continue;
  55. $aItem['name'] = 'event';
  56. $aResults[] = $aItem;
  57. }
  58. if(count($aResults) > $iPerPage)
  59. $aResults = array_slice($aResults, 0, $iPerPage);
  60. $aFirst = reset($aItems);
  61. $this->_oModule->_oDb->markAsRead($this->_iOwnerId, $aFirst['id']);
  62. return array_merge($aResults, $this->_oQuery->getMenuItems());
  63. }
  64. protected function _getMenuItemDefault ($aItem)
  65. {
  66. return $this->_oTemplate->parseHtmlByContent($this->_sTmplContentPreviewItem, $aItem);
  67. }
  68. protected function _getMenuItemEvent($aItem)
  69. {
  70. return $this->_oTemplate->getPost($aItem, $this->_aBrowseParams);
  71. }
  72. }
  73. /** @} */