1Workflow::Condition(3)User Contributed Perl DocumentationWorkflow::Condition(3)
2
3
4

NAME

6       Workflow::Condition - Evaluate a condition depending on the workflow
7       state and environment
8

SYNOPSIS

10        # First declare the condition in a 'workflow_condition.xml'...
11
12        <conditions>
13          <condition
14             name="IsAdminUser"
15             class="MyApp::Condition::IsAdminUser">
16                <param name="admin_group_id" value="5" />
17                <param name="admin_group_id" value="6" />
18          </condition>
19        ...
20
21        # Reference the condition in an action of the state/workflow definition...
22        <workflow>
23          <state>
24            ...
25            <action name="SomeAdminAction">
26              ...
27              <condition name="IsAdminUser" />
28            </action>
29            <action name="AnotherAdminAction">
30             ...
31             <condition name="IsAdminUser" />
32            </action>
33            <action name="AUserAction">
34             ...
35             <condition name="!IsAdminUser" />
36            </action>
37          </state>
38          ...
39        </workflow>
40
41        # Then implement the condition
42
43        package MyApp::Condition::IsAdminUser;
44
45        use strict;
46        use base qw( Workflow::Condition );
47        use Workflow::Exception qw( condition_error configuration_error );
48
49        __PACKAGE__->mk_accessors( 'admin_group_id' );
50
51        sub _init {
52            my ( $self, $params ) = @_;
53            unless ( $params->{admin_group_id} ) {
54                configuration_error
55                    "You must define one or more values for 'admin_group_id' in ",
56                    "declaration of condition ", $self->name;
57            }
58            my @admin_ids = $self->_normalize_array( $params->{admin_group_id} );
59            $self->admin_group_id( { map { $_ => 1 } @admin_ids } );
60        }
61
62        sub evaluate {
63            my ( $self, $wf ) = @_;
64            my $admin_ids = $self->admin_group_id;
65            my $current_user = $wf->context->param( 'current_user' );
66            unless ( $current_user ) {
67                condition_error "No user defined, cannot check groups";
68            }
69            foreach my $group ( @{ $current_user->get_groups } ) {
70                return if ( $admin_ids->{ $group->id } );
71            }
72            condition_error "Not member of any Admin groups";
73        }
74

DESCRIPTION

76       Conditions are used by the workflow to see whether actions are avail‐
77       able in a particular context. So if user A asks the workflow for the
78       available actions she might get a different answer than user B since
79       they determine separate contexts.
80
81       NOTE: The condition is enforced by Workflow::State. This means that the
82       condition name must be visible inside of the state definition. If you
83       specify the reference to the condition only inside of the full action
84       specification in a seperate file then nothing will happen. The refer‐
85       ence to the condition must be defined inside of the state/workflow
86       specification.
87

SUBCLASSING

89       Strategy
90
91       The idea behind conditions is that they can be stateless. So when the
92       Workflow::Factory object reads in the condition configuration it cre‐
93       ates the condition objects and initializes them with whatever informa‐
94       tion is passed in.
95
96       Then when the condition is evaluated we just call "evaluate()" on the
97       condition. Hopefully the operation can be done very quickly since the
98       condition may be called many, many times during a workflow lifecycle --
99       they are typically used to show users what options they have given the
100       current state of the workflow for things like menu options. So keep it
101       short!
102
103       Methods
104
105       To create your own condition you should implement the following:
106
107       _init( \%params )
108
109       This is optional, but called when the condition is first initialized.
110       It may contain information you will want to initialize your condition
111       with in "\%params", which are all the declared parameters in the condi‐
112       tion declartion except for 'class' and 'name'.
113
114       You may also do any initialization here -- you can fetch data from the
115       database and store it in the class or object, whatever you need.
116
117       If you do not have sufficient information in "\%params" you should
118       throw an exception (preferably 'configuration_error' imported from
119       Workflow::Exception).
120
121       evaluate( $workflow )
122
123       Determine whether your condition fails by throwing an exception. You
124       can get the application context information necessary to process your
125       condition from the $workflow object.
126
127       #=head3 init
128
129       Caching and inverting the result
130
131       If in one state, you ask for the same condition again, Workflow uses
132       the cached result, so that within one list of available actions, you
133       will get a consistent view. Note that if we would not use caching, this
134       might not necessary be the case, as something external might change
135       between the two evaluate() calls.
136
137       Caching is also used with an inverted condition, which you can specify
138       in the definition using "<condition name="!some_condition"">.  This
139       condition returns exactly the opposite of the original one, i.e.  if
140       the original condition fails, this one does not and the other way
141       round. As caching is used, you can model "yes/no" decisions using this
142       feature - if you have both "<condition name="some_condition""> and
143       "<condition name="!some_condition""> in your workflow state definition,
144       exactly one of them will succeed and one will fail - which is particu‐
145       larly useful if you use "autorun" a lot.
146
148       Copyright (c) 2003-2004 Chris Winters. All rights reserved.
149
150       This library is free software; you can redistribute it and/or modify it
151       under the same terms as Perl itself.
152

AUTHORS

154       Chris Winters <chris@cwinters.com>
155
156
157
158perl v5.8.8                       2007-04-25            Workflow::Condition(3)
Impressum