.
* -------------------------------------------------------------------------
* @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 extends CommonDBTM {
use Glpi\Features\Clonable;
protected $objecttype;
//Internal field counter
private $cpt = 0;
//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])) {
$type_name = $LANG['genericobject'][$class][0];
} else {
$type_name = $item->objecttype->fields['name'];
}
return ucwords($type_name);
}
public function __construct() {
$class = get_called_class();
$this->table = getTableForItemType($class);
if (class_exists($class)) {
$this->objecttype = PluginGenericobjectType::getInstance($class);
}
$this->dohistory = $this->canUseHistory();
if (preg_match("/PluginGenericobject(.*)/", $class, $results)) {
if (preg_match("/^(.*)y$/i", $results[1], $end_results)) {
static::$rightname = 'plugin_genericobject_'.strtolower($end_results[1]).'ies';
} else if (preg_match("/^(.*)ss$/i", $results[1])) {
static::$rightname = 'plugin_genericobject_'.strtolower($results[1]).'es';
} else {
static::$rightname = 'plugin_genericobject_'.strtolower($results[1]).'s';
}
}
if ($this->canUseNotepad()) {
// For GLPI 0.85.x
$this->usenotepadrights = true;
// For GLPI 0.90.x
$this->usenotepad = true;
}
}
public function getCloneRelations() :array {
return [
Computer_Item::class,
Contract_Item::class,
Document_Item::class,
Infocom::class,
Item_devices::class,
NetworkPort::class,
];
}
/**
* Display information on treeview plugin
*
* @params itemtype, id, pic, url, name
*
* @return params
**/
static function showGenericObjectTreeview($params) {
global $CFG_GLPI;
if (array_key_exists($params['itemtype'], PluginGenericobjectType::getTypes())) {
$item = new $params['itemtype']();
if ($item->getFromDB($params['id'])) {
$params['name'] = $item->fields["name"];
$params['url'] = Plugin::getWebDir('genericobject')."/front/object.form.php".
"?itemtype=".$params['itemtype']."&id=".$params['id'];
}
}
return $params;
}
/**
* Display node search url on treeview plugin
*
* @params itemtype, id, pic, url, name
*
* @return params
**/
static function getParentNodeSearchUrl($params) {
if (array_key_exists($params['itemtype'], PluginGenericobjectType::getTypes())) {
$item = new $params['itemtype']();
$search = $item->rawSearchOptions();
//get searchoption id for location_id
foreach ($search as $key => $val) {
if (isset($val['table']) && $val['table'] === 'glpi_locations') {
$index= $key;
}
}
$token = Session::getNewCSRFToken();
$params['searchurl'] = $params['itemtype']::getSearchURL()."&is_deleted=0&criteria[0][field]=".$index."&criteria[0]".
"[searchtype]=equals&criteria[0][value]=".$params['locations_id']."&search=Rechercher&start=0&_glpi_csrf_token=$token";
return $params;
}
return $params;
}
static function install() {
}
static function uninstall() {
}
static function registerType() {
global $DB, $PLUGIN_HOOKS, $UNINSTALL_TYPES, $ORDER_TYPES, $CFG_GLPI,
$GO_LINKED_TYPES, $GENINVENTORYNUMBER_TYPES;
$class = get_called_class();
$item = new $class();
$fields = PluginGenericobjectSingletonObjectField::getInstance($class);
$plugin = new Plugin();
PluginGenericobjectType::includeLocales($item->getObjectTypeName());
PluginGenericobjectType::includeConstants($item->getObjectTypeName());
Plugin::registerClass($class, [
"document_types" => $item->canUseDocuments(),
"helpdesk_visible_types" => $item->canUseTickets(),
"linkgroup_types" => isset($fields["groups_id"]),
"linkuser_types" => isset($fields["users_id"]),
"linkgroup_tech_types" => isset($fields["groups_id_tech"]),
"linkuser_tech_types" => isset($fields["users_id_tech"]),
"ticket_types" => $item->canUseTickets(),
"infocom_types" => $item->canUseInfocoms(),
"networkport_types" => $item->canUseNetworkPorts(),
"reservation_types" => $item->canBeReserved(),
"contract_types" => $item->canUseContracts(),
"unicity_types" => $item->canUseUnicity(),
"location_types" => isset($fields['locations_id']),
"itemdevices_types" => $item->canUseItemDevice()
]);
if (plugin_genericobject_haveRight($class, READ)) {
//Change url for adding a new object, depending on template management activation
if ($item->canUseTemplate()) {
//Template management is active
$add_url = "/front/setup.templates.php?itemtype=$class&add=1";
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$class]['links']['template']
= "/front/setup.templates.php?itemtype=$class&add=0";
} else {
//Template management is not active
$add_url = Toolbox::getItemTypeFormURL($class, false);
}
//Menu management
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$class]['title']
= $class::getTypeName();
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$class]['page']
= Toolbox::getItemTypeSearchURL($class, false);
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$class]['links']['search']
= Toolbox::getItemTypeSearchURL($class, false);
if (plugin_genericobject_haveRight($class, UPDATE)) {
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$class]['links']['add']
= $add_url;
}
//Add configuration icon, if user has right
if (Session::haveRight('config', UPDATE)) {
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$class]['links']['config']
= Toolbox::getItemTypeSearchURL('PluginGenericobjectType', false)."?itemtype=$class";
}
if ($item->canUsePluginUninstall()) {
if (!in_array($class, $UNINSTALL_TYPES)) {
array_push($UNINSTALL_TYPES, $class);
}
}
if ($item->canUsePluginSimcard()) {
if ($plugin->isActivated('simcard') && $plugin->isActivated('simcard')) {
PluginSimcardSimcard_Item::registerItemtype($class);
}
}
if ($item->canUsePluginOrder()) {
if (!in_array($class, $ORDER_TYPES)) {
array_push($ORDER_TYPES, $class);
}
}
if ($item->canUseGlobalSearch()) {
if (!in_array($class, $CFG_GLPI['asset_types'])) {
array_push($CFG_GLPI['asset_types'], $class);
}
if (!in_array($class, $CFG_GLPI['state_types'])) {
array_push($CFG_GLPI['state_types'], $class);
}
if (!in_array($class, $CFG_GLPI['globalsearch_types'])) {
array_push($CFG_GLPI['globalsearch_types'], $class);
}
}
if ($item->canUseDirectConnections()) {
if (!in_array($class, $GO_LINKED_TYPES)) {
array_push($GO_LINKED_TYPES, $class);
}
$items_class = $class."_Item";
//if (class_exists($items_class)) {
$items_class::registerType();
//}
}
if ($item->canUseProjects()) {
if (!in_array($class, $CFG_GLPI['project_asset_types'])) {
array_push($CFG_GLPI['project_asset_types'], $class);
}
}
$plugin_gen_path = Plugin::getPhpDir('geninventorynumber');
if ($item->canUsePluginGeninventorynumber()) {
if (!in_array($class, $GENINVENTORYNUMBER_TYPES)) {
include_once ("$plugin_gen_path/inc/profile.class.php");
PluginGeninventorynumberConfigField::registerNewItemType($class);
array_push($GENINVENTORYNUMBER_TYPES, $class);
}
} else if ($plugin->isActivated('geninventorynumber')) {
include_once ("$plugin_gen_path/inc/profile.class.php");
PluginGeninventorynumberConfigField::unregisterNewItemType($class);
}
}
foreach (PluginGenericobjectType::getDropdownForItemtype($class) as $table) {
$itemtype = getItemTypeForTable($table);
if (class_exists($itemtype)) {
$item = new $itemtype();
//If entity dropdown, check rights to view & create
if ($itemtype::canView()) {
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$itemtype]['links']['search']
= Toolbox::getItemTypeSearchURL($itemtype, false);
if ($itemtype::canCreate()) {
$PLUGIN_HOOKS['submenu_entry']['genericobject']['options'][$class]['links']['add']
= Toolbox::getItemTypeFormURL($class, false);
}
}
}
}
}
static function getMenuIcon($itemtype) {
global $CFG_GLPI;
$itemtype_table = getTableForItemType($itemtype);
$itemtype_shortname = preg_replace("/^glpi_plugin_genericobject_/", "", $itemtype_table);
$itemtype_icons = glob(
GENERICOBJECT_PICS_PATH . '/' . getSingular($itemtype_shortname) . ".*"
);
$finfo = new finfo(FILEINFO_MIME);
$icon_found = null;
foreach ($itemtype_icons as $icon) {
if (preg_match("|^image/|", $finfo->file($icon))) {
$icon_found = preg_replace("|^".GLPI_ROOT."|", "", $icon);
}
}
if (!is_null($icon_found)) {
$icon_path = $CFG_GLPI['root_doc'] . $icon_found;
} else {
$icon_path = Plugin::getWebDir('genericobject') . "/pics/default-icon.png";
}
return "".
"";
}
static function checkItemtypeRight($class, $right) {
if (!is_null($class) and class_exists($class)) {
$right_name = PluginGenericobjectProfile::getProfileNameForItemtype(
$class
);
return Session::haveRight($right_name, $right);
}
}
static function canCreate() {
$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);
}
return static::checkItemtypeRight($class, CREATE);
}
static function canView() {
$class = get_called_class();
return static::checkItemtypeRight($class, READ);
}
static function canUpdate() {
$class = get_called_class();
return static::checkItemtypeRight($class, UPDATE);
}
static function canDelete() {
$class = get_called_class();
return static::checkItemtypeRight($class, DELETE);
}
static function canPurge() {
$class = get_called_class();
return static::checkItemtypeRight($class, PURGE);
}
function defineTabs($options = []) {
$tabs = [];
$this->addDefaultFormTab($tabs);
if (!$this->isNewItem()) {
if ($this->canUseNetworkPorts()) {
$this->addStandardTab('NetworkPort', $tabs, $options);
}
if ($this->canUseItemDevice()) {
$this->addStandardTab('Item_Devices', $tabs, $options);
}
if ($this->canUseInfocoms()) {
$this->addStandardTab('Infocom', $tabs, $options);
}
if ($this->canUseContracts()) {
$this->addStandardTab('Contract_Item', $tabs, $options);
}
if ($this->canUseDocuments()) {
$this->addStandardTab('Document_Item', $tabs, $options);
}
if ($this->canUseTickets()) {
$this->addStandardTab('Ticket', $tabs, $options);
$this->addStandardTab('Item_Problem', $tabs, $options);
$this->addStandardTab('Change_Item', $tabs, $options);
}
if ($this->canUseNotepad()) {
$this->addStandardTab('Notepad', $tabs, $options);
}
if ($this->canBeReserved()) {
$this->addStandardTab('Reservation', $tabs, $options);
}
if ($this->canUseHistory()) {
$this->addStandardTab('Log', $tabs, $options);
}
}
return $tabs;
}
//------------------------ CAN methods -------------------------------------//
function getObjectTypeName() {
return $this->objecttype->getName();
}
function canUseInfocoms() {
return ($this->objecttype->canUseInfocoms() && Session::haveRight("infocom", READ));
}
function canUseContracts() {
return ($this->objecttype->canUseContracts() && Session::haveRight("contract", READ));
}
function canUseTemplate() {
return $this->objecttype->canUseTemplate();
}
function canUseNotepad() {
return $this->objecttype->canUseNotepad();
}
function canUseUnicity() {
// Disable unicity feature (for GLPI 0.85 onward) : see issue #16
// Related code : search for #16
// FIXME : The bug may be in GLPI itself
return ($this->objecttype->canUseUnicity() && Session::haveRight("config", READ));
}
function canUseDocuments() {
return ($this->objecttype->canUseDocuments() && Session::haveRight("document", READ));
}
function canUseTickets() {
return ($this->objecttype->canUseTickets());
}
function canUseGlobalSearch() {
return ($this->objecttype->canUseGlobalSearch());
}
function canBeReserved() {
return (
$this->objecttype->canBeReserved()
and Session::haveRight(ReservationItem::$rightname, ReservationItem::RESERVEANITEM)
);
}
function canUseHistory() {
return ($this->objecttype->canUseHistory());
}
function canUsePluginDataInjection() {
return ($this->objecttype->canUsePluginDataInjection());
}
function canUsePluginPDF() {
return ($this->objecttype->canUsePluginPDF());
}
function canUsePluginOrder() {
return ($this->objecttype->canUsePluginOrder());
}
function canUsePluginGeninventorynumber() {
return ($this->objecttype->canUsePluginGeninventorynumber());
}
function canUseNetworkPorts() {
return ($this->objecttype->canUseNetworkPorts());
}
function canUseDirectConnections() {
return ($this->objecttype->canUseDirectConnections());
}
function canUseProjects() {
return ($this->objecttype->canUseProjects());
}
function canUsePluginUninstall() {
return ($this->objecttype->canUsePluginUninstall());
}
function canUsePluginSimcard() {
return ($this->objecttype->canUsePluginSimcard());
}
function getLinkedItemTypesAsArray() {
return $this->objecttype->getLinkedItemTypesAsArray();
}
function canUseItemDevice() {
return ($this->objecttype->canUseItemDevice());
}
function title() {
}
function showForm($id, $options = [], $previsualisation = false) {
global $DB;
$display_date = (!method_exists('CommonDBTM', 'showDates'));
if ($previsualisation) {
$canedit = true;
$this->getEmpty();
} else {
if ($id > 0) {
$this->check($id, READ);
} else {
// Create item
$this->check(-1, CREATE);
$this->getEmpty();
}
$canedit = $this->can($id, UPDATE);
}
if (isset($options['withtemplate']) && $options['withtemplate'] == 2) {
$template = "newcomp";
$date = sprintf(__('Created on %s'), Html::convDateTime($_SESSION["glpi_currenttime"]));
} else if (isset($options['withtemplate']) && $options['withtemplate'] == 1) {
$template = "newtemplate";
$date = sprintf(__('Created on %s'), Html::convDateTime($_SESSION["glpi_currenttime"]));
} else {
$date = sprintf(__('Last update on %s'), Html::convDateTime($this->fields["date_mod"]));
$template = false;
}
$this->fields['id'] = $id;
$this->initForm($id, $options);
$this->showFormHeader($options);
if ($previsualisation) {
echo "