2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-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 function submit($name, $value, $echo=true, $title=false, $async=false, $icon=false)
109 global $path_to_root;
111 default_focus($name);
112 $submit_str = "<button class=\""
113 .($async ? 'ajaxsubmit' : 'inputsubmit')
114 ."\" type=\"submit\""
115 .($async === null ? (fallback_mode() ? ' aspect="fallback"' : " style='display:none;'" ):
116 ($async === 'process' ? 'aspect="process"' : '') )
117 ." name=\"$name\" id=\"$name\" value=\"$value\""
118 .($title ? " title='$title'" : '')
119 ."><span>$value</span>"
120 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/$icon'>" : '')
128 function submit_center($name, $value, $echo=true, $title=false, $async=false, $icon=false)
131 submit($name, $value, $echo, $title, $async, $icon);
135 function submit_center_first($name, $value, $title=false, $async=false, $icon=false)
138 submit($name, $value, true, $title, $async, $icon);
142 function submit_center_last($name, $value, $title=false, $async=false, $icon=false)
145 submit($name, $value, true, $title, $async, $icon);
149 function submit_add_or_update($add=true, $title=false, $async=false)
152 submit('ADD_ITEM', _("Add new"), true, $title, $async);
154 submit('UPDATE_ITEM', _("Update"), true, $title, $async);
155 submit('RESET', _("Cancel"), true, $title, $async);
159 function submit_add_or_update_center($add=true, $title=false, $async=false)
162 submit_add_or_update($add, $title, $async);
167 function submit_add_or_update_row($add=true)
169 echo "<tr><td colspan=99 align=center>";
170 submit_add_or_update($add);
174 function submit_add_or_update_row($add=true, $right=true, $extra="", $title=false, $async=false)
178 echo "<td> </td>\n";
180 submit_add_or_update($add, $title, $async);
184 function submit_cells($name, $value, $extra="", $title=false, $async=false)
187 submit($name, $value, true, $title, $async);
191 function submit_row($name, $value, $right=true, $extra="", $title=false, $async=false)
195 echo "<td> </td>\n";
196 submit_cells($name, $value, $extra, $title, $async);
200 function submit_return($name, $value, $title=false, $async=false)
202 if (count($_SESSION['Context'])) {
203 submit($name, $value, true, $title, $async);
206 //---------------------------------------------------------------------------------
208 function button($name, $value, $onclick, $title=false)
210 default_focus($name);
211 echo "<input type=\"button\" class=\"inputsubmit\" name=\"$name\" value=\"$value\" onclick=\"$onclick\""
212 .($title ? " title='$title'" : '')
216 function button_cell($name, $value, $onclick, $title=false)
219 button($name, $value, $onclick, $title);
223 //-----------------------------------------------------------------------------------
225 function check_value($name)
227 if (!isset($_POST[$name]))
232 function check($label, $name, $value=null, $submit_on_change=false, $title=false)
236 default_focus($name);
241 $value = get_post($name,0);
244 .($value == 1 ? ' checked':'')
245 ." type='checkbox' name='$name' value='1'"
246 .($submit_on_change ? " onclick='JsHttpRequest.request(\"_{$name}_update\", this.form);'" : '')
247 .($title ? " title='$title'" : '')
249 $Ajax->addUpdate($name, $name, $value);
252 function check_cells($label, $name, $value, $submit_on_change=false, $title=false)
255 echo "<td>$label</td>\n";
257 check(null, $name, $value, $submit_on_change, $title);
261 function check_row($label, $name, $value, $submit_on_change=false, $title=false)
264 check_cells($label, $name, $value, $submit_on_change, $title);
268 //-----------------------------------------------------------------------------------
270 function labelheader_cell($label, $params="")
272 echo "<td class='tableheader' $params>$label</td>\n";
275 function label_cell($label, $params="", $id=null)
281 $params .= " id='$id'";
282 $Ajax->addUpdate($id, $id, $label);
284 echo "<td $params>$label</td>\n";
289 function amount_cell($label, $bold=false, $params="", $id=null)
292 label_cell("<b>".price_format($label)."</b>", "nowrap align=right ".$params, $id);
294 label_cell(price_format($label), "nowrap align=right ".$params, $id);
297 function percent_cell($label, $bold=false, $id=null)
300 label_cell("<b>".percent_format($label)."</b>", "nowrap align=right", $id);
302 label_cell(percent_format($label), "nowrap align=right", $id);
304 // 2008-06-15. Changed
305 function qty_cell($label, $bold=false, $dec=null, $id=null)
308 $dec = get_qty_dec();
310 label_cell("<b>".number_format2($label, $dec)."</b>", "nowrap align=right", $id);
312 label_cell(number_format2($label, $dec), "nowrap align=right", $id);
315 function label_cells($label, $value, $params="", $params2="", $id='')
318 echo "<td $params>$label</td>\n";
319 label_cell($value, $params2, $id);
322 function label_row($label, $value, $params="", $params2="", $leftfill=0, $id='')
325 label_cells($label, $value, $params, $params2, $id);
327 echo "<td colspan=$leftfill></td>";
331 //-----------------------------------------------------------------------------------
333 function text_cells($label, $name, $value=null, $size="", $max="", $title=false, $params="", $post_label="", $disabled="")
337 default_focus($name);
339 label_cell($label, $params);
343 $value = get_post($name);
344 echo "<input $disabled type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\""
345 .($title ? " title='$title'" : '')
348 if ($post_label != "")
349 echo " " . $post_label;
352 $Ajax->addUpdate($name, $name, $value);
355 function text_cells_ex($label, $name, $size, $max=null, $init=null, $title=null, $params=null, $post_label=null, $submit_on_change=false)
359 default_focus($name);
360 if (!isset($_POST[$name]) || $_POST[$name] == "")
363 $_POST[$name] = $init;
368 label_cell($label, $params);
374 $class = $submit_on_change ? 'class="searchbox"' : '';
375 echo "<input $class type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"" . $_POST[$name]. "\""
376 .($title ? " title='$title'": '')." >";
379 echo " " . $post_label;
382 $Ajax->addUpdate($name, $name, $_POST[$name]);
385 function text_row($label, $name, $value, $size, $max, $title=null, $params="", $post_label="")
389 text_cells($label, $name, $value, $size, $max, $title, $params, $post_label);
394 //-----------------------------------------------------------------------------------
396 function text_row_ex($label, $name, $size, $max=null, $title=null, $value=null, $params=null, $post_label=null)
400 text_cells_ex($label, $name, $size, $max, $value, $title, $params, $post_label);
405 //-----------------------------------------------------------------------------------
407 function date_cells($label, $name, $title = null, $init=null, $inc_days=0,
408 $inc_months=0, $inc_years=0, $params=null, $submit_on_change=false)
410 global $use_date_picker, $path_to_root;
411 if (!isset($_POST[$name]) || $_POST[$name] == "")
415 if ($inc_years == 1001)
416 $_POST[$name] = null;
421 $dd = add_days($dd, $inc_days);
422 if ($inc_months != 0)
423 $dd = add_months($dd, $inc_months);
425 $dd = add_years($dd, $inc_years);
430 $_POST[$name] = $init;
432 if ($use_date_picker)
433 $post_label = "<a tabindex='-1' href=\"javascript:date_picker(document.getElementsByName('$name')[0]);\">"
434 . " <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";
437 text_cells_ex($label, $name, 9, 12, $_POST[$name], $title, $params, $post_label, $submit_on_change);
440 function date_row($label, $name, $title=null, $init=null, $inc_days=0, $inc_months=0,
441 $inc_years=0, $params=null, $submit_on_change=false)
444 date_cells($label, $name, $title, $init, $inc_days, $inc_months,
445 $inc_years, $params, $submit_on_change);
449 //-----------------------------------------------------------------------------------
451 function ref_cells($label, $name, $title=null, $init=null, $params=null, $submit_on_change=false)
453 text_cells_ex($label, $name, 16, 18, $init, $title, $params, null, $submit_on_change);
456 //-----------------------------------------------------------------------------------
458 function ref_row($label, $name, $title=null, $init=null, $submit_on_change=false)
461 ref_cells($label, $name, $title, $init, null, $submit_on_change);
465 //-----------------------------------------------------------------------------------
467 function percent_row($label, $name, $init=null)
470 if (!isset($_POST[$name]) || $_POST[$name]=="")
472 $_POST[$name] = $init == null ? '' : $init;
475 small_amount_row($label, $name, $_POST[$name], null, "%", user_percent_dec());
478 function amount_cells_ex($label, $name, $size, $max=null, $init=null, $params=null, $post_label=null, $dec=null)
483 $dec = user_price_dec();
484 if (!isset($_POST[$name]) || $_POST[$name] == "")
487 $_POST[$name] = $init;
492 label_cell($label, $params);
499 echo "<input class='amount' type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" dec=\"$dec\" value=\"" . $_POST[$name]. "\">";
502 echo " " . $post_label;
505 $Ajax->addUpdate($name, $name, $_POST[$name]);
506 $Ajax->addAssign($name, $name, 'dec', $dec);
510 //-----------------------------------------------------------------------------------
512 function amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
514 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
517 function amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
520 amount_cells($label, $name, $init, $params, $post_label, $dec);
524 function small_amount_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
527 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
531 //-----------------------------------------------------------------------------------
533 function qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
536 $dec = user_qty_dec();
538 amount_cells_ex($label, $name, 15, 15, $init, $params, $post_label, $dec);
541 function qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
544 $dec = user_qty_dec();
547 amount_cells($label, $name, $init, $params, $post_label, $dec);
551 function small_qty_row($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
554 $dec = user_qty_dec();
557 small_amount_cells($label, $name, $init, $params, $post_label, $dec);
561 //-----------------------------------------------------------------------------------
563 function small_amount_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
565 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
568 //-----------------------------------------------------------------------------------
570 function small_qty_cells($label, $name, $init=null, $params=null, $post_label=null, $dec=null)
573 $dec = user_qty_dec();
574 amount_cells_ex($label, $name, 7, 12, $init, $params, $post_label, $dec);
577 //-----------------------------------------------------------------------------------
579 function textarea_cells($label, $name, $value, $cols, $rows, $title = null, $params="")
583 default_focus($name);
585 echo "<td $params>$label</td>\n";
587 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
588 echo "<td><textarea name='$name' cols='$cols' rows='$rows'"
589 .($title ? " title='$title'" : '')
590 .">$value</textarea></td>\n";
591 $Ajax->addUpdate($name, $name, $value);
594 function textarea_row($label, $name, $value, $cols, $rows, $title=null, $params="")
597 textarea_cells($label, $name, $value, $cols, $rows, $title, $params);
601 //-----------------------------------------------------------------------------------
603 function text_row_with_submit($label, $name, $value, $size, $max, $input_name, $input_value)
607 default_focus($name);
608 echo "<tr><td>$label</td>\n";
612 $value = (!isset($_POST[$name]) ? "" : $_POST[$name]);
613 echo "<input type=\"text\" name=\"$name\" size=\"$size\" maxlength=\"$max\" value=\"$value\"> ";
615 submit($input_name, $input_value);
618 $Ajax->addUpdate($name, $name, $value);
621 //-----------------------------------------------------------------------------------