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 function get_post($name, $dflt='')
15 return ((!isset($_POST[$name]) || $_POST[$name] === '') ? $dflt : $_POST[$name]);
18 // Sets local POST value and adds Value to ajax posting if needed
20 /*function set_post($name, $value, $ajax_trigger=true) {
23 $_POST[$name] = $value;
24 if ($ajax_trigger) $Ajax->activate($name);
27 //------------------------------------------------------------------------------
28 // Seek for _POST variable with $prefix.
29 // If var is found returns variable name with prefix stripped,
30 // and null or -1 otherwise.
32 function find_submit($prefix, $numeric=true)
35 foreach($_POST as $postkey=>$postval )
37 if (strpos($postkey, $prefix) === 0)
39 $id = substr($postkey, strlen($prefix));
40 return $numeric ? (int)$id : $id;
43 return $numeric ? -1 : null;
45 //------------------------------------------------------------------------------
47 // Helper function for simple db table editor pages
49 function simple_page_mode($numeric_id = true)
51 global $Ajax, $Mode, $selected_id;
53 $default = $numeric_id ? -1 : '';
54 $selected_id = get_post('selected_id', $default);
55 foreach (array('ADD_ITEM', 'UPDATE_ITEM', 'RESET') as $m) {
56 if (isset($_POST[$m])) {
57 $Ajax->activate('_page_body');
59 $selected_id = $default;
63 foreach (array('Edit', 'Delete') as $m) {
64 foreach ($_POST as $p => $pvar) {
65 if (strpos($p, $m) === 0) {
66 // $selected_id = strtr(substr($p, strlen($m)), array('%2E'=>'.'));
67 unset($_POST['_focus']); // focus on first form entry
68 $selected_id = quoted_printable_decode(substr($p, strlen($m)));
69 $Ajax->activate('_page_body');
78 //------------------------------------------------------------------------------
80 // Read numeric value from user formatted input
82 function input_num($postname=null, $dflt=null)
84 if (!isset($_POST[$postname]) || $_POST[$postname] == "")
87 return user_numeric($_POST[$postname]);
90 //---------------------------------------------------------------------------------
92 function hidden($name, $value=null, $echo=true)
97 $value = get_post($name);
99 $ret = "<input type=\"hidden\" name=\"$name\" value=\"$value\">";
100 $Ajax->addUpdate($name, $name, $value);
107 Various types of submit button selected by $type:
108 null - button visible only in fallback mode
109 false - normal submit button
110 true - ajax driven submit
111 'process' - ajax button with long timeout
113 function submit($name, $value, $echo=true, $title=false, $type=false, $icon=false)
115 global $path_to_root;
117 default_focus($name);
118 $submit_str = "<button class=\""
119 .($type ? 'ajaxsubmit' : 'inputsubmit')
120 ."\" type=\"submit\""
121 .($type === null ? (fallback_mode() ? ' aspect="fallback"' : " style='display:none;'" ):
122 ($type === 'process' ? 'aspect="process"' : '') )
123 ." name=\"$name\" id=\"$name\" value=\"$value\""
124 .($title ? " title='$title'" : '')
125 ."><span>$value</span>"
126 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon'>" : '')
134 function submit_center($name, $value, $echo=true, $title=false, $async=false, $icon=false)
137 submit($name, $value, $echo, $title, $async, $icon);
141 function submit_center_first($name, $value, $title=false, $async=false, $icon=false)
144 submit($name, $value, true, $title, $async, $icon);
148 function submit_center_last($name, $value, $title=false, $async=false, $icon=false)
151 submit($name, $value, true, $title, $async, $icon);
155 function submit_add_or_update($add=true, $title=false, $async=false)
158 submit('ADD_ITEM', _("Add new"), true, $title, $async);
160 submit('UPDATE_ITEM', _("Update"), true, $title, $async);
161 submit('RESET', _("Cancel"), true, $title, $async);
165 function submit_add_or_update_center($add=true, $title=false, $async=false)
168 submit_add_or_update($add, $title, $async);
173 function submit_add_or_update_row($add=true)
175 echo "<tr><td colspan=99 align=center>";
176 submit_add_or_update($add);
180 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false)
184 echo "<td> </td>\n";
186 submit_add_or_update($add, $title, $async);
190 function submit_cells($name, $value, $extra="", $title=false, $async=false)
193 submit($name, $value, true, $title, $async);
197 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
201 echo "<td> </td>\n";
202 submit_cells($name, $value, $extra, $title, $async);
206 function submit_return($name, $value, $title=false, $async=false)
208 if (count($_SESSION['Context'])) {
209 submit($name, $value, true, $title, $async);
213 function submit_js_confirm($name, $msg) {
215 "_validate.$name=function(){ return confirm('$msg');};");
217 //-----------------------------------------------------------------------------------
219 function set_icon($icon, $title=false)
221 global $path_to_root;
222 return "<img src='$path_to_root/themes/".user_theme()."/images/$icon' width='14' height='14' border='0'".($title ? " title='$title'" : "")." />\n";
225 function button($name, $value, $title=false, $icon=false)
227 // php silently changes dots,spaces,'[' and characters 128-159
228 // to underscore in POST names, to maintain compatibility with register_globals
229 if (user_graphic_links() && $icon)
231 if ($value == _("Delete")) // Helper during implementation
233 return "<button type='submit' class='editbutton' name='".
234 htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B'))).
235 "' value='1'" . ($title ? " title='$title'":" title='$value'")." />".set_icon($icon)."\n";
238 return "<input type='submit' class='editbutton' name='"
239 .htmlentities(strtr($name, array('.'=>'=2E',' '=>'=20','='=>'=3D','['=>'=5B')))
241 .($title ? " title='$title'":'')." />\n";
244 function button_cell($name, $value, $title=false, $icon=false)
247 echo button($name, $value, $title, $icon);
251 function delete_button_cell($name, $value, $title=false)
253 button_cell($name, $value, $title, ICON_DELETE);
256 function edit_button_cell($name, $value, $title=false)
258 button_cell($name, $value, $title, ICON_EDIT);
260 //-----------------------------------------------------------------------------------
262 function check_value($name)
264 if (!isset($_POST[$name]))
269 function checkbox($label, $name, $value=null, $submit_on_change=false, $title=false)
274 default_focus($name);
276 $str .= $label . " ";
277 if ($submit_on_change !== false) {
278 if ($submit_on_change === true)
280 "JsHttpRequest.request(\"_{$name}_update\", this.form);";
283 $value = get_post($name,0);
286 .($value == 1 ? ' checked':'')
287 ." type='checkbox' name='$name' value='1'"
288 .($submit_on_change ? " onclick='$submit_on_change'" : '')
289 .($title ? " title='$title'" : '')
292 $Ajax->addUpdate($name, $name, $value);
296 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
298 echo checkbox($label, $name, $value, $submit_on_change, $title);
301 function check_cells($label, $name, $value, $submit_on_change=false, $title=false)
304 echo "<td>$label</td>\n";
306 echo check(null, $name, $value, $submit_on_change, $title);
310 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
313 echo check_cells($label, $name, $value, $submit_on_change, $title);
317 //-----------------------------------------------------------------------------------
319 function labelheader_cell($label, $params="")
321 echo "<td class='tableheader' $params>$label</td>\n";
324 function label_cell($label, $params="", $id=null)
330 $params .= " id='$id'";
331 $Ajax->addUpdate($id, $id, $label);
333 echo "<td $params>$label</td>\n";
338 function email_cell($label, $params="", $id=null)
340 label_cell("<a href='mailto:$label'>$label</a>", $params, $id);
343 function amount_cell($label, $bold=false, $params="", $id=null)
346 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
348 label_cell(price_format($label), "nowrap align=right ".$params, $id);
351 function percent_cell($label, $bold=false, $id=null)
354 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
356 label_cell(percent_format($label), "nowrap align=right", $id);
358 // 2008-06-15. Changed
359 function qty_cell($label, $bold=false, $dec=null, $id=null)
362 $dec = get_qty_dec();
364 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
366 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
369 function label_cells($label, $value, $params="", $params2="", $id='')
372 echo "<td $params>$label</td>\n";
373 label_cell($value, $params2, $id);
376 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
379 label_cells($label, $value, $params, $params2, $id);
381 echo "<td colspan=$leftfill></td>";
385 //-----------------------------------------------------------------------------------
387 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, $params="", $post_label="", $disabled="")
391 default_focus($name);
393 label_cell($label, $params);
397 $value = get_post($name);
398 echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
399 .($title ? " title='$title'" : '')
402 if ($post_label != "")
403 echo " " . $post_label;
406 $Ajax->addUpdate($name, $name, $value);
409 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null, $params=null, $post_label=null, $submit_on_change=false)
413 default_focus($name);
414 if (!isset($_POST[$name]) || $_POST[$name] == "")
417 $_POST[$name] = $init;
422 label_cell($label, $params);
428 $class = $submit_on_change ? 'class="searchbox"' : '';
429 echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
430 .($title ? " title='$title'": '')." >";
433 echo " " . $post_label;
436 $Ajax->addUpdate($name, $name, $_POST[$name]);
439 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
443 text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
448 //-----------------------------------------------------------------------------------
450 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
454 text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
459 //-----------------------------------------------------------------------------------
460 function email_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
463 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
464 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
467 function email_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
470 $label = "<a href='Mailto:".$_POST[$name]."'>$label</a>";
471 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
474 function link_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
476 $val = get_post($name);
478 if (strpos($val,'http://')===false)
479 $val = 'http://'.$val;
480 $label = "<a href='$val' target='_blank'>$label</a>";
482 text_row($label, $name, $value, $size, $max, $title, $params, $post_label);
485 function link_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
487 $val = get_post($name);
489 if (strpos($val,'http://')===false)
490 $val = 'http://'.$val;
491 $label = "<a href='$val' target='_blank'>$label</a>";
493 text_row_ex($label, $name, $size, $max, $title, $value, $params, $post_label);
496 //-----------------------------------------------------------------------------------
498 function date_cells($label, $name, $title = null, $init=null, $inc_days=0,
499 $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
501 global $use_date_picker, $path_to_root;
502 if (!isset($_POST[$name]) || $_POST[$name] == "")
506 if ($inc_years == 1001)
507 $_POST[$name] = null;
512 $dd = add_days($dd, $inc_days);
513 if ($inc_months != 0)
514 $dd = add_months($dd, $inc_months);
516 $dd = add_years($dd, $inc_years);
521 $_POST[$name] = $init;
523 if ($use_date_picker)
524 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
525 . " <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";
528 text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label, $submit_on_change);
531 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0,
532 $inc_years=0, $params=null, $submit_on_change=false)
535 date_cells($label, $name, $title, $init, $inc_days, $inc_months,
536 $inc_years, $params, $submit_on_change);
540 //-----------------------------------------------------------------------------------
542 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
544 text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
547 //-----------------------------------------------------------------------------------
549 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
552 ref_cells($label, $name, $title, $init, null, $submit_on_change);
556 //-----------------------------------------------------------------------------------
558 function percent_row($label, $name, $init=null)
561 if (!isset($_POST[$name]) || $_POST[$name]=="")
563 $_POST[$name] = $init == null ? '' : $init;
566 small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
569 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
574 $dec = user_price_dec();
575 if (!isset($_POST[$name]) || $_POST[$name] == "")
578 $_POST[$name] = $init;
583 label_cell($label, $params);
591 echo "<td align='right'>";
593 echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
596 echo "<span id='_{$name}_label'> $post_label</span>";
597 $Ajax->addUpdate($name, '_'.$name.'_label', $post_label);
600 $Ajax->addUpdate($name, $name, $_POST[$name]);
601 $Ajax->addAssign($name, $name, 'dec', $dec);
605 //-----------------------------------------------------------------------------------
607 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
609 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
612 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
615 amount_cells($label, $name, $init, $params, $post_label, $dec);
619 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
622 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
626 //-----------------------------------------------------------------------------------
628 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
631 $dec = user_qty_dec();
633 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
636 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
639 $dec = user_qty_dec();
642 amount_cells($label, $name, $init, $params, $post_label, $dec);
646 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
649 $dec = user_qty_dec();
652 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
656 //-----------------------------------------------------------------------------------
658 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
660 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
663 //-----------------------------------------------------------------------------------
665 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
668 $dec = user_qty_dec();
669 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
672 //-----------------------------------------------------------------------------------
674 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
678 default_focus($name);
680 echo "<td $params>$label</td>\n";
682 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
683 echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
684 .($title ? " title='$title'" : '')
685 .">$value</textarea></td>\n";
686 $Ajax->addUpdate($name, $name, $value);
689 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
692 textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
696 //-----------------------------------------------------------------------------------
698 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
702 default_focus($name);
703 echo "<tr><td>$label</td>\n";
707 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
708 echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\"> ";
710 submit($input_name, $input_value);
713 $Ajax->addUpdate($name, $name, $value);
716 //-----------------------------------------------------------------------------------