* Do you to prevent them saving and lock it yourself..
*
*
+ *
+ *
+ *
* -- interacts with Roo and _lock = id..
*
*
+ * call :
+ * try and lock it..
+ * baseURL + /Core/Lock/lock?on_id=...&on_table=...
+ * - returns id or an array of who has the locks.
+ *
+ * Force an unlock after a warning..
+ * baseURL + /Core/Lock/lock?on_id=...&on_table=...&force=1
+ * - returns id..
+ *
+ * Unlock - call when window is closed..
+ * baseURL + /Core/Lock/unlock?on_id=...&on_table=...&force=1
+ * - returns jerr or jok
*/
require_once 'Pman.php';
return true;
}
- function get($action)
+ function get($action, $opts=array())
+ {
+ // should we allow url links to lock things???
+ // only for debugging??
+ $this->post($action);
+ //
+ $this->jerr("invalid request");
+ }
+
+ function post($action)
{
// default action is to attempt to lock..
- $action = empty($action) ? 'lock' : 'unlock';
- $this->$action($curlock);
-
-
-
+ $action = empty($action) || $action == 'lock' ? 'lock' : 'unlock';
+ $this->$action();
}
- function unlock($curlock)
+ function unlock()
{
if (empty($_REQUEST['id'])) {
$this->jerr("No lock id");
}
- $curlock = DB_DataObject::factory('Core_locking');
+ $curlock = DB_DataObject::factory('core_locking');
if (!$curlock->get($_REQUEST['id'])) {
- $this->jerr("No lock exists");
+ $this->jok("No lock exists"); // been deleted before.. probably ok..
}
- if ($curlock->person_id != $this->authUser->id) {
+ if ($curlock->person_id != $this->authUser->id && empty($_REQUEST['force'])) {
+ // this is an error conditon..
$this->jerr("Lock id is invalid");
}
$this->jerr("Missing table or id");
}
- $tab = str_replace('/', '',$_REQUEST['on_table']); // basic protection??
+ $tab = str_replace('/', '', strtolower($_REQUEST['on_table'])); // basic protection??
$x = DB_DataObject::factory($tab);
if (!$x->get($_REQUEST['on_id'])) {
$this->jerr("Item does not exist");
}
// is there a current lock on the item..
- $curlock = DB_DataObject::factory('Core_locking');
+ $curlock = DB_DataObject::factory('core_locking');
$curlock->setFrom(array(
'on_id' => $_REQUEST['on_id'],
- 'on_table' => $_REQUEST['on_table']
+ 'on_table' => strtolower($_REQUEST['on_table'])
));
- if ($curlock->count()) {
- $err = $this->canUnlock();
- if ($err !== true) {
- $this->jerr($err);
+
+ // remove old locks..
+ $llc = clone($curlock);
+ $exp = date('Y-m-d', strtotime('NOW - 1 WEEK'));
+ $llc->whereAdd("created < '$exp'");
+ if ($llc->count()) {
+ $llc->find();
+ while($llc->fetch()) {
+ $llcd = clone($llc);
+ $llcd->delete();
+
}
}
- // make a lock..
- $curlock = DB_DataObject::factory('Core_locking');
- $curlock->setFrom(array(
- 'on_id' => $_REQUEST['on_id'],
- 'on_table' => $_REQUEST['on_table'],
- 'created' => date('Y-m-d H:i:s'),
- 'person_id' => $this->authUser->id,
- ));
- $id = $curlock->insert();
- $this->jok($id);
- }
-
- function canUnlock()
- {
- // the only scenario where we can automatically unlock is:::
+ $curlock_ex = clone($curlock);
+ $curlock->person_id = $this->authUser->id;
- // this user owns the lock.
- $curlock = DB_DataObject::factory('Core_locking');
- $curlock->setFrom(array(
- 'on_id' => $_REQUEST['on_id'],
- 'on_table' => $_REQUEST['on_table']
- ));
- $cc = clone($curlock);
- // the user who owns the lock is not logged in.. ?? - their last
- $curlock->find();
- $users = array();
- while ($curlock->fetch()) {
- $u = DB_DataObject::factory('Person');
- $u->get($curlock->person_id);
- if (!$u->isCurrentlyLoggedIn()) {
- $cc = clone($curlock);
- $cc->delete();
- continue;
+ $curlock_ex->whereAdd('person_id != '. $this->authUser->id);
+ $nlocks = $curlock_ex->count() ;
+
+ $ret = false;
+
+ if ($nlocks && empty($_REQUEST['force'])) {
+ // DB_DataObjecT::debugLevel(1);
+ $ar = $curlock_ex->fetchAll('person_id', 'created');
+ $p = DB_DataObject::factory('core_person');
+ $p->selectAdd();
+ $p->selectAdd('id,name,email');
+
+ $p->whereAddIn('id', array_keys($ar), 'int');
+ $p->find();
+ $ret = array();
+ while ($p->fetch()) {
+ $ret[$p->id] = $p->toArray();
+ $ret[$p->id]['lock_created'] = $ar[$p->id];
}
- $users[] = clone($u);
+ $this->jok(array_values($ret));
}
- if (empty($users)) {
- return true;
+ // trash the lock if it belongs to current user..
+ $ulocks = $curlock->count();
+ if ($ulocks) {
+ // trash all the locks..
+ $curlock = DB_DataObject::factory('core_locking');
+ $curlock->setFrom(array(
+ 'on_id' => $_REQUEST['on_id'],
+ 'on_table' => strtolower($_REQUEST['on_table']),
+ 'person_id' => $this->authUser->id
+ ));
+ $curlock->find();
+ while($curlock->fetch()) {
+ $cc =clone($curlock);
+ $cc->delete();
+ }
+ }
+ if ($nlocks && !empty($_REQUEST['force'])) {
+ // user has decied to delete eveyone elses locks..
+ $curlock_ex->find();
+ while($curlock_ex->fetch()) {
+ $cc =clone($curlock_ex);
+ $cc->delete();
+ }
}
- // situations
-
- //- the user is logged in, and we can clear it..
-
- //- the user is logged in multiple times, on different browser..
-
- //- the user is logged in multiple times on the same browser..
-
-
-
- // one of two error messages..
-
- $this->jerr("Item is Locked by " . $u->name . ' (' . $u->email . "), Try asking them to log out");
-
- return true;
-
+ // make a lock..
+ $curlock = DB_DataObject::factory('core_locking');
+ $curlock->setFrom(array(
+ 'on_id' => $_REQUEST['on_id'],
+ 'on_table' => strtolower($_REQUEST['on_table']),
+ 'created' => date('Y-m-d H:i:s'),
+ 'person_id' => $this->authUser->id,
+ ));
+ $id = $curlock->insert();
+ $this->jok( $id);
}
+