Web Hooks on TimeTrex Actions

Discussion for TimeTrex open source community developers.
Locked
visaolive
Posts: 21
Joined: Mon May 20, 2013 5:16 am

Web Hooks on TimeTrex Actions

Post by visaolive »

Is it possible to setup a Web Hook upon the following actions:

- Adding Employee from System
- Adding Employee to Schedule
- Changing Employee Assignment on Schedule
- Removing Employee from System

If there isn't, would this need to be setup as a DB Trigger instead?
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: Web Hooks on TimeTrex Actions

Post by shaunw »

You can use the plugin system to create almost any type of "hook" that you wish. See classes/modules/plugins directory for example plugins.
visaolive
Posts: 21
Joined: Mon May 20, 2013 5:16 am

Re: Web Hooks on TimeTrex Actions

Post by visaolive »

Thanks. Is there any further documentation online on plugins.

I'm looking at the UserFactory.plugin.example, changed it to UserFactory.plugin.php, but an unable to get it to fire.

Is there anything else that needs to be done to TimeTrex to get this plugin to fire?

Also, might there be a way to extend the setUser function in root/classes/modules/api/users/APIUser.class.php file to do the following:

- When a user is added, edited, or deleted perform HTTP POST/GET with Array values

Thanks!
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: Web Hooks on TimeTrex Actions

Post by shaunw »

Thanks. Is there any further documentation online on plugins.

I'm looking at the UserFactory.plugin.example, changed it to UserFactory.plugin.php, but an unable to get it to fire.

Is there anything else that needs to be done to TimeTrex to get this plugin to fire?
See the answer to your other post:
http://forums.timetrex.com/viewtopic.php?f=5&t=5993
- When a user is added, edited, or deleted perform HTTP POST/GET with Array values
You will want to write a plugin almost identical to the UserFactoryPlugin example:

Code: Select all

    function postSave() {
        parent::postSave(); //Make sure you always call the parents function to maintain proper code operation.

        //User record was saved. We can do all sorts of things here like trigger real-time data exporting.
      Debug::Arr( $this->getObjectAsArray(), 'UserFactory Plugin postSave(): ', __FILE__, __LINE__, __METHOD__,10);

        return TRUE;
    }
postSave() is called after a record is added/modified/deleted, so this is the function where you can call your HTTP POST/GET or do anything else you want to exchange data.
visaolive
Posts: 21
Joined: Mon May 20, 2013 5:16 am

Re: Web Hooks on TimeTrex Actions

Post by visaolive »

Thanks.

I updated the .ini file, and added enable_plugins = TRUE

After doing do, I notice that on the Log File, this began to appear:

DEBUG [L0263] [206ms]: <b>TTgetPluginClassName()</b>: Found Plugin: /timetrex/classes/modules/plugins/UserFactory.plugin.php Class: UserFactoryPlugin<br>

After doing this, I notice that it's not referencing the postSave function I have in my plugin, just the existing postSave function. I only have the Debug statement in this function to see if I get any reference in the Log file first.

I performed the following tasks, with their respective results:

- Add New User = No Debug Entry
- Edit Existing User = No Debug Entry
- Logout / Login - Add New User = No Debug Entry
- Clear Cache Directory - Add New User = No Debug Entry

Here is the Debug section I currently have set in the .ini file:

[debug]
;Set to false if you're debugging
production = FALSE

enable = TRUE
enable_display = TRUE
buffer_output = TRUE
enable_log = TRUE
verbosity = 10


Here is the Code I have for this plugin:
Note, I've switched UserListFactory with UserFactory as well.

Code: Select all


<?php

/*$License$*/
/*
 * $Revision: 4034 $
 * $Id: UserFactory.plugin.php 4034 2010-12-15 00:02:50Z ipso $
 * $Date: 2010-12-14 16:02:50 -0800 (Tue, 14 Dec 2010) $
 */

/*
 * Example plugin.
 */


//Extend the "ListFactory" if you want your plugin to affect it AND the base Factory class.
//Extend just the "Factory" if you just want it to affect just it, and not account for objects read/modified through iterators.


class UserFactoryPlugin extends UserFactory {

    function postSave() {
        parent::postSave(); //Make sure you always call the parents function to maintain proper code operation.

        //User record was saved. We can do all sorts of things here like trigger real-time data exporting.
		Debug::Arr( $this->getObjectAsArray(), 'UserFactory Updated Plugin postSave(): ', __FILE__, __LINE__, __METHOD__,10);

        return TRUE;
    }
    
    

}

?>


shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: Web Hooks on TimeTrex Actions

Post by shaunw »

If your plugin just overloads the UserFactory class, it will only get called in very rare circumstances, usually only when creating new users from scratch. Instead you will need to put your actual plugin code in UserListFactory.plugin.php and create a UserFactory.plugin.php that extends that plugin as well. Then it will be called anytime UserFactory/UserListFactory is used.

See how the PermissionFactory/PermissionListFactory example plugins work, you should be able to copy them.
visaolive
Posts: 21
Joined: Mon May 20, 2013 5:16 am

Re: Web Hooks on TimeTrex Actions

Post by visaolive »

Thanks! This Worked Perfectly!

One last question on this. Is it possible that in the postSave function, or in another function, to update the current record with some text, like an ID.

For example:

- Update Employee record in TimeTrex (Say TimeTrex User ID = 5)
- The Update Triggers the Plugin to be called, and perform an outside action
- The result of this outside action produces an ID (Say: Outside User ID = 11233)
- Before the function returns, the function updates the (TimeTrex User ID = 5) on a Custom Field or Open Field, and Sets the Outside User ID to this newly created value = 11233

Is this possible without creating an infinite loop?
Can this second update be performed without calling postSave again?

Thanks!
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: Web Hooks on TimeTrex Actions

Post by shaunw »

That can be a little tricky, but if you do it properly it should work.

In postSave(), once you get the remoteID, you could use $this->setOtherID1( $remote_id ); then call $this->Save() again. The key here is to always check and only obtain a remoteID and call Save() again if the remoteID is not already set, so that should avoid the infinite loop. However you will want to handle error cases where the remote system is down or not accessible, perhaps by using a remote_id = -1 or something.
visaolive
Posts: 21
Joined: Mon May 20, 2013 5:16 am

Re: Web Hooks on TimeTrex Actions

Post by visaolive »

Thanks. I'll check it out!
Locked