Add GenericObject plugin files and assets
- Introduced default icon images in PNG and SVG formats. - Added example icons for car and cube in SVG format. - Created setup file for the GenericObject plugin with versioning and directory definitions. - Included a header file with licensing information for the plugin. - Added a remove.txt file with instructions for safe deletion.
This commit is contained in:
95
plugins/genericobject/inc/autoload.php
Normal file
95
plugins/genericobject/inc/autoload.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
class PluginGenericobjectAutoloader
|
||||
{
|
||||
protected $paths = [];
|
||||
|
||||
public function __construct($options = null) {
|
||||
if (null !== $options) {
|
||||
$this->setOptions($options);
|
||||
}
|
||||
}
|
||||
|
||||
public function setOptions($options) {
|
||||
if (!is_array($options) && !($options instanceof \Traversable)) {
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
foreach ($options as $path) {
|
||||
if (!in_array($path, $this->paths)) {
|
||||
$this->paths[] = $path;
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function processClassname($classname) {
|
||||
preg_match("/^Plugin([A-Z][a-z0-9]+)([A-Z]\w+)$/", $classname, $matches);
|
||||
|
||||
if (count($matches) < 3) {
|
||||
return false;
|
||||
} else {
|
||||
return $matches;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function autoload($classname) {
|
||||
$matches = $this->processClassname($classname);
|
||||
|
||||
if ($matches !== false) {
|
||||
$plugin_name = strtolower($matches[1]);
|
||||
$class_name = strtolower($matches[2]);
|
||||
|
||||
if ($plugin_name !== "genericobject") {
|
||||
return false;
|
||||
}
|
||||
|
||||
$filename = implode(".", [
|
||||
$class_name,
|
||||
"class",
|
||||
"php"
|
||||
]);
|
||||
|
||||
foreach ($this->paths as $path) {
|
||||
$test = $path . DIRECTORY_SEPARATOR . $filename;
|
||||
if (file_exists($test)) {
|
||||
return include_once($test);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function register() {
|
||||
spl_autoload_register([$this, 'autoload']);
|
||||
}
|
||||
}
|
||||
50
plugins/genericobject/inc/commondropdown.class.php
Normal file
50
plugins/genericobject/inc/commondropdown.class.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
class PluginGenericobjectCommonDropdown extends CommonDropdown {
|
||||
|
||||
//Get itemtype name
|
||||
static function getTypeName($nb = 0) {
|
||||
$class=get_called_class();
|
||||
return dropdown_getTypeName($class, $nb);
|
||||
}
|
||||
|
||||
static function getFormURL($full = true) {
|
||||
return Toolbox::getItemTypeFormURL( get_parent_class(get_called_class()), $full) .
|
||||
"?itemtype=".get_called_class();
|
||||
}
|
||||
static function getSearchURL($full = true) {
|
||||
return Toolbox::getItemTypeSearchURL( get_parent_class(get_called_class()), $full) .
|
||||
"?itemtype=".get_called_class();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
51
plugins/genericobject/inc/commontreedropdown.class.php
Normal file
51
plugins/genericobject/inc/commontreedropdown.class.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
class PluginGenericobjectCommonTreeDropdown extends CommonTreeDropdown {
|
||||
|
||||
//Get itemtype name
|
||||
static function getTypeName($nb = 0) {
|
||||
$class=get_called_class();
|
||||
return dropdown_getTypeName($class, $nb);
|
||||
}
|
||||
|
||||
static function getFormURL($full = true) {
|
||||
_log("PluginGenericobjectCommonTreeDropdown::getFormURL", get_parent_class(get_called_class()));
|
||||
return Toolbox::getItemTypeFormURL( get_parent_class(get_called_class()), $full) .
|
||||
"?itemtype=".get_called_class();
|
||||
}
|
||||
static function getSearchURL($full = true) {
|
||||
_log("PluginGenericobjectCommonTreeDropdown::getSearchURL", get_parent_class(get_called_class()));
|
||||
return Toolbox::getItemTypeSearchURL( get_parent_class(get_called_class()), $full) .
|
||||
"?itemtype=".get_called_class();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
538
plugins/genericobject/inc/field.class.php
Normal file
538
plugins/genericobject/inc/field.class.php
Normal file
@ -0,0 +1,538 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
class PluginGenericobjectField extends CommonDBTM {
|
||||
|
||||
/**
|
||||
*
|
||||
* Displat all fields present in DB for an itemtype
|
||||
* @param $id the itemtype's id
|
||||
*/
|
||||
public static function showObjectFieldsForm($id) {
|
||||
global $DB, $GO_BLACKLIST_FIELDS, $GO_READONLY_FIELDS, $GO_FIELDS, $CFG_GLPI;
|
||||
|
||||
$url = Toolbox::getItemTypeFormURL(__CLASS__);
|
||||
$object_type = new PluginGenericobjectType();
|
||||
$object_type->getFromDB($id);
|
||||
$itemtype = $object_type->fields['itemtype'];
|
||||
$fields_in_db = PluginGenericobjectSingletonObjectField::getInstance($itemtype);
|
||||
$used_fields = [];
|
||||
|
||||
//Reset fields definition only to keep the itemtype ones
|
||||
$GO_FIELDS = [];
|
||||
plugin_genericobject_includeCommonFields(true);
|
||||
|
||||
PluginGenericobjectType::includeLocales($object_type->fields['name']);
|
||||
PluginGenericobjectType::includeConstants($object_type->fields['name'], true);
|
||||
|
||||
self::addReadOnlyFields($object_type);
|
||||
|
||||
foreach ($GO_BLACKLIST_FIELDS as $autofield) {
|
||||
if (!in_array($autofield, $used_fields)) {
|
||||
$used_fields[$autofield] = $autofield;
|
||||
}
|
||||
}
|
||||
|
||||
echo "<div class='center'>";
|
||||
echo "<form id='fieldslist' method='POST' action='$url'>";
|
||||
echo "<table class='tab_cadre_fixe' >";
|
||||
echo "<input type='hidden' name='id' value='$id'>";
|
||||
echo "<tr class='tab_bg_1'><th colspan='7'>";
|
||||
echo __("Fields associated with the object", "genericobject") . " : ";
|
||||
echo $itemtype::getTypeName();
|
||||
echo "</th></tr>";
|
||||
|
||||
echo "<tr class='tab_bg_1'>";
|
||||
echo "<th width='10'></th>";
|
||||
echo "<th>" . __("Label", "genericobject") . "</th>";
|
||||
echo "<th>" . __("Name in DB", "genericobject") . "</th>";
|
||||
echo "<th width='10'></th>";
|
||||
echo "<th width='10'></th>";
|
||||
echo "</tr>";
|
||||
|
||||
$total = count($fields_in_db);
|
||||
$global_index = $index = 1;
|
||||
$haveCheckbox = false;
|
||||
|
||||
foreach ($fields_in_db as $field => $value) {
|
||||
$readonly = in_array($field, $GO_READONLY_FIELDS);
|
||||
$blacklist = in_array($field, $GO_BLACKLIST_FIELDS);
|
||||
|
||||
self::displayFieldDefinition($url, $itemtype, $field, $index, ($global_index==$total));
|
||||
|
||||
//All backlisted fields cannot be moved, and are listed first
|
||||
if (!$readonly) {
|
||||
$index++;
|
||||
}
|
||||
|
||||
if (!$blacklist && !$readonly) {
|
||||
$haveCheckbox = true;
|
||||
}
|
||||
|
||||
//$table = getTableNameForForeignKeyField($field);
|
||||
$used_fields[$field] = $field;
|
||||
$global_index++;
|
||||
}
|
||||
echo "</table>";
|
||||
if ($haveCheckbox) {
|
||||
echo "<table class='tab_glpi' width='950px'>";
|
||||
echo "<tr>";
|
||||
echo "<td><img src='".$CFG_GLPI["root_doc"]."/pics/arrow-left.png'
|
||||
alt=''></td>";
|
||||
echo "<td class='center' style='white-space:nowrap;'>";
|
||||
echo "<a onclick= \"if ( markCheckboxes('fieldslist') ) return false;\"
|
||||
href='#'>".__('Check all')."</a></td>";
|
||||
echo "<td>/</td>";
|
||||
echo "<td class='center' style='white-space:nowrap;'>";
|
||||
echo "<a onclick= \"if ( unMarkCheckboxes('fieldslist') ) return false;\"
|
||||
href='#'>".__('Uncheck all')."</a></td>";
|
||||
echo "<td class='left' width='80%'>";
|
||||
echo Html::submit(__("Delete permanently"), [
|
||||
'name' => 'delete',
|
||||
]);
|
||||
echo "</td></tr>";
|
||||
echo "</table>";
|
||||
}
|
||||
|
||||
$dropdownFields = self::dropdownFields("new_field", $itemtype, $used_fields);
|
||||
|
||||
if ($dropdownFields) {
|
||||
echo "<br>";
|
||||
echo "<table class='tab_cadre genericobject_fields add_new'>";
|
||||
echo "<tr class='tab_bg_1'>";
|
||||
echo "<td class='label'>" . __("Add new field", "genericobject") . "</td>";
|
||||
echo "<td align='left' class='dropdown'>";
|
||||
echo $dropdownFields;
|
||||
echo "</td>";
|
||||
echo "<td>";
|
||||
echo "<input type='submit' name='add_field' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
|
||||
echo "</tr>";
|
||||
echo "</table>";
|
||||
}
|
||||
|
||||
Html::closeForm();
|
||||
echo "</div>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to set fields as read only, when the depend on some features
|
||||
* that are enabled
|
||||
* @since 0.85+2.4.0
|
||||
*/
|
||||
static function addReadOnlyFields(PluginGenericobjectType $type) {
|
||||
global $GO_READONLY_FIELDS;
|
||||
|
||||
if ($type->canBeReserved()) {
|
||||
$GO_READONLY_FIELDS[] = 'users_id';
|
||||
$GO_READONLY_FIELDS[] = 'locations_id';
|
||||
}
|
||||
|
||||
if ($type->canUseGlobalSearch()) {
|
||||
$GO_READONLY_FIELDS[] = 'serial';
|
||||
$GO_READONLY_FIELDS[] = 'otherserial';
|
||||
$GO_READONLY_FIELDS[] = 'locations_id';
|
||||
$GO_READONLY_FIELDS[] = 'states_id';
|
||||
$GO_READONLY_FIELDS[] = 'users_id';
|
||||
$GO_READONLY_FIELDS[] = 'groups_id';
|
||||
$GO_READONLY_FIELDS[] = 'manufacturers_id';
|
||||
$GO_READONLY_FIELDS[] = 'users_id_tech';
|
||||
}
|
||||
|
||||
if ($type->canUseItemDevice()) {
|
||||
$GO_READONLY_FIELDS[] = 'locations_id';
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Get the name of the field, as defined in a constant file
|
||||
* The name may be the same, or not depending if it's an isolated dropdown or not
|
||||
*/
|
||||
static function getFieldName($field, $itemtype, $options, $remove_prefix = false) {
|
||||
global $DB;
|
||||
$field_orig = $field;
|
||||
$field_table = null;
|
||||
$input_type = isset($options['input_type'])
|
||||
? $options['input_type']
|
||||
: null;
|
||||
switch ($input_type) {
|
||||
|
||||
case 'dropdown':
|
||||
$dropdown_type = isset($options['dropdown_type'])
|
||||
? $options['dropdown_type']
|
||||
: null;
|
||||
$fk = getForeignKeyFieldForTable(getTableForItemType($itemtype));
|
||||
|
||||
if ($dropdown_type == 'isolated') {
|
||||
if (!$remove_prefix) {
|
||||
$field = preg_replace("/s_id$/", $field, $fk);
|
||||
} else {
|
||||
$fk = preg_replace("/s_id$/", "", $fk);
|
||||
$field = preg_replace("/".$fk."/", "", $field);
|
||||
}
|
||||
}
|
||||
$field_table = getTableNameForForeignKeyField($field);
|
||||
|
||||
//Prepend plugin's table prefix if this dropdown is not already handled by GLPI natively
|
||||
if (substr($field, 0, strlen('plugin_genericobject')) !== 'plugin_genericobject'
|
||||
and (
|
||||
substr($field_table, strlen('glpi_'))
|
||||
=== substr($field, 0, strlen($field) -strlen('_id'))
|
||||
)
|
||||
and !$DB->tableExists($field_table)
|
||||
) {
|
||||
if (!$remove_prefix) {
|
||||
$field = 'plugin_genericobject_' . $field;}
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Display a dropdown with all available fields for an itemtype
|
||||
* @since
|
||||
* @param $name the dropdown name
|
||||
* @param $itemtype the itemtype
|
||||
* @param $used an array which contains all fields already added
|
||||
*
|
||||
* @return the dropdown random ID
|
||||
*/
|
||||
static function dropdownFields($name, $itemtype, $used = []) {
|
||||
global $GO_FIELDS;
|
||||
|
||||
$dropdown_types = [];
|
||||
foreach ($GO_FIELDS as $field => $values) {
|
||||
$message = "";
|
||||
$field_options = [];
|
||||
$field = self::getFieldName($field, $itemtype, $values, false);
|
||||
if (!in_array($field, $used)) {
|
||||
if (!isset($dropdown_types[$field])) {
|
||||
//Global management :
|
||||
//meaning that a dropdown can be useful in all types (for example type, model, etc.)
|
||||
if (isset($values['input_type']) && $values['input_type'] == 'dropdown') {
|
||||
if (isset($values['entities_id'])) {
|
||||
$field_options[] = __("Entity")." : ".Dropdown::getYesNo($values['entities_id']);
|
||||
if ($values['entities_id']) {
|
||||
if (isset($values['is_recursive'])) {
|
||||
$field_options[] = __("Child entities")." : ".Dropdown::getYesNo($values['is_recursive']);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$field_options[] = __("Entity")." : ".Dropdown::getYesNo(0);
|
||||
}
|
||||
if (isset($values['is_tree'])) {
|
||||
$field_options[] = __("tree structure")." : ".Dropdown::getYesNo($values['is_tree']);
|
||||
} else {
|
||||
$field_options[] = __("tree structure")." : ".Dropdown::getYesNo(0);
|
||||
}
|
||||
//if (isset($values['isolated']) and $values['isolated']) {
|
||||
// $field_options[] = __("Isolated") . " : ". Dropdown::getYesNo($values['isolated']);
|
||||
//} else {
|
||||
// $field_options[] = __("Isolated") . " : ". Dropdown::getYesNo(0);
|
||||
//}
|
||||
}
|
||||
if (!empty($field_options)) {
|
||||
$message = "(".trim( implode(", ", $field_options)).")";
|
||||
}
|
||||
}
|
||||
$dropdown_types[$field] = $values['name']." ".$message;
|
||||
}
|
||||
}
|
||||
|
||||
// Don't show dropdown empty
|
||||
if (empty($dropdown_types)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
ksort($dropdown_types);
|
||||
return Dropdown::showFromArray($name, $dropdown_types, ['display' => false]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Get field's options defined in constant files.
|
||||
* If this field has not been defined, it means that this field has been defined globally and
|
||||
* must be dynamically created.
|
||||
*
|
||||
* @param $field the current field
|
||||
* @param $itemtype the itemtype
|
||||
* @return an array which contains the full field definition
|
||||
*/
|
||||
static function getFieldOptions($field, $itemtype = "") {
|
||||
global $GO_FIELDS;
|
||||
|
||||
$options = [];
|
||||
$cleaned_field = preg_replace("/^plugin_genericobject_/", '', $field);
|
||||
if (!isset($GO_FIELDS[$cleaned_field]) && !empty($itemtype)) {
|
||||
// This field has been dynamically defined because it's an isolated dropdown
|
||||
$tmpfield = self::getFieldName(
|
||||
$field, $itemtype,
|
||||
[
|
||||
'dropdown_type' => 'isolated',
|
||||
'input_type' => 'dropdown'
|
||||
],
|
||||
true
|
||||
);
|
||||
$options = $GO_FIELDS[$tmpfield];
|
||||
$options['realname'] = $tmpfield;
|
||||
} else if (isset($GO_FIELDS[$cleaned_field])) {
|
||||
$options = $GO_FIELDS[$cleaned_field];
|
||||
$options['realname'] = $cleaned_field;
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
public static function displayFieldDefinition($target, $itemtype, $field, $index, $last = false) {
|
||||
global $GO_FIELDS, $CFG_GLPI, $GO_BLACKLIST_FIELDS, $GO_READONLY_FIELDS;
|
||||
|
||||
$readonly = in_array($field, $GO_READONLY_FIELDS);
|
||||
$blacklist = in_array($field, $GO_BLACKLIST_FIELDS);
|
||||
$options = self::getFieldOptions($field, $itemtype);
|
||||
|
||||
echo "<tr class='tab_bg_".(($index%2)+1)."' align='center'>";
|
||||
echo "<td width='10'>";
|
||||
if (!$blacklist && !$readonly) {
|
||||
echo "<input type='checkbox' name='fields[" .$field. "]' value='1'>";
|
||||
} else {
|
||||
echo "<i class='fa fa-lock' title='".__("Read-only field", 'genericobject')."'>";
|
||||
}
|
||||
echo "</td>";
|
||||
echo "<td>" . __($options['name'], 'genericobject') . "</td>";
|
||||
echo "<td>" . $field . "</td>";
|
||||
|
||||
echo "<td width='10'>";
|
||||
if ((!$blacklist || $readonly) && $index > 1) {
|
||||
Html::showSimpleForm($target, $CFG_GLPI["root_doc"] . "/pics/deplier_up.png", 'up',
|
||||
['field' => $field, 'action' => 'up', 'itemtype' => $itemtype],
|
||||
$CFG_GLPI["root_doc"] . "/pics/deplier_up.png");
|
||||
}
|
||||
echo "</td>";
|
||||
|
||||
echo "<td width='10'>";
|
||||
if ((!$blacklist || $readonly) && !$last) {
|
||||
Html::showSimpleForm($target, $CFG_GLPI["root_doc"] . "/pics/deplier_down.png", 'down',
|
||||
['field' => $field, 'action' => 'down', 'itemtype' => $itemtype],
|
||||
$CFG_GLPI["root_doc"] . "/pics/deplier_down.png");
|
||||
}
|
||||
echo "</td>";
|
||||
|
||||
echo "</tr>";
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new field in DB
|
||||
* @param table the table
|
||||
* @param field the field to delete
|
||||
* @return nothing
|
||||
*/
|
||||
public static function addNewField($table, $field, $after = false) {
|
||||
global $DB;
|
||||
|
||||
_log("add", $field, "from", $table);
|
||||
$itemtype = getItemTypeForTable($table);
|
||||
if (!$DB->fieldExists($table, $field, false)) {
|
||||
$options = self::getFieldOptions($field, $itemtype);
|
||||
$query = "ALTER TABLE `$table` ADD `$field` ";
|
||||
switch ($options['input_type']) {
|
||||
case 'dropdown_yesno' :
|
||||
case 'dropdown_global' :
|
||||
case 'bool' :
|
||||
$query .= "TINYINT (1) NOT NULL DEFAULT '0'";
|
||||
break;
|
||||
case 'emptyspace' :
|
||||
case 'text' :
|
||||
$query .= "VARCHAR ( 255 ) collate utf8_unicode_ci NOT NULL DEFAULT ''";
|
||||
break;
|
||||
case 'multitext' :
|
||||
$query .= "TEXT NULL";
|
||||
break;
|
||||
case 'dropdown' :
|
||||
case 'integer' :
|
||||
$query .= "INT ( 11 ) NOT NULL DEFAULT '0'";
|
||||
break;
|
||||
case 'date':
|
||||
$query.="DATE DEFAULT NULL";
|
||||
break;
|
||||
case 'datetime':
|
||||
$query.="TIMESTAMP NULL DEFAULT NULL";
|
||||
break;
|
||||
case 'float' :
|
||||
$query .= "FLOAT NOT NULL DEFAULT '0'";
|
||||
break;
|
||||
case 'decimal' :
|
||||
$query .= "DECIMAL(20,4) NOT NULL DEFAULT '0.0000'";
|
||||
break;
|
||||
}
|
||||
if ($after) {
|
||||
$query.=" AFTER `$after`";
|
||||
}
|
||||
$DB->query($query);
|
||||
|
||||
//Reload list of fields for this itemtype in the singleton
|
||||
|
||||
$recursive = $entity_assign = $tree = false;
|
||||
|
||||
$table = getTableNameForForeignKeyField($field);
|
||||
|
||||
if ($table != '' && !$DB->tableExists($table)) {
|
||||
//Cannot use standard methods because class doesn't exists yet !
|
||||
$name = str_replace("glpi_plugin_genericobject_", "", $table);
|
||||
$name = getSingular($name);
|
||||
|
||||
$options['linked_itemtype'] = $itemtype;
|
||||
|
||||
PluginGenericobjectType::addNewDropdown(
|
||||
$name, 'PluginGenericobject'.ucfirst($name), $options
|
||||
);
|
||||
}
|
||||
// Invalidate menu data in current session
|
||||
unset($_SESSION['glpimenu']);
|
||||
|
||||
PluginGenericobjectSingletonObjectField::getInstance($itemtype, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a field in DB
|
||||
* @param table the table
|
||||
* @param field the field to delete
|
||||
* @return nothing
|
||||
*/
|
||||
static function deleteField($table, $field) {
|
||||
global $DB;
|
||||
|
||||
//Remove field from displaypreferences
|
||||
self::deleteDisplayPreferences($table, $field);
|
||||
|
||||
//If field exists, drop it !
|
||||
if ($DB->fieldExists($table, $field)) {
|
||||
$DB->query("ALTER TABLE `$table` DROP `$field`");
|
||||
}
|
||||
|
||||
$table = getTableNameForForeignKeyField($field);
|
||||
//If dropdown is managed by the plugin
|
||||
if ($table != '' && preg_match('/plugin_genericobject_(.*)/', $table, $results)) {
|
||||
//Delete dropdown table
|
||||
$query = "DROP TABLE `$table`";
|
||||
$DB->query($query);
|
||||
//Delete dropdown files & class
|
||||
$name = getSingular($results[1]);
|
||||
PluginGenericobjectType::deleteClassFile($name);
|
||||
PluginGenericobjectType::deleteFormFile($name);
|
||||
PluginGenericobjectType::deletesearchFile($name);
|
||||
}
|
||||
}
|
||||
|
||||
static function deleteDisplayPreferences($table, $field) {
|
||||
|
||||
$pref = new DisplayPreference();
|
||||
$itemtype = getItemTypeForTable($table);
|
||||
$searchopt = Search::getCleanedOptions($itemtype);
|
||||
foreach ($searchopt as $num => $option) {
|
||||
if ((isset($option['field']) && ($option['field'] == $field))
|
||||
|| (isset($option['field']) && $option['linkfield'] == $field)) {
|
||||
$pref->deleteByCriteria([
|
||||
'itemtype' => $itemtype,
|
||||
'num' => $num
|
||||
]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change field order in DB
|
||||
* @params an array which contains the itemtype, the field to move and the action (up/down)
|
||||
* @return nothing
|
||||
*/
|
||||
static function changeFieldOrder($params = []) {
|
||||
global $DB;
|
||||
$itemtype = $params['itemtype'];
|
||||
$field = $params['field'];
|
||||
$table = getTableForItemType($itemtype);
|
||||
$fields = PluginGenericobjectSingletonObjectField::getInstance($params['itemtype']);
|
||||
|
||||
//If action is down, reverse array first
|
||||
if ($params['action'] == 'down') {
|
||||
$fields = array_reverse($fields);
|
||||
}
|
||||
|
||||
//Get array keys
|
||||
$keys = array_keys($fields);
|
||||
//Index represents current position of $field
|
||||
$index = 0;
|
||||
foreach ($keys as $id => $key) {
|
||||
if ($key == $field) {
|
||||
$index = $id;
|
||||
}
|
||||
}
|
||||
//Get 2 positions before and move field
|
||||
if ($params['action'] == 'down') {
|
||||
$previous = $index - 1;
|
||||
} else {
|
||||
$previous = $index - 2;
|
||||
}
|
||||
|
||||
if (isset($keys[$previous])) {
|
||||
$parent = $fields[$keys[$previous]];
|
||||
$query = "ALTER TABLE `$table` MODIFY `$field` ".$fields[$field]['Type'];
|
||||
$query .= " AFTER `".$fields[$keys[$previous]]['Field']."`";
|
||||
$DB->query($query) or die ($DB->error());
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkNecessaryFieldsDelete($itemtype, $field) {
|
||||
$type = new PluginGenericobjectType();
|
||||
$type->getFromDBByType($itemtype);
|
||||
|
||||
if ($type->canUseNetworkPorts() && 'locations_id' == $field) {
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
if ($type->fields['use_direct_connections']) {
|
||||
foreach(['users_id','groups_id',' states_id','locations_id'] as $tmp_field) {
|
||||
if ($tmp_field == $field) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
return true;
|
||||
}
|
||||
|
||||
static function install(Migration $migration) {
|
||||
}
|
||||
|
||||
static function uninstall() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
130
plugins/genericobject/inc/functions.php
Normal file
130
plugins/genericobject/inc/functions.php
Normal file
@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* return the name of a dropdown type
|
||||
* This shared by the following classes :
|
||||
* - PluginGenericobjectCommonDropdown
|
||||
* - PluginGenericobjectCommonTreeDropdown
|
||||
*/
|
||||
function dropdown_getTypeName($class, $nb = 0) {
|
||||
global $GO_FIELDS;
|
||||
$fk = getForeignKeyFieldForTable(getTableForItemType($class));
|
||||
$instance = new $class();
|
||||
$options = PluginGenericobjectField::getFieldOptions($fk, $instance->linked_itemtype);
|
||||
$dropdown_type = isset($options['dropdown_type'])
|
||||
? $options['dropdown_type']
|
||||
: null;
|
||||
$label = $options['name'] ?? "no-name";
|
||||
if (!is_null($dropdown_type) and $dropdown_type==='isolated') {
|
||||
$linked_itemtype_object = new $instance->linked_itemtype();
|
||||
$label .= " (" . __($linked_itemtype_object::getTypeName(), 'genericobject') . ")";
|
||||
}
|
||||
if ($label != '') {
|
||||
return $label;
|
||||
} else {
|
||||
return $class;
|
||||
}
|
||||
}
|
||||
global $LOG_FILTER;
|
||||
$LOG_FILTER = [];
|
||||
/*
|
||||
* a simple logger function
|
||||
* You can disable logging by using the global $LOG_FILTER
|
||||
* in setup.php after including this file
|
||||
*/
|
||||
function _log() {
|
||||
global $LOG_FILTER;
|
||||
$trace = debug_backtrace();
|
||||
if (count($trace)>0) {
|
||||
$glpi_root = str_replace( "\\", "/", GLPI_ROOT );
|
||||
$trace_file = str_replace( "\\", "/", $trace[0]['file'] );
|
||||
$filename = preg_replace("|^".$glpi_root."/".Plugin::getPhpDir('genericobject', false)."/|", "", $trace_file);
|
||||
}
|
||||
if (count($trace) > 1) {
|
||||
$caller = $trace[1];
|
||||
} else {
|
||||
$caller = null;
|
||||
}
|
||||
$msg = _format_trace($trace, func_get_args());
|
||||
$msg .= "\n";
|
||||
$show_log = false;
|
||||
if (!is_null($caller) and
|
||||
isset($caller['class']) and
|
||||
in_array($caller['class'], $LOG_FILTER)
|
||||
) {
|
||||
$callee = array_shift($trace);
|
||||
$show_log = true;
|
||||
}
|
||||
if (in_array($filename, $LOG_FILTER)) {
|
||||
$show_log = true;
|
||||
}
|
||||
if ($show_log) {
|
||||
call_user_func_array("Toolbox::logInFile", ['generic-object', $msg, true]);
|
||||
}
|
||||
}
|
||||
|
||||
function _format_trace($bt, $args) {
|
||||
static $tps = 0;
|
||||
$msg = "";
|
||||
$msg = "From \n";
|
||||
if (count($bt) > 0) {
|
||||
foreach (array_reverse($bt) as $idx => $trace) {
|
||||
$msg .= sprintf(" [%d] ", $idx);
|
||||
if (isset($trace['class'])) {
|
||||
$msg .= $trace['class'].'::';
|
||||
}
|
||||
$msg .= $trace['function'].'()';
|
||||
if (isset($trace['file'])) {
|
||||
$msg .= ' called in '. $trace['file'] . ', line ' . $trace['line'];
|
||||
}
|
||||
$msg .= "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ($tps && function_exists('memory_get_usage')) {
|
||||
$msg .= ' ('.number_format(microtime(true)-$tps, 3).'", '.
|
||||
number_format(memory_get_usage()/1024/1024, 2).'Mio)';
|
||||
}
|
||||
$msg .= "\n ";
|
||||
foreach ($args as $arg) {
|
||||
if (is_array($arg) || is_object($arg)) {
|
||||
$msg .= " ".str_replace("\n", "\n ", print_r($arg, true));
|
||||
} else if (is_null($arg)) {
|
||||
$msg .= 'NULL ';
|
||||
} else if (is_bool($arg)) {
|
||||
$msg .= ($arg ? 'true' : 'false').' ';
|
||||
} else {
|
||||
$msg .= $arg . ' ';
|
||||
}
|
||||
}
|
||||
$msg .= "\n";
|
||||
return $msg;
|
||||
}
|
||||
1257
plugins/genericobject/inc/object.class.php
Normal file
1257
plugins/genericobject/inc/object.class.php
Normal file
File diff suppressed because it is too large
Load Diff
128
plugins/genericobject/inc/object_item.class.php
Normal file
128
plugins/genericobject/inc/object_item.class.php
Normal file
@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
class PluginGenericobjectObject_Item extends CommonDBChild {
|
||||
|
||||
public $dohistory = true;
|
||||
|
||||
// From CommonDBRelation
|
||||
static public $itemtype_1 = "PluginGenericobjectObject";
|
||||
static public $items_id_1 = 'plugin_genericobject_objects_id';
|
||||
|
||||
static public $itemtype_2 = 'itemtype';
|
||||
static public $items_id_2 = 'items_id';
|
||||
|
||||
//Get itemtype name
|
||||
static function getTypeName($nb = 0) {
|
||||
global $LANG;
|
||||
$class = get_called_class();
|
||||
//Datainjection : Don't understand why I need this trick : need to be investigated !
|
||||
if (preg_match("/Injection$/i", $class)) {
|
||||
$class = str_replace("Injection", "", $class);
|
||||
}
|
||||
$item = new $class();
|
||||
//Itemtype name can be contained in a specific locale field : try to load it
|
||||
PluginGenericobjectType::includeLocales($item->objecttype->fields['name']);
|
||||
if (isset($LANG['genericobject'][$class][0])) {
|
||||
return $LANG['genericobject'][$class][0];
|
||||
} else {
|
||||
return $item->objecttype->fields['name'];
|
||||
}
|
||||
}
|
||||
|
||||
static function canView() {
|
||||
return Session::haveRight(self::$itemtype_1, READ);
|
||||
}
|
||||
|
||||
static function canCreate() {
|
||||
return Session::haveRight(self::$itemtype_1, CREATE);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @since 2.2.0
|
||||
* @param CommonDBTM $item
|
||||
*/
|
||||
static function showItemsForSource(CommonDBTM $item) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @since 2.2.0
|
||||
* @param CommonDBTM $item
|
||||
*/
|
||||
static function showItemsForTarget(CommonDBTM $item) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @since 2.2.0
|
||||
*/
|
||||
static function registerType() {
|
||||
Plugin::registerClass(get_called_class(), ['addtabon' => self::getLinkedItemTypes()]);
|
||||
}
|
||||
|
||||
static function getLinkedItemTypes() {
|
||||
$source_itemtype = self::getItemType1();
|
||||
$source_item = new $source_itemtype;
|
||||
return $source_item->getLinkedItemTypesAsArray();
|
||||
}
|
||||
|
||||
static function getItemType1() {
|
||||
$classname = get_called_class();
|
||||
return $classname::$itemtype_1;
|
||||
}
|
||||
|
||||
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
|
||||
if (!$withtemplate) {
|
||||
$itemtypes = self::getLinkedItemTypes();
|
||||
if (in_array(get_class($item), $itemtypes) || get_class($item) == self::getItemType1()) {
|
||||
return [1 => __("Objects management", "genericobject")];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
|
||||
$itemtypes = self::getLinkedItemTypes();
|
||||
if (get_class($item) == self::getItemType1()) {
|
||||
self::showItemsForSource($item);
|
||||
} else if (in_array(get_class($item), $itemtypes)) {
|
||||
self::showItemsForTarget($item);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
450
plugins/genericobject/inc/profile.class.php
Normal file
450
plugins/genericobject/inc/profile.class.php
Normal file
@ -0,0 +1,450 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
class PluginGenericobjectProfile extends Profile {
|
||||
|
||||
/* if profile deleted */
|
||||
function cleanProfiles($id) {
|
||||
$this->deleteByCriteria(['id' => $id]);
|
||||
}
|
||||
|
||||
function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
|
||||
|
||||
switch ($item->getType()) {
|
||||
case 'Profile':
|
||||
return self::createTabEntry(__('Objects management', 'genericobject'));
|
||||
break;
|
||||
case 'PluginGenericobjectType':
|
||||
return self::createTabEntry(_n('Profile', 'Profiles', 2));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
|
||||
switch ($item->getType()) {
|
||||
case 'Profile':
|
||||
$profile = new self();
|
||||
$profile->showForm($item->getID());
|
||||
break;
|
||||
case 'PluginGenericobjectType':
|
||||
_log($item);
|
||||
self::showForItemtype($item);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static function showForItemtype($type) {
|
||||
global $DB;
|
||||
|
||||
if (!Session::haveRight("profile", READ)) {
|
||||
return false;
|
||||
}
|
||||
self::installRights();
|
||||
$canedit = Session::haveRight("profile", UPDATE);
|
||||
|
||||
echo "<form action='" . self::getFormUrl() . "' method='post'>";
|
||||
echo "<table class='tab_cadre_fixe'>";
|
||||
$itemtype = $type->fields['itemtype'];
|
||||
echo "<tr><th colspan='2' align='center'><strong>";
|
||||
echo __("Rights assignment").": ";
|
||||
echo $itemtype::getTypeName();
|
||||
echo "</strong></th></tr>";
|
||||
|
||||
echo "<tr><td class='genericobject_type_profiles'>";
|
||||
foreach (getAllDataFromTable(getTableForItemtype("Profile")) as $profile) {
|
||||
$prof = new Profile();
|
||||
$prof->getFromDB($profile['id']);
|
||||
$rights = [
|
||||
[
|
||||
'label' => $profile['name'],
|
||||
'itemtype' => $itemtype,
|
||||
'field' => self::getProfileNameForItemtype($itemtype),
|
||||
'html_field' => "profile_" . $profile['id'],
|
||||
]
|
||||
];
|
||||
$prof->displayRightsChoiceMatrix(
|
||||
$rights
|
||||
);
|
||||
}
|
||||
|
||||
echo "</td></tr>";
|
||||
echo "<input type='hidden' name='itemtype' value='".$itemtype."'>";
|
||||
|
||||
if ($canedit) {
|
||||
echo "<tr class='tab_bg_1'>";
|
||||
echo "<td align='center' colspan='2'>";
|
||||
echo "<input type='submit' name='update_all_rights' value=\"" .
|
||||
_sx('button', 'Post') . "\" class='submit'>";
|
||||
echo "</td></tr>";
|
||||
}
|
||||
|
||||
echo "</table>";
|
||||
Html::closeForm();
|
||||
}
|
||||
|
||||
static function getProfileNameForItemtype($itemtype) {
|
||||
return preg_replace("/^glpi_/", "", getTableForItemType($itemtype));
|
||||
}
|
||||
|
||||
|
||||
/* profiles modification */
|
||||
function showForm($profiles_id, $options = []) {
|
||||
if (!Session::haveRight("profile", READ)) {
|
||||
return false;
|
||||
}
|
||||
$canedit = Session::haveRight("profile", UPDATE);
|
||||
//if ($id) {
|
||||
// $this->getProfilesFromDB($id);
|
||||
//}
|
||||
|
||||
//Ensure rights are defined in database
|
||||
self::installRights();
|
||||
|
||||
$profile = new Profile();
|
||||
$profile->getFromDB($profiles_id);
|
||||
|
||||
echo "<form action='" . Profile::getFormUrl() . "' method='post'>";
|
||||
echo "<table class='tab_cadre_fixe'>";
|
||||
|
||||
$general_rights = self::getGeneralRights();
|
||||
|
||||
$profile->displayRightsChoiceMatrix(
|
||||
$general_rights,
|
||||
[
|
||||
'canedit' => $canedit,
|
||||
'default_class' => 'tab_bg_2',
|
||||
'title' => __('General', 'genericobject')
|
||||
]
|
||||
);
|
||||
|
||||
$types_rights = self::getTypesRights();
|
||||
|
||||
$title = __('Objects', 'genericobject');
|
||||
if (count($types_rights) == 0) {
|
||||
$title .= ' ' . __("(No types defined yet)", "genericobject");
|
||||
}
|
||||
|
||||
$profile->displayRightsChoiceMatrix(
|
||||
$types_rights,
|
||||
[
|
||||
'canedit' => $canedit,
|
||||
'default_class' => 'tab_bg_2',
|
||||
'title' => $title
|
||||
]
|
||||
);
|
||||
$profile->showLegend();
|
||||
if ($canedit) {
|
||||
echo "<div class='center'>";
|
||||
echo Html::hidden('id', ['value' => $profiles_id]);
|
||||
echo Html::submit(_sx('button', 'Save'), ['name' => 'update']);
|
||||
echo "</div>\n";
|
||||
Html::closeForm();
|
||||
}
|
||||
echo "</div>";
|
||||
|
||||
}
|
||||
|
||||
static function getProfileforItemtype($profiles_id, $itemtype) {
|
||||
$rights = ProfileRight::getProfileRights($profiles_id);
|
||||
$itemtype_rightname = self::getProfileNameForItemtype($itemtype);
|
||||
return isset($rights[$itemtype_rightname]) ? $rights[$itemtype_rightname] : 0;
|
||||
}
|
||||
|
||||
function getProfilesFromDB($id, $config = true) {
|
||||
global $DB;
|
||||
$prof_datas = [];
|
||||
foreach (getAllDataFromTable(getTableForItemType(__CLASS__),
|
||||
['profiles_id' => $id]) as $prof) {
|
||||
if ($prof['right'] != "" || $config) {
|
||||
$prof_datas[$prof['itemtype']] = $prof['right'];
|
||||
$prof_datas[$prof['itemtype'].'_open_ticket'] = $prof['open_ticket'];
|
||||
$prof_datas['id'] = $prof['id'];
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($prof_datas) && !$config) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$prof_datas['profiles_id'] = $id;
|
||||
$this->fields = $prof_datas;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function saveProfileToDB($params) {
|
||||
global $DB;
|
||||
|
||||
$types = PluginGenericobjectType::getTypes(true);
|
||||
if (!empty ($types)) {
|
||||
foreach ($types as $tmp => $profile) {
|
||||
$query = "UPDATE `".getTableForItemType(__CLASS__)."` " .
|
||||
"SET ";
|
||||
|
||||
if (isset($params[$profile['itemtype']]) && $params[$profile['itemtype']] == 'NULL') {
|
||||
$query.="`right`='' ";
|
||||
} else {
|
||||
if (isset($params[$profile['itemtype']])) {
|
||||
$query.="`right`='".$params[$profile['itemtype']]."'";
|
||||
} else {
|
||||
$query.="`right`=''";
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($params[$profile['itemtype'].'_open_ticket'])) {
|
||||
$query.=", `open_ticket`='".$params[$profile['itemtype'].'_open_ticket']."' ";
|
||||
}
|
||||
|
||||
$query.="WHERE `profiles_id`='".$params['profiles_id']."' " .
|
||||
"AND `itemtype`='".$profile['itemtype']."'";
|
||||
$DB->query($query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create rights for the current profile
|
||||
* @param profileID the profile ID
|
||||
* @return nothing
|
||||
*/
|
||||
public static function createFirstAccess() {
|
||||
if (!self::profileExists($_SESSION["glpiactiveprofile"]["id"], 'PluginGenericobjectType')) {
|
||||
self::createAccess($_SESSION["glpiactiveprofile"]["id"], "PluginGenericobjectType", true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if rights for a profile still exists
|
||||
* @param profiles_id the profile ID
|
||||
* @param itemtype name of the type
|
||||
* @return true if exists, no if not
|
||||
*/
|
||||
public static function profileExists($profiles_id, $itemtype = false) {
|
||||
$profile = new Profile();
|
||||
$profile->getFromDB($profiles_id);
|
||||
$rights = ProfileRight::getProfileRights($profiles_id);
|
||||
$itemtype_rightname = self::getProfileNameForItemtype($itemtype);
|
||||
if ($itemtype) {
|
||||
_log(
|
||||
"get rights on itemtype ".$itemtype." for profile ".$profile->fields['name'], ':',
|
||||
isset($rights[$itemtype_rightname]) ? $rights[$itemtype_rightname] : "NONE"
|
||||
);
|
||||
return (isset($rights[self::getProfileNameForItemtype($itemtype)]));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create rights for the profile if it doesn't exists
|
||||
* @param profileID the profile ID
|
||||
* @return nothing
|
||||
*/
|
||||
public static function createAccess($profiles_id, $itemtype, $first = false) {
|
||||
|
||||
$rights = getAllDataFromTable('glpi_profiles');
|
||||
$profile_right = new ProfileRight();
|
||||
$itemtype_rightname = self::getProfileNameForItemtype($itemtype);
|
||||
|
||||
foreach ($rights as $right) {
|
||||
if ($right['id'] == $profiles_id) {
|
||||
$r = ALLSTANDARDRIGHT | READNOTE | UPDATENOTE;
|
||||
} else {
|
||||
$r = 0;
|
||||
}
|
||||
$profile_right->updateProfileRights($right['id'], [$itemtype_rightname => $r]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getGeneralRights() {
|
||||
return [[
|
||||
'itemtype' => 'PluginGenericobjectType',
|
||||
'label' => __("Type of objects", "genericobject"),
|
||||
'field' => self::getProfileNameForItemtype('PluginGenericobjectType'),
|
||||
]];
|
||||
}
|
||||
|
||||
public static function getTypesRights() {
|
||||
$rights = [];
|
||||
|
||||
include_once(GENERICOBJECT_DIR."/inc/type.class.php");
|
||||
|
||||
$types = PluginGenericobjectType::getTypes(true);
|
||||
if (count( $types) > 0) {
|
||||
foreach ($types as $_ => $type) {
|
||||
$itemtype = $type['itemtype'];
|
||||
|
||||
if (!class_exists($itemtype)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$field = self::getProfileNameForItemtype($itemtype);
|
||||
$objecttype = new PluginGenericobjectType($itemtype);
|
||||
$rights[] = [
|
||||
'itemtype' => $itemtype,
|
||||
'label' => $itemtype::getTypeName(),
|
||||
'field' => self::getProfileNameForItemtype($itemtype)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $rights;
|
||||
}
|
||||
|
||||
public static function installRights($first = false) {
|
||||
$missing_rights = [];
|
||||
$installed_rights = ProfileRight::getAllPossibleRights();
|
||||
$right_names = [];
|
||||
|
||||
// Add common plugin's rights
|
||||
$right_names[] = self::getProfileNameForItemtype('PluginGenericobjectType');
|
||||
|
||||
// Add types' rights
|
||||
$types = PluginGenericobjectType::getTypes(true);
|
||||
foreach ($types as $_ => $type) {
|
||||
$itemtype = $type['itemtype'];
|
||||
$right_names[] = self::getProfileNameForItemtype($itemtype);
|
||||
}
|
||||
|
||||
// Check for already defined rights
|
||||
foreach ($right_names as $right_name) {
|
||||
_log($right_name, isset($installed_rights[$right_name]));
|
||||
if (!isset($installed_rights[$right_name])) {
|
||||
$missing_rights[] = $right_name;
|
||||
}
|
||||
}
|
||||
|
||||
//Install missing rights in profile and update the object
|
||||
if (count($missing_rights) > 0) {
|
||||
ProfileRight::addProfileRights($missing_rights);
|
||||
self::changeProfile();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete type from the rights
|
||||
* @param name the name of the type
|
||||
* @return nothing
|
||||
*/
|
||||
public static function deleteTypeFromProfile($itemtype) {
|
||||
$rights = [self::getProfileNameForItemtype($itemtype)];
|
||||
ProfileRight::deleteProfileRights($rights);
|
||||
}
|
||||
|
||||
public static function changeProfile() {
|
||||
$general_rights = self::getGeneralRights();
|
||||
$type_rights = self::getTypesRights();
|
||||
$db_rights = ProfileRight::getProfileRights($_SESSION['glpiactiveprofile']['id']);
|
||||
$rights = array_merge($general_rights, $type_rights);
|
||||
|
||||
foreach ($rights as $right) {
|
||||
$str_right = $right['field'];
|
||||
if (preg_match("/plugin_genericobject_/", $str_right)) {
|
||||
unset($_SESSION['glpiactiveprofile'][$str_right]);
|
||||
if (!empty($db_rights) && isset($db_rights[$str_right])) {
|
||||
$_SESSION['glpiactiveprofile'][$str_right] = $db_rights[$str_right];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static function install(Migration $migration) {
|
||||
global $DB;
|
||||
|
||||
$profileRight = new ProfileRight();
|
||||
$profile = new Profile();
|
||||
|
||||
//Update needed
|
||||
if ($DB->tableExists('glpi_plugin_genericobject_profiles')) {
|
||||
foreach (getAllDataFromTable('glpi_plugin_genericobject_profiles') as $right) {
|
||||
if (preg_match("/PluginGenericobject(.*)/", $right['itemtype'], $results)) {
|
||||
$newrightname = 'plugin_genericobject_'.strtolower($results[1]).'s';
|
||||
if (!countElementsInTable('glpi_profilerights',
|
||||
['profiles_id' => $right['profiles_id'],
|
||||
'name' => $newrightname])) {
|
||||
switch ($right['right']) {
|
||||
case null:
|
||||
case '':
|
||||
$rightvalue = 0;
|
||||
break;
|
||||
case 'r':
|
||||
$rightvalue = READ;
|
||||
break;
|
||||
case 'w':
|
||||
$rightvalue = ALLSTANDARDRIGHT;
|
||||
break;
|
||||
}
|
||||
|
||||
$profileRight->add(['profiles_id' => $right['profiles_id'],
|
||||
'name' => $newrightname,
|
||||
'rights' => $rightvalue]);
|
||||
|
||||
if (!countElementsInTable('glpi_profilerights',
|
||||
['profiles_id' => $right['profiles_id'],
|
||||
'name' => 'plugin_genericobject_types'])) {
|
||||
$profileRight->add(['profiles_id' => $right['profiles_id'],
|
||||
'name' => 'plugin_genericobject_types',
|
||||
'rights' => 23]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($right['open_ticket']) {
|
||||
$profile->getFromDB($right['profiles_id']);
|
||||
$helpdesk_item_types = json_decode($profile->fields['helpdesk_item_type'], true);
|
||||
if (is_array($helpdesk_item_types)) {
|
||||
if (!in_array($right['itemtype'], $helpdesk_item_types)) {
|
||||
$helpdesk_item_types[] = $right['itemtype'];
|
||||
}
|
||||
} else {
|
||||
$helpdesk_item_types = [$right['itemtype']];
|
||||
}
|
||||
|
||||
$tmp['id'] = $profile->getID();
|
||||
$tmp['helpdesk_item_type'] = json_encode($helpdesk_item_types);
|
||||
$profile->update($tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
//$migration->dropTable('glpi_plugin_genericobject_profiles');
|
||||
}
|
||||
if (!countElementsInTable('glpi_profilerights', ['name' => ['LIKE', '%genericobject%']])) {
|
||||
self::createFirstAccess();
|
||||
}
|
||||
}
|
||||
|
||||
static function uninstall() {
|
||||
global $DB;
|
||||
$query = "DELETE FROM `glpi_profilerights`
|
||||
WHERE `name` LIKE '%plugin_genericobject%'";
|
||||
$DB->query($query) or die($DB->error());
|
||||
}
|
||||
}
|
||||
60
plugins/genericobject/inc/singletonobjectfield.class.php
Normal file
60
plugins/genericobject/inc/singletonobjectfield.class.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Original Author of file: Walid Nouh
|
||||
// Purpose of file:
|
||||
// ----------------------------------------------------------------------
|
||||
if (!defined('GLPI_ROOT')) {
|
||||
die("Sorry. You can't access directly to this file");
|
||||
}
|
||||
|
||||
class PluginGenericobjectSingletonObjectField {
|
||||
/// Items list
|
||||
static $_dbfields = [];
|
||||
|
||||
/**
|
||||
* Singleton to store DB fields definition
|
||||
*
|
||||
* @since 2.1.0
|
||||
* @param itemtype itemtype to query
|
||||
* @param reload reload db fields configuration from DB
|
||||
*
|
||||
* @return an array which contains DB fields definition
|
||||
*/
|
||||
public static function getInstance($itemtype, $reload = false) {
|
||||
global $DB;
|
||||
if (!isset(self::$_dbfields[$itemtype]) || $reload) {
|
||||
self::$_dbfields[$itemtype] = $DB->listFields(getTableForItemType($itemtype));
|
||||
}
|
||||
return self::$_dbfields[$itemtype];
|
||||
}
|
||||
}
|
||||
|
||||
2417
plugins/genericobject/inc/type.class.php
Normal file
2417
plugins/genericobject/inc/type.class.php
Normal file
File diff suppressed because it is too large
Load Diff
101
plugins/genericobject/inc/typefamily.class.php
Normal file
101
plugins/genericobject/inc/typefamily.class.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* -------------------------------------------------------------------------
|
||||
* GenericObject plugin for GLPI
|
||||
* -------------------------------------------------------------------------
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This file is part of GenericObject.
|
||||
*
|
||||
* GenericObject is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GenericObject is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with GenericObject. If not, see <http://www.gnu.org/licenses/>.
|
||||
* -------------------------------------------------------------------------
|
||||
* @copyright Copyright (C) 2009-2022 by GenericObject plugin team.
|
||||
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* @link https://github.com/pluginsGLPI/genericobject
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
if (!defined('GLPI_ROOT')) {
|
||||
die("Sorry. You can't access directly to this file");
|
||||
}
|
||||
|
||||
class PluginGenericobjectTypeFamily extends CommonDropdown {
|
||||
var $can_be_translated = true;
|
||||
|
||||
static function getTypeName($nb = 0) {
|
||||
return __('Family of type of objects', 'genericobject');
|
||||
}
|
||||
|
||||
static function install(Migration $migration) {
|
||||
global $DB;
|
||||
|
||||
$table = getTableForItemType(__CLASS__);
|
||||
if (!$DB->tableExists($table)) {
|
||||
$query = "CREATE TABLE `$table` (
|
||||
`id` INT( 11 ) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) collate utf8_unicode_ci default NULL,
|
||||
`comment` text NULL,
|
||||
`date_mod` TIMESTAMP NULL DEFAULT NULL,
|
||||
`date_creation` TIMESTAMP NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `date_mod` (`date_mod`),
|
||||
KEY `date_creation` (`date_creation`)
|
||||
) ENGINE = InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
|
||||
$DB->query($query) or die($DB->error());
|
||||
}
|
||||
}
|
||||
|
||||
static function uninstall() {
|
||||
global $DB;
|
||||
|
||||
$table = getTableForItemType(__CLASS__);
|
||||
if ($DB->tableExists($table)) {
|
||||
$query = "DROP TABLE IF EXISTS `$table`";
|
||||
$DB->query($query) or die($DB->error());
|
||||
}
|
||||
}
|
||||
|
||||
static function getFamilies() {
|
||||
global $DB;
|
||||
|
||||
$query = "SELECT f.id as id, f.name as name, t.itemtype as itemtype
|
||||
FROM glpi_plugin_genericobject_typefamilies as f
|
||||
LEFT JOIN glpi_plugin_genericobject_types AS t
|
||||
ON (f.id = t.plugin_genericobject_typefamilies_id)
|
||||
WHERE t.id IN (SELECT DISTINCT `id`
|
||||
FROM glpi_plugin_genericobject_types
|
||||
WHERE is_active=1)";
|
||||
$families = [];
|
||||
foreach ($DB->request($query) as $fam) {
|
||||
$itemtype = $fam['itemtype'];
|
||||
if ($itemtype::canCreate()) {
|
||||
$families[$fam['id']] = $fam['name'];
|
||||
}
|
||||
}
|
||||
return $families;
|
||||
}
|
||||
|
||||
|
||||
static function getItemtypesByFamily($families_id) {
|
||||
return getAllDataFromTable(
|
||||
'glpi_plugin_genericobject_types',
|
||||
[
|
||||
'plugin_genericobject_typefamilies_id' => $families_id,
|
||||
'is_active' => 1
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user