[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZTrigger class 4 // 5 // Created on: <11-ÁÕÝ-2002 13:11:15 sp> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! \file eztrigger.php 30 */ 31 32 /*! 33 \class eZTrigger eztrigger.php 34 \brief The class eZTrigger does 35 36 */ 37 include_once ( 'kernel/classes/ezworkflowprocess.php' ); 38 include_once ( 'kernel/classes/ezworkflow.php' ); 39 40 define( "EZ_TRIGGER_STATUS_CRON_JOB", 0 ); 41 define( "EZ_TRIGGER_WORKFLOW_DONE", 1 ); 42 define( "EZ_TRIGGER_WORKFLOW_CANCELED", 2 ); 43 define( "EZ_TRIGGER_NO_CONNECTED_WORKFLOWS", 3 ); 44 define( "EZ_TRIGGER_FETCH_TEMPLATE", 4 ); 45 define( "EZ_TRIGGER_REDIRECT", 5 ); 46 define( "EZ_TRIGGER_WORKFLOW_RESET", 6 ); 47 48 49 class eZTrigger extends eZPersistentObject 50 { 51 /*! 52 Constructor 53 */ 54 function eZTrigger( $row ) 55 { 56 $this->eZPersistentObject( $row ); 57 } 58 59 function definition() 60 { 61 return array( "fields" => array( 'id' => array( 'name' => 'ID', 62 'datatype' => 'integer', 63 'default' => 0, 64 'required' => true ), 65 'module_name' => array( 'name' => 'ModuleName', 66 'datatype' => 'string', 67 'default' => '', 68 'required' => true ), 69 'function_name' => array( 'name' => 'FunctionName', 70 'datatype' => 'string', 71 'default' => '', 72 'required' => true ), 73 'connect_type' => array( 'name' => 'ConnectType', 74 'datatype' => 'string', 75 'default' => '', 76 'required' => true ), 77 'workflow_id' => array( 'name' => 'WorkflowID', 78 'datatype' => 'integer', 79 'default' => 0, 80 'required' => true, 81 'foreign_class' => 'eZWorkflow', 82 'foreign_attribute' => 'id', 83 'multiplicity' => '1..*' ), 84 'name' => array( 'name' => 'Name', 85 'datatype' => 'string', 86 'default' => '', 87 'required' => true ) ), 88 "class_name" => "eZTrigger", 89 "keys" => array( 'id' ), 90 'function_attributes' => array( 'allowed_workflows' => 'fetchAllowedWorkflows' ), 91 "increment_key" => "id", 92 "name" => "eztrigger" ); 93 } 94 95 /*! 96 Get array containing allowed workflows for this trigger. 97 98 \return array containing allowed workflows 99 */ 100 function &fetchAllowedWorkflows() 101 { 102 $connectionType = '*'; 103 if ( $this->attribute( 'connect_type') == 'b' ) 104 { 105 $connectionType = 'before'; 106 } 107 else if ( $this->attribute( 'connect_type') == 'a' ) 108 { 109 $connectionType = 'after'; 110 } 111 112 return eZWorkflow::fetchLimited( $this->attribute( 'module_name' ), 113 $this->attribute( 'function_name' ), 114 $connectionType ); 115 } 116 117 function fetch( $triggerID ) 118 { 119 return eZPersistentObject::fetchObject( eZTrigger::definition(), 120 null, 121 array( 'id' => $triggerID ), 122 true); 123 } 124 function fetchList( $parameters = array(), $asObject = true ) 125 { 126 $filterArray = array(); 127 if ( array_key_exists('module', $parameters ) && $parameters[ 'module' ] != '*' ) 128 { 129 $filterArray['module_name'] = $parameters['module']; 130 } 131 if ( array_key_exists('function', $parameters ) && $parameters[ 'function' ] != '*' ) 132 { 133 $filterArray['function_name'] = $parameters['function']; 134 } 135 if ( array_key_exists('connectType', $parameters ) && $parameters[ 'connectType' ] != '*' ) 136 { 137 $filterArray['connect_type'] = $parameters['connectType']; 138 } 139 if ( array_key_exists('name', $parameters ) && $parameters[ 'name' ] != '' ) 140 { 141 $filterArray['name'] = $parameters['name']; 142 } 143 return eZPersistentObject::fetchObjectList( eZTrigger::definition(), 144 null, 145 $filterArray, array( 'module_name' => 'asc' , 146 'function_name' => 'asc', 147 'connect_type' => 'asc' ), 148 null, 149 $asObject ); 150 } 151 152 /*! 153 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 154 the calls within a db transaction; thus within db->begin and db->commit. 155 */ 156 function runTrigger( $name, $moduleName, $function, $parameters, $keys = null ) 157 { 158 $trigger = eZPersistentObject::fetchObject( eZTrigger::definition(), 159 null, 160 array( 'name' => $name, 161 'module_name' => $moduleName, 162 'function_name' => $function ), 163 true ); 164 if ( $trigger !== NULL ) 165 { 166 $workflowID = $trigger->attribute( 'workflow_id' ); 167 $workflow = eZWorkflow::fetch( $workflowID ); 168 if ( $keys != null ) 169 { 170 $keys[] = 'workflow_id'; 171 } 172 173 $parameters['workflow_id'] = $workflowID; 174 // It is very important that the user_id is set correctly. 175 // If it was not supplied by the calling code we will use 176 // the currently logged in user. 177 if ( !isset( $parameters['user_id'] ) or 178 $parameters['user_id'] == 0 ) 179 { 180 $user =& eZUser::currentUser(); 181 $parameters['user_id'] = $user->attribute( 'contentobject_id' ); 182 } 183 $processKey = eZWorkflowProcess::createKey( $parameters, $keys ); 184 185 // $searchKey = eZWorkflowProcess::createKey( $keyArray ); 186 187 $workflowProcessList = eZWorkflowProcess::fetchListByKey( $processKey ); 188 189 if ( count( $workflowProcessList ) > 0 ) 190 { 191 $existingWorkflowProcess =& $workflowProcessList[0]; 192 $existingWorkflowStatus = $existingWorkflowProcess->attribute( 'status' ); 193 194 195 switch( $existingWorkflowStatus ) 196 { 197 case EZ_WORKFLOW_STATUS_FAILED: 198 case EZ_WORKFLOW_STATUS_CANCELLED: 199 case EZ_WORKFLOW_STATUS_NONE: 200 case EZ_WORKFLOW_STATUS_BUSY: 201 { 202 $existingWorkflowProcess->remove(); 203 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED, 204 'Result' => null ); 205 } break; 206 case EZ_WORKFLOW_STATUS_FETCH_TEMPLATE: 207 case EZ_WORKFLOW_STATUS_REDIRECT: 208 case EZ_WORKFLOW_STATUS_RESET: 209 { 210 return eZTrigger::runWorkflow( $existingWorkflowProcess ); 211 // return EZ_TRIGGER_FETCH_TEMPLATE; 212 } break; 213 case EZ_WORKFLOW_STATUS_DEFERRED_TO_CRON: 214 { 215 return eZTrigger::runWorkflow( $existingWorkflowProcess ); 216 /* return array( 'Status' => EZ_TRIGGER_STATUS_CRON_JOB, 217 218 '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/>', 219 'path' => array( array( 'text' => 'Operation halt', 220 'url' => false ) ) ) ); 221 */ } break; 222 case EZ_WORKFLOW_STATUS_DONE: 223 { 224 $existingWorkflowProcess->remove(); 225 return array( 'Status' => EZ_TRIGGER_WORKFLOW_DONE, 226 'Result' => null ); 227 } 228 } 229 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED, 230 'Result' => null ); 231 }else 232 { 233 // print( "\n starting new workflow process \n"); 234 // var_dump( $keyArray ); 235 // print( " $workflowID, $userID, $objectID, $version, $nodeID, \n "); 236 } 237 $workflowProcess = eZWorkflowProcess::create( $processKey, $parameters ); 238 239 $workflowProcess->store(); 240 241 return eZTrigger::runWorkflow( $workflowProcess ); 242 243 } 244 else 245 { 246 return array( 'Status' => EZ_TRIGGER_NO_CONNECTED_WORKFLOWS, 247 'Result' => null ); 248 } 249 } 250 251 /*! 252 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 253 the calls within a db transaction; thus within db->begin and db->commit. 254 */ 255 function runWorkflow( &$workflowProcess ) 256 { 257 $workflow = eZWorkflow::fetch( $workflowProcess->attribute( "workflow_id" ) ); 258 $workflowEvent = null; 259 260 $workflowStatus = $workflowProcess->run( $workflow, $workflowEvent, $eventLog ); 261 262 $db =& eZDB::instance(); 263 $db->begin(); 264 $workflowProcess->store(); 265 266 switch ( $workflowStatus ) 267 { 268 case EZ_WORKFLOW_STATUS_FAILED: 269 case EZ_WORKFLOW_STATUS_CANCELLED: 270 case EZ_WORKFLOW_STATUS_NONE: 271 case EZ_WORKFLOW_STATUS_BUSY: 272 { 273 $workflowProcess->remove(); 274 $db->commit(); 275 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED, 276 'Result' => null ); 277 } break; 278 case EZ_WORKFLOW_STATUS_FETCH_TEMPLATE: 279 { 280 include_once ( 'kernel/common/template.php' ); 281 $tpl =& templateInit(); 282 $result = array(); 283 foreach ( array_keys( $workflowProcess->Template['templateVars'] ) as $key ) 284 { 285 $value =& $workflowProcess->Template['templateVars'][$key]; 286 $tpl->setVariable( $key, $value ); 287 } 288 $result['content'] =& $tpl->fetch( $workflowProcess->Template['templateName'] ); 289 if ( isset( $workflowProcess->Template['path'] ) ) 290 $result['path'] = $workflowProcess->Template['path']; 291 292 $db->commit(); 293 return array( 'Status' => EZ_TRIGGER_FETCH_TEMPLATE, 294 'WorkflowProcess' => &$workflowProcess, 295 'Result' => $result ); 296 } break; 297 case EZ_WORKFLOW_STATUS_REDIRECT: 298 { 299 // var_dump( $workflowProcess->RedirectUrl ); 300 $db->commit(); 301 return array( 'Status' => EZ_TRIGGER_REDIRECT, 302 'WorkflowProcess' => &$workflowProcess, 303 'Result' => $workflowProcess->RedirectUrl ); 304 305 } break; 306 case EZ_WORKFLOW_STATUS_DEFERRED_TO_CRON: 307 { 308 309 $db->commit(); 310 return array( 'Status' => EZ_TRIGGER_STATUS_CRON_JOB, 311 'WorkflowProcess' => &$workflowProcess, 312 '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/>', 313 'path' => array( array( 'text' => 'Operation halt', 314 'url' => false ) ) ) ); 315 /* 316 return array( 'Status' => EZ_TRIGGER_STATUS_CRON_JOB, 317 'Result' => $workflowProcess->attribute( 'id') ); 318 */ 319 } break; 320 case EZ_WORKFLOW_STATUS_RESET: 321 { 322 $db->commit(); 323 return array( 'Status' => EZ_TRIGGER_WORKFLOW_RESET, 324 'WorkflowProcess' => &$workflowProcess, 325 'Result' => array( 'content' => 'Workflow was reset', 326 'path' => array( array( 'text' => 'Operation halt', 327 'url' => false ) ) ) ); 328 } break; 329 case EZ_WORKFLOW_STATUS_DONE: 330 { 331 $workflowProcess->remove(); 332 $db->commit(); 333 return array( 'Status' => EZ_TRIGGER_WORKFLOW_DONE, 334 'Result' => null ); 335 } 336 } 337 338 $db->commit(); 339 return array( 'Status' => EZ_TRIGGER_WORKFLOW_CANCELED, 340 'Result' => null ); 341 342 343 344 } 345 346 /*! 347 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 348 the calls within a db transaction; thus within db->begin and db->commit. 349 */ 350 function createNew( $moduleName, $functionName, $connectType, $workflowID, $name = false ) 351 { 352 if ( !$name ) 353 { 354 if ( $connectType == 'b' ) 355 { 356 $name = 'pre_'; 357 } 358 else if ( $connectType == 'a' ) 359 { 360 $name = 'post_'; 361 } 362 $name .= $functionName; 363 } 364 $trigger = new eZTrigger( array( 'module_name' => $moduleName, 365 'function_name' => $functionName, 366 'connect_type' => $connectType, 367 'workflow_id' => $workflowID, 368 'name' => $name ) ); 369 $trigger->store(); 370 return $trigger; 371 } 372 373 /*! 374 Removes triggers which uses the given workflowID. 375 \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 376 the calls within a db transaction; thus within db->begin and db->commit. 377 */ 378 function removeTriggerForWorkflow( $workFlowID ) 379 { 380 $db =& eZDB::instance(); 381 $workFlowID = (int)$workFlowID; 382 $db->query( "DELETE FROM eztrigger WHERE workflow_id=$workFlowID" ); 383 } 384 } 385 386 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |