fields_functions.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:22k
- <?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);
- }
- } elseif ($type == "edit") {
- if ($default_value == '|||' . $val[0] . '|||' AND (!$customvalue)) {
- $checked = "checked="checked"";
- } else {
- unset($checked);
- }
- // default data
- } else {
- if ($val[3] == "1" AND (!$customvalue)) {
- $checked = "checked="checked"";
- } else {
- unset($checked);
- }
- }
-
- $html .= $val[2] . "<input type="radio" name="" . $name . "[" . $fielddata[name] . "]" . "" value="$val[0]" $checked> ";
- $count++;
- }
- }
- }
- //////////////////////// CHECKBOX FIELDS ////////////////////////
- if ($fielddata[formtype] == "checkbox") {
- unset($fielddata[extrainput]);
- if ($type == 'redo') {
- // needs reforming of data
- if (is_array($fieldvalue)) {
- foreach ($fieldvalue AS $key => $var) {
- $temp[] = $var;
- }
- $fieldvalue = $temp;
- }
- } elseif ($type == 'edit' AND $default_value) {
- // if edit sort out fields
- $default_value = split('|||', $default_value);
- if (is_array($default_value)) {
- if (!is_array($fieldvalue)) {
- $fieldvalue = array();
- }
- foreach ($default_value AS $key => $var) {
- if ($var > 0) {
- $fieldvalue[] = $var;
- }
- }
- }
- }
- // run through data
- if (is_array($data)) {
- foreach ($data AS $key => $val) {
- if (($fielddata['perline']) AND ($count >= $fielddata['perline'])) {
- $html .= "<br />";
- $count = 0;
- }
- // submitted data
- if ($type == 'redo' OR $type == 'edit') {
- if (is_array($fieldvalue)) {
- if (in_array($val[0], $fieldvalue)) {
- $checked = "checked="checked"";
- } else {
- unset($checked);
- }
- } else {
- unset($checked);
- }
- // default data
- } else {
-
- if ($val[3] == "1") {
- $checked = "checked="checked"";
- } else {
- unset($checked);
- }
- }
- $html .= $val[2] . "<input type="checkbox" name="" . $name . "[" . $fielddata[name] . "][]" . "" value="$val[0]" $checked> ";
- $count++;
- }
- }
- }
-
- //////////////////////// EXTRA INPUT FIELD ////////////////////////
- if ($fielddata[extrainput] == "1") {
- $html .= "<br />$fielddata[extrainput_text]";
- if ($fielddata[extrainput_location] == "1") {
- $html .= "<br />";
- } elseif ($fielddata[extrainput_text] != "") {
- $html .= " ";
- }
- if (!$customvalue) {
- if ($type == 'edit' OR $type == 'redo') {
- $customvalue = $editvalue;
- } else {
- $customvalue = $fieldvalue;
- }
- }
- if (in_string('|||', $customvalue)) {
- $customvalue = '';
- }
- $html .= form_input('extra' . $fielddata[name], $customvalue, $fielddata[length], $name);
- }
- ////////////////////////////////////////////////////////////////////////
- // Generate boolean search fields if needed
- if ($bools AND ($fielddata['formtype'] == 'checkbox' or ($fielddata['formtype'] == 'select' AND $fielddata['multiselect']))) {
- if ($match == 'and') {
- $and_checked = 1;
- } elseif ($match == 'or') {
- $or_checked = 1;
- } else {
- $none_checked = 1;
- }
- $name = $name."[$fielddata[name]";
- $html .= '</td><td><B>Must Match:</B> (' . form_checkbox_single($name.'_not]', 1, $not, '') . ' Negate/logical NOT)<BR>';
- $html .= form_radio_single($name.'_match]', 'or', $or_checked) . ' Any checked ';
- $html .= form_radio_single($name.'_match]', 'and', $and_checked) . ' All checked<BR>';
- $html .= form_radio_single($name.'_match]', 'none', $none_checked) . ' No matching<BR>';
- $html = "<table border="0" cellpadding="2"><tr><td>$html</td></tr></table>";
- }
- return $html;
- }
- /*****************************************************
- function field_search
- -----DESCRIPTION: -----------------------------------
- - generates part of a search query on custom fields
- -----ARGUMENTS: -------------------------------------
- data : the data from the x_def table
- var : the submitted data
- customvar : any extrainput
- tablename : of the field (ie X_def)
- match : for select and checkbox, the kind of boolean logic
- to use; AND for logical AND (all items must be present),
- OR for logical OR (at least one must be present). Defaults
- to AND.
- not : for logical NOT; if false, no changes; if true, the
- final result is preceded by a NOT logical operator.
- -----RETURNS:----------------------------------------
- part of an sql WHERE query used to search upon the custom fields
- *****************************************************/
- function field_search($data, $var, $customvar, $tablename='', $match='and', $not=NULL, $noand=NULL) {
-
- $match = strtolower($match);
- if ((!$match) or (($match != 'and') AND ($match != 'or'))) {
- $match = 'and';
- }
- $match = strtoupper($match);
- if ($tablename) {
- $tablename = $tablename . '.';
- }
- $terms = array();
- // input or textarea
- if (($data['formtype'] == 'input' OR $data['formtype'] == 'textarea') AND (trim($var != ''))) {
- $terms[] = $tablename . $data[name] ." LIKE '" . mysql_escape_string($var) . "'";
-
- // radio
- } elseif ($data['formtype'] == 'radio') {
- $fielddata = unserialize($data['data']);
- foreach ($fielddata AS $key2 => $var2) {
- $values[] = $var2[0];
- }
- if ($customvar) {
- $terms[] = $tablename . $data[name] ." LIKE '" . mysql_escape_string($customvar) . "'";
- } elseif (in_array($var, $values)) {
- $terms[] .= $tablename . $data[name] ." LIKE '%|||$var|||%'";
- }
- // checkbox
- } elseif ($data['formtype'] == 'checkbox') {
- $fielddata = unserialize($data['data']);
- foreach ($fielddata AS $key2 => $var2) {
- $values[] = $var2[0];
- }
- if (!is_array($var)) {
- $var = explode('|||', $var);
- }
- if (is_array($var)) {
- foreach ($var AS $key2 => $var2) {
- if (in_array($var2, $values)) {
- $terms[] = $tablename . $data[name] ." LIKE '%|||$var2|||%'";
- }
- }
- }
-
- // select
- } elseif ($data['formtype'] == 'select') {
- if (!is_array($var)) {
- $var = explode('|||', $var);
- }
- $fielddata = unserialize($data['data']);
- foreach ($fielddata AS $key2 => $var2) {
- $values[] = $var2[0];
- }
- if ($customvar) {
- $where .= $tablename . $data[name] ." LIKE '" . mysql_escape_string($customvar) . "'";
- } elseif (is_array($var)) {
- foreach ($var AS $key2 => $var2) {
- if (in_array($var2, $values)) {
- $terms[] = $tablename . $data[name] ." LIKE '%|||$var2|||%'";
- }
- }
- } elseif ($var) {
- if (in_array($var, $values)) {
- $terms[] = $tablename . $data[name] ." LIKE '%|||$var|||%' ";
- }
- }
- }
-
- $match = " $match ";
- if ($terms) {
- $where = join($match, $terms);
- }
- if ($where) {
- if (!$noand) {
- $query = 'AND';
- }
- if ($not == 'NOT') {
- $query .= ' NOT';
- }
- $query .= " ($where)";
- }
-
- return $where;
- }
- /*****************************************************
- function field_display
- -----DESCRIPTION: -----------------------------------
- - displays custom field form elements
- -----ARGUMENTS: -------------------------------------
- fieldata : the data from the x_def table
- fieldvalue : the value from the database to display
- -----RETURNS:----------------------------------------
- the html to display the element
- *****************************************************/
- function field_display($fielddata, $fieldvalue='', $do_text = 0) {
- // data manipulation
- $data = unserialize($fielddata[data]);
- //////////////////////// SELECT FIELDS ////////////////////////
- if ($fielddata[formtype] == "select") {
- foreach($data AS $key => $var) {
- $data_regex[] = $var[0];
- $data_options[$var[0]] = $var[2];
- }
- // show in select form if select box or as normal text
- $tmp = split('|||', $fieldvalue);
- foreach ($tmp AS $key => $var) {
- if (in_array($var, $data_regex)) {
- $elements[] = $var;
- }
- }
- if (is_array($elements)) {
- if (count($elements) > 5) {
- $rows = 5;
- } else {
- $rows = count($elements);
- }
- if ($fielddata['multiselect']) {
- $multi = "multiple";
- }
- $html .= "<select $multi rows="$count">";
- foreach ($elements AS $key => $var) {
- if ($var) {
- $html .= "<option>" . htmlspecialchars($data_options[$var]) . "</option>";
- $text[] = $data_options[$var];
- $opt_set++;
- }
- }
- if (!$opt_set) {
- $html .= "<option>[None]</option>n";
- }
- $html .= "</select>";
- } elseif ($fielddata[data]) {
- $html = htmlspecialchars($fieldvalue);
- }
- if (is_array($text)) {
- $text = join(', ', $text);
- }
- // if we have nothing so far and we allow extrainput, we display it
- if ($fielddata[extrainput] AND $fieldvalue AND !$text) {
- $text = htmlspecialchars($fieldvalue);
- $html = htmlspecialchars($fieldvalue);
- }
- }
- //////////////////////// TEXTAREA FIELDS ////////////////////////
-
- if ($fielddata[formtype] == "textarea") {
- $html = htmlspecialchars($fieldvalue);
- $text = htmlspecialchars($fieldvalue);
- }
- //////////////////////// INPUT FIELDS ////////////////////////
- if ($fielddata[formtype] == "input") {
- $html = htmlspecialchars($fieldvalue);
- $text = htmlspecialchars($fieldvalue);
- }
-
- //////////////////////// RADIO FIELDS ////////////////////////
-
- if ($fielddata[formtype] == "radio") {
- foreach($data AS $key => $var) {
- $data_regex[] = $var[0];
- $data_options[$var[0]] = $var[2];
- }
- // radio field or custom
- $tmp = explode('|||', $fieldvalue);
- foreach ($tmp AS $key => $var) {
- if (in_array($var, $data_regex)) {
- $html = "<input type="radio" checked="checked" > " .$data_options[$var];
- $text[] = $data_options[$var];
- }
- }
- if (is_array($text)) {
- $text = join(', ', $text);
- }
- // if we have nothing so far and we allow extrainput, we display it
- if ($fielddata[extrainput] AND !$html AND $fieldvalue) {
- $text = htmlspecialchars($fieldvalue);
- $html = htmlspecialchars($fieldvalue);
- }
- }
- //////////////////////// CHECKBOX FIELDS ////////////////////////
- if ($fielddata[formtype] == "checkbox") {
- // radio field or custom
- if (is_string(strstr($fieldvalue, '|||'))) {
- $fieldvalue = explode('|||', $fieldvalue);
- foreach ($data AS $key => $var) {
-
- if (($fielddata[perline]) AND $count >= $fielddata[perline]) {
- $html .= "<br />";
- $count = 0;
- }
- if (in_array($var[0], $fieldvalue)) {
- $html .= $data[$key][2] . " <input type="checkbox" checked="checked" > ";
- $text[] = $data[$key][2];
- $count++;
- }
- }
- if (is_array($text)) {
- if (count($text)) {
- $text = join(', ', $text);
- }
- }
- } else {
- $html = htmlspecialchars($fieldvalue);
- $text = htmlspecialchars($fieldvalue);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- if ($do_text) {
- return $text;
- } else {
- return $html;
- }
- }
- ?>