Hooking into the ItemManager backend methods

Erstellt am: Tuesday 31 January 2017  |  Letzte Änderung am: Monday 28 January 2019

This article describes how to change the functionality of ItemManager admin area, and is a part of the Working with ItemManager 2.0 series.

Sometimes you don't want to write your own plugin with all its complex logic, and you just want to extend or change a certain functionality of the ItemManager in the admin area. ItemManager offers the ability to extend the backend functionality by writing your own methods or extending existing ones as you like.

Note, you'll need to copy the plugins/imanager/lib/inc/config.php file to the data/imanager/settings/config.php directory and set the $this->injectActions variable to true, so you are able to inject your logic into the ItemManager methods.

My next example should demonstrate how you can interact with a backend method and extend it with any desired functionality. Let's hook in the save method for items and write a log entry with the current date and time before saving is done.

To extend ItemManager you should always create your own plugin, this should always be done according to this principle:

$thisfile = basename(__FILE__, '.php');

register_plugin(
	$thisfile,
	'ItemManager\'s Save Item Hook',
	'0.1',
	'Your name',
	'https://your-site.com',
	'An ItemManger backend extension that executes on item save event',
	'',
	''
);

add_action('ImActivated', 'im_action');

function im_action($manager) {
    ...
}

As you can see, we have installed a function called im_action() here (you are free to choose the function name). This function is executed when ItemManager is started, where an instance of the ItemManager class will be passed to the function:

function im_action($manager) {
    ...

The passed instance can now be modified as desired. Now we extend the native Admin class with our own extra method beforeItemSave(), to do this we hook into the Admin's callModelMethod() and execute our method when saveItem() is called:

$thisfile = basename(__FILE__, '.php');

register_plugin(
	$thisfile,
	'ItemManager\'s Save Item Hook',
	'0.1',
	'Your name',
	'https://your-site.com',
	'An ItemManger backend extension that executes on item save event',
	'',
	''
);

add_action('ImActivated', 'im_action');

function im_action($manager) {

	if(!class_exists('AdminExtended'))
	{
		class AdminExtended extends Admin
		{
			protected function callModelMethod($method, $args)
			{
				if($method == 'saveItem') {
					$this->beforeItemSave($args[0]);
				}
				parent::callModelMethod($method, $args);
			}

			/**
			 * Here you can write your own method that will be executed before
			 * someone saves an item from the ItemManager's backend.
			 *
			 */
			protected function beforeItemSave(& $input)
			{
				$date = new \DateTime();
				$date->setTimeZone(new \DateTimeZone('Europe/Berlin'));
				$datetime = $date->format('d.m.Y H:i:s');

				$itemname = $this->manager->sanitizer->text($_POST['name']);

				Util::dataLog("Item '$itemname' was saved on $datetime");
			}
		}

		$newbackend = new \AdminExtended;
		$manager->setAdmin($newbackend);
	}
}

The utility method dataLog() is executed and the data to be written to the log file is passed to it (of course you have to activate the plugin before you can use it). By the way, the IM log files can always be found here /data/other/logs/imlog_******.txt.

Autor: Bigin  |  Tags:  FrameworkPHPGetSimpleDevelopmentItemManager