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