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 include_once( "lib/ezdb/classes/ezdb.php" );
00038 include_once( "kernel/classes/ezpersistentobject.php" );
00039 include_once( "kernel/classes/ezworkflowevent.php" );
00040 include_once( 'kernel/classes/ezworkflowgrouplink.php' );
00041
00042 define( "EZ_WORKFLOW_STATUS_NONE", 0 );
00043 define( "EZ_WORKFLOW_STATUS_BUSY", 1 );
00044 define( "EZ_WORKFLOW_STATUS_DONE", 2 );
00045 define( "EZ_WORKFLOW_STATUS_FAILED", 3 );
00046 define( "EZ_WORKFLOW_STATUS_DEFERRED_TO_CRON", 4 );
00047 define( "EZ_WORKFLOW_STATUS_CANCELLED", 5 );
00048 define( "EZ_WORKFLOW_STATUS_FETCH_TEMPLATE", 6 );
00049 define( "EZ_WORKFLOW_STATUS_REDIRECT", 7 );
00050 define( "EZ_WORKFLOW_STATUS_RESET", 8 );
00051 define( "EZ_WORKFLOW_STATUS_WAITING_PARENT", 9 );
00052
00053 class eZWorkflow extends eZPersistentObject
00054 {
00055 function eZWorkflow( $row )
00056 {
00057 $this->eZPersistentObject( $row );
00058 }
00059
00060 function definition()
00061 {
00062 return array( "fields" => array( "id" => array( 'name' => 'ID',
00063 'datatype' => 'integer',
00064 'default' => 0,
00065 'required' => true ),
00066 "version" => array( 'name' => "Version",
00067 'datatype' => 'integer',
00068 'default' => 0,
00069 'required' => true ),
00070 "is_enabled" => array( 'name' => "IsEnabled",
00071 'datatype' => 'integer',
00072 'default' => 0,
00073 'required' => true ),
00074 "workflow_type_string" => array( 'name' => "WorkflowTypeString",
00075 'datatype' => 'string',
00076 'default' => '',
00077 'required' => true,
00078 'max_length' => 50 ),
00079 "name" => array( 'name' => "Name",
00080 'datatype' => 'string',
00081 'default' => '',
00082 'required' => true,
00083 'max_length' => 255 ),
00084 "creator_id" => array( 'name' => "CreatorID",
00085 'datatype' => 'integer',
00086 'default' => 0,
00087 'required' => true,
00088 'foreign_class' => 'eZUser',
00089 'foreign_attribute' => 'contentobject_id',
00090 'multiplicity' => '1..*' ),
00091 "modifier_id" => array( 'name' => "ModifierID",
00092 'datatype' => 'integer',
00093 'default' => 0,
00094 'required' => true,
00095 'foreign_class' => 'eZUser',
00096 'foreign_attribute' => 'contentobject_id',
00097 'multiplicity' => '1..*' ),
00098 "created" => array( 'name' => "Created",
00099 'datatype' => 'integer',
00100 'default' => 0,
00101 'required' => true ),
00102 "modified" => array( 'name' => "Modified",
00103 'datatype' => 'integer',
00104 'default' => 0,
00105 'required' => true ) ),
00106 "keys" => array( "id", "version" ),
00107 'function_attributes' => array( 'creator' => 'creator',
00108 'modifier' => 'modifier',
00109 'workflow_type' => 'workflowType',
00110 'event_count' => 'fetchEventCount',
00111 'ordered_event_list' => 'fetchEvents',
00112 'ingroup_list' => 'ingroupList',
00113 'ingroup_id_list' => 'ingroupIDList',
00114 'group_list' => 'groupList' ),
00115 "increment_key" => "id",
00116 "class_name" => "eZWorkflow",
00117 "sort" => array( "name" => "asc" ),
00118 "name" => "ezworkflow" );
00119 }
00120
00121 function statusName( $status )
00122 {
00123 include_once( 'kernel/workflow/ezworkflowfunctioncollection.php' );
00124 $statusNames = eZWorkflowFunctionCollection::fetchWorkflowStatuses();
00125 if ( isset( $statusNames[$status] ) )
00126 return $statusNames[$status];
00127 return false;
00128 }
00129
00130 function create( $user_id )
00131 {
00132 $date_time = time();
00133 $row = array(
00134 "id" => null,
00135 "workflow_type_string" => "group_ezserial",
00136 "version" => 1,
00137 "is_enabled" => 1,
00138 "name" => "",
00139 "creator_id" => $user_id,
00140 "modifier_id" => $user_id,
00141 "created" => $date_time,
00142 "modified" => $date_time );
00143 return new eZWorkflow( $row );
00144 }
00145
00146 function setIsEnabled( $enabled, $id = false, $version = 0 )
00147 {
00148 if ( $id === false )
00149 $id = $this->attribute( "id" );
00150 eZPersistentObject::updateObjectList( array(
00151 "definition" => eZWorkflow::definition(),
00152 "update_fields" => array( "is_enabled" => ( $enabled ? 1 : 0 ) ),
00153 "conditions" => array( "id" => $id,
00154 "version" => $version )
00155 )
00156 );
00157 }
00158
00159
00160
00161
00162
00163 function removeWorkflow( $id, $version )
00164 {
00165 eZPersistentObject::removeObject( eZWorkflow::definition(),
00166 array("id" => $id,
00167 "version" => $version ) );
00168 }
00169
00170
00171
00172
00173
00174 function remove( $remove_childs = false )
00175 {
00176 $db =& eZDB::instance();
00177 $db->begin();
00178 if ( is_array( $remove_childs ) or $remove_childs )
00179 {
00180 if ( is_array( $remove_childs ) )
00181 {
00182 $events =& $remove_childs;
00183 for ( $i = 0; $i < count( $events ); ++$i )
00184 {
00185 $event =& $events[$i];
00186 $event->remove();
00187 }
00188 }
00189 else
00190 {
00191 eZPersistentObject::removeObject( eZWorkflowEvent::definition(),
00192 array( "workflow_id" => $this->ID,
00193 "version" => $this->Version ) );
00194 }
00195 }
00196 eZPersistentObject::remove();
00197 $db->commit();
00198 }
00199
00200
00201
00202
00203
00204
00205
00206 function removeTemporary()
00207 {
00208 $version = 1;
00209 $temporaryWorkflows = eZWorkflow::fetchList( $version, null, true );
00210
00211 $db =& eZDB::instance();
00212 $db->begin();
00213 foreach ( $temporaryWorkflows as $workflow )
00214 {
00215 $workflow->remove( true );
00216 }
00217 eZPersistentObject::removeObject( eZWorkflowEvent::definition(),
00218 array( 'version' => $version ) );
00219 $db->commit();
00220 }
00221
00222
00223
00224
00225
00226 function removeEvents( $events = false, $id = false, $version = false )
00227 {
00228 if ( is_array( $events ) )
00229 {
00230 $db =& eZDB::instance();
00231 $db->begin();
00232 for ( $i = 0; $i < count( $events ); ++$i )
00233 {
00234 $event =& $events[$i];
00235 $event->remove();
00236 }
00237 $db->commit();
00238 }
00239 else
00240 {
00241 if ( $version === false )
00242 $version = $this->Version;
00243 if ( $id === false )
00244 $id = $this->ID;
00245 eZPersistentObject::removeObject( eZWorkflowEvent::definition(),
00246 array( "workflow_id" => $id,
00247 "version" => $version ) );
00248 }
00249 }
00250
00251 function adjustEventPlacements( &$events )
00252 {
00253 if ( !is_array( $events ) )
00254 return;
00255 for ( $i = 0; $i < count( $events ); ++$i )
00256 {
00257 $event =& $events[$i];
00258 $event->setAttribute( "placement", $i + 1 );
00259 }
00260 }
00261
00262
00263
00264
00265
00266 function store( $store_childs = false )
00267 {
00268 $db =& eZDB::instance();
00269 $db->begin();
00270 if ( is_array( $store_childs ) or $store_childs )
00271 {
00272 if ( is_array( $store_childs ) )
00273 $events =& $store_childs;
00274 else
00275 $events =& $this->fetchEvents();
00276 for ( $i = 0; $i < count( $events ); ++$i )
00277 {
00278 $event =& $events[$i];
00279 $event->store();
00280 }
00281 }
00282 eZPersistentObject::store();
00283 $db->commit();
00284 }
00285
00286
00287
00288
00289 function storeDefined( $store_childs = false )
00290 {
00291 $db = eZDB::instance();
00292 $db->begin();
00293 if ( is_array( $store_childs ) or $store_childs )
00294 {
00295 if ( is_array( $store_childs ) )
00296 {
00297 $events = $store_childs;
00298 }
00299 else
00300 {
00301 $events = $this->fetchEvents();
00302 }
00303 foreach ( $events as $event )
00304 {
00305 $event->storeDefined();
00306 }
00307 }
00308 eZPersistentObject::store();
00309 $db->commit();
00310 }
00311
00312 function setVersion( $version, $set_childs = false )
00313 {
00314 if ( is_array( $set_childs ) or $set_childs )
00315 {
00316 if ( is_array( $set_childs ) )
00317 $events =& $set_childs;
00318 else
00319 $events =& $this->fetchEvents();
00320 for ( $i = 0; $i < count( $events ); ++$i )
00321 {
00322 $event =& $events[$i];
00323 $event->setAttribute( "version", $version );
00324 }
00325 }
00326 eZPersistentObject::setAttribute( "version", $version );
00327 }
00328
00329 function fetch( $id, $asObject = true, $version = 0 )
00330 {
00331 return eZPersistentObject::fetchObject( eZWorkflow::definition(),
00332 null,
00333 array( "id" => $id,
00334 "version" => $version ),
00335 $asObject );
00336 }
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 function &fetchLimited( $moduleName, $functionName, $connectType )
00349 {
00350 $workflowArray = eZWorkflow::fetchList();
00351 $returnArray = array();
00352
00353 foreach ( array_keys( $workflowArray ) as $key )
00354 {
00355 if ( $workflowArray[$key]->isAllowed( $moduleName,
00356 $functionName,
00357 $connectType ) )
00358 {
00359 $returnArray[] = $workflowArray[$key];
00360 }
00361 }
00362
00363 return $returnArray;
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 function isAllowed( $moduleName, $functionName, $connectType )
00376 {
00377 $eventArray =& $this->fetchEvents();
00378
00379 foreach ( array_keys( $eventArray ) as $key )
00380 {
00381 $eventType =& $eventArray[$key]->attribute( 'workflow_type' );
00382 if ( !is_object( $eventType ) or !$eventType->isAllowed( $moduleName, $functionName, $connectType ) )
00383 {
00384 return false;
00385 }
00386 }
00387
00388 return true;
00389 }
00390
00391 function fetchList( $version = 0, $enabled = 1, $asObject = true )
00392 {
00393 $conds = array( 'version' => $version );
00394 if ( $enabled !== null )
00395 $conds['is_enabled'] = $enabled;
00396 return eZPersistentObject::fetchObjectList( eZWorkflow::definition(),
00397 null, $conds, null, null,
00398 $asObject );
00399 }
00400
00401 function &fetchListCount( $version = 0, $enabled = 1 )
00402 {
00403 $list = eZPersistentObject::fetchObjectList( eZWorkflow::definition(),
00404 array(),
00405 array( 'version' => $version,
00406 'is_enabled' => $enabled ),
00407 false,
00408 null,
00409 false,
00410 null,
00411 array( array( 'operation' => 'count( id )',
00412 'name' => 'count' ) ) );
00413 return $list[0]["count"];
00414 }
00415
00416 function fetchEventIndexed( $index )
00417 {
00418 $id = $this->ID;
00419 eZDebugSetting::writeDebug( 'workflow-event', $index, 'index in fetchEventIndexed' );
00420 $list = eZPersistentObject::fetchObjectList( eZWorkflowEvent::definition(),
00421 array( "id", "placement" ),
00422 array( "workflow_id" => $id,
00423 "version" => 0 ),
00424 array( "placement" => "asc" ),
00425 array( "offset" => $index - 1,
00426 "length" => 1 ),
00427 false );
00428
00429 eZDebugSetting::writeDebug( 'workflow-event', $list, "event indexed" );
00430 if ( count( $list ) > 0 )
00431 return $list[$index - 1]["id"];
00432 return null;
00433 }
00434
00435 function &fetchEvents( $id = false, $asObject = true, $version = 0 )
00436 {
00437 if ( $id === false )
00438 {
00439 if ( isset( $this ) and
00440 get_class( $this ) == "ezworkflow" )
00441 {
00442 $id = $this->ID;
00443 $version = $this->Version;
00444 }
00445 else
00446 {
00447 $retValue = null;
00448 return $retValue;
00449 }
00450 }
00451 $filteredList = eZWorkflowEvent::fetchFilteredList( array( "workflow_id" => $id,
00452 "version" => $version ) );
00453 return $filteredList;
00454 }
00455
00456 function &fetchEventCount( $id = false, $version = 0 )
00457 {
00458 if ( $id === false )
00459 {
00460 if ( isset( $this ) and
00461 get_class( $this ) == "ezworkflow" )
00462 {
00463 $id = $this->ID;
00464 $version = $this->Version;
00465 }
00466 else
00467 {
00468 $count = null;
00469 return $count;
00470 }
00471 }
00472 $list = eZPersistentObject::fetchObjectList( eZWorkflowEvent::definition(),
00473 array(),
00474 array( 'version' => $version,
00475 'workflow_id' => $id ),
00476 false,
00477 null,
00478 false,
00479 false,
00480 array( array( 'operation' => 'count( id )',
00481 'name' => 'count' ) ) );
00482 return $list[0]["count"];
00483 }
00484
00485 function &creator()
00486 {
00487 if ( isset( $this->CreatorID ) and $this->CreatorID )
00488 {
00489 include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00490 $user = eZUser::fetch( $this->CreatorID );
00491 }
00492 else
00493 $user = null;
00494 return $user;
00495 }
00496
00497 function &modifier()
00498 {
00499 if ( isset( $this->ModifierID ) and $this->ModifierID )
00500 {
00501 include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
00502 $user = eZUser::fetch( $this->ModifierID );
00503 }
00504 else
00505 $user = null;
00506 return $user;
00507 }
00508
00509 function &ingroupList()
00510 {
00511 $this->InGroups = eZWorkflowGroupLink::fetchGroupList( $this->attribute("id"),
00512 $this->attribute("version"),
00513 true );
00514 return $this->InGroups;
00515 }
00516
00517 function &ingroupIDList()
00518 {
00519 $list = eZWorkflowGroupLink::fetchGroupList( $this->attribute("id"),
00520 $this->attribute("version"),
00521 false );
00522
00523 $this->InGroupIDs = array();
00524 foreach ( $list as $item )
00525 {
00526 $this->InGroupIDs[] = $item['group_id'];
00527 }
00528 return $this->InGroupIDs;
00529 }
00530
00531 function &groupList()
00532 {
00533 $this->AllGroups = eZWorkflowGroup::fetchList();
00534 return $this->AllGroups;
00535 }
00536
00537 function &workflowType()
00538 {
00539 include_once( "kernel/classes/ezworkflowtype.php" );
00540 $workflowType =& eZWorkflowType::createType( $this->WorkflowTypeString );
00541 return $workflowType;
00542 }
00543
00544
00545
00546
00547
00548 function cleanupWorkFlowProcess()
00549 {
00550 if ( isset( $this ) )
00551 {
00552 $db =& eZDB::instance();
00553 $workflowID = $this->attribute( 'id' );
00554 $event_list =& $this->fetchEvents();
00555 if ( $event_list != null )
00556 {
00557 $existEventIDArray = array();
00558 foreach ( array_keys( $event_list ) as $key )
00559 {
00560 $event =& $event_list[$key];
00561 $eventID = $event->attribute( 'id' );
00562 $existEventIDArray[] = $eventID;
00563 }
00564 $existEventIDString = implode( ',', $existEventIDArray );
00565 $db->query( "DELETE FROM ezworkflow_process
00566 WHERE workflow_id=$workflowID
00567 AND event_id not in ( $existEventIDString )" );
00568 }
00569 else
00570 {
00571 $db->query( "DELETE FROM ezworkflow_process
00572 WHERE workflow_id=$workflowID");
00573 }
00574 }
00575 }
00576
00577
00578 var $ID;
00579 var $Name;
00580 var $WorkflowTypeString;
00581 var $Version;
00582 var $IsEnabled;
00583 var $CreatorID;
00584 var $ModifierID;
00585 var $Created;
00586 var $Modified;
00587 var $InGroups;
00588 var $InGroupIDs;
00589 var $AllGroups;
00590 }
00591
00592 ?>