diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php index 5db9b4be136..fd8fbe163b7 100644 --- a/htdocs/categories/class/categorie.class.php +++ b/htdocs/categories/class/categorie.class.php @@ -983,19 +983,42 @@ class Categorie extends CommonObject * fulllabel = nom avec chemin complet de la categorie * fullpath = chemin complet compose des id * - * @param string $type Type of categories ('customer', 'supplier', 'contact', 'product', 'member') or (0, 1, 2, ...). - * @param int $markafterid Removed all categories including the leaf $markafterid in category tree. - * @param int $keeponlyifinleafid Keep only of category is inside the leaf starting with this id. - * @return array|int Array of categories. this->cats and this->motherof are set, -1 on error + * @param string $type Type of categories ('customer', 'supplier', 'contact', 'product', 'member') or (0, 1, 2, ...). + * @param int|string|array $markafterid Removed all categories including the leaf $markafterid in category tree (exclude) or Keep only of category is inside the leaf starting with this id. + * $markafterid can be an : int (id of category) + * string (categories ids seprated by comma) + * array (list of categories ids) + * @param int $include [=0] Exlude or 1=Include + * @return array|int Array of categories. this->cats and this->motherof are set, -1 on error */ - public function get_full_arbo($type, $markafterid = 0, $keeponlyifinleafid = 0) + public function get_full_arbo($type, $markafterid = 0, $include = 0) { // phpcs:enable global $conf, $langs; if (! is_numeric($type)) $type = $this->MAP_ID[$type]; - $this->cats = array(); + if (is_string($markafterid)) + { + $markafterid = explode(',', $markafterid); + } + elseif (is_numeric($markafterid)) + { + if ($markafterid > 0) + { + $markafterid = array($markafterid); + } + else + { + $markafterid = array(); + } + } + elseif (!is_array($markafterid)) + { + $markafterid = array(); + } + + $this->cats = array(); // Init this->motherof that is array(id_son=>id_parent, ...) $this->load_motherof(); @@ -1040,39 +1063,22 @@ class Categorie extends CommonObject $this->build_path_from_id_categ($key, 0); // Process a branch from the root category key (this category has no parent) } - // Exclude leaf including $markafterid from tree - if ($markafterid) + // Include or exclude leaf including $markafterid from tree + if (count($markafterid) > 0) { + $keyfiltercatid = implode('|', $markafterid); + //print "Look to discard category ".$markafterid."\n"; - $keyfilter1='^'.$markafterid.'$'; - $keyfilter2='_'.$markafterid.'$'; - $keyfilter3='^'.$markafterid.'_'; - $keyfilter4='_'.$markafterid.'_'; + $keyfilter1 = '^' . $keyfiltercatid . '$'; + $keyfilter2 = '_' . $keyfiltercatid . '$'; + $keyfilter3 = '^' . $keyfiltercatid . '_'; + $keyfilter4 = '_' . $keyfiltercatid . '_'; foreach($this->cats as $key => $val) { - if (preg_match('/'.$keyfilter1.'/', $val['fullpath']) || preg_match('/'.$keyfilter2.'/', $val['fullpath']) - || preg_match('/'.$keyfilter3.'/', $val['fullpath']) || preg_match('/'.$keyfilter4.'/', $val['fullpath'])) - { - unset($this->cats[$key]); - } - } - } - // Exclude leaf including $markafterid from tree - if ($keeponlyifinleafid) - { - //print "Look to discard category ".$keeponlyifinleafid."\n"; - $keyfilter1='^'.$keeponlyifinleafid.'$'; - $keyfilter2='_'.$keeponlyifinleafid.'$'; - $keyfilter3='^'.$keeponlyifinleafid.'_'; - $keyfilter4='_'.$keeponlyifinleafid.'_'; - foreach($this->cats as $key => $val) - { - if (preg_match('/'.$keyfilter1.'/', $val['fullpath']) || preg_match('/'.$keyfilter2.'/', $val['fullpath']) - || preg_match('/'.$keyfilter3.'/', $val['fullpath']) || preg_match('/'.$keyfilter4.'/', $val['fullpath'])) - { - // We keep - } - else + $test = (preg_match('/' . $keyfilter1 . '/', $val['fullpath']) || preg_match('/' . $keyfilter2 . '/', $val['fullpath']) + || preg_match('/' . $keyfilter3 . '/', $val['fullpath']) || preg_match('/' . $keyfilter4 . '/', $val['fullpath'])); + + if (($test && !$include) || (!$test && $include)) { unset($this->cats[$key]); } diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 32238ea4ef8..865aa50b679 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1180,6 +1180,8 @@ class ExtraFields // 2 : key fields name (if differ of rowid) // 3 : key field parent (for dependent lists) // 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value + // 5 : id category type + // 6 : ids categories list separated by comma for category root $keyList=(empty($InfoFieldList[2])?'rowid':$InfoFieldList[2].' as rowid'); @@ -1198,136 +1200,130 @@ class ExtraFields $keyList.= ', '.$parentField; } - $fields_label = explode('|', $InfoFieldList[1]); - if (is_array($fields_label)) - { - $keyList .=', '; - $keyList .= implode(', ', $fields_label); - } + $filter_categorie = false; + if (count($InfoFieldList) > 5) { + if ($InfoFieldList[0] == 'categorie') { + $filter_categorie = true; + } + } - $sqlwhere=''; - $sql = 'SELECT '.$keyList; - $sql.= ' FROM '.MAIN_DB_PREFIX .$InfoFieldList[0]; - if (!empty($InfoFieldList[4])) - { - // can use curent entity filter - if (strpos($InfoFieldList[4], '$ENTITY$')!==false) { - $InfoFieldList[4]=str_replace('$ENTITY$', $conf->entity, $InfoFieldList[4]); - } - // can use SELECT request - if (strpos($InfoFieldList[4], '$SEL$')!==false) { - $InfoFieldList[4]=str_replace('$SEL$', 'SELECT', $InfoFieldList[4]); - } + if ($filter_categorie === false) { + $fields_label = explode('|', $InfoFieldList[1]); + if (is_array($fields_label)) { + $keyList .= ', '; + $keyList .= implode(', ', $fields_label); + } - // current object id can be use into filter - if (strpos($InfoFieldList[4], '$ID$')!==false && !empty($objectid)) { - $InfoFieldList[4]=str_replace('$ID$', $objectid, $InfoFieldList[4]); - } else { - $InfoFieldList[4]=str_replace('$ID$', '0', $InfoFieldList[4]); - } - //We have to join on extrafield table - if (strpos($InfoFieldList[4], 'extra')!==false) - { - $sql.= ' as main, '.MAIN_DB_PREFIX .$InfoFieldList[0].'_extrafields as extra'; - $sqlwhere.= ' WHERE extra.fk_object=main.'.$InfoFieldList[2]. ' AND '.$InfoFieldList[4]; - } - else - { - $sqlwhere.= ' WHERE '.$InfoFieldList[4]; - } - } - else - { - $sqlwhere.= ' WHERE 1=1'; - } - // Some tables may have field, some other not. For the moment we disable it. - if (in_array($InfoFieldList[0], array('tablewithentity'))) - { - $sqlwhere.= ' AND entity = '.$conf->entity; - } - $sql.=$sqlwhere; - //print $sql; + $sqlwhere = ''; + $sql = 'SELECT ' . $keyList; + $sql .= ' FROM ' . MAIN_DB_PREFIX . $InfoFieldList[0]; + if (!empty($InfoFieldList[4])) { + // can use curent entity filter + if (strpos($InfoFieldList[4], '$ENTITY$') !== false) { + $InfoFieldList[4] = str_replace('$ENTITY$', $conf->entity, $InfoFieldList[4]); + } + // can use SELECT request + if (strpos($InfoFieldList[4], '$SEL$') !== false) { + $InfoFieldList[4] = str_replace('$SEL$', 'SELECT', $InfoFieldList[4]); + } - $sql .= ' ORDER BY ' . implode(', ', $fields_label); + // current object id can be use into filter + if (strpos($InfoFieldList[4], '$ID$') !== false && !empty($objectid)) { + $InfoFieldList[4] = str_replace('$ID$', $objectid, $InfoFieldList[4]); + } else { + $InfoFieldList[4] = str_replace('$ID$', '0', $InfoFieldList[4]); + } + //We have to join on extrafield table + if (strpos($InfoFieldList[4], 'extra') !== false) { + $sql .= ' as main, ' . MAIN_DB_PREFIX . $InfoFieldList[0] . '_extrafields as extra'; + $sqlwhere .= ' WHERE extra.fk_object=main.' . $InfoFieldList[2] . ' AND ' . $InfoFieldList[4]; + } else { + $sqlwhere .= ' WHERE ' . $InfoFieldList[4]; + } + } else { + $sqlwhere .= ' WHERE 1=1'; + } + // Some tables may have field, some other not. For the moment we disable it. + if (in_array($InfoFieldList[0], array('tablewithentity'))) { + $sqlwhere .= ' AND entity = ' . $conf->entity; + } + $sql .= $sqlwhere; + //print $sql; - dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) - { - $out.=''; - $num = $this->db->num_rows($resql); - $i = 0; - while ($i < $num) - { - $labeltoshow=''; - $obj = $this->db->fetch_object($resql); + $sql .= ' ORDER BY ' . implode(', ', $fields_label); - // Several field into label (eq table:code|libelle:rowid) - $notrans = false; - $fields_label = explode('|', $InfoFieldList[1]); - if (is_array($fields_label)) - { - $notrans = true; - foreach ($fields_label as $field_toshow) - { - $labeltoshow.= $obj->$field_toshow.' '; - } - } - else - { - $labeltoshow=$obj->{$InfoFieldList[1]}; - } - $labeltoshow=dol_trunc($labeltoshow, 45); + dol_syslog(get_class($this) . '::showInputField type=sellist', LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $out .= ''; + $num = $this->db->num_rows($resql); + $i = 0; + while ($i < $num) { + $labeltoshow = ''; + $obj = $this->db->fetch_object($resql); - if ($value == $obj->rowid) - { - foreach ($fields_label as $field_toshow) - { - $translabel=$langs->trans($obj->$field_toshow); - if ($translabel!=$obj->$field_toshow) { - $labeltoshow=dol_trunc($translabel, 18).' '; - }else { - $labeltoshow=dol_trunc($obj->$field_toshow, 18).' '; - } - } - $out.=''; - } - else - { - if (! $notrans) - { - $translabel=$langs->trans($obj->{$InfoFieldList[1]}); - if ($translabel!=$obj->{$InfoFieldList[1]}) { - $labeltoshow=dol_trunc($translabel, 18); - } - else { - $labeltoshow=dol_trunc($obj->{$InfoFieldList[1]}, 18); - } - } - if (empty($labeltoshow)) $labeltoshow='(not defined)'; - if ($value==$obj->rowid) - { - $out.=''; - } + // Several field into label (eq table:code|libelle:rowid) + $notrans = false; + $fields_label = explode('|', $InfoFieldList[1]); + if (is_array($fields_label)) { + $notrans = true; + foreach ($fields_label as $field_toshow) { + $labeltoshow .= $obj->$field_toshow . ' '; + } + } else { + $labeltoshow = $obj->{$InfoFieldList[1]}; + } + $labeltoshow = dol_trunc($labeltoshow, 45); - if (!empty($InfoFieldList[3]) && $parentField) - { - $parent = $parentName.':'.$obj->{$parentField}; - } + if ($value == $obj->rowid) { + foreach ($fields_label as $field_toshow) { + $translabel = $langs->trans($obj->$field_toshow); + if ($translabel != $obj->$field_toshow) { + $labeltoshow = dol_trunc($translabel, 18) . ' '; + } else { + $labeltoshow = dol_trunc($obj->$field_toshow, 18) . ' '; + } + } + $out .= ''; + } else { + if (!$notrans) { + $translabel = $langs->trans($obj->{$InfoFieldList[1]}); + if ($translabel != $obj->{$InfoFieldList[1]}) { + $labeltoshow = dol_trunc($translabel, 18); + } else { + $labeltoshow = dol_trunc($obj->{$InfoFieldList[1]}, 18); + } + } + if (empty($labeltoshow)) $labeltoshow = '(not defined)'; + if ($value == $obj->rowid) { + $out .= ''; + } - $out.=''; - } + if (!empty($InfoFieldList[3]) && $parentField) { + $parent = $parentName . ':' . $obj->{$parentField}; + } - $i++; - } - $this->db->free($resql); - } - else { - print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.
'; - } + $out .= ''; + } + + $i++; + } + $this->db->free($resql); + } else { + print 'Error in request ' . $sql . ' ' . $this->db->lasterror() . '. Check setup of extra parameters.
'; + } + } else { + $data = $form->select_all_categories(Categorie::$MAP_ID_TO_CODE[$InfoFieldList[5]], '', 'parent', 64, $InfoFieldList[6], 1, 1); + $out .= ''; + foreach ($data as $data_key => $data_value) { + $out .= '