Lock.php
[Pman.Core] / Lock.php
1 <?php
2
3
4
5 /**
6  * 
7  * Locking theory
8  * 
9  * 
10  * This page is locked by XXXXXXX.. 
11  * Do you to prevent them saving and lock it yourself..
12  * 
13  * 
14  * -- interacts with Roo and _lock = id..
15  * 
16  * 
17  */
18
19 require_once 'Pman.php';
20
21 class Pman_Core_Lock extends Pman
22 {
23     
24     function getAuth()
25     {
26          $au = $this->getAuthUser();
27         if (!$au) {
28              $this->jerr("Not authenticated", array('authFailure' => true));
29         }
30         $this->authUser = $au;
31         // check that it's a supplier!!!! 
32         
33         return true; 
34     }
35     
36     function get($action)
37     {
38         
39         // default action is to attempt to lock..
40         $action = empty($action) ? 'lock' : 'unlock';
41         $this->$action($curlock);
42         
43        
44         
45         
46     }
47     
48     function unlock($curlock)
49     {
50     
51         if (empty($_REQUEST['id'])) {
52             $this->jerr("No lock id");
53         }
54         $curlock = DB_DataObject::factory('Core_locking');
55         if (!$curlock->get($_REQUEST['id'])) {
56             $this->jerr("No lock exists");
57         }
58         
59         if ($curlock->person_id != $this->authUser->id) {
60             $this->jerr("Lock id is invalid");
61         }
62         
63         $curlock->delete();
64         
65         $this->jok('unlocked');
66     }
67     function lock()
68     {
69         
70         if (empty($_REQUEST['on_id']) || empty($_REQUEST['on_table'])) {
71             $this->jerr("Missing table or id");
72         }
73        
74         $tab = str_replace('/', '',$_REQUEST['on_table']); // basic protection??
75         $x = DB_DataObject::factory($tab);
76         if (!$x->get($_REQUEST['on_id'])) {
77             $this->jerr("Item does not exist");
78         }
79         // is there a current lock on the item..
80         
81         $curlock = DB_DataObject::factory('Core_locking');
82         $curlock->setFrom(array(
83             'on_id' => $_REQUEST['on_id'],
84             'on_table' => $_REQUEST['on_table']
85         ));
86         if ($curlock->count()) {
87             $err  = $this->canUnlock();
88             if ($err !== true) {
89                 $this->jerr($err);
90             }
91         }
92         // make a lock..
93         
94         $curlock = DB_DataObject::factory('Core_locking');
95         $curlock->setFrom(array(
96             'on_id' => $_REQUEST['on_id'],
97             'on_table' => $_REQUEST['on_table'],
98             'created' => date('Y-m-d H:i:s'),
99             'person_id' => $this->authUser->id,
100         ));
101         $id = $curlock->insert();
102         $this->jok($id);
103         
104     }
105     
106     function canUnlock()
107     {
108         // the only scenario where we can automatically unlock is:::
109         
110         // this user owns the lock.
111         
112         $curlock = DB_DataObject::factory('Core_locking');
113         $curlock->setFrom(array(
114             'on_id' => $_REQUEST['on_id'],
115             'on_table' => $_REQUEST['on_table']
116         ));
117         $cc = clone($curlock);
118         // the user who owns the lock is not logged in.. ?? - their last 
119         $curlock->find();
120         $users = array();
121         while ($curlock->fetch()) {
122             $u = DB_DataObject::factory('Person');
123             $u->get($curlock->person_id);
124             if (!$u->isCurrentlyLoggedIn()) {
125                 $cc = clone($curlock);
126                 $cc->delete();
127                 continue;
128             }
129             $users[] = clone($u);
130             
131         }
132         if (empty($users)) {
133             return true;
134             
135         }
136         // situations
137         
138         //- the user is logged in, and we can clear it..
139         
140         //- the user is logged in multiple times, on different browser..
141         
142         //- the user is logged in multiple times on the same browser..
143         
144         
145         
146         // one of two error messages..
147         
148         $this->jerr("Item is Locked by " . $u->name . ' (' . $u->email . "),  Try asking them to log out");
149         
150         return true;
151         
152         
153         
154         
155         
156     }
157     
158         
159     
160 }