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                 }
67                 _t.id = data;
68                 _t.cfg.success(_t); //dont care about scope..
69                 
70                 
71             }
72         })
73     },
74     confirmBreak : function (ar)
75     {
76         
77         var msg = "This Record is Locked by the following people, <br/>" + 
78             "Do you want to continue, this will prevent these people from saving their changes<br/>";
79             
80         Roo.each(ar, function(p) {
81             msg += '<br/>' + p.name + ' at ' + p.lock_created ;
82         })
83         var _t = this;
84         Roo.MessageBox.confirm("Confirm breaking locks", msg, function(r) {
85             if (r != 'yes') {
86                 return;
87             }
88             _t.callLock(1);
89         });
90             
91         
92         
93         
94     },
95     
96     
97     unlock : function() {
98         Pman.Request({
99             url : baseURL + '/Core/Lock/unlock',
100             params : {
101                 id : this.id
102             },
103             failure : function() {
104                 Roo.MessageBox.alert("Error", "UnLock Request failed, you may get a warning when trying to edit again");
105             },
106             success : function(data)
107             {
108                 // don nothing
109                 
110             }
111         });
112     }
113     
114
115 });
116