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  * 
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()
52     {
53         // should we allow url links to lock things???
54         $this->jerr("invalid request");
55     }
56     
57     function post($action)
58     {
59         
60         // default action is to attempt to lock..
61         $action = empty($action) || $action == 'lock' ? 'lock' : 'unlock';
62         $this->$action();
63         
64     }
65     
66     function unlock()
67     {
68     
69         if (empty($_REQUEST['id'])) {
70             $this->jerr("No lock id");
71         }
72         $curlock = DB_DataObject::factory('Core_locking');
73         if (!$curlock->get($_REQUEST['id'])) {
74             $this->jerr("No lock exists");
75         }
76         
77         if ($curlock->person_id != $this->authUser->id) {
78             $this->jerr("Lock id is invalid");
79         }
80         
81         $curlock->delete();
82         
83         $this->jok('unlocked');
84     }
85     function lock()
86     {
87         
88         if (empty($_REQUEST['on_id']) || empty($_REQUEST['on_table'])) {
89             $this->jerr("Missing table or id");
90         }
91        
92         $tab = str_replace('/', '',$_REQUEST['on_table']); // basic protection??
93         $x = DB_DataObject::factory($tab);
94         if (!$x->get($_REQUEST['on_id'])) {
95             $this->jerr("Item does not exist");
96         }
97         // is there a current lock on the item..
98         
99         $curlock = DB_DataObject::factory('Core_locking');
100         $curlock->setFrom(array(
101             'on_id' => $_REQUEST['on_id'],
102             'on_table' => $_REQUEST['on_table']
103         ));
104         
105         
106         $nlocks = $curlock->count() ;
107         if ($nlocks && empty($_REQUEST['force'])) {
108             
109             $ar = $curlock->fetchAll('person_id', 'created');
110             $p = DB_DataObject::factory('Person');
111             $p->whereAddIn('id', $ar, 'int');
112             $p->find();
113             $ret = array();
114             while ($p->fetch()) {
115                 $ret[$p->id] = $p->toRooArray();
116                 $ret[$p->id]->lock_created = $ar[$p->id];
117             }
118             $this->jok(array_values($ret));
119             
120         }
121         if ($nlocks) {
122             // trash all the locks..
123             $curlock->find();
124             while($curlock->fetch()) {
125                 $cc =clone($curlock);
126                 $cc->delete();
127             }
128         }
129         
130         // make a lock..
131         
132         $curlock = DB_DataObject::factory('Core_locking');
133         $curlock->setFrom(array(
134             'on_id' => $_REQUEST['on_id'],
135             'on_table' => $_REQUEST['on_table'],
136             'created' => date('Y-m-d H:i:s'),
137             'person_id' => $this->authUser->id,
138         ));
139         $id = $curlock->insert();
140         $this->jok($id);
141         
142     }
143      
144     
145         
146     
147 }