1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview function used in table data manipulation pages
7 * @requires js/functions.js
13 * Modify from controls when the "NULL" checkbox is selected
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
20 * @return boolean always true
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;
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;
36 else if (theType == 2) {
37 var elts = rowForm.elements['field_' + md5Field + multi_edit + '[]'];
38 // when there is just one option in ENUM:
42 var elts_cnt = elts.length;
43 for (var i = 0; i < elts_cnt; i++ ) {
44 elts[i].checked = false;
49 // foreign key field (with browsing icon for foreign values)
50 else if (theType == 6) {
51 rowForm.elements['field_' + md5Field + multi_edit + '[]'].value = '';
54 else /*if (theType == 5)*/ {
55 rowForm.elements['fields' + multi_edit + '[' + md5Field + ']'].value = '';
56 } // end if... else if... else
59 } // end of the 'nullify()' function
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
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)
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;
91 /* function to check the validity of date
92 * The following patterns are accepted in this validation (accepted in mysql as well)
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++)
104 if(arrayVal[a].length==1)
105 arrayVal[a]=fractionReplace(arrayVal[a]);
107 val=arrayVal.join("-");
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)))$/);
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)))$/);
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))
122 if(val.substring(0,pos+2).length==2)
124 if(val.substring(0,pos+2).length==2)
125 year=parseInt("20"+val.substring(0,pos+2));
127 year=parseInt("19"+val.substring(0,pos+2));
131 if(year<1978) return false;
132 if(year>2038||(year>2037&&day>19&&month>=1)||(year>2037&&month>1)) return false;
140 /* function to check the validity of time
141 * The following patterns are accepted in this validation (accepted in mysql as well)
147 var arrayVal=val.split(":");
148 for(var a=0;a<arrayVal.length;a++)
150 if(arrayVal[a].length==1)
151 arrayVal[a]=fractionReplace(arrayVal[a]);
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]))$/);
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")
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")
173 if(!isDate(dt.value))
175 dt.className="invalid_value";
179 else if(theType=="time")
181 if(!isTime(dt.value))
183 dt.className="invalid_value";
187 else if(theType=="datetime"||theType=="timestamp")
190 if(dt.value=="CURRENT_TIMESTAMP")
195 if(theType=="timestamp")
199 if(dt.value=="0000-00-00 00:00:00")
201 var dv=dt.value.indexOf(" ");
204 dt.className="invalid_value";
209 if(!(isDate(dt.value.substring(0,dv),tmstmp)&&isTime(dt.value.substring(dv+1))))
211 dt.className="invalid_value";
217 //validate for integer type
218 if(theType.substring(0,3)=="int"){
221 dt.className="invalid_value";
229 /* End of datetime validation*/
232 * Unchecks the "NULL" control when a function has been selected or a value
235 * @param string the urlencoded field name
236 * @param string the multi_edit row sequence number
238 * @return boolean always true
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
248 if (typeof(rowForm.elements['insert_ignore_' + multi_edit]) != 'undefined') {
249 rowForm.elements['insert_ignore_' + multi_edit].checked = false
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.
262 $(document).ready(function() {
265 * Submission of data to be inserted into table
267 * @uses PMA_ajaxShowMessage()
269 $("#insertForm").live('submit', function(event) {
272 * @var the_form Object referring to the insertion form
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")
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();
296 //Clear the data in the forms
297 $(the_form).find('input:reset').trigger('click');
300 PMA_ajaxShowMessage(PMA_messages['strErrorProcessingRequest'] + " : "+data.error, "7000");
303 }) // end submission of data to be inserted into table
306 * Restart Insertion form
308 $("#insert_rows").live('change', function(event) {
309 event.preventDefault();
312 * @var curr_rows Number of current insert rows already on page
314 var curr_rows = $(".insertRowTable").length;
316 * @var target_rows Number of rows the user wants
318 var target_rows = $("#insert_rows").val();
320 if(curr_rows < target_rows ) {
321 while( curr_rows < target_rows ) {
324 * @var last_row Object referring to the last row
326 var last_row = $("#insertForm").find(".insertRowTable:last");
328 //Clone the insert tables
331 .insertBefore("#insertForm > fieldset")
332 .find('input[name*=multi_edit],select[name*=multi_edit]')
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>]
341 * @var this_name String containing name of the input/select elements
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);
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>');
368 * @var last_checkbox Object reference to the last checkbox in #insertForm
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);
381 .attr({'id':new_name, 'name': new_name})
382 .add('label[for^=insert_ignore_check]:last')
384 .attr('for', new_name)
386 .insertBefore(".insertRowTable:last");
391 else if( curr_rows > target_rows) {
392 while(curr_rows > target_rows) {
393 $("input[id^=insert_ignore_check]:last")
394 .nextUntil("fieldset")
401 }, 'top.frame_content'); //end $(document).ready()