Proper indentation.
[phpmyadmin/crack.git] / js / sql.js
blob1c46c056b19a4fb52a014bd320f09962ebe6ed27
1 /* vim: set expandtab sw=4 ts=4 sts=4: */
2 /**
3  * @fileoverview    functions used wherever an sql query form is used
4  *
5  * @requires    jQuery
6  * @requires    js/functions.js
7  *
8  */
10 /**
11  * decode a string URL_encoded
12  *
13  * @param string str
14  * @return string the URL-decoded string
15  */
16 var data_vt;
17 function PMA_urldecode(str) {
18     return decodeURIComponent(str.replace(/\+/g, '%20'));
21 /**
22  * Get the field name for the current field.  Required to construct the query
23  * for inline editing
24  *
25  * @param   $this_field  jQuery object that points to the current field's tr
26  * @param   disp_mode    string
27  */
28 function getFieldName($this_field, disp_mode) {
30     if(disp_mode == 'vertical') {
31         var field_name = $this_field.siblings('th').find('a').text();
32         // happens when just one row (headings contain no a)
33         if ("" == field_name) {
34             field_name = $this_field.siblings('th').text();
35         }
36     }
37     else {
38         var this_field_index = $this_field.index();
39         // ltr or rtl direction does not impact how the DOM was generated
40         //
41         // 5 columns to account for the checkbox, edit, appended inline edit, copy and delete anchors but index is zero-based so substract 4
42         var field_name = $('#table_results').find('thead').find('th:nth('+ (this_field_index-4 )+') a').text();
43         // happens when just one row (headings contain no a)
44         if ("" == field_name) {
45             field_name = $('#table_results').find('thead').find('th:nth('+ (this_field_index-4 )+')').text();
46         }
47     }
49     field_name = $.trim(field_name);
51     return field_name;
54 /**
55  * The function that iterates over each row in the table_results and appends a
56  * new inline edit anchor to each table row.
57  *
58  */
59 function appendInlineAnchor() {
60     var disp_mode = $("#top_direction_dropdown").val();
62     if (disp_mode == 'vertical') {
63         // there can be one or two tr containing this class, depending
64         // on the ModifyDeleteAtLeft and ModifyDeleteAtRight cfg parameters
65         $('#table_results tr')
66             .find('.edit_row_anchor')
67             .removeClass('.edit_row_anchor')
68             .parent().each(function() {
69             var $this_tr = $(this);
70             var $cloned_tr = $this_tr.clone();
72             var $img_object = $cloned_tr.find('img:first').attr('title', PMA_messages['strInlineEdit']);
73             if ($img_object.length != 0) {
74                 var img_src = $img_object.attr('src').replace(/b_edit/,'b_inline_edit');
75                 $img_object.attr('src', img_src);
76             }
78             $cloned_tr.find('td')
79              .addClass('inline_edit_anchor')
80              .find('a').attr('href', '#')
81              .find('span')
82              .text(' ' + PMA_messages['strInlineEdit'])
83              .prepend($img_object);
85             $cloned_tr.insertAfter($this_tr);
86         });
88         $('#rowsDeleteForm').find('tbody').find('th').each(function() {
89             var $this_th = $(this);
90             if ($this_th.attr('rowspan') == 4) {
91                 $this_th.attr('rowspan', '5');
92             }
93         });
94     }
95     else {
96         // horizontal mode
97         $('.edit_row_anchor').each(function() {
99             var $this_td = $(this);
100             $this_td.removeClass('edit_row_anchor');
102             var $cloned_anchor = $this_td.clone();
104             var $img_object = $cloned_anchor.find('img').attr('title', PMA_messages['strInlineEdit']);
105             if ($img_object.length != 0) {
106                 var img_src = $img_object.attr('src').replace(/b_edit/,'b_inline_edit');
107                 $img_object.attr('src', img_src);
108                 $cloned_anchor
109                  .find('a').attr('href', '#')
110                  .find('span')
111                  .text(' ' + PMA_messages['strInlineEdit']);
112                 $cloned_anchor
113                  .find('span')
114                  .first()
115                  .prepend($img_object);
116             } else {
117                 // the link was too big so <input type="image"> is there
118                 $img_object = $cloned_anchor.find('input:image').attr('title', PMA_messages['strInlineEdit']);
119                 var img_src = $img_object.attr('src').replace(/b_edit/,'b_inline_edit');
120                 $img_object.attr('src', img_src);
121                 $cloned_anchor
122                  .find('.clickprevimage')
123                  .text(' ' + PMA_messages['strInlineEdit']);
124             }
126             $cloned_anchor
127              .addClass('inline_edit_anchor');
129             $this_td.after($cloned_anchor);
130         });
132         $('#rowsDeleteForm').find('thead, tbody').find('th').each(function() {
133             var $this_th = $(this);
134             if ($this_th.attr('colspan') == 4) {
135                 $this_th.attr('colspan', '5');
136             }
137         });
138     }
141 /**#@+
142  * @namespace   jQuery
143  */
146  * @description <p>Ajax scripts for sql and browse pages</p>
148  * Actions ajaxified here:
149  * <ul>
150  * <li>Retrieve results of an SQL query</li>
151  * <li>Paginate the results table</li>
152  * <li>Sort the results table</li>
153  * <li>Change table according to display options</li>
154  * <li>Inline editing of data</li>
155  * </ul>
157  * @name        document.ready
158  * @memberOf    jQuery
159  */
160 $(document).ready(function() {
162     /**
163      * Set a parameter for all Ajax queries made on this page.  Don't let the
164      * web server serve cached pages
165      */
166     $.ajaxSetup({
167         cache: 'false'
168     });
170     /**
171      * current value of the direction in which the table is displayed
172      * @type    String
173      * @fieldOf jQuery
174      * @name    disp_mode
175      */
176     var disp_mode = $("#top_direction_dropdown").val();
178     /**
179      * Update value of {@link jQuery.disp_mode} everytime the direction dropdown changes value
180      * @memberOf    jQuery
181      * @name        direction_dropdown_change
182      */
183     $("#top_direction_dropdown, #bottom_direction_dropdown").live('change', function(event) {
184         disp_mode = $(this).val();
185     })
187     /**
188      * Attach the {@link appendInlineAnchor} function to a custom event, which
189      * will be triggered manually everytime the table of results is reloaded
190      * @memberOf    jQuery
191      */
192     $("#sqlqueryresults").live('appendAnchor',function() {
193         appendInlineAnchor();
194     })
196     /**
197      * Trigger the appendAnchor event to prepare the first table for inline edit
198      * (see $GLOBALS['cfg']['AjaxEnable'])
199      * @memberOf    jQuery
200      * @name        sqlqueryresults_trigger
201      */
202     $("#sqlqueryresults.ajax").trigger('appendAnchor');
204     /**
205      * Append the "Show/Hide query box" message to the query input form
206      *
207      * @memberOf jQuery
208      * @name    appendToggleSpan
209      */
210     // do not add this link more than once
211     if (! $('#sqlqueryform').find('a').is('#togglequerybox')) {
212         $('<a id="togglequerybox"></a>')
213         .html(PMA_messages['strHideQueryBox'])
214         .appendTo("#sqlqueryform")
215         // initially hidden because at this point, nothing else
216         // appears under the link
217         .hide();
219         // Attach the toggling of the query box visibility to a click
220         $("#togglequerybox").bind('click', function() {
221             var $link = $(this)
222             $link.siblings().slideToggle("fast");
223             if ($link.text() == PMA_messages['strHideQueryBox']) {
224                 $link.text(PMA_messages['strShowQueryBox']);
225                 // cheap trick to add a spacer between the menu tabs
226                 // and "Show query box"; feel free to improve!
227                 $('#togglequerybox_spacer').remove();
228                 $link.before('<br id="togglequerybox_spacer" />');
229             } else {
230                 $link.text(PMA_messages['strHideQueryBox']);
231             }
232             // avoid default click action
233             return false;
234         })
235     }
237     /**
238      * Ajax Event handler for 'SQL Query Submit'
239      *
240      * @see         PMA_ajaxShowMessage()
241      * @see         $cfg['AjaxEnable']
242      * @memberOf    jQuery
243      * @name        sqlqueryform_submit
244      */
245     $("#sqlqueryform.ajax").live('submit', function(event) {
246         event.preventDefault();
247         // remove any div containing a previous error message
248         $('.error').remove();
250         $form = $(this);
251         PMA_ajaxShowMessage();
253         if (! $form.find('input:hidden').is('#ajax_request_hidden')) {
254             $form.append('<input type="hidden" id="ajax_request_hidden" name="ajax_request" value="true" />');
255         }
257         $.post($(this).attr('action'), $(this).serialize() , function(data) {
258             if(data.success == true) {
259                 // fade out previous messages, if any
260                 $('.success').fadeOut();
261                 $('.sqlquery_message').fadeOut();
262                 // show a message that stays on screen
263                 $('#sqlqueryform').before(data.message);
264                 // and display the query
265                 $('<div class="sqlquery_message"></div>')
266                  .html(data.sql_query)
267                  .insertBefore('#sqlqueryform');
268                 // unnecessary div that came from data.sql_query
269                 $('.notice').remove();
270                 $('#sqlqueryresults').show();
271                 // this happens if a USE command was typed
272                 if (typeof data.reload != 'undefined') {
273                     $form.find('input[name=db]').val(data.db);
274                     // need to regenerate the whole upper part
275                     $form.find('input[name=ajax_request]').remove();
276                     $form.append('<input type="hidden" name="reload" value="true" />');
277                     $.post('db_sql.php', $form.serialize(), function(data) {
278                         $('body').html(data);
279                     }); // end inner post
280                 }
281             }
282             else if (data.success == false ) {
283                 // show an error message that stays on screen
284                 $('#sqlqueryform').before(data.error);
285                 $('#sqlqueryresults').hide();
286             }
287             else {
288                 // real results are returned
289                 $received_data = $(data);
290                 $zero_row_results = $received_data.find('textarea[name="sql_query"]');
291                 // if zero rows are returned from the query execution
292                 if ($zero_row_results.length > 0) {
293                     $('#sqlquery').val($zero_row_results.val());
294                 } else {
295                     $('#sqlqueryresults').show();
296                     $("#sqlqueryresults").html(data);
297                     $("#sqlqueryresults").trigger('appendAnchor');
298                     $('#togglequerybox').show();
299                     if($("#togglequerybox").siblings(":visible").length > 0) {
300                         $("#togglequerybox").trigger('click');
301                     }
302                     PMA_init_slider();
303                 }
304             }
305         }) // end $.post()
306     }) // end SQL Query submit
308     /**
309      * Ajax Event handlers for Paginating the results table
310      */
312     /**
313      * Paginate when we click any of the navigation buttons
314      * (only if the element has the ajax class, see $cfg['AjaxEnable'])
315      * @memberOf    jQuery
316      * @name        paginate_nav_button_click
317      * @uses        PMA_ajaxShowMessage()
318      * @see         $cfg['AjaxEnable']
319      */
320     $("input[name=navig].ajax").live('click', function(event) {
321         /** @lends jQuery */
322         event.preventDefault();
324         PMA_ajaxShowMessage();
326         /**
327          * @var $the_form    Object referring to the form element that paginates the results table
328          */
329         var $the_form = $(this).parent("form");
331         $the_form.append('<input type="hidden" name="ajax_request" value="true" />');
333         $.post($the_form.attr('action'), $the_form.serialize(), function(data) {
334             $("#sqlqueryresults").html(data);
335             $("#sqlqueryresults").trigger('appendAnchor');
336             PMA_init_slider();
337         }) // end $.post()
338     })// end Paginate results table
340     /**
341      * Paginate results with Page Selector dropdown
342      * @memberOf    jQuery
343      * @name        paginate_dropdown_change
344      * @see         $cfg['AjaxEnable']
345      */
346     $("#pageselector").live('change', function(event) {
347         var $the_form = $(this).parent("form");
349         if ($(this).hasClass('ajax')) {
350             event.preventDefault();
352             PMA_ajaxShowMessage();
354             $.post($the_form.attr('action'), $the_form.serialize() + '&ajax_request=true', function(data) {
355                 $("#sqlqueryresults").html(data);
356                 $("#sqlqueryresults").trigger('appendAnchor');
357                 PMA_init_slider();
358             }) // end $.post()
359         } else {
360             $the_form.submit();
361         }
363     })// end Paginate results with Page Selector
365     /**
366      * Ajax Event handler for sorting the results table
367      * @memberOf    jQuery
368      * @name        table_results_sort_click
369      * @see         $cfg['AjaxEnable']
370      */
371     $("#table_results.ajax").find("a[title=Sort]").live('click', function(event) {
372         event.preventDefault();
374         PMA_ajaxShowMessage();
376         $anchor = $(this);
378         $.get($anchor.attr('href'), $anchor.serialize() + '&ajax_request=true', function(data) {
379             $("#sqlqueryresults")
380              .html(data)
381              .trigger('appendAnchor');
382         }) // end $.get()
383     })//end Sort results table
385     /**
386      * Ajax Event handler for the display options
387      * @memberOf    jQuery
388      * @name        displayOptionsForm_submit
389      * @see         $cfg['AjaxEnable']
390      */
391     $("#displayOptionsForm.ajax").live('submit', function(event) {
392         event.preventDefault();
394         $form = $(this);
396         $.post($form.attr('action'), $form.serialize() + '&ajax_request=true' , function(data) {
397             $("#sqlqueryresults")
398              .html(data)
399              .trigger('appendAnchor');
400             PMA_init_slider();
401         }) // end $.post()
402     })
403     //end displayOptionsForm handler
405     /**
406      * Ajax Event handlers for Inline Editing
407      */
409     /**
410      * On click, replace the fields of current row with an input/textarea
411      * @memberOf    jQuery
412      * @name        inline_edit_start
413      * @see         PMA_ajaxShowMessage()
414      * @see         getFieldName()
415      */
416     $(".inline_edit_anchor").live('click', function(event) {
417         /** @lends jQuery */
418         event.preventDefault();
419         $(this).removeClass('inline_edit_anchor').addClass('inline_edit_active');
421         // adding submit and hide buttons to inline edit <td>
422         // for "hide", button the original data to be restored is present in .original_data
423         // looping through all columns or rows, to find the required data and then storing it in an array.
425         var $this_children = $(this).children('span.nowrap').children('a').children('span.nowrap');
426         if (disp_mode != 'vertical') {
427             $this_children.empty();
428             $this_children.text(PMA_messages['strSave']);
429         } else {
430             // vertical 
431             data_vt = $this_children.html();
432             $this_children.text(PMA_messages['strSave']);
433         }
434        
435         var hide_link = '<br /><br /><a id="hide">' + PMA_messages['strHide'] + '</a>';
436         if (disp_mode != 'vertical') {
437             $(this).append(hide_link);
438             $('#table_results tbody tr td a#hide').click(function() {
439                 $this_children = $(this).siblings('span.nowrap').children('a').children('span.nowrap');
440                 $this_children.empty();
441                 $this_children.text(PMA_messages['strInlineEdit']);
443                 var $this_hide = $(this).parent();
444                 $this_hide.removeClass("inline_edit_active hover").addClass("inline_edit_anchor");
445                 $this_hide.parent().removeClass("hover");
446                 $this_hide.siblings().removeClass("hover");
448                 var last_column = $this_hide.siblings().length;
449                 var txt = '';
450                 for(var i = 4; i < last_column; i++) {
451                     if($this_hide.siblings("td:eq(" + i + ")").hasClass("inline_edit") == false) {
452                         continue;
453                     }
454                     txt = $this_hide.siblings("td:eq(" + i + ")").children(' .original_data').html();
455                     if($this_hide.siblings("td:eq(" + i + ")").children().length != 0) {
456                         $this_hide.siblings("td:eq(" + i + ")").empty();
457                         $this_hide.siblings("td:eq(" + i + ")").append(txt);
458                     }
459                 }
460                 $(this).prev().prev().remove();
461                 $(this).prev().remove();
462                 $(this).remove();
463             });
464         } else {
465             var txt = '';
466             var rows = $(this).parent().siblings().length;
468             $(this).append(hide_link);
469             $('#table_results tbody tr td a#hide').click(function() {
470                 var pos = $(this).parent().index();
471                 var $chg_submit = $(this).parent().children('span.nowrap').children('a').children('span.nowrap');
472                 $chg_submit.empty();
473                 $chg_submit.append(data_vt);
475                 var $this_row = $(this).parent().parent();
476                 if(parseInt(pos) % 2 == 0) {
477                     $this_row.siblings("tr:eq(3) td:eq(" + pos + ")").removeClass("odd edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_active").addClass("odd edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_anchor");
479                     $this_row.siblings("tr:eq(3) td:eq(" + pos + ")").removeClass("odd edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_active hover").addClass("odd edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_anchor");
480                 } else {
481                     $this_row.siblings("tr:eq(3) td:eq(" + pos + ")").removeClass("even edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_active").addClass("even edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_anchor");
483                     $this_row.siblings("tr:eq(3) td:eq(" + pos + ")").removeClass("even edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_active hover").addClass("even edit_row_anchor row_" + pos + " vpointer vmarker inline_edit_anchor");
484                 }
485                 for( var i = 6; i <= rows + 2; i++){
486                     if( $this_row.siblings("tr:eq(" + i + ") td:eq(" + pos + ")").hasClass("inline_edit") == false) {
487                         continue;
488                     }
489                     txt = $this_row.siblings("tr:eq(" + i + ") td:eq(" + pos + ") span.original_data").html();
490                     $this_row.siblings("tr:eq(" + i + ") td:eq(" + pos + ")").empty();
491                     $this_row.siblings("tr:eq(" + i + ") td:eq(" + pos + ")").append(txt);
492                 }
493                 $(this).prev().remove();
494                 $(this).prev().remove();
495                 $(this).remove();
496             });
497         }
499         // Initialize some variables
500         if(disp_mode == 'vertical') {
501             /**
502              * @var this_row_index  Index of the current <td> in the parent <tr>
503              *                      Current <td> is the inline edit anchor.
504              */
505             var this_row_index = $(this).index();
506             /**
507              * @var $input_siblings  Object referring to all inline editable events from same row
508              */
509             var $input_siblings = $(this).parents('tbody').find('tr').find('.inline_edit:nth('+this_row_index+')');
510             /**
511              * @var where_clause    String containing the WHERE clause to select this row
512              */
513             var where_clause = $(this).parents('tbody').find('tr').find('.where_clause:nth('+this_row_index+')').val();
514         }
515         // horizontal mode
516         else {
517             var this_row_index = $(this).parent().index();
518             var $input_siblings = $(this).parent('tr').find('.inline_edit');
519             var where_clause = $(this).parent('tr').find('.where_clause').val();
520         }
522         $input_siblings.each(function() {
523             /** @lends jQuery */
524             /**
525              * @var data_value  Current value of this field
526              */
527             var data_value = $(this).html();
529             // We need to retrieve the value from the server for truncated/relation fields
530             // Find the field name
532             /**
533              * @var this_field  Object referring to this field (<td>)
534              */
535             var $this_field = $(this);
536             /**
537              * @var field_name  String containing the name of this field.
538              * @see getFieldName()
539              */
540             var field_name = getFieldName($this_field, disp_mode);
541             /**
542              * @var relation_curr_value String current value of the field (for fields that are foreign keyed).
543              */
544             var relation_curr_value = $this_field.find('a').text();
545             /**
546              * @var curr_value String current value of the field (for fields that are of type enum or set).
547              */
548             var curr_value = $this_field.text();
550             if($this_field.is(':not(.not_null)')){
551                 // add a checkbox to mark null for all the field that are nullable.
552                 $this_field.html('<div class="null_div">Null :<input type="checkbox" class="checkbox_null_'+ field_name + '_' + this_row_index +'"></div>');
553                 // check the 'checkbox_null_<field_name>_<row_index>' if the corresponding value is null
554                 if($this_field.is('.null')) {
555                     $('.checkbox_null_' + field_name + '_' + this_row_index).attr('checked', true);
556                 }
558                 // if the select/editor is changed un-check the 'checkbox_null_<field_name>_<row_index>'.
559                 if ($this_field.is('.enum, .set')) {
560                     $this_field.find('select').live('change', function(e) {
561                         $('.checkbox_null_' + field_name + '_' + this_row_index).attr('checked', false);
562                     })
563                 } else if ($this_field.is('.relation')) {
564                     $this_field.find('select').live('change', function(e) {
565                         $('.checkbox_null_' + field_name + '_' + this_row_index).attr('checked', false);
566                     })
567                     $this_field.find('.browse_foreign').live('click', function(e) {
568                         $('.checkbox_null_' + field_name + '_' + this_row_index).attr('checked', false);
569                     })
570                 } else {
571                     $this_field.find('textarea').live('keypress', function(e) {
572                         $('.checkbox_null_' + field_name + '_' + this_row_index).attr('checked', false);
573                     })
574                 }
576                 // if 'chechbox_null_<field_name>_<row_index>' is clicked empty the corresponding select/editor.
577                 $('.checkbox_null_' + field_name + '_' + this_row_index).bind('click', function(e) {
578                     if ($this_field.is('.enum')) {
579                         $this_field.find('select').attr('value', '');
580                     } else if ($this_field.is('.set')) {
581                         $this_field.find('select').find('option').each(function() {
582                             var $option = $(this);
583                             $option.attr('selected', false);
584                         })
585                     } else if ($this_field.is('.relation')) {
586                         // if the dropdown is there to select the foreign value
587                         if ($this_field.find('select').length > 0) {
588                             $this_field.find('select').attr('value', '');
589                         // if foriegn value is selected by browsing foreing values
590                         } else {
591                             $this_field.find('span.curr_value').empty();
592                         }
593                     } else {
594                         $this_field.find('textarea').val('');
595                     }
596                 })
598             } else {
599                 $this_field.html('<div class="null_div"></div>');
600             }
602             // In each input sibling, wrap the current value in a textarea
603             // and store the current value in a hidden span
604             if($this_field.is(':not(.truncated, .transformed, .relation, .enum, .set, .null)')) {
605                 // handle non-truncated, non-transformed, non-relation values
606                 // We don't need to get any more data, just wrap the value
607                 $this_field.append('<textarea>'+data_value+'</textarea>');
608                 $this_field.append('<span class="original_data">'+data_value+'</span>');
609                 $(".original_data").hide();
610             }
611             else if($this_field.is('.truncated, .transformed')) {
612                 /** @lends jQuery */
613                 //handle truncated/transformed values values
615                 /**
616                  * @var sql_query   String containing the SQL query used to retrieve value of truncated/transformed data
617                  */
618                 var sql_query = 'SELECT ' + field_name + ' FROM ' + window.parent.table + ' WHERE ' + where_clause;
620                 // Make the Ajax call and get the data, wrap it and insert it
621                 $.post('sql.php', {
622                     'token' : window.parent.token,
623                     'db' : window.parent.db,
624                     'ajax_request' : true,
625                     'sql_query' : sql_query,
626                     'inline_edit' : true
627                 }, function(data) {
628                     if(data.success == true) {
629                         $this_field.append('<textarea>'+data.value+'</textarea>');
630                         $this_field.append('<span class="original_data">'+data_value+'</span>');
631                         $(".original_data").hide();
632                     }
633                     else {
634                         PMA_ajaxShowMessage(data.error);
635                     }
636                 }) // end $.post()
637             }
638             else if($this_field.is('.relation')) {
639                 /** @lends jQuery */
640                 //handle relations
642                 /**
643                  * @var post_params Object containing parameters for the POST request
644                  */
645                 var post_params = {
646                         'ajax_request' : true,
647                         'get_relational_values' : true,
648                         'db' : window.parent.db,
649                         'table' : window.parent.table,
650                         'column' : field_name,
651                         'token' : window.parent.token,
652                         'curr_value' : relation_curr_value
653                 }
655                 $.post('sql.php', post_params, function(data) {
656                     $this_field.append(data.dropdown);
657                     $this_field.append('<span class="original_data">'+data_value+'</span>');
658                     $(".original_data").hide();
659                 }) // end $.post()
660             }
661             else if($this_field.is('.enum')) {
662                 /** @lends jQuery */
663                 //handle enum fields
665                 /**
666                  * @var post_params Object containing parameters for the POST request
667                  */
668                 var post_params = {
669                         'ajax_request' : true,
670                         'get_enum_values' : true,
671                         'db' : window.parent.db,
672                         'table' : window.parent.table,
673                         'column' : field_name,
674                         'token' : window.parent.token,
675                         'curr_value' : curr_value
676                 }
678                 $.post('sql.php', post_params, function(data) {
679                     $this_field.append(data.dropdown);
680                     $this_field.append('<span class="original_data">'+data_value+'</span>');
681                     $(".original_data").hide();
682                 }) // end $.post()
683             }
684             else if($this_field.is('.set')) {
685                 /** @lends jQuery */
686                 //handle set fields
688                 /**
689                  * @var post_params Object containing parameters for the POST request
690                  */
691                 var post_params = {
692                         'ajax_request' : true,
693                         'get_set_values' : true,
694                         'db' : window.parent.db,
695                         'table' : window.parent.table,
696                         'column' : field_name,
697                         'token' : window.parent.token,
698                         'curr_value' : curr_value
699                 }
701                 $.post('sql.php', post_params, function(data) {
702                     $this_field.append(data.select);
703                     $this_field.append('<span class="original_data">'+data_value+'</span>');
704                     $(".original_data").hide();
705                 }) // end $.post()
706             }
707             else if($this_field.is('.null')) {
708                 //handle null fields
709                 $this_field.append('<textarea></textarea>');
710                 $this_field.append('<span class="original_data">NULL</span>');
711                 $(".original_data").hide();
712             }
713         })
714     }) // End On click, replace the current field with an input/textarea
716     /**
717      * After editing, clicking again should post data
718      *
719      * @memberOf    jQuery
720      * @name        inline_edit_save
721      * @see         PMA_ajaxShowMessage()
722      * @see         getFieldName()
723      */
724     $(".inline_edit_active span a").live('click', function(event) {
725         /** @lends jQuery */
727         event.preventDefault();
729         /**
730          * @var $this_td    Object referring to the td containing the
731          * "Inline Edit" link that was clicked to save the row that is
732          * being edited
733          *
734          */
735         var $this_td = $(this).parent().parent();
736         var $test_element = ''; // to test the presence of a element
738         // Initialize variables
739         if(disp_mode == 'vertical') {
740             /**
741              * @var this_td_index  Index of the current <td> in the parent <tr>
742              *                      Current <td> is the inline edit anchor.
743              */
744             var this_td_index = $this_td.index();
745             /**
746              * @var $input_siblings  Object referring to all inline editable events from same row
747              */
748             var $input_siblings = $this_td.parents('tbody').find('tr').find('.inline_edit:nth('+this_td_index+')');
749             /**
750              * @var where_clause    String containing the WHERE clause to select this row
751              */
752             var where_clause = $this_td.parents('tbody').find('tr').find('.where_clause:nth('+this_td_index+')').val();
753         } else {
754             var $input_siblings = $this_td.parent('tr').find('.inline_edit');
755             var where_clause = $this_td.parent('tr').find('.where_clause').val();
756         }
758         /**
759          * @var nonunique   Boolean, whether this row is unique or not
760          */
761         if($this_td.is('.nonunique')) {
762             var nonunique = 0;
763         }
764         else {
765             var nonunique = 1;
766         }
768         // Collect values of all fields to submit, we don't know which changed
769         /**
770          * @var relation_fields Array containing the name/value pairs of relational fields
771          */
772         var relation_fields = {};
773         /**
774          * @var transform_fields    Array containing the name/value pairs for transformed fields
775          */
776         var transform_fields = {};
777         /**
778          * @var transformation_fields   Boolean, if there are any transformed fields in this row
779          */
780         var transformation_fields = false;
782         /**
783          * @var sql_query String containing the SQL query to update this row
784          */
785         var sql_query = 'UPDATE `' + window.parent.table + '` SET ';
787         $input_siblings.each(function() {
788             /** @lends jQuery */
789             /**
790              * @var this_field  Object referring to this field (<td>)
791              */
792                 var $this_field = $(this);
793             /**
794              * @var field_name  String containing the name of this field.
795              * @see getFieldName()
796              */
797             var field_name = getFieldName($this_field, disp_mode);
799             /**
800              * @var this_field_params   Array temporary storage for the name/value of current field
801              */
802             var this_field_params = {};
804             if($this_field.is('.transformed')) {
805                 transformation_fields =  true;
806             }
807             /**
808              * @var is_null String capturing whether 'checkbox_null_<field_name>_<row_index>' is checked.
809              */
810             var is_null = $this_field.find('input:checkbox').is(':checked');
811             var value;
813             if (is_null) {
814                 sql_query += ' `' + field_name + "`=NULL , ";
815             } else {
816                 if($this_field.is(":not(.relation, .enum, .set)")) {
817                     this_field_params[field_name] = $this_field.find('textarea').val();
818                     if($this_field.is('.transformed')) {
819                         $.extend(transform_fields, this_field_params);
820                     }
821                 } else if ($this_field.is('.set')) {
822                     $test_element = $this_field.find('select');
823                     this_field_params[field_name] = $test_element.map(function(){
824                         return $(this).val();
825                     }).get().join(",");
826                 } else {
827                     // results from a drop-down
828                     $test_element = $this_field.find('select');
829                     if ($test_element.length != 0) {
830                         this_field_params[field_name] = $test_element.val();
831                     }
833                     // results from Browse foreign value
834                     $test_element = $this_field.find('span.curr_value');
835                     if ($test_element.length != 0) {
836                         this_field_params[field_name] = $test_element.text();
837                     }
839                     if($this_field.is('.relation')) {
840                         $.extend(relation_fields, this_field_params);
841                     }
842                 }
843                 sql_query += ' `' + field_name + "`='" + this_field_params[field_name].replace(/'/g, "''") + "' , ";
844             }
845         })
847         //Remove the last ',' appended in the above loop
848         sql_query = sql_query.replace(/,\s$/, '');
849         sql_query += ' WHERE ' + PMA_urldecode(where_clause);
851         /**
852          * @var rel_fields_list  String, url encoded representation of {@link relations_fields}
853          */
854         var rel_fields_list = $.param(relation_fields);
856         /**
857          * @var transform_fields_list  String, url encoded representation of {@link transform_fields}
858          */
859         var transform_fields_list = $.param(transform_fields);
861         // Make the Ajax post after setting all parameters
862         /**
863          * @var post_params Object containing parameters for the POST request
864          */
865         var post_params = {'ajax_request' : true,
866                             'sql_query' : sql_query,
867                             'disp_direction' : disp_mode,
868                             'token' : window.parent.token,
869                             'db' : window.parent.db,
870                             'table' : window.parent.table,
871                             'clause_is_unique' : nonunique,
872                             'where_clause' : where_clause,
873                             'rel_fields_list' : rel_fields_list,
874                             'do_transformations' : transformation_fields,
875                             'transform_fields_list' : transform_fields_list,
876                             'goto' : 'sql.php',
877                             'submit_type' : 'save'
878                           };
880         // if inline_edit is successful, we need to go back to default view
881          var $del_hide=$(this).parent();
882          var $chg_submit=$(this);
884         $.post('tbl_replace.php', post_params, function(data) {
885             if(data.success == true) {
886                 // deleting the hide button if my query was successful
887                 // remove <br><br><a> tags 
888                  for ( var i=0;i<=2;i++) { $del_hide.next().remove(); }
889                  if(disp_mode!='vertical'){
890                      $chg_submit.empty();
891                      $chg_submit.html('<span class="nowrap"></span>');
892                      $chg_submit.children('span.nowrap').text(PMA_messages['strInlineEdit']);
893                  }
894                  else {
895                      $chg_submit.children('span.nowrap').empty();
896                      $chg_submit.children('span.nowrap').append(data_vt);
897                  }
899                 PMA_ajaxShowMessage(data.message);
900                 $this_td.removeClass('inline_edit_active hover').addClass('inline_edit_anchor');
901                 $this_td.parent().removeClass('hover')
902                 $this_td.siblings().removeClass('hover');
904                 $input_siblings.each(function() {
905                     // Inline edit post has been successful.
906                     $this_sibling = $(this);
908                     var is_null = $this_sibling.find('input:checkbox').is(':checked');
909                     if (is_null) {
910                         $this_sibling.html('NULL');
911                         $this_sibling.addClass('null');
912                     } else {
913                         $this_sibling.removeClass('null');
914                         if($this_sibling.is(':not(.relation, .enum, .set)')) {
915                             /**
916                              * @var new_html    String containing value of the data field after edit
917                              */
918                             var new_html = $this_sibling.find('textarea').val();
920                             if($this_sibling.is('.transformed')) {
921                                 var field_name = getFieldName($this_sibling, disp_mode);
922                                 $.each(data.transformations, function(key, value) {
923                                     if(key == field_name) {
924                                         if($this_sibling.is('.text_plain, .application_octetstream')) {
925                                             new_html = value;
926                                             return false;
927                                         }
928                                         else {
929                                             var new_value = $this_sibling.find('textarea').val();
930                                             new_html = $(value).append(new_value);
931                                             return false;
932                                         }
933                                     }
934                                 })
935                             }
936                         }
937                         else {
938                             var new_html = '';
939                             var new_value = '';
940                             $test_element = $this_sibling.find('select');
941                             if ($test_element.length != 0) {
942                                 new_value = $test_element.val();
943                             }
945                             $test_element = $this_sibling.find('span.curr_value');
946                             if ($test_element.length != 0) {
947                                 new_value = $test_element.text();
948                             }
950                             if($this_sibling.is('.relation')) {
951                                 var field_name = getFieldName($this_sibling, disp_mode);
952                                 $.each(data.relations, function(key, value) {
953                                     if(key == field_name) {
954                                         new_html = $(value).append(new_value);
955                                         return false;
956                                     }
957                                 })
958                             } else if ($this_sibling.is('.enum')) {
959                                 new_html = new_value;
960                             } else if ($this_sibling.is('.set')) {
961                                 if (new_value != null) {
962                                     $.each(new_value, function(key, value) {
963                                         new_html = new_html + value + ',';
964                                     })
965                                     new_html = new_html.substring(0, new_html.length-1);
966                                 }
967                             }
968                         }
969                         $this_sibling.html(new_html);
970                     }
971                 })
972             }
973             else {
974                 PMA_ajaxShowMessage(data.error);
975             };
976         }) // end $.post()
977     }) // End After editing, clicking again should post data
978 }, 'top.frame_content') // end $(document).ready()
981  * Starting from some th, change the class of all td under it
982  */
983 function PMA_changeClassForColumn($this_th, newclass) {
984     // index 0 is the th containing the big T
985     var th_index = $this_th.index();
986     // .eq() is zero-based
987     th_index--;
988     var $tds = $this_th.closest('table').find('tbody tr').find('td.data:eq('+th_index+')');
989     if ($this_th.data('has_class_'+newclass)) {
990         $tds.removeClass(newclass);
991         $this_th.data('has_class_'+newclass, false);
992     } else {
993         $tds.addClass(newclass);
994         $this_th.data('has_class_'+newclass, true);
995     }
998 $(document).ready(function() {
1000     $('.browse_foreign').live('click', function(e) {
1001         e.preventDefault();
1002         window.open(this.href, 'foreigners', 'width=640,height=240,scrollbars=yes,resizable=yes');
1003         $anchor = $(this);
1004         $anchor.addClass('browse_foreign_clicked');
1005         return false;
1006     });
1008     /**
1009      * vertical column highlighting in horizontal mode when hovering over the column header
1010      */
1011     $('.column_heading').live('hover', function() {
1012         PMA_changeClassForColumn($(this), 'hover');
1013         });
1015     /**
1016      * vertical column marking in horizontal mode when clicking the column header
1017      */
1018     $('.column_heading').live('click', function() {
1019         PMA_changeClassForColumn($(this), 'marked');
1020         });
1023 /**#@- */