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  * baseURL + /Core/Lock/lock?on_id=...&on_table=...&force=1
26  * - returns id..
27  * 
28  * baseURL + /Core/Lock/unlock?on_id=...&on_table=...&force=1
29  * - returns jerr or jok
30  */
31
32 require_once 'Pman.php';
33
34 class Pman_Core_Lock extends Pman
35 {
36     
37     function getAuth()
38     {
39          $au = $this->getAuthUser();
40         if (!$au) {
41              $this->jerr("Not authenticated", array('authFailure' => true));
42         }
43         $this->authUser = $au;
44         // check that it's a supplier!!!! 
45         
46         return true; 
47     }
48     
49     function get($action)
50     {
51         
52         // default action is to attempt to lock..
53         $action = empty($action) ? 'lock' : 'unlock';
54         $this->$action($curlock);
55         
56        
57         
58         
59     }
60     
61     function unlock($curlock)
62     {
63     
64         if (empty($_REQUEST['id'])) {
65             $this->jerr("No lock id");
66         }
67         $curlock = DB_DataObject::factory('Core_locking');
68         if (!$curlock->get($_REQUEST['id'])) {
69             $this->jerr("No lock exists");
70         }
71         
72         if ($curlock->person_id != $this->authUser->id) {
73             $this->jerr("Lock id is invalid");
74         }
75         
76         $curlock->delete();
77         
78         $this->jok('unlocked');
79     }
80     function lock()
81     {
82         
83         if (empty($_REQUEST['on_id']) || empty($_REQUEST['on_table'])) {
84             $this->jerr("Missing table or id");
85         }
86        
87         $tab = str_replace('/', '',$_REQUEST['on_table']); // basic protection??
88         $x = DB_DataObject::factory($tab);
89         if (!$x->get($_REQUEST['on_id'])) {
90             $this->jerr("Item does not exist");
91         }
92         // is there a current lock on the item..
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         ));
99         
100         
101         $nlocks = $curlock->count() 
102         if ($nlocks && empty($_REQUEST['force'])) {
103             $curlock->selectAdd();
104             $curlock->selectAdd('distinct(person_id), created');
105             
106             
107             $ar = $curlock->fetchAll('person_id', 'created');
108             $p = DB_DataObject::factory('Person');
109             $p->whereAddIn('id', $ar, 'int');
110             $p->find();
111             while ($p->fetch()) {
112                 $ret[$p->id] = $p->toRooArray();
113                 $ret[$p->id]->lock_created = $ar[$p->id];
114             }
115             $this->jok($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 }