Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztrigger.php
Go to the documentation of this file.
1 <?php
16 class eZTrigger extends eZPersistentObject
17 {
18 const STATUS_CRON_JOB = 0;
19 const WORKFLOW_DONE = 1;
20 const WORKFLOW_CANCELLED = 2;
21 const NO_CONNECTED_WORKFLOWS = 3;
22 const FETCH_TEMPLATE = 4;
23 const REDIRECT = 5;
24 const WORKFLOW_RESET = 6;
25 const FETCH_TEMPLATE_REPEAT = 7;
26
27 static function definition()
28 {
29 return array( "fields" => array( 'id' => array( 'name' => 'ID',
30 'datatype' => 'integer',
31 'default' => 0,
32 'required' => true ),
33 'module_name' => array( 'name' => 'ModuleName',
34 'datatype' => 'string',
35 'default' => '',
36 'required' => true ),
37 'function_name' => array( 'name' => 'FunctionName',
38 'datatype' => 'string',
39 'default' => '',
40 'required' => true ),
41 'connect_type' => array( 'name' => 'ConnectType',
42 'datatype' => 'string',
43 'default' => '',
44 'required' => true ),
45 'workflow_id' => array( 'name' => 'WorkflowID',
46 'datatype' => 'integer',
47 'default' => 0,
48 'required' => true,
49 'foreign_class' => 'eZWorkflow',
50 'foreign_attribute' => 'id',
51 'multiplicity' => '1..*' ),
52 'name' => array( 'name' => 'Name',
53 'datatype' => 'string',
54 'default' => '',
55 'required' => true ) ),
56 "class_name" => "eZTrigger",
57 "keys" => array( 'id' ),
58 'function_attributes' => array( 'allowed_workflows' => 'fetchAllowedWorkflows' ),
59 "increment_key" => "id",
60 "name" => "eztrigger" );
61 }
62
68 function fetchAllowedWorkflows()
69 {
70 $connectionType = '*';
71 if ( $this->attribute( 'connect_type') == 'b' )
72 {
73 $connectionType = 'before';
74 }
75 else if ( $this->attribute( 'connect_type') == 'a' )
76 {
77 $connectionType = 'after';
78 }
79
80 return eZWorkflow::fetchLimited( $this->attribute( 'module_name' ),
81 $this->attribute( 'function_name' ),
82 $connectionType );
83 }
84
85 static function fetch( $triggerID )
86 {
87 return eZPersistentObject::fetchObject( eZTrigger::definition(),
88 null,
89 array( 'id' => $triggerID ),
90 true);
91 }
92
93 static function fetchList( $parameters = array(), $asObject = true )
94 {
95 $filterArray = array();
96 if ( array_key_exists('module', $parameters ) && $parameters[ 'module' ] != '*' )
97 {
98 $filterArray['module_name'] = $parameters['module'];
99 }
100 if ( array_key_exists('function', $parameters ) && $parameters[ 'function' ] != '*' )
101 {
102 $filterArray['function_name'] = $parameters['function'];
103 }
104 if ( array_key_exists('connectType', $parameters ) && $parameters[ 'connectType' ] != '*' )
105 {
106 $filterArray['connect_type'] = $parameters['connectType'];
107 }
108 if ( array_key_exists('name', $parameters ) && $parameters[ 'name' ] != '' )
109 {
110 $filterArray['name'] = $parameters['name'];
111 }
112 return eZPersistentObject::fetchObjectList( eZTrigger::definition(),
113 null,
114 $filterArray, array( 'module_name' => 'asc' ,
115 'function_name' => 'asc',
116 'connect_type' => 'asc' ),
117 null,
118 $asObject );
119 }
120
125 static function runTrigger( $name, $moduleName, $function, $parameters, $keys = null )
126 {
127 $trigger = eZPersistentObject::fetchObject( eZTrigger::definition(),
128 null,
129 array( 'name' => $name,
130 'module_name' => $moduleName,
131 'function_name' => $function ),
132 true );
133 if ( $trigger !== NULL )
134 {
135 $workflowID = $trigger->attribute( 'workflow_id' );
136 $workflow = eZWorkflow::fetch( $workflowID );
137 if ( $keys != null )
138 {
139 $keys[] = 'workflow_id';
140 }
141
142 $parameters['workflow_id'] = $workflowID;
143 $parameters['trigger_name'] = $name;
144 $parameters['module_name'] = $moduleName;
145 $parameters['module_function'] = $function;
146 // It is very important that the user_id is set correctly.
147 // If it was not supplied by the calling code we will use
148 // the currently logged in user.
149 if ( !isset( $parameters['user_id'] ) or
150 $parameters['user_id'] == 0 )
151 {
152 $user = eZUser::currentUser();
153 $parameters['user_id'] = $user->attribute( 'contentobject_id' );
154 }
155 $processKey = eZWorkflowProcess::createKey( $parameters, $keys );
156
157 // $searchKey = eZWorkflowProcess::createKey( $keyArray );
158
159 $workflowProcessList = eZWorkflowProcess::fetchListByKey( $processKey );
160
161 if ( count( $workflowProcessList ) > 0 )
162 {
163 $existingWorkflowProcess = $workflowProcessList[0];
164 $existingWorkflowStatus = $existingWorkflowProcess->attribute( 'status' );
165
166
167 switch( $existingWorkflowStatus )
168 {
169 case eZWorkflow::STATUS_FAILED:
170 case eZWorkflow::STATUS_CANCELLED:
171 case eZWorkflow::STATUS_NONE:
172 case eZWorkflow::STATUS_BUSY:
173 {
174 $existingWorkflowProcess->removeThis();
175 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED,
176 'Result' => null );
177 } break;
178 case eZWorkflow::STATUS_FETCH_TEMPLATE:
179 case eZWorkflow::STATUS_FETCH_TEMPLATE_REPEAT:
180 case eZWorkflow::STATUS_REDIRECT:
181 case eZWorkflow::STATUS_RESET:
182 {
183 return eZTrigger::runWorkflow( $existingWorkflowProcess );
184 // return eZTrigger::FETCH_TEMPLATE;
185 } break;
186 case eZWorkflow::STATUS_DEFERRED_TO_CRON:
187 {
188 return eZTrigger::runWorkflow( $existingWorkflowProcess );
189 /* return array( 'Status' => eZTrigger::STATUS_CRON_JOB,
190
191 '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/>',
192 'path' => array( array( 'text' => 'Operation halt',
193 'url' => false ) ) ) );
194 */ } break;
195 case eZWorkflow::STATUS_DONE:
196 {
197 $existingWorkflowProcess->removeThis();
198 return array( 'Status' => eZTrigger::WORKFLOW_DONE,
199 'Result' => null );
200 }
201 }
202 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED,
203 'Result' => null );
204 }else
205 {
206 // print( "\n starting new workflow process \n");
207 // var_dump( $keyArray );
208 // print( " $workflowID, $userID, $objectID, $version, $nodeID, \n ");
209 }
210 $workflowProcess = eZWorkflowProcess::create( $processKey, $parameters );
211
212 $workflowProcess->store();
213
214 return eZTrigger::runWorkflow( $workflowProcess );
215
216 }
217 else
218 {
219 return array( 'Status' => eZTrigger::NO_CONNECTED_WORKFLOWS,
220 'Result' => null );
221 }
222 }
223
228 static function runWorkflow( $workflowProcess )
229 {
230 $workflow = eZWorkflow::fetch( $workflowProcess->attribute( "workflow_id" ) );
231 $workflowEvent = null;
232
233 $workflowStatus = $workflowProcess->run( $workflow, $workflowEvent, $eventLog );
234
235 $db = eZDB::instance();
236 $db->begin();
237 $workflowProcess->store();
238
239 switch ( $workflowStatus )
240 {
241 case eZWorkflow::STATUS_FAILED:
242 case eZWorkflow::STATUS_CANCELLED:
243 case eZWorkflow::STATUS_NONE:
244 case eZWorkflow::STATUS_BUSY:
245 {
246 $workflowProcess->removeThis();
247 $db->commit();
248 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED,
249 'Result' => null );
250 } break;
251 case eZWorkflow::STATUS_FETCH_TEMPLATE:
252 case eZWorkflow::STATUS_FETCH_TEMPLATE_REPEAT:
253 {
254 $tpl = eZTemplate::factory();
255 $result = array();
256 foreach ( array_keys( $workflowProcess->Template['templateVars'] ) as $key )
257 {
258 $value = $workflowProcess->Template['templateVars'][$key];
259 $tpl->setVariable( $key, $value );
260 }
261 $result['content'] = $tpl->fetch( $workflowProcess->Template['templateName'] );
262 if ( isset( $workflowProcess->Template['path'] ) )
263 $result['path'] = $workflowProcess->Template['path'];
264
265 $db->commit();
266 if ( $workflowStatus == eZWorkflow::STATUS_FETCH_TEMPLATE )
267 {
268 $triggerStatus = eZTrigger::FETCH_TEMPLATE;
269 }
270 elseif ( $workflowStatus == eZWorkflow::STATUS_FETCH_TEMPLATE_REPEAT )
271 {
272 $triggerStatus = eZTrigger::FETCH_TEMPLATE_REPEAT;
273 }
274 return array( 'Status' => $triggerStatus,
275 'WorkflowProcess' => $workflowProcess,
276 'Result' => $result );
277 } break;
278 case eZWorkflow::STATUS_REDIRECT:
279 {
280 // var_dump( $workflowProcess->RedirectUrl );
281 $db->commit();
282 return array( 'Status' => eZTrigger::REDIRECT,
283 'WorkflowProcess' => $workflowProcess,
284 'Result' => $workflowProcess->RedirectUrl );
285
286 } break;
287 case eZWorkflow::STATUS_DEFERRED_TO_CRON:
288 {
289
290 $db->commit();
291 return array( 'Status' => eZTrigger::STATUS_CRON_JOB,
292 'WorkflowProcess' => $workflowProcess,
293 '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/>',
294 'path' => array( array( 'text' => 'Operation halt',
295 'url' => false ) ) ) );
296 /*
297 return array( 'Status' => eZTrigger::STATUS_CRON_JOB,
298 'Result' => $workflowProcess->attribute( 'id') );
299 */
300 } break;
301 case eZWorkflow::STATUS_RESET:
302 {
303 $db->commit();
304 return array( 'Status' => eZTrigger::WORKFLOW_RESET,
305 'WorkflowProcess' => $workflowProcess,
306 'Result' => array( 'content' => 'Workflow was reset',
307 'path' => array( array( 'text' => 'Operation halt',
308 'url' => false ) ) ) );
309 } break;
310 case eZWorkflow::STATUS_DONE:
311 {
312 $workflowProcess->removeThis();
313 $db->commit();
314 return array( 'Status' => eZTrigger::WORKFLOW_DONE,
315 'Result' => null );
316 }
317 }
318
319 $db->commit();
320 return array( 'Status' => eZTrigger::WORKFLOW_CANCELLED,
321 'Result' => null );
322
323
324
325 }
326
331 static function createNew( $moduleName, $functionName, $connectType, $workflowID, $name = false )
332 {
333 if ( !$name )
334 {
335 if ( $connectType == 'b' )
336 {
337 $name = 'pre_';
338 }
339 else if ( $connectType == 'a' )
340 {
341 $name = 'post_';
342 }
343 $name .= $functionName;
344 }
345 $trigger = new eZTrigger( array( 'module_name' => $moduleName,
346 'function_name' => $functionName,
347 'connect_type' => $connectType,
348 'workflow_id' => $workflowID,
349 'name' => $name ) );
350 $trigger->store();
351 return $trigger;
352 }
353
359 static function removeTriggerForWorkflow( $workFlowID )
360 {
361 $db = eZDB::instance();
362 $workFlowID = (int)$workFlowID;
363 $db->query( "DELETE FROM eztrigger WHERE workflow_id=$workFlowID" );
364 }
365 }
366
367 ?>
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZPersistentObject
Definition: ezpersistentobject.php:33
eZPersistentObject\fetchObject
static fetchObject( $def, $field_filters, $conds, $asObject=true, $grouping=null, $custom_fields=null)
Definition: ezpersistentobject.php:213
eZPersistentObject\count
static count( $def, $conds=null, $field=null)
Definition: ezpersistentobject.php:187
eZPersistentObject\attribute
attribute( $attr, $noFunction=false)
Definition: ezpersistentobject.php:1329
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:806
eZTemplate\factory
static factory()
Definition: eztemplate.php:2380
eZTrigger
The class eZTrigger does.
Definition: eztrigger.php:17
eZTrigger\WORKFLOW_RESET
const WORKFLOW_RESET
Definition: eztrigger.php:24
eZTrigger\WORKFLOW_DONE
const WORKFLOW_DONE
Definition: eztrigger.php:19
eZTrigger\NO_CONNECTED_WORKFLOWS
const NO_CONNECTED_WORKFLOWS
Definition: eztrigger.php:21
eZTrigger\FETCH_TEMPLATE
const FETCH_TEMPLATE
Definition: eztrigger.php:22
eZTrigger\runWorkflow
static runWorkflow( $workflowProcess)
Definition: eztrigger.php:228
eZTrigger\definition
static definition()
Definition: eztrigger.php:27
eZTrigger\STATUS_CRON_JOB
const STATUS_CRON_JOB
Definition: eztrigger.php:18
eZTrigger\fetchList
static fetchList( $parameters=array(), $asObject=true)
Definition: eztrigger.php:93
eZTrigger\createNew
static createNew( $moduleName, $functionName, $connectType, $workflowID, $name=false)
Definition: eztrigger.php:331
eZTrigger\REDIRECT
const REDIRECT
Definition: eztrigger.php:23
eZTrigger\FETCH_TEMPLATE_REPEAT
const FETCH_TEMPLATE_REPEAT
Definition: eztrigger.php:25
eZTrigger\fetchAllowedWorkflows
fetchAllowedWorkflows()
Definition: eztrigger.php:68
eZTrigger\fetch
static fetch( $triggerID)
Definition: eztrigger.php:85
eZTrigger\WORKFLOW_CANCELLED
const WORKFLOW_CANCELLED
Definition: eztrigger.php:20
eZTrigger\runTrigger
static runTrigger( $name, $moduleName, $function, $parameters, $keys=null)
Definition: eztrigger.php:125
eZTrigger\removeTriggerForWorkflow
static removeTriggerForWorkflow( $workFlowID)
Definition: eztrigger.php:359
eZUser\currentUser
static currentUser()
Definition: ezuser.php:1716
eZWorkflowProcess\create
static create( $processKey, $parameters)
Definition: ezworkflowprocess.php:134
eZWorkflowProcess\createKey
static createKey( $parameters, $keys=null)
Definition: ezworkflowprocess.php:454
eZWorkflowProcess\fetchListByKey
static fetchListByKey( $searchKey, $asObject=true)
Definition: ezworkflowprocess.php:485
eZWorkflow\STATUS_CANCELLED
const STATUS_CANCELLED
Definition: ezworkflow.php:24
eZWorkflow\fetchLimited
static fetchLimited( $moduleName, $functionName, $connectType)
Definition: ezworkflow.php:321
eZWorkflow\STATUS_BUSY
const STATUS_BUSY
Definition: ezworkflow.php:20
eZWorkflow\fetch
static fetch( $id, $asObject=true, $version=0)
Definition: ezworkflow.php:302
eZWorkflow\STATUS_DEFERRED_TO_CRON
const STATUS_DEFERRED_TO_CRON
Definition: ezworkflow.php:23
eZWorkflow\STATUS_FAILED
const STATUS_FAILED
Definition: ezworkflow.php:22
eZWorkflow\STATUS_DONE
const STATUS_DONE
Definition: ezworkflow.php:21
eZWorkflow\STATUS_FETCH_TEMPLATE
const STATUS_FETCH_TEMPLATE
Definition: ezworkflow.php:25
eZWorkflow\STATUS_FETCH_TEMPLATE_REPEAT
const STATUS_FETCH_TEMPLATE_REPEAT
Definition: ezworkflow.php:29
eZWorkflow\STATUS_RESET
const STATUS_RESET
Definition: ezworkflow.php:27
eZWorkflow\STATUS_REDIRECT
const STATUS_REDIRECT
Definition: ezworkflow.php:26
eZWorkflow\STATUS_NONE
const STATUS_NONE
Definition: ezworkflow.php:19
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.