From 47d32be2b8f1d1dfa915f4648a24510f62e1e9ec Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Mon, 31 Oct 2016 20:57:13 -0400 Subject: [PATCH] Custom fields page uses new UI --- admin/custom_fields.php | 1288 ++++++++++++++++++++++++++++++++++++ inc/show_admin_nav.inc.php | 8 +- language/en/text.php | 1 + 3 files changed, 1293 insertions(+), 4 deletions(-) create mode 100755 admin/custom_fields.php diff --git a/admin/custom_fields.php b/admin/custom_fields.php new file mode 100755 index 00000000..5ab6c61e --- /dev/null +++ b/admin/custom_fields.php @@ -0,0 +1,1288 @@ + +
+ +
+
+ +
+
+ +
+$error\n"; + } + $hesk_error_buffer = $tmp; + + $hesk_error_buffer = $hesklang['rfm'].'

'; + hesk_process_messages($hesk_error_buffer,'custom_fields.php'); + } + + // Add custom field data into database + hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` SET + `use` = '{$cf['use']}', + `place` = '{$cf['place']}', + `type` = '{$cf['type']}', + `req` = '{$cf['req']}', + `category` = ".(count($cf['categories']) ? "'".json_encode($cf['categories'])."'" : 'NULL').", + `name` = '".hesk_dbEscape($cf['names'])."', + `value` = ".(strlen($cf['value']) ? "'".hesk_dbEscape($cf['value'])."'" : 'NULL')." + WHERE `id`={$id}"); + + // Clear cache + clear_cf_cache(); + + // Show success + $_SESSION['cford'] = $id; + hesk_process_messages($hesklang['cf_mdf'],'custom_fields.php','SUCCESS'); + +} // End save_cf() + + +function edit_cf() +{ + global $hesk_settings, $hesklang; + + // Get custom field ID + $id = intval( hesk_GET('id') ) or hesk_error($hesklang['cf_e_id']); + + // Get details from the database + $res = hesk_dbQuery("SELECT * FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` WHERE `id`={$id} LIMIT 1"); + if ( hesk_dbNumRows($res) != 1 ) + { + hesk_error($hesklang['cf_not_found']); + } + $cf = hesk_dbFetchAssoc($res); + + $cf['names'] = json_decode($cf['name'], true); + unset($cf['name']); + + if (strlen($cf['category'])) + { + $cf['categories'] = json_decode($cf['category'], true); + $cf['category'] = 1; + } + else + { + $cf['categories'] = array(); + $cf['category'] = 0; + } + + $_SESSION['new_cf'] = $cf; + $_SESSION['edit_cf'] = true; + +} // End edit_cf() + + +function order_cf() +{ + global $hesk_settings, $hesklang; + + // A security check + hesk_token_check(); + + // Get ID and move parameters + $id = intval( hesk_GET('id') ) or hesk_error($hesklang['cf_e_id']); + $move = intval( hesk_GET('move') ); + $_SESSION['cford'] = $id; + + // Update article details + hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` SET `order`=`order`+".intval($move)." WHERE `id`={$id}"); + + // Update order of all custom fields + update_cf_order(); + + // Clear cache + clear_cf_cache(); + + // Finish + header('Location: custom_fields.php'); + exit(); + +} // End order_cf() + + +function update_cf_order() +{ + global $hesk_settings, $hesklang; + + // Get list of current custom fields + $res = hesk_dbQuery("SELECT `id` FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` WHERE `use` IN ('1','2') ORDER BY `place` ASC, `order` ASC"); + + // Update database + $i = 10; + while ( $cf = hesk_dbFetchAssoc($res) ) + { + hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` SET `order`=".intval($i)." WHERE `id`='".intval($cf['id'])."'"); + $i += 10; + } + + hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` SET `order`=1000 WHERE `use`='0'"); + + return true; + +} // END update_cf_order() + + +function remove_cf() +{ + global $hesk_settings, $hesklang; + + // A security check + hesk_token_check(); + + // Get ID + $id = intval( hesk_GET('id') ) or hesk_error($hesklang['cf_e_id']); + + // Reset the custom field + hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` SET `use`='0', `place`='0', `type`='text', `req`='0', `category`=NULL, `name`='', `value`=NULL, `order`=1000 WHERE `id`={$id}"); + + // Were we successful? + if ( hesk_dbAffectedRows() == 1 ) + { + // Update order + update_cf_order(); + + // Clear cache + clear_cf_cache(); + + // Delete custom field data from tickets + hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."tickets` SET `custom{$id}`=''"); + + // Show success message + hesk_process_messages($hesklang['cf_deleted'],'./custom_fields.php','SUCCESS'); + } + else + { + hesk_process_messages($hesklang['cf_not_found'],'./custom_fields.php'); + } + +} // End remove_cf() + + +function cf_validate() +{ + global $hesk_settings, $hesklang; + global $hesk_error_buffer; + + $hesk_error_buffer = array(); + + // Get names + $cf['names'] = hesk_POST_array('name'); + + // Make sure only valid names pass + foreach ($cf['names'] as $key => $name) + { + if ( ! isset($hesk_settings['languages'][$key])) + { + unset($cf['names'][$key]); + } + else + { + $name = is_array($name) ? '' : hesk_input($name, 0, 0, HESK_SLASH); + + if (strlen($name) < 1) + { + unset($cf['names'][$key]); + } + else + { + $cf['names'][$key] = stripslashes($name); + } + } + } + + // No name entered? + if ( ! count($cf['names'])) + { + $hesk_error_buffer[] = $hesklang['err_custname']; + } + + // Get type and values + $cf['type'] = hesk_POST('type'); + switch ($cf['type']) + { + case 'textarea': + $cf['rows'] = hesk_checkMinMax(intval(hesk_POST('rows')), 1, 100, 12); + $cf['cols'] = hesk_checkMinMax(intval(hesk_POST('cols')), 1, 500, 60); + $cf['value'] = array('rows' => $cf['rows'], 'cols' => $cf['cols']); + break; + + case 'radio': + $cf['radio_options'] = stripslashes(hesk_input(hesk_POST('radio_options'), 0, 0, HESK_SLASH)); + + $options = preg_split("/\\r\\n|\\r|\\n/", $cf['radio_options']); + + $no_default = hesk_POST('no_default') ? 1 : 0; + + $cf['value'] = array('radio_options' => $options, 'no_default' => $no_default); + + if (count($options) < 2) + { + $hesk_error_buffer[] = $hesklang['atl2']; + } + + break; + + case 'select': + $cf['select_options'] = stripslashes(hesk_input(hesk_POST('select_options'), 0, 0, HESK_SLASH)); + + $options = preg_split("/\\r\\n|\\r|\\n/", $cf['select_options']); + + $show_select = hesk_POST('show_select') ? 1 : 0; + + $cf['value'] = array('show_select' => $show_select, 'select_options' => $options); + + if (count($options) < 2) + { + $hesk_error_buffer[] = $hesklang['atl2']; + } + + break; + + case 'checkbox': + $cf['checkbox_options'] = stripslashes(hesk_input(hesk_POST('checkbox_options'), 0, 0, HESK_SLASH)); + + $options = preg_split("/\\r\\n|\\r|\\n/", $cf['checkbox_options']); + + $cf['value'] = array('checkbox_options' => $options); + + if ( ! isset($options[0]) || strlen($options[0]) < 1) + { + $hesk_error_buffer[] = $hesklang['atl1']; + } + + break; + + case 'date': + $cf['dmin'] = ''; + $cf['dmax'] = ''; + + // Minimum date + $dmin_rf = hesk_POST('dmin_rf'); + + if ($dmin_rf == 1) + { + $dmin = hesk_POST('dmin'); + + if (preg_match("/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/", $dmin)) + { + $cf['dmin'] = $dmin; + } + } + elseif ($dmin_rf == 2) + { + $dmin_pm = hesk_POST('dmin_pm') == '+' ? '+' : '-'; + $dmin_num = intval(hesk_POST('dmin_num', 0)); + $dmin_type = hesk_POST('dmin_type'); + if ( ! in_array($dmin_type, array('day', 'week', 'month', 'year'))) + { + $dmin_type = 'day'; + } + + $cf['dmin'] = $dmin_pm . $dmin_num . ' ' . $dmin_type; + } + + // Maximum date + $dmax_rf = hesk_POST('dmax_rf'); + + if ($dmax_rf == 1) + { + $dmax = hesk_POST('dmax'); + + if (preg_match("/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/", $dmax)) + { + $cf['dmax'] = $dmax; + } + } + elseif ($dmax_rf == 2) + { + $dmax_pm = hesk_POST('dmax_pm') == '+' ? '+' : '-'; + $dmax_num = intval(hesk_POST('dmax_num', 0)); + $dmax_type = hesk_POST('dmax_type'); + if ( ! in_array($dmax_type, array('day', 'week', 'month', 'year'))) + { + $dmax_type = 'day'; + } + + $cf['dmax'] = $dmax_pm . $dmax_num . ' ' . $dmax_type; + } + + // Minimum date should not be higher than maximum date + if (strlen($cf['dmin']) && strlen($cf['dmax'])) + { + if (strtotime($cf['dmin']) > strtotime($cf['dmax'])) + { + $hesk_error_buffer[] = $hesklang['d_mm']; + } + } + + // Date format + $date_format = hesk_POST('date_format'); + if ($date_format == 'custom') + { + $date_format = hesk_POST('date_format_custom'); + } + + $cf['date_format'] = preg_replace('/[^a-zA-Z0-9 \/\.\_+\-,;:#(){}\[\]\'@*]/', '', $date_format); + + $cf['value'] = array('dmin' => $cf['dmin'], 'dmax' => $cf['dmax'], 'date_format' => $cf['date_format']); + + break; + + case 'email': + $cf['email_multi'] = hesk_POST('email_multi') ? 1 : 0; + $cf['value'] = array('multiple' => $cf['email_multi']); + break; + + case 'hidden': + $cf['hidden_max_length'] = hesk_checkMinMax(intval(hesk_POST('hidden_max_length')), 1, 10000, 255); + $cf['hidden_default_value'] = stripslashes(hesk_input(hesk_POST('hidden_default_value'), 0, 0, HESK_SLASH)); + $cf['value'] = array('max_length' => $cf['hidden_max_length'], 'default_value' => $cf['hidden_default_value']); + break; + + default: + $cf['type'] = 'text'; + $cf['max_length'] = hesk_checkMinMax(intval(hesk_POST('max_length')), 1, 10000, 255); + $cf['default_value'] = stripslashes(hesk_input(hesk_POST('default_value'), 0, 0, HESK_SLASH)); + $cf['value'] = array('max_length' => $cf['max_length'], 'default_value' => $cf['default_value']); + + } + + // Enable + $cf['use'] = hesk_POST('use') == 2 ? 2 : 1; + + // req + $cf['req'] = hesk_POST('req'); + $cf['req'] = $cf['req'] == 2 ? 2 : ($cf['req'] == 1 ? 1 : 0); + + // Private fields cannot be req for customers + if ($cf['use'] == 2 && $cf['req'] == 1) + { + $cf['req'] = 0; + } + + // Located above or below "Message"? + $cf['place'] = hesk_POST('place') ? 1 : 0; + + // Get allowed categories + if (hesk_POST('category')) + { + $cf['category'] = 1; + $cf['categories'] = hesk_POST_array('categories'); + + foreach ($cf['categories'] as $key => $cat_id) + { + if ( ! isset($hesk_settings['categories'][$cat_id]) ) + { + unset($cf['categories'][$key]); + } + } + + if ( ! count($cf['categories'])) + { + $hesk_error_buffer[] = $hesklang['cf_nocat']; + } + } + else + { + $cf['category'] = 0; + $cf['categories'] = array(); + } + + // Any errors? + if (count($hesk_error_buffer)) + { + $_SESSION['new_cf'] = $cf; + return false; + } + + $cf['names'] = addslashes(json_encode($cf['names'])); + $cf['value'] = $cf['type'] == 'date' ? json_encode($cf['value']) : addslashes(json_encode($cf['value'])); + + return $cf; +} // END cf_validate() + + +function new_cf() +{ + global $hesk_settings, $hesklang; + global $hesk_error_buffer; + + // A security check + # hesk_token_check('POST'); + + // Validate inputs + if (($cf = cf_validate()) == false) + { + $tmp = ''; + foreach ($hesk_error_buffer as $error) + { + $tmp .= "
  • $error
  • \n"; + } + $hesk_error_buffer = $tmp; + + $hesk_error_buffer = $hesklang['rfm'].'

    '; + hesk_process_messages($hesk_error_buffer,'custom_fields.php'); + } + + // Get the lowest available custom field ID + $res = hesk_dbQuery("SELECT `id` FROM `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` WHERE `use`='0' ORDER BY `id` ASC LIMIT 1"); + $row = hesk_dbFetchRow($res); + $_SESSION['cford'] = intval($row[0]); + + // Insert custom field into database + hesk_dbQuery("UPDATE `".hesk_dbEscape($hesk_settings['db_pfix'])."custom_fields` SET + `use` = '{$cf['use']}', + `place` = '{$cf['place']}', + `type` = '{$cf['type']}', + `req` = '{$cf['req']}', + `category` = ".(count($cf['categories']) ? "'".json_encode($cf['categories'])."'" : 'NULL').", + `name` = '".hesk_dbEscape($cf['names'])."', + `value` = ".(strlen($cf['value']) ? "'".hesk_dbEscape($cf['value'])."'" : 'NULL').", + `order` = 990 + WHERE `id`={$_SESSION['cford']}"); + + // Update order + update_cf_order(); + + // Clear cache + clear_cf_cache(); + + // Show success + hesk_process_messages($hesklang['cf_added'],'custom_fields.php','SUCCESS'); + +} // End new_cf() + + +function clear_cf_cache() +{ + global $hesk_settings; + + $cache_dir = dirname(dirname(__FILE__)).'/'.$hesk_settings['attach_dir'].'/cf/'; + + if (is_dir($cache_dir)) + { + $files = preg_grep('/index\.htm$/', glob($cache_dir.'*', GLOB_NOSORT), PREG_GREP_INVERT); + array_walk($files, 'hesk_unlink'); + } + + return true; + +} // END clear_cf_cache() diff --git a/inc/show_admin_nav.inc.php b/inc/show_admin_nav.inc.php index 32ea57e6..bd79a55e 100644 --- a/inc/show_admin_nav.inc.php +++ b/inc/show_admin_nav.inc.php @@ -125,10 +125,6 @@ $mails = mfh_get_mail_headers_for_dropdown($_SESSION['id'], $hesk_settings, $hes
  • - -

    @@ -382,6 +378,10 @@ $mails = mfh_get_mail_headers_for_dropdown($_SESSION['id'], $hesk_settings, $hes $tools_count++; $dropdown_items['manage_statuses'] = $hesklang['manage_statuses']; } + if (hesk_checkPermission('can_man_settings', 0)) { + $tools_count++; + $dropdown_items['custom_fields'] = $hesklang['manage_custom_fields']; + } if (hesk_checkPermission('can_view_logs', 0)) { $tools_count++; $dropdown_items['view_message_log'] = $hesklang['view_message_log']; diff --git a/language/en/text.php b/language/en/text.php index f514a8d3..d9aa9937 100644 --- a/language/en/text.php +++ b/language/en/text.php @@ -66,6 +66,7 @@ $hesklang['generated_token_colon'] = 'Generated Token:'; $hesklang['record_this_token_warning'] = 'Please record this token, as this is the only time you will be able to view it!'; $hesklang['all_tokens_revoked'] = 'All tokens for this user have been revoked'; $hesklang['staff_login_title'] = 'Staff Login'; +$hesklang['manage_custom_fields'] = 'Manage Custom Fields'; // ADDED OR MODIFIED IN Mods for HESK 2.6.0 $hesklang['search_logs'] = 'Search Logs';