FormCategory : create method to select the product category

This commit is contained in:
Thomas Negre
2022-02-03 16:03:25 +01:00
parent 8d997de3bb
commit 2cf433019e
2 changed files with 40 additions and 1 deletions

View File

@@ -3708,7 +3708,6 @@ class Form
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Load into cache list of payment terms

View File

@@ -60,4 +60,44 @@ class FormCategory extends Form
return $filter;
}
/**
* Prints a select form for products categories
* @param string $selected Id category pre-selection
* @param string $htmlname Name of HTML field
* @param int $showempty Add an empty field
* @return integer|null
*/
public function selectProductCategory($selected = 0, $htmlname = 'product_category_id', $showempty = 0)
{
global $conf;
$sql = "SELECT cp.fk_categorie as cat_index, cat.label FROM `llx_categorie_product` as cp INNER JOIN llx_categorie as cat ON cat.rowid = cp.fk_categorie GROUP BY cp.fk_categorie;";
dol_syslog(get_class($this)."::selectProductCategory", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
print '<select class="flat" id="select_'.$htmlname.'" name="'.$htmlname.'">';
if ($showempty) {
print '<option value="0">&nbsp;</option>';
}
$i = 0;
$num_rows = $this->db->num_rows($resql);
while ($i < $num_rows) {
$category = $this->db->fetch_object($resql);
if ($selected && $selected == $category->cat_index) {
print '<option value="'.$category->cat_index.'" selected>'.$category->label.'</option>';
} else {
print '<option value="'.$category->cat_index.'">'.$category->label.'</option>';
}
$i++;
}
print ('</select>');
return $num_rows;
} else {
dol_print_error($this->db);
}
}
}