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