From bf104f609afc098402c96fbe4e266d5f20333e65 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 10 Sep 2019 01:44:36 +0200 Subject: [PATCH] NEW Add pagination on list of object of a category --- htdocs/categories/class/categorie.class.php | 23 ++- htdocs/categories/viewcat.php | 192 +++++++++++++++++--- 2 files changed, 181 insertions(+), 34 deletions(-) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index b9a92f85d5e..60fb76293ea 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -746,13 +746,19 @@ class Categorie extends CommonObject /** * Return list of fetched instance of elements having this category * - * @param string $type Type of category ('customer', 'supplier', 'contact', 'product', 'member') - * @param int $onlyids Return only ids of objects (consume less memory) - * @return array|int -1 if KO, array of instance of object if OK + * @param string $type Type of category ('customer', 'supplier', 'contact', 'product', 'member') + * @param int $onlyids Return only ids of objects (consume less memory) + * @param int $limit Limit + * @param int $offset Offset + * @param string $sortfield Sort fields + * @param string $sortorder Sort order ('ASC' or 'DESC'); + * @return array|int -1 if KO, array of instance of object if OK * @see containsObject() */ - public function getObjectsInCateg($type, $onlyids = 0) + public function getObjectsInCateg($type, $onlyids = 0, $limit = 0, $offset = 0, $sortfield = '', $sortorder = 'ASC') { + global $user; + $objs = array(); $obj = new $this->MAP_OBJ_CLASS[$type]( $this->db ); @@ -761,8 +767,15 @@ class Categorie extends CommonObject $sql .= " FROM " . MAIN_DB_PREFIX . "categorie_" . $this->MAP_CAT_TABLE[$type] . " as c"; $sql .= ", " . MAIN_DB_PREFIX . $this->MAP_OBJ_TABLE[$type] . " as o"; $sql .= " WHERE o.entity IN (" . getEntity($obj->element).")"; - $sql.= " AND c.fk_categorie = ".$this->id; + $sql .= " AND c.fk_categorie = ".$this->id; $sql .= " AND c.fk_" . $this->MAP_CAT_FK[$type] . " = o.rowid"; + // Protection for external users + if (($type == 'customer' || $type == 'supplier') && $user->societe_id > 0) + { + $sql.= " AND o.rowid = ".$user->societe_id; + } + if ($limit > 0 || $offset > 0) $sql .= $this->db->plimit($limit + 1, $offset); + $sql .= $this->db->order($sortfield, $sortorder); dol_syslog(get_class($this)."::getObjectsInCateg", LOG_DEBUG); $resql = $this->db->query($sql); diff --git a/htdocs/categories/viewcat.php b/htdocs/categories/viewcat.php index a7a0767c6b5..67ef7905235 100644 --- a/htdocs/categories/viewcat.php +++ b/htdocs/categories/viewcat.php @@ -34,14 +34,33 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; // Load translation files required by the page $langs->load("categories"); -$id = GETPOST('id', 'int'); -$label= GETPOST('label', 'alpha'); -$type = GETPOST('type', 'az09'); -$action=GETPOST('action', 'aZ09'); -$confirm = GETPOST('confirm', 'alpha'); +$id = GETPOST('id', 'int'); +$label = GETPOST('label', 'alpha'); +$type = GETPOST('type', 'az09'); $removeelem = GETPOST('removeelem', 'int'); $elemid = GETPOST('elemid', 'int'); +$action = GETPOST('action', 'aZ09')?GETPOST('action', 'aZ09'):'view'; // The action 'add', 'create', 'edit', 'update', 'view', ... +$massaction = GETPOST('massaction', 'alpha'); // The bulk action (combo box choice into lists) +$show_files = GETPOST('show_files', 'int'); // Show files area generated by bulk actions ? +$confirm = GETPOST('confirm', 'alpha'); // Result of a confirmation +$cancel = GETPOST('cancel', 'alpha'); // We click on a Cancel button +$toselect = GETPOST('toselect', 'array'); // Array of ids of elements selected into a list +$contextpage= GETPOST('contextpage', 'aZ')?GETPOST('contextpage', 'aZ'):'myobjectlist'; // To manage different context of search +$backtopage = GETPOST('backtopage', 'alpha'); // Go back to a dedicated page +$optioncss = GETPOST('optioncss', 'aZ'); // Option for the css output (always '' except when 'print') + + +// Load variable for pagination +$limit = GETPOST('limit', 'int')?GETPOST('limit', 'int'):$conf->liste_limit; +$sortfield = GETPOST('sortfield', 'alpha'); +$sortorder = GETPOST('sortorder', 'alpha'); +$page = GETPOST('page', 'int'); +if (empty($page) || $page == -1 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha') || (empty($toselect) && $massaction === '0')) { $page = 0; } // If $page is not defined, or '' or -1 or if we click on clear filters or if we select empty mass action +$offset = $limit * $page; +$pageprev = $page - 1; +$pagenext = $page + 1; + if ($id == "" && $label == "") { dol_print_error('', 'Missing parameter id'); @@ -319,10 +338,20 @@ else } +// List of mass actions available +$arrayofmassactions = array( + //'validate'=>$langs->trans("Validate"), + //'generate_doc'=>$langs->trans("ReGeneratePDF"), + //'builddoc'=>$langs->trans("PDFMerge"), + //'presend'=>$langs->trans("SendByMail"), +); +$massactionbutton=$form->selectMassAction('', $arrayofmassactions); + + // List of products or services (type is type of category) if ($type == Categorie::TYPE_PRODUCT) { - $prods = $object->getObjectsInCateg("product"); + $prods = $object->getObjectsInCateg("product", 0, $limit, $offset); if ($prods < 0) { dol_print_error($db, $prods->error, $prods->errors); @@ -351,14 +380,28 @@ if ($type == Categorie::TYPE_PRODUCT) print ''; } - print "
"; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($prods); $nbtotalofrecords = ''; $newcardbutton = ''; + print_barre_liste($langs->trans("ProductsAndServices"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, $newcardbutton, '', $limit); + print "\n"; - print ''."\n"; + print ''."\n"; if (count($prods) > 0) { + $i = 0; foreach ($prods as $prod) { + $i++; + if ($i > $limit) break; + print "\t".''."\n"; print ''; } print "
'.$langs->trans("ProductsAndServices").' '.count($prods).'
'.$langs->trans("Ref").'
'; print $prod->getNomUrl(1); @@ -388,26 +431,42 @@ if ($type == Categorie::TYPE_PRODUCT) print '
'.$langs->trans("ThisCategoryHasNoProduct").'
\n"; + + print '
'."\n"; } } if ($type == Categorie::TYPE_SUPPLIER) { - $socs = $object->getObjectsInCateg("supplier"); + $socs = $object->getObjectsInCateg("supplier", 0, $limit, $offset); if ($socs < 0) { dol_print_error($db, $socs->error, $socs->errors); } else { - print "
"; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($socs); $nbtotalofrecords = ''; $newcardbutton = ''; + print_barre_liste($langs->trans("Suppliers"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, $newcardbutton, '', $limit); + print ''."\n"; - print '\n"; + print '\n"; if (count($socs) > 0) { + $i = 0; foreach ($socs as $soc) { + $i++; + if ($i > $limit) break; + print "\t".''."\n"; print ''; } print "
'.$langs->trans("Suppliers").' '.count($socs)."
'.$langs->trans("Name")."
'; print $soc->getNomUrl(1); @@ -437,30 +496,41 @@ if ($type == Categorie::TYPE_SUPPLIER) print '
'.$langs->trans("ThisCategoryHasNoSupplier").'
\n"; + + print '
'."\n"; } } if($type == Categorie::TYPE_CUSTOMER) { - $socs = $object->getObjectsInCateg("customer"); + $socs = $object->getObjectsInCateg("customer", 0, $limit, $offset); if ($socs < 0) { dol_print_error($db, $socs->error, $socs->errors); } else { - print "
"; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($socs); $nbtotalofrecords = ''; $newcardbutton = ''; + print_barre_liste($langs->trans("Customers"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, $newcardbutton, '', $limit); + print ''."\n"; - print ''."\n"; + print ''."\n"; if (count($socs) > 0) { $i = 0; foreach ($socs as $key => $soc) { - if ($user->societe_id > 0 && $soc->id != $user->societe_id) continue; // External user always see only themself - $i++; + if ($i > $limit) break; print "\t".''."\n"; print ''; } print "
'.$langs->trans("Customers").' '.count($socs).'
'.$langs->trans("Name").'
'; @@ -490,6 +560,8 @@ if($type == Categorie::TYPE_CUSTOMER) print '
'.$langs->trans("ThisCategoryHasNoCustomer").'
\n"; + + print '
'."\n"; } } @@ -498,21 +570,35 @@ if ($type == Categorie::TYPE_MEMBER) { require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; - $prods = $object->getObjectsInCateg("member"); + $prods = $object->getObjectsInCateg("member", 0, $limit, $offset); if ($prods < 0) { dol_print_error($db, $prods->error, $prods->errors); } else { - print "
"; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($prods); $nbtotalofrecords = ''; $newcardbutton = ''; + print_barre_liste($langs->trans("Member"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, $newcardbutton, '', $limit); + print "\n"; - print ''."\n"; + print ''."\n"; if (count($prods) > 0) { + $i = 0; foreach ($prods as $key => $member) { + $i++; + if ($i > $limit) break; + print "\t".''."\n"; print ''; } print "
'.$langs->trans("Member").' '.count($prods).'
'.$langs->trans("Name").'
'; $member->ref=$member->login; @@ -543,22 +629,35 @@ if ($type == Categorie::TYPE_MEMBER) print '
'.$langs->trans("ThisCategoryHasNoMember").'
\n"; + + print '
'."\n"; } } // Categorie contact if ($type == Categorie::TYPE_CONTACT) { - $contacts = $object->getObjectsInCateg("contact"); + $contacts = $object->getObjectsInCateg("contact", 0, $limit, $offset); if ($contacts < 0) { dol_print_error($db, $contacts->error, $contacts->errors); } else { - print "
"; + + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($contacts); $nbtotalofrecords = ''; $newcardbutton = ''; + print_barre_liste($langs->trans("Contact"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, $newcardbutton, '', $limit); + print ''."\n"; - print ''."\n"; + print ''."\n"; if (count($contacts) > 0) { @@ -566,6 +665,7 @@ if ($type == Categorie::TYPE_CONTACT) foreach ($contacts as $key => $contact) { $i++; + if ($i > $limit) break; print "\t".''."\n"; print ''; } print "
'.$langs->trans("Contact").' '.count($contacts).'
'.$langs->trans("Ref").'
'; @@ -595,6 +695,8 @@ if ($type == Categorie::TYPE_CONTACT) print '
'.$langs->trans("ThisCategoryHasNoContact").'
\n"; + + print '
'."\n"; } } @@ -603,21 +705,35 @@ if ($type == Categorie::TYPE_ACCOUNT) { require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; - $accounts = $object->getObjectsInCateg("account"); + $accounts = $object->getObjectsInCateg("account", 0, $limit, $offset); if ($accounts < 0) { dol_print_error($db, $accounts->error, $accounts->errors); } else { - print "
"; - print "\n"; - print ''."\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($accounts); $nbtotalofrecords = ''; $newcardbutton = ''; + print_barre_liste($langs->trans("Account"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, $newcardbutton, '', $limit); + + print "
'.$langs->trans("Account").' '.count($accounts).'
\n"; + print ''."\n"; if (count($accounts) > 0) { + $i = 0; foreach ($accounts as $key => $account) { + $i++; + if ($i > $limit) break; + print "\t".''."\n"; print ''; } print "
'.$langs->trans("Ref").'
'; print $account->getNomUrl(1, 0); @@ -647,6 +763,8 @@ if ($type == Categorie::TYPE_ACCOUNT) print '
'.$langs->trans("ThisCategoryHasNoAccount").'
\n"; + + print ''."\n"; } } @@ -655,21 +773,35 @@ if ($type == Categorie::TYPE_PROJECT) { require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; - $projects = $object->getObjectsInCateg("project"); + $projects = $object->getObjectsInCateg("project", 0, $limit, $offset); if ($projects < 0) { dol_print_error($db, $object->error, $object->errors); } else { - print "
"; + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + + print '
'; + $param = '&limit='.$limit.'&id='.$id.'&type='.$type; $num = count($projects); $nbtotalofrecords = ''; $newcardbutton = ''; + print_barre_liste($langs->trans("Project"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, '', 0, $newcardbutton, '', $limit); + print "\n"; - print ''."\n"; + print ''."\n"; if (count($projects) > 0) { + $i = 0; foreach ($projects as $key => $project) { + $i++; + if ($i > $limit) break; + print "\t".''."\n"; print ''; } print "
'.$langs->trans("Project").' '.count($projects).'
'.$langs->trans("Ref").'
'; print $project->getNomUrl(1); @@ -699,6 +831,8 @@ if ($type == Categorie::TYPE_PROJECT) print '
'.$langs->trans("ThisCategoryHasNoProject").'
\n"; + + print '
'."\n"; } }