You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2020-07-30 10:34:53

ThePotager
Member
Registered: 2020-07-30
Posts: 2

Élément introuvable , simple CRUD

Bonjour,
Ceci sont mes premiers pas sur GLPI, soyez indulgent.
Je souhaite créer un CRUD totalement basique, rien de plus, rien de moins.
Je farfouille donc la documentation developpeur de GLPI.
J'ai au préalable créer un plugin avec empty, qui se nomme : myexempleplugin
Je suis précisement les étapes sur la documentation comme ceci :

SETUP.PHP

<?php

define('MYEXAMPLEPLUGIN_VERSION', '1.2.10');

/**
 * Init the hooks of the plugins - Needed
 *
 * @return void
 */
function plugin_init_myexampleplugin() {
   global $PLUGIN_HOOKS;

   //required!
   $PLUGIN_HOOKS['csrf_compliant']['myexample'] = true;

   //some code here, like call to Plugin::registerClass(), populating PLUGIN_HOOKS, ...
}

/**
 * Get the name and the version of the plugin - Needed
 *
 * @return array
 */
function plugin_version_myexampleplugin() {
   return [
      'name'           => 'Plugin name that will be displayed',
      'version'        => MYEXAMPLEPLUGIN_VERSION,
      'author'         => 'John Doe and <a href="http://foobar.com">Foo Bar</a>',
      'license'        => 'GLPv3',
      'homepage'       => 'http://perdu.com',
      'requirements'   => [
         'glpi'   => [
            'min' => '9.1'
         ]
      ]
   ];
}

/**
 * Optional : check prerequisites before install : may print errors or add to message after redirect
 *
 * @return boolean
 */
function plugin_myexampleplugin_check_prerequisites() {
   //do what the checks you want
   return true;
}

/**
 * Check configuration process for plugin : need to return true if succeeded
 * Can display a message only if failure and $verbose is true
 *
 * @param boolean $verbose Enable verbosity. Default to false
 *
 * @return boolean
 */
function plugin_myexampleplugin_check_config($verbose = false) {
   if (true) { // Your configuration check
      return true;
   }

   if ($verbose) {
      echo "Installed, but not configured";
   }
   return false;
}

Je modifie maintenant le HOOK.PHP

<?php
/**
 * Install hook
 *
 * @return boolean
 */
function plugin_myexampleplugin_install() {
   global $DB;

   //instanciate migration with version
   $migration = new Migration(100);

   //Create table only if it does not exists yet!
   if (!$DB->tableExists('glpi_plugin_myexampleplugin_configs')) {
      //table creation query
      $query = "CREATE TABLE `glpi_plugin_myexampleplugin_configs` (
                  `id` INT(11) NOT NULL autoincrement,
                  `name` VARCHAR(255) NOT NULL,
                  PRIMARY KEY  (`id`)
               ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
      $DB->queryOrDie($query, $DB->error());
   }

   if ($DB->tableExists('glpi_plugin_myexampleplugin_configs')) {
      //missed value for configuration
      $migration->addField(
         'glpi_plugin_myexampleplugin_configs',
         'value',
         'string'
      );

      $migration->addKey(
         'glpi_plugin_myexampleplugin_configs',
         'name'
      );
   }

   //execute the whole migration
   $migration->executeMigration();

   return true;
}

/**
 * Uninstall hook
 *
 * @return boolean
 */
function plugin_myexampleplugin_uninstall() {
   global $DB;

   $tables = [
      'configs'
   ];

   foreach ($tables as $table) {
      $tablename = 'glpi_plugin_myexampleplugin_' . $table;
      //Create table only if it does not exists yet!
      if ($DB->tableExists($tablename)) {
         $DB->queryOrDie(
            "DROP TABLE `$tablename`",
            $DB->error()
         );
      }
   }

   return true;
}

Ensuite je continue de faire comme dans la doc, je créer le dossier inc a la racine de mon plugin, et je créer le fichier ; MYOBJECT.CLASS.PHP

<?php
class PluginMyExampleMyObject extends CommonDBTM {
   public function showForm($ID, $options = []) {
      global $CFG_GLPI;

      $this->initForm($ID, $options);
      $this->showFormHeader($options);

      if (!isset($options['display'])) {
         //display per default
         $options['display'] = true;
      }

      $params = $options;
      //do not display called elements per default; they'll be displayed or returned here
      $params['display'] = false;

      $out = '<tr>';
      $out .= '<th>' . __('My label', 'myexampleplugin') . '</th>'

      $objectName = autoName(
         $this->fields["name"],
         "name",
         (isset($options['withtemplate']) && $options['withtemplate']==2),
         $this->getType(),
         $this->fields["entities_id"]
      );

      $out .= '<td>';
      $out .= Html::autocompletionTextField(
         $this,
         'name',
         [
            'value'     => $objectName,
            'display'   => false
         ]
      );
      $out .= '</td>';

      $out .= $this->showFormButtons($params);

      if ($options['display'] == true) {
         echo $out;
      } else {
         return $out;
      }
   }
}

Ensuite je créer le dossier front à la racine et je créer le fichier MYOBJECT.PHP

<?php
include ("../../../inc/includes.php");

// Check if plugin is activated...
$plugin = new Plugin();
if (!$plugin->isInstalled('myexampleplugin') || !$plugin->isActivated('myexampleplugin')) {
   Html::displayNotFoundError();
}

//check for ACLs
if (PluginMyExampleMyObject::canView()) {
   //View is granted: display the list.

   //Add page header
   Html::header(
      __('My example plugin', 'myexampleplugin'),
      $_SERVER['PHP_SELF'],
      'assets',
      'pluginmyexamplemyobject',
      'myobject'
   );

   Search::show('PluginMyExampleMyObject');

   Html::footer();
} else {
   //View is not granted.
   Html::displayRightError();
}

Et j'ajoute toujours dans le dossier front MYOBJECT.FORM.PHP

<?php
include ("../../../inc/includes.php");

// Check if plugin is activated...
$plugin = new Plugin();
if (!$plugin->isInstalled('myexampleplugin') || !$plugin->isActivated('myexampleplugin')) {
   Html::displayNotFoundError();
}

$object = new PluginMyExampleMyObject();

if (isset($_POST['add'])) {
   //Check CREATE ACL
   $object->check(-1, CREATE, $_POST);
   //Do object creation
   $newid = $object->add($_POST);
   //Redirect to newly created object form
   Html::redirect("{$CFG_GLPI['root_doc']}/plugins/front/myobject.form.php?id=$newid");
} else if (isset($_POST['update'])) {
   //Check UPDATE ACL
   $object->check($_POST['id'], UPDATE);
   //Do object update
   $object->update($_POST);
   //Redirect to object form
   Html::back();
} else if (isset($_POST['delete'])) {
   //Check DELETE ACL
   $object->check($_POST['id'], DELETE);
   //Put object in dustbin
   $object->delete($_POST);
   //Redirect to objects list
   $object->redirectToList();
} else if (isset($_POST['purge'])) {
   //Check PURGE ACL
   $object->check($_POST['id'], PURGE);
   //Do object purge
   $object->delete($_POST, 1);
   //Redirect to objects list
   Html::redirect("{$CFG_GLPI['root_doc']}/plugins/front/myobject.php");
} else {
   //per default, display object
   $withtemplate = (isset($_GET['withtemplate']) ? $_GET['withtemplate'] : 0);
   $object->display(
      [
         'id'           => $_GET['id'],
         'withtemplate' => $withtemplate
      ]
   );
}

Creative Commons License

Tout ceci sort de la documentation je n'invente rien, quand je veux afficher le page du formulaire GLPI me met élément introuvable.
Je vous remercie de la lecture et de l'aide apporté.

Offline

Board footer

Powered by FluxBB