Translation update done using Pootle.
[phpmyadmin/dkf.git] / js / tbl_change.js
bloba092171cd4bf32ee72caa9edeb67c78a4217f04c
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    function used in table data manipulation pages
4  *
5  * @requires    jQuery
6  * @requires    jQueryUI
7  * @requires    js/functions.js
8  *
9  * @version $Id$
10  */
12 /**
13  * Modify from controls when the "NULL" checkbox is selected
14  *
15  * @param   string   the MySQL field type
16  * @param   string   the urlencoded field name - OBSOLETE
17  * @param   string   the md5 hashed field name
18  * @param   string   the multi_edit row sequence number
19  *
20  * @return  boolean  always true
21  */
22 function nullify(theType, urlField, md5Field, multi_edit)
24     var rowForm = document.forms['insertForm'];
26     if (typeof(rowForm.elements['funcs' + multi_edit + '[' + md5Field + ']']) != 'undefined') {
27         rowForm.elements['funcs' + multi_edit + '[' + md5Field + ']'].selectedIndex = -1;
28     }
30     // "SET" field , "ENUM" field with more than 20 characters
31     // or foreign key field (drop-down)
32     if (theType == 1 || theType == 3 || theType == 4) {
33         rowForm.elements['field_' + md5Field + multi_edit + '[]'].selectedIndex = -1;
34     }
35     // Other "ENUM" field
36     else if (theType == 2) {
37         var elts     = rowForm.elements['field_' + md5Field + multi_edit + '[]'];
38         // when there is just one option in ENUM:
39         if (elts.checked) {
40             elts.checked = false;
41         } else {
42             var elts_cnt = elts.length;
43             for (var i = 0; i < elts_cnt; i++ ) {
44                 elts[i].checked = false;
45             } // end for
47         } // end if
48     }
49     // foreign key field (with browsing icon for foreign values)
50     else if (theType == 6) {
51         rowForm.elements['field_' + md5Field + multi_edit + '[]'].value = '';
52     }
53     // Other field types
54     else /*if (theType == 5)*/ {
55         rowForm.elements['fields' + multi_edit + '[' + md5Field + ']'].value = '';
56     } // end if... else if... else
58     return true;
59 } // end of the 'nullify()' function
62 /**
63  * javascript DateTime format validation.
64  * its used to prevent adding default (0000-00-00 00:00:00) to database when user enter wrong values
65  * Start of validation part
66  */
67 //function checks the number of days in febuary
68 function daysInFebruary (year){
69     return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
71 //function to convert single digit to double digit
72 function fractionReplace(num)
74     num=parseInt(num);
75     var res="00";
76     switch(num)
77     {
78         case 1:res= "01";break;
79         case 2:res= "02";break;
80         case 3:res= "03";break;
81         case 4:res= "04";break;
82         case 5:res= "05";break;
83         case 6:res= "06";break;
84         case 7:res= "07";break;
85         case 8:res= "08";break;
86         case 9:res= "09";break;
87         }
88     return res;
91 /* function to check the validity of date
92 * The following patterns are accepted in this validation (accepted in mysql as well)
93 * 1) 2001-12-23
94 * 2) 2001-1-2
95 * 3) 02-12-23
96 * 4) And instead of using '-' the following punctuations can be used (+,.,*,^,@,/) All these are accepted by mysql as well. Therefore no issues
98 function isDate(val,tmstmp)
100     val=val.replace(/[.|*|^|+|//|@]/g,'-');
101     var arrayVal=val.split("-");
102     for(var a=0;a<arrayVal.length;a++)
103     {
104         if(arrayVal[a].length==1)
105             arrayVal[a]=fractionReplace(arrayVal[a]);
106     }
107     val=arrayVal.join("-");
108     var pos=2;
109             dtexp=new RegExp(/^([0-9]{4})-(((01|03|05|07|08|10|12)-((0[0-9])|([1-2][0-9])|(3[0-1])))|((02|04|06|09|11)-((0[0-9])|([1-2][0-9])|30)))$/);
110         if(val.length==8)
111         {
112             dtexp=new RegExp(/^([0-9]{2})-(((01|03|05|07|08|10|12)-((0[0-9])|([1-2][0-9])|(3[0-1])))|((02|04|06|09|11)-((0[0-9])|([1-2][0-9])|30)))$/);
113             pos=0;
114         }
115         if(dtexp.test(val))
116         {
117             var month=parseInt(val.substring(pos+3,pos+5));
118             var day=parseInt(val.substring(pos+6,pos+8));
119             var year=parseInt(val.substring(0,pos+2));
120             if(month==2&&day>daysInFebruary(year))
121                 return false;
122             if(val.substring(0,pos+2).length==2)
123             {
124                 if(val.substring(0,pos+2).length==2)
125                     year=parseInt("20"+val.substring(0,pos+2));
126                 else
127                     year=parseInt("19"+val.substring(0,pos+2));
128             }
129             if(tmstmp==true)
130             {
131                 if(year<1978) return false;
132                 if(year>2038||(year>2037&&day>19&&month>=1)||(year>2037&&month>1)) return false;
133                 }
134         }
135         else
136             return false;
137         return true;
140 /* function to check the validity of time
141 * The following patterns are accepted in this validation (accepted in mysql as well)
142 * 1) 2:3:4
143 * 2) 2:23:43
145 function isTime(val)
147     var arrayVal=val.split(":");
148     for(var a=0;a<arrayVal.length;a++)
149     {
150         if(arrayVal[a].length==1)
151             arrayVal[a]=fractionReplace(arrayVal[a]);
152     }
153     val=arrayVal.join(":");
154     tmexp=new RegExp(/^(([0-1][0-9])|(2[0-3])):((0[0-9])|([1-5][0-9])):((0[0-9])|([1-5][0-9]))$/);
155         if(!tmexp.test(val))
156             return false;
157         return true;
159 //validate the datetime and integer
160 function Validator(urlField, multi_edit,theType){
161     var rowForm = document.forms['insertForm'];
162     var evt = window.event || arguments.callee.caller.arguments[0];
163     var target = evt.target || evt.srcElement;
164     unNullify(urlField, multi_edit);
166     if(target.name.substring(0,6)=="fields")
167     {
168         var dt=rowForm.elements['fields[multi_edit][' + multi_edit + '][' + urlField + ']'];
169         // validate for date time
170         if(theType=="datetime"||theType=="time"||theType=="date"||theType=="timestamp")
171         {
172             if(theType=="date"){
173                 if(!isDate(dt.value))
174                     {
175                         dt.className="invalid_value";
176                         return false;
177                     }
178             }
179             else if(theType=="time")
180             {
181                 if(!isTime(dt.value))
182                 {
183                     dt.className="invalid_value";
184                     return false;
185                 }
186             }
187             else if(theType=="datetime"||theType=="timestamp")
188             {
189                 tmstmp=false;
190                 if(dt.value=="CURRENT_TIMESTAMP")
191                 {
192                     dt.className="";
193                     return true;
194                 }
195                 if(theType=="timestamp")
196                 {
197                     tmstmp=true;
198                 }
199                 if(dt.value=="0000-00-00 00:00:00")
200                     return true;
201                 var dv=dt.value.indexOf(" ");
202                 if(dv==-1)
203                 {
204                     dt.className="invalid_value";
205                     return false;
206                 }
207                 else
208                 {
209                     if(!(isDate(dt.value.substring(0,dv),tmstmp)&&isTime(dt.value.substring(dv+1))))
210                     {
211                         dt.className="invalid_value";
212                         return false;
213                     }
214                 }
215             }
216         }
217         //validate for integer type
218         if(theType.substring(0,3)=="int"){
220             if(isNaN(dt.value)){
221                     dt.className="invalid_value";
222                     return false;
223             }
224         }
225     }
227     dt.className="";
229  /* End of datetime validation*/
232  * Unchecks the "NULL" control when a function has been selected or a value
233  * entered
235  * @param   string   the urlencoded field name
236  * @param   string   the multi_edit row sequence number
238  * @return  boolean  always true
239  */
240 function unNullify(urlField, multi_edit)
242     var rowForm = document.forms['insertForm'];
244     if (typeof(rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']']) != 'undefined') {
245         rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']'].checked = false
246     } // end if
248     if (typeof(rowForm.elements['insert_ignore_' + multi_edit]) != 'undefined') {
249         rowForm.elements['insert_ignore_' + multi_edit].checked = false
250     } // end if
252     return true;
253 } // end of the 'unNullify()' function
256  * Ajax handlers for Change Table page
258  * Actions Ajaxified here:
259  * Submit Data to be inserted into the table
260  * Restart insertion with 'N' rows.
261  */
262 $(document).ready(function() {
264     /**
265      * Submission of data to be inserted into table
266      * 
267      * @uses    PMA_ajaxShowMessage()
268      */
269     $("#insertForm").live('submit', function(event) {
271         /**
272          * @var the_form    Object referring to the insertion form
273          */
274         var the_form = $(this);
275         event.preventDefault();
277         PMA_ajaxShowMessage();
278         $(the_form).append('<input type="hidden" name="ajax_request" value="true" />');
280         $.post($(the_form).attr('action'), $(the_form).serialize(), function(data) {
281             if(data.success == true) {
282                 PMA_ajaxShowMessage(data.message);
284                 $("#topmenucontainer")
285                 .next('div')
286                 .remove()
287                 .end()
288                 .after(data.sql_query);
290                 //Remove the empty notice div generated due to a NULL query passed to PMA_showMessage()
291                 var notice_class = $("#topmenucontainer").next("div").find('.notice');
292                 if($(notice_class).text() == '') {
293                     $(notice_class).remove();
294                 }
296                 //Clear the data in the forms
297                 $(the_form).find('input:reset').trigger('click');
298             }
299             else {
300                 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
301             }
302         })
303     }) // end submission of data to be inserted into table
305     /**
306      * Restart Insertion form
307      */
308     $("#insert_rows").live('change', function(event) {
309         event.preventDefault();
311         /**
312          * @var curr_rows   Number of current insert rows already on page
313          */
314         var curr_rows = $(".insertRowTable").length;
315         /**
316          * @var target_rows Number of rows the user wants
317          */
318         var target_rows = $("#insert_rows").val();
320         if(curr_rows < target_rows ) {
321             while( curr_rows < target_rows ) {
323                 /**
324                  * @var last_row    Object referring to the last row
325                  */
326                 var last_row = $("#insertForm").find(".insertRowTable:last");
328                 //Clone the insert tables
329                 $(last_row)
330                 .clone()
331                 .insertBefore("#insertForm > fieldset")
332                 .find('input[name*=multi_edit],select[name*=multi_edit]')
333                 .each(function() {
335                     /**
336                      * Extract the index from the name attribute for all input/select fields and increment it
337                      * name is of format funcs[multi_edit][10][<long random string of alphanum chars>]
338                      */
340                     /**
341                      * @var this_name   String containing name of the input/select elements
342                      */
343                     var this_name = $(this).attr('name');
344                     /** split {@link this_name} at [10], so we have the parts that can be concatenated later */
345                     var name_parts = this_name.split(/\[\d+\]/);
346                     /** extract the [10] from  {@link name_parts} */
347                     var old_row_index_string = this_name.match(/\[\d+\]/)[0];
348                     /** extract 10 - had to split into two steps to accomodate double digits */
349                     var old_row_index = parseInt(old_row_index_string.match(/\d+/)[0]);
351                     /** calculate next index i.e. 11 */
352                     var new_row_index = old_row_index + 1;
353                     /** generate the new name i.e. funcs[multi_edit][11][foobarbaz] */
354                     var new_name = name_parts[0] + '[' + new_row_index + ']' + name_parts[1];
356                     $(this).attr('name', new_name);
357                 });
359                 //Insert/Clone the ignore checkboxes
360                 if(curr_rows == 1 ) {
361                     $('<input id="insert_ignore_check_1" type="checkbox" name="insert_ignore_check_1" checked="checked" />')
362                     .insertBefore(".insertRowTable:last")
363                     .after('<label for="insert_ignore_check_1">' + PMA_messages['strIgnore'] + '</label>');
364                 }
365                 else {
367                     /**
368                      * @var last_checkbox   Object reference to the last checkbox in #insertForm
369                      */
370                     var last_checkbox = $("#insertForm").children('input:checkbox:last');
372                     /** name of {@link last_checkbox} */
373                     var last_checkbox_name = $(last_checkbox).attr('name');
374                     /** index of {@link last_checkbox} */
375                     var last_checkbox_index = parseInt(last_checkbox_name.match(/\d+/));
376                     /** name of new {@link last_checkbox} */
377                     var new_name = last_checkbox_name.replace(/\d+/,last_checkbox_index+1);
379                     $(last_checkbox)
380                     .clone()
381                     .attr({'id':new_name, 'name': new_name})
382                     .add('label[for^=insert_ignore_check]:last')
383                     .clone()
384                     .attr('for', new_name)
385                     .before('<br />')
386                     .insertBefore(".insertRowTable:last");
387                 }
388                 curr_rows++;
389             }
390         }
391         else if( curr_rows > target_rows) {
392             while(curr_rows > target_rows) {
393                 $("input[id^=insert_ignore_check]:last")
394                 .nextUntil("fieldset")
395                 .andSelf()
396                 .remove();
397                 curr_rows--;
398             }
399         }
400     })
401 }, 'top.frame_content'); //end $(document).ready()