Fix example to adapt to new MassiveAction system (regarding ticket #4385 of the core)

git-svn-id: https://forge.glpi-project.org/svn/example/trunk@218 349b9182-4a13-0410-896f-e5e9767dd1b3
This commit is contained in:
webmyster 2013-11-01 06:41:05 +00:00
parent 0edcafd1fb
commit 76d6268bf1
2 changed files with 108 additions and 93 deletions

View File

@ -270,102 +270,15 @@ function plugin_example_MassiveActions($type) {
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' : case 'Computer' :
return array("plugin_example_DoIt" => __("plugin_example_DoIt", 'example')); return array('PluginExampleExample'.MassiveAction::CLASS_ACTION_SEPARATOR.'DoIt' =>
__("plugin_example_DoIt", 'example'));
// Actions for types provided by the plugin // Actions for types provided by the plugin are included inside the classes
case 'PluginExampleExample' :
return array("add_document" => _x('button', 'Add a document'), // GLPI core one
"do_nothing" => __('Do Nothing - just for fun', 'example')); // Specific one
} }
return array(); return array();
} }
// How to display specific actions ?
// options contain at least itemtype and and action
function plugin_example_MassiveActionsDisplay($options=array()) {
switch ($options['itemtype']) {
case 'Computer' :
switch ($options['action']) {
case "plugin_example_DoIt" :
echo "&nbsp;<input type='hidden' name='toto' value='1'>".
"<input type='submit' name='massiveaction' class='submit' value='".
_sx('button','Post')."'> ".__('Write in item history', 'example');
break;
}
break;
case 'PluginExampleExample' :
switch ($options['action']) {
// No case for add_document : use GLPI core one
case "do_nothing" :
echo "&nbsp;<input type='submit' name='massiveaction' class='submit' value='".
_sx('button','Post')."'> ".__('but do nothing :)', 'example');
break;
}
break;
}
return "";
}
// How to process specific actions ?
// May return array of stats containing number of process: ok / ko / noright count to display stats
function plugin_example_MassiveActionsProcess($data) {
$ok = 0;
$ko = 0;
$noright = 0;
switch ($data['action']) {
case 'plugin_example_DoIt' :
if ($data['itemtype'] == 'Computer') {
$comp = new Computer();
Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example'));
Session::addMessageAfterRedirect(__('Write in item history', 'example'));
$changes = array(0, 'old value', 'new value');
foreach ($data['item'] as $key => $val) {
if ($val == 1) {
if ($comp->getFromDB($key)) {
Session::addMessageAfterRedirect("- ".$comp->getField("name"));
Log::history($key, 'Computer', $changes, 'PluginExampleExample', Log::HISTORY_PLUGIN);
$ok++;
} else {
// Example of ko count
$ko++;
}
}
}
}
break;
case 'do_nothing' :
if ($data['itemtype'] == 'PluginExampleExample') {
$ex = new PluginExampleExample();
Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example'));
Session::addMessageAfterRedirect(__("But... I say I will do nothing for:", 'example'));
foreach ($data['item'] as $key => $val) {
if ($val == 1) {
if ($ex->getFromDB($key)) {
Session::addMessageAfterRedirect("- ".$ex->getField("name"));
$ok++;
} else {
// Example for noright / Maybe do it with can function is better
$noright++;
}
}
}
}
break;
}
return array('ok' => $ok,
'ko' => $ko,
'noright' => $noright);
}
// How to display specific update fields ? // How to display specific update fields ?
// options must contain at least itemtype and options array // options must contain at least itemtype and options array
function plugin_example_MassiveActionsFieldsDisplay($options=array()) { function plugin_example_MassiveActionsFieldsDisplay($options=array()) {
@ -449,7 +362,10 @@ function plugin_item_update_example($item) {
// Hook done on get empty item case // Hook done on get empty item case
function plugin_item_empty_example($item) { function plugin_item_empty_example($item) {
Session::addMessageAfterRedirect(__("Empty Computer Hook", 'example'),true); if (empty($_SESSION['Already displayed "Empty Computer Hook"'])) {
Session::addMessageAfterRedirect(__("Empty Computer Hook", 'example'),true);
$_SESSION['Already displayed "Empty Computer Hook"'] = true;
}
return true; return true;
} }

View File

@ -81,8 +81,7 @@ class PluginExampleExample extends CommonDBTM {
return $links; return $links;
} }
function getSearchOptions() { function getSearchOptions() {
@ -347,5 +346,105 @@ class PluginExampleExample extends CommonDBTM {
return ''; return '';
} }
//////////////////////////////
////// SPECIFIC MODIF MASSIVE FUNCTIONS ///////
/**
* @since version 0.85
*
* @see CommonDBTM::getSpecificMassiveActions()
**/
function getSpecificMassiveActions($checkitem=NULL) {
Toolbox::logDebug();
$actions = parent::getSpecificMassiveActions($checkitem);
$actions['Document_Item'.MassiveAction::CLASS_ACTION_SEPARATOR.'add'] =
_x('button', 'Add a document'); // GLPI core one
$actions[__CLASS__.MassiveAction::CLASS_ACTION_SEPARATOR.'do_nothing'] =
__('Do Nothing - just for fun', 'example'); // Specific one
return $actions;
}
/**
* @since version 0.85
*
* @see CommonDBTM::showMassiveActionsSubForm()
**/
static function showMassiveActionsSubForm(MassiveAction $ma) {
switch ($ma->getAction()) {
case 'DoIt':
echo "&nbsp;<input type='hidden' name='toto' value='1'>".
Html::submit(_x('button','Post'), array('name' => 'massiveaction')).
" ".__('Write in item history', 'example');
return true;
case 'do_nothing' :
echo "&nbsp;".Html::submit(_x('button','Post'), array('name' => 'massiveaction')).
" ".__('but do nothing :)', 'example');
return true;
}
return parent::showMassiveActionsSubForm($ma);
}
/**
* @since version 0.85
*
* @see CommonDBTM::processMassiveActionsForOneItemtype()
**/
static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item,
array $ids) {
global $DB;
switch ($ma->getAction()) {
case 'DoIt' :
if ($item->getType() == 'Computer') {
Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example'));
Session::addMessageAfterRedirect(__('Write in item history', 'example'));
$changes = array(0, 'old value', 'new value');
foreach ($ids as $id) {
if ($item->getFromDB($id)) {
Session::addMessageAfterRedirect("- ".$item->getField("name"));
Log::history($id, 'Computer', $changes, 'PluginExampleExample',
Log::HISTORY_PLUGIN);
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
// Example of ko count
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
}
}
} else {
// When nothing is possible ...
$ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
}
return;
case 'do_nothing' :
If ($item->getType() == 'PluginExampleExample') {
Session::addMessageAfterRedirect(__("Right it is the type I want...", 'example'));
Session::addMessageAfterRedirect(__("But... I say I will do nothing for:",
'example'));
foreach ($ids as $id) {
if ($item->getFromDB($id)) {
Session::addMessageAfterRedirect("- ".$item->getField("name"));
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
// Example for noright / Maybe do it with can function is better
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
}
}
} else {
$ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
}
Return;
}
parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
}
} }
?> ?>