.
* ---------------------------------------------------------------------
*/
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 0.85
*
* @param $nb
**/
static function getTypeName($nb = 0) {
return _n('Dropdown', 'Dropdowns', $nb);
}
/**
* Is translation enabled for this itemtype
*
* @since 0.85
*
* @return true if translation is available, false otherwise
**/
function maybeTranslated () {
return $this->can_be_translated;
}
/**
* @see CommonGLPI::getMenuShorcut()
*
* @since 0.85
**/
static function getMenuShorcut() {
return 'n';
}
/**
* @see CommonGLPI::getMenuContent()
*
* @since 0.85
**/
static function getMenuContent() {
$menu = [];
if (get_called_class() == 'CommonDropdown') {
$menu['title'] = static::getTypeName(Session::getPluralNumber());
$menu['shortcut'] = 'n';
$menu['page'] = '/front/dropdown.php';
$menu['icon'] = self::getIcon();
$menu['config']['default'] = '/front/dropdown.php';
$dps = Dropdown::getStandardDropdownItemTypes();
$menu['options'] = [];
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]['icon'] = $tmp->getIcon();
$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
*
* @return array
**/
function getAdditionalFields() {
global $DB;
if ($DB->fieldExists($this->getTable(), 'product_number')) {
return [['name' => 'product_number',
'type' => 'text',
'label' => __('Product Number')]];
} else {
return [];
}
}
function defineTabs($options = []) {
$ong = [];
$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 void 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 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']) && !isset($input['_is_update'])) {
$iterator = $DB->request([
'SELECT' => ['entities_id'],
'FROM' => 'glpi_locations',
'WHERE' => [
'id' => $input['locations_id']
]
]);
while ($data = $iterator->next()) {
$input['entities_id'] = $data['entities_id'];
}
}
if (isset($input['name'])) {
// leading/ending space will break findID/import
$input['name'] = trim($input['name']);
}
if (isset($input['_is_update'])) {
unset($input['_is_update']);
}
return $input;
}
/**
* @since 0.83.3
*
* @see CommonDBTM::prepareInputForUpdate()
**/
function prepareInputForUpdate($input) {
//add a "metadata to find if we're on an update or a add
$input['_is_update'] = true;
return self::prepareInputForAdd($input);
}
function showForm($ID, $options = []) {
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 "
";
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 "