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