ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilBuddySystemGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/JSON/classes/class.ilJsonUtil.php';
5require_once 'Services/Contact/BuddySystem/exceptions/class.ilBuddySystemException.php';
6
13{
16
20 protected static $frontend_initialized = false;
21
25 protected $ctrl;
26
30 protected $buddylist;
31
35 protected $statefactory;
36
40 protected $user;
41
45 protected $lng;
46
50 public function __construct()
51 {
52 global $DIC;
53
54 $this->ctrl = $DIC['ilCtrl'];
55 $this->user = $DIC['ilUser'];
56 $this->lng = $DIC['lng'];
57
58 require_once 'Services/Contact/BuddySystem/classes/class.ilBuddyList.php';
59 require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemRelationStateFactory.php';
60 $this->buddylist = ilBuddyList::getInstanceByGlobalUser();
62
63 $this->lng->loadLanguageModule('buddysystem');
64 }
65
69 public static function initializeFrontend()
70 {
71 global $DIC;
72
73 if(!self::$frontend_initialized)
74 {
75 $DIC->language()->loadLanguageModule('buddysystem');
76
77 require_once 'Services/JSON/classes/class.ilJsonUtil.php';
78
79 $DIC['tpl']->addJavascript('./Services/Contact/BuddySystem/js/buddy_system.js');
80
81 $config = new stdClass();
82 $config->http_post_url = $DIC->ctrl()->getFormActionByClass(array('ilUIPluginRouterGUI', 'ilBuddySystemGUI'), '', '', true, false);
83 $config->transition_state_cmd = 'transitionAsync';
84 $DIC['tpl']->addOnLoadCode("il.BuddySystem.setConfig(".ilJsonUtil::encode($config).");");
85
86 $btn_config = new stdClass();
87 $btn_config->bnt_class = 'ilBuddySystemLinkWidget';
88
89 $DIC['tpl']->addOnLoadCode("il.BuddySystemButton.setConfig(".ilJsonUtil::encode($btn_config).");");
90 $DIC['tpl']->addOnLoadCode("il.BuddySystemButton.init();");
91
92 self::$frontend_initialized = true;
93 }
94 }
95
99 public function executeCommand()
100 {
101 if($this->user->isAnonymous())
102 {
103 throw new RuntimeException('This controller only accepts requests of logged in users');
104 }
105
106 $next_class = $this->ctrl->getNextClass($this);
107 $cmd = $this->ctrl->getCmd();
108
109 switch($next_class)
110 {
111 default:
112 $cmd .= 'Command';
113 $this->$cmd();
114 break;
115 }
116 }
117
123 protected function isRequestParameterGiven($key, $type)
124 {
125 switch($type)
126 {
128 return isset($_POST[$key]) && strlen($_POST[$key]);
129 break;
130
132 default:
133 return isset($_GET[$key]) && strlen($_GET[$key]);
134 break;
135 }
136 }
137
141 private function requestCommand()
142 {
143 $this->transitionCommand('request', 'buddy_relation_requested', function(ilBuddySystemRelation $relation) {
144 if($relation->isUnlinked() && !ilUtil::yn2tf(ilObjUser::_lookupPref($relation->getBuddyUserId(), 'bs_allow_to_contact_me')))
145 {
146 throw new ilException("The requested user does not want to get contact requests");
147 }
148 });
149 }
150
154 private function ignoreCommand()
155 {
156 $this->transitionCommand('ignore', 'buddy_request_ignored');
157 }
158
162 private function linkCommand()
163 {
164 $this->transitionCommand('link', 'buddy_request_approved');
165 }
166
172 private function transitionCommand($cmd, $positive_feedback_lng_id, callable $onBeforeExecute = null)
173 {
174 if(!$this->isRequestParameterGiven('user_id', self::BS_REQUEST_HTTP_GET))
175 {
176 ilUtil::sendInfo($this->lng->txt('buddy_bs_action_not_possible'), true);
177 $this->ctrl->returnToParent($this);
178 }
179
180 try
181 {
182 require_once 'Services/Contact/BuddySystem/classes/class.ilBuddyList.php';
183 $relation = ilBuddyList::getInstanceByGlobalUser()->getRelationByUserId((int)$_GET['user_id']);
184
185 if(null !== $onBeforeExecute)
186 {
187 $onBeforeExecute($relation);
188 }
189
190 ilBuddyList::getInstanceByGlobalUser()->$cmd($relation);
191 ilUtil::sendSuccess($this->lng->txt($positive_feedback_lng_id), true);
192
193 }
195 {
196 ilUtil::sendInfo(sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$_GET['user_id'])), true);
197 }
199 {
200 ilUtil::sendInfo(sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$_GET['user_id'])), true);
201 }
202 catch(ilException $e)
203 {
204 ilUtil::sendInfo($this->lng->txt('buddy_bs_action_not_possible'), true);
205 }
206
207 $this->ctrl->returnToParent($this);
208 }
209
213 private function transitionAsyncCommand()
214 {
215 if(!$this->ctrl->isAsynch())
216 {
217 throw new RuntimeException('This action only supports AJAX http requests');
218 }
219
220 if(!isset($_POST['usr_id']) || !is_numeric($_POST['usr_id']))
221 {
222 throw new RuntimeException('Missing "usr_id" parameter');
223 }
224
225 if(!isset($_POST['action']) || !strlen($_POST['action']))
226 {
227 throw new RuntimeException('Missing "action" parameter');
228 }
229
230 $response = new stdClass();
231 $response->success = false;
232
233 try
234 {
235 $usr_id = (int)$_POST['usr_id'];
236 $action = ilUtil::stripSlashes($_POST['action']);
237
238 if(ilObjUser::_isAnonymous($usr_id))
239 {
240 throw new ilBuddySystemException(sprintf("You cannot perform a state transition for the anonymous user (id: %s)", $usr_id));
241 }
242
243 if(!strlen(ilObjUser::_lookupLogin($usr_id)))
244 {
245 throw new ilBuddySystemException(sprintf("You cannot perform a state transition for a non existing user (id: %s)", $usr_id));
246 }
247
248 $relation = $this->buddylist->getRelationByUserId($usr_id);
249
250 // The ILIAS JF decided to add a new personal setting
251 if($relation->isUnlinked() && !ilUtil::yn2tf(ilObjUser::_lookupPref($relation->getBuddyUserId(), 'bs_allow_to_contact_me')))
252 {
253 throw new ilException("The requested user does not want to get contact requests");
254 }
255
256 try
257 {
258 $this->buddylist->{$action}($relation);
259 $response->success = true;
260 }
262 {
263 $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$usr_id));
264 }
266 {
267 $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$usr_id));
268 }
269 catch(Exception $e)
270 {
271 $response->message = $this->lng->txt('buddy_bs_action_not_possible');
272 }
273
274 $response->state = get_class($relation->getState());
275 $response->state_html = $this->statefactory->getRendererByOwnerAndRelation($this->buddylist->getOwnerId(), $relation)->getHtml();
276 }
277 catch(Exception $e)
278 {
279 $response->message = $this->lng->txt('buddy_bs_action_not_possible');
280 }
281
282 echo json_encode($response);
283 exit();
284 }
285}
sprintf('%.4f', $callTime)
user()
Definition: user.php:4
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
static getInstanceByGlobalUser()
Class ilBuddySystemGUI.
transitionCommand($cmd, $positive_feedback_lng_id, callable $onBeforeExecute=null)
transitionAsyncCommand()
Performs a state transition based on the request action.
isRequestParameterGiven($key, $type)
Class ilBuddySystemRelation.
Base class for ILIAS Exception handling.
static encode($mixed, $suppress_native=false)
static _lookupPref($a_usr_id, $a_keyword)
static _lookupLogin($a_user_id)
lookup login
static _isAnonymous($usr_id)
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
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.
$cmd
Definition: sahs_server.php:35
global $DIC