Share » Learn » eZ Publish » Creating a simple custom workflow event

Creating a simple custom workflow event

Thursday 09 December 2010 8:01:18 am

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 4 - Develop a workflow event: Arc90 OAuth Twitter library

Arc90_Service_Twitter aims to provide a robust but easy-to-use interface to the Twitter API. You can download the OAuth version of the library here: http://github.com/yemkay/arc90-twitteroauth

What you need is the “Arc90” and “oauth” folders located inside the downloaded archive. Copy this folder and its content into ~extension/mytwitter/lib/

Note: see further down this tutorial to see how to generate the key, token and secrets.

 

As a preliminary step we will now create an INI file to store some key variables: ~extension/mytwitter/settings/mytwitter.ini

In the file we will create below, the setup.php script will be described futher down in this tutorial at Step 8.

#?ini charset="utf-8"?
[TwitterSettings]
# Optional: you can register for a Twitter API profile at: http://dev.twitter.com/
# then copy and paste the Consumer Key and Consumer Secret strings here. This will
# show your app name instead of ‘eztweeter’ when ever a status update is posted.
# If you choose to do so, please also update extension/mytwitter/lib/setup.php

ConsumerKey=Gq2fR3F8qPqyOariWv4csQ
ConsumerSecret=kNO49WT3xw44hovPOlQSbw2dwYHQoJ6Jr5KLyI3Y

# The following variables should be configured in your siteaccess settings
# To generate those values use the setup.php script in the lib/ folder, go to the lib/ directory of our mytwitter extension directory and run the following command in CLI
# php setup.php --register
# then copy and paste the generated URL into a browser, login into Twitter and allow access to the eztweeter app, copy the PIN
# php setup.php -–validate=[PIN]
# save the Key and Secret as the values below
#AccessToken=
#AccessSecret=

# will write debug message in var/log/common.log
#DebugOutput=enabled
 

Please note the extension of this file is “.ini”. This file is a raw INI file. Its usual purpose is to declare common variables and their default values. It also allows eZ Publish to display an entry for this file when you go in the admin interface to Setup > Ini settings > Select ini file to view.

You should comment all here and create a copy of it as mytwitter.ini.append.php in the siteaccess settings folder. This follows one of the best practices in eZ Publish development and integration : never edit the default configuration files ( the ones located in settings/*.ini ), rather use the override system to modify a directive's value. More information on this here : http://doc.ez.no/eZ-Publish/Technical-manual/4.4/Concepts-and-basics/Configuration.

We will now start our code with loading some parameters from a mytwitter.ini file and instantiate the Arc90_Service_Twitter class. For this, re-edit the file ~extension/mytwitter/eventtypes/event/twitterstatusupdate/twitterstatusupdatetype.php and start the code after the line

“/* YOUR CODE GOES HERE */”

The following code is loading the INI settings and then instantiate the Twitter ARC90 OAuth class. Please also note the use of eZLog class that allows us to write debug messages to ~/var/log/common.log

public function execute( $process, $event )
{
       $parameters = $process->attribute( 'parameter_list' );
       /*  YOUR CODE GOES HERE */


       $twitterINI = eZINI::instance( 'mytwitter.ini' );
       $twitterDebugOutput = $twitterINI->variable( 'TwitterSettings', 'DebugOutput' );

       eZLog::write( "Entering eztwitter workflow" );
       $twitterConsumerKey = $twitterINI->variable( 'TwitterSettings', 'ConsumerKey' );
       $twitterConsumerSecret = $twitterINI->variable( 'TwitterSettings', 'ConsumerSecret');
       $twitterAccessToken = $twitterINI->variable( 'TwitterSettings', 'AccessToken' );
       $twitterAccessSecret = $twitterINI->variable( 'TwitterSettings', 'AccessSecret' );

       if( empty( $twitterConsumerKey ) || 
           empty( $twitterConsumerSecret ) || 
           empty( $twitterAccessToken ) || 
           empty( $twitterAccessSecret ) ) 
       {
              if( $twitterDebugOutput == 'enabled' ) 
                    eZLog::write( "Please configure mytwitter.ini" );    
       }
       if( $twitterDebugOutput == 'enabled' )
             eZLog::write( "Credentials found in mytwitter.ini" );


       $twitter = new Arc90_Service_Twitter();
       $twitter->useOAuth( $twitterConsumerKey, 
                           $twitterConsumerSecret, 
                           $twitterAccessToken, 
                           $twitterAccessSecret );
 

As we are using an external 3rd party library, PHP will most likely output an error message if the library is including its own PHP file even with the autoload process already updated. To fix this, we could either edit all PHP files from the library and remove all the include() or require() calls and let the autoload take care of this. Or we can also edit the config.php file at the root directory of our eZ Publish installation and set the correct ‘include_path’ at the end of the file:

ini_set( 'include_path', ini_get('include_path'). PATH_SEPARATOR . '/path/to/ezpublish/extension/mytwitter/lib' );