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