📄 fields_functions.php
字号:
<?php
// +-------------------------------------------------------------+
// | DeskPRO v [2.0.1 Production]
// | Copyright (C) 2001 - 2004 Headstart Solutions Limited
// | Supplied by WTN-WDYL
// | Nullified by WTN-WDYL
// | Distribution via WebForum, ForumRU and associated file dumps
// +-------------------------------------------------------------+
// | DESKPRO IS NOT FREE SOFTWARE
// +-------------------------------------------------------------+
// | License ID : Full Enterprise License =) ...
// | License Owner : WTN-WDYL Team
// +-------------------------------------------------------------+
// | $RCSfile: fields_functions.php,v $
// | $Date: 2004/02/10 01:34:25 $
// | $Revision: 1.46 $
// +-------------------------------------------------------------+
// | File Details:
// | - Utility functions for custom fields.
// +-------------------------------------------------------------+
error_reporting(E_ALL ^ E_NOTICE);
#################################################################################################
# CUSTOM FIELD FUNCTIONS
#################################################################################################
/*****************************************************
function field_def_val
-----DESCRIPTION: -----------------------------------
- validates custom field form elements
-----ARGUMENTS: -------------------------------------
fielddata : the data from the x_def table
fieldvalue : the value submitted (value or an array)
customvalue : extra data for when an extra optional input field is entered
noerror : set if we only want validated results but not errors
-----RETURNS:----------------------------------------
if valid result returns the result
if empty result (but this is allowed) returns empty string
if validation fails then returns null
*****************************************************/
function field_def_val($fielddata, $fieldvalue, $customvalue='', $noerror='') {
// get what we need from the database
$data = unserialize($fielddata[data]);
if (is_array($data)) {
// get allowed values
foreach ($data AS $key => $val) {
$data_array[] = $val[0];
}
}
$reg_ex = stripslashes($fielddata[reg_ex]);
//////////////////////// SELECT FIELDS ////////////////////////
if ($fielddata[formtype] == "select") {
/*
1) Single select
- require a selection
- or extra input over rides
2) Multi select
- min options
- max options
*/
// sort multiselect data
if ($fielddata[multiselect] == "1") {
// build array of selected options that match allowed ones
if (is_array($fieldvalue)) {
$value = '|||';
foreach($fieldvalue AS $key => $var) {
if (in_array($var, $data_array)) {
$value .= $var . "|||";
$regex_array[] = $var;
}
}
// too many / too few
$count = count($regex_array);
if (($fielddata[minoptions] != "0") AND ($count < $fielddata[minoptions])) {
$error = 1;
}
if (($fielddata[maxoptions] != "0") AND ($count > $fielddata[maxoptions])) {
$error = 1;
}
// nothing selected for multiselect
} else {
if ($fielddata[minoptions]) {
$error = 1;
}
}
} else {
// not multiselect
if (@in_array($fieldvalue, $data_array)) {
$value = "|||" . $fieldvalue . "|||";
// check if required
} elseif ($fielddata[required] == "1") {
$error_temp = 1;
}
}
}
//////////////////////// TEXTAREA AND INPUT FIELDS ////////////////////////
if ($fielddata[formtype] == "textarea" OR $fielddata[formtype] == "input") {
/*
1) minlength
2) maxlength
3) regex
*/
if ($fielddata[regex]) {
if (!(preg_match($fielddata[regex], $fieldvalue))) {
$error = 1;
}
}
$length = strlen($fieldvalue);
if (($fielddata[maxlength] != "0") AND ($length > $fielddata[maxlength])) {
$error = 1;
}
if (($fielddata[minlength] != "0") AND ($length < $fielddata[minlength])) {
$error = 1;
}
$value = $fieldvalue;
}
//////////////////////// RADIO FIELDS ////////////////////////
if ($fielddata[formtype] == "radio") {
/*
1) Require a selection
2) Extrainput overrides
*/
if (in_array($fieldvalue, $data_array)) {
$value = '|||' . $fieldvalue . '|||';
} else {
// if we need a selection
if ($fielddata[required] == "1") {
$error_temp = 1;
}
}
}
//////////////////////// CHECKBOX FIELDS ////////////////////////
if ($fielddata[formtype] == "checkbox") {
/*
1) Minimum options
2) Maximum options
*/
// check not empty
if (is_array($fieldvalue)) {
$value = '|||';
foreach($fieldvalue AS $key => $var) {
// build data
if (in_array($var, $data_array)) {
$value .= $var . "|||";
$regex_array[] = $var;
}
}
}
// too many / too few
$count = count($regex_array);
if (($fielddata[minoptions] != "0") AND ($count < $fielddata[minoptions])) {
$error = 1;
}
if (($fielddata[maxoptions] != "0") AND ($count > $fielddata[maxoptions])) {
$error = 1;
}
}
//////////////////////// EXTRA INPUT ////////////////////////
if ($fielddata[extrainput] == "1") {
/*
1) regex
2) minlength
3) maxlength
*/
if (trim($customvalue) != "") {
$val_extrainput = 1;
if ($fielddata[regex]) {
if (!(preg_match($fielddata[regex], $customvalue))) {
$error = 1;
}
}
$length = strlen($customvalue);
if ($fielddata[maxlength] AND ($length > $fielddata[maxlength])) {
$error = 1;
unset($val_extrainput);
}
if ($fielddata[maxlength] AND ($length < $fielddata[minlength])) {
$error = 1;
unset($val_extrainput);
}
$value = $customvalue;
}
}
//////////////////////// RETURN VALUE ////////////////////////
// check if error from lack of custom field
if (($error_temp) AND (!$val_extrainput)) {
$error = 1;
}
// empty options
if ($value == '||||||') {
unset($value);
}
// return error
if ($error AND !$noerror) {
return;
} else {
if ($value) {
return $value;
} else {
// return empty string, different from error
return '';
}
}
}
/*****************************************************
function field_def
-----DESCRIPTION: -----------------------------------
- generates custom field form elements
-----ARGUMENTS: -------------------------------------
fielddata : the data from the x_def table
type : can be default (using default value in table), edit (for editing fields) or redo (redoing a form if there where errors)
fieldvalue : the value submitted (value or an array) this is used when we are redoing/editing the form
customvalue : extra data for when an extra optional input field is entered this is used when we are redoing/editing the form
editvalue : the value from the database
name : the name of the form array when it is not custom_fields for example when two _defs on the same page
bools : [optional] If true, show boolean search fields
match : [optional] The user-submitted (or DB-stored) match selection
not : [optional] The user-submitted (or DB-stored) not selection
-----RETURNS:----------------------------------------
the html to generate the form element
*****************************************************/
function field_def($fielddata, $type='default', $fieldvalue='', $customvalue='', $editvalue='', $name='custom_fields', $bools=NULL, $match=NULL, $not=NULL) {
global $user, $ticket;
if (!$name) {
$name = 'custom_fields';
}
// default value (failed regex/parsed default/default)
if ($type == 'redo') {
$default_value = $fieldvalue;
} elseif ($type == 'edit') {
$default_value = $editvalue;
} elseif ($fielddata[parsed_default_value]) {
eval ("\$default_value = $fielddata[parsed_default_value];");
} elseif ($fielddata[default_value]) {
$default_value = $fielddata[default_value];
}
// data manipulation
$data = unserialize($fielddata[data]);
$fielddata[name] = htmlspecialchars($fielddata[name]);
//////////////////////// SELECT FIELDS ////////////////////////
if ($fielddata[formtype] == "select") {
// size
if ($fielddata[height] > 0) {
$size .= " size=\"$fielddata[height]\"";
}
// allow multiple selections
if ($fielddata[multiselect]) {
unset($fielddata[extrainput]);
$html = "<select name=\"" . $name . "[" . $fielddata[name] . "][]\" multiple$size>";
} else {
$html = "<select name=\"" . $name . "[$fielddata[name]]\"$size>";
// empty element for single selects
$html .= "<option>[None]</option>\n";
}
// run through data
if (is_array($data)) {
if ($type == 'edit' AND $default_value) {
// if edit sort out fields
$default_value = explode('|||', $default_value);
if (!is_array($fieldvalue)) {
$fieldvalue = array();
}
foreach ($default_value AS $key => $var) {
if (strlen($var)) {
$fieldvalue[] = $var;
}
}
}
foreach ($data AS $key => $val) {
if (!(is_array($val))) {
$val = array('0' => '', '2' => '', '3' => '');
} else {
$val[0] = htmlspecialchars($val[0]);
$val[2] = htmlspecialchars($val[2]);
}
// submitted data
if ($type == "redo" OR $type == 'edit') {
if (is_array($fieldvalue)) {
if (in_array($val[0], $fieldvalue) AND ((!$customvalue) OR in_array($val[2], $customvalue))) {
$html .= "<option selected=\"selected\" value=\"$val[0]\">$val[2]</option>\n";
if (is_array($customvalue)) {
if (in_array($val[0], $customvalue)) {
$customvalue = NULL;
}
} else {
if ($val[0] == $customvalue) {
$customvalue = NULL;
}
}
} else {
$html .= "<option value=\"$val[0]\">$val[2]</option>\n";
}
} else {
if ($fieldvalue == $val[0] AND ((!$customvalue) OR $customvalue == $val[2])) {
$html .= "<option selected=\"selected\" value=\"$val[0]\">$val[2]</option>\n";
if ($customvalue == $val[0]) {
$customvalue = NULL;
}
} else {
$html .= "<option value=\"$val[0]\">$val[2]</option>\n";
}
}
// default data
} else {
if ($val[3] == "1" AND (!$customvalue)) {
$html .= "<option selected=\"selected\" value=\"$val[0]\">$val[2]</option>\n";
} else {
$html .= "<option value=\"$val[0]\">$val[2]</option>\n";
}
}
}
}
$html .= "</select>\n";
}
//////////////////////// TEXTAREA FIELDS ////////////////////////
if ($fielddata[formtype] == "textarea") {
$html = form_textarea($fielddata[name], $fielddata[length], $fielddata[height], $default_value, $name);
}
//////////////////////// INPUT FIELDS ////////////////////////
if ($fielddata[formtype] == "input") {
$html = form_input($fielddata[name], $default_value, $fielddata[length], $name);
}
//////////////////////// RADIO FIELDS ////////////////////////
if ($fielddata[formtype] == "radio") {
// run through data
if (is_array($data)) {
foreach ($data AS $key => $val) {
$val[0] = htmlspecialchars($val[0]);
if ($fielddata[perline] == $count) {
$count = 0;
$html .= "<br />";
}
// submitted data
if ($type == "redo") {
if ($val[0] == $fieldvalue AND (!$customvalue)) {
$checked = "checked=\"checked\"";
} else {
unset($checked);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -