Pman.Lock.js
[Pman.Core] / Pman.Lock.js
1 //<script type="text/javascript">
2 /**
3  * Locking - 
4  * 
5  * usage:
6  * 
7  
8      new Pman.Lock( {
9           table : 'Product',
10          id : 123,
11          success : function(lock) { ..show dialog etc..... 
12           
13            ... dostuff..
14            ... send _lock=XXX to Roo updated code..
15            
16             lock.unlock() -- we dont care about the result..
17           }
18         }
19     * 
20  * 
21  * 
22  *  call : 
23  * try and lock it..
24  * baseURL + /Core/Lock/lock?on_id=...&on_table=...
25  * - returns id or an array of who has the locks.
26  * 
27  * Force an unlock after a warning..
28  * baseURL + /Core/Lock/lock?on_id=...&on_table=...&force=1
29  * - returns id..
30  * 
31  * Unlock - call when window is closed..
32  * baseURL + /Core/Lock/unlock?on_id=...&on_table=...&force=1
33  * - returns jerr or jok
34  * 
35  */
36 Pman.Lock = function (cfg) {
37     this.cfg = cfg;
38     this.callLock();
39 }
40
41 Roo.apply(Pman.Lock.prototype, {
42     
43     callLock : function(force)
44     {
45         force = force || 0;
46         var _t = this
47         Pman.Request({
48             url : baseURL + 'Core/Lock/lock',
49             params : {
50                 on_table : cfg.table,
51                 on_id : cfg.id,
52                 force : force
53             },
54             failure : function() {
55                 Roo.MessageBox.alert("Error", "Lock Request failed, please try again");
56             },
57             success : function(data)
58             {
59                 
60                 
61                 if (!force && typeof(data) == 'object') {
62                     _t.confirmBreak(data);
63                 }
64                 
65                 _t.cfg.success(_t); //dont care about scope..
66                 
67                 
68             }
69         })
70     },
71     confirmBreak : function (ar)
72     {
73         
74         var msg = "This Record is Locked by the following people, <br/>" + 
75             "Do you want to continue, this will prevent these people from saving their changes<br/>";
76             
77         Roo.each(ar, function(p) {
78             msg += '<br/>' + p.name + ' at ' + p.lock_created ;
79         })
80         var _t = this;
81         Roo.messageBox.confirm("Confirm breaking locks", msg, function(r) {
82             if (r != 'yes') {
83                 return;
84             }
85             _t.callLock(1);
86         });
87             
88         
89         
90         
91     }
92     
93     
94     unlock : function() {
95         Pman.Request({
96             url : baseURL + 'Core/Lock/unlock',
97             params : {
98                 id : this.lock_id,
99                 on_id : cfg.id
100             },
101             failure : function() {
102                 Roo.MessageBox.alert("Error", "Lock Request failed, please try again");
103             },
104             success : function(data)
105             {
106                 Roo.log(data);
107                 
108                 if (typeof(data) == 'object') {
109                     _t.confirmBreak(data);
110                 }
111                 
112                 _t.cfg.success(_t); //dont care about scope..
113                 
114                 
115             }
116         })
117     }
118     
119
120 });
121