Browse Source

Merge pull request #20779 from atm-GregM/develop_new_massaction_extrafield_product

NEW : Add massaction "Edit Extrafield" for Product
Laurent Destailleur 3 years ago
parent
commit
f14e614430

+ 43 - 0
htdocs/core/actions_massactions.inc.php

@@ -1302,6 +1302,49 @@ if (!$error && ($massaction == 'disable' || ($action == 'disable' && $confirm ==
 	}
 }
 
+if (!$error && $action == 'confirm_edit_value_extrafields' && $confirm == 'yes' && $permissiontoadd) {
+	$db->begin();
+
+	$objecttmp = new $objectclass($db);
+	$e = new ExtraFields($db);// fetch optionals attributes and labels
+	$e->fetch_name_optionals_label($objecttmp->table_element);
+
+	$nbok = 0;
+	$extrafieldKeyToUpdate = GETPOST('extrafield-key-to-update');
+
+
+	foreach ($toselect as $toselectid) {
+		/** @var CommonObject $objecttmp */
+		$objecttmp = new $objectclass($db); // to avoid ghost data
+		$result = $objecttmp->fetch($toselectid);
+		if ($result>0) {
+			// Fill array 'array_options' with data from add form
+			$ret = $e->setOptionalsFromPost(null, $objecttmp, $extrafieldKeyToUpdate);
+			if ($ret > 0) {
+				$objecttmp->insertExtraFields();
+			} else {
+				$error++;
+				setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
+			}
+		} else {
+			setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
+			$error++;
+			break;
+		}
+	}
+
+	if (!$error) {
+		if ($nbok > 1) {
+			setEventMessages($langs->trans("RecordsDisabled", $nbok), null, 'mesgs');
+		} else {
+			setEventMessages($langs->trans("save"), null, 'mesgs');
+		}
+		$db->commit();
+	} else {
+		$db->rollback();
+	}
+}
+
 if (!$error && ($massaction == 'affectcommercial' || ($action == 'affectcommercial' && $confirm == 'yes')) && $permissiontoadd) {
 	$db->begin();
 

+ 53 - 0
htdocs/core/tpl/massactions_pre.tpl.php

@@ -211,6 +211,59 @@ if ($massaction == 'presend') {
 	print dol_get_fiche_end();
 }
 
+if ($massaction == 'edit_extrafields') {
+	require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
+	$elementtype = $objecttmp->element;
+	/** @var CommonObject $objecttmp */
+	$extrafields = new ExtraFields($db);
+	$keysuffix = '';
+	$extrafields->fetch_name_optionals_label($elementtype);
+	$extrafields_list = $extrafields->attributes[$elementtype]['label'];
+
+	$formquestion = array();
+	if (!empty($extrafields_list)) {
+		$myParamExtra = $object->showOptionals($extrafields, 'create');
+
+		$formquestion[] = array(
+			'type' => 'other',
+			'value' => $form->selectarray('extrafield-key-to-update', $extrafields_list, GETPOST('extrafield-key-to-update'), 1)
+		);
+
+
+		$outputShowOutputFields = '<div class="extrafields-inputs">';
+
+		foreach ($extrafields_list as $extraKey => $extraLabel) {
+			$outputShowOutputFields.= '<div class="mass-action-extrafield" data-extrafield="'.$extraKey.'" style="display:none;" >';
+			$outputShowOutputFields.= '<br><span>'. $langs->trans('NewValue').'</span>';
+			$outputShowOutputFields.= $extrafields->showInputField($extraKey, '', '', $keysuffix, '', 0, $objecttmp->id, $objecttmp->table_element);
+			$outputShowOutputFields.= '</div>';
+		}
+		$outputShowOutputFields.= '<script>
+		jQuery(function($) {
+            $("#extrafield-key-to-update").on(\'change\',function(){
+            	let selectedExtrtafield = $(this).val();
+            	if($(".extrafields-inputs .product_extras_"+selectedExtrtafield) != undefined){
+					$(".mass-action-extrafield").hide();
+					$(".mass-action-extrafield[data-extrafield=" + selectedExtrtafield + "]").show();
+                }
+            });
+		});
+		</script>';
+		$outputShowOutputFields.= '</div>';
+
+
+
+		$formquestion[] = array(
+			'type' => 'other',
+			'value' => $outputShowOutputFields
+			);
+
+		print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmEditExtrafield"), $langs->trans("ConfirmEditExtrafieldQuestion", count($toselect)), "confirm_edit_value_extrafields", $formquestion, 1, 0, 200, 500, 1);
+	} else {
+		setEventMessage($langs->trans("noExtrafields"));
+	}
+}
+
 if ($massaction == 'preenable') {
 	print $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans("ConfirmMassEnabling"), $langs->trans("ConfirmMassEnablingQuestion", count($toselect)), "enable", null, 'yes', 0, 200, 500, 1);
 }

+ 3 - 0
htdocs/langs/en_US/products.lang

@@ -424,3 +424,6 @@ PMPExpected=Expected PMP
 ExpectedValuation=Expected Valuation
 PMPReal=Real PMP
 RealValuation=Real Valuation
+ConfirmEditExtrafield = Select the extrafield you want modify
+ConfirmEditExtrafieldQuestion = Are you sure you want to modify this extrafield?
+ModifyValueExtrafields = Modify value of an extrafield

+ 2 - 1
htdocs/product/list.php

@@ -727,6 +727,7 @@ if ($resql) {
 	// List of mass actions available
 	$arrayofmassactions = array(
 		'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
+		'edit_extrafields'=>img_picto('', 'edit', 'class="pictofixedwidth"').$langs->trans("ModifyValueExtrafields"),
 		//'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
 		//'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
 	);
@@ -739,7 +740,7 @@ if ($resql) {
 		$arrayofmassactions['switchonpurchasestatus'] = img_picto('', 'stop-circle', 'class="pictofixedwidth"').$langs->trans("SwitchOnPurchaseStatus");
 		$arrayofmassactions['preaffecttag'] = img_picto('', 'category', 'class="pictofixedwidth"').$langs->trans("AffectTag");
 	}
-	if (in_array($massaction, array('presend', 'predelete','preaffecttag'))) {
+	if (in_array($massaction, array('presend', 'predelete','preaffecttag', 'edit_extrafields'))) {
 		$arrayofmassactions = array();
 	}
 	$massactionbutton = $form->selectMassAction('', $arrayofmassactions);