From 2e6d865c7d07b48254228cba07e082d42a53ed28 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Sat, 29 Apr 2017 00:44:25 +0200 Subject: [PATCH] NEW Can define default values for create forms. NEW Can define default filters for list pages. NEW Can define default sort order for list pages. --- htdocs/admin/defaultvalues.php | 95 +++++++++++++++++++++++++------ htdocs/admin/dict.php | 2 +- htdocs/core/db/DoliDB.class.php | 17 ++++-- htdocs/core/lib/functions.lib.php | 53 ++++++++++++----- htdocs/langs/en_US/admin.lang | 4 +- htdocs/theme/eldy/style.css.php | 2 +- 6 files changed, 135 insertions(+), 38 deletions(-) diff --git a/htdocs/admin/defaultvalues.php b/htdocs/admin/defaultvalues.php index 96fef57e7cf..8a9bd4f209b 100644 --- a/htdocs/admin/defaultvalues.php +++ b/htdocs/admin/defaultvalues.php @@ -56,6 +56,10 @@ $defaultvalue = GETPOST('defaultvalue'); $defaulturl=preg_replace('/^\//', '', $defaulturl); +$urlpage = GETPOST('urlpage'); +$key = GETPOST('key'); +$value = GETPOST('value'); + /* * Actions @@ -81,25 +85,51 @@ if (GETPOST("button_removefilter_x") || GETPOST("button_removefilter.x") || GETP } -if ($action == 'add' || (GETPOST('add') && $action != 'update')) +if (($action == 'add' || (GETPOST('add') && $action != 'update')) || GETPOST('actionmodify')) { $error=0; - if (empty($defaulturl)) + if (($action == 'add' || (GETPOST('add') && $action != 'update'))) { - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors'); - $error++; + if (empty($defaulturl)) + { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors'); + $error++; + } + if (empty($defaultkey)) + { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors'); + $error++; + } } - if (empty($defaultkey)) + if (GETPOST('actionmodify')) { - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Key")), null, 'errors'); - $error++; + if (empty($urlpage)) + { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Url")), null, 'errors'); + $error++; + } + if (empty($key)) + { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Field")), null, 'errors'); + $error++; + } } + if (! $error) { $db->begin(); - - $sql = "INSERT INTO ".MAIN_DB_PREFIX."default_values(type, user_id, page, param, value, entity) VALUES ('".$db->escape($mode)."', 0, '".$db->escape($defaulturl)."','".$db->escape($defaultkey)."','".$db->escape($defaultvalue)."', ".$db->escape($conf->entity).")"; + + if ($action == 'add' || (GETPOST('add') && $action != 'update')) + { + $sql = "INSERT INTO ".MAIN_DB_PREFIX."default_values(type, user_id, page, param, value, entity) VALUES ('".$db->escape($mode)."', 0, '".$db->escape($defaulturl)."','".$db->escape($defaultkey)."','".$db->escape($defaultvalue)."', ".$db->escape($conf->entity).")"; + } + if (GETPOST('actionmodify')) + { + $sql = "UPDATE ".MAIN_DB_PREFIX."default_values SET page = '".$db->escape($urlpage)."', param = '".$db->escape($key)."', value = '".$db->escape($value)."'"; + $sql.= " WHERE rowid = ".$id; + } + $result = $db->query($sql); if ($result > 0) { @@ -176,6 +206,10 @@ $head=defaultvalues_prepare_head(); dol_fiche_head($head, $mode, '', -1, ''); +if ($mode == 'sortorder') +{ + print info_admin($langs->trans("WarningSettingSortOrder")).'<br>'; +} print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">'; print '<input type="hidden" id="action" name="action" value="">'; @@ -189,8 +223,15 @@ else $texthelp.=$langs->trans("PageUrlForDefaultValuesList", 'societe/list.php') $texturl=$form->textwithpicto($langs->trans("Url"), $texthelp); print_liste_field_titre($texturl,$_SERVER["PHP_SELF"],'page,param','',$param,'',$sortfield,$sortorder); $texthelp=$langs->trans("TheKeyIsTheNameOfHtmlField"); -if ($mode != 'sortorder') $textkey=$form->textwithpicto($langs->trans("Key"), $texthelp); -else $textkey=$form->textwithpicto($langs->trans("Key"), $texthelp); +if ($mode != 'sortorder') +{ + $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp); +} +else +{ + $texthelp='field or alias.field'; + $textkey=$form->textwithpicto($langs->trans("Field"), $texthelp); +} print_liste_field_titre($textkey,$_SERVER["PHP_SELF"],'param','',$param,'',$sortfield,$sortorder); if ($mode != 'sortorder') { @@ -270,9 +311,17 @@ if ($result) print '<tr class="oddeven">'; - print '<td>'.$obj->page.'</td>'."\n"; - print '<td>'.$obj->param.'</td>'."\n"; - + print '<td>'; + if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->page; + else print '<input type="text" name="urlpage" value="'.dol_escape_htmltag($obj->page).'">'; + print '</td>'."\n"; + + // Key + print '<td>'; + if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->param; + else print '<input type="text" name="key" value="'.dol_escape_htmltag($obj->param).'">'; + print '</td>'."\n"; + // Value print '<td>'; /*print '<input type="hidden" name="const['.$i.'][rowid]" value="'.$obj->rowid.'">'; @@ -280,11 +329,25 @@ if ($result) print '<input type="hidden" name="const['.$i.'][name]" value="'.$obj->transkey.'">'; print '<input type="text" id="value_'.$i.'" class="flat inputforupdate" size="30" name="const['.$i.'][value]" value="'.dol_escape_htmltag($obj->transvalue).'">'; */ - print $obj->value; + if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) print $obj->value; + else print '<input type="text" name="value" value="'.dol_escape_htmltag($obj->value).'">'; print '</td>'; print '<td align="center">'; - print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&mode='.$mode.'&action=delete'.((empty($user->entity) && $debug)?'&debug=1':'').'">'.img_delete().'</a>'; + if ($action != 'edit' || GETPOST('rowid') != $obj->rowid) + { + print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&mode='.$mode.'&action=edit'.((empty($user->entity) && $debug)?'&debug=1':'').'">'.img_edit().'</a>'; + print ' '; + print '<a href="'.$_SERVER['PHP_SELF'].'?rowid='.$obj->rowid.'&entity='.$obj->entity.'&mode='.$mode.'&action=delete'.((empty($user->entity) && $debug)?'&debug=1':'').'">'.img_delete().'</a>'; + } + else + { + print '<input type="hidden" name="page" value="'.$page.'">'; + print '<input type="hidden" name="rowid" value="'.$id.'">'; + print '<div name="'.(! empty($obj->rowid)?$obj->rowid:'none').'"></div>'; + print '<input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">'; + print '<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'">'; + } print '</td>'; print "</tr>\n"; diff --git a/htdocs/admin/dict.php b/htdocs/admin/dict.php index c8632f57772..125f913eb8d 100644 --- a/htdocs/admin/dict.php +++ b/htdocs/admin/dict.php @@ -1306,10 +1306,10 @@ if ($id) if (empty($reshook)) fieldList($fieldlist,$obj,$tabname[$id],'edit'); print '<td colspan="3" align="center">'; + print '<div name="'.(! empty($obj->rowid)?$obj->rowid:$obj->code).'"></div>'; print '<input type="hidden" name="page" value="'.$page.'">'; print '<input type="hidden" name="rowid" value="'.$rowid.'">'; print '<input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">'; - print '<div name="'.(! empty($obj->rowid)?$obj->rowid:$obj->code).'"></div>'; print '<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'">'; print '</td>'; } diff --git a/htdocs/core/db/DoliDB.class.php b/htdocs/core/db/DoliDB.class.php index c63227e1601..9fc1739334c 100644 --- a/htdocs/core/db/DoliDB.class.php +++ b/htdocs/core/db/DoliDB.class.php @@ -220,9 +220,9 @@ abstract class DoliDB implements Database /** * Define sort criteria of request * - * @param string $sortfield List of sort fields, separated by comma. Example: 't1.fielda, t2.fieldb' + * @param string $sortfield List of sort fields, separated by comma. Example: 't1.fielda, t2.fieldb' * @param 'ASC'|'DESC' $sortorder Sort order - * @return string String to provide syntax of a sort sql string + * @return string String to provide syntax of a sort sql string */ function order($sortfield=null,$sortorder=null) { @@ -230,18 +230,25 @@ abstract class DoliDB implements Database { $return=''; $fields=explode(',',$sortfield); + $orders=explode(',',$sortorder); + $i=0; foreach($fields as $val) { if (! $return) $return.=' ORDER BY '; - else $return.=','; + else $return.=', '; $return.=preg_replace('/[^0-9a-z_\.]/i','',$val); + + $tmpsortorder = trim($orders[$i]); + // Only ASC and DESC values are valid SQL - if (strtoupper($sortorder) === 'ASC') { + if (strtoupper($tmpsortorder) === 'ASC') { $return .= ' ASC'; - } elseif (strtoupper($sortorder) === 'DESC') { + } elseif (strtoupper($tmpsortorder) === 'DESC') { $return .= ' DESC'; } + + $i++; } return $return; } diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 7743da27c2d..0d54593f0db 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -246,16 +246,16 @@ function dol_shutdown() * @param mixed $options Options to pass to filter_var when $check is set to custom * @return string|string[] Value found (string or array), or '' if check fails */ -function GETPOST($paramname,$check='',$method=0,$filter=NULL,$options=NULL) +function GETPOST($paramname, $check='', $method=0, $filter=NULL, $options=NULL) { if (empty($method)) { $out = isset($_GET[$paramname])?$_GET[$paramname]:(isset($_POST[$paramname])?$_POST[$paramname]:''); - + // Management of default values - if (! isset($_GET['sortfield'])) // If we did a click on a field so sort, we do no appl default values + if (! isset($_GET['sortfield'])) // If we did a click on a field to sort, we do no apply default values { - if (! empty($_GET['action']) && $_GET['action'] == 'create' && ! empty($paramname) && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname])) + if (! empty($_GET['action']) && $_GET['action'] == 'create' && ! empty($paramname) && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname])) { $relativepathstring = $_SERVER["PHP_SELF"]; if (constant('DOL_URL_ROOT')) $relativepathstring = preg_replace('/^'.preg_quote(constant('DOL_URL_ROOT'),'/').'/', '', $relativepathstring); @@ -268,18 +268,40 @@ function GETPOST($paramname,$check='',$method=0,$filter=NULL,$options=NULL) if (isset($user->default_values[$relativepathstring]['createform'][$paramname])) $out = $user->default_values[$relativepathstring]['createform'][$paramname]; } } - // Management of default search_filters + // Management of default search_filters and sort order elseif (preg_match('/list.php$/', $_SERVER["PHP_SELF"]) && ! empty($paramname) && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname])) { - $relativepathstring = $_SERVER["PHP_SELF"]; + $relativepathstring = $_SERVER["PHP_SELF"]; if (constant('DOL_URL_ROOT')) $relativepathstring = preg_replace('/^'.preg_quote(constant('DOL_URL_ROOT'),'/').'/', '', $relativepathstring); $relativepathstring = preg_replace('/^custom\//', '', $relativepathstring); $relativepathstring = preg_replace('/^\//', '', $relativepathstring); global $user; if (! empty($user->default_values)) // $user->default_values defined from menu default values, and values loaded not at first { - //var_dump($user->default_values[$relativepathstring]); - if (isset($user->default_values[$relativepathstring]['filters'][$paramname])) $out = $user->default_values[$relativepathstring]['filters'][$paramname]; + //var_dump($user->default_values[$relativepathstring]); + if ($paramname == 'sortfield') + { + if (isset($user->default_values[$relativepathstring]['sortorder'])) + { + foreach($user->default_values[$relativepathstring]['sortorder'] as $key => $val) + { + if ($out) $out.=', '; + $out.=$key; + } + } + } + elseif ($paramname == 'sortorder') + { + if (isset($user->default_values[$relativepathstring]['sortorder'])) + { + foreach($user->default_values[$relativepathstring]['sortorder'] as $key => $val) + { + if ($out) $out.=', '; + $out.=$val; + } + } + } + elseif (isset($user->default_values[$relativepathstring]['filters'][$paramname])) $out = $user->default_values[$relativepathstring]['filters'][$paramname]; } } } @@ -3178,6 +3200,9 @@ function getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $m $tag='th'; if ($thead==2) $tag='div'; + $tmpsortfield=explode(',',$sortfield); + $sortfield=trim($tmpsortfield[0]); + // If field is used as sort criteria we use a specific class // Example if (sortfield,field)=("nom","xxx.nom") or (sortfield,field)=("nom","nom") if ($field && ($sortfield == $field || $sortfield == preg_replace("/^[^\.]+\./","",$field))) $out.= '<'.$tag.' class="'.$prefix.'liste_titre_sel" '. $moreattrib.'>'; @@ -3192,13 +3217,13 @@ function getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $m if ($field != $sortfield) { - if ($sortorder == 'DESC') $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'; - if ($sortorder == 'ASC' || ! $sortorder) $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'; + if (preg_match('/^DESC/', $sortorder)) $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'; + else $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'; } else { - if ($sortorder == 'DESC' || ! $sortorder) $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'; - if ($sortorder == 'ASC') $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'; + if (preg_match('/^ASC/', $sortorder)) $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'; + else $out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'; } } @@ -3227,12 +3252,12 @@ function getTitleFieldOfList($name, $thead=0, $file="", $field="", $begin="", $m } else { - if ($sortorder == 'DESC' ) { + if (preg_match('/^DESC/', $sortorder)) { //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'.img_down("A-Z",0).'</a>'; //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'.img_up("Z-A",1).'</a>'; $sortimg.= '<span class="nowrap">'.img_up("Z-A",0).'</span>'; } - if ($sortorder == 'ASC' ) { + if (preg_match('/^ASC/', $sortorder)) { //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=asc&begin='.$begin.$options.'">'.img_down("A-Z",1).'</a>'; //$out.= '<a href="'.$file.'?sortfield='.$field.'&sortorder=desc&begin='.$begin.$options.'">'.img_up("Z-A",0).'</a>'; $sortimg.= '<span class="nowrap">'.img_down("A-Z",0).'</span>'; diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 1bfa5c0230c..69c72fc7aad 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -427,11 +427,13 @@ WarningPHPMail=WARNING: Some email providers (like Yahoo) does not allow you to ClickToShowDescription=Click to show description DependsOn=This module need the module(s) RequiredBy=This module is required by module(s) -TheKeyIsTheNameOfHtmlField=The key is the name of the html field. This need to have technical knowledges to read the content of the HTML page to get the key name of a field. +TheKeyIsTheNameOfHtmlField=This is the name of the HTML field. This need to have technical knowledges to read the content of the HTML page to get the key name of a field. PageUrlForDefaultValues=You must enter here the relative url of the page. If you include parameters in URL, the default values will be effective if all parameters are set to same value. Examples: PageUrlForDefaultValuesCreate=<br>For form to create a new thirdparty, it is <strong>%s</strong> PageUrlForDefaultValuesList=<br>For page that list thirdparties, it is <strong>%s</strong> GoIntoTranslationMenuToChangeThis=A translation has been found for the key with this code, so to change this value, you must edit it fom Home-Setup-translation. +WarningSettingSortOrder=Warning, setting a default sort order may result in a technical error when going on the list page if field is an unknown field. If you experience such an error, come back to this page to remove the default sort order to retreive default behavior. +Field=Field # Modules Module0Name=Users & groups Module0Desc=Users / Employees and Groups management diff --git a/htdocs/theme/eldy/style.css.php b/htdocs/theme/eldy/style.css.php index f0e615f29ce..4d711599992 100644 --- a/htdocs/theme/eldy/style.css.php +++ b/htdocs/theme/eldy/style.css.php @@ -253,7 +253,7 @@ a.tab { font-weight: bold !important; } a:link, a:visited, a:hover, a:active { font-family: <?php print $fontlist ?>; font-weight: normal; color: rgb(<?php print $colortextlink; ?>); text-decoration: none; } a:hover { text-decoration: underline; color: rgb(<?php print $colortextlink; ?>); } a.commonlink { color: rgb(<?php print $colortextlink; ?>) !important; text-decoration: none; } - +th.liste_titre a div div:hover, th.liste_titre_sel a div div:hover { text-decoration: underline; } input, input.flat, textarea, textarea.flat, form.flat select, select, select.flat, .dataTables_length label select { background-color: #FFF; } -- GitLab