ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilBuddySystemGUI.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
3/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
4
6
13{
16
18 protected static $isFrontendInitialized = false;
19
21 protected $ctrl;
22
24 protected $buddyList;
25
27 protected $stateFactory;
28
30 protected $user;
31
33 protected $lng;
34
36 protected $http;
37
41 public function __construct()
42 {
43 global $DIC;
44
45 $this->http = $DIC->http();
46 $this->ctrl = $DIC['ilCtrl'];
47 $this->user = $DIC['ilUser'];
48 $this->lng = $DIC['lng'];
49
50 $this->buddyList = ilBuddyList::getInstanceByGlobalUser();
52
53 $this->lng->loadLanguageModule('buddysystem');
54 }
55
59 public static function initializeFrontend(ilGlobalTemplateInterface $page) : void
60 {
61 global $DIC;
62
63 if (
64 ilBuddySystem::getInstance()->isEnabled() &&
65 !$DIC->user()->isAnonymous() &&
66 !self::$isFrontendInitialized
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',
75 'ilBuddySystemGUI'
76 ], '', '', true, false);
77 $config->transition_state_cmd = 'transitionAsync';
78 $page->addOnLoadCode("il.BuddySystem.setConfig(" . json_encode($config) . ");");
79
80 $btn_config = new stdClass();
81 $btn_config->bnt_class = 'ilBuddySystemLinkWidget';
82
83 $page->addOnLoadCode("il.BuddySystemButton.setConfig(" . json_encode($btn_config) . ");");
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) {
119 return isset($_POST[$key]) && strlen($_POST[$key]);
120 break;
121
123 default:
124 return isset($_GET[$key]) && strlen($_GET[$key]);
125 break;
126 }
127 }
128
132 private function requestCommand() : void
133 {
134 $this->transitionCommand('request', 'buddy_relation_requested', function (ilBuddySystemRelation $relation) {
135 if (
136 $relation->isUnlinked() &&
137 !ilUtil::yn2tf(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
147 private function ignoreCommand() : void
148 {
149 $this->transitionCommand('ignore', 'buddy_request_ignored');
150 }
151
155 private function linkCommand() : void
156 {
157 $this->transitionCommand('link', 'buddy_request_approved');
158 }
159
165 private function transitionCommand(
166 string $cmd,
167 string $positiveFeedbackLanguageId,
168 callable $onBeforeExecute = null
169 ) : void {
170 if (!$this->isRequestParameterGiven('user_id', self::BS_REQUEST_HTTP_GET)) {
171 ilUtil::sendInfo($this->lng->txt('buddy_bs_action_not_possible'), true);
172 $this->ctrl->returnToParent($this);
173 }
174
175 try {
176 $relation = ilBuddyList::getInstanceByGlobalUser()->getRelationByUserId((int) $_GET['user_id']);
177
178 if (null !== $onBeforeExecute) {
179 $onBeforeExecute($relation);
180 }
181
182 ilBuddyList::getInstanceByGlobalUser()->{$cmd}($relation);
183 ilUtil::sendSuccess($this->lng->txt($positiveFeedbackLanguageId), true);
185 ilUtil::sendInfo(sprintf(
186 $this->lng->txt($e->getMessage()),
187 ilObjUser::_lookupLogin((int) $_GET['user_id'])
188 ), true);
190 ilUtil::sendInfo(sprintf(
191 $this->lng->txt($e->getMessage()),
192 ilObjUser::_lookupLogin((int) $_GET['user_id'])
193 ), true);
194 } catch (ilException $e) {
195 ilUtil::sendInfo($this->lng->txt('buddy_bs_action_not_possible'), true);
196 }
197
198 $this->redirectToReferer();
199 }
200
204 private function transitionAsyncCommand() : void
205 {
206 if (!$this->ctrl->isAsynch()) {
207 throw new RuntimeException('This action only supports AJAX http requests');
208 }
209
210 if (!isset($_POST['usr_id']) || !is_numeric($_POST['usr_id'])) {
211 throw new RuntimeException('Missing "usr_id" parameter');
212 }
213
214 if (!isset($_POST['action']) || !strlen($_POST['action'])) {
215 throw new RuntimeException('Missing "action" parameter');
216 }
217
218 $response = new stdClass();
219 $response->success = false;
220
221 try {
222 $usr_id = (int) $_POST['usr_id'];
223 $action = ilUtil::stripSlashes($_POST['action']);
224
225 if (ilObjUser::_isAnonymous($usr_id)) {
226 throw new ilBuddySystemException(sprintf(
227 "You cannot perform a state transition for the anonymous user (id: %s)",
228 $usr_id
229 ));
230 }
231
232 if (!strlen(ilObjUser::_lookupLogin($usr_id))) {
233 throw new ilBuddySystemException(sprintf(
234 "You cannot perform a state transition for a non existing user (id: %s)",
235 $usr_id
236 ));
237 }
238
239 $relation = $this->buddyList->getRelationByUserId($usr_id);
240
241 // The ILIAS JF decided to add a new personal setting
242 if (
243 $relation->isUnlinked() &&
244 !ilUtil::yn2tf(ilObjUser::_lookupPref($relation->getBuddyUsrId(), 'bs_allow_to_contact_me'))
245 ) {
246 throw new ilException("The requested user does not want to get contact requests");
247 }
248
249 try {
250 $this->buddyList->{$action}($relation);
251 $response->success = true;
253 $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int) $usr_id));
255 $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int) $usr_id));
256 } catch (Exception $e) {
257 $response->message = $this->lng->txt('buddy_bs_action_not_possible');
258 }
259
260 $response->state = get_class($relation->getState());
261 $response->state_html = $this->stateFactory->getStateButtonRendererByOwnerAndRelation(
262 $this->buddyList->getOwnerId(),
263 $relation
264 )->getHtml();
265 } catch (Exception $e) {
266 $response->message = $this->lng->txt('buddy_bs_action_not_possible');
267 }
268
269 echo json_encode($response);
270 exit();
271 }
272
273 private function redirectToReferer() : void
274 {
275 if (isset($this->http->request()->getServerParams()['HTTP_REFERER'])) {
276 $redirectUrl = $this->http->request()->getServerParams()['HTTP_REFERER'];
277 $urlParts = parse_url($redirectUrl);
278
279 if (isset($urlParts['path'])) {
280 $script = basename($urlParts['path'], '.php');
281 if ($script === 'login') {
282 $this->ctrl->returnToParent($this);
283 } else {
284 $redirectUrl = ltrim(basename($urlParts['path']), '/');
285 if (isset($urlParts['query'])) {
286 $redirectUrl .= '?' . $urlParts['query'];
287 }
288 }
289 }
290
291 if ($redirectUrl !== '') {
292 $this->ctrl->redirectToURL($redirectUrl);
293 }
294 }
295
296 $this->ctrl->returnToParent($this);
297 }
298}
user()
Definition: user.php:4
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
Provides an interface to the ILIAS HTTP services.
static getInstanceByGlobalUser()
Class ilBuddySystemException.
Class ilBuddySystemGUI.
transitionCommand(string $cmd, string $positiveFeedbackLanguageId, callable $onBeforeExecute=null)
isRequestParameterGiven(string $key, int $type)
transitionAsyncCommand()
Performs a state transition based on the request action.
__construct()
ilBuddySystemGUI constructor.
static initializeFrontend(ilGlobalTemplateInterface $page)
Class ilBuddySystemRelation.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupPref($a_usr_id, $a_keyword)
static _lookupLogin($a_user_id)
lookup login
static _isAnonymous($usr_id)
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static yn2tf($a_yn)
convert "y"/"n" to true/false
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
if(!file_exists(getcwd() . '/ilias.ini.php'))
registration confirmation script for ilias
Definition: confirmReg.php:12
global $DIC
Definition: goto.php:24
addOnLoadCode($a_code, $a_batch=2)
Add on load code.
exit
Definition: login.php:29
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
static http()
Fetches the global http state from ILIAS.
$type
$response