From 9f130aa2af4ae5c97c261b391f61c47ab5c1ecc4 Mon Sep 17 00:00:00 2001 From: Egils Consulting Date: Fri, 7 Aug 2020 23:17:43 +0200 Subject: [PATCH] New function count_all_categories This is useful to count the total number of categories e.g. to decide whether to load the full tree or not. It might make most sense to wait with merging until there is code in viewcat.php or index.php using it. --- htdocs/categories/class/categorie.class.php | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 7d3cdfde518..111f3bf8a88 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -2010,4 +2010,29 @@ class Categorie extends CommonObject return ""; } } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Count all categories + * + * @return int Number of categories, -1 on error + */ + public function count_all_categories() + { + dol_syslog(get_class($this)."::count_all_categories", LOG_DEBUG); + $sql = "SELECT COUNT(rowid) FROM ".MAIN_DB_PREFIX."categorie"; + $sql .= " WHERE entity IN (".getEntity('category').")"; + + $res = $this->db->query($sql); + if ($res) + { + $obj = $this->db->fetch_object($res); + return $obj->count; + } + else + { + dol_print_error($this->db); + return -1; + } + } }