ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilChatroomGUIHandler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 
37 abstract class ilChatroomGUIHandler
38 {
39  protected ilObjUser $ilUser;
41  protected ilLanguage $ilLng;
44  protected FileUpload $upload;
47  protected ILIAS $ilias;
49  protected ilTree $tree;
50  protected ilTabsGUI $tabs;
51  protected UIFactory $uiFactory;
54  protected Refinery $refinery;
55 
59  public function __construct(protected ilChatroomObjectGUI $gui)
60  {
62  global $DIC;
63  $this->ilUser = $DIC->user();
64  $this->ilCtrl = $DIC->ctrl();
65  $this->ilLng = $DIC->language();
66  $this->rbacsystem = $DIC->rbac()->system();
67  $this->mainTpl = $DIC->ui()->mainTemplate();
68  $this->upload = $DIC->upload();
69  $this->webDirectory = $DIC->filesystem()->web();
70  $this->obj_service = $DIC->object();
71  $this->ilias = $DIC['ilias'];
72  $this->tabs = $DIC->tabs();
73  $this->navigationHistory = $DIC['ilNavigationHistory'];
74  $this->tree = $DIC['tree'];
75  $this->uiFactory = $DIC->ui()->factory();
76  $this->uiRenderer = $DIC->ui()->renderer();
77  $this->http = $DIC->http();
78  $this->refinery = $DIC->refinery();
79  }
80 
85  protected function getRequestValue(string $key, Transformation $trafo, $default = null)
86  {
87  if ($this->http->wrapper()->query()->has($key)) {
88  return $this->http->wrapper()->query()->retrieve($key, $trafo);
89  }
90 
91  if ($this->http->wrapper()->post()->has($key)) {
92  return $this->http->wrapper()->post()->retrieve($key, $trafo);
93  }
94 
95  return $default;
96  }
97 
98  protected function hasRequestValue(string $key): bool
99  {
100  if ($this->http->wrapper()->query()->has($key)) {
101  return true;
102  }
103 
104  return $this->http->wrapper()->post()->has($key);
105  }
106 
107  public function execute(string $method): void
108  {
109  $this->ilLng->loadLanguageModule('chatroom');
110 
111  if (is_callable([$this, $method])) {
112  $this->$method();
113  return;
114  }
115 
116  $this->executeDefault($method);
117  }
118 
119  abstract public function executeDefault(string $requestedMethod): void;
120 
125  public function redirectIfNoPermission($permission): void
126  {
127  if (!ilChatroom::checkUserPermissions($permission, $this->gui->getRefId())) {
128  $this->ilCtrl->setParameterByClass(ilRepositoryGUI::class, 'ref_id', ROOT_FOLDER_ID);
129  $this->ilCtrl->redirectByClass(ilRepositoryGUI::class);
130  }
131  }
132 
138  public function isSuccessful($response): bool
139  {
140  if (!is_string($response)) {
141  return false;
142  }
143 
144  $response = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
145 
146  return $response !== null && array_key_exists('success', $response) && $response['success'];
147  }
148 
149  protected function getRoomByObjectId(int $objectId): ?ilChatroom
150  {
151  return ilChatroom::byObjectId($objectId);
152  }
153 
157  protected function exitIfNoRoomExists(?ilChatroom $room): void
158  {
159  if (null === $room) {
160  $this->sendJSONResponse([
161  'success' => false,
162  'reason' => 'unknown room',
163  ]);
164  }
165  }
166 
170  protected function sendJSONResponse($response): void
171  {
172  $this->sendResponse(json_encode($response), 'application/json');
173  }
174 
178  protected function sendResponse(string $content, string $type): void
179  {
180  $this->http->saveResponse(
181  $this->http->response()
182  ->withHeader(ResponseHeader::CONTENT_TYPE, $type)
183  ->withBody(Streams::ofString($content))
184  );
185  $this->http->sendResponse();
186  $this->http->close();
187  }
188 
189  public function hasPermission(string $permission): bool
190  {
191  return ilChatroom::checkUserPermissions($permission, $this->gui->getRefId());
192  }
193 }
static checkUserPermissions($permissions, int $ref_id, bool $send_info=true)
Checks user permissions by given array and ref_id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const ROOT_FOLDER_ID
Definition: constants.php:32
redirectIfNoPermission($permission)
Checks for requested permissions and redirects if the permission check failed.
Interface Observer Contains several chained tasks and infos about them.
getRequestValue(string $key, Transformation $trafo, $default=null)
$response
Definition: xapitoken.php:93
exitIfNoRoomExists(?ilChatroom $room)
Checks if a ilChatroom exists.
ilNavigationHistory $navigationHistory
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
sendResponse(string $content, string $type)
Sends a response and exits the php process.
executeDefault(string $requestedMethod)
static http()
Fetches the global http state from ILIAS.
Class ilChatroomGUIHandler.
global $DIC
Definition: shib_login.php:26
Class ilObjForumAdministration.
isSuccessful($response)
Checks for success param in an json decoded response.
Class FileUpload.
Definition: FileUpload.php:37
sendJSONResponse($response)
Sends a json encoded response and exits the php process.
Last visited history for repository items.
ilGlobalTemplateInterface $mainTpl
__construct(Container $dic, ilPlugin $plugin)
static byObjectId(int $object_id)
A transformation is a function from one datatype to another.