Sponsorenverwaltung - Team StarCraft e.V.
 All Data Structures Files Functions Variables
functions_locks.php
Go to the documentation of this file.
1 <?php
2  /**
3  * @file functions_locks.php
4  *
5  * @brief This file bundles most of the functions needed for mutual exclusion
6  * to prevent lost update problems.
7  *
8  * @details
9  * This file bundles most of the functions needed for mutual exclusive working on
10  * shared datasets to prevent lost update problems and similar problems.
11  *
12  * This file is linked with the other funcions via the inc/common.php.
13  *
14  * @copyright (c) 2013, Team StarCraft e.V.
15  * @version 1.0.0
16  * @author Daniel Seichter
17  * @date 02.07.2013
18  */
19 
20 
21  /**
22  * Gets the lock state of a table row
23  * @param string $tableName name of the table in the database
24  * @param string $whereColumn column of the table row to compare
25  * @param integer $whereValue required value in the column
26  * @return mixed
27  * - FALSE if the table row is not locked
28  * - user id if the table row is locked by the current user
29  * - TRUE if the table row is locked by another user
30  * @author Daniel Seichter
31  */
32  function getTableRowLock($tableName, $whereColumn, $whereValue) {
33  global $mysqli, $db, $loggedInUser;
34 
35  // fetch lockdata
36  $stmt = $mysqli->prepare('SELECT
37  lock_time,
38  lock_user_id
39  FROM '.$tableName.'
40  WHERE '.$whereColumn.' = ?');
41  $stmt->bind_param("i", $whereValue);
42  $stmt->execute();
43  $stmt->bind_result($lockTime, $lockUserId);
44 
45  $result = FALSE;
46  $curTime = time();
47  while ($stmt->fetch()) {
48  // check all results
49  if (($lockTime == 0) || $lockTime < $curTime) {
50  // not locked
51  continue;
52  }
53  if (($lockUserId == $loggedInUser->userId) && $lockTime > $curTime) {
54  // locked by the current user
55  $result = $lockUserId;
56  continue;
57  }
58  if ($lockTime > $curTime) {
59  // locked by another user
60  $result = TRUE;
61  break;
62  }
63  }
64  $stmt->close();
65  return $result;
66  }
67 
68  /**
69  * Unlocks a table row in the database by removing the lock time and the lock user id
70  * @param string $tableName name of the table in the database
71  * @param string $whereColumn column of the table row to compare
72  * @param integer $whereValue required value in the column
73  * @return boolean TRUE if the table row could be unlocked, else FALSE
74  * @author Daniel Seichter
75  */
76  function unlockTableRow($tableName, $whereColumn, $whereValue) {
77  global $mysqli, $loggedInUser;
78  // get lock state
79  $lockState = getTableRowLock($tableName,$whereColumn,$whereValue);
80 
81  if ($lockState === FALSE) {
82  // nothing to do (row is not locked)
83  return TRUE;
84  }
85  if ($lockState === $loggedInUser->userId) {
86  // table row is locked by the current user => unlock
87  $stmt = $mysqli->prepare('UPDATE '.$tableName.'
88  SET lock_time = -1 , lock_user_id = 0
89  WHERE '.$whereColumn.' = ?');
90  $stmt->bind_param("i", $whereValue);
91  $result = $stmt->execute();
92  $stmt->close();
93  return $result;
94  }
95  return FALSE;
96  }
97 
98  /**
99  * Locks a table row in the database by adding a lock time and the lock user id
100  * @param string $tableName name of the table in the database
101  * @param string $whereColumn column of the table row to compare
102  * @param integer $whereValue required value in the column
103  * @param integer $lockUntil until this time the entry is locked
104  * @return boolean FALSE if an error occured
105  * @author Daniel Seichter
106  */
107  function lockTableRow($tableName, $whereColumn, $whereValue, $lockUntil) {
108  global $mysqli, $db, $loggedInUser;
109  $stmt = $mysqli->prepare('UPDATE '.$tableName.'
110  SET lock_time = ? , lock_user_id = ?
111  WHERE '.$whereColumn.' = ?');
112  $stmt->bind_param("iii", $lockUntil, $loggedInUser->userId, $whereValue);
113  $result = $stmt->execute();
114  $stmt->close();
115  return $result;
116  }
117 
118  /**
119  * Gets the table name corresponding to the edit section (used in lock functions)
120  * @param integer $secId id of the edit section
121  * @return mixed
122  * - table name string corresponding to the edit section
123  * - FALSE if there is no table name for this section
124  * @author Daniel Seichter
125  */
126  function getTableNameForEditSection($secId) {
127  global $db;
128  switch ($secId) {
129  case 1:
130  // sponsor details, last contact details, hints
131  return $db['sponsor'];
132  case 2:
133  // contact person details
134  return $db['contact_person'];
135  case 3:
136  // production facilities
137  return $db['sp_production_facility'];
138  case 4:
139  // sponsor cars (lock only entry-point to the cars)
140  return $db['sponsor_car'];
141  default:
142  return FALSE;
143  }
144  }
145 
146  /**
147  * Checks if a sponsor edit section is locked
148  * @param integer $secId id of the edit section
149  * @param integer $sponsorId id of the current edited sponsor
150  * @return boolean TRUE if edit section is locked, else FALSE
151  * @author Daniel Seichter
152  */
153  function isSponsorEditSectionLocked($secId, $sponsorId) {
154  global $loggedInUser;
155  // get table name
156  $tableName = getTableNameForEditSection($secId);
157  if ($tableName !== FALSE) {
158  // get lock information
159  $result = getTableRowLock($tableName, 'sponsor_id', $sponsorId);
160  if ($result === FALSE || $result === $loggedInUser->userId) {
161  // not locked or locked by the user himself
162  return FALSE;
163  }
164  else {
165  // locked
166  return TRUE;
167  }
168  }
169  return TRUE;
170  }
171 
172  /**
173  * Locks a sponsor edit section
174  * @param integer $secId id of the edit section
175  * @param integer $sponsorId id of the current edited sponsor
176  * @return boolean FALSE if an error occured
177  * @author Daniel Seichter
178  */
179  function lockSponsorEditSection($secId, $sponsorId) {
180  // get table name
181  $tableName = getTableNameForEditSection($secId);
182  // calc the time the lock expires
183  $lockUntil = time() + EDIT_SECTION_LOCK_DURATION;
184  if ($tableName !== FALSE) {
185  // lock edit section
186  return lockTableRow($tableName, 'sponsor_id', $sponsorId, $lockUntil);
187  }
188  return FALSE;
189  }
190 
191  /**
192  * Unlocks a sponsor edit section
193  * @param integer $secId id of the edit section
194  * @param integer $sponsorId id of the current edited sponsor
195  * @return boolean FALSE if an error occured
196  * @author Daniel Seichter
197  */
198  function unlockSponsorEditSection($secId, $sponsorId) {
199  // get table name
200  $tableName = getTableNameForEditSection($secId);
201  if ($tableName !== FALSE) {
202  // unlock edit section
203  return unlockTableRow($tableName,'sponsor_id', $sponsorId);
204  }
205  return FALSE;
206  }
207 
208  /**
209  * Renews the the lock time of a sponsor edit section
210  * @param integer $secId id of the edit section
211  * @param integer $sponsorId id of the current edited sponsor
212  * @return boolean FALSE if an error occured
213  * @author Daniel Seichter
214  */
215  function renewSponsorEditSectionLock($secId, $sponsorId) {
216  global $loggedInUser;
217  // get table name
218  $tableName = getTableNameForEditSection($secId);
219  if ($tableName !== FALSE) {
220  $result = getTableRowLock($tableName, 'sponsor_id', $sponsorId);
221  if ($result === $loggedInUser->userId) {
222  // if locked by the current user renew lock time
223  // calc the time the lock expires
224  $lockUntil = time() + EDIT_SECTION_LOCK_DURATION;
225  return lockTableRow($tableName, 'sponsor_id', $sponsorId, $lockUntil);
226  }
227  }
228  }
229 
230 
231  /**
232  * Checks if the production facility is locked (dropdown fields)
233  * @param integer $id of the production facility
234  * @return boolean TRUE if the production facility is locked, else FALSE
235  * @author Daniel Seichter
236  */
238  global $db, $loggedInUser;
239  // get lock information
240  $result = getTableRowLock($db['production_facility'], 'production_facility_id', $id);
241  if ($result === FALSE || $result === $loggedInUser->userId) {
242  return FALSE;
243  }
244  return TRUE;
245  }
246 
247  /**
248  * Locks the production facility (dropdown fields)
249  * @param integer $id of the production facility
250  * @return boolean FALSE if an error occured
251  * @author Daniel Seichter
252  */
253  function lockProductionFacility($id) {
254  global $db;
255  // calc the time the lock expires
256  $lockUntil = time() + EDIT_SECTION_LOCK_DURATION;
257  return lockTableRow($db['production_facility'], 'production_facility_id', $id, $lockUntil);
258  }
259 
260  /**
261  * Unlocks the production facility (dropdown fields)
262  * @param integer $id of the production facility
263  * @return boolean FALSE if an error occured
264  * @author Daniel Seichter
265  */
266  function unlockProductionFacility($id) {
267  global $db;
268  return unlockTableRow($db['production_facility'], 'production_facility_id', $id);
269  }
270 
271  /**
272  * Renews the the lock time of the production facility (dropdown fields)
273  * @param integer $id of the production facility
274  * @return boolean FALSE if an error occured
275  * @author Daniel Seichter
276  */
278  global $db, $loggedInUser;
279  $result = getTableRowLock($db['production_facility'], 'production_facility_id', $id);
280  if ($result === $loggedInUser->userId) {
281  return lockProductionFacility($id);
282  }
283  }
284 
285  /**
286  * Checks if the status is locked (dropdown fields)
287  * @param integer $id of the status
288  * @return boolean TRUE if the status is locked, else FALSE
289  * @author Daniel Seichter
290  */
291  function isStatusLocked($id) {
292  global $db, $loggedInUser;
293  // get lock information
294  $result = getTableRowLock($db['status'], 'status_id', $id);
295  if ($result === FALSE || $result === $loggedInUser->userId) {
296  return FALSE;
297  }
298  return TRUE;
299  }
300 
301  /**
302  * Locks the status (dropdown fields)
303  * @param integer $id of the status
304  * @return boolean FALSE if an error occured
305  * @author Daniel Seichter
306  */
307  function lockStatus($id) {
308  global $db;
309  // calc the time the lock expires
310  $lockUntil = time() + EDIT_SECTION_LOCK_DURATION;
311  return lockTableRow($db['status'], 'status_id', $id, $lockUntil);
312  }
313 
314  /**
315  * Unlock the status (dropdown fields)
316  * @param integer $id of the status
317  * @return boolean FALSE if an error occured
318  * @author Daniel Seichter
319  */
320  function unlockStatus($id) {
321  global $db;
322  return unlockTableRow($db['status'], 'status_id', $id);
323  }
324 
325  /**
326  * Renews the the lock time of the status (dropdown fields)
327  * @param integer $id of the status
328  * @return boolean FALSE if an error occured
329  * @author Daniel Seichter
330  */
331  function renewStatusLock($id) {
332  global $db, $loggedInUser;
333  $result = getTableRowLock($db['status'], 'status_id', $id);
334  if ($result === $loggedInUser->userId) {
335  return lockStatus($id);
336  }
337  }
338 
339  /**
340  * Checks if the category is locked (dropdown fields)
341  * @param integer $id of the status
342  * @return boolean TRUE if the category is locked, else FALSE
343  * @author Daniel Seichter
344  */
345  function isCategoryLocked($id) {
346  global $db, $loggedInUser;
347  // get lock information
348  $result = getTableRowLock($db['category'], 'category_id', $id);
349  if ($result === FALSE || $result === $loggedInUser->userId) {
350  return FALSE;
351  }
352  return TRUE;
353  }
354 
355  /**
356  * Locks the category (dropdown fields)
357  * @param integer $id of the category
358  * @return boolean FALSE if an error occured
359  * @author Daniel Seichter
360  */
361  function lockCategory($id) {
362  global $db;
363  // calc the time the lock expires
364  $lockUntil = time() + EDIT_SECTION_LOCK_DURATION;
365  return lockTableRow($db['category'], 'category_id', $id, $lockUntil);
366  }
367 
368  /**
369  * Unlock the category (dropdown fields)
370  * @param integer $id of the category
371  * @return boolean FALSE if an error occured
372  * @author Daniel Seichter
373  */
374  function unlockCategory($id) {
375  global $db;
376  return unlockTableRow($db['category'], 'category_id', $id);
377  }
378 
379  /**
380  * Renews the the lock time of the category (dropdown fields)
381  * @param integer $id of the category
382  * @return boolean FALSE if an error occured
383  * @author Daniel Seichter
384  */
385  function renewCategoryLock($id) {
386  global $db, $loggedInUser;
387  $result = getTableRowLock($db['category'], 'category_id', $id);
388  if ($result === $loggedInUser->userId) {
389  return lockCategory($id);
390  }
391  }
392 
393  /**
394  * Checks if the car is locked (dropdown fields)
395  * @param integer $id of the status
396  * @return boolean TRUE if the car is locked, else FALSE
397  * @author Daniel Seichter
398  */
399  function isCarLocked($id) {
400  global $db, $loggedInUser;
401  // get lock information
402  $result = getTableRowLock($db['car'], 'car_id', $id);
403  if ($result === FALSE || $result === $loggedInUser->userId) {
404  return FALSE;
405  }
406  return TRUE;
407  }
408 
409  /**
410  * Locks the car (dropdown fields)
411  * @param integer $id of the car
412  * @return boolean FALSE if an error occured
413  * @author Daniel Seichter
414  */
415  function lockCar($id) {
416  global $db;
417  // calc the time the lock expires
418  $lockUntil = time() + EDIT_SECTION_LOCK_DURATION;
419  return lockTableRow($db['car'], 'car_id', $id, $lockUntil);
420  }
421 
422  /**
423  * Unlocks the car (dropdown fields)
424  * @param integer $id of the car
425  * @return boolean FALSE if an error occured
426  * @author Daniel Seichter
427  */
428  function unlockCar($id) {
429  global $db;
430  return unlockTableRow($db['car'], 'car_id', $id);
431  }
432 
433  /**
434  * Renews the the lock time of the car (dropdown fields)
435  * @param integer $id of the car
436  * @return boolean FALSE if an error occured
437  * @author Daniel Seichter
438  */
439  function renewCarLock($id) {
440  global $db, $loggedInUser;
441  $result = getTableRowLock($db['car'], 'car_id', $id);
442  if ($result === $loggedInUser->userId) {
443  return lockCar($id);
444  }
445  }
446 ?>