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', 'CLONE') as $m) {
51 if (isset($_POST[$m])) {
52 $Ajax->activate('_page_body');
53 if ($m == 'RESET' || $m == 'CLONE')
54 $selected_id = $default;
55 unset($_POST['_focus']);
59 foreach (array('Edit', 'Delete') as $m) {
60 foreach ($_POST as $p => $pvar) {
61 if (strpos($p, $m) === 0) {
62 // $selected_id = strtr(substr($p, strlen($m)), array('%2E'=>'.'));
63 unset($_POST['_focus']); // focus on first form entry
64 $selected_id = quoted_printable_decode(substr($p, strlen($m)));
65 $Ajax->activate('_page_body');
74 //------------------------------------------------------------------------------
76 // Read numeric value from user formatted input
78 function input_num($postname=null, $dflt=0)
80 if (!isset($_POST[$postname]) || $_POST[$postname] == "")
83 return user_numeric($_POST[$postname]);
86 //---------------------------------------------------------------------------------
88 function hidden($name, $value=null, $echo=true)
93 $value = get_post($name);
95 $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
96 $Ajax->addUpdate($name, $name, $value);
103 Universal submit form button.
104 $atype - type of submit:
106 false - normal button; optional icon
107 null - button visible only in fallback mode; optional icon
109 true - standard button; optional icon
110 'process' - displays progress bar during call; optional icon
111 'default' - default form submit on Ctrl-Enter press; dflt ICON_OK icon
112 'selector' - ditto with closing current popup editor window
113 'cancel' - cancel form entry on Escape press; dflt ICON_CANCEL
115 function submit($name, $value, $echo=true, $title=false, $atype=false, $icon=false)
117 global $path_to_root;
120 if (!is_bool($atype)) // necessary: switch uses '=='
123 $aspect = " aspect='process'"; break;
125 $aspect = " aspect='selector' rel = '$value'";
126 $value = _("Select");
127 if ($icon===false) $icon=ICON_SUBMIT; break;
129 $aspect = " aspect='default'";
130 if ($icon===false) $icon=ICON_SUBMIT; break;
132 $aspect = " aspect='cancel'";
133 if ($icon===false) $icon=ICON_ESCAPE; break;
135 $aspect = fallback_mode() ?
136 " aspect='fallback'" : " style='display:none;'"; break;
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)
157 if ($echo) echo "<center>";
158 submit($name, $value, $echo, $title, $async, $icon);
159 if ($echo) echo "</center>";
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, $clone=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, _('Submit changes'), $async);
196 if ($clone) submit('CLONE', _("Clone"), true,
197 _('Edit new record with current data'), $async);
198 submit('RESET', _("Cancel"), true, _('Cancel edition'), $cancel);
202 function submit_add_or_update_center($add=true, $title=false, $async=false, $clone=false)
205 submit_add_or_update($add, $title, $async, $clone);
209 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false, $clone = false)
213 echo "<td> </td>\n";
215 submit_add_or_update($add, $title, $async, $clone);
219 function submit_cells($name, $value, $extra="", $title=false, $async=false)
222 submit($name, $value, true, $title, $async);
226 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
230 echo "<td> </td>\n";
231 submit_cells($name, $value, $extra, $title, $async);
235 function submit_return($name, $value, $title=false)
237 if (@$_REQUEST['popup']) {
238 submit($name, $value, true, $title, 'selector');
242 function submit_js_confirm($name, $msg) {
244 "_validate.$name=function(){ return confirm('"
245 . strtr($msg, array("\n"=>'\\n')) . "');};");
247 //-----------------------------------------------------------------------------------
249 function set_icon($icon, $title=false)
251 global $path_to_root;
252 return "<img src='$path_to_root/themes/".user_theme()."/images/$icon' width='12' height='12' border='0'".($title ? " title='$title'" : "")." />\n";
255 function button($name, $value, $title=false, $icon=false, $aspect='')
257 // php silently changes dots,spaces,'[' and characters 128-159
258 // to underscore in POST names, to maintain compatibility with register_globals
260 if ($aspect == 'selector') {
261 $rel = " rel='$value'";
262 $value = _("Select");
264 if (user_graphic_links() && $icon)
266 if ($value == _("Delete")) // Helper during implementation
268 return "<button type='submit' class='editbutton' name='".
269 htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))).
270 "' value='1'" . ($title ? " title='$title'":" title='$value'")
271 . ($aspect ? " aspect='$aspect'" : '')
273 ." />".set_icon($icon)."</button>\n";
276 return "<input type='submit' class='editbutton' name='"
277 .htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B')))
279 .($title ? " title='$title'":'')
280 . ($aspect ? " aspect='$aspect'" : '')
285 function button_cell($name, $value, $title=false, $icon=false, $aspect='')
287 echo "<td align='center'>";
288 echo button($name, $value, $title, $icon, $aspect);
292 function delete_button_cell($name, $value, $title=false)
294 button_cell($name, $value, $title, ICON_DELETE);
297 function edit_button_cell($name, $value, $title=false)
299 button_cell($name, $value, $title, ICON_EDIT);
302 function select_button_cell($name, $value, $title=false)
304 button_cell($name, $value, $title, ICON_ADD, 'selector');
306 //-----------------------------------------------------------------------------------
308 function check_value($name)
310 if (!isset($_POST[$name]))
315 function checkbox($label, $name, $value=null, $submit_on_change=false, $title=false)
322 $str .= $label . " ";
323 if ($submit_on_change !== false) {
324 if ($submit_on_change === true)
326 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
329 $value = get_post($name,0);
332 .($value == 1 ? ' checked':'')
333 ." type='checkbox' name='$name' value='1'"
334 .($submit_on_change ? " onclick='$submit_on_change'" : '')
335 .($title ? " title='$title'" : '')
338 $Ajax->addUpdate($name, $name, $value);
342 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
344 echo checkbox($label, $name, $value, $submit_on_change, $title);
347 function check_cells($label, $name, $value=null, $submit_on_change=false, $title=false,
351 echo "<td>$label</td>\n";
353 echo check(null, $name, $value, $submit_on_change, $title);
357 function check_row($label, $name, $value=null, $submit_on_change=false, $title=false)
359 echo "<tr><td class='label'>$label</td>";
360 echo check_cells(NULL, $name, $value, $submit_on_change, $title);
364 //-----------------------------------------------------------------------------------
365 function radio($label, $name, $value, $selected, $submit_on_change=false) {
367 if ($submit_on_change === true)
369 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
371 return "<input type='radio' name=$name value='$value' ".($selected ? "checked":'')
372 .($submit_on_change ? " onclick='$submit_on_change'" : '')
373 .">".($label ? $label : '');
376 //-----------------------------------------------------------------------------------
377 function labelheader_cell($label, $params="")
379 echo "<td class='tableheader' $params>$label</td>\n";
382 function label_cell($label, $params="", $id=null)
388 $params .= " id='$id'";
389 $Ajax->addUpdate($id, $id, $label);
391 echo "<td $params>$label</td>\n";
396 function email_cell($label, $params="", $id=null)
398 label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
401 function amount_decimal_cell($label, $params="", $id=null)
404 label_cell(price_decimal_format($label, $dec), "nowrap align=right ".$params, $id);
407 function amount_cell($label, $bold=false, $params="", $id=null)
410 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
412 label_cell(price_format($label), "nowrap align=right ".$params, $id);
415 //JAM Allow entered unit prices to be fractional
416 function unit_amount_cell($label, $bold=false, $params="", $id=null)
419 label_cell("<b>".unit_price_format($label)."</b>", "nowrap align=right ".$params, $id);
421 label_cell(unit_price_format($label), "nowrap align=right ".$params, $id);
425 function percent_cell($label, $bold=false, $id=null)
428 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
430 label_cell(percent_format($label), "nowrap align=right", $id);
432 // 2008-06-15. Changed
433 function qty_cell($label, $bold=false, $dec=null, $id=null)
436 $dec = get_qty_dec();
438 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
440 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
443 function label_cells($label, $value, $params="", $params2="", $id='')
446 echo "<td $params>$label</td>\n";
447 label_cell($value, $params2, $id);
450 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
455 echo "<td class='label'>$label</td>";
458 label_cells($label, $value, $params, $params2, $id);
460 echo "<td colspan=$leftfill></td>";
464 //-----------------------------------------------------------------------------------
466 function text_cells($label, $name, $value=null, $size="", $max="", $title=false,
467 $labparams="", $post_label="", $inparams="")
471 default_focus($name);
473 label_cell($label, $labparams);
477 $value = get_post($name);
478 echo "<input $inparams type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
479 .($title ? " title='$title'" : '')
482 if ($post_label != "")
483 echo " " . $post_label;
486 $Ajax->addUpdate($name, $name, $value);
489 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null,
490 $labparams=null, $post_label=null, $submit_on_change=false)
494 default_focus($name);
495 if (!isset($_POST[$name]) || $_POST[$name] == "")
498 $_POST[$name] = $init;
503 label_cell($label, $labparams);
509 $class = $submit_on_change ? 'class="searchbox"' : '';
510 echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
511 .($title ? " title='$title'": '')." >";
514 echo " " . $post_label;
517 $Ajax->addUpdate($name, $name, $_POST[$name]);
520 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
522 echo "<tr><td class='label'>$label</td>";
523 text_cells(null, $name, $value, $size, $max, $title, $params, $post_label);
528 //-----------------------------------------------------------------------------------
530 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
532 echo "<tr><td class='label'>$label</td>";
533 text_cells_ex(null, $name, $size, $max, $value, $title, $params, $post_label);
538 //-----------------------------------------------------------------------------------
539 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
542 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
543 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
546 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
549 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
550 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
553 function link_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
555 $val = get_post($name);
557 if (strpos($val,'http://')===false)
558 $val = 'http://'.$val;
559 $label = "<a href='$val' target='_blank'>$label</a>";
561 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
564 function link_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
566 $val = get_post($name);
568 if (strpos($val,'http://')===false)
569 $val = 'http://'.$val;
570 $label = "<a href='$val' target='_blank'>$label</a>";
572 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
575 //-----------------------------------------------------------------------------------
577 // Since FA 2.2 $init parameter is superseded by $check.
578 // When $check!=null current date is displayed in red when set to other
579 // than current date.
581 function date_cells($label, $name, $title = null, $check=null, $inc_days=0,
582 $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
584 global $use_date_picker, $path_to_root, $Ajax;
586 if (!isset($_POST[$name]) || $_POST[$name] == "")
588 if ($inc_years == 1001)
589 $_POST[$name] = null;
594 $dd = add_days($dd, $inc_days);
595 if ($inc_months != 0)
596 $dd = add_months($dd, $inc_months);
598 $dd = add_years($dd, $inc_years);
602 if ($use_date_picker)
603 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
604 . " <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";
609 label_cell($label, $params);
613 $class = $submit_on_change ? 'class="searchbox"' : '';
615 $aspect = $check ? 'aspect="cdate"' : '';
616 if ($check && (get_post($name) != Today()))
617 $aspect .= ' style="color:#FF0000"';
619 default_focus($name);
621 echo "<input type=\"text\" name=\"$name\" $class $aspect size=\"9\" maxlength=\"12\" value=\""
622 . $_POST[$name]. "\""
623 .($title ? " title='$title'": '')." > $post_label";
625 $Ajax->addUpdate($name, $name, $_POST[$name]);
628 function date_row($label, $name, $title=null, $check=null, $inc_days=0, $inc_months=0,
629 $inc_years=0, $params=null, $submit_on_change=false)
631 echo "<tr><td class='label'>$label</td>";
632 date_cells(null, $name, $title, $check, $inc_days, $inc_months,
633 $inc_years, $params, $submit_on_change);
637 //-----------------------------------------------------------------------------------
638 function password_row($label, $name, $value)
640 echo "<tr><td class='label'>$label</td>";
641 label_cell("<input type='password' name='$name' size=20 maxlength=20 value='$value' />");
645 //-----------------------------------------------------------------------------------
646 function file_cells($label, $name, $id="")
650 label_cells($label, "<input type='file' name='$name' $id />");
652 function file_row($label, $name, $id = "")
654 echo "<tr><td class='label'>$label</td>";
655 file_cells(null, $name, $id);
659 //-----------------------------------------------------------------------------------
661 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
663 text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
666 //-----------------------------------------------------------------------------------
668 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
670 echo "<tr><td class='label'>$label</td>";
671 ref_cells(null, $name, $title, $init, null, $submit_on_change);
675 //-----------------------------------------------------------------------------------
677 function percent_row($label, $name, $init=null)
680 if (!isset($_POST[$name]) || $_POST[$name]=="")
682 $_POST[$name] = $init == null ? '' : $init;
685 small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
688 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
693 $dec = user_price_dec();
694 if (!isset($_POST[$name]) || $_POST[$name] == "")
697 $_POST[$name] = $init;
704 $params = "class='label'";
705 label_cell($label, $params);
713 echo "<td align='right'>";
715 echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
718 echo "<span id='_{$name}_label'> $post_label</span>";
719 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
722 $Ajax->addUpdate($name, $name, $_POST[$name]);
723 $Ajax->addAssign($name, $name, 'dec', $dec);
727 //-----------------------------------------------------------------------------------
729 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
731 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
734 //JAM Allow entered unit prices to be fractional
735 function unit_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
738 $dec = user_price_dec()+2;
740 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec+2);
743 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
746 amount_cells($label, $name, $init, $params, $post_label, $dec);
750 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
753 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
757 //-----------------------------------------------------------------------------------
759 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
762 $dec = user_qty_dec();
764 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
767 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
770 $dec = user_qty_dec();
773 amount_cells($label, $name, $init, $params, $post_label, $dec);
777 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
780 $dec = user_qty_dec();
783 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
787 //-----------------------------------------------------------------------------------
789 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
791 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
794 //-----------------------------------------------------------------------------------
796 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
799 $dec = user_qty_dec();
800 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
803 //-----------------------------------------------------------------------------------
805 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
809 default_focus($name);
811 echo "<td $params>$label</td>\n";
813 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
814 echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
815 .($title ? " title='$title'" : '')
816 .">$value</textarea></td>\n";
817 $Ajax->addUpdate($name, $name, $value);
820 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
822 echo "<tr><td class='label'>$label</td>";
823 textarea_cells(null, $name, $value, $cols, $rows, $title, $params);
827 //-----------------------------------------------------------------------------------
829 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
833 default_focus($name);
834 echo "<tr><td>$label</td>\n";
838 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
839 echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\"> ";
841 submit($input_name, $input_value);
844 $Ajax->addUpdate($name, $name, $value);
847 //-----------------------------------------------------------------------------------
849 // When show_inactive page option is set
850 // displays value of inactive field as checkbox cell.
851 // Also updates database record after status change.
853 function inactive_control_cell($id, $value, $table, $key)
857 $name = "Inactive". $id;
858 $value = $value ? 1:0;
860 if (check_value('show_inactive')) {
861 if (isset($_POST['LInact'][$id]) && (get_post('_Inactive'.$id.'_update') ||
862 get_post('Update')) && (check_value('Inactive'.$id) != $value)) {
863 update_record_status($id, !$value, $table, $key);
865 echo '<td align="center">'. checkbox(null, $name, $value, true, '', "align='center'")
866 . hidden("LInact[$id]", $value, false) . '</td>';
870 // Displays controls for optional display of inactive records
872 function inactive_control_row($th) {
873 echo "<tr><td colspan=".(count($th)).">"
874 ."<div style='float:left;'>"
875 . checkbox(null, 'show_inactive', null, true). _("Show also Inactive")
876 ."</div><div style='float:right;'>"
877 . submit('Update', _('Update'), false, '', null)
881 // Inserts additional column header when display of inactive records is on.
883 function inactive_control_column(&$th) {
886 if (check_value('show_inactive'))
887 array_insert($th, count($th)-2 , _("Inactive"));
888 if (get_post('_show_inactive_update')) {
889 $Ajax->activate('_page_body');