ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBuddySystemGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 {
30  private const BS_REQUEST_HTTP_GET = 1;
31  private const BS_REQUEST_HTTP_POST = 2;
32 
33  protected static bool $isFrontendInitialized = false;
34 
38  protected ilObjUser $user;
39  protected ilLanguage $lng;
40  protected Services $http;
42 
43  public function __construct()
44  {
45  global $DIC;
46  $this->main_tpl = $DIC->ui()->mainTemplate();
47 
48  $this->http = $DIC->http();
49  $this->ctrl = $DIC['ilCtrl'];
50  $this->user = $DIC['ilUser'];
51  $this->lng = $DIC['lng'];
52 
53  $this->buddyList = ilBuddyList::getInstanceByGlobalUser();
54  $this->stateFactory = ilBuddySystemRelationStateFactory::getInstance();
55 
56  $this->lng->loadLanguageModule('buddysystem');
57  }
58 
59  public static function initializeFrontend(ilGlobalTemplateInterface $page): void
60  {
61  global $DIC;
62 
63  if (
64  !self::$isFrontendInitialized &&
65  ilBuddySystem::getInstance()->isEnabled() &&
66  !$DIC->user()->isAnonymous()
67  ) {
68  $DIC->language()->loadLanguageModule('buddysystem');
69 
70  $page->addJavaScript('./Services/Contact/BuddySystem/js/buddy_system.js');
71 
72  $config = new stdClass();
73  $config->http_post_url = $DIC->ctrl()->getFormActionByClass([
74  ilUIPluginRouterGUI::class,
75  self::class
76  ], '', '', true, false);
77  $config->transition_state_cmd = 'transitionAsync';
78  $page->addOnLoadCode('il.BuddySystem.setConfig(' . json_encode($config, JSON_THROW_ON_ERROR) . ');');
79 
80  $btn_config = new stdClass();
81  $btn_config->bnt_class = 'ilBuddySystemLinkWidget';
82 
83  $page->addOnLoadCode('il.BuddySystemButton.setConfig(' . json_encode($btn_config, JSON_THROW_ON_ERROR) . ');');
84  $page->addOnLoadCode('il.BuddySystemButton.init();');
85 
86  self::$isFrontendInitialized = true;
87  }
88  }
89 
93  public function executeCommand(): void
94  {
95  if ($this->user->isAnonymous()) {
96  throw new RuntimeException('This controller only accepts requests of logged in users');
97  }
98 
99  $nextClass = $this->ctrl->getNextClass($this);
100  $cmd = $this->ctrl->getCmd();
101 
102  switch ($nextClass) {
103  default:
104  $cmd .= 'Command';
105  $this->$cmd();
106  break;
107  }
108  }
109 
115  protected function isRequestParameterGiven(string $key, int $type): bool
116  {
117  switch ($type) {
118  case self::BS_REQUEST_HTTP_POST:
119  $body = $this->http->request()->getParsedBody();
120  return (isset($body[$key]) && is_string($body[$key]) && $body[$key] !== '');
121 
122  case self::BS_REQUEST_HTTP_GET:
123  default:
124  $query = $this->http->request()->getQueryParams();
125  return (isset($query[$key]) && is_string($query[$key]) && $query[$key] !== '');
126  }
127  }
128 
129  private function requestCommand(): void
130  {
131  $this->transitionCommand(
132  'request',
133  'buddy_relation_requested',
134  static function (ilBuddySystemRelation $relation): void {
135  if (
136  $relation->isUnlinked() &&
137  !ilUtil::yn2tf((string) ilObjUser::_lookupPref($relation->getBuddyUsrId(), 'bs_allow_to_contact_me'))
138  ) {
139  throw new ilException('The requested user does not want to get contact requests');
140  }
141  }
142  );
143  }
144 
145  private function ignoreCommand(): void
146  {
147  $this->transitionCommand('ignore', 'buddy_request_ignored');
148  }
149 
150  private function linkCommand(): void
151  {
152  $this->transitionCommand('link', 'buddy_request_approved');
153  }
154 
155  private function transitionCommand(
156  string $cmd,
157  string $positiveFeedbackLanguageId,
158  callable $onBeforeExecute = null
159  ): void {
160  if (!$this->isRequestParameterGiven('user_id', self::BS_REQUEST_HTTP_GET)) {
161  $this->main_tpl->setOnScreenMessage('info', $this->lng->txt('buddy_bs_action_not_possible'), true);
162  $this->ctrl->returnToParent($this);
163  }
164 
165  $usrId = (int) $this->http->request()->getQueryParams()['user_id'];
166  try {
167  $relation = ilBuddyList::getInstanceByGlobalUser()->getRelationByUserId($usrId);
168 
169  if (null !== $onBeforeExecute) {
170  $onBeforeExecute($relation);
171  }
172 
173  ilBuddyList::getInstanceByGlobalUser()->{$cmd}($relation);
174  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt($positiveFeedbackLanguageId), true);
176  $this->main_tpl->setOnScreenMessage('info', sprintf(
177  $this->lng->txt($e->getMessage()),
179  ), true);
180  } catch (ilException $e) {
181  $this->main_tpl->setOnScreenMessage('info', $this->lng->txt('buddy_bs_action_not_possible'), true);
182  }
183 
184  $this->redirectToReferer();
185  }
186 
190  private function transitionAsyncCommand(): void
191  {
192  if (!$this->ctrl->isAsynch()) {
193  throw new RuntimeException('This action only supports AJAX http requests');
194  }
195 
196  if (!$this->isRequestParameterGiven('usr_id', self::BS_REQUEST_HTTP_POST)) {
197  throw new RuntimeException('Missing "usr_id" parameter');
198  }
199 
200  if (!$this->isRequestParameterGiven('action', self::BS_REQUEST_HTTP_POST)) {
201  throw new RuntimeException('Missing "action" parameter');
202  }
203 
204  $response = new stdClass();
205  $response->success = false;
206 
207  try {
208  $usr_id = (int) $this->http->request()->getParsedBody()['usr_id'];
209  $action = ilUtil::stripSlashes($this->http->request()->getParsedBody()['action']);
210 
211  if (ilObjUser::_isAnonymous($usr_id)) {
212  throw new ilBuddySystemException(sprintf(
213  'You cannot perform a state transition for the anonymous user (id: %s)',
214  $usr_id
215  ));
216  }
217 
218  $login = ilObjUser::_lookupLogin($usr_id);
219  if ($login === '') {
220  throw new ilBuddySystemException(sprintf(
221  'You cannot perform a state transition for a non existing user (id: %s)',
222  $usr_id
223  ));
224  }
225 
226  $relation = $this->buddyList->getRelationByUserId($usr_id);
227 
228  // The ILIAS JF decided to add a new personal setting
229  if (
230  $relation->isUnlinked() &&
231  !ilUtil::yn2tf((string) ilObjUser::_lookupPref($relation->getBuddyUsrId(), 'bs_allow_to_contact_me'))
232  ) {
233  throw new ilException('The requested user does not want to get contact requests');
234  }
235 
236  try {
237  $this->buddyList->{$action}($relation);
238  $response->success = true;
240  $response->message = sprintf($this->lng->txt($e->getMessage()), $login);
241  } catch (Exception $e) {
242  $response->message = $this->lng->txt('buddy_bs_action_not_possible');
243  }
244 
245  $response->state = get_class($relation->getState());
246  $response->state_html = $this->stateFactory->getStateButtonRendererByOwnerAndRelation(
247  $this->buddyList->getOwnerId(),
248  $relation
249  )->getHtml();
250  } catch (Exception $e) {
251  $response->message = $this->lng->txt('buddy_bs_action_not_possible');
252  }
253 
254  $this->http->saveResponse(
255  $this->http->response()
256  ->withAddedHeader('Content-Type', 'application/json')
257  ->withBody(ILIAS\Filesystem\Stream\Streams::ofString(json_encode($response, JSON_THROW_ON_ERROR)))
258  );
259  $this->http->sendResponse();
260  $this->http->close();
261  }
262 
263  private function redirectToReferer(): void
264  {
265  if (isset($this->http->request()->getServerParams()['HTTP_REFERER'])) {
266  $redirectUrl = $this->http->request()->getServerParams()['HTTP_REFERER'];
267  $urlParts = parse_url($redirectUrl);
268 
269  if (isset($urlParts['path'])) {
270  $script = basename($urlParts['path'], '.php');
271  if ($script === 'login') {
272  $this->ctrl->returnToParent($this);
273  } else {
274  $redirectUrl = ltrim(basename($urlParts['path']), '/');
275  if (isset($urlParts['query'])) {
276  $redirectUrl .= '?' . $urlParts['query'];
277  }
278  }
279  }
280 
281  if ($redirectUrl !== '') {
282  $this->ctrl->redirectToURL($redirectUrl);
283  }
284  }
285 
286  $this->ctrl->returnToParent($this);
287  }
288 }
Class ilBuddySystemGUI.
static getInstanceByGlobalUser()
Class ilBuddySystemRelationStateFactory.
$type
ilBuddySystemRelationStateFactory $stateFactory
Class ChatMainBarProvider .
ilGlobalTemplateInterface $main_tpl
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
static _lookupPref(int $a_usr_id, string $a_keyword)
Class ilBuddySystemException.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
addJavaScript(string $a_js_file, bool $a_add_version_parameter=true, int $a_batch=2)
Add a javascript file that should be included in the header.
$query
static initializeFrontend(ilGlobalTemplateInterface $page)
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
static _isAnonymous(int $usr_id)
transitionAsyncCommand()
Performs a state transition based on the request action.
transitionCommand(string $cmd, string $positiveFeedbackLanguageId, callable $onBeforeExecute=null)
static yn2tf(string $a_yn)
$response
Class FlySystemFileAccessTest disabled disabled disabled.
static bool $isFrontendInitialized
isRequestParameterGiven(string $key, int $type)
static _lookupLogin(int $a_user_id)