From acf04907276c2a671abb1bac6ee94190884505d3 Mon Sep 17 00:00:00 2001 From: yllen Date: Thu, 8 Sep 2011 09:51:40 +0000 Subject: [PATCH] CS + call functions git-svn-id: https://forge.glpi-project.org/svn/example/trunk@172 349b9182-4a13-0410-896f-e5e9767dd1b3 --- hook.php | 94 +++++++++++++------------ inc/example.class.php | 15 ++-- inc/notificationtargetexample.class.php | 1 - inc/ruletest.class.php | 13 ++-- inc/ruletestcollection.class.php | 9 +-- setup.php | 39 +++++----- 6 files changed, 90 insertions(+), 81 deletions(-) diff --git a/hook.php b/hook.php index 27d0dcf..672de6f 100644 --- a/hook.php +++ b/hook.php @@ -63,6 +63,8 @@ function plugin_example_getDropdown() { return array('PluginExampleDropdown' => "Plugin Example Dropdown"); } + + ////// SEARCH FUNCTIONS ///////(){ // Define Additionnal search options for types (other than the plugin ones) @@ -100,23 +102,23 @@ function plugin_example_giveItem($type,$ID,$data,$num) { return ""; } + function plugin_example_displayConfigItem($type, $ID, $data, $num) { $searchopt = &Search::getOptions($type); - $table = $searchopt[$ID]["table"]; - $field = $searchopt[$ID]["field"]; - + $table = $searchopt[$ID]["table"]; + $field = $searchopt[$ID]["field"]; // Example of specific style options // No need of the function if you do not have specific cases switch ($table.'.'.$field) { case "glpi_plugin_example_examples.name" : return " style=\"background-color:#DDDDDD;\" "; - break; } return ""; } + function plugin_example_addDefaultJoin($type, $ref_table, &$already_link_tables) { // Example of default JOIN clause @@ -125,12 +127,12 @@ function plugin_example_addDefaultJoin($type, $ref_table, &$already_link_tables) // case "PluginExampleExample" : case "MyType" : return Search::addLeftJoin($type, $ref_table, $already_link_tables, - "newtable", "linkfield"); - break; + "newtable", "linkfield"); } return ""; } + function plugin_example_addDefaultSelect($type) { // Example of default SELECT item to be added @@ -139,11 +141,11 @@ function plugin_example_addDefaultSelect($type) { // case "PluginExampleExample" : case "MyType" : return "`mytable`.`myfield` = 'myvalue' AS MYNAME, "; - break; } return ""; } + function plugin_example_addDefaultWhere($type) { // Example of default WHERE item to be added @@ -152,13 +154,12 @@ function plugin_example_addDefaultWhere($type) { // case "PluginExampleExample" : case "MyType" : return " `mytable`.`myfield` = 'myvalue' "; - break; } 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 // No need of the function if you do not have specific cases @@ -184,8 +185,8 @@ function plugin_example_forceGroupBy($type) { function plugin_example_addWhere($link,$nott,$type,$ID,$val) { $searchopt = &Search::getOptions($type); - $table = $searchopt[$ID]["table"]; - $field = $searchopt[$ID]["field"]; + $table = $searchopt[$ID]["table"]; + $field = $searchopt[$ID]["field"]; $SEARCH = Search::makeTextSearch($val,$nott); @@ -206,11 +207,11 @@ function plugin_example_addWhere($link,$nott,$type,$ID,$val) { // 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) { $searchopt = &Search::getOptions($type); - $table = $searchopt[$ID]["table"]; - $field = $searchopt[$ID]["field"]; + $table = $searchopt[$ID]["table"]; + $field = $searchopt[$ID]["field"]; $SEARCH = Search::makeTextSearch($val,$nott); @@ -232,8 +233,8 @@ function plugin_example_addHaving($link,$nott,$type,$ID,$val,$num) { function plugin_example_addSelect($type,$ID,$num) { $searchopt = &Search::getOptions($type); - $table = $searchopt[$ID]["table"]; - $field = $searchopt[$ID]["field"]; + $table = $searchopt[$ID]["table"]; + $field = $searchopt[$ID]["field"]; // Example of standard Select clause but use it ONLY for specific Select // No need of the function if you do not have specific cases @@ -248,8 +249,8 @@ function plugin_example_addSelect($type,$ID,$num) { function plugin_example_addOrderBy($type,$ID,$order,$key=0) { $searchopt = &Search::getOptions($type); - $table = $searchopt[$ID]["table"]; - $field = $searchopt[$ID]["field"]; + $table = $searchopt[$ID]["table"]; + $field = $searchopt[$ID]["field"]; // Example of standard OrderBy clause but use it ONLY for specific order by // No need of the function if you do not have specific cases @@ -291,9 +292,9 @@ function plugin_example_MassiveActionsDisplay($options=array()) { case 'Computer' : switch ($options['action']) { case "plugin_example_DoIt" : - - echo "  but do nothing :)"; + echo " ". + " but do nothing :)"; break; } break; @@ -319,13 +320,13 @@ function plugin_example_MassiveActionsProcess($data) { switch ($data['action']) { case 'plugin_example_DoIt' : if ($data['itemtype'] == 'Computer') { - $comp = new Computer; - addMessageAfterRedirect("Right it is the type I want..."); - addMessageAfterRedirect("But... I say I will do nothing for :"); + $comp = new Computer(); + Session::addMessageAfterRedirect("Right it is the type I want..."); + Session::addMessageAfterRedirect("But... I say I will do nothing for :"); foreach ($data['item'] as $key => $val) { if ($val == 1) { if ($comp->getFromDB($key)) { - addMessageAfterRedirect("- ".$comp->getField("name")); + Session::addMessageAfterRedirect("- ".$comp->getField("name")); } } } @@ -335,12 +336,12 @@ function plugin_example_MassiveActionsProcess($data) { case 'do_nothing' : if ($data['itemtype'] == 'PluginExampleExample') { $ex = new PluginExampleExample(); - addMessageAfterRedirect("Right it is the type I want..."); - addMessageAfterRedirect("But... I say I will do nothing for :"); + Session::addMessageAfterRedirect("Right it is the type I want..."); + Session::addMessageAfterRedirect("But... I say I will do nothing for :"); foreach ($data['item'] as $key => $val) { if ($val == 1) { if ($ex->getFromDB($key)) { - addMessageAfterRedirect("- ".$ex->getField("name")); + Session::addMessageAfterRedirect("- ".$ex->getField("name")); } } } @@ -355,11 +356,11 @@ function plugin_example_MassiveActionsProcess($data) { function plugin_example_MassiveActionsFieldsDisplay($options=array()) { //$type,$table,$field,$linkfield - $table = $options['options']['table']; - $field = $options['options']['field']; + $table = $options['options']['table']; + $field = $options['options']['field']; $linkfield = $options['options']['linkfield']; - if ($table == getTableForItemType($options['itemtype'])) { + if ($table == getTableForItemType($options['itemtype'])) { // Table fields switch ($table.".".$field) { case 'glpi_plugin_example_examples.serial' : @@ -383,6 +384,7 @@ function plugin_example_MassiveActionsFieldsDisplay($options=array()) { return false; } + // How to display specific search fields or dropdown ? // options must contain at least itemtype and options array // MUST Use a specific AddWhere & $tab[X]['searchtype'] = 'equals'; declaration @@ -394,13 +396,12 @@ function plugin_example_searchOptionsValues($options=array()) { // Table fields switch ($table.".".$field) { - case "glpi_plugin_example_examples.serial" : echo "Not really specific - Use your own dropdown - Just for example "; Dropdown::show(getItemTypeForTable($options['searchoption']['table']), - array('value' => $options['value'], - 'name' => $options['name'], - 'comments' => 0)); + array('value' => $options['value'], + 'name' => $options['name'], + 'comments' => 0)); // Need to return true if specific display return true; } @@ -419,17 +420,18 @@ function plugin_pre_item_update_example($item) { } $item->input['comment'] .= addslashes("\nUpdate: ".date('r')); */ - Session::addMessageAfterRedirect("Pre Update Computer Hook",true); + Session::addMessageAfterRedirect("Pre Update Computer Hook", true); } // Hook done on update item case function plugin_item_update_example($item) { - Session::addMessageAfterRedirect("Update Computer Hook (".implode(',',$item->updates).")",true); + Session::addMessageAfterRedirect("Update Computer Hook (".implode(',',$item->updates).")", true); return true; } + // Hook done on get empty item case function plugin_item_empty_example($item) { @@ -485,6 +487,7 @@ function plugin_pre_item_restore_example2($item) { Session::addMessageAfterRedirect("Pre Restore Phone Hook"); } + // Hook done on restore item case function plugin_item_restore_example($item) { @@ -497,7 +500,7 @@ function plugin_item_restore_example($item) { function plugin_item_transfer_example($parm) { Session::addMessageAfterRedirect("Transfer Computer Hook ".$parm['type']." ".$parm['id']." -> ". - $parm['newID']); + $parm['newID']); return false; } @@ -532,14 +535,14 @@ function plugin_display_planning_example($parm) { // Add items in the items fields of the parm array switch ($parm["type"]) { 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; case "from" : break; case "begin" : - echo $LANG["buttons"][33]." ".date("H:i",strtotime($parm["begin"])).": "; + echo $LANG["buttons"][33]." ".date("H:i", strtotime($parm["begin"])).": "; break; case "end" : @@ -558,18 +561,16 @@ function plugin_get_headings_example($item, $withtemplate) { $prof = new Profile(); if ($item->fields['interface'] == 'central') { return array(1 => "Test PLugin"); - } else { - return array(); } + return array(); case 'Computer' : // new object / template case if ($withtemplate) { return array(); // Non template case / editing an existing object - } else { - return array(1 => "Test PLugin"); } + return array(1 => "Test PLugin"); case 'ComputerDisk' : case 'Supplier' : @@ -679,7 +680,7 @@ function plugin_example_addParamFordynamicReport($itemtype) { // Return array data containing all params to add : may be single data or array data // Search config are available from session variable return array('add1' => $_SESSION['glpisearch'][$itemtype]['order'], - 'add2' => array('tutu'=>'Second Add', + 'add2' => array('tutu' => 'Second Add', 'Other Data')); } // Return false or a non array data if not needed @@ -781,14 +782,17 @@ function plugin_example_AssignToTicket($types) { return $types; } + function plugin_example_get_events(NotificationTargetTicket $target) { $target->events['plugin_example'] = "Example event"; } + function plugin_example_get_datas(NotificationTargetTicket $target) { $target->datas['##ticket.example##'] = "Example datas"; } + function plugin_example_postinit() { global $CFG_GLPI; diff --git a/inc/example.class.php b/inc/example.class.php index 652c2a5..2ad1e66 100644 --- a/inc/example.class.php +++ b/inc/example.class.php @@ -36,23 +36,24 @@ // Class of the defined type class PluginExampleExample extends CommonDBTM { - // From CommonDBTM - public $table = 'glpi_plugin_example_examples'; - public $type = 'PluginExampleExample'; // Should return the localized name of the type static function getTypeName() { return 'Example Type'; } + function canCreate() { + if (isset($_SESSION["glpi_plugin_example_profile"])) { return ($_SESSION["glpi_plugin_example_profile"]['example'] == 'w'); } return false; } + function canView() { + if (isset($_SESSION["glpi_plugin_example_profile"])) { return ($_SESSION["glpi_plugin_example_profile"]['example'] == 'w' || $_SESSION["glpi_plugin_example_profile"]['example'] == 'r'); @@ -60,6 +61,7 @@ class PluginExampleExample extends CommonDBTM { return false; } + function getSearchOptions() { global $LANG; @@ -87,6 +89,7 @@ class PluginExampleExample extends CommonDBTM { return $tab; } + /** * Give localized information about 1 task * @@ -105,6 +108,7 @@ class PluginExampleExample extends CommonDBTM { return array(); } + /** * Execute 1 task manage by the plugin * @@ -123,16 +127,17 @@ class PluginExampleExample extends CommonDBTM { return 1; } + // Hook done on before add item case static function pre_item_add_example($item) { - addMessageAfterRedirect("Pre Add Computer Hook",true); + Session::addMessageAfterRedirect("Pre Add Computer Hook", true); } // Hook done on add item case static function item_add_example($item) { - addMessageAfterRedirect("Add Computer Hook, ID=".$item->getField('id'),true); + Session::addMessageAfterRedirect("Add Computer Hook, ID=".$item->getField('id'), true); return true; } diff --git a/inc/notificationtargetexample.class.php b/inc/notificationtargetexample.class.php index 3c8f6b5..902484e 100644 --- a/inc/notificationtargetexample.class.php +++ b/inc/notificationtargetexample.class.php @@ -36,7 +36,6 @@ if (!defined('GLPI_ROOT')){ class PluginExampleNotificationTargetExample extends NotificationTarget { function getEvents() { - global $LANG; return array ('alert' => 'alert example'); } diff --git a/inc/ruletest.class.php b/inc/ruletest.class.php index c7b5eaf..a096bfc 100644 --- a/inc/ruletest.class.php +++ b/inc/ruletest.class.php @@ -47,21 +47,23 @@ if (!defined('GLPI_ROOT')) { class PluginExampleRuleTest extends Rule { // From Rule - public $right='rule_ocs'; - public $can_sort=true; + public $right = 'rule_ocs'; + public $can_sort = true; + function getTitle() { - global $LANG; - return 'test'; } + function maxActionsCount() { return 1; } + function getCriterias() { global $LANG; + $criterias = array(); $criterias['name']['field'] = 'name'; $criterias['name']['name'] = $LANG['help'][31]; @@ -70,8 +72,10 @@ class PluginExampleRuleTest extends Rule { return $criterias; } + function getActions() { global $LANG; + $actions = array(); $actions['softwarecategories_id']['name'] = $LANG['common'][36]; $actions['softwarecategories_id']['type'] = 'dropdown'; @@ -79,5 +83,4 @@ class PluginExampleRuleTest extends Rule { return $actions; } } - ?> diff --git a/inc/ruletestcollection.class.php b/inc/ruletestcollection.class.php index be7ad97..ac4ef6d 100644 --- a/inc/ruletestcollection.class.php +++ b/inc/ruletestcollection.class.php @@ -40,15 +40,12 @@ if (!defined('GLPI_ROOT')) { class PluginExampleRuleTestCollection extends RuleCollection { // From RuleCollection - public $stop_on_first_match=true; - public $right='rule_ocs'; - public $menu_option='test'; + public $stop_on_first_match = true; + public $right = 'rule_ocs'; + public $menu_option = 'test'; function getTitle() { - global $LANG; - return 'Rulesengine test'; } } - ?> diff --git a/setup.php b/setup.php index 4067d21..e2c0c76 100755 --- a/setup.php +++ b/setup.php @@ -51,16 +51,16 @@ function plugin_init_example() { 'addtabon' => array('Phone'))); Plugin::registerClass('PluginExampleRuleTestCollection', - array('rulecollections_types' => true)); + array('rulecollections_types' => true)); // Display a menu entry ? if (isset($_SESSION["glpi_plugin_example_profile"])) { // Right set in change_profile hook $PLUGIN_HOOKS['menu_entry']['example'] = 'front/example.php'; $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['title'] = "Search"; - $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['page'] = '/plugins/example/front/example.php'; + $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['page'] = '/plugins/example/front/example.php'; $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['links']['search'] = '/plugins/example/front/example.php'; - $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['links']['add'] = '/plugins/example/front/example.form.php'; + $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['links']['add'] = '/plugins/example/front/example.form.php'; $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['links']['config'] = '/plugins/example/index.php'; $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['links']["".$LANG["] = '/plugins/example/index.php'; $PLUGIN_HOOKS['submenu_entry']['example']['options']['optionname']['links'][$LANG['plugin_example']["test"]] = '/plugins/example/index.php'; @@ -85,10 +85,10 @@ function plugin_init_example() { $PLUGIN_HOOKS['headings_action']['example'] = 'plugin_headings_actions_example'; // Item action event // See define.php for defined ITEM_TYPE - $PLUGIN_HOOKS['pre_item_update']['example'] = array('Computer'=>'plugin_pre_item_update_example'); - $PLUGIN_HOOKS['item_update']['example'] = array('Computer'=>'plugin_item_update_example'); + $PLUGIN_HOOKS['pre_item_update']['example'] = array('Computer' => 'plugin_pre_item_update_example'); + $PLUGIN_HOOKS['item_update']['example'] = array('Computer' => 'plugin_item_update_example'); - $PLUGIN_HOOKS['item_empty']['example'] = array('Computer'=>'plugin_item_empty_example'); + $PLUGIN_HOOKS['item_empty']['example'] = array('Computer' => 'plugin_item_empty_example'); // Example using a method in class $PLUGIN_HOOKS['pre_item_add']['example'] = array('Computer' => array('PluginExampleExample', @@ -96,26 +96,28 @@ function plugin_init_example() { $PLUGIN_HOOKS['item_add']['example'] = array('Computer' => array('PluginExampleExample', 'item_add_example')); - $PLUGIN_HOOKS['pre_item_delete']['example'] = array('Computer'=>'plugin_pre_item_delete_example'); - $PLUGIN_HOOKS['item_delete']['example'] = array('Computer'=>'plugin_item_delete_example'); + $PLUGIN_HOOKS['pre_item_delete']['example'] = array('Computer' => 'plugin_pre_item_delete_example'); + $PLUGIN_HOOKS['item_delete']['example'] = array('Computer' => 'plugin_item_delete_example'); // Example using the same function - $PLUGIN_HOOKS['pre_item_purge']['example'] = array('Computer'=>'plugin_pre_item_purge_example', - 'Phone'=>'plugin_pre_item_purge_example'); - $PLUGIN_HOOKS['item_purge']['example'] = array('Computer'=>'plugin_item_purge_example', - 'Phone'=>'plugin_item_purge_example'); + $PLUGIN_HOOKS['pre_item_purge']['example'] = array('Computer' => 'plugin_pre_item_purge_example', + 'Phone' => 'plugin_pre_item_purge_example'); + $PLUGIN_HOOKS['item_purge']['example'] = array('Computer' => 'plugin_item_purge_example', + 'Phone' => 'plugin_item_purge_example'); // Example with 2 different functions - $PLUGIN_HOOKS['pre_item_restore']['example'] = array('Computer'=>'plugin_pre_item_restore_example', - 'Phone'=>'plugin_pre_item_restore_example2'); - $PLUGIN_HOOKS['item_restore']['example'] = array('Computer'=>'plugin_item_restore_example'); + $PLUGIN_HOOKS['pre_item_restore']['example'] = array('Computer' => 'plugin_pre_item_restore_example', + 'Phone' => 'plugin_pre_item_restore_example2'); + $PLUGIN_HOOKS['item_restore']['example'] = array('Computer' => 'plugin_item_restore_example'); // Add event to GLPI core itemtype, event will be raised by the plugin. // See plugin_example_uninstall for cleanup of notification - $PLUGIN_HOOKS['item_get_events']['example'] = array('NotificationTargetTicket'=>'plugin_example_get_events'); + $PLUGIN_HOOKS['item_get_events']['example'] + = array('NotificationTargetTicket' => 'plugin_example_get_events'); // Add datas to GLPI core itemtype for notifications template. - $PLUGIN_HOOKS['item_get_datas']['example'] = array('NotificationTargetTicket'=>'plugin_example_get_datas'); + $PLUGIN_HOOKS['item_get_datas']['example'] + = array('NotificationTargetTicket' => 'plugin_example_get_datas'); $PLUGIN_HOOKS['item_transfer']['example'] = 'plugin_item_transfer_example'; @@ -188,11 +190,10 @@ function plugin_example_check_config($verbose=false) { if (true) { // Your configuration check return true; } + if ($verbose) { echo $LANG['plugins'][2]; } return false; } - - ?>