Skip to content

Commit

Permalink
Merge pull request #9 from delcroip/develop
Browse files Browse the repository at this point in the history
1.0.1.rc2
  • Loading branch information
delcroip authored May 13, 2021
2 parents dd4977a + f6ef99c commit 86595f7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 271 deletions.
204 changes: 5 additions & 199 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,209 +41,15 @@ This module enable you to create and manage your Medical device

6. Start adding your Medical device

7. Start adding medical component (if any)

8. Start adding medical consomable type and consomables (if any)

## Traduction

This plugin will be managed on transiflex or lokalise


## development

until now the key is the nomenclature, to have the module working it is a must have


All class MUST start with PluginPluginnameObject : don't use a capital letter in the middle of the Pluginname or it will mess with GLPI (it will interpret PluginNameObject as NameObject part of the "Plugin" plugin)

Follow to the letter this page:
https://glpi-developer-documentation.readthedocs.io/en/master/devapi/database/dbmodel.html?highlight=name#naming-conventions


https://glpi-developer-documentation.readthedocs.io/en/master/codingstandards.html#variables-and-constants


To build the assset (Medical device) I duplciated the peripherical code and db (Shown as "ASSET > Device" in glpi)

To build the new asset Item (ASSET > ASSET X > Component in GLPI) I duplicate the medicalaccessories code and db

## Dropdown

It seem that to create a simple dropdown one need to create 3 files
- 1 in the "inc" folder to define the class a subclass of **CommonDropdown** (e.g medicaldevicemodel.class.php)
- 2 in the "front" folder: to instanciate the dropdown (e.g. medicaldevicemodel.php) and to define the edit form (e.g. medicaldevicemodel.form.php)


and a table:

``` sql
DROP TABLE IF EXISTS `glpi_plugin_openmedis_medicaldevicemodels`;
CREATE TABLE `glpi_plugin_openmedis_medicaldevicemodels` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`comment` text COLLATE utf8_unicode_ci,
`product_number` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `product_number` (`product_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
```

**CommonTreeDropdown** class enable and structured tree, (e.g. softwarecategory.class.php)


``` sql
DROP TABLE IF EXISTS `glpi_plugin_openmedis_items_devicemedicalaccessories` ;
CREATE TABLE `glpi_plugin_openmedis_items_devicemedicalaccessories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`items_id` int(11) NOT NULL DEFAULT '0',
`itemtype` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`plugin_openmedis_devicemedicalaccessories_id` int(11) NOT NULL DEFAULT '0',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0',
`is_dynamic` tinyint(1) NOT NULL DEFAULT '0',
`entities_id` int(11) NOT NULL DEFAULT '0',
`is_recursive` tinyint(1) NOT NULL DEFAULT '0',
`serial` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`otherserial` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`locations_id` int(11) NOT NULL DEFAULT '0',
`states_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `plugin_openmedis_medicaldevices_id` (`items_id`),
KEY `plugin_openmedis_devicemedicalaccessories_id` (`plugin_openmedis_devicemedicalaccessories_id`),
KEY `is_deleted` (`is_deleted`),
KEY `is_dynamic` (`is_dynamic`),
KEY `entities_id` (`entities_id`),
KEY `is_recursive` (`is_recursive`),
KEY `serial` (`serial`),
KEY `item` (`itemtype`,`items_id`),
KEY `otherserial` (`otherserial`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
```

## asset

https://glpi-developer-documentation.readthedocs.io/en/master/plugins/objects.html#add-a-front-for-my-object-crud

It seems that to create an assest 3 files are required:
- 1 class file in the "inc" folder a subclass of **CommonDBTM**
- 2 files in the "front" folder:to instanciate the serach list (e.g. medicaldevice.php) and to define the edit form (e.g. medicaldevice.form.php)

plus a table:
``` sql
DROP TABLE IF EXISTS `glpi_plugin_openmedis_medicaldevices` ;
CREATE TABLE `glpi_plugin_openmedis_medicaldevices` (
`id` int(11) NOT NULL AUTO_INCREMENT, -- assetid
`entities_id` int(11) NOT NULL DEFAULT '0',
`name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, -- AssetFullName
`date_mod` datetime DEFAULT NULL,
`contact` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`contact_num` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`users_id_tech` int(11) NOT NULL DEFAULT '0',
`groups_id_tech` int(11) NOT NULL DEFAULT '0',
`comment` text COLLATE utf8_unicode_ci,
`serial` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`otherserial` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`locations_id` int(11) NOT NULL DEFAULT '0',
`plugin_openmedis_medicaldevicemodels_id` int(11) NOT NULL DEFAULT '0',
`plugin_openmedis_medicaldevicecategories_id` int(11) NOT NULL DEFAULT '0',
`brand` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`manufacturers_id` int(11) NOT NULL DEFAULT '0',
`is_global` tinyint(1) NOT NULL DEFAULT '0',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0',
`is_template` tinyint(1) NOT NULL DEFAULT '0',
`template_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`users_id` int(11) NOT NULL DEFAULT '0',
`groups_id` int(11) NOT NULL DEFAULT '0',
`states_id` int(11) NOT NULL DEFAULT '0',
`ticket_tco` decimal(20,4) DEFAULT '0.0000',
`is_dynamic` tinyint(1) NOT NULL DEFAULT '0',
`date_creation` datetime DEFAULT NULL,
`is_recursive` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `is_template` (`is_template`),
KEY `is_global` (`is_global`),
KEY `entities_id` (`entities_id`),
KEY `manufacturers_id` (`manufacturers_id`),
KEY `groups_id` (`groups_id`),
KEY `users_id` (`users_id`),
KEY `locations_id` (`locations_id`),
KEY `plugin_openmedis_medicaldevicemodels_id` (`plugin_openmedis_medicaldevicemodels_id`),
KEY `states_id` (`states_id`),
KEY `users_id_tech` (`users_id_tech`),
KEY `plugin_openmedis_medicaldevicecategories_id` (`plugin_openmedis_medicaldevicecategories_id`),
KEY `is_deleted` (`is_deleted`),
KEY `date_mod` (`date_mod`),
KEY `groups_id_tech` (`groups_id_tech`),
KEY `is_dynamic` (`is_dynamic`),
KEY `serial` (`serial`),
KEY `otherserial` (`otherserial`),
KEY `date_creation` (`date_creation`),
KEY `is_recursive` (`is_recursive`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
```


## item (not yet working)

It seems that to create an assest 4 files are required:
- 2 class file in the "inc" folder: on to define the asset-itme relationship , a subclass of **Item_Devices** an other to define the class itself ta subclass of **CommonDevice**
- 2 files in the "front" folder:to instanciate the dropdown (e.g. medicaldevice.php) and to define the edit form (e.g. medicaldevice.form.php)

plus a table:
``` sql
DROP TABLE IF EXISTS `glpi_plugin_openmedis_items_devicemedicalaccessories` ;
CREATE TABLE `glpi_plugin_openmedis_items_devicemedicalaccessories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`items_id` int(11) NOT NULL DEFAULT '0',
`itemtype` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`plugin_openmedis_devicemedicalaccessories_id` int(11) NOT NULL DEFAULT '0',
`is_deleted` tinyint(1) NOT NULL DEFAULT '0',
`is_dynamic` tinyint(1) NOT NULL DEFAULT '0',
`entities_id` int(11) NOT NULL DEFAULT '0',
`is_recursive` tinyint(1) NOT NULL DEFAULT '0',
`serial` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`otherserial` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`locations_id` int(11) NOT NULL DEFAULT '0',
`states_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `plugin_openmedis_medicaldevices_id` (`items_id`),
KEY `plugin_openmedis_devicemedicalaccessories_id` (`plugin_openmedis_devicemedicalaccessories_id`),
KEY `is_deleted` (`is_deleted`),
KEY `is_dynamic` (`is_dynamic`),
KEY `entities_id` (`entities_id`),
KEY `is_recursive` (`is_recursive`),
KEY `serial` (`serial`),
KEY `item` (`itemtype`,`items_id`),
KEY `otherserial` (`otherserial`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
```


## profile

It seems that to manage new right, one need to create class file in the inc folder,

a good example can be found here https://github.com/InfotelGLPI/racks/blob/master/inc/profile.class.php

for more information check this page https://glpi-developer-documentation.readthedocs.io/en/master/devapi/acl.html


## config

This files is required in the inc folder if there is some configuuration needed for the whole plugin

for the rack module the unit (metric of not) can be defined for the wholé plugin https://github.com/InfotelGLPI/racks/blob/master/inc/config.class.php


## hooks

the hook page define the hook that can be called from elswhere but mostl of the plugin hook are called from the setup page

for more infornation https://glpi-developer-documentation.readthedocs.io/en/master/plugins/requirements.html#hook-php

## setup


for more information https://glpi-developer-documentation.readthedocs.io/en/master/plugins/requirements.html#setup-php

## tips and trics

https://glpi-developer-documentation.readthedocs.io/en/master/plugins/tips.html
- dedicate reports
5 changes: 4 additions & 1 deletion inc/medicaldevice.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class PluginOpenmedisMedicalDevice extends CommonDBTM {
static $rightname = 'plugin_openmedis';
protected $usenotepad = true;

static public $itemtype_1 = 'pluginOpenmedisMedicalDevice';
static public $items_id_1 = 'pluginopenmedismedicaldevice_id';

static $types = ['PluginOpenmedisDeviceMedicalAccessory'];
/**
* Name of the type
Expand Down Expand Up @@ -205,7 +208,7 @@ function showForm($ID, $options = []) {
State::dropdown([
'value' => $this->fields["states_id"],
'entity' => $this->fields["entities_id"],
'condition' => ['is_visible_medicaldevice' => 1]
'condition' => ['is_visible_pluginopenmedismedicaldevice' => 1]
]);
echo "</td></tr>\n";

Expand Down
57 changes: 0 additions & 57 deletions install/mysql/upgrade_to_1_1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,60 +84,3 @@ CREATE TABLE `glpi_plugin_openmedis_medicalconsumableitems` (
KEY `date_creation` (`date_creation`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DELIMITER $$

DROP PROCEDURE IF EXISTS replace_key_if_exists $$
CREATE PROCEDURE replace_key_if_exists(in theTable varchar(128), in oldIndexName varchar(128), in newIndexNameDef varchar(128) )
BEGIN
IF((SELECT COUNT(*) AS index_exists FROM information_schema.statistics WHERE TABLE_SCHEMA = DATABASE() and table_name =
theTable AND INDEX_NAME = oldIndexName) > 0) THEN
SET @s = CONCAT('ALTER TABLE ',theTable, ' DROP KEY ' , oldIndexName , ', ADD KEY ',newIndexNameDef );
PREPARE stmt FROM @s;
EXECUTE stmt;
END IF;
END $$

DROP PROCEDURE IF EXISTS add_column_if_not_exists $$
CREATE PROCEDURE add_column_if_not_exists(in theTable varchar(128), in ColumnName varchar(128), in ColumnDef varchar(128) )
BEGIN
IF NOT ((SELECT COUNT(*) AS index_exists FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() and table_name =
theTable AND COLUMN_NAME = ColumnName) > 0) THEN
SET @s = CONCAT('ALTER TABLE ',theTable, ' ADD ' , ColumnName , ' ',ColumnDef );
PREPARE stmt FROM @s;
EXECUTE stmt;
END IF;
END $$

DROP PROCEDURE IF EXISTS rename_column_if_exists $$
CREATE PROCEDURE rename_column_if_exists(in theTable varchar(128), in oldColumnName varchar(128), in newColumnNameDef varchar(128) )
BEGIN
IF ((SELECT COUNT(*) AS index_exists FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() and table_name =
theTable AND COLUMN_NAME = oldColumnName) > 0) THEN
SET @s = CONCAT('ALTER TABLE ',theTable, ' change ' , oldColumnName , ' ',newColumnNameDef );
PREPARE stmt FROM @s;
EXECUTE stmt;
END IF;
END $$

DROP PROCEDURE IF EXISTS remane_table_if_exists $$
CREATE PROCEDURE remane_table_if_exists(in oldtableName varchar(128), in newTableName varchar(128) )
BEGIN
IF((SELECT COUNT(*) AS index_exists FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() and table_name =
theTable ) > 0) THEN
SET @s = CONCAT('ALTER TABLE ',oldtableName, ' remane ' , newTableName );
PREPARE stmt FROM @s;
EXECUTE stmt;
END IF;
END $$

DELIMITER ;
### change key plugin_openmedis_medicaldevice_id -->> plugin_openmedis_medicaldevices_id

CALL replace_key_if_exists('glpi_plugin_openmedis_devicemedicalaccessories_items','plugin_openmedis_medicaldevice_id', 'plugin_openmedis_medicaldevices_id (items_id)');
CALL add_column_if_not_exists('glpi_plugin_openmedis_medicaldevices','init_usages_counter','int(11) NOT NULL DEFAULT 0');
CALL add_column_if_not_exists('glpi_plugin_openmedis_medicaldevices','last_usages_counter','int(11) NOT NULL DEFAULT 0');
CALL remane_table_if_exists('glpi_plugin_openmedis_item_devicemedicalaccessories', 'glpi_plugin_openmedis_item_medicalaccessories');
CALL remane_table_if_exists('glpi_plugin_openmedis_devicemedicalaccessories', 'glpi_plugin_openmedis_devicemedicalaccessories');
CALL rename_column_if_exists('glpi_plugin_openmedis_items_medicalaccessories','plugin_openmedis_devicemedicalaccessories_id','plugin_openmedis_devicemedicalaccessories_id int(11) NOT NULL DEFAULT 0');
CALL replace_key_if_exists('plugin_openmedis_devicemedicalaccessories_id','plugin_openmedis_devicemedicalaccessories_id' , 'plugin_openmedis_devicemedicalaccessories_id (`plugin_openmedis_devicemedicalaccessories_id`)");
68 changes: 67 additions & 1 deletion install/upgrade_to_1_1.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,73 @@ public function upgrade(Migration $migration) {
}
return true;
}
$this->addfieldIfNotExists('glpi_states',
'is_visible_pluginopenmedismedicaldevice',"tinyint(1) NOT NULL DEFAULT '1'", true);
$this->renameTableifExists('glpi_plugin_openmedis_item_devicemedicalaccessories',
'glpi_plugin_openmedis_item_medicalaccessories');
$this->renameTableIfExists('glpi_plugin_openmedis_devicemedicalaccessorie',
'glpi_plugin_openmedis_devicemedicalaccessories');
$this->addfieldIfNotExists('glpi_plugin_openmedis_medicaldevices',
'init_usages_counter','int(11) NOT NULL DEFAULT 0');
$this->addfieldIfNotExists('glpi_plugin_openmedis_medicaldevices',
'last_usages_counter','int(11) NOT NULL DEFAULT 0');
$this->renamefieldIfExists('glpi_plugin_openmedis_items_medicalaccessories',
'plugin_openmedis_devicemedicalaccessories_id','plugin_openmedis_devicemedicalaccessories_id',
'int(11) NOT NULL DEFAULT 0');
$this->replaceIndexIfExists('glpi_plugin_openmedis_devicemedicalaccessories_items',
'plugin_openmedis_medicaldevice_id', 'plugin_openmedis_medicaldevices_id', 'items_id');
$this->migration->displayWarning("table to be created by the migration already existing : " . $DB->error(), true);
return false;
}
}

private function addfieldIfNotExists($table, $field, $fieldOptions, $index = false){
global $DB;
if(!$DB->fieldExists($table,$field)){
$sql = "ALTER TABLE ".$table;
$sql .= " ADD `".$field.'` '.$fieldOptions;
if($index)$sql .= ", ADD KEY `".$field.'` (`'.$field.'`)';
$DB->query($sql);
}
}

private function renameTableIfExists($oldTable, $newTable){
global $DB;
if($DB->tableExists($oldTable)){
$sql = "ALTER TABLE ".$oldTable;
$sql .= " RENAME ".$newTable;
$DB->query($sql);
}
}

private function renamefieldIfExists($table, $oldfield,$newfield, $fieldOptions, $index = false, $indexName = ''){
global $DB;
if($DB->fieldExists($table,$oldfield)){
$sql = "ALTER TABLE ".$table;
$sql .= " change ".$oldfield.' '.$newfield.' '.$fieldOptions ;
if($index){
if($indexName == '')$indexName = $newfield;
$sql .=' DROP KEY '.$oldfield.', ADD KEY `'.$newfield.'` (`'.$indexName.'`)';
}
$DB->query($sql);
}
}

private function indexExists($table, $index){
global $DB;
$sql = "SELECT COUNT(*) AS index_exists FROM information_schema.statistics
WHERE TABLE_SCHEMA = DATABASE() and table_name =
${table} AND INDEX_NAME = ${index}";
if ($DB->query($sql) == 1 ) return true;
else return false;
}

private function replaceIndexIfExists($table, $oldIndex, $field, $newIndex){
global $DB;
if($this->indexExists($table,$oldIndex)){
$sql = "ALTER TABLE ".$table;
$sql .=' DROP KEY '.$oldIndex.', ADD KEY `'.$newIndex.'` (`'.$field.'`)';
$DB->query($sql);
}
}

}
Loading

0 comments on commit 86595f7

Please sign in to comment.