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