ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
4 require_once 'Services/JSON/classes/class.ilJsonUtil.php';
5 require_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  protected $http;
51 
55  public function __construct()
56  {
57  global $DIC;
58 
59  $this->http = $DIC->http();
60  $this->ctrl = $DIC['ilCtrl'];
61  $this->user = $DIC['ilUser'];
62  $this->lng = $DIC['lng'];
63 
64  require_once 'Services/Contact/BuddySystem/classes/class.ilBuddyList.php';
65  require_once 'Services/Contact/BuddySystem/classes/states/class.ilBuddySystemRelationStateFactory.php';
66  $this->buddylist = ilBuddyList::getInstanceByGlobalUser();
67  $this->statefactory = ilBuddySystemRelationStateFactory::getInstance();
68 
69  $this->lng->loadLanguageModule('buddysystem');
70  }
71 
75  public static function initializeFrontend()
76  {
77  global $DIC;
78 
79  if (!self::$frontend_initialized) {
80  $DIC->language()->loadLanguageModule('buddysystem');
81 
82  require_once 'Services/JSON/classes/class.ilJsonUtil.php';
83 
84  $DIC['tpl']->addJavascript('./Services/Contact/BuddySystem/js/buddy_system.js');
85 
86  $config = new stdClass();
87  $config->http_post_url = $DIC->ctrl()->getFormActionByClass(array('ilUIPluginRouterGUI', 'ilBuddySystemGUI'), '', '', true, false);
88  $config->transition_state_cmd = 'transitionAsync';
89  $DIC['tpl']->addOnLoadCode("il.BuddySystem.setConfig(" . ilJsonUtil::encode($config) . ");");
90 
91  $btn_config = new stdClass();
92  $btn_config->bnt_class = 'ilBuddySystemLinkWidget';
93 
94  $DIC['tpl']->addOnLoadCode("il.BuddySystemButton.setConfig(" . ilJsonUtil::encode($btn_config) . ");");
95  $DIC['tpl']->addOnLoadCode("il.BuddySystemButton.init();");
96 
97  self::$frontend_initialized = true;
98  }
99  }
100 
104  public function executeCommand()
105  {
106  if ($this->user->isAnonymous()) {
107  throw new RuntimeException('This controller only accepts requests of logged in users');
108  }
109 
110  $next_class = $this->ctrl->getNextClass($this);
111  $cmd = $this->ctrl->getCmd();
112 
113  switch ($next_class) {
114  default:
115  $cmd .= 'Command';
116  $this->$cmd();
117  break;
118  }
119  }
120 
126  protected function isRequestParameterGiven($key, $type)
127  {
128  switch ($type) {
129  case self::BS_REQUEST_HTTP_POST:
130  return isset($_POST[$key]) && strlen($_POST[$key]);
131  break;
132 
133  case self::BS_REQUEST_HTTP_GET:
134  default:
135  return isset($_GET[$key]) && strlen($_GET[$key]);
136  break;
137  }
138  }
139 
143  private function requestCommand()
144  {
145  $this->transitionCommand('request', 'buddy_relation_requested', function (ilBuddySystemRelation $relation) {
146  if ($relation->isUnlinked() && !ilUtil::yn2tf(ilObjUser::_lookupPref($relation->getBuddyUserId(), 'bs_allow_to_contact_me'))) {
147  throw new ilException("The requested user does not want to get contact requests");
148  }
149  });
150  }
151 
155  private function ignoreCommand()
156  {
157  $this->transitionCommand('ignore', 'buddy_request_ignored');
158  }
159 
163  private function linkCommand()
164  {
165  $this->transitionCommand('link', 'buddy_request_approved');
166  }
167 
173  private function transitionCommand($cmd, $positive_feedback_lng_id, callable $onBeforeExecute = null)
174  {
175  if (!$this->isRequestParameterGiven('user_id', self::BS_REQUEST_HTTP_GET)) {
176  ilUtil::sendInfo($this->lng->txt('buddy_bs_action_not_possible'), true);
177  $this->ctrl->returnToParent($this);
178  }
179 
180  try {
181  require_once 'Services/Contact/BuddySystem/classes/class.ilBuddyList.php';
182  $relation = ilBuddyList::getInstanceByGlobalUser()->getRelationByUserId((int) $_GET['user_id']);
183 
184  if (null !== $onBeforeExecute) {
185  $onBeforeExecute($relation);
186  }
187 
188  ilBuddyList::getInstanceByGlobalUser()->$cmd($relation);
189  ilUtil::sendSuccess($this->lng->txt($positive_feedback_lng_id), true);
191  ilUtil::sendInfo(sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int) $_GET['user_id'])), true);
193  ilUtil::sendInfo(sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int) $_GET['user_id'])), 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()
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("You cannot perform a state transition for the anonymous user (id: %s)", $usr_id));
227  }
228 
229  if (!strlen(ilObjUser::_lookupLogin($usr_id))) {
230  throw new ilBuddySystemException(sprintf("You cannot perform a state transition for a non existing user (id: %s)", $usr_id));
231  }
232 
233  $relation = $this->buddylist->getRelationByUserId($usr_id);
234 
235  // The ILIAS JF decided to add a new personal setting
236  if ($relation->isUnlinked() && !ilUtil::yn2tf(ilObjUser::_lookupPref($relation->getBuddyUserId(), 'bs_allow_to_contact_me'))) {
237  throw new ilException("The requested user does not want to get contact requests");
238  }
239 
240  try {
241  $this->buddylist->{$action}($relation);
242  $response->success = true;
244  $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int) $usr_id));
246  $response->message = sprintf($this->lng->txt($e->getMessage()), ilObjUser::_lookupLogin((int) $usr_id));
247  } catch (Exception $e) {
248  $response->message = $this->lng->txt('buddy_bs_action_not_possible');
249  }
250 
251  $response->state = get_class($relation->getState());
252  $response->state_html = $this->statefactory->getRendererByOwnerAndRelation($this->buddylist->getOwnerId(), $relation)->getHtml();
253  } catch (Exception $e) {
254  $response->message = $this->lng->txt('buddy_bs_action_not_possible');
255  }
256 
257  echo json_encode($response);
258  exit();
259  }
260 
261  private function redirectToReferer()
262  {
263  if (isset($this->http->request()->getServerParams()['HTTP_REFERER'])) {
264  $redirectUrl = $this->http->request()->getServerParams()['HTTP_REFERER'];
265  $urlParts = parse_url($redirectUrl);
266 
267  if (isset($urlParts['path'])) {
268  $script = basename($urlParts['path'], '.php');
269  if ($script === 'login') {
270  $this->ctrl->returnToParent($this);
271  } else {
272  $redirectUrl = ltrim(basename($urlParts['path']), '/');
273  if (isset($urlParts['query'])) {
274  $redirectUrl .= '?' . $urlParts['query'];
275  }
276  }
277  }
278  $this->ctrl->redirectToURL($redirectUrl);
279  }
280 
281  $this->ctrl->returnToParent($this);
282  }
283 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
static _lookupLogin($a_user_id)
lookup login
Class ilBuddySystemGUI.
static getInstanceByGlobalUser()
$action
$type
isRequestParameterGiven($key, $type)
global $DIC
Definition: saml.php:7
$_GET["client_id"]
user()
Definition: user.php:4
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
static http()
Fetches the global http state from ILIAS.
static encode($mixed, $suppress_native=false)
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
Create styles array
The data for the language used.
static _isAnonymous($usr_id)
transitionAsyncCommand()
Performs a state transition based on the request action.
Class ilBuddySystemRelation.
static _lookupPref($a_usr_id, $a_keyword)
transitionCommand($cmd, $positive_feedback_lng_id, callable $onBeforeExecute=null)
static yn2tf($a_yn)
convert "y"/"n" to true/false
$response
$key
Definition: croninfo.php:18
$_POST["username"]