19declare(strict_types=1);
46 $this->main_tpl =
$DIC->ui()->mainTemplate();
48 $this->
http = $DIC->http();
49 $this->
ctrl = $DIC[
'ilCtrl'];
50 $this->
user = $DIC[
'ilUser'];
51 $this->
lng = $DIC[
'lng'];
56 $this->
lng->loadLanguageModule(
'buddysystem');
64 !self::$isFrontendInitialized &&
66 !
$DIC->user()->isAnonymous()
68 $DIC->language()->loadLanguageModule(
'buddysystem');
72 $config =
new stdClass();
73 $config->http_post_url =
$DIC->ctrl()->getFormActionByClass([
74 ilUIPluginRouterGUI::class,
76 ],
'',
'',
true,
false);
77 $config->transition_state_cmd =
'transitionAsync';
78 $page->
addOnLoadCode(
'il.BuddySystem.setConfig(' . json_encode($config, JSON_THROW_ON_ERROR) .
');');
80 $btn_config =
new stdClass();
83 $page->
addOnLoadCode(
'il.BuddySystemButton.setConfig(' . json_encode($btn_config, JSON_THROW_ON_ERROR) .
');');
86 self::$isFrontendInitialized =
true;
95 if ($this->
user->isAnonymous()) {
96 throw new RuntimeException(
'This controller only accepts requests of logged in users');
99 $this->{$this->
ctrl->getCmd() .
'Command'}();
106 $body = $this->
http->request()->getParsedBody();
107 return (isset($body[$key]) && is_string($body[$key]) && $body[$key] !==
'');
111 $query = $this->
http->request()->getQueryParams();
112 return (isset($query[$key]) && is_string($query[$key]) && $query[$key] !==
'');
120 'buddy_relation_requested',
126 throw new ilException(
'The requested user does not want to get contact requests');
144 string $positiveFeedbackLanguageId,
145 ?callable $onBeforeExecute =
null
148 $this->main_tpl->setOnScreenMessage(
'info', $this->
lng->txt(
'buddy_bs_action_not_possible'),
true);
149 $this->
ctrl->returnToParent($this);
152 $usrId = (
int) $this->
http->request()->getQueryParams()[
'user_id'];
156 if (
null !== $onBeforeExecute) {
161 $this->main_tpl->setOnScreenMessage(
'success', $this->
lng->txt($positiveFeedbackLanguageId),
true);
163 $this->main_tpl->setOnScreenMessage(
'info', sprintf(
164 $this->
lng->txt($e->getMessage()),
167 }
catch (Exception) {
168 $this->main_tpl->setOnScreenMessage(
'info', $this->
lng->txt(
'buddy_bs_action_not_possible'),
true);
171 $this->redirectToReferer();
179 if (!$this->
ctrl->isAsynch()) {
180 throw new RuntimeException(
'This action only supports AJAX http requests');
183 if (!$this->isRequestParameterGiven(
'usr_id', self::BS_REQUEST_HTTP_POST)) {
184 throw new RuntimeException(
'Missing "usr_id" parameter');
187 if (!$this->isRequestParameterGiven(
'action', self::BS_REQUEST_HTTP_POST)) {
188 throw new RuntimeException(
'Missing "action" parameter');
195 $usr_id = (
int) $this->
http->request()->getParsedBody()[
'usr_id'];
200 'You cannot perform a state transition for the anonymous user (id: %s)',
208 'You cannot perform a state transition for a non existing user (id: %s)',
213 $relation = $this->buddyList->getRelationByUserId($usr_id);
220 throw new ilException(
'The requested user does not want to get contact requests');
227 $response->message = sprintf($this->
lng->txt($e->getMessage()), $login);
228 }
catch (Exception) {
229 $response->message = $this->
lng->txt(
'buddy_bs_action_not_possible');
233 $response->state_html = $this->stateFactory->getStateButtonRendererByOwnerAndRelation(
234 $this->buddyList->getOwnerId(),
237 }
catch (Exception) {
238 $response->message = $this->
lng->txt(
'buddy_bs_action_not_possible');
241 $this->
http->saveResponse(
242 $this->
http->response()
243 ->withAddedHeader(
'Content-Type',
'application/json')
244 ->withBody(
ILIAS\Filesystem\Stream\Streams::ofString(json_encode(
$response, JSON_THROW_ON_ERROR)))
246 $this->
http->sendResponse();
247 $this->
http->close();
252 if (isset($this->
http->request()->getServerParams()[
'HTTP_REFERER'])) {
253 $redirectUrl = $this->
http->request()->getServerParams()[
'HTTP_REFERER'];
254 $urlParts = parse_url($redirectUrl);
256 if (isset($urlParts[
'path'])) {
257 $script = basename($urlParts[
'path'],
'.php');
258 if ($script ===
'login') {
259 $this->
ctrl->returnToParent($this);
261 $redirectUrl = ltrim(basename($urlParts[
'path']),
'/');
262 if (isset($urlParts[
'query'])) {
263 $redirectUrl .=
'?' . $urlParts[
'query'];
268 if ($redirectUrl !==
'') {
269 $this->
ctrl->redirectToURL($redirectUrl);
273 $this->
ctrl->returnToParent($this);
static getInstanceByGlobalUser(?ilObjUser $user=null)
Class ilBuddySystemException.
transitionCommand(string $cmd, string $positiveFeedbackLanguageId, ?callable $onBeforeExecute=null)
isRequestParameterGiven(string $key, int $type)
readonly ilGlobalTemplateInterface $main_tpl
transitionAsyncCommand()
Performs a state transition based on the request action.
ilBuddySystemRelationStateFactory $stateFactory
const int BS_REQUEST_HTTP_POST
const int BS_REQUEST_HTTP_GET
static initializeFrontend(ilGlobalTemplateInterface $page)
static bool $isFrontendInitialized
Class ilBuddySystemRelationStateAlreadyGivenException.
Class ilBuddySystemRelationStateFactory.
static getInstance(?ilLanguage $lng=null)
Class ilBuddySystemRelationStateTransitionException.
Class ilBuddySystemRelation.
Base class for ILIAS Exception handling.
static _isAnonymous(int $usr_id)
static _lookupPref(int $a_usr_id, string $a_keyword)
static _lookupLogin(int $a_user_id)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
static yn2tf(string $a_yn)
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.
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
if(!file_exists('../ilias.ini.php'))