From defce63a24456d2f57e14bdfa09faa282ed138aa Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Fri, 28 Apr 2017 14:05:28 +0200
Subject: [PATCH] Work on management of default values

---
 htdocs/admin/defaultvalues.php    |  9 ++++---
 htdocs/core/lib/functions.lib.php | 43 ++++++++++++++++++++++++-------
 htdocs/langs/en_US/admin.lang     |  2 +-
 htdocs/master.inc.php             |  2 +-
 htdocs/user/class/user.class.php  |  4 +--
 5 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/htdocs/admin/defaultvalues.php b/htdocs/admin/defaultvalues.php
index 0bc620c41bf..96fef57e7cf 100644
--- a/htdocs/admin/defaultvalues.php
+++ b/htdocs/admin/defaultvalues.php
@@ -187,29 +187,30 @@ $texthelp=$langs->trans("PageUrlForDefaultValues");
 if ($mode == 'createform') $texthelp.=$langs->trans("PageUrlForDefaultValuesCreate", 'societe/card.php');
 else $texthelp.=$langs->trans("PageUrlForDefaultValuesList", 'societe/list.php');
 $texturl=$form->textwithpicto($langs->trans("Url"), $texthelp);
-print_liste_field_titre($texturl,$_SERVER["PHP_SELF"],'defaulturl','',$param,'',$sortfield,$sortorder);
+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);
-print_liste_field_titre($textkey,$_SERVER["PHP_SELF"],'defaultkey','',$param,'',$sortfield,$sortorder);
+print_liste_field_titre($textkey,$_SERVER["PHP_SELF"],'param','',$param,'',$sortfield,$sortorder);
 if ($mode != 'sortorder')
 {
     $texthelp=$langs->trans("FollowingConstantsWillBeSubstituted").'<br>';
     // See list into GETPOST
     $texthelp.='__USERID__<br>';
+    $texthelp.='__SUPERVISORID__<br>';
     $texthelp.='__MYCOUNTRYID__<br>';
     $texthelp.='__DAY__<br>';
     $texthelp.='__MONTH__<br>';
     $texthelp.='__YEAR__<br>';
     if (! empty($conf->multicompany->enabled)) $texthelp.='__ENTITYID__<br>';
-    $textvalue=$form->textwithpicto($langs->trans("Value"), $texthelp);
+    $textvalue=$form->textwithpicto($langs->trans("Value"), $texthelp, 1, 'help', '', 0, 2, '');
 }
 else
 {
     $texthelp='ASC or DESC';
     $textvalue=$form->textwithpicto($langs->trans("SortOrder"), $texthelp);
 }
-print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'defaultvalue', '', $param, '', $sortfield, $sortorder);
+print_liste_field_titre($textvalue, $_SERVER["PHP_SELF"], 'value', '', $param, '', $sortfield, $sortorder);
 if (! empty($conf->multicompany->enabled) && !$user->entity) print_liste_field_titre($langs->trans("Entity"),$_SERVER["PHP_SELF"],'entity,page','',$param,'',$sortfield,$sortorder);
 print '<td align="center"></td>';
 print "</tr>\n";
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index d430a8ea53b..7743da27c2d 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -253,17 +253,35 @@ function GETPOST($paramname,$check='',$method=0,$filter=NULL,$options=NULL)
 		$out = isset($_GET[$paramname])?$_GET[$paramname]:(isset($_POST[$paramname])?$_POST[$paramname]:'');
 		
 		// Management of default values
-		if (! empty($_GET['action']) && $_GET['action'] == 'create' && ! empty($paramname) && ! isset($_GET[$paramname]) && ! isset($_POST[$paramname]))
+		if (! isset($_GET['sortfield']))	// If we did a click on a field so sort, we do no appl default values
 		{
-			$relativepathstring = preg_replace('/\.[a-z]+$/', '', $_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);
-			$relativepathstring=dol_string_nospecial($relativepathstring, '-');
-			// $relativepathstring is now string that identify the page: '_societe_card', '_agenda_card', ...
-			$keyfordefaultvalue = 'MAIN_DEFAULT_FOR_'.$relativepathstring.'_'.$paramname;
-			global $conf;
-			if (isset($conf->global->$keyfordefaultvalue)) $out = $conf->global->$keyfordefaultvalue;
+			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);
+				$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]['createform']);
+					if (isset($user->default_values[$relativepathstring]['createform'][$paramname])) $out = $user->default_values[$relativepathstring]['createform'][$paramname];
+				}
+			}
+			// Management of default search_filters
+			elseif (preg_match('/list.php$/', $_SERVER["PHP_SELF"]) && ! 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);
+				$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];
+				}
+			}
 		}
 	}
 	elseif ($method==1) $out = isset($_GET[$paramname])?$_GET[$paramname]:'';
@@ -303,6 +321,11 @@ function GETPOST($paramname,$check='',$method=0,$filter=NULL,$options=NULL)
 	            global $user;
 	            $out = $user->id;
 	        }
+	    	elseif ($reg[1] == 'SUPERVISORID')
+	        {
+	            global $user;
+	            $out = $user->fk_user;
+	        }
 	        elseif ($reg[1] == 'ENTITYID')
 	        {
 	            global $conf;
diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang
index af23343398b..1bfa5c0230c 100644
--- a/htdocs/langs/en_US/admin.lang
+++ b/htdocs/langs/en_US/admin.lang
@@ -428,7 +428,7 @@ 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.
-PageUrlForDefaultValues=You must enter here the relative url of the page. Examples:
+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.
diff --git a/htdocs/master.inc.php b/htdocs/master.inc.php
index ebb99eb9807..61e51999479 100644
--- a/htdocs/master.inc.php
+++ b/htdocs/master.inc.php
@@ -180,7 +180,7 @@ if (! defined('NOREQUIREDB'))
 
 	//print "Will work with data into entity instance number '".$conf->entity."'";
 
-	// Here we read database (llx_const table and llx_default_values) and define $conf->global->XXX var.
+	// Here we read database (llx_const table) and define $conf->global->XXX var.
 	$conf->setValues($db);
 }
 
diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php
index a3006d6ebcd..f23c8aca0b6 100644
--- a/htdocs/user/class/user.class.php
+++ b/htdocs/user/class/user.class.php
@@ -376,7 +376,7 @@ class User extends CommonObject
 			    {
 			        if (! empty($obj->page) && ! empty($obj->type) && ! empty($obj->param)) 
 			        {
-			            $user->default_values[$obj->page][$obj->type][$obj->param]=$obj->value;
+			            $this->default_values[$obj->page][$obj->type][$obj->param]=$obj->value;
 			        }
 			    }
 			    $this->db->free($resql);
@@ -387,7 +387,7 @@ class User extends CommonObject
 				return -3;
 			}
 		}
-
+		
 		return 1;
 	}
 
-- 
GitLab