From 452636b0e41ac0f6ffd9cb19aa1e16504d65bdcf Mon Sep 17 00:00:00 2001
From: Maxime Kohlhaas <maxime@atm-consulting.fr>
Date: Tue, 14 Oct 2014 15:27:40 +0200
Subject: [PATCH] New : Add option to allow extrafield to be modified
 regardless of the document status

---
 htdocs/comm/propal.php                        | 13 ++++++----
 htdocs/core/actions_extrafields.inc.php       |  4 ++--
 htdocs/core/class/extrafields.class.php       | 24 ++++++++++++-------
 htdocs/core/tpl/admin_extrafields_add.tpl.php |  2 ++
 .../core/tpl/admin_extrafields_edit.tpl.php   |  3 +++
 .../install/mysql/migration/3.6.0-3.7.0.sql   |  2 ++
 .../install/mysql/tables/llx_extrafields.sql  |  3 ++-
 7 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/htdocs/comm/propal.php b/htdocs/comm/propal.php
index b344da39112..1f211c3cb79 100644
--- a/htdocs/comm/propal.php
+++ b/htdocs/comm/propal.php
@@ -2032,9 +2032,16 @@ if ($action == 'create')
 			}
 			else
 			{
-				print '<tr><td';
+				print '<tr><td>';
+				print '<table width="100%" class="nobordernopadding"><tr><td';
 				if (! empty($extrafields->attribute_required [$key])) print ' class="fieldrequired"';
-				print '>' . $label . '</td><td colspan="5">';
+				print '>' . $label . '</td>';
+				if (($object->statut == 0 || $extrafields->attribute_alwayseditable[$key]) && $user->rights->propal->creer && ($action != 'edit_extras' || GETPOST('attribute') != $key))
+					print '<td align="right"><a href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=edit_extras&attribute=' . $key . '">' . img_edit().'</a></td>';
+				
+				print '</tr></table>';
+				print '<td colspan="5">';
+				
 				// Convert date into timestamp format
 				if (in_array($extrafields->attribute_type [$key], array('date','datetime'))) {
 					$value = isset($_POST ["options_" . $key]) ? dol_mktime($_POST ["options_" . $key . "hour"], $_POST ["options_" . $key . "min"], 0, $_POST ["options_" . $key . "month"], $_POST ["options_" . $key . "day"], $_POST ["options_" . $key . "year"]) : $db->jdate($object->array_options ['options_' . $key]);
@@ -2057,8 +2064,6 @@ if ($action == 'create')
 				else
 				{
 					print $extrafields->showOutputField($key, $value);
-					if ($object->statut == 0 && $user->rights->propal->creer)
-						print '<a href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=edit_extras&attribute=' . $key . '">' . img_picto('', 'edit') . ' ' . $langs->trans('Modify') . '</a>';
 				}
 				print '</td></tr>' . "\n";
 			}
diff --git a/htdocs/core/actions_extrafields.inc.php b/htdocs/core/actions_extrafields.inc.php
index 89edfcbae8e..d5f8ea7c18c 100644
--- a/htdocs/core/actions_extrafields.inc.php
+++ b/htdocs/core/actions_extrafields.inc.php
@@ -141,7 +141,7 @@ if ($action == 'add')
     				}
     			}
 
-                $result=$extrafields->addExtraField($_POST['attrname'],$_POST['label'],$_POST['type'],$_POST['pos'],$extrasize,$elementtype,(GETPOST('unique')?1:0),(GETPOST('required')?1:0),$default_value,$params);
+                $result=$extrafields->addExtraField($_POST['attrname'],$_POST['label'],$_POST['type'],$_POST['pos'],$extrasize,$elementtype,(GETPOST('unique')?1:0),(GETPOST('required')?1:0),$default_value,$params,(GETPOST('alwayseditable')?1:0));
     			if ($result > 0)
     			{
     				setEventMessage($langs->trans('SetupSaved'));
@@ -278,7 +278,7 @@ if ($action == 'update')
     					$params['options'][$key] = $value;
     				}
     			}
-    			$result=$extrafields->update($_POST['attrname'],$_POST['label'],$_POST['type'],$extrasize,$elementtype,(GETPOST('unique')?1:0),(GETPOST('required')?1:0),$pos,$params);
+    			$result=$extrafields->update($_POST['attrname'],$_POST['label'],$_POST['type'],$extrasize,$elementtype,(GETPOST('unique')?1:0),(GETPOST('required')?1:0),$pos,$params,(GETPOST('alwayseditable')?1:0));
     			if ($result > 0)
     			{
     				setEventMessage($langs->trans('SetupSaved'));
diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php
index 42c505a54bf..9075cac94e3 100644
--- a/htdocs/core/class/extrafields.class.php
+++ b/htdocs/core/class/extrafields.class.php
@@ -50,6 +50,8 @@ class ExtraFields
 	var $attribute_param;
 	// Int to store position of attribute
 	var $attribute_pos;
+	// Int to store if attribute is editable regardless of the document status
+	var $attribute_alwayseditable;
 
 	var $error;
 	var $errno;
@@ -105,7 +107,7 @@ class ExtraFields
 	 *  @param  array	$param				Params for field
 	 *  @return int      					<=0 if KO, >0 if OK
 	 */
-	function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique=0, $required=0,$default_value='', $param=0)
+	function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique=0, $required=0, $default_value='', $param=0, $alwayseditable=0)
 	{
 		if (empty($attrname)) return -1;
 		if (empty($label)) return -1;
@@ -119,7 +121,7 @@ class ExtraFields
 		if ($result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate')
 		{
 			// Add declaration of field into table
-			$result2=$this->create_label($attrname,$label,$type,$pos,$size,$elementtype, $unique, $required, $param);
+			$result2=$this->create_label($attrname,$label,$type,$pos,$size,$elementtype, $unique, $required, $param, $alwayseditable);
 			$err2=$this->errno;
 			if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS'))
 			{
@@ -219,7 +221,7 @@ class ExtraFields
 	 *  @param  array||string	$param			Params for field  (ex for select list : array('options' => array(value'=>'label of option')) )
 	 *  @return	int								<=0 if KO, >0 if OK
 	 */
-	private function create_label($attrname, $label='', $type='', $pos=0, $size=0, $elementtype='member', $unique=0, $required=0, $param='')
+	private function create_label($attrname, $label='', $type='', $pos=0, $size=0, $elementtype='member', $unique=0, $required=0, $param='', $alwayseditable=0)
 	{
 		global $conf;
 
@@ -241,7 +243,7 @@ class ExtraFields
 				$params='';
 			}
 
-			$sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param)";
+			$sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(name, label, type, pos, size, entity, elementtype, fieldunique, fieldrequired, param, alwayseditable)";
 			$sql.= " VALUES('".$attrname."',";
 			$sql.= " '".$this->db->escape($label)."',";
 			$sql.= " '".$type."',";
@@ -251,7 +253,8 @@ class ExtraFields
 			$sql.= " '".$elementtype."',";
 			$sql.= " '".$unique."',";
 			$sql.= " '".$required."',";
-			$sql.= " '".$params."'";
+			$sql.= " '".$params."',";
+			$sql.= " '".$alwayseditable."'";
 			$sql.=')';
 
 			dol_syslog(get_class($this)."::create_label", LOG_DEBUG);
@@ -349,7 +352,7 @@ class ExtraFields
 	 *  @param  array	$param				Params for field  (ex for select list : array('options' => array(value'=>'label of option')) )
 	 * 	@return	int							>0 if OK, <=0 if KO
 	 */
-	function update($attrname,$label,$type,$length,$elementtype,$unique=0,$required=0,$pos=0,$param='')
+	function update($attrname,$label,$type,$length,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0)
 	{
 		$table=$elementtype.'_extrafields';
 
@@ -384,7 +387,7 @@ class ExtraFields
 			{
 				if ($label)
 				{
-					$result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param);
+					$result=$this->update_label($attrname,$label,$type,$length,$elementtype,$unique,$required,$pos,$param,$alwayseditable);
 				}
 				if ($result > 0)
 				{
@@ -434,7 +437,7 @@ class ExtraFields
 	 *  @param  array	$param				Params for field  (ex for select list : array('options' => array(value'=>'label of option')) )
 	 *  @return	int							<=0 if KO, >0 if OK
 	 */
-	private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='')
+	private function update_label($attrname,$label,$type,$size,$elementtype,$unique=0,$required=0,$pos=0,$param='',$alwayseditable=0)
 	{
 		global $conf;
 		dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required);
@@ -465,6 +468,7 @@ class ExtraFields
 			$sql.= " fieldunique,";
 			$sql.= " fieldrequired,";
 			$sql.= " pos,";
+			$sql.= " alwayseditable,";
 			$sql.= " param";
 			$sql.= ") VALUES (";
 			$sql.= "'".$attrname."',";
@@ -476,6 +480,7 @@ class ExtraFields
 			$sql.= " '".$unique."',";
 			$sql.= " '".$required."',";
 			$sql.= " '".$pos."',";
+			$sql.= " '".$alwayseditable."',";
 			$sql.= " '".$param."'";
 			$sql.= ")";
 			dol_syslog(get_class($this)."::update_label", LOG_DEBUG);
@@ -529,7 +534,7 @@ class ExtraFields
 		if (!$forceload && !empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))
 			return $array_name_label;
 
-		$sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos";
+		$sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable";
 		$sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
 		$sql.= " WHERE entity IN (0,".$conf->entity.")";
 		if ($elementtype) $sql.= " AND elementtype = '".$elementtype."'";
@@ -557,6 +562,7 @@ class ExtraFields
 					$this->attribute_required[$tab->name]=$tab->fieldrequired;
 					$this->attribute_param[$tab->name]=unserialize($tab->param);
 					$this->attribute_pos[$tab->name]=$tab->pos;
+					$this->attribute_alwayseditable[$tab->name]=$tab->alwayseditable;
 				}
 			}
 
diff --git a/htdocs/core/tpl/admin_extrafields_add.tpl.php b/htdocs/core/tpl/admin_extrafields_add.tpl.php
index e4c4d75c85e..bdced511867 100644
--- a/htdocs/core/tpl/admin_extrafields_add.tpl.php
+++ b/htdocs/core/tpl/admin_extrafields_add.tpl.php
@@ -101,6 +101,8 @@
 <tr><td><?php echo $langs->trans("Unique"); ?></td><td class="valeur"><input id="unique" type="checkbox" name="unique" <?php echo (GETPOST('unique')?' checked="true"':''); ?>></td></tr>
 <!-- Required -->
 <tr><td><?php echo $langs->trans("Required"); ?></td><td class="valeur"><input id="required" type="checkbox" name="required" <?php echo (GETPOST('required')?' checked="true"':''); ?>></td></tr>
+<!-- Always editable -->
+<tr><td><?php echo $langs->trans("AlwaysEditable"); ?></td><td class="valeur"><input id="alwayseditable" type="checkbox" name="alwayseditable" <?php echo (GETPOST('alwayseditable')?' checked="true"':''); ?>></td></tr>
 </table>
 
 <div align="center"><br><input type="submit" name="button" class="button" value="<?php echo $langs->trans("Save"); ?>"> &nbsp;
diff --git a/htdocs/core/tpl/admin_extrafields_edit.tpl.php b/htdocs/core/tpl/admin_extrafields_edit.tpl.php
index e24a3e86948..dcf1af56ed1 100644
--- a/htdocs/core/tpl/admin_extrafields_edit.tpl.php
+++ b/htdocs/core/tpl/admin_extrafields_edit.tpl.php
@@ -54,6 +54,7 @@ $size=$extrafields->attribute_size[$attrname];
 $unique=$extrafields->attribute_unique[$attrname];
 $required=$extrafields->attribute_required[$attrname];
 $pos=$extrafields->attribute_pos[$attrname];
+$alwayseditable=$extrafields->attribute_alwayseditable[$attrname];
 $param=$extrafields->attribute_param[$attrname];
 
 if((($type == 'select') || ($type == 'checkbox') ||(($type == 'radio'))) && is_array($param))
@@ -110,6 +111,8 @@ if(($type == 'select') || ($type == 'sellist') || ($type == 'checkbox') ||(($typ
 <tr><td><?php echo $langs->trans("Unique"); ?></td><td class="valeur"><input id="unique" type="checkbox" name="unique" <?php echo ($unique?' checked="true"':''); ?>></td></tr>
 <!-- Required -->
 <tr><td><?php echo $langs->trans("Required"); ?></td><td class="valeur"><input id="required" type="checkbox" name="required" <?php echo ($required?' checked="true"':''); ?>></td></tr>
+<!-- Always editable -->
+<tr><td><?php echo $langs->trans("AlwaysEditable"); ?></td><td class="valeur"><input id="alwayseditable" type="checkbox" name="alwayseditable" <?php echo ($alwayseditable?' checked="true"':''); ?>></td></tr>
 
 </table>
 
diff --git a/htdocs/install/mysql/migration/3.6.0-3.7.0.sql b/htdocs/install/mysql/migration/3.6.0-3.7.0.sql
index 5e633eb2660..dfbf0676eae 100644
--- a/htdocs/install/mysql/migration/3.6.0-3.7.0.sql
+++ b/htdocs/install/mysql/migration/3.6.0-3.7.0.sql
@@ -1096,3 +1096,5 @@ DELETE FROM llx_const WHERE name = 'MAIN_MODULE_BOUTIQUE';
 DELETE FROM llx_const WHERE name = 'OSC_DB_HOST';
 DELETE FROM llx_menu WHERE module = 'boutique';
 
+-- Add option always editable on extrafield
+ALTER TABLE llx_extrafields ADD alwayseditable INT(11) NOT NULL AFTER pos;
\ No newline at end of file
diff --git a/htdocs/install/mysql/tables/llx_extrafields.sql b/htdocs/install/mysql/tables/llx_extrafields.sql
index ba4c7df964f..04963eb3905 100644
--- a/htdocs/install/mysql/tables/llx_extrafields.sql
+++ b/htdocs/install/mysql/tables/llx_extrafields.sql
@@ -22,7 +22,7 @@ create table llx_extrafields
 	rowid           integer AUTO_INCREMENT PRIMARY KEY,
 	name            varchar(64) NOT NULL,       -- nom de l'attribut
 	entity          integer DEFAULT 1 NOT NULL,	-- multi company id
-    	elementtype     varchar(64) NOT NULL DEFAULT 'member',
+    elementtype     varchar(64) NOT NULL DEFAULT 'member',
 	tms             timestamp,
 	label           varchar(255) NOT NULL,      -- label correspondant a l'attribut
 	type            varchar(8),
@@ -30,5 +30,6 @@ create table llx_extrafields
 	fieldunique     integer DEFAULT 0,
 	fieldrequired   integer DEFAULT 0,
 	pos             integer DEFAULT 0,
+	alwayseditable  integer DEFAULT 0,
 	param		text
 )ENGINE=innodb;
-- 
GitLab