WecomCenterView.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php declare(strict_types = 0);
  2. /*
  3. ** WeCom Center - GET controller. Gathers current state and renders the page.
  4. */
  5. namespace Modules\WecomCenter\Actions;
  6. use CController;
  7. use CControllerResponseData;
  8. use CWebUser;
  9. use Modules\WecomCenter\Lib\Helper;
  10. use Modules\WecomCenter\Lib\I18n;
  11. class WecomCenterView extends CController {
  12. protected function init() {
  13. $this->disableCsrfValidation();
  14. }
  15. protected function checkInput() {
  16. return true;
  17. }
  18. protected function checkPermissions() {
  19. return CWebUser::getType() == USER_TYPE_SUPER_ADMIN;
  20. }
  21. protected function doAction() {
  22. $module_dir = dirname(__DIR__);
  23. $mediatypes = Helper::webhookMediatypes();
  24. // selected media type
  25. $mediatypeid = (string) ($_REQUEST['mediatypeid'] ?? '');
  26. if ($mediatypeid === '' && $mediatypes) {
  27. // prefer one whose name mentions wecom
  28. foreach ($mediatypes as $mt) {
  29. if (stripos($mt['name'], 'wecom') !== false) {
  30. $mediatypeid = (string) $mt['mediatypeid'];
  31. break;
  32. }
  33. }
  34. if ($mediatypeid === '') {
  35. $mediatypeid = (string) $mediatypes[0]['mediatypeid'];
  36. }
  37. }
  38. $mediatype = null;
  39. $webhook_url = '';
  40. $robot = null;
  41. $media = null;
  42. if ($mediatypeid !== '') {
  43. $mediatype = Helper::getMediatype($mediatypeid);
  44. if ($mediatype) {
  45. $webhook_url = Helper::webhookUrl($mediatype);
  46. }
  47. $robot = Helper::robotUser('wecomrobot');
  48. $media = Helper::currentMedia($robot, $mediatypeid);
  49. }
  50. // selected host for map tab
  51. $selected_hostid = (string) ($_REQUEST['hostid'] ?? '');
  52. $host_loc = ['location_lat' => '', 'location_lon' => ''];
  53. if ($selected_hostid !== '') {
  54. $h = Helper::hostLocation($selected_hostid);
  55. $inv = $h['inventory'] ?? [];
  56. $host_loc = [
  57. 'location_lat' => $inv['location_lat'] ?? '',
  58. 'location_lon' => $inv['location_lon'] ?? ''
  59. ];
  60. }
  61. $data = [
  62. 'title' => I18n::t('page.title'),
  63. 'mediatypes' => $mediatypes,
  64. 'mediatypeid' => $mediatypeid,
  65. 'mediatype' => $mediatype,
  66. 'webhook_url' => $webhook_url,
  67. 'robot' => $robot,
  68. 'robot_name' => 'wecomrobot',
  69. 'media' => $media,
  70. 'severity' => $media['severity'] ?? 48,
  71. 'active' => $media['active'] ?? 0,
  72. 'sendto' => $media['sendto'] ?? 'WeCom',
  73. 'schemes' => Helper::SCHEMES,
  74. 'templates' => Helper::bundledTemplates($module_dir),
  75. 'hosts' => Helper::hosts(),
  76. 'selected_hostid' => $selected_hostid,
  77. 'host_loc' => $host_loc,
  78. 'lang' => I18n::lang()
  79. ];
  80. $response = new CControllerResponseData($data);
  81. $response->setTitle(I18n::t('page.title'));
  82. $this->setResponse($response);
  83. }
  84. }