2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 // Sets local POST value and adds Value to ajax posting if needed
15 /*function set_post($name, $value, $ajax_trigger=true) {
18 $_POST[$name] = $value;
19 if ($ajax_trigger) $Ajax->activate($name);
22 //------------------------------------------------------------------------------
23 // Seek for _POST variable with $prefix.
24 // If var is found returns variable name with prefix stripped,
25 // and null or -1 otherwise.
27 function find_submit($prefix, $numeric=true)
30 foreach($_POST as $postkey=>$postval )
32 if (strpos($postkey, $prefix) === 0)
34 $id = substr($postkey, strlen($prefix));
35 return $numeric ? (int)$id : $id;
38 return $numeric ? -1 : null;
40 //------------------------------------------------------------------------------
42 // Helper function for simple db table editor pages
44 function simple_page_mode($numeric_id = true)
46 global $Ajax, $Mode, $selected_id;
48 $default = $numeric_id ? -1 : '';
49 $selected_id = get_post('selected_id', $default);
50 foreach (array('ADD_ITEM', 'UPDATE_ITEM', 'RESET') as $m) {
51 if (isset($_POST[$m])) {
52 $Ajax->activate('_page_body');
54 $selected_id = $default;
58 foreach (array('Edit', 'Delete') as $m) {
59 foreach ($_POST as $p => $pvar) {
60 if (strpos($p, $m) === 0) {
61 // $selected_id = strtr(substr($p, strlen($m)), array('%2E'=>'.'));
62 unset($_POST['_focus']); // focus on first form entry
63 $selected_id = quoted_printable_decode(substr($p, strlen($m)));
64 $Ajax->activate('_page_body');
73 //------------------------------------------------------------------------------
75 // Read numeric value from user formatted input
77 function input_num($postname=null, $dflt=null)
79 if (!isset($_POST[$postname]) || $_POST[$postname] == "")
82 return user_numeric($_POST[$postname]);
85 //---------------------------------------------------------------------------------
87 function hidden($name, $value=null, $echo=true)
92 $value = get_post($name);
94 $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
95 $Ajax->addUpdate($name, $name, $value);
102 Universal submit form button.
103 $atype - type of submit:
105 false - normal button; optional icon
106 null - button visible only in fallback mode; optional icon
108 true - standard button; optional icon
109 'process' - displays progress bar during call; optional icon
110 'default' - default form submit on Ctrl-Enter press; dflt ICON_OK icon
111 'selector' - ditto with closing current popup editor window
112 'cancel' - cancel form entry on Escape press; dflt ICON_CANCEL
114 function submit($name, $value, $echo=true, $title=false, $atype=false, $icon=false)
116 global $path_to_root;
119 if (!is_bool($atype)) // necessary: switch uses '=='
122 $aspect = " aspect='process'"; break;
124 $aspect = " aspect='selector' rel = '$value'";
125 $value = _("Select");
126 if ($icon===false) $icon=ICON_SUBMIT; break;
128 $aspect = " aspect='default'";
129 if ($icon===false) $icon=ICON_SUBMIT; break;
131 $aspect = " aspect='cancel'";
132 if ($icon===false) $icon=ICON_ESCAPE; break;
134 $aspect = fallback_mode() ?
135 " aspect='fallback'" : " style='display:none;'"; break;
138 default_focus($name);
139 $submit_str = "<button class=\""
140 .($atype ? 'ajaxsubmit' : 'inputsubmit')
141 ."\" type=\"submit\""
143 ." name=\"$name\" id=\"$name\" value=\"$value\""
144 .($title ? " title='$title'" : '')
146 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon' height='12'>" : '')
147 ."<span>$value</span>"
155 function submit_center($name, $value, $echo=true, $title=false, $async=false, $icon=false)
158 submit($name, $value, $echo, $title, $async, $icon);
162 function submit_center_first($name, $value, $title=false, $async=false, $icon=false)
165 submit($name, $value, true, $title, $async, $icon);
169 function submit_center_last($name, $value, $title=false, $async=false, $icon=false)
172 submit($name, $value, true, $title, $async, $icon);
176 For following controls:
177 'both' - use both Ctrl-Enter and Escape hotkeys
178 'cancel' - apply to 'RESET' button
180 function submit_add_or_update($add=true, $title=false, $async=false)
184 if ($async === 'both') {
185 $async = 'default'; $cancel = 'cancel';
187 else if ($async === 'default')
189 else if ($async === 'cancel')
193 submit('ADD_ITEM', _("Add new"), true, $title, $async);
195 submit('UPDATE_ITEM', _("Update"), true, $title, $async);
196 submit('RESET', _("Cancel"), true, $title, $cancel);
200 function submit_add_or_update_center($add=true, $title=false, $async=false)
203 submit_add_or_update($add, $title, $async);
207 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false)
211 echo "<td> </td>\n";
213 submit_add_or_update($add, $title, $async);
217 function submit_cells($name, $value, $extra="", $title=false, $async=false)
220 submit($name, $value, true, $title, $async);
224 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
228 echo "<td> </td>\n";
229 submit_cells($name, $value, $extra, $title, $async);
233 function submit_return($name, $value, $title=false)
235 if (@$_REQUEST['popup']) {
236 submit($name, $value, true, $title, 'selector');
240 function submit_js_confirm($name, $msg) {
242 "_validate.$name=function(){ return confirm('$msg');};");
244 //-----------------------------------------------------------------------------------
246 function set_icon($icon, $title=false)
248 global $path_to_root;
249 return "<img src='$path_to_root/themes/".user_theme()."/images/$icon' width='12' height='12' border='0'".($title ? " title='$title'" : "")." />\n";
252 function button($name, $value, $title=false, $icon=false, $aspect='')
254 // php silently changes dots,spaces,'[' and characters 128-159
255 // to underscore in POST names, to maintain compatibility with register_globals
257 if ($aspect == 'selector') {
258 $rel = " rel='$value'";
259 $value = _("Select");
261 if (user_graphic_links() && $icon)
263 if ($value == _("Delete")) // Helper during implementation
265 return "<button type='submit' class='editbutton' name='".
266 htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))).
267 "' value='1'" . ($title ? " title='$title'":" title='$value'")
268 . ($aspect ? " aspect='$aspect'" : '')
270 ." />".set_icon($icon)."\n";
273 return "<input type='submit' class='editbutton' name='"
274 .htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B')))
276 .($title ? " title='$title'":'')
277 . ($aspect ? " aspect='$aspect'" : '')
282 function button_cell($name, $value, $title=false, $icon=false, $aspect='')
284 echo "<td align='center'>";
285 echo button($name, $value, $title, $icon, $aspect);
289 function delete_button_cell($name, $value, $title=false)
291 button_cell($name, $value, $title, ICON_DELETE);
294 function edit_button_cell($name, $value, $title=false)
296 button_cell($name, $value, $title, ICON_EDIT);
299 function select_button_cell($name, $value, $title=false)
301 button_cell($name, $value, $title, ICON_ADD, 'selector');
303 //-----------------------------------------------------------------------------------
305 function check_value($name)
307 if (!isset($_POST[$name]))
312 function checkbox($label, $name, $value=null, $submit_on_change=false, $title=false)
317 default_focus($name);
319 $str .= $label . " ";
320 if ($submit_on_change !== false) {
321 if ($submit_on_change === true)
323 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
326 $value = get_post($name,0);
329 .($value == 1 ? ' checked':'')
330 ." type='checkbox' name='$name' value='1'"
331 .($submit_on_change ? " onclick='$submit_on_change'" : '')
332 .($title ? " title='$title'" : '')
335 $Ajax->addUpdate($name, $name, $value);
339 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
341 echo checkbox($label, $name, $value, $submit_on_change, $title);
344 function check_cells($label, $name, $value, $submit_on_change=false, $title=false,
348 echo "<td>$label</td>\n";
350 echo check(null, $name, $value, $submit_on_change, $title);
354 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
357 echo check_cells($label, $name, $value, $submit_on_change, $title);
361 //-----------------------------------------------------------------------------------
363 function labelheader_cell($label, $params="")
365 echo "<td class='tableheader' $params>$label</td>\n";
368 function label_cell($label, $params="", $id=null)
374 $params .= " id='$id'";
375 $Ajax->addUpdate($id, $id, $label);
377 echo "<td $params>$label</td>\n";
382 function email_cell($label, $params="", $id=null)
384 label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
387 function amount_cell($label, $bold=false, $params="", $id=null)
390 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
392 label_cell(price_format($label), "nowrap align=right ".$params, $id);
395 function percent_cell($label, $bold=false, $id=null)
398 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
400 label_cell(percent_format($label), "nowrap align=right", $id);
402 // 2008-06-15. Changed
403 function qty_cell($label, $bold=false, $dec=null, $id=null)
406 $dec = get_qty_dec();
408 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
410 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
413 function label_cells($label, $value, $params="", $params2="", $id='')
416 echo "<td $params>$label</td>\n";
417 label_cell($value, $params2, $id);
420 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
423 label_cells($label, $value, $params, $params2, $id);
425 echo "<td colspan=$leftfill></td>";
429 //-----------------------------------------------------------------------------------
431 function text_cells($label, $name, $value=null, $size="", $max="", $title=false,
432 $labparams="", $post_label="", $inparams="")
436 default_focus($name);
438 label_cell($label, $labparams);
442 $value = get_post($name);
443 echo "<input $inparams type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
444 .($title ? " title='$title'" : '')
447 if ($post_label != "")
448 echo " " . $post_label;
451 $Ajax->addUpdate($name, $name, $value);
454 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null,
455 $labparams=null, $post_label=null, $submit_on_change=false)
459 default_focus($name);
460 if (!isset($_POST[$name]) || $_POST[$name] == "")
463 $_POST[$name] = $init;
468 label_cell($label, $labparams);
474 $class = $submit_on_change ? 'class="searchbox"' : '';
475 echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
476 .($title ? " title='$title'": '')." >";
479 echo " " . $post_label;
482 $Ajax->addUpdate($name, $name, $_POST[$name]);
485 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
489 text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
494 //-----------------------------------------------------------------------------------
496 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
500 text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
505 //-----------------------------------------------------------------------------------
506 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
509 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
510 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
513 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
516 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
517 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
520 function link_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
522 $val = get_post($name);
524 if (strpos($val,'http://')===false)
525 $val = 'http://'.$val;
526 $label = "<a href='$val' target='_blank'>$label</a>";
528 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
531 function link_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
533 $val = get_post($name);
535 if (strpos($val,'http://')===false)
536 $val = 'http://'.$val;
537 $label = "<a href='$val' target='_blank'>$label</a>";
539 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
542 //-----------------------------------------------------------------------------------
544 // Since FA 2.2 $init parameter is superseded by $check.
545 // When $check!=null current date is displayed in red when set to other
546 // than current date.
548 function date_cells($label, $name, $title = null, $check=null, $inc_days=0,
549 $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
551 global $use_date_picker, $path_to_root, $Ajax;
553 if (!isset($_POST[$name]) || $_POST[$name] == "")
555 if ($inc_years == 1001)
556 $_POST[$name] = null;
561 $dd = add_days($dd, $inc_days);
562 if ($inc_months != 0)
563 $dd = add_months($dd, $inc_months);
565 $dd = add_years($dd, $inc_years);
569 if ($use_date_picker)
570 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
571 . " <img src='$path_to_root/themes/default/images/cal.gif' width='16' height='16' border='0' alt='"._('Click Here to Pick up the date')."'></a>\n";
576 label_cell($label, $params);
580 $class = $submit_on_change ? 'class="searchbox"' : '';
582 $aspect = $check ? 'aspect="cdate"' : '';
583 if ($check && (get_post($name) != Today()))
584 $aspect .= ' style="color:#FF0000"';
586 echo "<input type=\"text\" name=\"$name\" $class $aspect size=\"9\" maxlength=\"12\" value=\""
587 . $_POST[$name]. "\""
588 .($title ? " title='$title'": '')." > $post_label";
590 $Ajax->addUpdate($name, $name, $_POST[$name]);
593 function date_row($label, $name, $title=null, $check=null, $inc_days=0, $inc_months=0,
594 $inc_years=0, $params=null, $submit_on_change=false)
597 date_cells($label, $name, $title, $check, $inc_days, $inc_months,
598 $inc_years, $params, $submit_on_change);
602 //-----------------------------------------------------------------------------------
604 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
606 text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
609 //-----------------------------------------------------------------------------------
611 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
614 ref_cells($label, $name, $title, $init, null, $submit_on_change);
618 //-----------------------------------------------------------------------------------
620 function percent_row($label, $name, $init=null)
623 if (!isset($_POST[$name]) || $_POST[$name]=="")
625 $_POST[$name] = $init == null ? '' : $init;
628 small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
631 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
636 $dec = user_price_dec();
637 if (!isset($_POST[$name]) || $_POST[$name] == "")
640 $_POST[$name] = $init;
645 label_cell($label, $params);
653 echo "<td align='right'>";
655 echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
658 echo "<span id='_{$name}_label'> $post_label</span>";
659 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
662 $Ajax->addUpdate($name, $name, $_POST[$name]);
663 $Ajax->addAssign($name, $name, 'dec', $dec);
667 //-----------------------------------------------------------------------------------
669 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
671 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
674 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
677 amount_cells($label, $name, $init, $params, $post_label, $dec);
681 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
684 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
688 //-----------------------------------------------------------------------------------
690 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
693 $dec = user_qty_dec();
695 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
698 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
701 $dec = user_qty_dec();
704 amount_cells($label, $name, $init, $params, $post_label, $dec);
708 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
711 $dec = user_qty_dec();
714 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
718 //-----------------------------------------------------------------------------------
720 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
722 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
725 //-----------------------------------------------------------------------------------
727 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
730 $dec = user_qty_dec();
731 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
734 //-----------------------------------------------------------------------------------
736 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
740 default_focus($name);
742 echo "<td $params>$label</td>\n";
744 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
745 echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
746 .($title ? " title='$title'" : '')
747 .">$value</textarea></td>\n";
748 $Ajax->addUpdate($name, $name, $value);
751 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
754 textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
758 //-----------------------------------------------------------------------------------
760 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
764 default_focus($name);
765 echo "<tr><td>$label</td>\n";
769 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
770 echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\"> ";
772 submit($input_name, $input_value);
775 $Ajax->addUpdate($name, $name, $value);
778 //-----------------------------------------------------------------------------------
780 // When show_inactive page option is set
781 // displays value of inactive field as checkbox cell.
782 // Also updates database record after status change.
784 function inactive_control_cell($id, $value, $table, $key)
788 $name = "Inactive". $id;
789 $value = $value ? 1:0;
791 if (check_value('show_inactive')) {
792 if (isset($_POST['LInact'][$id]) && (get_post('_Inactive'.$id.'_update') ||
793 get_post('Update')) && (check_value('Inactive'.$id) != $value)) {
794 update_record_status($id, !$value, $table, $key);
796 echo '<td align="center">'. checkbox(null, $name, $value, true, '', "align='center'")
797 . hidden("LInact[$id]", $value, false) . '</td>';
801 // Displays controls for optional display of inactive records
803 function inactive_control_row($th) {
804 echo "<tr><td colspan=".(count($th)).">"
805 ."<div style='float:left;'>"
806 . checkbox(null, 'show_inactive', null, true). _("Show also Inactive")
807 ."</div><div style='float:right;'>"
808 . submit('Update', _('Update'), false, '', null)
812 // Inserts additional column header when display of inactive records is on.
814 function inactive_control_column(&$th) {
817 if (check_value('show_inactive'))
818 array_insert($th, count($th)-2 , _("Inactive"));
819 if (get_post('_show_inactive_update')) {
820 $Ajax->activate('_page_body');