Fix ajax selectobject with commonobjectline objects (#29941)

* Fix ajax selectobject with commonobjectline objects

* Clean code : put in another branch

* Parent element defined for CommonObjectLines

* Clean code

* Clean code

* Parent element defined for CommonObjectLines

* Parent element defined for CommonObjectLines

* Clean code

* getElementProperties for commonObjectLines

* Remove changes to split into multiple PRs

* fix mistake in previous commit

* fix mistakes (thanks pre-commit)

* fix mistakes (thanks again pre-commit)

* Update html.form.class.php

---------

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
Mathieu Moulin
2024-10-26 17:03:34 +02:00
committed by GitHub
parent 3d49587253
commit 6ee1d83dff
3 changed files with 116 additions and 14 deletions

View File

@@ -8519,7 +8519,16 @@ class Form
}
if (empty($fieldstoshow)) {
if (isset($objecttmp->fields['ref'])) {
if (!empty($objecttmp->parent_element)) {
$fieldstoshow = 'o.ref';
if (empty($sortfield)) {
$sortfield = 'o.ref';
}
if (in_array($objecttmp->element, ['commandedet', 'propaldet', 'facturedet', 'expeditiondet'])) {
$fieldstoshow .= ',p.ref AS p_ref,p.label,t.description';
$sortfield .= ', p.ref';
}
} elseif (isset($objecttmp->fields['ref'])) {
$fieldstoshow = 't.ref';
} else {
$langs->load("errors");
@@ -8535,14 +8544,21 @@ class Form
$num = 0;
// Search data
$sql = "SELECT t.rowid, " . $fieldstoshow . " FROM " . $this->db->prefix() . $objecttmp->table_element . " as t";
$sql = "SELECT t.rowid, " . $fieldstoshow . " FROM " . $this->db->prefix() . $this->db->sanitize($objecttmp->table_element) . " as t";
if (!empty($objecttmp->isextrafieldmanaged)) {
$sql .= " LEFT JOIN " . $this->db->prefix() . $objecttmp->table_element . "_extrafields as e ON t.rowid=e.fk_object";
$sql .= " LEFT JOIN " . $this->db->prefix() . $this->db->sanitize($objecttmp->table_element) . "_extrafields as e ON t.rowid = e.fk_object";
}
if (!empty($objecttmp->parent_element)) {
$parent_properties = getElementProperties($objecttmp->parent_element);
$sql .= " INNER JOIN " . $this->db->prefix() . $this->db->sanitize($parent_properties['table_element']) . " as o ON o.rowid = t.".$objecttmp->fk_parent_attribute;
}
if (in_array($objecttmp->parent_element, ['commande', 'propal', 'facture', 'expedition'])) {
$sql .= " LEFT JOIN " . $this->db->prefix() . "product as p ON p.rowid = t.fk_product";
}
if (isset($objecttmp->ismultientitymanaged)) {
if (!is_numeric($objecttmp->ismultientitymanaged)) {
$tmparray = explode('@', $objecttmp->ismultientitymanaged);
$sql .= " INNER JOIN " . $this->db->prefix() . $tmparray[1] . " as parenttable ON parenttable.rowid = t." . $tmparray[0];
$sql .= " INNER JOIN " . $this->db->prefix() . $this->db->sanitize($tmparray[1]) . " as parenttable ON parenttable.rowid = t." . $this->db->sanitize($tmparray[0]);
}
if ($objecttmp->ismultientitymanaged === 'fk_soc@societe') {
if (!$user->hasRight('societe', 'client', 'voir')) {
@@ -8569,7 +8585,7 @@ class Form
$sql .= " AND t.entity IN (" . getEntity($objecttmp->table_element) . ")";
}
if (!is_numeric($objecttmp->ismultientitymanaged)) {
$sql .= " AND parenttable.entity = t." . $tmparray[0];
$sql .= " AND parenttable.entity = t." . $this->db->sanitize($tmparray[0]);
}
if ($objecttmp->ismultientitymanaged == 1 && !empty($user->socid)) {
if ($objecttmp->element == 'societe') {
@@ -8584,8 +8600,14 @@ class Form
}
}
}
$splittedfieldstoshow = explode(',', $fieldstoshow);
foreach ($splittedfieldstoshow as &$field2) {
if (is_numeric($pos=strpos($field2, ' '))) {
$field2 = substr($field2, 0, $pos);
}
}
if ($searchkey != '') {
$sql .= natural_search(explode(',', $fieldstoshow), $searchkey);
$sql .= natural_search($splittedfieldstoshow, $searchkey);
}
if ($filter) { // Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
@@ -8631,7 +8653,7 @@ class Form
$tmparray = explode(',', $fieldstoshow);
$oldvalueforshowoncombobox = 0;
foreach ($tmparray as $key => $val) {
$val = preg_replace('/t\./', '', $val);
$val = preg_replace('/(t|p|o)\./', '', $val);
$label .= (($label && $obj->$val) ? ($oldvalueforshowoncombobox != $objecttmp->fields[$val]['showoncombobox'] ? ' - ' : ' ') : '');
$labelhtml .= (($label && $obj->$val) ? ($oldvalueforshowoncombobox != $objecttmp->fields[$val]['showoncombobox'] ? ' - ' : ' ') : '');
$label .= $obj->$val;

View File

@@ -12889,7 +12889,7 @@ function getElementProperties($elementType)
//$element_type='facture';
$classfile = $classname = $classpath = $subdir = $dir_output = '';
$classfile = $classname = $classpath = $subdir = $dir_output = $parent_element = '';
// Parse element/subelement
$module = $elementType;
@@ -12957,6 +12957,11 @@ function getElementProperties($elementType)
} elseif ($elementType == 'inventory') {
$module = 'product';
$classpath = 'product/inventory/class';
} elseif ($elementType == 'inventoryline') {
$module = 'product';
$classpath = 'product/inventory/class';
$table_element = 'inventorydet';
$parent_element = 'inventory';
} elseif ($elementType == 'stock' || $elementType == 'entrepot') {
$module = 'stock';
$classpath = 'product/stock/class';
@@ -12977,6 +12982,13 @@ function getElementProperties($elementType)
$module = 'facture';
$subelement = 'facture';
$table_element = 'facture';
} elseif ($elementType == 'facturedet') {
$classpath = 'compta/facture/class';
$classfile = 'facture';
$classname = 'FactureLigne';
$module = 'facture';
$table_element = 'facturedet';
$parent_element = 'facture';
} elseif ($elementType == 'facturerec') {
$classpath = 'compta/facture/class';
$module = 'facture';
@@ -12986,15 +12998,36 @@ function getElementProperties($elementType)
$module = 'commande';
$subelement = 'commande';
$table_element = 'commande';
} elseif ($elementType == 'commandedet') {
$classpath = 'commande/class';
$classfile = 'commande';
$classname = 'OrderLine';
$module = 'commande';
$table_element = 'commandedet';
$parent_element = 'commande';
} elseif ($elementType == 'propal') {
$classpath = 'comm/propal/class';
$table_element = 'propal';
} elseif ($elementType == 'propaldet') {
$classpath = 'comm/propal/class';
$classfile = 'propal';
$subelement = 'propaleligne';
$module = 'propal';
$table_element = 'propaldet';
$parent_element = 'propal';
} elseif ($elementType == 'shipping') {
$classpath = 'expedition/class';
$classfile = 'expedition';
$classname = 'Expedition';
$module = 'expedition';
$table_element = 'expedition';
} elseif ($elementType == 'expeditiondet' || $elementType == 'shippingdet') {
$classpath = 'expedition/class';
$classfile = 'expedition';
$classname = 'ExpeditionLigne';
$module = 'expedition';
$table_element = 'expeditiondet';
$parent_element = 'expedition';
} elseif ($elementType == 'delivery_note') {
$classpath = 'delivery/class';
$subelement = 'delivery';
@@ -13003,17 +13036,30 @@ function getElementProperties($elementType)
$classpath = 'delivery/class';
$subelement = 'delivery';
$module = 'expedition';
} elseif ($elementType == 'deliverydet') {
// @todo
} elseif ($elementType == 'supplier_proposal') {
$classpath = 'supplier_proposal/class';
$module = 'supplier_proposal';
$element = 'supplierproposal';
$classfile = 'supplier_proposal';
$subelement = 'supplierproposal';
} elseif ($elementType == 'supplier_proposaldet') {
$classpath = 'supplier_proposal/class';
$module = 'supplier_proposal';
$classfile = 'supplier_proposal';
$table_element = 'supplier_proposaldet';
$parent_element = 'supplier_proposal';
} elseif ($elementType == 'contract') {
$classpath = 'contrat/class';
$module = 'contrat';
$subelement = 'contrat';
$table_element = 'contract';
} elseif ($elementType == 'contratdet') {
$classpath = 'contrat/class';
$module = 'contrat';
$table_element = 'contratdet';
$parent_element = 'contrat';
} elseif ($elementType == 'mailing') {
$classpath = 'comm/mailing/class';
$module = 'mailing';
@@ -13035,6 +13081,14 @@ function getElementProperties($elementType)
$module = 'mrp';
$subelement = '';
$table_element = 'mrp_mo';
} elseif ($elementType == 'mrp_production') {
$classpath = 'mrp/class';
$classfile = 'mo';
$classname = 'MoLine';
$module = 'mrp';
$subelement = '';
$table_element = 'mrp_production';
$parent_element = 'mo';
} elseif ($elementType == 'cabinetmed_cons') {
$classpath = 'cabinetmed/class';
$module = 'cabinetmed';
@@ -13050,15 +13104,11 @@ function getElementProperties($elementType)
$module = 'resource';
$subelement = 'dolresource';
$table_element = 'resource';
} elseif ($elementType == 'propaldet') {
$classpath = 'comm/propal/class';
$module = 'propal';
$subelement = 'propaleligne';
} elseif ($elementType == 'opensurvey_sondage') {
$classpath = 'opensurvey/class';
$module = 'opensurvey';
$subelement = 'opensurveysondage';
} elseif ($elementType == 'order_supplier') {
} elseif ($elementType == 'order_supplier' || $elementType == 'commande_fournisseur') {
$classpath = 'fourn/class';
$module = 'fournisseur';
$classfile = 'fournisseur.commande';
@@ -13074,6 +13124,7 @@ function getElementProperties($elementType)
$subelement = '';
$classname = 'CommandeFournisseurLigne';
$table_element = 'commande_fournisseurdet';
$parent_element = 'commande_fournisseur';
} elseif ($elementType == 'invoice_supplier') {
$classpath = 'fourn/class';
$module = 'fournisseur';
@@ -13082,6 +13133,15 @@ function getElementProperties($elementType)
$subelement = '';
$classname = 'FactureFournisseur';
$table_element = 'facture_fourn';
} elseif ($elementType == 'facture_fourn_det') {
$classpath = 'fourn/class';
$module = 'fournisseur';
$classfile = 'fournisseur.facture';
$element = 'facture_fourn_det';
$subelement = '';
$classname = 'SupplierInvoiceLine';
$table_element = 'facture_fourn_det';
$parent_element = 'invoice_supplier';
} elseif ($elementType == "service") {
$classpath = 'product/class';
$subelement = 'product';
@@ -13199,7 +13259,8 @@ function getElementProperties($elementType)
'classpath' => $classpath,
'classfile' => $classfile,
'classname' => $classname,
'dir_output' => $dir_output
'dir_output' => $dir_output,
'parent_element' => $parent_element,
);

View File

@@ -398,6 +398,7 @@ function restrictedArea(User $user, $features, $object = 0, $tableandshare = '',
$parentfortableentity = '';
// Fix syntax of $features param to support non standard module names.
// @todo : use elseif ?
$originalfeatures = $features;
if ($features == 'agenda') {
$tableandshare = 'actioncomm&societe';
@@ -454,6 +455,24 @@ function restrictedArea(User $user, $features, $object = 0, $tableandshare = '',
$tableandshare = 'paiementcharge';
$parentfortableentity = 'fk_charge@chargesociales';
}
// if commonObjectLine : Using many2one related commonObject
// @see commonObjectLine::parentElement
if (in_array($features, ['commandedet', 'propaldet', 'facturedet', 'supplier_proposaldet', 'evaluationdet', 'skilldet', 'deliverydet', 'contratdet'])) {
$features = substr($features, 0, -3);
} elseif (in_array($features, ['stocktransferline', 'inventoryline', 'bomline', 'expensereport_det', 'facture_fourn_det'])) {
$features = substr($features, 0, -4);
} elseif ($features == 'commandefournisseurdispatch') {
$features = 'commandefournisseur';
} elseif ($features == 'invoice_supplier_det_rec') {
$features = 'invoice_supplier_rec';
}
// @todo check : project_task
// @todo possible ?
// elseif (substr($features, -3, 3) == 'det') {
// $features = substr($features, 0, -3);
// } elseif (substr($features, -4, 4) == '_det' || substr($features, -4, 4) == 'line') {
// $features = substr($features, 0, -4);
// }
//print $features.' - '.$tableandshare.' - '.$feature2.' - '.$dbt_select."\n";