ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilBuddySystemRelationRepository.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 
10 {
11  const TYPE_APPROVED = 'app';
12  const TYPE_REQUESTED = 'req';
13  const TYPE_IGNORED = 'ign';
14 
18  protected $db;
19 
23  protected $usr_id;
24 
28  public function __construct($usr_id)
29  {
30  global $DIC;
31 
32  $this->db = $DIC['ilDB'];
33  $this->usr_id = $usr_id;
34  }
35 
39  public function getDatabaseAdapter()
40  {
41  return $this->db;
42  }
43 
48  {
49  $this->db = $db;
50  }
51 
56  public function getAll()
57  {
58  $relations = array();
59 
60  $res = $this->db->queryF(
61  "
62  SELECT usr_id, buddy_usr_id, ts, %s rel_type FROM buddylist WHERE usr_id = %s
63  UNION
64  SELECT usr_id, buddy_usr_id, ts, (CASE WHEN ignored = 1 THEN %s ELSE %s END) rel_type FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s
65  ",
66  array(
67  'text', 'integer', 'text', 'text', 'integer', 'integer'
68  ),
69  array(
70  self::TYPE_APPROVED, $this->usr_id, self::TYPE_IGNORED, self::TYPE_REQUESTED, $this->usr_id, $this->usr_id
71  )
72  );
73 
74  while($row = $this->db->fetchAssoc($res))
75  {
76  $relation = $this->getRelationByDatabaseRecord($row);
77  $relation->setUserId($row['usr_id']);
78  $relation->setBuddyUserId($row['buddy_usr_id']);
79  $relation->setTimestamp($row['ts']);
80  $relation->setIsOwnedByRequest($relation->getUserId() == $this->usr_id);
81  $key = $this->usr_id == $relation->getUserId() ? $relation->getBuddyUserId() : $relation->getUserId();
82  $relations[$key] = $relation;
83  }
84 
85  return $relations;
86  }
87 
93  {
94  if(self::TYPE_APPROVED == $row['rel_type'])
95  {
97  return $relation;
98  }
99  else
100  {
101  if(self::TYPE_IGNORED == $row['rel_type'])
102  {
104  return $relation;
105  }
106  else
107  {
109  return $relation;
110  }
111  }
112  }
113 
117  public function destroy()
118  {
119  $this->db->queryF(
120  "DELETE FROM buddylist WHERE usr_id = %s OR buddy_usr_id = %s",
121  array('integer', 'integer'),
122  array($this->usr_id, $this->usr_id)
123  );
124 
125  $this->db->queryF(
126  "DELETE FROM buddylist_requests WHERE usr_id = %s OR buddy_usr_id = %s",
127  array('integer', 'integer'),
128  array($this->usr_id, $this->usr_id)
129  );
130  }
131 
135  private function addToApprovedBuddies(ilBuddySystemRelation $relation)
136  {
137  $this->db->replace(
138  'buddylist',
139  array(
140  'usr_id' => array('integer', $relation->getUserId()),
141  'buddy_usr_id' => array('integer', $relation->getBuddyUserId())
142  ),
143  array(
144  'ts' => array('integer', $relation->getTimestamp())
145  )
146  );
147 
148  $this->db->replace(
149  'buddylist',
150  array(
151  'usr_id' => array('integer', $relation->getBuddyUserId()),
152  'buddy_usr_id' => array('integer', $relation->getUserId())
153  ),
154  array(
155  'ts' => array('integer', $relation->getTimestamp())
156  )
157  );
158  }
159 
164  {
165  $this->db->manipulateF(
166  "DELETE FROM buddylist WHERE usr_id = %s AND buddy_usr_id = %s",
167  array('integer', 'integer'),
168  array($relation->getUserId(), $relation->getBuddyUserId())
169  );
170 
171  $this->db->manipulateF(
172  "DELETE FROM buddylist WHERE buddy_usr_id = %s AND usr_id = %s",
173  array('integer', 'integer'),
174  array($relation->getUserId(), $relation->getBuddyUserId())
175  );
176  }
177 
182  private function addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
183  {
184  $this->db->replace(
185  'buddylist_requests',
186  array(
187  'usr_id' => array('integer', $relation->getUserId()),
188  'buddy_usr_id' => array('integer', $relation->getBuddyUserId())
189  ),
190  array(
191  'ts' => array('integer', $relation->getTimestamp()),
192  'ignored' => array('integer', (int)$ignored)
193  )
194  );
195  }
196 
201  {
202  $this->db->manipulateF(
203  "DELETE FROM buddylist_requests WHERE usr_id = %s AND buddy_usr_id = %s",
204  array('integer', 'integer'),
205  array($relation->getUserId(), $relation->getBuddyUserId())
206  );
207 
208  $this->db->manipulateF(
209  "DELETE FROM buddylist_requests WHERE buddy_usr_id = %s AND usr_id = %s",
210  array('integer', 'integer'),
211  array($relation->getUserId(), $relation->getBuddyUserId())
212  );
213  }
214 
218  public function save(ilBuddySystemRelation $relation)
219  {
220  $ilAtomQuery = $this->db->buildAtomQuery();
221  $ilAtomQuery->addTableLock('buddylist_requests');
222  $ilAtomQuery->addTableLock('buddylist');
223 
224  $ilAtomQuery->addQueryCallable(function(ilDBInterface $ilDB) use ($relation) {
225  if($relation->isLinked())
226  {
227  $this->addToApprovedBuddies($relation);
228  }
229  else if($relation->wasLinked())
230  {
231  $this->removeFromApprovedBuddies($relation);
232  }
233 
234  if($relation->isRequested())
235  {
236  $this->addToRequestedBuddies($relation, false);
237  }
238  else if($relation->isIgnored())
239  {
240  $this->addToRequestedBuddies($relation, true);
241  }
242  else if($relation->wasRequested() || $relation->wasIgnored())
243  {
244  $this->removeFromRequestedBuddies($relation);
245  }
246  });
247 
248  $ilAtomQuery->run();
249  }
250 }
removeFromRequestedBuddies(ilBuddySystemRelation $relation)
Class ilBuddySystemRelationRepository.
Interface ilDBInterface.
removeFromApprovedBuddies(ilBuddySystemRelation $relation)
addToApprovedBuddies(ilBuddySystemRelation $relation)
Create styles array
The data for the language used.
Class ilBuddySystemRelation.
global $ilDB
addToRequestedBuddies(ilBuddySystemRelation $relation, $ignored)
global $DIC