dolibarr 21.0.0-beta
html.formcategory.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
27require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
28
29
33class FormCategory extends Form
34{
47 public function getFilterBox($type, array $preSelected, $morecss = "minwidth300imp widthcentpercentminusx", $searchCategoryProductOperator = -1, $multiselect = 1, $nocateg = 1, $showempty = '')
48 {
49 global $langs, $db;
50
51 if (empty($preSelected) || !is_array($preSelected)) {
52 $preSelected = array();
53 }
54
55 if ($showempty && !is_numeric($showempty)) {
56 $tmptitle = $showempty;
57 } else {
58 $tmptitle = $langs->transnoentitiesnoconv("Category");
59 }
60
61 $filter = '';
62 $filter .= '<div class="divsearchfield">';
63 $filter .= img_picto($tmptitle, 'category', 'class="pictofixedwidth"');
64 if ($multiselect) {
65 $categoryArray = $this->select_all_categories($type, '', '', 64, 0, 2);
66 if ($nocateg) {
67 $categoryArray[-2] = "- ".$langs->trans('NotCategorized')." -";
68 }
69 $htmlName = "search_category_".$type."_list";
70 $htmlName2 = "search_category_".$type."_operator";
71
72 $filter .= Form::multiselectarray($htmlName, $categoryArray, $preSelected, 0, 0, $morecss, 0, 0, '', '', $tmptitle);
73 } else {
74 $htmlName = "search_".$type."_category";
75 $htmlName2 = "";
76 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
77 $formother = new FormOther($db);
78
79 $filter .= $formother->select_categories($type, $preSelected[0], $htmlName, $nocateg, $tmptitle, $morecss);
80 }
81 if ($multiselect && $searchCategoryProductOperator >= 0) {
82 $filter .= ' <input type="checkbox" class="valignmiddle '.$htmlName2.'" id="'.$htmlName2.'" name="'.$htmlName2.'" value="1"'.($searchCategoryProductOperator == 1 ? ' checked="checked"' : '').' title="'.dol_escape_htmltag($langs->trans('UseOrOperatorForCategories')).'" />';
83 $filter .= '<label class="none valignmiddle '.$htmlName2.'" for="'.$htmlName2.'" title="'.dol_escape_htmltag($langs->trans('UseOrOperatorForCategories')).'">';
84 $filter .= $langs->trans('UseOrOperatorShort');
85 $filter .= '</label>';
86
87 $filter .= '<script>'."\n";
88 $filter .= "var nbSelected = jQuery('#".$htmlName."').val().length;";
89 $filter .= "console.log('Nb of element now = '+nbSelected);\n";
90 $filter .= "if (nbSelected > 1) { jQuery('.".$htmlName2."').show(); } else { jQuery('.".$htmlName2."').hide(); }\n";
91 $filter .= "jQuery('#".$htmlName."').change(function() {\n";
92 $filter .= "console.log('Content of select box has been modified.');";
93 $filter .= 'var nbSelected = $(this).val().length;';
94 $filter .= "console.log('Nb of element now = '+nbSelected);\n";
95 $filter .= "if (nbSelected > 1) { jQuery('.".$htmlName2."').show(); } else { jQuery('.".$htmlName2."').hide(); }\n";
96 $filter .= '});'."\n";
97 $filter .= '</script>'."\n";
98 }
99 $filter .= "</div>";
100
101 return $filter;
102 }
103
113 public function selectProductCategory($selected = 0, $htmlname = 'product_category_id', $showempty = 0)
114 {
115 $sql = "SELECT cat.rowid, cat.label";
116 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as cat";
117 $sql .= " WHERE cat.type = 0";
118
119 dol_syslog(get_class($this)."::selectProductCategory", LOG_DEBUG);
120 $resql = $this->db->query($sql);
121 if ($resql) {
122 print '<select class="flat minwidth100" id="select_'.$htmlname.'" name="'.$htmlname.'">';
123 if ($showempty) {
124 print '<option value="0">&nbsp;</option>';
125 }
126
127 $i = 0;
128 $num_rows = $this->db->num_rows($resql);
129 while ($i < $num_rows) {
130 $category = $this->db->fetch_object($resql);
131 if ($selected && $selected == $category->rowid) {
132 print '<option value="'.$category->rowid.'" selected>'.$category->label.'</option>';
133 } else {
134 print '<option value="'.$category->rowid.'">'.$category->label.'</option>';
135 }
136 $i++;
137 }
138 print('</select>');
139
140 return $num_rows;
141 } else {
142 dol_print_error($this->db);
143 return null;
144 }
145 }
146}
Class to manage forms for categories.
getFilterBox($type, array $preSelected, $morecss="minwidth300imp widthcentpercentminusx", $searchCategoryProductOperator=-1, $multiselect=1, $nocateg=1, $showempty='')
Return a HTML filter box for a list filter view.
selectProductCategory($selected=0, $htmlname='product_category_id', $showempty=0)
Prints a select form for products categories TODO Remove this.
Class to manage generation of HTML components Only common components must be here.
select_all_categories($type, $selected='', $htmlname="parent", $maxlength=64, $fromid=0, $outputmode=0, $include=0, $morecss='', $useempty=1)
Return list of categories having chosen type.
static multiselectarray($htmlname, $array, $selected=array(), $key_in_label=0, $value_as_key=0, $morecss='', $translate=0, $width=0, $moreattrib='', $elemtype='', $placeholder='', $addjscombo=-1)
Show a multiselect form from an array.
Class permettant la generation de composants html autre Only common components are here.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...