00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 include_once( 'kernel/classes/ezworkflowprocess.php' );
00040 include_once( 'kernel/classes/ezworkflow.php' );
00041
00042 define( "EZ_TRIGGER_STATUS_CRON_JOB", 0 );
00043 define( "EZ_TRIGGER_WORKFLOW_DONE", 1 );
00044 define( "EZ_TRIGGER_WORKFLOW_CANCELED", 2 );
00045 define( "EZ_TRIGGER_NO_CONNECTED_WORKFLOWS", 3 );
00046 define( "EZ_TRIGGER_FETCH_TEMPLATE", 4 );
00047 define( "EZ_TRIGGER_REDIRECT", 5 );
00048 define( "EZ_TRIGGER_WORKFLOW_RESET", 6 );
00049
00050
00051 class eZTrigger extends eZPersistentObject
00052 {
00053
00054
00055
00056 function eZTrigger( $row )
00057 {
00058 $this->eZPersistentObject( $row );
00059 }
00060
00061 function definition()
00062 {
00063 return array( "fields" => array( 'id' => array( 'name' => 'ID',
00064 'datatype' => 'integer',
00065 'default' => 0,
00066 'required' => true ),
00067 'module_name' => array( 'name' => 'ModuleName',
00068 'datatype' => 'string',
00069 'default' => '',
00070 'required' => true ),
00071 'function_name' => array( 'name' => 'FunctionName',
00072 'datatype' => 'string',
00073 'default' => '',
00074 'required' => true ),
00075 'connect_type' => array( 'name' => 'ConnectType',
00076 'datatype' => 'string',
00077 'default' => '',
00078 'required' => true ),
00079 'workflow_id' => array( 'name' => 'WorkflowID',
00080 'datatype' => 'integer',
00081 'default' => 0,
00082 'required' => true,
00083 'foreign_class' => 'eZWorkflow',
00084 'foreign_attribute' => 'id',
00085 'multiplicity' => '1..*' ),
00086 'name' => array( 'name' => 'Name',
00087 'datatype' => 'string',
00088 'default' => '',
00089 'required' => true ) ),
00090 "class_name" => "eZTrigger",
00091 "keys" => array( 'id' ),
00092 'function_attributes' => array( 'allowed_workflows' => 'fetchAllowedWorkflows' ),
00093 "increment_key" => "id",
00094 "name" => "eztrigger" );
00095 }
00096
00097
00098
00099
00100
00101
00102 function &fetchAllowedWorkflows()
00103 {
00104 $connectionType = '*';
00105 if ( $this->attribute( 'connect_type') == 'b' )
00106 {
00107 $connectionType = 'before';
00108 }
00109 else if ( $this->attribute( 'connect_type') == 'a' )
00110 {
00111 $connectionType = 'after';
00112 }
00113
00114 return eZWorkflow::fetchLimited( $this->attribute( 'module_name' ),
00115 $this->attribute( 'function_name' ),
00116 $connectionType );
00117 }
00118
00119 function fetch( $triggerID )
00120 {
00121 return eZPersistentObject::fetchObject( eZTrigger::definition(),
00122 null,
00123 array( 'id' => $triggerID ),
00124 true);
00125 }
00126 function fetchList( $parameters = array(), $asObject = true )
00127 {
00128 $filterArray = array();
00129 if ( array_key_exists('module', $parameters ) && $parameters[ 'module' ] != '*' )
00130 {
00131 $filterArray['module_name'] = $parameters['module'];
00132 }
00133 if ( array_key_exists('function', $parameters ) && $parameters[ 'function' ] != '*' )
00134 {
00135 $filterArray['function_name'] = $parameters['function'];
00136 }
00137 if ( array_key_exists('connectType', $parameters ) && $parameters[ 'connectType' ] != '*' )
00138 {
00139 $filterArray['connect_type'] = $parameters['connectType'];
00140 }
00141 if ( array_key_exists('name', $parameters ) && $parameters[ 'name' ] != '' )
00142 {
00143 $filterArray['name'] = $parameters['name'];
00144 }
00145 return eZPersistentObject::fetchObjectList( eZTrigger::definition(),
00146 null,
00147 $filterArray, array( 'module_name' => 'asc' ,
00148 'function_name' => 'asc',
00149 'connect_type' => 'asc' ),
00150 null,
00151 $asObject );
00152 }
00153
00154
00155
00156
00157
00158 function runTrigger( $name, $moduleName, $function, $parameters, $keys = null )
00159 {
00160 $trigger = eZPersistentObject::fetchObject( eZTrigger::definition(),
00161 null,
00162 array( 'name' => $name,
00163 'module_name' => $moduleName,
00164 'function_name' => $function ),
00165 true );
00166 if ( $trigger !== NULL )
00167 {
00168 $workflowID = $trigger->attribute( 'workflow_id' );
00169 $workflow = eZWorkflow::fetch( $workflowID );
00170 if ( $keys != null )
00171 {
00172 $keys[] = 'workflow_id';
00173 }
00174
00175 $parameters['workflow_id'] = $workflowID;
00176
00177
00178
00179 if ( !isset( $parameters['user_id'] ) or
00180 $parameters['user_id'] == 0 )
00181 {
00182 $user =& eZUser::currentUser();
00183 $parameters['user_id'] = $user->attribute( 'contentobject_id' );
00184 }
00185 $processKey = eZWorkflowProcess::createKey( $parameters, $keys );
00186
00187
00188
00189 $workflowProcessList = eZWorkflowProcess::fetchListByKey( $processKey );
00190
00191 if ( count( $workflowProcessList ) > 0 )
00192 {
00193 $existingWorkflowProcess =& $workflowProcessList[0];
00194 $existingWorkflowStatus = $existingWorkflowProcess->attribute( 'status' );
00195
00196
00197 switch( $existingWorkflowStatus )
00198 {
00199 case EZ_WORKFLOW_STATUS_FAILED:
00200 case EZ_WORKFLOW_STATUS_CANCELLED:
00201 case EZ_WORKFLOW_STATUS_NONE:
00202 case EZ_WORKFLOW_STATUS_BUSY:
00203 {
00204 $existingWorkflowProcess->remove();
00205 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED,
00206 'Result' => null );
00207 } break;
00208 case EZ_WORKFLOW_STATUS_FETCH_TEMPLATE:
00209 case EZ_WORKFLOW_STATUS_REDIRECT:
00210 case EZ_WORKFLOW_STATUS_RESET:
00211 {
00212 return eZTrigger::runWorkflow( $existingWorkflowProcess );
00213
00214 } break;
00215 case EZ_WORKFLOW_STATUS_DEFERRED_TO_CRON:
00216 {
00217 return eZTrigger::runWorkflow( $existingWorkflowProcess );
00218
00219
00220
00221
00222
00223 } break;
00224 case EZ_WORKFLOW_STATUS_DONE:
00225 {
00226 $existingWorkflowProcess->remove();
00227 return array( 'Status' => EZ_TRIGGER_WORKFLOW_DONE,
00228 'Result' => null );
00229 }
00230 }
00231 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED,
00232 'Result' => null );
00233 }else
00234 {
00235
00236
00237
00238 }
00239 $workflowProcess = eZWorkflowProcess::create( $processKey, $parameters );
00240
00241 $workflowProcess->store();
00242
00243 return eZTrigger::runWorkflow( $workflowProcess );
00244
00245 }
00246 else
00247 {
00248 return array( 'Status' => EZ_TRIGGER_NO_CONNECTED_WORKFLOWS,
00249 'Result' => null );
00250 }
00251 }
00252
00253
00254
00255
00256
00257 function runWorkflow( &$workflowProcess )
00258 {
00259 $workflow = eZWorkflow::fetch( $workflowProcess->attribute( "workflow_id" ) );
00260 $workflowEvent = null;
00261
00262 $workflowStatus = $workflowProcess->run( $workflow, $workflowEvent, $eventLog );
00263
00264 $db =& eZDB::instance();
00265 $db->begin();
00266 $workflowProcess->store();
00267
00268 switch ( $workflowStatus )
00269 {
00270 case EZ_WORKFLOW_STATUS_FAILED:
00271 case EZ_WORKFLOW_STATUS_CANCELLED:
00272 case EZ_WORKFLOW_STATUS_NONE:
00273 case EZ_WORKFLOW_STATUS_BUSY:
00274 {
00275 $workflowProcess->remove();
00276 $db->commit();
00277 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED,
00278 'Result' => null );
00279 } break;
00280 case EZ_WORKFLOW_STATUS_FETCH_TEMPLATE:
00281 {
00282 include_once( 'kernel/common/template.php' );
00283 $tpl =& templateInit();
00284 $result = array();
00285 foreach ( array_keys( $workflowProcess->Template['templateVars'] ) as $key )
00286 {
00287 $value =& $workflowProcess->Template['templateVars'][$key];
00288 $tpl->setVariable( $key, $value );
00289 }
00290 $result['content'] =& $tpl->fetch( $workflowProcess->Template['templateName'] );
00291 if ( isset( $workflowProcess->Template['path'] ) )
00292 $result['path'] = $workflowProcess->Template['path'];
00293
00294 $db->commit();
00295 return array( 'Status' => EZ_TRIGGER_FETCH_TEMPLATE,
00296 'WorkflowProcess' => &$workflowProcess,
00297 'Result' => $result );
00298 } break;
00299 case EZ_WORKFLOW_STATUS_REDIRECT:
00300 {
00301
00302 $db->commit();
00303 return array( 'Status' => EZ_TRIGGER_REDIRECT,
00304 'WorkflowProcess' => &$workflowProcess,
00305 'Result' => $workflowProcess->RedirectUrl );
00306
00307 } break;
00308 case EZ_WORKFLOW_STATUS_DEFERRED_TO_CRON:
00309 {
00310
00311 $db->commit();
00312 return array( 'Status' => EZ_TRIGGER_STATUS_CRON_JOB,
00313 'WorkflowProcess' => &$workflowProcess,
00314 'Result' => array( 'content' => 'Deffered to cron. Operation halted during execution. <br/>Refresh page to continue<br/><br/><b>Note: The halt is just a temporary test</b><br/>',
00315 'path' => array( array( 'text' => 'Operation halt',
00316 'url' => false ) ) ) );
00317
00318
00319
00320
00321 } break;
00322 case EZ_WORKFLOW_STATUS_RESET:
00323 {
00324 $db->commit();
00325 return array( 'Status' => EZ_TRIGGER_WORKFLOW_RESET,
00326 'WorkflowProcess' => &$workflowProcess,
00327 'Result' => array( 'content' => 'Workflow was reset',
00328 'path' => array( array( 'text' => 'Operation halt',
00329 'url' => false ) ) ) );
00330 } break;
00331 case EZ_WORKFLOW_STATUS_DONE:
00332 {
00333 $workflowProcess->remove();
00334 $db->commit();
00335 return array( 'Status' => EZ_TRIGGER_WORKFLOW_DONE,
00336 'Result' => null );
00337 }
00338 }
00339
00340 $db->commit();
00341 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED,
00342 'Result' => null );
00343
00344
00345
00346 }
00347
00348
00349
00350
00351
00352 function createNew( $moduleName, $functionName, $connectType, $workflowID, $name = false )
00353 {
00354 if ( !$name )
00355 {
00356 if ( $connectType == 'b' )
00357 {
00358 $name = 'pre_';
00359 }
00360 else if ( $connectType == 'a' )
00361 {
00362 $name = 'post_';
00363 }
00364 $name .= $functionName;
00365 }
00366 $trigger = new eZTrigger( array( 'module_name' => $moduleName,
00367 'function_name' => $functionName,
00368 'connect_type' => $connectType,
00369 'workflow_id' => $workflowID,
00370 'name' => $name ) );
00371 $trigger->store();
00372 return $trigger;
00373 }
00374
00375
00376
00377
00378
00379
00380 function removeTriggerForWorkflow( $workFlowID )
00381 {
00382 $db =& eZDB::instance();
00383 $workFlowID = (int)$workFlowID;
00384 $db->query( "DELETE FROM eztrigger WHERE workflow_id=$workFlowID" );
00385 }
00386 }
00387
00388 ?>