From f823feba520e866cb60362d38bdae6da356626dd Mon Sep 17 00:00:00 2001 From: Tobias Sekan Date: Wed, 4 Mar 2020 13:31:19 +0100 Subject: [PATCH] Add sorting for "Contacts of ..." --- htdocs/core/tpl/contacts.tpl.php | 218 ++++++++++++++++++++++--------- 1 file changed, 159 insertions(+), 59 deletions(-) diff --git a/htdocs/core/tpl/contacts.tpl.php b/htdocs/core/tpl/contacts.tpl.php index d14503a9039..1b580fe5593 100644 --- a/htdocs/core/tpl/contacts.tpl.php +++ b/htdocs/core/tpl/contacts.tpl.php @@ -28,7 +28,6 @@ if (empty($object) || !is_object($object)) exit; } - require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; @@ -66,8 +65,9 @@ $userstatic = new User($db);
+if ($permission) +{ + ?>
trans("NatureOfContact"); ?>
trans("ThirdParty"); ?>
@@ -153,94 +153,180 @@ if ($permission) { - -
trans("NatureOfContact"); ?>
-
trans("ThirdParty"); ?>
-
trans("Users").'/'.$langs->trans("Contacts"); ?>
-
trans("ContactType"); ?>
-
trans("Status"); ?>
-
 
-
+print "
"; -element == 'shipping'|| $object->element == 'reception') && is_object($objectsrc)) $tmpobject=$objectsrc; +/** +* Prepare list +*/ + +// TODO: replace this with direct SQL string to use $db->sort($sortfield, $sortorder) +$list = array(); +foreach(array('internal', 'external') as $source) +{ + $tmpobject = $object; + + if (($object->element == 'shipping'|| $object->element == 'reception') && is_object($objectsrc)) + { + $tmpobject = $objectsrc; + } $tab = $tmpobject->liste_contact(-1, $source); - $num=count($tab); + $num = count($tab); $i = 0; - while ($i < $num) { - ?> + while ($i < $num) + { + $entry = ''; + $entry->id = $tab[$i]['rowid']; + $entry->type = $tab[$i]['libelle']; + + if ($tab[$i]['source'] == 'internal') + { + $entry->nature = $langs->trans("User"); + } + else if ($tab[$i]['source'] == 'external') + { + $entry->nature = $langs->trans("ThirdPartyContact"); + } -
-
- trans("User"); ?> - trans("ThirdPartyContact"); ?> -
-
- 0) { $companystatic->fetch($tab[$i]['socid']); - echo $companystatic->getNomUrl(1); + $entry->thirdparty = $companystatic->getNomUrl(1); } - if ($tab[$i]['socid'] < 0) + else if ($tab[$i]['socid'] < 0) { - echo $conf->global->MAIN_INFO_SOCIETE_NOM; + $entry->thirdparty = $conf->global->MAIN_INFO_SOCIETE_NOM; } - if (! $tab[$i]['socid']) + else if (! $tab[$i]['socid']) { - echo ' '; + $entry->thirdparty = ""; } - ?> -
-
- fetch($tab[$i]['id']); - echo $userstatic->getNomUrl(-1, '', 0, 0, 0, 0, '', 'valignmiddle'); + $entry->contact = $userstatic->getNomUrl(-1, '', 0, 0, 0, 0, '', 'valignmiddle'); } - if ($tab[$i]['source']=='external') + else if ($tab[$i]['source']=='external') { $contactstatic->fetch($tab[$i]['id']); - echo $contactstatic->getNomUrl(1, '', 0, '', 0, 0); + $entry->contact =$contactstatic->getNomUrl(1, '', 0, '', 0, 0); } - ?> -
-
- - -
- \n"; -print "\n"; + +$sortfield = GETPOST("sortfield", "alpha"); +$sortorder = GETPOST("sortorder", 'alpha'); + +if (!$sortfield) $sortfield = "nature"; +if (!$sortorder) $sortorder = "asc"; + +/** + * Re-sort list + */ + +// TODO: switch to $db->sort($sortfield, $sortorder); +if($sortorder == "asc") +{ + if($sortfield == "nature") usort($list, "nature_asc"); + if($sortfield == "thirdparty") usort($list, "thirdparty_asc"); + if($sortfield == "contact") usort($list, "contact_asc"); + if($sortfield == "type") usort($list, "type_asc"); + if($sortfield == "status") usort($list, "status_asc"); +} +else +{ + if($sortfield == "nature") usort($list, "nature_desc"); + if($sortfield == "thirdparty") usort($list, "thirdparty_desc"); + if($sortfield == "contact") usort($list, "contact_desc"); + if($sortfield == "type") usort($list, "type_desc"); + if($sortfield == "status") usort($list, "status_desc"); +} + +$arrayfields = array( + 'rowid' => array('label'=>$langs->trans("Id"), 'checked'=>1), + 'nature' => array('label'=>$langs->trans("NatureOfContact"), 'checked'=>1), + 'thirdparty' => array('label'=>$langs->trans("ThirdParty"), 'checked'=>1), + 'contact' => array('label'=>$langs->trans("Users").'/'.$langs->trans("Contacts"), 'checked'=>1), + 'type' => array('label'=>$langs->trans("ContactType"), 'checked'=>1), + 'status' => array('label'=>$langs->trans("Status"), 'checked'=>1), + 'link' => array('label'=>$langs->trans("Link"), 'checked'=>1), +); + +$param = 'id='.$object->id.'&mainmenu=home'; + +/** + * Show list + */ + +print '
'; +print ''; +print ''; +print ''; +print ''; +print ''; + +print ''; + +print ''; +print ''; + +print ''; +print_liste_field_titre($arrayfields['nature']['label'], $_SERVER["PHP_SELF"], "nature", "", $param, "", $sortfield, $sortorder); +print_liste_field_titre($arrayfields['thirdparty']['label'], $_SERVER["PHP_SELF"], "thirdparty", "", $param, "", $sortfield, $sortorder); +print_liste_field_titre($arrayfields['contact']['label'], $_SERVER["PHP_SELF"], "contact", "", $param, "", $sortfield, $sortorder); +print_liste_field_titre($arrayfields['type']['label'], $_SERVER["PHP_SELF"], "type", "", $param, "", $sortfield, $sortorder); +print_liste_field_titre($arrayfields['status']['label'], $_SERVER["PHP_SELF"], "statut", "", $param, "", $sortfield, $sortorder); +print_liste_field_titre($arrayfields['link']['label'], $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder, 'center maxwidthsearch '); +print ""; + +foreach($list as $entry) +{ + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + + if ($permission) + { + $href = $_SERVER["PHP_SELF"]; + $href .= "?id=".$object->id; + $href .= "&action=deletecontact"; + $href .= "&lineid=".$entry->id; + + print ""; + } + + print ""; +} + +print "
'.$entry->nature.''.$entry->thirdparty.''.$entry->contact.''.$entry->type.''.$entry->status.'"; + print ""; + print img_picto($langs->trans("Unlink"), "unlink"); + print ""; + print "
"; +print ""; +print "
"; +print ""; + print "\n"; if (is_object($hookmanager)) { $hookmanager->initHooks(array('contacttpl')); @@ -248,3 +334,17 @@ if (is_object($hookmanager)) { $reshook=$hookmanager->executeHooks('formContactTpl', $parameters, $object, $action); } print "\n"; + + +// TODO: Remove this functions after switch to $db->sort($sortfield, $sortorder); +function nature_asc($left, $right) { return $left->nature > $right->nature; } +function thirdparty_asc($left, $right) { return $left->thirdparty > $right->thirdparty; } +function contact_asc($left, $right) { return $left->contact > $right->contact; } +function type_asc($left, $right) { return $left->type > $right->type; } +function status_asc($left, $right) { return $left->status > $right->status; } + +function nature_desc($left, $right) { return $left->nature < $right->nature; } +function thirdparty_desc($left, $right) { return $left->thirdparty < $right->thirdparty; } +function contact_desc($left, $right) { return $left->contact < $right->contact; } +function type_desc($left, $right) { return $left->type < $right->type; } +function status_desc($left, $right) { return $left->status < $right->status; }