|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZTrigger class 00004 // 00005 // Created on: <11-Сен-2002 13:11:15 sp> 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 /*! \file eztrigger.php 00032 */ 00033 00034 /*! 00035 \class eZTrigger eztrigger.php 00036 \brief The class eZTrigger does 00037 00038 */ 00039 //include_once( 'kernel/classes/ezworkflowprocess.php' ); 00040 //include_once( 'kernel/classes/ezworkflow.php' ); 00041 00042 class eZTrigger extends eZPersistentObject 00043 { 00044 const STATUS_CRON_JOB = 0; 00045 const WORKFLOW_DONE = 1; 00046 const WORKFLOW_CANCELLED = 2; 00047 const NO_CONNECTED_WORKFLOWS = 3; 00048 const FETCH_TEMPLATE = 4; 00049 const REDIRECT = 5; 00050 const WORKFLOW_RESET = 6; 00051 const FETCH_TEMPLATE_REPEAT = 7; 00052 00053 /*! 00054 Constructor 00055 */ 00056 function eZTrigger( $row ) 00057 { 00058 $this->eZPersistentObject( $row ); 00059 } 00060 00061 static 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 Get array containing allowed workflows for this trigger. 00099 00100 \return array containing allowed workflows 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 static function fetch( $triggerID ) 00120 { 00121 return eZPersistentObject::fetchObject( eZTrigger::definition(), 00122 null, 00123 array( 'id' => $triggerID ), 00124 true); 00125 } 00126 00127 static function fetchList( $parameters = array(), $asObject = true ) 00128 { 00129 $filterArray = array(); 00130 if ( array_key_exists('module', $parameters ) && $parameters[ 'module' ] != '*' ) 00131 { 00132 $filterArray['module_name'] = $parameters['module']; 00133 } 00134 if ( array_key_exists('function', $parameters ) && $parameters[ 'function' ] != '*' ) 00135 { 00136 $filterArray['function_name'] = $parameters['function']; 00137 } 00138 if ( array_key_exists('connectType', $parameters ) && $parameters[ 'connectType' ] != '*' ) 00139 { 00140 $filterArray['connect_type'] = $parameters['connectType']; 00141 } 00142 if ( array_key_exists('name', $parameters ) && $parameters[ 'name' ] != '' ) 00143 { 00144 $filterArray['name'] = $parameters['name']; 00145 } 00146 return eZPersistentObject::fetchObjectList( eZTrigger::definition(), 00147 null, 00148 $filterArray, array( 'module_name' => 'asc' , 00149 'function_name' => 'asc', 00150 'connect_type' => 'asc' ), 00151 null, 00152 $asObject ); 00153 } 00154 00155 /*! 00156 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00157 the calls within a db transaction; thus within db->begin and db->commit. 00158 */ 00159 static function runTrigger( $name, $moduleName, $function, $parameters, $keys = null ) 00160 { 00161 $trigger = eZPersistentObject::fetchObject( eZTrigger::definition(), 00162 null, 00163 array( 'name' => $name, 00164 'module_name' => $moduleName, 00165 'function_name' => $function ), 00166 true ); 00167 if ( $trigger !== NULL ) 00168 { 00169 $workflowID = $trigger->attribute( 'workflow_id' ); 00170 $workflow = eZWorkflow::fetch( $workflowID ); 00171 if ( $keys != null ) 00172 { 00173 $keys[] = 'workflow_id'; 00174 } 00175 00176 $parameters['workflow_id'] = $workflowID; 00177 // It is very important that the user_id is set correctly. 00178 // If it was not supplied by the calling code we will use 00179 // the currently logged in user. 00180 if ( !isset( $parameters['user_id'] ) or 00181 $parameters['user_id'] == 0 ) 00182 { 00183 $user = eZUser::currentUser(); 00184 $parameters['user_id'] = $user->attribute( 'contentobject_id' ); 00185 } 00186 $processKey = eZWorkflowProcess::createKey( $parameters, $keys ); 00187 00188 // $searchKey = eZWorkflowProcess::createKey( $keyArray ); 00189 00190 $workflowProcessList = eZWorkflowProcess::fetchListByKey( $processKey ); 00191 00192 if ( count( $workflowProcessList ) > 0 ) 00193 { 00194 $existingWorkflowProcess = $workflowProcessList[0]; 00195 $existingWorkflowStatus = $existingWorkflowProcess->attribute( 'status' ); 00196 00197 00198 switch( $existingWorkflowStatus ) 00199 { 00200 case eZWorkflow::STATUS_FAILED: 00201 case eZWorkflow::STATUS_CANCELLED: 00202 case eZWorkflow::STATUS_NONE: 00203 case eZWorkflow::STATUS_BUSY: 00204 { 00205 $existingWorkflowProcess->removeThis(); 00206 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED, 00207 'Result' => null ); 00208 } break; 00209 case eZWorkflow::STATUS_FETCH_TEMPLATE: 00210 case eZWorkflow::STATUS_FETCH_TEMPLATE_REPEAT: 00211 case eZWorkflow::STATUS_REDIRECT: 00212 case eZWorkflow::STATUS_RESET: 00213 { 00214 return eZTrigger::runWorkflow( $existingWorkflowProcess ); 00215 // return eZTrigger::FETCH_TEMPLATE; 00216 } break; 00217 case eZWorkflow::STATUS_DEFERRED_TO_CRON: 00218 { 00219 return eZTrigger::runWorkflow( $existingWorkflowProcess ); 00220 /* return array( 'Status' => eZTrigger::STATUS_CRON_JOB, 00221 00222 'Result' => array( 'content' => 'Operation halted during execution.<br/>Refresh page to continue<br/><br/><b>Note: The halt is just a temporary test</b><br/>', 00223 'path' => array( array( 'text' => 'Operation halt', 00224 'url' => false ) ) ) ); 00225 */ } break; 00226 case eZWorkflow::STATUS_DONE: 00227 { 00228 $existingWorkflowProcess->removeThis(); 00229 return array( 'Status' => eZTrigger::WORKFLOW_DONE, 00230 'Result' => null ); 00231 } 00232 } 00233 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED, 00234 'Result' => null ); 00235 }else 00236 { 00237 // print( "\n starting new workflow process \n"); 00238 // var_dump( $keyArray ); 00239 // print( " $workflowID, $userID, $objectID, $version, $nodeID, \n "); 00240 } 00241 $workflowProcess = eZWorkflowProcess::create( $processKey, $parameters ); 00242 00243 $workflowProcess->store(); 00244 00245 return eZTrigger::runWorkflow( $workflowProcess ); 00246 00247 } 00248 else 00249 { 00250 return array( 'Status' => eZTrigger::NO_CONNECTED_WORKFLOWS, 00251 'Result' => null ); 00252 } 00253 } 00254 00255 /*! 00256 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00257 the calls within a db transaction; thus within db->begin and db->commit. 00258 */ 00259 static function runWorkflow( $workflowProcess ) 00260 { 00261 $workflow = eZWorkflow::fetch( $workflowProcess->attribute( "workflow_id" ) ); 00262 $workflowEvent = null; 00263 00264 $workflowStatus = $workflowProcess->run( $workflow, $workflowEvent, $eventLog ); 00265 00266 $db = eZDB::instance(); 00267 $db->begin(); 00268 $workflowProcess->store(); 00269 00270 switch ( $workflowStatus ) 00271 { 00272 case eZWorkflow::STATUS_FAILED: 00273 case eZWorkflow::STATUS_CANCELLED: 00274 case eZWorkflow::STATUS_NONE: 00275 case eZWorkflow::STATUS_BUSY: 00276 { 00277 $workflowProcess->removeThis(); 00278 $db->commit(); 00279 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED, 00280 'Result' => null ); 00281 } break; 00282 case eZWorkflow::STATUS_FETCH_TEMPLATE: 00283 case eZWorkflow::STATUS_FETCH_TEMPLATE_REPEAT: 00284 { 00285 require_once( 'kernel/common/template.php' ); 00286 $tpl = templateInit(); 00287 $result = array(); 00288 foreach ( array_keys( $workflowProcess->Template['templateVars'] ) as $key ) 00289 { 00290 $value = $workflowProcess->Template['templateVars'][$key]; 00291 $tpl->setVariable( $key, $value ); 00292 } 00293 $result['content'] = $tpl->fetch( $workflowProcess->Template['templateName'] ); 00294 if ( isset( $workflowProcess->Template['path'] ) ) 00295 $result['path'] = $workflowProcess->Template['path']; 00296 00297 $db->commit(); 00298 if ( $workflowStatus == eZWorkflow::STATUS_FETCH_TEMPLATE ) 00299 { 00300 $triggerStatus = eZTrigger::FETCH_TEMPLATE; 00301 } 00302 elseif ( $workflowStatus == eZWorkflow::STATUS_FETCH_TEMPLATE_REPEAT ) 00303 { 00304 $triggerStatus = eZTrigger::FETCH_TEMPLATE_REPEAT; 00305 } 00306 return array( 'Status' => $triggerStatus, 00307 'WorkflowProcess' => $workflowProcess, 00308 'Result' => $result ); 00309 } break; 00310 case eZWorkflow::STATUS_REDIRECT: 00311 { 00312 // var_dump( $workflowProcess->RedirectUrl ); 00313 $db->commit(); 00314 return array( 'Status' => eZTrigger::REDIRECT, 00315 'WorkflowProcess' => $workflowProcess, 00316 'Result' => $workflowProcess->RedirectUrl ); 00317 00318 } break; 00319 case eZWorkflow::STATUS_DEFERRED_TO_CRON: 00320 { 00321 00322 $db->commit(); 00323 return array( 'Status' => eZTrigger::STATUS_CRON_JOB, 00324 'WorkflowProcess' => $workflowProcess, 00325 '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/>', 00326 'path' => array( array( 'text' => 'Operation halt', 00327 'url' => false ) ) ) ); 00328 /* 00329 return array( 'Status' => eZTrigger::STATUS_CRON_JOB, 00330 'Result' => $workflowProcess->attribute( 'id') ); 00331 */ 00332 } break; 00333 case eZWorkflow::STATUS_RESET: 00334 { 00335 $db->commit(); 00336 return array( 'Status' => eZTrigger::WORKFLOW_RESET, 00337 'WorkflowProcess' => $workflowProcess, 00338 'Result' => array( 'content' => 'Workflow was reset', 00339 'path' => array( array( 'text' => 'Operation halt', 00340 'url' => false ) ) ) ); 00341 } break; 00342 case eZWorkflow::STATUS_DONE: 00343 { 00344 $workflowProcess->removeThis(); 00345 $db->commit(); 00346 return array( 'Status' => eZTrigger::WORKFLOW_DONE, 00347 'Result' => null ); 00348 } 00349 } 00350 00351 $db->commit(); 00352 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED, 00353 'Result' => null ); 00354 00355 00356 00357 } 00358 00359 /*! 00360 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00361 the calls within a db transaction; thus within db->begin and db->commit. 00362 */ 00363 static function createNew( $moduleName, $functionName, $connectType, $workflowID, $name = false ) 00364 { 00365 if ( !$name ) 00366 { 00367 if ( $connectType == 'b' ) 00368 { 00369 $name = 'pre_'; 00370 } 00371 else if ( $connectType == 'a' ) 00372 { 00373 $name = 'post_'; 00374 } 00375 $name .= $functionName; 00376 } 00377 $trigger = new eZTrigger( array( 'module_name' => $moduleName, 00378 'function_name' => $functionName, 00379 'connect_type' => $connectType, 00380 'workflow_id' => $workflowID, 00381 'name' => $name ) ); 00382 $trigger->store(); 00383 return $trigger; 00384 } 00385 00386 /*! 00387 Removes triggers which uses the given workflowID. 00388 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00389 the calls within a db transaction; thus within db->begin and db->commit. 00390 */ 00391 static function removeTriggerForWorkflow( $workFlowID ) 00392 { 00393 $db = eZDB::instance(); 00394 $workFlowID = (int)$workFlowID; 00395 $db->query( "DELETE FROM eztrigger WHERE workflow_id=$workFlowID" ); 00396 } 00397 } 00398 00399 ?>