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