10 * This page is locked by XXXXXXX..
11 * Do you to prevent them saving and lock it yourself..
17 * -- interacts with Roo and _lock = id..
22 * baseURL + /Core/Lock/lock?on_id=...&on_table=...
23 * - returns id or an array of who has the locks.
25 * Force an unlock after a warning..
26 * baseURL + /Core/Lock/lock?on_id=...&on_table=...&force=1
29 * Unlock - call when window is closed..
30 * baseURL + /Core/Lock/unlock?on_id=...&on_table=...&force=1
31 * - returns jerr or jok
34 require_once 'Pman.php';
36 class Pman_Core_Lock extends Pman
41 $au = $this->getAuthUser();
43 $this->jerr("Not authenticated", array('authFailure' => true));
45 $this->authUser = $au;
46 // check that it's a supplier!!!!
53 // should we allow url links to lock things???
54 // only for debugging??
57 $this->jerr("invalid request");
60 function post($action)
63 // default action is to attempt to lock..
64 $action = empty($action) || $action == 'lock' ? 'lock' : 'unlock';
72 if (empty($_REQUEST['id'])) {
73 $this->jerr("No lock id");
75 $curlock = DB_DataObject::factory('Core_locking');
76 if (!$curlock->get($_REQUEST['id'])) {
77 $this->jok("No lock exists"); // been deleted before.. probably ok..
80 if ($curlock->person_id != $this->authUser->id) {
81 // this is an error conditon..
82 $this->jerr("Lock id is invalid");
87 $this->jok('unlocked');
92 if (empty($_REQUEST['on_id']) || empty($_REQUEST['on_table'])) {
93 $this->jerr("Missing table or id");
96 $tab = str_replace('/', '', strtolower($_REQUEST['on_table'])); // basic protection??
97 $x = DB_DataObject::factory($tab);
98 if (!$x->get($_REQUEST['on_id'])) {
99 $this->jerr("Item does not exist");
101 // is there a current lock on the item..
103 $curlock = DB_DataObject::factory('Core_locking');
104 $curlock->setFrom(array(
105 'on_id' => $_REQUEST['on_id'],
106 'on_table' => strtolower($_REQUEST['on_table'])
108 $curlock->person_id = $this->authUser->id;
109 $curlock_ex = clone($curlock);
110 $curlock_ex->whereAdd('person_id != '. $this->authUser->id);
111 $nlocks = $curlock_ex->count() ;
115 if ($nlocks && empty($_REQUEST['force'])) {
116 // DB_DataObjecT::debugLevel(1);
117 $ar = $curlock->fetchAll('person_id', 'created');
118 $p = DB_DataObject::factory('Person');
120 $p->selectAdd('id,name,email');
122 $p->whereAddIn('id', array_keys($ar), 'int');
125 while ($p->fetch()) {
126 $ret[$p->id] = $p->toArray();
127 $ret[$p->id]['lock_created'] = $ar[$p->id];
129 $ret = array_values($ret);
130 //$this->jok(array_values($ret));
133 // trash the lock if it belongs to current user..
134 $ulocks = $curlock->count();
136 // trash all the locks..
137 $curlock = DB_DataObject::factory('Core_locking');
138 $curlock->setFrom(array(
139 'on_id' => $_REQUEST['on_id'],
140 'on_table' => strtolower($_REQUEST['on_table'])
142 $curlock->person_id = $this->authUser->id;
144 while($curlock->fetch()) {
145 $cc =clone($curlock);
149 if ($nlocks && !empty($_REQUEST['force'])) {
150 // user has decied to delete eveyone elses locks..
152 while($curlock_ex->fetch()) {
153 $cc =clone($curlock_ex);
161 $curlock = DB_DataObject::factory('Core_locking');
162 $curlock->setFrom(array(
163 'on_id' => $_REQUEST['on_id'],
164 'on_table' => strtolower($_REQUEST['on_table']),
165 'created' => date('Y-m-d H:i:s'),
166 'person_id' => $this->authUser->id,
168 $id = $curlock->insert();
169 $this->jok($ret ? $ret : $id);