promptmenu.html.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * The ai prompt menu view file of ai module of ZenTaoPMS.
  4. *
  5. * This view file is used to print the prompt menu, acts just like header php files.
  6. * Prompt menus are generated with php and injected with javascript. A lot of hacking
  7. * went into this, so please don't touch it unless you know what you are doing.
  8. *
  9. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  10. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  11. * @author Wenrui LI <liwenrui@easycorp.ltd>
  12. * @package ai
  13. * @link https://www.zentao.net
  14. */
  15. ?>
  16. <?php
  17. $this->loadModel('ai');
  18. if($this->ai->hasModelsAvailable() && commonModel::hasPriv('ai', 'promptExecute')):?>
  19. <?php
  20. $this->app->loadConfig('ai');
  21. $module = $this->app->getModuleName();
  22. $method = $this->app->getMethodName();
  23. if(isset($config->ai->menuPrint->locations[$module][$method])):
  24. ?>
  25. <?php
  26. $menuOptions = $config->ai->menuPrint->locations[$module][$method];
  27. $prompts = $this->ai->getPromptsForUser($menuOptions->module);
  28. $prompts = $this->ai->filterPromptsForExecution($prompts, true);
  29. if(!empty($prompts)):
  30. ?>
  31. <?php
  32. $html = '';
  33. $objectVarName = empty($menuOptions->objectVarName) ? $menuOptions->module : $menuOptions->objectVarName;
  34. $currentObjectId = !empty($this->view->$objectVarName) ? $this->view->$objectVarName->id : 0;
  35. if(count($prompts) > 1)
  36. {
  37. $btnName = sprintf($this->lang->ai->promptMenu->dropdownTitle, isset($this->lang->ai->dataSource[$module]['common']) ? $this->lang->ai->dataSource[$module]['common'] : '');
  38. $html .= '<div class="prompts dropdown' . ((isset($menuOptions->class) ? ' ' . $menuOptions->class : '') . (isset($menuOptions->dropdownClass) ? ' ' . $menuOptions->dropdownClass : '')) . '"><button class="btn btn-link' . (isset($menuOptions->buttonClass) ? ' ' . $menuOptions->buttonClass : '') . '" type="button" data-toggle="dropdown">' . $btnName . ' <i class="icon-caret-down"></i></button><ul class="dropdown-menu pull-right">';
  39. foreach($prompts as $prompt) $html .= '<li>' . html::linkButton($prompt->name . ($prompt->status != 'active' ? '<span class="label label-info label-badge" style="margin-left: 4px;">' . $lang->ai->prompts->statuses[$prompt->status] . '</span>' : ''), helper::createLink('ai', 'promptExecute', "promptId=$prompt->id&objectId=$currentObjectId"), 'self', "style='width: 100%;'" . (empty($prompt->unauthorized) ? '' : ' disabled') . (empty($prompt->desc) ? '' : " data-toggle='popover' data-container='body' data-trigger='hover' data-content='$prompt->desc' data-title='$prompt->name' data-placement='left'"), 'btn btn-link text-left') . '</li>';
  40. $html .= '</ul></div>';
  41. }
  42. else
  43. {
  44. $prompt = current($prompts);
  45. $html .= html::linkButton($prompt->name . ($prompt->status != 'active' ? '<span class="label label-info label-badge" style="margin-left: 4px;">' . $lang->ai->prompts->statuses[$prompt->status] . '</span>' : ''), helper::createLink('ai', 'promptExecute', "promptId=$prompt->id&objectId=$currentObjectId"), 'self', (empty($prompt->unauthorized) ? '' : 'disabled') . (empty($prompt->desc) ? '' : " data-toggle='popover' data-container='body' data-trigger='hover' data-content='$prompt->desc' data-title='$prompt->name' data-placement='bottom'"), 'prompt btn btn-link' . ((isset($menuOptions->class) ? ' ' . $menuOptions->class : '') . (isset($menuOptions->buttonClass) ? ' ' . $menuOptions->buttonClass : '')));
  46. }
  47. ?>
  48. <?php if(isset($menuOptions->stylesheet)):?>
  49. <style><?php echo $menuOptions->stylesheet;?></style>
  50. <?php endif;?>
  51. <script>
  52. $(function()
  53. {
  54. if(window.location.search.includes('onlybody')) return;
  55. const container = window.frameElement?.closest('.load-indicator');
  56. if(container && container.dataset.loading)
  57. {
  58. delete container.dataset.loading;
  59. container.classList.remove('loading');
  60. container.classList.remove('no-delay');
  61. }
  62. $(`<?php echo $menuOptions->targetContainer;?>`).<?php echo isset($menuOptions->injectMethod) ? $menuOptions->injectMethod : 'append';?>(`<?php echo $html;?>`);
  63. $('[data-toggle="popover"]').popover({template: '<div class="popover"><h3 class="popover-title"></h3><div class="popover-content"></div></div>'});
  64. $('<?php echo count($prompts) > 1 ? '.prompts.dropdown ul.dropdown-menu' : '.prompt';?>').on('click', <?php if(count($prompts) > 1) echo "'button',";?> function(e)
  65. {
  66. if(!container) return;
  67. container.dataset.loading = e.target.querySelector('.label') ? '<?php echo $lang->ai->execute->auditing;?>' : '<?php echo $lang->ai->execute->loading;?>';
  68. container.classList.add('loading');
  69. container.classList.add('no-delay');
  70. /* Checks for session storage to cancel loading status (see inputinject.html.php). */
  71. sessionStorage.removeItem('ai-prompt-data-injected');
  72. const loadCheckInterval = setInterval(function()
  73. {
  74. if(sessionStorage.getItem('ai-prompt-data-injected'))
  75. {
  76. if(container && container.dataset.loading)
  77. {
  78. delete container.dataset.loading;
  79. container.classList.remove('loading');
  80. container.classList.remove('no-delay');
  81. }
  82. sessionStorage.removeItem('ai-prompt-data-injected');
  83. clearInterval(loadCheckInterval);
  84. }
  85. }, 200);
  86. });
  87. });
  88. </script>
  89. <?php endif; endif; endif;?>