BxTasksMenuTimer.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @defgroup Tasks Tasks
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. class BxTasksMenuTimer extends BxTemplMenuCustom
  11. {
  12. protected $_sModule;
  13. protected $_oModule;
  14. protected $_iContentId;
  15. protected $_iProfileId;
  16. protected $_bShowAsButton;
  17. protected $_bShowTitle;
  18. public function __construct($aObject, $oTemplate = false)
  19. {
  20. parent::__construct($aObject, $oTemplate);
  21. $this->_sModule = 'bx_tasks';
  22. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  23. $this->_iContentId = 0;
  24. $this->_iProfileId = 0;
  25. $this->_bShowAsButton = true;
  26. $this->_bShowTitle = true;
  27. }
  28. public function setParams($iContentId, $iProfileId)
  29. {
  30. $this->_iContentId = $iContentId;
  31. $this->_iProfileId = $iProfileId;
  32. }
  33. protected function getMenuItemsRaw ()
  34. {
  35. $sJsObject = $this->_oModule->_oConfig->getJsObject('timer');
  36. $aTimer = $this->_oModule->_oDb->getTimers([
  37. 'sample' => 'content_profile_ids',
  38. 'content_id' => $this->_iContentId,
  39. 'profile_id' => $this->_iProfileId
  40. ]);
  41. $aItems = [];
  42. if($aTimer && is_array($aTimer)) {
  43. if((int)$aTimer['started'] > 0)
  44. $aItems = [
  45. ['id' => 2, 'name' => 'pause', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.pause(this, ' . $this->_iContentId . ', ' . $this->_iProfileId . ')', 'target' => '_self', 'title' => _t('_bx_tasks_txt_timer_pause')],
  46. ['id' => 6, 'name' => 'log', 'class' => '', 'link' => '', 'onclick' => '', 'target' => '', 'title' => _t('_bx_tasks_txt_timer_log')],
  47. ['id' => 7, 'name' => 'edit-and-log', 'class' => '', 'link' => '', 'onclick' => '', 'target' => '', 'title' => ''],
  48. ];
  49. else
  50. $aItems = [
  51. ['id' => 3, 'name' => 'resume', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.resume(this, ' . $this->_iContentId . ', ' . $this->_iProfileId . ')', 'target' => '_self', 'title' => _t('_bx_tasks_txt_timer_resume')],
  52. ['id' => 4, 'name' => 'clear', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.clear(this, ' . $this->_iContentId . ', ' . $this->_iProfileId . ')', 'target' => '_self', 'title' => _t('_bx_tasks_txt_timer_clear')],
  53. ['id' => 6, 'name' => 'log', 'class' => '', 'link' => '', 'onclick' => '', 'target' => '', 'title' => _t('_bx_tasks_txt_timer_log')],
  54. ['id' => 7, 'name' => 'edit-and-log', 'class' => '', 'link' => '', 'onclick' => '', 'target' => '', 'title' => ''],
  55. ];
  56. }
  57. else {
  58. $aItems = [
  59. ['id' => 1, 'name' => 'start', 'class' => '', 'link' => 'javascript:void(0)', 'onclick' => 'javascript:' . $sJsObject . '.start(this, ' . $this->_iContentId . ', ' . $this->_iProfileId . ')', 'target' => '_self', 'title' => _t('_bx_tasks_txt_timer_start')],
  60. ['id' => 5, 'name' => 'time', 'class' => '', 'link' => '', 'onclick' => '', 'target' => '', 'title' => ''],
  61. ];
  62. }
  63. return $aItems;
  64. }
  65. protected function _getMenuItemTime($aItem, $aParams = [])
  66. {
  67. $CNF = &$this->_oModule->_oConfig->CNF;
  68. $iId = $this->_iContentId;
  69. $sObject = $CNF['OBJECT_REPORTS_TIME'];
  70. $oObject = !empty($sObject) ? BxDolReport::getObjectInstance($sObject, $iId) : false;
  71. if(!$oObject || !$oObject->isEnabled())
  72. return '';
  73. $aObjectOptions = [
  74. 'dynamic_mode' => $this->_bDynamicMode,
  75. 'show_do_report_as_button' => $this->_bShowAsButton,
  76. 'show_do_report_label' => $this->_bShowTitle,
  77. 'show_do_report_icon' => false,
  78. 'show_counter' => false
  79. ];
  80. if($aParams && is_array($aParams))
  81. $aObjectOptions = array_merge($aObjectOptions, $aParams);
  82. if($this->_bIsApi)
  83. return [
  84. 'id' => $aItem['id'],
  85. 'name' => $aItem['name'],
  86. 'display_type' => 'element',
  87. 'data' => $oObject->getElementApi($aObjectOptions)
  88. ];
  89. return $oObject->getElementBlock($aObjectOptions);
  90. }
  91. protected function _getMenuItemLog($aItem)
  92. {
  93. if($this->_bIsApi)
  94. return array_merge($this->_getMenuItemAPI($aItem), [
  95. 'display_type' => 'callback',
  96. 'data' => [
  97. 'request_url' => $this->_sModule . '/process_timer/&params[]=log&params[]=' . $this->_iContentId . '&params[]=' . $this->_iProfileId,
  98. 'on_callback' => 'hide'
  99. ]
  100. ]);
  101. return $this->_getMenuItemTime($aItem, [
  102. 'uniq_id' => genRndPwd(3, false),
  103. 'show_do_report_form' => false,
  104. 'do_report_label' => '_bx_tasks_txt_timer_log'
  105. ]);
  106. }
  107. protected function _getMenuItemEditAndLog($aItem)
  108. {
  109. return $this->_getMenuItemTime($aItem, [
  110. 'do_report_label' => '_bx_tasks_txt_timer_edit_and_log'
  111. ]);
  112. }
  113. protected function _getMenuItemDefault($aItem)
  114. {
  115. if($this->_bIsApi)
  116. return $aItem;
  117. $aItem['class_wrp'] = 'bx-tasks-timer-action' . (!empty($aItem['class_wrp']) ? ' ' . $aItem['class_wrp'] : '');
  118. if($this->_bShowAsButton)
  119. $aItem['class_link'] = 'bx-btn' . (isset($aItem['primary']) && (int)$aItem['primary'] == 1 ? ' bx-btn-primary' : '') . (!empty($aItem['class_link']) ? ' ' . $aItem['class_link'] : '');
  120. if(!$this->_bShowTitle)
  121. $aItem['bx_if:title']['condition'] = false;
  122. return parent::_getMenuItemDefault ($aItem);
  123. }
  124. protected function _getMenuCallbackDataAPI($a)
  125. {
  126. $aResult = [];
  127. switch($a['name']) {
  128. case 'start':
  129. $aResult = [
  130. 'request_url' => $this->_sModule . '/process_timer/&params[]=start&params[]=' . $this->_iContentId . '&params[]=' . $this->_iProfileId,
  131. 'on_callback' => 'hide'
  132. ];
  133. break;
  134. case 'pause':
  135. $aResult = [
  136. 'request_url' => $this->_sModule . '/process_timer/&params[]=pause&params[]=' . $this->_iContentId . '&params[]=' . $this->_iProfileId,
  137. 'on_callback' => 'hide'
  138. ];
  139. break;
  140. case 'resume':
  141. $aResult = [
  142. 'request_url' => $this->_sModule . '/process_timer/&params[]=resume&params[]=' . $this->_iContentId . '&params[]=' . $this->_iProfileId,
  143. 'on_callback' => 'hide'
  144. ];
  145. break;
  146. case 'clear':
  147. $aResult = [
  148. 'request_url' => $this->_sModule . '/process_timer/&params[]=clear&params[]=' . $this->_iContentId . '&params[]=' . $this->_iProfileId,
  149. 'on_callback' => 'hide'
  150. ];
  151. break;
  152. }
  153. return $aResult;
  154. }
  155. }
  156. /** @} */