Coding standard (yllen)

git-svn-id: https://forge.glpi-project.org/svn/example/trunk@117 349b9182-4a13-0410-896f-e5e9767dd1b3
This commit is contained in:
remicollet 2009-12-13 17:07:33 +00:00
parent c16272abf4
commit 41a5750e6f
6 changed files with 629 additions and 628 deletions

309
hook.php
View File

@ -48,12 +48,11 @@ function plugin_change_profile_example() {
} else { } else {
unset($_SESSION["glpi_plugin_example_profile"]); unset($_SESSION["glpi_plugin_example_profile"]);
} }
} }
// Define dropdown relations // Define dropdown relations
function plugin_example_getDatabaseRelations() { function plugin_example_getDatabaseRelations() {
//
return array("glpi_plugin_example_dropdowns" => array("glpi_plugin_example" => "plugin_example_dropdowns_id")); return array("glpi_plugin_example_dropdowns" => array("glpi_plugin_example" => "plugin_example_dropdowns_id"));
} }
@ -71,17 +70,17 @@ function plugin_example_getAddSearchOptions($itemtype){
global $LANG; global $LANG;
$sopt = array(); $sopt = array();
if ($itemtype == COMPUTER_TYPE) { if ($itemtype == 'Computer') {
// Just for example, not working... // Just for example, not working...
$sopt[1001]['table'] = 'glpi_plugin_example_dropdowns'; $sopt[1001]['table'] = 'glpi_plugin_example_dropdowns';
$sopt[1001]['field'] = 'name'; $sopt[1001]['field'] = 'name';
$sopt[1001]['linkfield'] = 'plugin_example_dropdowns_id'; $sopt[1001]['linkfield'] = 'plugin_example_dropdowns_id';
$sopt[1001]['name'] = 'Example plugin'; $sopt[1001]['name'] = 'Example plugin';
} }
return $sopt; return $sopt;
} }
function plugin_example_giveItem($type,$ID,$data,$num) { function plugin_example_giveItem($type,$ID,$data,$num) {
$searchopt = &Search::getOptions($type); $searchopt = &Search::getOptions($type);
@ -90,40 +89,41 @@ function plugin_example_giveItem($type,$ID,$data,$num){
switch ($table.'.'.$field) { switch ($table.'.'.$field) {
case "glpi_plugin_example_examples.name" : case "glpi_plugin_example_examples.name" :
$out= "<a href=\"".getItemTypeFormURL('PluginExampleExample')."?id=".$data['id']."\">"; $out = "<a href='".getItemTypeFormURL('PluginExampleExample')."?id=".$data['id']."'>";
$out .= $data["ITEM_$num"]; $out .= $data["ITEM_$num"];
if ($_SESSION["glpiis_ids_visible"]||empty($data["ITEM_$num"])) $out.= " (".$data["id"].")"; if ($_SESSION["glpiis_ids_visible"] || empty($data["ITEM_$num"])) {
$out .= " (".$data["id"].")";
}
$out .= "</a>"; $out .= "</a>";
return $out; return $out;
break;
} }
return ""; return "";
} }
function plugin_example_addLeftJoin($type,$ref_table,$new_table,$linkfield) { function plugin_example_addLeftJoin($type,$ref_table,$new_table,$linkfield) {
// Example of standard LEFT JOIN clause but use it ONLY for specific LEFT JOIN // Example of standard LEFT JOIN clause but use it ONLY for specific LEFT JOIN
// No need of the function if you do not have specific cases // No need of the function if you do not have specific cases
switch ($new_table) { switch ($new_table) {
case "glpi_plugin_example_dropdowns" : case "glpi_plugin_example_dropdowns" :
return " LEFT JOIN $new_table ON ($ref_table.$linkfield = $new_table.id) "; return " LEFT JOIN `$new_table` ON (`$ref_table`.`$linkfield` = `$new_table`.`id`) ";
break;
} }
return ""; return "";
} }
function plugin_example_forceGroupBy($type) { function plugin_example_forceGroupBy($type) {
switch ($type) { switch ($type) {
case 'PluginExampleExample' : case 'PluginExampleExample' :
// Force add GROUP BY IN REQUEST // Force add GROUP BY IN REQUEST
return true; return true;
break;
} }
return false; return false;
} }
function plugin_example_addWhere($link,$nott,$type,$ID,$val) { function plugin_example_addWhere($link,$nott,$type,$ID,$val) {
$searchopt = &Search::getOptions($type); $searchopt = &Search::getOptions($type);
@ -138,14 +138,14 @@ function plugin_example_addWhere($link,$nott,$type,$ID,$val){
// case "glpi_plugin_example.name" : // case "glpi_plugin_example.name" :
// $ADD = ""; // $ADD = "";
// if ($nott && $val!="NULL") { // if ($nott && $val!="NULL") {
// $ADD=" OR $table.$field IS NULL"; // $ADD = " OR `$table`.`$field` IS NULL";
// } // }
// return $link." ($table.$field $SEARCH ".$ADD." ) "; // return $link." (`$table`.`$field` $SEARCH ".$ADD." ) ";
// break;
// } // }
return ""; return "";
} }
// This is not a real example because the use of Having condition in this case is not suitable // This is not a real example because the use of Having condition in this case is not suitable
function plugin_example_addHaving($link,$nott,$type,$ID,$val,$num) { function plugin_example_addHaving($link,$nott,$type,$ID,$val,$num) {
@ -160,18 +160,16 @@ function plugin_example_addHaving($link,$nott,$type,$ID,$val,$num){
switch ($table.".".$field) { switch ($table.".".$field) {
case "glpi_plugin_example.serial" : case "glpi_plugin_example.serial" :
$ADD = ""; $ADD = "";
if (($nott&&$val!="NULL")||$val=='^$') { if (($nott && $val!="NULL")
|| $val == '^$') {
$ADD = " OR ITEM_$num IS NULL"; $ADD = " OR ITEM_$num IS NULL";
} }
return " $LINK ( ITEM_".$num.$SEARCH." $ADD ) "; return " $LINK ( ITEM_".$num.$SEARCH." $ADD ) ";
break;
} }
return ""; return "";
} }
function plugin_example_addSelect($type,$ID,$num) { function plugin_example_addSelect($type,$ID,$num) {
$searchopt = &Search::getOptions($type); $searchopt = &Search::getOptions($type);
@ -183,11 +181,11 @@ function plugin_example_addSelect($type,$ID,$num){
// switch ($table.".".$field) { // switch ($table.".".$field) {
// case "glpi_plugin_example.name" : // case "glpi_plugin_example.name" :
// return $table.".".$field." AS ITEM_$num, "; // return $table.".".$field." AS ITEM_$num, ";
// break;
// } // }
return ""; return "";
} }
function plugin_example_addOrderBy($type,$ID,$order,$key=0) { function plugin_example_addOrderBy($type,$ID,$order,$key=0) {
$searchopt = &Search::getOptions($type); $searchopt = &Search::getOptions($type);
@ -199,53 +197,52 @@ function plugin_example_addOrderBy($type,$ID,$order,$key=0){
// switch ($table.".".$field) { // switch ($table.".".$field) {
// case "glpi_plugin_example.name" : // case "glpi_plugin_example.name" :
// return " ORDER BY $table.$field $order "; // return " ORDER BY $table.$field $order ";
// break;
// } // }
return ""; return "";
} }
////////////////////////////// //////////////////////////////
////// SPECIFIC MODIF MASSIVE FUNCTIONS /////// ////// SPECIFIC MODIF MASSIVE FUNCTIONS ///////
// Define actions : // Define actions :
function plugin_example_MassiveActions($type) { function plugin_example_MassiveActions($type) {
global $LANG; global $LANG;
switch ($type) { switch ($type) {
// New action for core and other plugin types : name = plugin_PLUGINNAME_actionname // New action for core and other plugin types : name = plugin_PLUGINNAME_actionname
case COMPUTER_TYPE : case 'Computer' :
return array( return array("plugin_example_DoIt" => "plugin_example_DoIt");
"plugin_example_DoIt"=>"plugin_example_DoIt",
);
break;
// Actions for types provided by the plugin // Actions for types provided by the plugin
case 'PluginExampleExample' : case 'PluginExampleExample' :
return array( return array("add_document" => $LANG["document"][16], // GLPI core one
// GLPI core one "do_nothing" => 'Do Nothing - just for fun'); // Specific one
"add_document"=>$LANG["document"][16],
// Specific one
"do_nothing"=>'Do Nothing - just for fun'
);
break;
} }
return array(); return array();
} }
// How to display specific actions ? // How to display specific actions ?
function plugin_example_MassiveActionsDisplay($type,$action) { function plugin_example_MassiveActionsDisplay($type,$action) {
global $LANG; global $LANG;
switch ($type) { switch ($type) {
case COMPUTER_TYPE: case 'Computer' :
switch ($action) { switch ($action) {
case "plugin_example_DoIt" : case "plugin_example_DoIt" :
echo "&nbsp;<input type=\"submit\" name=\"massiveaction\" class=\"submit\" value=\"".$LANG["buttons"][2]."\" >&nbsp;but do nothing :)"; echo "&nbsp;<input type='submit' name='massiveaction' class='submit' value='".
$LANG["buttons"][2]."'>&nbsp;but do nothing :)";
break; break;
} }
break; break;
case 'PluginExampleExample' : case 'PluginExampleExample' :
switch ($action) { switch ($action) {
// No case for add_document : use GLPI core one // No case for add_document : use GLPI core one
case "do_nothing" : case "do_nothing" :
echo "&nbsp;<input type=\"submit\" name=\"massiveaction\" class=\"submit\" value=\"".$LANG["buttons"][2]."\" >&nbsp;but do nothing :)"; echo "&nbsp;<input type='submit' name='massiveaction' class='submit' value='".
$LANG["buttons"][2]."'>&nbsp;but do nothing :)";
break; break;
} }
break; break;
@ -253,11 +250,11 @@ function plugin_example_MassiveActionsDisplay($type,$action){
return ""; return "";
} }
// How to process specific actions ? // How to process specific actions ?
function plugin_example_MassiveActionsProcess($data) { function plugin_example_MassiveActionsProcess($data) {
global $LANG; global $LANG;
switch ($data['action']) { switch ($data['action']) {
case 'plugin_example_DoIt' : case 'plugin_example_DoIt' :
if ($data['itemtype'] == 'Computer') { if ($data['itemtype'] == 'Computer') {
@ -273,6 +270,7 @@ function plugin_example_MassiveActionsProcess($data){
} }
} }
break; break;
case 'do_nothing' : case 'do_nothing' :
if ($data['itemtype'] == 'PluginExampleExample') { if ($data['itemtype'] == 'PluginExampleExample') {
$ex = new PluginExampleExample(); $ex = new PluginExampleExample();
@ -289,6 +287,8 @@ function plugin_example_MassiveActionsProcess($data){
break; break;
} }
} }
// How to display specific update fields ? // How to display specific update fields ?
function plugin_example_MassiveActionsFieldsDisplay($type,$table,$field,$linkfield) { function plugin_example_MassiveActionsFieldsDisplay($type,$table,$field,$linkfield) {
@ -301,7 +301,6 @@ function plugin_example_MassiveActionsFieldsDisplay($type,$table,$field,$linkfie
// Dropdown::showYesNo($linkfield); // Dropdown::showYesNo($linkfield);
// Need to return true if specific display // Need to return true if specific display
return true; return true;
break;
} }
} else { } else {
@ -313,24 +312,26 @@ function plugin_example_MassiveActionsFieldsDisplay($type,$table,$field,$linkfie
//dropdownUsers($linkfield,0,"own_ticket",0,1,$_SESSION["glpiactive_entity"]); //dropdownUsers($linkfield,0,"own_ticket",0,1,$_SESSION["glpiactive_entity"]);
// Need to return true if specific display // Need to return true if specific display
return true; return true;
break;
} }
} }
// Need to return false on non display item // Need to return false on non display item
return false; return false;
} }
////////////////////////////// //////////////////////////////
// Hook done on before update item case // Hook done on before update item case
function plugin_pre_item_update_example($input) { function plugin_pre_item_update_example($input) {
if (isset($input["_item_type_"]))
if (isset($input["_item_type_"])) {
switch ($input["_item_type_"]) { switch ($input["_item_type_"]) {
case COMPUTER_TYPE : case 'Computer' :
// Manipulate data if needed // Manipulate data if needed
addMessageAfterRedirect("Pre Update Computer Hook",true); addMessageAfterRedirect("Pre Update Computer Hook",true);
break; break;
} }
}
return $input; return $input;
} }
@ -338,121 +339,143 @@ function plugin_pre_item_update_example($input){
// Hook done on update item case // Hook done on update item case
function plugin_item_update_example($parm) { function plugin_item_update_example($parm) {
if (isset($parm["type"])) if (isset($parm["type"])) {
switch ($parm["type"]){ switch ($parm["type"]){
case COMPUTER_TYPE : case 'Computer' :
addMessageAfterRedirect("Update Computer Hook",true); addMessageAfterRedirect("Update Computer Hook",true);
return true; return true;
break; }
} }
return false; return false;
} }
// Hook done on before add item case // Hook done on before add item case
function plugin_pre_item_add_example($input) { function plugin_pre_item_add_example($input) {
if (isset($input["_item_type_"]))
if (isset($input["_item_type_"])) {
switch ($input["_item_type_"]) { switch ($input["_item_type_"]) {
case COMPUTER_TYPE : case 'Computer' :
// Manipulate data if needed // Manipulate data if needed
addMessageAfterRedirect("Pre Add Computer Hook",true); addMessageAfterRedirect("Pre Add Computer Hook",true);
break; break;
} }
}
return $input; return $input;
} }
// Hook done on add item case // Hook done on add item case
function plugin_item_add_example($parm) { function plugin_item_add_example($parm) {
if (isset($parm["type"])) if (isset($parm["type"])) {
switch ($parm["type"]) { switch ($parm["type"]) {
case COMPUTER_TYPE : case 'Computer' :
addMessageAfterRedirect("Add Computer Hook",true); addMessageAfterRedirect("Add Computer Hook",true);
return true; return true;
break; }
} }
return false; return false;
} }
// Hook done on before delete item case // Hook done on before delete item case
function plugin_pre_item_delete_example($input) { function plugin_pre_item_delete_example($input) {
if (isset($input["_item_type_"]))
if (isset($input["_item_type_"])) {
switch ($input["_item_type_"]) { switch ($input["_item_type_"]) {
case COMPUTER_TYPE : case 'Computer' :
// Manipulate data if needed // Manipulate data if needed
addMessageAfterRedirect("Pre Delete Computer Hook",true); addMessageAfterRedirect("Pre Delete Computer Hook",true);
break; break;
} }
}
return $input; return $input;
} }
// Hook done on delete item case // Hook done on delete item case
function plugin_item_delete_example($parm) { function plugin_item_delete_example($parm) {
if (isset($parm["type"])) if (isset($parm["type"])) {
switch ($parm["type"]) { switch ($parm["type"]) {
case COMPUTER_TYPE : case 'Computer' :
addMessageAfterRedirect("Delete Computer Hook",true); addMessageAfterRedirect("Delete Computer Hook",true);
return true; return true;
break; }
} }
return false; return false;
} }
// Hook done on before purge item case // Hook done on before purge item case
function plugin_pre_item_purge_example($input) { function plugin_pre_item_purge_example($input) {
if (isset($input["_item_type_"]))
if (isset($input["_item_type_"])) {
switch ($input["_item_type_"]) { switch ($input["_item_type_"]) {
case COMPUTER_TYPE : case 'Computer' :
// Manipulate data if needed // Manipulate data if needed
addMessageAfterRedirect("Pre Purge Computer Hook",true); addMessageAfterRedirect("Pre Purge Computer Hook",true);
break; break;
} }
}
return $input; return $input;
} }
// Hook done on purge item case // Hook done on purge item case
function plugin_item_purge_example($parm) { function plugin_item_purge_example($parm) {
if (isset($parm["type"])) if (isset($parm["type"])) {
switch ($parm["type"]) { switch ($parm["type"]) {
case COMPUTER_TYPE : case 'Computer' :
addMessageAfterRedirect("Purge Computer Hook",true); addMessageAfterRedirect("Purge Computer Hook",true);
return true; return true;
break; }
} }
return false; return false;
} }
// Hook done on before restore item case // Hook done on before restore item case
function plugin_pre_item_restore_example($input) { function plugin_pre_item_restore_example($input) {
if (isset($input["_item_type_"]))
if (isset($input["_item_type_"])) {
switch ($input["_item_type_"]) { switch ($input["_item_type_"]) {
case COMPUTER_TYPE : case 'Computer' :
// Manipulate data if needed // Manipulate data if needed
addMessageAfterRedirect("Pre Restore Computer Hook"); addMessageAfterRedirect("Pre Restore Computer Hook");
break; break;
} }
}
return $input; return $input;
} }
// Hook done on restore item case // Hook done on restore item case
function plugin_item_restore_example($parm) { function plugin_item_restore_example($parm) {
if (isset($parm["type"])) if (isset($parm["type"])) {
switch ($parm["type"]) { switch ($parm["type"]) {
case COMPUTER_TYPE : case 'Computer' :
addMessageAfterRedirect("Restore Computer Hook"); addMessageAfterRedirect("Restore Computer Hook");
return true; return true;
break; }
} }
return false; return false;
} }
// Hook done on restore item case // Hook done on restore item case
function plugin_item_transfer_example($parm) { function plugin_item_transfer_example($parm) {
addMessageAfterRedirect("Transfer Computer Hook ".$parm['type']." ".$parm['id']." -> ".$parm['newID']); addMessageAfterRedirect("Transfer Computer Hook ".$parm['type']." ".$parm['id']." -> ".
$parm['newID']);
return false; return false;
} }
// Parm contains begin, end and who // Parm contains begin, end and who
// Create data to be displayed in the planning of $parm["who"] or $parm["who_group"] between $parm["begin"] and $parm["end"] // Create data to be displayed in the planning of $parm["who"] or $parm["who_group"] between $parm["begin"] and $parm["end"]
@ -462,7 +485,6 @@ function plugin_planning_populate_example($parm){
// Items need to have an unique index beginning by the begin date of the item to display // Items need to have an unique index beginning by the begin date of the item to display
// needed to be correcly displayed // needed to be correcly displayed
$parm["items"][$parm["begin"]."$$$"."plugin_example1"]["plugin"] = "example"; $parm["items"][$parm["begin"]."$$$"."plugin_example1"]["plugin"] = "example";
$parm["items"][$parm["begin"]."$$$"."plugin_example1"]["begin"] = date("Y-m-d 17:00:00"); $parm["items"][$parm["begin"]."$$$"."plugin_example1"]["begin"] = date("Y-m-d 17:00:00");
$parm["items"][$parm["begin"]."$$$"."plugin_example1"]["end"] = date("Y-m-d 18:00:00"); $parm["items"][$parm["begin"]."$$$"."plugin_example1"]["end"] = date("Y-m-d 18:00:00");
@ -474,124 +496,99 @@ function plugin_planning_populate_example($parm){
return $parm; return $parm;
} }
// Display the planning item // Display the planning item
function plugin_display_planning_example($parm) { function plugin_display_planning_example($parm) {
global $LANG;
// $parm["type"] say begin end in or from type // $parm["type"] say begin end in or from type
// Add items in the items fields of the parm array // Add items in the items fields of the parm array
global $LANG;
switch ($parm["type"]) { switch ($parm["type"]) {
case "in" : case "in" :
echo date("H:i",strtotime($parm["begin"]))." -> ".date("H:i",strtotime($parm["end"])).": "; echo date("H:i",strtotime($parm["begin"]))." -> ".date("H:i",strtotime($parm["end"])).": ";
break; break;
case "from" : case "from" :
break; break;
case "begin";
case "begin" :
echo $LANG["buttons"][33]." ".date("H:i",strtotime($parm["begin"])).": "; echo $LANG["buttons"][33]." ".date("H:i",strtotime($parm["begin"])).": ";
break; break;
case "end";
case "end" :
echo $LANG["buttons"][32]." ".date("H:i",strtotime($parm["end"])).": "; echo $LANG["buttons"][32]." ".date("H:i",strtotime($parm["end"])).": ";
break; break;
} }
echo $parm["name"]; echo $parm["name"];
} }
// Define headings added by the plugin // Define headings added by the plugin
function plugin_get_headings_example($item, $withtemplate) { function plugin_get_headings_example($item, $withtemplate) {
switch (get_class($item)) { switch (get_class($item)) {
case 'Profile' : case 'Profile' :
$prof = new Profile(); $prof = new Profile();
if ($item->fields['interface'] == 'central') { if ($item->fields['interface'] == 'central') {
return array( return array(1 => "Test PLugin");
1 => "Test PLugin",
);
} else { } else {
return array(); return array();
} }
break;
case 'Computer' : case 'Computer' :
// new object / template case // new object / template case
if ($withtemplate) { if ($withtemplate) {
return array(); return array();
// Non template case / editing an existing object // Non template case / editing an existing object
} else { } else {
return array( return array(1 => "Test PLugin");
1 => "Test PLugin",
);
} }
break;
case 'ComputerDisk' : case 'ComputerDisk' :
case 'Supplier' : case 'Supplier' :
if ($item->getField('id')) { // Not in create mode if ($item->getField('id')) { // Not in create mode
return array( return array(1 => "Test PLugin",
1 => "Test PLugin", 2 => "Test PLugin 2");
2 => "Test PLugin 2",
);
} }
break; break;
case 'Central':
return array(
1 => "Test PLugin",
);
break;
case 'Preference':
return array(
1 => "Test PLugin",
);
break;
case 'Notification':
return array(
1 => "Test PLugin",
);
break;
case 'Central' :
case 'Preference':
case 'Notification':
return array(1 => "Test PLugin");
} }
return false; return false;
} }
// Define headings actions added by the plugin // Define headings actions added by the plugin
function plugin_headings_actions_example($item) { function plugin_headings_actions_example($item) {
switch (get_class($item)) { switch (get_class($item)) {
case 'Profile' : case 'Profile' :
case 'Computer' : case 'Computer' :
return array( return array(1 => "plugin_headings_example");
1 => "plugin_headings_example",
);
break;
case 'ComputerDisk' : case 'ComputerDisk' :
case 'Supplier' : case 'Supplier' :
return array( return array(1 => "plugin_headings_example",
1 => "plugin_headings_example", 2 => "plugin_headings_example");
2 => "plugin_headings_example",
);
break;
case 'Central' : case 'Central' :
return array(
1 => "plugin_headings_example",
);
break;
case 'Preference' : case 'Preference' :
return array(
1 => "plugin_headings_example",
);
break;
case 'Notification' : case 'Notification' :
return array( return array(1 => "plugin_headings_example");
1 => "plugin_headings_example",
);
break;
} }
return false; return false;
} }
// Example of an action heading // Example of an action heading
function plugin_headings_example($item, $withtemplate=0) { function plugin_headings_example($item, $withtemplate=0) {
global $LANG; global $LANG;
if (!$withtemplate) { if (!$withtemplate) {
echo "<div align='center'>"; echo "<div class='center'>";
switch (get_class($item)) { switch (get_class($item)) {
case 'Central' : case 'Central' :
echo "Plugin central action ".$LANG['plugin_example']["test"]; echo "Plugin central action ".$LANG['plugin_example']["test"];
@ -603,12 +600,11 @@ function plugin_headings_example($item, $withtemplate=0){
echo "<form action='Where to post form'>"; echo "<form action='Where to post form'>";
echo "<table class='tab_cadre_fixe'>"; echo "<table class='tab_cadre_fixe'>";
echo "<tr><th colspan='3'>".$data['name']; echo "<tr><th colspan='3'>".$data['name']." - ".$data['version'];
echo " - ".$data['version'];
echo "</th></tr>"; echo "</th></tr>";
echo "<tr class='tab_bg_1'><td>Name of the pref"; echo "<tr class='tab_bg_1'><td>Name of the pref</td>";
echo "</td><td>Input to set the pref</td>"; echo "<td>Input to set the pref</td>";
echo "<td><input class='submit' type='submit' name='submit' value='submit'></td>"; echo "<td><input class='submit' type='submit' name='submit' value='submit'></td>";
echo "</tr>"; echo "</tr>";
@ -622,13 +618,14 @@ function plugin_headings_example($item, $withtemplate=0){
break; break;
default : default :
echo "Plugin function with headings CLASS=".get_class($item)." ID=".$item->getField('id'); echo "Plugin function with headings CLASS=".get_class($item)." id=".$item->getField('id');
break; break;
} }
echo "</div>"; echo "</div>";
} }
} }
/** /**
* Execute 1 task manage by the plugin * Execute 1 task manage by the plugin
* *
@ -640,12 +637,14 @@ function plugin_headings_example($item, $withtemplate=0){
* 0 : nothing to do * 0 : nothing to do
*/ */
function plugin_example_cron_sample1_run($task) { function plugin_example_cron_sample1_run($task) {
$task->log("Example log message from hook"); $task->log("Example log message from hook");
$task->setVolume(mt_rand(0,$task->fields['param'])); $task->setVolume(mt_rand(0,$task->fields['param']));
return 1; return 1;
} }
/** /**
* Give localized information about 1 task * Give localized information about 1 task
* *
@ -658,10 +657,8 @@ function plugin_example_cron_info($name) {
switch ($name) { switch ($name) {
case 'sample1' : case 'sample1' :
return array ( return array('description' => $LANG['plugin_example']['test'] . " (hook)", // Mandatory
'description' => $LANG['plugin_example']['test'] . " (hook)", // Mandatory
'parameter' => $LANG['plugin_example']['test']); // Optional 'parameter' => $LANG['plugin_example']['test']); // Optional
break;
} }
return array(); return array();
} }
@ -669,6 +666,7 @@ function plugin_example_cron_info($name) {
// Do special actions for dynamic report // Do special actions for dynamic report
function plugin_example_dynamicReport($parm) { function plugin_example_dynamicReport($parm) {
if ($parm["item_type"] == 'PluginExampleExample') { if ($parm["item_type"] == 'PluginExampleExample') {
// Do all what you want for export depending on $parm // Do all what you want for export depending on $parm
echo "Personalized export for type ".$parm["display_type"]; echo "Personalized export for type ".$parm["display_type"];
@ -684,58 +682,65 @@ function plugin_example_dynamicReport($parm){
return false; return false;
} }
// Add parameters to printPager in search system // Add parameters to printPager in search system
function plugin_example_addParamFordynamicReport($device_type){ function plugin_example_addParamFordynamicReport($itemtype) {
if ($device_type=='PluginExampleExample'){
if ($itemtype == 'PluginExampleExample') {
// Return array data containing all params to add : may be single data or array data // Return array data containing all params to add : may be single data or array data
// Search config are available from session variable // Search config are available from session variable
return array( return array('add1' => $_SESSION['glpisearch'][$itemtype]['order'],
'add1' => $_SESSION['glpisearch'][$device_type]['order'], 'add2' => array('tutu'=>'Second Add',
'add2' => array('tutu'=>'Second Add','Other Data')); 'Other Data'));
} }
// Return false or a non array data if not needed // Return false or a non array data if not needed
return false; return false;
} }
// Install process for plugin : need to return true if succeeded // Install process for plugin : need to return true if succeeded
function plugin_example_install() { function plugin_example_install() {
global $DB; global $DB;
if (!TableExists("glpi_plugin_example_examples")) { if (!TableExists("glpi_plugin_example_examples")) {
$query = "CREATE TABLE `glpi_plugin_example_examples` ( $query = "CREATE TABLE `glpi_plugin_example_examples` (
`id` int(11) NOT NULL auto_increment, `id` int(11) NOT NULL auto_increment,
`name` varchar(255) collate utf8_unicode_ci default NULL, `name` varchar(255) collate utf8_unicode_ci default NULL,
`serial` varchar(255) collate utf8_unicode_ci NOT NULL, `serial` varchar(255) collate utf8_unicode_ci NOT NULL,
`plugin_example_dropdowns_id` int(11) NOT NULL default '0', `plugin_example_dropdowns_id` int(11) NOT NULL default '0',
`is_deleted` smallint(6) NOT NULL default '0', `is_deleted` tinyint(1) NOT NULL default '0',
`is_template` smallint(6) NOT NULL default '0', `is_template` tinyint(1) NOT NULL default '0',
`template_name` varchar(255) collate utf8_unicode_ci default NULL, `template_name` varchar(255) collate utf8_unicode_ci default NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
";
$DB->query($query) or die("error creating glpi_plugin_example_examples ". $DB->error()); $DB->query($query) or die("error creating glpi_plugin_example_examples ". $DB->error());
$query="INSERT INTO `glpi_plugin_example_examples` (`id`, `name`, `serial`, `plugin_example_dropdowns_id`,
`is_deleted`, `is_template`, `template_name`) VALUES $query = "INSERT INTO `glpi_plugin_example_examples`
(1, 'example 1', 'serial 1', 1, 0, 0, NULL), (`id`, `name`, `serial`, `plugin_example_dropdowns_id`, `is_deleted`,
`is_template`, `template_name`)
VALUES (1, 'example 1', 'serial 1', 1, 0, 0, NULL),
(2, 'example 2', 'serial 2', 2, 0, 0, NULL), (2, 'example 2', 'serial 2', 2, 0, 0, NULL),
(3, 'example 3', 'serial 3', 1, 0, 0, NULL);"; (3, 'example 3', 'serial 3', 1, 0, 0, NULL)";
$DB->query($query) or die("error populate glpi_plugin_example ". $DB->error()); $DB->query($query) or die("error populate glpi_plugin_example ". $DB->error());
} }
if (!TableExists("glpi_plugin_example_dropdowns")){
if (!TableExists("glpi_plugin_example_dropdowns")) {
$query = "CREATE TABLE `glpi_plugin_example_dropdowns` ( $query = "CREATE TABLE `glpi_plugin_example_dropdowns` (
`id` int(11) NOT NULL auto_increment, `id` int(11) NOT NULL auto_increment,
`name` varchar(255) collate utf8_unicode_ci default NULL, `name` varchar(255) collate utf8_unicode_ci default NULL,
`comment` text collate utf8_unicode_ci, `comment` text collate utf8_unicode_ci,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `name` (`name`) KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->query($query) or die("error creating glpi_plugin_example_dropdowns". $DB->error()); $DB->query($query) or die("error creating glpi_plugin_example_dropdowns". $DB->error());
$query="INSERT INTO `glpi_plugin_example_dropdowns` (`id`, `name`, `comment`) VALUES
(1, 'dp 1', 'comment 1'), $query = "INSERT INTO `glpi_plugin_example_dropdowns`
(2, 'dp2', 'comment 2');"; (`id`, `name`, `comment`)
VALUES (1, 'dp 1', 'comment 1'),
(2, 'dp2', 'comment 2')";
$DB->query($query) or die("error populate glpi_plugin_example_dropdowns". $DB->error()); $DB->query($query) or die("error populate glpi_plugin_example_dropdowns". $DB->error());
} }
@ -748,22 +753,23 @@ function plugin_example_install(){
return true; return true;
} }
// Uninstall process for plugin : need to return true if succeeded // Uninstall process for plugin : need to return true if succeeded
function plugin_example_uninstall() { function plugin_example_uninstall() {
global $DB; global $DB;
// Old version tables // Old version tables
if (TableExists("glpi_dropdown_plugin_example")) { if (TableExists("glpi_dropdown_plugin_example")) {
$query="DROP TABLE `glpi_dropdown_plugin_example`;"; $query = "DROP TABLE `glpi_dropdown_plugin_example`";
$DB->query($query) or die("error deleting glpi_dropdown_plugin_example"); $DB->query($query) or die("error deleting glpi_dropdown_plugin_example");
} }
if (TableExists("glpi_plugin_example")) { if (TableExists("glpi_plugin_example")) {
$query="DROP TABLE `glpi_plugin_example`;"; $query = "DROP TABLE `glpi_plugin_example`";
$DB->query($query) or die("error deleting glpi_plugin_example"); $DB->query($query) or die("error deleting glpi_plugin_example");
} }
// Current version tables // Current version tables
if (TableExists("glpi_plugin_example_example")) { if (TableExists("glpi_plugin_example_example")) {
$query="DROP TABLE `glpi_plugin_example_example`;"; $query = "DROP TABLE `glpi_plugin_example_example`";
$DB->query($query) or die("error deleting glpi_plugin_example_example"); $DB->query($query) or die("error deleting glpi_plugin_example_example");
} }
if (TableExists("glpi_plugin_example_dropdowns")) { if (TableExists("glpi_plugin_example_dropdowns")) {
@ -773,8 +779,9 @@ function plugin_example_uninstall(){
return true; return true;
} }
function plugin_example_AssignToTicket($types)
{ function plugin_example_AssignToTicket($types) {
$types['PluginExampleExample'] = "Example"; $types['PluginExampleExample'] = "Example";
return $types; return $types;
} }

View File

@ -42,8 +42,6 @@ class PluginExampleExample extends CommonDBTM {
// Should return the localized name of the type // Should return the localized name of the type
static function getTypeName() { static function getTypeName() {
global $LANG;
return 'Example Type'; return 'Example Type';
} }
@ -104,10 +102,8 @@ class PluginExampleExample extends CommonDBTM {
switch ($name) { switch ($name) {
case 'sample2' : case 'sample2' :
return array ( return array('description' => $LANG['plugin_example']['test']." (class)",
'description' => $LANG['plugin_example']['test']." (class)",
'parameter' => $LANG['plugin_example']['test']); 'parameter' => $LANG['plugin_example']['test']);
break;
} }
return array(); return array();
} }
@ -123,6 +119,7 @@ class PluginExampleExample extends CommonDBTM {
* 0 : nothing to do * 0 : nothing to do
*/ */
static function cronSample2($task) { static function cronSample2($task) {
$task->log("Example log message from class"); $task->log("Example log message from class");
$task->setVolume(mt_rand(0,10)); $task->setVolume(mt_rand(0,10));

View File

@ -33,6 +33,6 @@
// Purpose of file: // Purpose of file:
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
$LANG['plugin_example']["test"]="test english"; $LANG['plugin_example']["test"] = "english test";
$LANG['plugin_example']["name"] = "Name"; $LANG['plugin_example']["name"] = "Name";
?> ?>

View File

@ -34,6 +34,6 @@
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
$LANG['plugin_example']["test"]="test french"; $LANG['plugin_example']["test"] = "test français";
$LANG['plugin_example']["name"] = "Nom"; $LANG['plugin_example']["name"] = "Nom";
?> ?>

View File

@ -38,20 +38,17 @@ function plugin_init_example() {
global $PLUGIN_HOOKS,$LANG,$CFG_GLPI; global $PLUGIN_HOOKS,$LANG,$CFG_GLPI;
// Params : plugin name - string type - ID - Array of attributes // Params : plugin name - string type - ID - Array of attributes
Plugin::registerClass('PluginExampleExample', array( Plugin::registerClass('PluginExampleExample',
'classname' => 'PluginExampleExample', array('classname' => 'PluginExampleExample',
'formpage' => 'front/example.form.php', 'formpage' => 'front/example.form.php',
'deleted_tables' => false, 'deleted_tables' => false,
'template_tables' => false, 'template_tables' => false,
'specif_entities_tables' => false, 'specif_entities_tables' => false,
'recursive_type' => false, 'recursive_type' => false));
//'linkuser_types' => true,
//'linkgroup_types' => true
));
// Params : plugin name - string type - ID - Array of attributes // Params : plugin name - string type - ID - Array of attributes
Plugin::registerClass('PluginExampleDropdown', array( Plugin::registerClass('PluginExampleDropdown',
'classname' => 'PluginExampleDropdown', array('classname' => 'PluginExampleDropdown',
'typename' => 'Example Dropdown Type', 'typename' => 'Example Dropdown Type',
'formpage' => 'front/dropdown.form.php')); 'formpage' => 'front/dropdown.form.php'));
@ -82,7 +79,6 @@ function plugin_init_example() {
// Change entity // Change entity
//$PLUGIN_HOOKS['change_entity']['example'] = 'plugin_change_entity_example'; //$PLUGIN_HOOKS['change_entity']['example'] = 'plugin_change_entity_example';
// Onglets management // Onglets management
$PLUGIN_HOOKS['headings']['example'] = 'plugin_get_headings_example'; $PLUGIN_HOOKS['headings']['example'] = 'plugin_get_headings_example';
$PLUGIN_HOOKS['headings_action']['example'] = 'plugin_headings_actions_example'; $PLUGIN_HOOKS['headings_action']['example'] = 'plugin_headings_actions_example';
@ -106,13 +102,13 @@ function plugin_init_example() {
$PLUGIN_HOOKS['item_transfer']['example'] = 'plugin_item_transfer_example'; $PLUGIN_HOOKS['item_transfer']['example'] = 'plugin_item_transfer_example';
//redirect appel http://localhost/glpi/index.php?redirect=plugin_example_2 (ID 2 du form) //redirect appel http://localhost/glpi/index.php?redirect=plugin_example_2 (ID 2 du form)
$PLUGIN_HOOKS['redirect_page']['example']="example.form.php"; $PLUGIN_HOOKS['redirect_page']['example'] = 'example.form.php';
//function to populate planning //function to populate planning
$PLUGIN_HOOKS['planning_populate']['example']="plugin_planning_populate_example"; $PLUGIN_HOOKS['planning_populate']['example'] = 'plugin_planning_populate_example';
//function to display planning items //function to display planning items
$PLUGIN_HOOKS['display_planning']['example']="plugin_display_planning_example"; $PLUGIN_HOOKS['display_planning']['example'] = 'plugin_display_planning_example';
// Massive Action definition // Massive Action definition
$PLUGIN_HOOKS['use_massive_action']['example'] = 1; $PLUGIN_HOOKS['use_massive_action']['example'] = 1;
@ -120,35 +116,36 @@ function plugin_init_example() {
$PLUGIN_HOOKS['assign_to_ticket']['example'] = 1; $PLUGIN_HOOKS['assign_to_ticket']['example'] = 1;
// Add specific files to add to the header : javascript or css // Add specific files to add to the header : javascript or css
$PLUGIN_HOOKS['add_javascript']['example']="example.js"; $PLUGIN_HOOKS['add_javascript']['example'] = 'example.js';
$PLUGIN_HOOKS['add_css']['example']="example.css"; $PLUGIN_HOOKS['add_css']['example'] = 'example.css';
// Retrieve others datas from LDAP // Retrieve others datas from LDAP
//$PLUGIN_HOOKS['retrieve_more_data_from_ldap']['example']="plugin_retrieve_more_data_from_ldap_example"; //$PLUGIN_HOOKS['retrieve_more_data_from_ldap']['example']="plugin_retrieve_more_data_from_ldap_example";
// Reports // Reports
$PLUGIN_HOOKS['reports']['example'] = array('report.php'=>'New Report', 'report.php?other'=>'New Report 2',); $PLUGIN_HOOKS['reports']['example'] = array('report.php' => 'New Report',
'report.php?other' => 'New Report 2');
// Stats // Stats
$PLUGIN_HOOKS['stats']['example'] = array('stat.php'=>'New stat', 'stat.php?other'=>'New stats 2',); $PLUGIN_HOOKS['stats']['example'] = array('stat.php' => 'New stat',
'stat.php?other' => 'New stats 2',);
} }
// Get the name and the version of the plugin - Needed // Get the name and the version of the plugin - Needed
function plugin_version_example() { function plugin_version_example() {
return array(
'name' => 'Plugin Example', return array('name' => 'Plugin Example',
'version' => '0.2.0', 'version' => '0.2.0',
'author' => 'Julien Dombre', 'author' => 'Julien Dombre',
'homepage'=> 'http://glpi-project.org', 'homepage' => 'https://forge.indepnet.net/projects/show/example',
'minGlpiVersion' => '0.80',// For compatibility / no install in version < 0.72 'minGlpiVersion' => '0.80');// For compatibility / no install in version < 0.72
);
} }
// Optional : check prerequisites before install : may print errors or add to message after redirect // Optional : check prerequisites before install : may print errors or add to message after redirect
function plugin_example_check_prerequisites() { function plugin_example_check_prerequisites() {
if (GLPI_VERSION >= 0.80) { if (GLPI_VERSION >= 0.80) {
return true; return true;
} else { } else {