grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026.
--------------------------------------------------------------------------
*/
/** @file
* @brief
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
/// CommonDropdown class - generic dropdown
abstract class CommonDropdown extends CommonDBTM {
// From CommonDBTM
public $dohistory = true;
// For delete operation (entity will overload this value)
public $must_be_replace = false;
//Menu & navigation
public $first_level_menu = "config";
public $second_level_menu = "CommonDropdown";
public $third_level_menu = "";
public $display_dropdowntitle = true;
//This dropdown can be translated
public $can_be_translated = true;
static $rightname = 'dropdown';
/**
* @since version 0.85
*
* @param $nb
**/
static function getTypeName($nb=0) {
return _n('Dropdown', 'Dropdowns', $nb);
}
/**
* Is translation enabled for this itemtype
*
* @since version 0.85
*
* @return true if translation is available, false otherwise
**/
function maybeTranslated () {
return $this->can_be_translated;
}
/**
* @see CommonGLPI::getMenuShorcut()
*
* @since version 0.85
**/
static function getMenuShorcut() {
return 'n';
}
/**
* @see CommonGLPI::getMenuContent()
*
* @since version 0.85
**/
static function getMenuContent() {
global $CFG_GLPI;
$menu = array();
if (get_called_class() == 'CommonDropdown') {
$menu['title'] = static::getTypeName(Session::getPluralNumber());
$menu['shortcut'] = 'n';
$menu['page'] = '/front/dropdown.php';
$menu['config']['default'] = '/front/dropdown.php';
$dps = Dropdown::getStandardDropdownItemTypes();
$menu['options'] = array();
foreach ($dps as $tab) {
foreach ($tab as $key => $val) {
if ($tmp = getItemForItemtype($key)) {
$menu['options'][$key]['title'] = $val;
$menu['options'][$key]['page'] = $tmp->getSearchURL(false);
$menu['options'][$key]['links']['search'] = $tmp->getSearchURL(false);
if ($tmp->canCreate()) {
$menu['options'][$key]['links']['add'] = $tmp->getFormURL(false);
}
}
}
}
if (count($menu['options'])) {
return $menu;
}
} else {
return parent::getMenuContent();
}
return false;
}
/**
* Return Additional Fields for this type
**/
function getAdditionalFields() {
return array();
}
function defineTabs($options=array()) {
$ong = array();
$this->addDefaultFormTab($ong);
if ($this->dohistory) {
$this->addStandardTab('Log',$ong, $options);
}
if (DropdownTranslation::canBeTranslated($this)) {
$this->addStandardTab('DropdownTranslation',$ong, $options);
}
return $ong;
}
/**
* Display title above search engine
*
* @return nothing (HTML display if needed)
**/
function title() {
if ($this->display_dropdowntitle) {
Dropdown::showItemTypeMenu(_n('Dropdown', 'Dropdowns', Session::getPluralNumber()),
Dropdown::getStandardDropdownItemTypes(),
$this->getSearchURL());
}
}
function displayHeader() {
if (empty($this->third_level_menu)) {
$this->third_level_menu = $this->getType();
}
Html::header($this->getTypeName(Session::getPluralNumber()), '', $this->first_level_menu, $this->second_level_menu,
$this->third_level_menu);
}
/**
* @since version 0.83.3
*
* @see CommonDBTM::prepareInputForAdd()
**/
function prepareInputForAdd($input) {
global $DB;
// if item based on location, create item in the same entity as location
if (isset($input['locations_id'])) {
foreach ($DB->request("SELECT `entities_id`
FROM `glpi_locations`
WHERE `id` = ".$input['locations_id']) as $data) {
$input['entities_id'] = $data['entities_id'];
}
}
if (isset($input['name'])) {
// leading/ending space will break findID/import
$input['name'] = trim($input['name']);
}
return $input;
}
/**
* @since version 0.83.3
*
* @see CommonDBTM::prepareInputForUpdate()
**/
function prepareInputForUpdate($input) {
return self::prepareInputForAdd($input);
}
function showForm($ID, $options=array()) {
global $CFG_GLPI;
if (!$this->isNewID($ID)) {
$this->check($ID, READ);
} else {
// Create item
$this->check(-1, CREATE);
}
$this->showFormHeader($options);
$fields = $this->getAdditionalFields();
$nb = count($fields);
echo "
".__('Name')."
";
echo "
";
if ($this instanceof CommonDevice) {
// Awfull hack for CommonDevice where name is designation
Html::autocompletionTextField($this, "designation");
} else {
Html::autocompletionTextField($this, "name");
}
echo "
";
echo "
". __('Comments')."
";
echo "
\n";
foreach ($fields as $field) {
if (($field['name'] == 'entities_id')
&& ($ID == 0)
&& !$this->isNewID($ID)) {
// No display for root entity
echo "
";
break;
}
if (!isset($field['type'])) {
$field['type'] = '';
}
if ($field['name'] == 'header') {
echo "
";
_e("Caution: you're about to remove a heading used for one or more items.");
echo "
";
if (!$this->must_be_replace) {
// Delete form (set to 0)
echo "
".__('If you confirm the deletion, all uses of this dropdown will be blanked.') .
"
";
echo "
";
}
/**
* check if a dropdown already exists (before import)
*
* @param &$input array of value to import (name)
*
* @return the ID of the new (or -1 if not found)
**/
function findID(array &$input) {
global $DB;
if (!empty($input["name"])) {
$query = "SELECT `id`
FROM `".$this->getTable()."`
WHERE `name` = '".$input["name"]."'";
if ($this->isEntityAssign()) {
$query .= getEntitiesRestrictRequest(' AND ', $this->getTable(), '',
$input['entities_id'], $this->maybeRecursive());
}
$query .= " LIMIT 1";
// Check twin :
if ($result_twin = $DB->query($query) ) {
if ($DB->numrows($result_twin) > 0) {
return $DB->result($result_twin, 0, "id");
}
}
}
return -1;
}
/**
* Import a dropdown - check if already exists
*
* @param $input array of value to import (name, ...)
*
* @return the ID of the new or existing dropdown (-1 on failure)
**/
function import(array $input) {
if (!isset($input['name'])) {
return -1;
}
// Clean datas
$input['name'] = trim($input['name']);
if (empty($input['name'])) {
return -1;
}
// Check twin :
if ($ID = $this->findID($input)) {
if ($ID > 0) {
return $ID;
}
}
return $this->add($input);
}
/**
* Import a value in a dropdown table.
*
* This import a new dropdown if it doesn't exist - Play dictionnary if needed
*
* @param $value string Value of the new dropdown (need to be addslashes)
* @param $entities_id int entity in case of specific dropdown (default -1)
* @param $external_params array (manufacturer) (need to be addslashes)
* @param $comment (default '') (need to be addslashes)
* @param $add if true, add it if not found. if false,
* just check if exists (true by default)
*
* @return integer : dropdown id.
**/
function importExternal($value, $entities_id=-1, $external_params=array(), $comment="",
$add=true) {
$value = trim($value);
if (strlen($value) == 0) {
return 0;
}
$ruleinput = array("name" => stripslashes($value));
$rulecollection = RuleCollection::getClassByType($this->getType(),true);
foreach ($this->additional_fields_for_dictionnary as $field) {
if (isset($external_params[$field])) {
$ruleinput[$field] = $external_params[$field];
} else {
$ruleinput[$field] = '';
}
}
/*
switch ($this->getTable()) {
case "glpi_computermodels" :
case "glpi_monitormodels" :
case "glpi_printermodels" :
case "glpi_peripheralmodels" :
case "glpi_phonemodels" :
case "glpi_networkequipmentmodels" :
$ruleinput["manufacturer"] = $external_params["manufacturer"];
break;
}*/
$input["name"] = $value;
$input["comment"] = $comment;
$input["entities_id"] = $entities_id;
if ($rulecollection) {
$res_rule = $rulecollection->processAllRules(Toolbox::stripslashes_deep($ruleinput), array(), array());
if (isset($res_rule["name"])) {
$input["name"] = $res_rule["name"];
}
}
return ($add ? $this->import($input) : $this->findID($input));
}
/**
* @see CommonDBTM::getSpecificMassiveActions()
**/
function getSpecificMassiveActions($checkitem=NULL) {
$isadmin = static::canUpdate();
$actions = parent::getSpecificMassiveActions($checkitem);
// Manage forbidden actions
$forbidden_actions = $this->getForbiddenStandardMassiveAction();
if ($isadmin
&& $this->maybeRecursive()
&& (count($_SESSION['glpiactiveentities']) > 1)
&& !in_array('merge', $forbidden_actions)) {
$actions[__CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'merge'] = __('Transfer and merge');
}
return $actions;
}
/**
* @since version 0.85
*
* @see CommonDBTM::showMassiveActionsSubForm()
**/
static function showMassiveActionsSubForm(MassiveAction $ma) {
switch ($ma->getAction()) {
case 'merge' :
echo " ".$_SESSION['glpiactive_entity_shortname'];
echo "
".Html::submit(_x('button', 'Merge'), array('name' => 'massiveaction'));
return true;
}
return parent::showMassiveActionsSubForm($ma);
}
/**
* @since version 0.85
*
* @see CommonDBTM::processMassiveActionsForOneItemtype()
**/
static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item,
array $ids) {
switch ($ma->getAction()) {
case 'merge' :
$fk = $item->getForeignKeyField();
foreach ($ids as $key) {
if ($item->can($key, UPDATE)) {
if ($item->getEntityID() == $_SESSION['glpiactive_entity']) {
if ($item->update(array('id' => $key,
'is_recursive' => 1))) {
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
$ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
}
} else {
$input2 = $item->fields;
// Remove keys (and name, tree dropdown will use completename)
if ($item instanceof CommonTreeDropdown) {
unset($input2['id'], $input2['name'], $input2[$fk]);
} else {
unset($input2['id']);
}
// Change entity
$input2['entities_id'] = $_SESSION['glpiactive_entity'];
$input2['is_recursive'] = 1;
$input2 = Toolbox::addslashes_deep($input2);
// Import new
if ($newid = $item->import($input2)) {
// Delete old
if ($newid > 0) {
// delete with purge for dropdown with dustbin (Budget)
$item->delete(array('id' => $key,
'_replace_by' => $newid), 1);
}
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
} else {
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
$ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
}
}
} else {
$ma->itemDone($item->getType(), $key, MassiveAction::ACTION_NORIGHT);
$ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
}
}
return;
}
parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
}
}
?>