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     cfg : false,
43     
44     id : false, // the id of the lock..
45     
46     callLock : function(force)
47     {
48         force = force || 0;
49         var _t = this
50         Pman.Request({
51             url : baseURL + '/Core/Lock/lock',
52             params : {
53                 on_table : this.cfg.table,
54                 on_id : this.cfg.id,
55                 force : force
56             },
57             failure : function() {
58                 Roo.MessageBox.alert("Error", "Lock Request failed, please try again");
59             },
60             success : function(data)
61             {
62                 
63                Roo.log(data);
64                 if (!force && typeof(data) == 'object') {
65                     _t.confirmBreak(data);
66                     return;
67                 }
68                 _t.id = data;
69                 _t.cfg.success(_t); //dont care about scope..
70                 
71                 
72             }
73         })
74     },
75     confirmBreak : function (ar)
76     {
77         
78         var msg = "This Record is Locked by the following people, <br/>" + 
79             "Do you want to continue, this will prevent these people from saving their changes<br/>";
80             
81         Roo.each(ar, function(p) {
82             msg += '<br/>' + p.name + ' at ' + p.lock_created ;
83         });
84         var _t = this;
85         Roo.MessageBox.confirm("Confirm breaking locks", msg, function(r) {
86             if (r != 'yes') {
87                 return;
88             }
89             _t.callLock(1);
90         });
91             
92         
93         
94         
95     },
96     
97     
98     unlock : function() {
99         Pman.Request({
100             url : baseURL + '/Core/Lock/unlock',
101             params : {
102                 id : this.id
103             },
104             failure : function() {
105                 Roo.MessageBox.alert("Error", "UnLock Request failed, you may get a warning when trying to edit again");
106             },
107             success : function(data)
108             {
109                 // don nothing
110                 
111             }
112         });
113     }
114     
115
116 });
117