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
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
114 'process' - displays progress bar during call; optional icon
116 $atype can contain also multiply type selectors separated by space,
117 however make sense only combination of 'process' and one of defualt/selector/cancel
119 function submit($name, $value, $echo=true, $title=false, $atype=false, $icon=false)
121 global $path_to_root;
124 if ($atype === null) {
125 $aspect = fallback_mode() ? " aspect='fallback'" : " style='display:none;'";
127 } elseif (!is_bool($atype)) { // necessary: switch uses '=='
129 $aspect = "aspect='$atype' ";
130 $types = explode(' ', $atype);
132 foreach ($types as $type) {
135 $aspect = " aspect='selector' rel = '$value'";
136 $value = _("Select");
137 if ($icon===false) $icon=ICON_SUBMIT; break;
140 if ($icon===false) $icon=ICON_SUBMIT; break;
143 if ($icon===false) $icon=ICON_ESCAPE; break;
147 $submit_str = "<button class=\""
148 .($atype ? 'ajaxsubmit' : 'inputsubmit')
149 ."\" type=\"submit\""
151 ." name=\"$name\" id=\"$name\" value=\"$value\""
152 .($title ? " title='$title'" : '')
154 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon' height='12'>" : '')
155 ."<span>$value</span>"
163 function submit_center($name, $value, $echo=true, $title=false, $async=false, $icon=false)
165 if ($echo) echo "<center>";
166 submit($name, $value, $echo, $title, $async, $icon);
167 if ($echo) echo "</center>";
170 function submit_center_first($name, $value, $title=false, $async=false, $icon=false)
173 submit($name, $value, true, $title, $async, $icon);
177 function submit_center_last($name, $value, $title=false, $async=false, $icon=false)
180 submit($name, $value, true, $title, $async, $icon);
184 For following controls:
185 'both' - use both Ctrl-Enter and Escape hotkeys
186 'cancel' - apply to 'RESET' button
188 function submit_add_or_update($add=true, $title=false, $async=false, $clone=false)
192 if ($async === 'both') {
193 $async = 'default'; $cancel = 'cancel';
195 else if ($async === 'default')
197 else if ($async === 'cancel')
201 submit('ADD_ITEM', _("Add new"), true, $title, $async);
203 submit('UPDATE_ITEM', _("Update"), true, _('Submit changes'), $async);
204 if ($clone) submit('CLONE', _("Clone"), true,
205 _('Edit new record with current data'), $async);
206 submit('RESET', _("Cancel"), true, _('Cancel edition'), $cancel);
210 function submit_add_or_update_center($add=true, $title=false, $async=false, $clone=false)
213 submit_add_or_update($add, $title, $async, $clone);
217 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false, $clone = false)
221 echo "<td> </td>\n";
223 submit_add_or_update($add, $title, $async, $clone);
227 function submit_cells($name, $value, $extra="", $title=false, $async=false)
230 submit($name, $value, true, $title, $async);
234 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
238 echo "<td> </td>\n";
239 submit_cells($name, $value, $extra, $title, $async);
243 function submit_return($name, $value, $title=false)
245 if (@$_REQUEST['popup']) {
246 submit($name, $value, true, $title, 'selector');
250 function submit_js_confirm($name, $msg) {
252 "_validate.$name=function(){ return confirm('"
253 . strtr($msg, array("\n"=>'\\n')) . "');};");
255 //-----------------------------------------------------------------------------------
257 function set_icon($icon, $title=false)
259 global $path_to_root;
260 return "<img src='$path_to_root/themes/".user_theme()."/images/$icon' width='12' height='12' border='0'".($title ? " title='$title'" : "")." />\n";
263 function button($name, $value, $title=false, $icon=false, $aspect='')
265 // php silently changes dots,spaces,'[' and characters 128-159
266 // to underscore in POST names, to maintain compatibility with register_globals
268 if ($aspect == 'selector') {
269 $rel = " rel='$value'";
270 $value = _("Select");
272 if (user_graphic_links() && $icon)
274 if ($value == _("Delete")) // Helper during implementation
276 return "<button type='submit' class='editbutton' name='".
277 htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))).
278 "' value='1'" . ($title ? " title='$title'":" title='$value'")
279 . ($aspect ? " aspect='$aspect'" : '')
281 ." />".set_icon($icon)."</button>\n";
284 return "<input type='submit' class='editbutton' name='"
285 .htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B')))
287 .($title ? " title='$title'":'')
288 . ($aspect ? " aspect='$aspect'" : '')
293 function button_cell($name, $value, $title=false, $icon=false, $aspect='')
295 echo "<td align='center'>";
296 echo button($name, $value, $title, $icon, $aspect);
300 function delete_button_cell($name, $value, $title=false)
302 button_cell($name, $value, $title, ICON_DELETE);
305 function edit_button_cell($name, $value, $title=false)
307 button_cell($name, $value, $title, ICON_EDIT);
310 function select_button_cell($name, $value, $title=false)
312 button_cell($name, $value, $title, ICON_ADD, 'selector');
314 //-----------------------------------------------------------------------------------
316 function check_value($name)
318 if (!isset($_POST[$name]))
323 function checkbox($label, $name, $value=null, $submit_on_change=false, $title=false)
330 $str .= $label . " ";
331 if ($submit_on_change !== false) {
332 if ($submit_on_change === true)
334 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
337 $value = get_post($name,0);
340 .($value == 1 ? ' checked':'')
341 ." type='checkbox' name='$name' value='1'"
342 .($submit_on_change ? " onclick='$submit_on_change'" : '')
343 .($title ? " title='$title'" : '')
346 $Ajax->addUpdate($name, $name, $value);
350 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
352 echo checkbox($label, $name, $value, $submit_on_change, $title);
355 function check_cells($label, $name, $value=null, $submit_on_change=false, $title=false,
359 echo "<td>$label</td>\n";
361 echo check(null, $name, $value, $submit_on_change, $title);
365 function check_row($label, $name, $value=null, $submit_on_change=false, $title=false)
367 echo "<tr><td class='label'>$label</td>";
368 echo check_cells(NULL, $name, $value, $submit_on_change, $title);
372 //-----------------------------------------------------------------------------------
373 function radio($label, $name, $value, $selected, $submit_on_change=false) {
375 if ($submit_on_change === true)
377 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
379 return "<input type='radio' name=$name value='$value' ".($selected ? "checked":'')
380 .($submit_on_change ? " onclick='$submit_on_change'" : '')
381 .">".($label ? $label : '');
384 //-----------------------------------------------------------------------------------
385 function labelheader_cell($label, $params="")
387 echo "<td class='tableheader' $params>$label</td>\n";
390 function label_cell($label, $params="", $id=null)
396 $params .= " id='$id'";
397 $Ajax->addUpdate($id, $id, $label);
399 echo "<td $params>$label</td>\n";
404 function email_cell($label, $params="", $id=null)
406 label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
409 function amount_decimal_cell($label, $params="", $id=null)
412 label_cell(price_decimal_format($label, $dec), "nowrap align=right ".$params, $id);
415 function amount_cell($label, $bold=false, $params="", $id=null)
418 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
420 label_cell(price_format($label), "nowrap align=right ".$params, $id);
423 //JAM Allow entered unit prices to be fractional
424 function unit_amount_cell($label, $bold=false, $params="", $id=null)
427 label_cell("<b>".unit_price_format($label)."</b>", "nowrap align=right ".$params, $id);
429 label_cell(unit_price_format($label), "nowrap align=right ".$params, $id);
433 function percent_cell($label, $bold=false, $id=null)
436 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
438 label_cell(percent_format($label), "nowrap align=right", $id);
440 // 2008-06-15. Changed
441 function qty_cell($label, $bold=false, $dec=null, $id=null)
444 $dec = get_qty_dec();
446 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
448 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
451 function label_cells($label, $value, $params="", $params2="", $id='')
454 echo "<td $params>$label</td>\n";
455 label_cell($value, $params2, $id);
458 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
463 echo "<td class='label'>$label</td>";
466 label_cells($label, $value, $params, $params2, $id);
468 echo "<td colspan=$leftfill></td>";
472 //-----------------------------------------------------------------------------------
474 function text_cells($label, $name, $value=null, $size="", $max="", $title=false,
475 $labparams="", $post_label="", $inparams="")
479 default_focus($name);
481 label_cell($label, $labparams);
485 $value = get_post($name);
486 echo "<input $inparams type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
487 .($title ? " title='$title'" : '')
490 if ($post_label != "")
491 echo " " . $post_label;
494 $Ajax->addUpdate($name, $name, $value);
497 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null,
498 $labparams=null, $post_label=null, $submit_on_change=false)
502 default_focus($name);
503 if (!isset($_POST[$name]) || $_POST[$name] == "")
506 $_POST[$name] = $init;
511 label_cell($label, $labparams);
517 $class = $submit_on_change ? 'class="searchbox"' : '';
518 echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
519 .($title ? " title='$title'": '')." >";
522 echo " " . $post_label;
525 $Ajax->addUpdate($name, $name, $_POST[$name]);
528 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
530 echo "<tr><td class='label'>$label</td>";
531 text_cells(null, $name, $value, $size, $max, $title, $params, $post_label);
536 //-----------------------------------------------------------------------------------
538 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
540 echo "<tr><td class='label'>$label</td>";
541 text_cells_ex(null, $name, $size, $max, $value, $title, $params, $post_label);
546 //-----------------------------------------------------------------------------------
547 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
550 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
551 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
554 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
557 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
558 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
561 function link_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
563 $val = get_post($name);
565 if (strpos($val,'http://')===false)
566 $val = 'http://'.$val;
567 $label = "<a href='$val' target='_blank'>$label</a>";
569 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
572 function link_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
574 $val = get_post($name);
576 if (strpos($val,'http://')===false)
577 $val = 'http://'.$val;
578 $label = "<a href='$val' target='_blank'>$label</a>";
580 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
583 //-----------------------------------------------------------------------------------
585 // Since FA 2.2 $init parameter is superseded by $check.
586 // When $check!=null current date is displayed in red when set to other
587 // than current date.
589 function date_cells($label, $name, $title = null, $check=null, $inc_days=0,
590 $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
592 global $use_date_picker, $path_to_root, $Ajax;
594 if (!isset($_POST[$name]) || $_POST[$name] == "")
596 if ($inc_years == 1001)
597 $_POST[$name] = null;
602 $dd = add_days($dd, $inc_days);
603 if ($inc_months != 0)
604 $dd = add_months($dd, $inc_months);
606 $dd = add_years($dd, $inc_years);
610 if ($use_date_picker)
611 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
612 . " <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";
617 label_cell($label, $params);
621 $class = $submit_on_change ? 'class="searchbox"' : '';
623 $aspect = $check ? 'aspect="cdate"' : '';
624 if ($check && (get_post($name) != Today()))
625 $aspect .= ' style="color:#FF0000"';
627 default_focus($name);
629 echo "<input type=\"text\" name=\"$name\" $class $aspect size=\"9\" maxlength=\"12\" value=\""
630 . $_POST[$name]. "\""
631 .($title ? " title='$title'": '')." > $post_label";
633 $Ajax->addUpdate($name, $name, $_POST[$name]);
636 function date_row($label, $name, $title=null, $check=null, $inc_days=0, $inc_months=0,
637 $inc_years=0, $params=null, $submit_on_change=false)
639 echo "<tr><td class='label'>$label</td>";
640 date_cells(null, $name, $title, $check, $inc_days, $inc_months,
641 $inc_years, $params, $submit_on_change);
645 //-----------------------------------------------------------------------------------
646 function password_row($label, $name, $value)
648 echo "<tr><td class='label'>$label</td>";
649 label_cell("<input type='password' name='$name' size=20 maxlength=20 value='$value' />");
653 //-----------------------------------------------------------------------------------
654 function file_cells($label, $name, $id="")
658 label_cells($label, "<input type='file' name='$name' $id />");
660 function file_row($label, $name, $id = "")
662 echo "<tr><td class='label'>$label</td>";
663 file_cells(null, $name, $id);
667 //-----------------------------------------------------------------------------------
669 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
671 text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
674 //-----------------------------------------------------------------------------------
676 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
678 echo "<tr><td class='label'>$label</td>";
679 ref_cells(null, $name, $title, $init, null, $submit_on_change);
683 //-----------------------------------------------------------------------------------
685 function percent_row($label, $name, $init=null)
688 if (!isset($_POST[$name]) || $_POST[$name]=="")
690 $_POST[$name] = $init == null ? '' : $init;
693 small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
696 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
701 $dec = user_price_dec();
702 if (!isset($_POST[$name]) || $_POST[$name] == "")
705 $_POST[$name] = $init;
712 $params = "class='label'";
713 label_cell($label, $params);
721 echo "<td align='right'>";
723 echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
726 echo "<span id='_{$name}_label'> $post_label</span>";
727 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
730 $Ajax->addUpdate($name, $name, $_POST[$name]);
731 $Ajax->addAssign($name, $name, 'dec', $dec);
735 //-----------------------------------------------------------------------------------
737 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
739 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
742 //JAM Allow entered unit prices to be fractional
743 function unit_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
746 $dec = user_price_dec()+2;
748 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec+2);
751 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
754 amount_cells($label, $name, $init, $params, $post_label, $dec);
758 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
761 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
765 //-----------------------------------------------------------------------------------
767 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
770 $dec = user_qty_dec();
772 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
775 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
778 $dec = user_qty_dec();
781 amount_cells($label, $name, $init, $params, $post_label, $dec);
785 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
788 $dec = user_qty_dec();
791 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
795 //-----------------------------------------------------------------------------------
797 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
799 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
802 //-----------------------------------------------------------------------------------
804 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
807 $dec = user_qty_dec();
808 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
811 //-----------------------------------------------------------------------------------
813 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
817 default_focus($name);
819 echo "<td $params>$label</td>\n";
821 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
822 echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
823 .($title ? " title='$title'" : '')
824 .">$value</textarea></td>\n";
825 $Ajax->addUpdate($name, $name, $value);
828 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
830 echo "<tr><td class='label'>$label</td>";
831 textarea_cells(null, $name, $value, $cols, $rows, $title, $params);
835 //-----------------------------------------------------------------------------------
837 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
841 default_focus($name);
842 echo "<tr><td>$label</td>\n";
846 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
847 echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\"> ";
849 submit($input_name, $input_value);
852 $Ajax->addUpdate($name, $name, $value);
855 //-----------------------------------------------------------------------------------
857 // When show_inactive page option is set
858 // displays value of inactive field as checkbox cell.
859 // Also updates database record after status change.
861 function inactive_control_cell($id, $value, $table, $key)
865 $name = "Inactive". $id;
866 $value = $value ? 1:0;
868 if (check_value('show_inactive')) {
869 if (isset($_POST['LInact'][$id]) && (get_post('_Inactive'.$id.'_update') ||
870 get_post('Update')) && (check_value('Inactive'.$id) != $value)) {
871 update_record_status($id, !$value, $table, $key);
873 echo '<td align="center">'. checkbox(null, $name, $value, true, '', "align='center'")
874 . hidden("LInact[$id]", $value, false) . '</td>';
878 // Displays controls for optional display of inactive records
880 function inactive_control_row($th) {
881 echo "<tr><td colspan=".(count($th)).">"
882 ."<div style='float:left;'>"
883 . checkbox(null, 'show_inactive', null, true). _("Show also Inactive")
884 ."</div><div style='float:right;'>"
885 . submit('Update', _('Update'), false, '', null)
889 // Inserts additional column header when display of inactive records is on.
891 function inactive_control_column(&$th) {
894 if (check_value('show_inactive'))
895 array_insert($th, count($th)-2 , _("Inactive"));
896 if (get_post('_show_inactive_update')) {
897 $Ajax->activate('_page_body');