Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify dropdowncriteria to add multiple option #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/autocriteria.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class PluginReportsAutoCriteria {
private $criterias_labels = [];

//Parameters are stored as name => value
private $parameters = [];
protected $parameters = [];

//Field in the SQL request (can be table.field)
private $sql_field = "";
Expand Down
1 change: 1 addition & 0 deletions inc/autoreport.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* - query executing using with criterias restriction
* - result display & export (HTML, PDF, CSV, SLK)
**/
#[\AllowDynamicProperties]
class PluginReportsAutoReport {

private $criterias = [];
Expand Down
127 changes: 87 additions & 40 deletions inc/dropdowncriteria.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,24 @@ class PluginReportsDropdownCriteria extends PluginReportsAutoCriteria {
// For special condition
private $condition = '';

private $multiple = false;

/**
* @param $report
* @param $name
* @param $tableortype (default '')
* @param $label (default '')
* @param $condition (default '')
**/
function __construct($report, $name, $tableortype='', $label='', $condition='') {

parent::__construct($report, $name, $name, $label);
/**
* @param $report
* @param $name
* @param $tableortype (default '')
* @param $label (default '')
* @param $condition (default '')
* @param $multiple (default false)
**/
function __construct($report, $name, $tableortype='', $label='', $condition='', $multiple = false) {
parent::__construct($report, $name, $name, $label);

$dbu = new DbUtils();

$this->condition = $condition;
$this->multiple = $multiple;

if (empty($tableortype)) {
$this->table = $dbu->getTableNameForForeignKeyField($name);
Expand Down Expand Up @@ -123,15 +126,19 @@ public function setSearchZero() {
}


/**
* Set default criteria value to 0 and entity restriction to current entity only
**/
public function setDefaultValues() {
/**
* Set default criteria value to 0 and entity restriction to current entity only
**/
public function setDefaultValues() {
if (!$this->multiple) {
$this->addParameter($this->getName(), 0);
} else {
$this->addParameter($this->getName(), []);
}

$this->addParameter($this->getName(), 0);
$this->setEntityRestriction($_SESSION["glpiactive_entity"]);
$this->setDisplayComments();
}
$this->setEntityRestriction($_SESSION["glpiactive_entity"]);
$this->setDisplayComments();
}


/**
Expand Down Expand Up @@ -219,10 +226,18 @@ public function getEntityRestrict() {
**/
public function getSubName() {

$value = $this->getParameterValue();
if ($value) {
return $this->getCriteriaLabel()." : ".Dropdown::getDropdownName($this->getTable(), $value);
}
$value = $this->getParameterValue();
if ($value) {
if ($this->multiple) {
$name = $this->getCriteriaLabel()." : ";
$names = [];
foreach($value as $v) {
$names[] = Dropdown::getDropdownName($this->getTable(), $v);
}
return $name . implode(', ', $names);
}
return $this->getCriteriaLabel()." : ".Dropdown::getDropdownName($this->getTable(), $value);
}

if ($this->searchzero) {
// zero
Expand Down Expand Up @@ -259,11 +274,40 @@ public function displayDropdownCriteria() {
'comments' => $this->getDisplayComments(),
'entity' => $this->getEntityRestrict()];

if ($this->condition) {
$options['condition'] = [$this->condition];
}
Dropdown::show($this->getItemType(), $options);
}
if ($this->condition) {
$options['condition'] = [$this->condition];
}

if ($this->multiple) {
$options['multiple'] = true;
$value = $this->getParameterValue() === 0 || $this->getParameterValue() === '0' ? [] : $this->getParameterValue();
$values = is_array($value) ? $value : [$value];
$options['used'] = $values;
$options['value'] = $values;
$options['name'] .= '[]';
$options['width'] = '100%';
}

Dropdown::show($this->getItemType(), $options);
}

public function getBookmarkUrl() {
if ($this->multiple) {
$url = "";
foreach ($this->parameters as $parameter => $value) {
if (is_array($value)) {
foreach($value as $i => $v) {
$url .= '&' . $parameter . "[$i]=" . $v;
}
} else {
$url .= '&' . $parameter . '=' . $value;
}
}
return $url;
} else {
return parent::getBookmarkUrl();
}
}


/**
Expand All @@ -275,19 +319,22 @@ public function getSqlCriteriasRestriction($link='AND') {

$dbu = new DbUtils();

if ($this->getParameterValue() || $this->searchzero) {
if (!$this->childrens) {
return $link . " " . $this->getSqlField() . "='" . $this->getParameterValue() . "' ";
}
if ($this->getParameterValue()) {
return $link . " " . $this->getSqlField() .
" IN (" . implode(',', $dbu->getSonsOf($this->getTable(),
$this->getParameterValue())) . ") ";
}
// 0 + its child means ALL
}
// Zero => means ALL => no criteria
return '';
}
if ($this->getParameterValue() || $this->searchzero) {
if ($this->multiple && $this->getParameterValue()) {
return $link . " " . $this->getSqlField() . " IN (" . implode(',', $this->getParameterValue()) . ") ";
}
if (!$this->childrens) {
return $link . " " . $this->getSqlField() . "='" . $this->getParameterValue() . "' ";
}
if ($this->getParameterValue()) {
return $link . " " . $this->getSqlField() .
" IN (" . implode(',', $dbu->getSonsOf($this->getTable(),
$this->getParameterValue())) . ") ";
}
// 0 + its child means ALL
}
// Zero => means ALL => no criteria
return '';
}

}
10 changes: 5 additions & 5 deletions inc/groupcriteria.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ class PluginReportsGroupCriteria extends PluginReportsDropdownCriteria {
* @param $name (default 'groups_id')
* @param $label (default '')
* @param $condition (default '')
**/
function __construct($report, $name='groups_id', $label='', $condition='') {

parent::__construct($report, $name, 'glpi_groups', ($label ? $label : __('Group')),
$condition);
* @param $multiple (default false)
**/
function __construct($report, $name='groups_id', $label='', $condition='', $multiple = false) {
parent::__construct($report, $name, 'glpi_groups', ($label ? $label : __('Group')),
$condition, $multiple);
}

}