eZ Publish  [trunk]
ezworkflowtype.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZWorkflowType class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 //!! eZKernel
00012 //! The class eZWorkflowType does
00013 /*!
00014 
00015 */
00016 
00017 class eZWorkflowType
00018 {
00019     const STATUS_NONE = 0;
00020     const STATUS_ACCEPTED = 1;
00021     const STATUS_REJECTED = 2;
00022     const STATUS_DEFERRED_TO_CRON = 3;
00023     const STATUS_DEFERRED_TO_CRON_REPEAT = 4;
00024     const STATUS_RUN_SUB_EVENT = 5;
00025     const STATUS_WORKFLOW_CANCELLED = 6;
00026     const STATUS_FETCH_TEMPLATE = 7;
00027     const STATUS_FETCH_TEMPLATE_REPEAT = 8;
00028     const STATUS_REDIRECT = 10;
00029     const STATUS_WORKFLOW_DONE = 9;
00030     const STATUS_REDIRECT_REPEAT = 11;
00031     const STATUS_WORKFLOW_RESET = 12;
00032 
00033     function eZWorkflowType( $group, $type,
00034                              $groupName, $name )
00035     {
00036         $this->Group = $group;
00037         $this->Type = $type;
00038         $this->TypeString = $group . "_" . $type;
00039         $this->GroupName = $groupName;
00040         $this->Name = $name;
00041         $this->Information = "";
00042         $this->ActivationDate = false;
00043         $this->Attributes = array();
00044         $this->Attributes["group"] =& $this->Group;
00045         $this->Attributes["type"] =& $this->Type;
00046         $this->Attributes["type_string"] =& $this->TypeString;
00047         $this->Attributes["group_name"] =& $this->GroupName;
00048         $this->Attributes["name"] =& $this->Name;
00049         $this->Attributes["information"] =& $this->Information;
00050         $this->Attributes["activation_date"] =& $this->ActivationDate;
00051     }
00052 
00053     static function statusName( $status )
00054     {
00055         $statusNameMap = self::statusNameMap();
00056         if ( isset( $statusNameMap[$status] ) )
00057             return $statusNameMap[$status];
00058         return false;
00059     }
00060 
00061     static function createType( $typeString )
00062     {
00063         $types =& $GLOBALS["eZWorkflowTypes"];
00064         if ( !isset( $types[$typeString] ) )
00065         {
00066             $result = eZWorkflowType::loadAndRegisterType( $typeString );
00067             if ( $result === false )
00068                 return null;
00069         }
00070 
00071         if ( isset( $types[$typeString] ) )
00072         {
00073             $class_name = $types[$typeString]["class_name"];
00074 
00075             if ( !isset( $GLOBALS["eZWorkflowTypeObjects"][$typeString] ) )
00076             {
00077                 if ( class_exists( $class_name ) )
00078                 {
00079                     $GLOBALS["eZWorkflowTypeObjects"][$typeString] = new $class_name();
00080                 }
00081                 else
00082                 {
00083                     eZDebug::writeError( "Undefined event type class: $class_name", __METHOD__ );
00084                 }
00085             }
00086             return $GLOBALS["eZWorkflowTypeObjects"][$typeString];
00087         }
00088         else
00089         {
00090             eZDebug::writeError( "Undefined type: $typeString", __METHOD__ );
00091         }
00092         return null;
00093     }
00094 
00095     static function fetchRegisteredTypes()
00096     {
00097         eZWorkflowType::loadAndRegisterAllTypes();
00098         $types = $GLOBALS["eZWorkflowTypes"];
00099         if ( is_array( $types ) )
00100         {
00101             foreach ( $types as $typeString => $type_def )
00102             {
00103                 $class_name = $type_def["class_name"];
00104                 $def =& $definition_objects[$typeString];
00105                 if ( !isset( $GLOBALS["eZWorkflowTypeObjects"][$typeString] ) )
00106                 {
00107                     if ( class_exists( $class_name ) )
00108                     {
00109                         $GLOBALS["eZWorkflowTypeObjects"][$typeString] = new $class_name();
00110                     }
00111                     else
00112                     {
00113                         eZDebug::writeError( "Undefined event type class: $class_name", __METHOD__ );
00114                     }
00115                 }
00116             }
00117         }
00118         return $GLOBALS["eZWorkflowTypeObjects"];
00119     }
00120 
00121     static function allowedTypes()
00122     {
00123         if ( !isset( $GLOBALS["eZWorkflowAllowedTypes"] ) ||
00124              !is_array( $GLOBALS["eZWorkflowAllowedTypes"] ) )
00125         {
00126             $wfINI = eZINI::instance( 'workflow.ini' );
00127             $eventTypes = $wfINI->variable( "EventSettings", "AvailableEventTypes" );
00128             $GLOBALS["eZWorkflowAllowedTypes"] = array_unique( $eventTypes );
00129         }
00130         return $GLOBALS["eZWorkflowAllowedTypes"];
00131     }
00132 
00133     static function loadAndRegisterAllTypes()
00134     {
00135         $allowedTypes = eZWorkflowType::allowedTypes();
00136         foreach( $allowedTypes as $type )
00137         {
00138             eZWorkflowType::loadAndRegisterType( $type );
00139         }
00140     }
00141 
00142     static function registerType( $group, $type, $class_name )
00143     {
00144         $typeString = $group . "_" . $type;
00145         if ( !isset( $GLOBALS["eZWorkflowTypes"] ) || !is_array( $GLOBALS["eZWorkflowTypes"] ) )
00146         {
00147             $GLOBALS["eZWorkflowTypes"] = array();
00148         }
00149         if ( isset( $GLOBALS["eZWorkflowTypes"][$typeString] ) )
00150         {
00151             eZDebug::writeError( "Type already registered: $typeString", __METHOD__ );
00152         }
00153         else
00154         {
00155             $GLOBALS["eZWorkflowTypes"][$typeString] = array( "class_name" => $class_name );
00156         }
00157     }
00158 
00159     static function loadAndRegisterType( $typeString )
00160     {
00161         $typeElements = explode( "_", $typeString );
00162         if ( count( $typeElements ) < 2 )
00163         {
00164             eZDebug::writeError( "Workflow type not found: $typeString", __METHOD__ );
00165             return false;
00166         }
00167 
00168         $types =& $GLOBALS["eZWorkflowTypes"];
00169         if ( isset( $types[ $typeString ] ) and
00170              isset( $types[ $typeString ][ 'class_name' ] ) and
00171              class_exists( $types[ $typeString ][ 'class_name' ] ) )
00172         {
00173             return true;
00174         }
00175 
00176         $group = $typeElements[0];
00177         $type = $typeElements[1];
00178 
00179         $baseDirectory = eZExtension::baseDirectory();
00180         $wfINI = eZINI::instance( 'workflow.ini' );
00181         $repositoryDirectories = $wfINI->variable( 'EventSettings', 'RepositoryDirectories' );
00182         $extensionDirectories = $wfINI->variable( 'EventSettings', 'ExtensionDirectories' );
00183         foreach ( $extensionDirectories as $extensionDirectory )
00184         {
00185             $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/eventtypes';
00186             if ( file_exists( $extensionPath ) )
00187                 $repositoryDirectories[] = $extensionPath;
00188         }
00189 
00190         foreach ( $repositoryDirectories as $repositoryDirectory )
00191         {
00192             $includeFile = "$repositoryDirectory/$group/$type/" . $type . "type.php";
00193             if ( file_exists( $includeFile ) )
00194             {
00195                 include_once( $includeFile );
00196                 return true;
00197             }
00198         }
00199 
00200         eZDebug::writeError( "Workflow type not found: $typeString, searched in these directories: " . implode( ', ', $repositoryDirectories ), __METHOD__ );
00201         return false;
00202     }
00203 
00204 
00205     function attributes()
00206     {
00207         return array_merge( array( 'description',
00208                                    'allowed_triggers' ),
00209                             array_keys( $this->Attributes ) );
00210     }
00211 
00212     function hasAttribute( $attr )
00213     {
00214         return in_array( $attr, $this->attributes() );
00215     }
00216 
00217     function attribute( $attr )
00218     {
00219         switch( $attr )
00220         {
00221             case 'description':
00222             {
00223                 return $this->eventDescription();
00224             } break;
00225 
00226             case 'allowed_triggers':
00227             {
00228                 return $this->TriggerTypes;
00229             } break;
00230 
00231             default:
00232             {
00233                 if ( isset( $this->Attributes[$attr] ) )
00234                     return $this->Attributes[$attr];
00235             } break;
00236         }
00237 
00238         eZDebug::writeError( "Attribute '$attr' does not exist", __METHOD__ );
00239         return null;
00240     }
00241 
00242     function setAttribute( $attr, $value )
00243     {
00244         if ( array_key_exists( $attr, $this->Attributes ) )
00245             $this->Attributes[$attr] = $value;
00246     }
00247 
00248     /*!
00249      Set trigger types.
00250 
00251      \param allowed trigger types format :
00252               array( <module> => array( <function> => array( <event> ) ) )
00253             if all is allowed,
00254               array( '*' => true )
00255      */
00256     function setTriggerTypes( $allowedTypes )
00257     {
00258         $this->TriggerTypes = $allowedTypes;
00259     }
00260 
00261     function eventDescription()
00262     {
00263         return $this->Attributes["name"];
00264     }
00265 
00266     function execute( $process, $event )
00267     {
00268         return eZWorkflowType::STATUS_NONE;
00269     }
00270 
00271     function initializeEvent( $event )
00272     {
00273     }
00274 
00275     function validateHTTPInput( $http, $base, $event, &$validation )
00276     {
00277         return eZInputValidator::STATE_ACCEPTED;
00278     }
00279 
00280     function fixupHTTPInput( $http, $base, $event )
00281     {
00282         return true;
00283     }
00284 
00285     function fetchHTTPInput( $http, $base, $event )
00286     {
00287     }
00288 
00289     function setActivationDate( $date )
00290     {
00291         $this->ActivationDate = $date;
00292     }
00293 
00294     function setInformation( $inf )
00295     {
00296         $this->Information = $inf;
00297     }
00298 
00299     function needCleanup()
00300     {
00301         return false;
00302     }
00303 
00304     function cleanupAfterRemoving( $attr = array() )
00305     {
00306     }
00307 
00308     function cleanup( $process, $event )
00309     {
00310     }
00311 
00312     function attributeDecoder( $event, $attr )
00313     {
00314         return null;
00315     }
00316 
00317     function typeFunctionalAttributes( )
00318     {
00319         return array();
00320     }
00321 
00322     function customWorkflowEventHTTPAction( $http, $action, $workflowEvent )
00323     {
00324     }
00325 
00326     function workflowEventContent( $event )
00327     {
00328         return "";
00329     }
00330     function storeEventData( $event, $version )
00331     {
00332     }
00333     function storeDefinedEventData( $event )
00334     {
00335     }
00336     /*!
00337      Check if specified trigger is allowed
00338 
00339       \param $moduleName module name
00340       \param $functionName function name
00341       \param $connectType connection type
00342 
00343      \return true is allowed, false if not.
00344     */
00345     function isAllowed( $moduleName, $functionName, $connectType )
00346     {
00347         if ( isset( $this->TriggerTypes['*'] ) )
00348         {
00349             return true;
00350         }
00351         else if ( isset( $this->TriggerTypes[$moduleName] ) )
00352         {
00353             if ( isset( $this->TriggerTypes[$moduleName][$functionName] ) )
00354             {
00355                 if ( in_array( $connectType, $this->TriggerTypes[$moduleName][$functionName] ) )
00356                 {
00357                     return true;
00358                 }
00359             }
00360         }
00361 
00362         return false;
00363     }
00364 
00365     /**
00366      * Get status name map.
00367      *
00368      * @return array Status name map
00369      */
00370     static function statusNameMap()
00371     {
00372         return array( self::STATUS_NONE => ezpI18n::tr( 'kernel/classes', 'No state yet' ),
00373                       self::STATUS_ACCEPTED => ezpI18n::tr( 'kernel/classes', 'Accepted event' ),
00374                       self::STATUS_REJECTED => ezpI18n::tr( 'kernel/classes', 'Rejected event' ),
00375                       self::STATUS_DEFERRED_TO_CRON => ezpI18n::tr( 'kernel/classes', 'Event deferred to cron job' ),
00376                       self::STATUS_DEFERRED_TO_CRON_REPEAT => ezpI18n::tr( 'kernel/classes', 'Event deferred to cron job, event will be rerun' ),
00377                       self::STATUS_RUN_SUB_EVENT => ezpI18n::tr( 'kernel/classes', 'Event runs a sub event' ),
00378                       self::STATUS_WORKFLOW_CANCELLED => ezpI18n::tr( 'kernel/classes', 'Canceled whole workflow' ),
00379                       self::STATUS_WORKFLOW_RESET => ezpI18n::tr( 'kernel/classes', 'Workflow was reset for reuse' ) );
00380     }
00381 
00382     /// \privatesection
00383     public $Group;
00384     public $Type;
00385     public $TypeString;
00386     public $GroupName;
00387     public $Name;
00388     public $ActivationDate;
00389     public $Information;
00390     public $TriggerTypes = array( '*' => true );
00391 }
00392 
00393 ?>