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