The file setup.php is used to define :
  • general behavior of the plugin (the core functionality used)
  • functions that the plugin needs in order to be installed

The file must contain at least 4 functions :

plugin_init_<plugin_name>()
  • Declares the type of plugin
  • Declares the display
  • Hook actions performed on core GLPI objects
  • Using the CRON scheduler
  • Use Planning
  • Use of mass actions (massiveactions)
  • Integration with Helpdesk
  • Added javascript and CSS Personalized
plugin_version_<plugin_name>()
  • Name
  • Version
  • Author
  • Minimum GLPI version
  • Maximum GLPI version
plugin_<plugin_name>_check_prerequisites()
  • Check prerequisites to install the plugin
plugin_<plugin_name>_check_config()
  • Verification of any GLPI config necessary to install the plugin

Hooks on GLPI actions

A plugin can have callbacks on each action taken in GLPI to perform custom actions. 
For each hook, a list of function names is included
  • Adding an object :
  • Before adding the object :
$PLUGIN_HOOKS['pre_item_add']['<plugin_name>'] = 'plugin_pre_item_add_<plugin_name>';
  • After adding the object :
$PLUGIN_HOOKS['item_add']['<plugin_name>'] = 'plugin_item_add_<plugin_name>';
  • Updating an object :
  • Before updating the object :
$PLUGIN_HOOKS['pre_item_update']['<plugin_name>'] = 'plugin_pre_item_update_<plugin_name>';
  • After updating the object :
$PLUGIN_HOOKS['item_update']['<plugin_name>'] = 'plugin_item_update_<plugin_name>';
  • Deleting an object :
  • Before object deletion :
$PLUGIN_HOOKS['pre_item_delete']['<plugin_name>'] = 'plugin_pre_item_delete_<plugin_name>';
  • After object deletion :
$PLUGIN_HOOKS['item_delete']['<plugin_name>'] = 'plugin_item_delete_<plugin_name>';
  • Purge an object :
  • Before purging the object from the database :
$PLUGIN_HOOKS['pre_item_purge']['<plugin_name>'] = 'plugin_pre_item_purge_<plugin_name>';
  • After purging the object from the database :
$PLUGIN_HOOKS['item_purge']['<plugin_name>'] = 'plugin_item_purge_<plugin_name>';
  • Restoring an object :
  • Before the object is restored :
$PLUGIN_HOOKS['pre_item_restore']['<plugin_name>'] = 'plugin_pre_item_restore_<plugin_name>';
  • After the object is restored :
$PLUGIN_HOOKS['item_restore']['<plugin_name>'] = 'plugin_item_restore_<plugin_name>';

Hooks on LDAP authentication

  • Perform additional operations to declare an authenticated user or not :
$PLUGIN_HOOKS['restrict_ldap_auth']['<plugin_name>'] = 'plugin_authentication_restrict_ldap_auth_<plugin_name>';
  • Perform additional operations to declare an authenticated user or not :
$PLUGIN_HOOKS['retrieve_more_data_from_ldap']['<plugin_name>'] = 'plugin_retrieve_more_data_from_ldap_<votre plugin>';

Visual Integration

  • Indicates if the plugin should be displayed in the "Helpdesk" menu
$PLUGIN_HOOKS['helpdesk_menu_entry']['<plugin_name>'] = true;
  • Indicates if the plugin should be displayed in the "Central Console" menu
$PLUGIN_HOOKS['menu_entry']['<plugin_name>'] = true;

The plugins menu refers to the root of the plugins, so to index.php.

  • Sets if necessary sub-menus for the "Central Console" interface :
$PLUGIN_HOOKS['submenu_entry']['<plugin_name>'][ACTION] = LINK;
  • ACTION Defines the item displayed in the submenu. The default actions are defined in GLPI (add, search, template, showall and summary).

For these actions the standard icons are used. But you can also choose to display a text or a personal icon (see example plugin).

or :

  • Defines sub-menus for the interface "Central Console":
$PLUGIN_HOOKS['submenu_entry']['<plugin_name>'][ACTION][OPTION] = LINK;

It is possible to display a custom link depending on the OPTION parameter. This allows to change the link according to the category under which it is located.

  • Page Tree plugin appear in the Configuration / Plugins:
$PLUGIN_HOOKS['config_page']['<plugin_name>'] = 'config.php';
  • Function defining the actions taken by the tabs for different GLPI types. It returns an array containing the list of functions corresponding to the shares returned by the function plugin_get_headings_<plugin_name>:
    $PLUGIN_HOOKS['headings_action']['<plugin_name>'] = 'plugin_headings_actions_<plugin_name>';
    
  • Function defining entries in the tabs of different types of GLPI :
$PLUGIN_HOOKS['headings']['<plugin_name>'] = 'plugin_get_headings_<plugin_name>';

0.72 : 2 additional types may be used: central and prefs to display on the home page of the interface "Central Console" and user preferences.
The complete list of types is available in the file config/define.php

  • Function defining the actions taken by the tabs for different GLPI types. It returns an array containing the list of functions corresponding to the shares returned by the function plugin_get_headings_<plugin_name> :
$PLUGIN_HOOKS['headings_action']['<plugin_name>'] = 'plugin_headings_actions_<plugin_name>';

Helpdesk

It is possible to make an object visible in the Helpdesk (ie be able to report an incident on it).

  • will make at least one type of plugin available from the Helpdesk. The definition of objects that are available is in the function plugin_<plugin_name>_assignToTicket in the file hook.php :
$PLUGIN_HOOKS['assign_to_ticket']['<plugin_name>']=1;

For an object to be visible in the "My Devices" list :

  • the option linkuser_types is either TRUE in registerNewType and a FK_users field exists in the objec's table

and/or

  • the option linkgroup_types is either TRUE in registerNewType and a FK_groups field exists n the object's table

Planning

Two functions can be defined and used to populate the schedule and to define how elements are displayed:

  • Add events to the schedule :
$PLUGIN_HOOKS['planning_populate']['<plugin_name>']="plugin_planning_populate_<plugin_name>";
  • Manage the display of an event in the schedule :
$PLUGIN_HOOKS['display_planning']['<plugin_name>']="plugin_display_planning_<plugin_name>";

CRON

You can add a scheduled task to the GLPI internal CRON.
Just define :

$PLUGIN_HOOKS['cron']['<plugin_name>'] = DAY_TIMESTAMP;

This will launch the function {{{cron_plugin_<plugin_name>}}} in hook.php in the background (daily).

Exemple : every 30 minutes :

$PLUGIN_HOOKS['cron']['<plugin_name>'] = 30*60

or
$PLUGIN_HOOKS['cron']['<plugin_name>'] = 30*MINUTE_TIMESTAMP

ou
$PLUGIN_HOOKS['cron']['<plugin_name>'] = HOUR_TIMESTAMP/2;

Redirecting

The call to the login page allows automatic redirection (eg for access when sending a link via email).
You can perform an automatic redirect to a page of your plugin by defining:

$PLUGIN_HOOKS['redirect_page']['<plugin_name>']="PAGE_TO_REDIRECT.php";

calling index.php?redirect=plugin_<plugin_name>_IDTOREDIRECT redirects to plugins/PLUGINNAME/PAGE_TO_REDIRECT.php?ID=IDTOREDIRECT

Management array_push

  • You must specify the table for a dropdown tree :
array_push($CFG_GLPI["dropdowntree_tables"],"glpi_dropdown_plugin_<plugin_name>");
  • If a separate dropdown for each entity :
array_push($CFG_GLPI["specif_entities_tables"],"glpi_dropdown_plugin_<plugin_name>");

Defining a new object type

  • For this you can use the fonction registerPluginType (replacing pluginNewType) :
registerPluginType('example',    // Plugin Name
    'PLUGIN_EXAMPLE_TYPE',    // String defining the type
    1001,            // type number
    array(            // Attribute
        'classname'  => 'pluginExample',    // Class Name
        'tablename'  => 'glpi_plugin_example',    // Table Name
        'formpage'   => 'example.form.php',    // Entry Form
        'searchpage' => 'example.php',        // Search Page
        'typename'   => 'Example',        // Name Type
        'deleted_tables' => false,        // handle "deleted" 
        'template_tables' => false,        // use "is_template" 
        'specif_entities_tables' => false,    // object in an entity defn
        'recursive_type' => false,        // Object recursive
        'reservation_type' => false,        // Handle reservations
                'linkuser_types' => true,               // Allow an object to be visible in the "My Materials" menu attached to a user" 
                'linkgroup_types' => true,              // Allow an object to be bisible in the "My Materials" menu attached to a group
                'massiveaction_noupdate' => true,       // Remove the "Modify" link
                'massiveaction_nodelete' => true,       // Remove the "Delete" link
                'doc_types' => true,                // From 0.72.1 : connection with the document 
                'helpdesk_visible_types' => true        // From 0.72.1 : is visible in the helpdesk
    ));

Attributes which are empty or FALSE can be omitted.

The type numbers are reserved in increments of 100 here : PluginTypesReservation. The values start at 1000.

Defining mass modifications

Default action types : modify / delete / purge / restore are accessible from the lists.

  • To add specific actions, we must add :
$PLUGIN_HOOKS['use_massive_action']['<plugin_name>']=1;