ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 {
57 global $ilCtrl, $ilUser, $lng;
58
59 $this->ctrl = $ilCtrl;
60 $this->user = $ilUser;
61 $this->lng = $lng;
62
63 require_once 'Services/Contact/BuddySystem/classes/class.ilBuddyList.php';
64 require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemRelationStateFactory.php';
65 $this->buddylist = ilBuddyList::getInstanceByGlobalUser();
67
68 $this->lng->loadLanguageModule('buddysystem');
69 }
70
74 public static function initializeFrontend()
75 {
81 global $tpl, $ilCtrl, $lng;
82
83 if(!self::$frontend_initialized)
84 {
85 $lng->loadLanguageModule('buddysystem');
86
87 require_once 'Services/JSON/classes/class.ilJsonUtil.php';
88
89 $tpl->addJavascript('./Services/Contact/BuddySystem/js/buddy_system.js');
90
91 $config = new stdClass();
92 $config->http_post_url = $ilCtrl->getFormActionByClass(array('ilUIPluginRouterGUI', 'ilBuddySystemGUI'), '', '', true, false);
93 $config->transition_state_cmd = 'transitionAsync';
94 $tpl->addOnLoadCode("il.BuddySystem.setConfig(".ilJsonUtil::encode($config).");");
95
96 $btn_config = new stdClass();
97 $btn_config->bnt_class = 'ilBuddySystemLinkWidget';
98
99 $tpl->addOnLoadCode("il.BuddySystemButton.setConfig(".ilJsonUtil::encode($btn_config).");");
100 $tpl->addOnLoadCode("il.BuddySystemButton.init();");
101
102 self::$frontend_initialized = true;
103 }
104 }
105
109 public function executeCommand()
110 {
111 if($this->user->isAnonymous())
112 {
113 throw new RuntimeException('This controller only accepts requests of logged in users');
114 }
115
116 $next_class = $this->ctrl->getNextClass($this);
117 $cmd = $this->ctrl->getCmd();
118
119 switch($next_class)
120 {
121 default:
122 $cmd .= 'Command';
123 $this->$cmd();
124 break;
125 }
126 }
127
133 protected function isRequestParameterGiven($key, $type)
134 {
135 switch($type)
136 {
138 return isset($_POST[$key]) && strlen($_POST[$key]);
139 break;
140
142 default:
143 return isset($_GET[$key]) && strlen($_GET[$key]);
144 break;
145 }
146 }
147
151 private function requestCommand()
152 {
153 $this->transitionCommand('request', 'buddy_relation_requested', function(ilBuddySystemRelation $relation) {
154 if($relation->isUnlinked() && !ilUtil::yn2tf(ilObjUser::_lookupPref($relation->getBuddyUserId(), 'bs_allow_to_contact_me')))
155 {
156 throw new ilException("The requested user does not want to get contact requests");
157 }
158 });
159 }
160
164 private function ignoreCommand()
165 {
166 $this->transitionCommand('ignore', 'buddy_request_ignored');
167 }
168
172 private function linkCommand()
173 {
174 $this->transitionCommand('link', 'buddy_request_approved');
175 }
176
182 private function transitionCommand($cmd, $positive_feedback_lng_id, callable $onBeforeExecute = null)
183 {
184 if(!$this->isRequestParameterGiven('user_id', self::BS_REQUEST_HTTP_GET))
185 {
186 ilUtil::sendInfo($this->lng->txt('buddy_bs_action_not_possible'), true);
187 $this->ctrl->returnToParent($this);
188 }
189
190 try
191 {
192 require_once 'Services/Contact/BuddySystem/classes/class.ilBuddyList.php';
193 $relation = ilBuddyList::getInstanceByGlobalUser()->getRelationByUserId((int)$_GET['user_id']);
194
195 if(null !== $onBeforeExecute)
196 {
197 $onBeforeExecute($relation);
198 }
199
200 ilBuddyList::getInstanceByGlobalUser()->$cmd($relation);
201 ilUtil::sendSuccess($this->lng->txt($positive_feedback_lng_id), true);
202
203 }
205 {
206 ilUtil::sendInfo(sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$_GET['user_id'])), true);
207 }
209 {
210 ilUtil::sendInfo(sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$_GET['user_id'])), true);
211 }
212 catch(ilException $e)
213 {
214 ilUtil::sendInfo($this->lng->txt('buddy_bs_action_not_possible'), true);
215 }
216
217 $this->ctrl->returnToParent($this);
218 }
219
223 private function transitionAsyncCommand()
224 {
228 global $lng;
229
230 if(!$this->ctrl->isAsynch())
231 {
232 throw new RuntimeException('This action only supports AJAX http requests');
233 }
234
235 if(!isset($_POST['usr_id']) || !is_numeric($_POST['usr_id']))
236 {
237 throw new RuntimeException('Missing "usr_id" parameter');
238 }
239
240 if(!isset($_POST['action']) || !strlen($_POST['action']))
241 {
242 throw new RuntimeException('Missing "action" parameter');
243 }
244
245 $response = new stdClass();
246 $response->success = false;
247
248 try
249 {
250 $usr_id = (int)$_POST['usr_id'];
251 $action = ilUtil::stripSlashes($_POST['action']);
252
253 if(ilObjUser::_isAnonymous($usr_id))
254 {
255 throw new ilBuddySystemException(sprintf("You cannot perform a state transition for the anonymous user (id: %s)", $usr_id));
256 }
257
258 if(!strlen(ilObjUser::_lookupLogin($usr_id)))
259 {
260 throw new ilBuddySystemException(sprintf("You cannot perform a state transition for a non existing user (id: %s)", $usr_id));
261 }
262
263 $relation = $this->buddylist->getRelationByUserId($usr_id);
264
265 // The ILIAS JF decided to add a new personal setting
266 if($relation->isUnlinked() && !ilUtil::yn2tf(ilObjUser::_lookupPref($relation->getBuddyUserId(), 'bs_allow_to_contact_me')))
267 {
268 throw new ilException("The requested user does not want to get contact requests");
269 }
270
271 try
272 {
273 $this->buddylist->{$action}($relation);
274 $response->success = true;
275 }
277 {
278 $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$usr_id));
279 }
281 {
282 $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int)$usr_id));
283 }
284 catch(Exception $e)
285 {
286 $response->message = $lng->txt('buddy_bs_action_not_possible');
287 }
288
289 $response->state = get_class($relation->getState());
290 $response->state_html = $this->statefactory->getRendererByOwnerAndRelation($this->buddylist->getOwnerId(), $relation)->getHtml();
291 }
292 catch(Exception $e)
293 {
294 $response->message = $lng->txt('buddy_bs_action_not_possible');
295 }
296
297 echo json_encode($response);
298 exit();
299 }
300}
global $tpl
Definition: ilias.php:8
$_GET["client_id"]
Class ilBuddySystemGUI.
transitionCommand($cmd, $positive_feedback_lng_id, callable $onBeforeExecute=null)
isRequestParameterGiven($key, $type)
Class ilBuddySystemRelation.
Base class for ILIAS Exception handling.
static encode($mixed, $suppress_native=false)
static _lookupLogin($a_user_id)
lookup login
_lookupPref($a_usr_id, $a_keyword)
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.
$_POST['username']
Definition: cron.php:12
global $ilCtrl
Definition: ilias.php:18
exit
Definition: login.php:54
$cmd
Definition: sahs_server.php:35
global $ilUser
Definition: imgupload.php:15