Pman.Gnumeric.js
[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  * 
15  * 
16  * 
17  * -- interacts with Roo and _lock = id..
18  * 
19  * 
20  * call : 
21  * try and lock it..
22  * baseURL + /Core/Lock/lock?on_id=...&on_table=...
23  * - returns id or an array of who has the locks.
24  * 
25  * Force an unlock after a warning..
26  * baseURL + /Core/Lock/lock?on_id=...&on_table=...&force=1
27  * - returns id..
28  * 
29  * Unlock - call when window is closed..
30  * baseURL + /Core/Lock/unlock?on_id=...&on_table=...&force=1
31  * - returns jerr or jok
32  */
33
34 require_once 'Pman.php';
35
36 class Pman_Core_Lock extends Pman
37 {
38     
39     function getAuth()
40     {
41          $au = $this->getAuthUser();
42         if (!$au) {
43              $this->jerr("Not authenticated", array('authFailure' => true));
44         }
45         $this->authUser = $au;
46         // check that it's a supplier!!!! 
47         
48         return true; 
49     }
50     
51     function get($action, $opts=array())
52     {
53         // should we allow url links to lock things???
54         // only for debugging??
55         $this->post($action);
56         // 
57         $this->jerr("invalid request");
58     }
59     
60     function post($action)
61     {
62         
63         // default action is to attempt to lock..
64         $action = empty($action) || $action == 'lock' ? 'lock' : 'unlock';
65         $this->$action();
66         
67     }
68     
69     function unlock()
70     {
71     
72         if (empty($_REQUEST['id'])) {
73             $this->jerr("No lock id");
74         }
75         $curlock = DB_DataObject::factory('core_locking');
76         if (!$curlock->get($_REQUEST['id'])) {
77             $this->jok("No lock exists"); // been deleted before.. probably ok..
78         }
79         
80         if ($curlock->person_id != $this->authUser->id) {
81             // this is an error conditon..
82             $this->jerr("Lock id is invalid");
83         }
84         
85         $curlock->delete();
86         
87         $this->jok('unlocked');
88     }
89     function lock()
90     {
91         
92         if (empty($_REQUEST['on_id']) || empty($_REQUEST['on_table'])) {
93             $this->jerr("Missing table or id");
94         }
95        
96         $tab = str_replace('/', '', strtolower($_REQUEST['on_table'])); // basic protection??
97         $x = DB_DataObject::factory($tab);
98         if (!$x->get($_REQUEST['on_id'])) {
99             $this->jerr("Item does not exist");
100         }
101         // is there a current lock on the item..
102         
103         $curlock = DB_DataObject::factory('core_locking');
104         $curlock->setFrom(array(
105             'on_id' => $_REQUEST['on_id'],
106             'on_table' => strtolower($_REQUEST['on_table'])
107         ));
108         
109         // remove old locks..
110         $llc = clone($curlock);
111         $exp = date('Y-m-d', strtotime('NOW - 1 WEEK'));
112         $llc->whereAdd("created < '$exp'");
113         if ($llc->count()) {
114             $llc->find();
115             while($llc->fetch()) {
116                 $llcd = clone($llc);
117                 $llcd->delete();
118            
119             }
120         }
121         
122         
123         $curlock_ex = clone($curlock);
124         $curlock->person_id = $this->authUser->id;
125         
126         
127         $curlock_ex->whereAdd('person_id != '. $this->authUser->id);
128         $nlocks = $curlock_ex->count() ;
129         
130         $ret = false;
131         
132         if ($nlocks && empty($_REQUEST['force'])) {
133            // DB_DataObjecT::debugLevel(1);
134             $ar = $curlock_ex->fetchAll('person_id', 'created');
135             $p = DB_DataObject::factory('core_person');
136             $p->selectAdd();
137             $p->selectAdd('id,name,email');
138             
139             $p->whereAddIn('id', array_keys($ar), 'int');
140             $p->find();
141             $ret = array();
142             while ($p->fetch()) {
143                 $ret[$p->id] = $p->toArray();
144                 $ret[$p->id]['lock_created'] = $ar[$p->id];
145             }
146              $this->jok(array_values($ret));
147             
148         }
149         // trash the lock if it belongs to current user..
150         $ulocks = $curlock->count();
151         if ($ulocks) {
152             // trash all the locks..
153             $curlock = DB_DataObject::factory('core_locking');
154             $curlock->setFrom(array(
155                 'on_id' => $_REQUEST['on_id'],
156                 'on_table' => strtolower($_REQUEST['on_table']),
157                 'person_id' => $this->authUser->id
158             ));
159             
160             $curlock->find();
161             while($curlock->fetch()) {
162                 $cc =clone($curlock);
163                 $cc->delete();
164             }
165         }
166         if ($nlocks && !empty($_REQUEST['force'])) {
167             // user has decied to delete eveyone elses locks..
168             $curlock_ex->find();
169             while($curlock_ex->fetch()) {
170                 $cc =clone($curlock_ex);
171                 $cc->delete();
172             }
173         }
174         
175         
176         // make a lock..
177         
178         $curlock = DB_DataObject::factory('core_locking');
179         $curlock->setFrom(array(
180             'on_id' => $_REQUEST['on_id'],
181             'on_table' => strtolower($_REQUEST['on_table']),
182             'created' => date('Y-m-d H:i:s'),
183             'person_id' => $this->authUser->id,
184         ));
185         $id = $curlock->insert();
186         $this->jok( $id);
187         
188     }
189      
190     
191         
192     
193 }