diff --git a/htdocs/core/actions_extrafields.inc.php b/htdocs/core/actions_extrafields.inc.php index 17b48ab42bc..816699e8989 100644 --- a/htdocs/core/actions_extrafields.inc.php +++ b/htdocs/core/actions_extrafields.inc.php @@ -90,6 +90,12 @@ if ($action == 'add') { $mesg[] = $langs->trans("ErrorSizeTooLongForIntType", $maxsizeint); $action = 'create'; } + if ($type == 'stars' && ($extrasize < 1 || $extrasize > 10)) { + $error++; + $langs->load("errors"); + $mesg[] = $langs->trans("ErrorSizeForStarsType"); + $action = 'create'; + } if ($type == 'select' && !$param) { $error++; $langs->load("errors"); @@ -282,6 +288,12 @@ if ($action == 'update') { $mesg[] = $langs->trans("ErrorNoValueForSelectListType"); $action = 'edit'; } + if ($type == 'stars' && ($extrasize < 1|| $extrasize > 10)) { + $error++; + $langs->load("errors"); + $mesg[] = $langs->trans("ErrorSizeForStarsType"); + $action = 'edit'; + } if ($type == 'checkbox' && !$param) { $error++; $langs->load("errors"); diff --git a/htdocs/core/ajax/editextrafield.php b/htdocs/core/ajax/editextrafield.php new file mode 100644 index 00000000000..1cc4178fbc8 --- /dev/null +++ b/htdocs/core/ajax/editextrafield.php @@ -0,0 +1,80 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/ajax/editextrafield.php + * \brief File to return Ajax response for stars extrafield request + */ + +if (!defined('NOTOKENRENEWAL')) { + define('NOTOKENRENEWAL', 1); // Disables token renewal +} +if (!defined('NOREQUIREMENU')) { + define('NOREQUIREMENU', '1'); +} +if (!defined('NOREQUIREHTML')) { + define('NOREQUIREHTML', '1'); +} +if (!defined('NOREQUIREAJAX')) { + define('NOREQUIREAJAX', '1'); +} +if (!defined('NOREQUIRESOC')) { + define('NOREQUIRESOC', '1'); +} +if (!defined('NOCSRFCHECK')) { + define('NOCSRFCHECK', '1'); +} +if (!defined('NOREQUIREHTML')) { + define('NOREQUIREHTML', '1'); +} + +// Load Dolibarr environment +include '../../main.inc.php'; + +$objectType = GETPOST('objectType', 'aZ09'); +$objectId = GETPOST('objectId', 'aZ09'); +$field = GETPOST('field', 'aZ09'); +$value = GETPOST('value', 'aZ09'); + +$module = getElementProperties($objectType)['module']; +$object = fetchObjectByElement($objectId, $objectType); + +// Security check +if (!$user->hasRight($module, $object->element, 'write') && !$user->hasRight($module, 'write')) { + accessforbidden(); +} + +/* + * View + */ + +dol_syslog("Call ajax core/ajax/editextrafield.php"); + +top_httphead(); + +// Update the object field with the new value +if ($object->id > 0 && $field && isset($value)) { + $object->array_options['options_'.$field] = $value; + $result = $object->update($user); + if ($result < 0) { + print json_encode(['status' => 'error', 'message' => 'Error updating '. $field]); + } else { + print json_encode(['status' => 'success', 'message' => $field . ' updated successfully']); + } +} + +$db->close(); diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 5416fc64f39..c1a76a53a9e 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -101,6 +101,7 @@ class ExtraFields 'linestrg' => 'ExtrafieldLinestringGeo', 'polygon' => 'ExtrafieldPolygonGeo', 'separate' => 'ExtrafieldSeparator', + 'stars' => 'ExtrafieldStars', ); /** @@ -337,6 +338,9 @@ class ExtraFields } elseif ($type == 'password') { $typedb = 'varchar'; $lengthdb = '128'; + } elseif ($type == 'stars') { + $typedb = 'int'; + $lengthdb = $length; } else { $typedb = $type; $lengthdb = $length; @@ -714,6 +718,9 @@ class ExtraFields } elseif ($type == 'password') { $typedb = 'varchar'; $lengthdb = '128'; + } elseif ($type == 'stars') { + $typedb = 'int'; + $lengthdb = $length; } else { $typedb = $type; $lengthdb = $length; @@ -1871,6 +1878,45 @@ class ExtraFields // If prefix is 'search_', field is used as a filter, we use a common text field. $out = ''; // Hidden field to reduce impact of evil Google Chrome autopopulate bug. $out .= ''; + } elseif ($type == 'stars') { + $out = ''; + $out .= '
'; + $i = 1; + while ($i <= $size) { + $out .= ''.img_picto('', 'fontawesome_star_fas').''; + $i++; + } + $out .= '
'; + $out .= ''; } if (!empty($hidden)) { $out = ''; @@ -1898,7 +1944,7 @@ class ExtraFields */ public function showOutputField($key, $value, $moreparam = '', $extrafieldsobjectkey = '', $outputlangs = null) { - global $conf, $langs; + global $conf, $langs, $object; if (is_null($outputlangs) || !is_object($outputlangs)) { $outputlangs = $langs; @@ -2256,6 +2302,63 @@ class ExtraFields $value = dol_htmlentitiesbr($value); } elseif ($type == 'password') { $value = dol_trunc(preg_replace('/./i', '*', $value), 8, 'right', 'UTF-8', 1); + } elseif ($type == 'stars') { + $value = ''; + $value .= '
'; + $i = 1; + while ($i <= $size) { + $value .= ''.img_picto('', 'fontawesome_star_fas').''; + $i++; + } + $value .= '
'; + $value .= ''; } else { $showsize = round((float) $size); if ($showsize > 48) { diff --git a/htdocs/core/tpl/admin_extrafields_add.tpl.php b/htdocs/core/tpl/admin_extrafields_add.tpl.php index 123ec9dcc1d..24e4d15e894 100644 --- a/htdocs/core/tpl/admin_extrafields_add.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_add.tpl.php @@ -107,6 +107,7 @@ $listofexamplesforlink = 'Societe:societe/class/societe.class.php
Contact:con else if (type == 'point') { size.val('').prop('disabled', true); unique.removeAttr('disabled'); jQuery("#value_choice").show(); jQuery(".spanforparamtooltip").hide(); jQuery("#helplink").show();} else if (type == 'linestrg') { size.val('').prop('disabled', true); unique.removeAttr('disabled'); jQuery("#value_choice").show(); jQuery(".spanforparamtooltip").hide(); jQuery("#helplink").show();} else if (type == 'polygon') { size.val('').prop('disabled', true); unique.removeAttr('disabled'); jQuery("#value_choice").show(); jQuery(".spanforparamtooltip").hide(); jQuery("#helplink").show();} + else if (type == 'stars') { size.val('5').removeAttr('disabled'); unique.removeAttr('disabled'); jQuery("#value_choice").hide(); jQuery("#helpchkbxlst").hide();} else if (type == 'separate') { langfile.val('').prop('disabled',true);size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); required.val('').prop('disabled', true); jQuery("#value_choice").show(); diff --git a/htdocs/core/tpl/admin_extrafields_edit.tpl.php b/htdocs/core/tpl/admin_extrafields_edit.tpl.php index 215cefa9444..c1ee397732a 100644 --- a/htdocs/core/tpl/admin_extrafields_edit.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_edit.tpl.php @@ -103,6 +103,7 @@ $listofexamplesforlink = 'Societe:societe/class/societe.class.php
Contact:con else if (type == 'checkbox') { size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); jQuery("#value_choice").show(); jQuery(".spanforparamtooltip").hide(); jQuery("#helpselect").show();} else if (type == 'chkbxlst') { size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); jQuery("#value_choice").show(); jQuery(".spanforparamtooltip").hide(); jQuery("#helpchkbxlst").show();} else if (type == 'link') { size.val('').prop('disabled', true); unique.removeAttr('disabled'); jQuery("#value_choice").show(); jQuery(".spanforparamtooltip").hide(); jQuery("#helplink").show();} + else if (type == 'stars') { size.removeAttr('disabled'); unique.removeAttr('disabled'); jQuery("#value_choice").hide(); jQuery("#helpchkbxlst").hide();} else if (type == 'separate') { size.val('').prop('disabled', true); unique.removeAttr('checked').prop('disabled', true); required.val('').prop('disabled', true); default_value.val('').prop('disabled', true); jQuery("#value_choice").show(); diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 46f1cb43fd1..913ee3b72ed 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -466,6 +466,7 @@ ExtrafieldPointGeo=Geometric Point ExtrafieldMultiPointGeo=Geometric Multi Point ExtrafieldLinestringGeo=Geometric Linestring ExtrafieldPolygonGeo=Geometric Polygon +ExtrafieldStars=Stars ComputedFormula=Computed field ComputedFormulaDesc=You can enter here a formula using other properties of object or any PHP coding to get a dynamic computed value. You can use any PHP compatible formulas including the "?" condition operator, and following global object: $db, $conf, $langs, $mysoc, $user, $objectoffield.
WARNING: If you need properties of an object not loaded, just fetch yourself the object into your formula like in the second example.
Using a computed field means you can't enter yourself any value from interface. Also, if there is a syntax error, the formula may return nothing.

Example of formula:
$objectoffield->id < 10 ? round($objectoffield->id / 2, 2): ($objectoffield->id + 2 * $user->id) * (int) substr($mysoc->zip, 1, 2)

Example to reload object
(($reloadedobj = new Societe($db)) && ($reloadedobj->fetchNoCompute($objectoffield->id) > 0 ? $reloadedobj->array_options['options_extrafieldkey'] * $reloadedobj->capital / 5: '-1')

Other example of formula to force load of object and its parent object:
(($reloadedobj = new Task($db)) && ($reloadedobj->fetchNoCompute($objectoffield->id) > 0) && ($secondloadedobj = new Project($db)) && ($secondloadedobj->fetchNoCompute($reloadedobj->fk_project) > 0)) ? $secondloadedobj->ref: 'Parent project not found' Computedpersistent=Store computed field diff --git a/htdocs/langs/en_US/errors.lang b/htdocs/langs/en_US/errors.lang index f98573a5bd9..02c07e6ca87 100644 --- a/htdocs/langs/en_US/errors.lang +++ b/htdocs/langs/en_US/errors.lang @@ -77,6 +77,7 @@ ErrorUploadBlockedByAddon=Upload blocked by a PHP/Apache plugin. ErrorFileSizeTooLarge=File size is too large or file not provided. ErrorFieldTooLong=Field %s is too long. ErrorSizeTooLongForIntType=Size too long for int type (%s digits maximum) +ErrorSizeForStarsType=The star size must be a number between 1 and 10 ErrorSizeTooLongForVarcharType=Size too long for string type (%s chars maximum) ErrorNoValueForSelectType=Please fill value for select list ErrorNoValueForCheckBoxType=Please fill value for checkbox list