1 <?php
2 /**
3 * this class provides helper methods for the simpleWorkflow behavior
4 */
5 class SWHelper
6 {
7 /**
8 * Returns the list of all statuses that can be reached from current status of the model
9 * passed as argument. The returned array is in the form suitable for dropDownList and listBox:
10 * <pre>
11 * array(
12 * 'statusId' => 'status label',
13 * 'status Id2' => 'status label 2',
14 * etc ...
15 * )
16 * </pre>
17 * Use the $options argument to speficy following options :
18 * <ul>
19 * <li><b>prompt</b> : specifies the prompt text shown as the first list option. Its value is empty.
20 * Note, the prompt text will NOT be HTML-encoded</li>
21 * <li><b>includeCurrent</b> : boolean, if TRUE (default) the current model status is included in the list,
22 * otherwise current model status is not inserted in the returned array.</li>
23 * <li><b>exclude</b> : array, list of statuses that should not be inserted in the returned array</li>
24 * </ul>
25 * Note that each status label is html encode by default.
26 * @param CModel $model the data model attaching a simpleWorkflow behavior
27 * @param array $options additional options
28 * @return array the list data that can be used in dropDownList and listBox
29 */
30 public static function nextStatuslistData($model, $options=array())
31 {
32 return SWHelper::_createListData($model,$model->swGetNextStatus(),$options);
33 }
34 /**
35 * Returns the list of all statuses belonging to the workflow the model passed as argument
36 * is in.
37 * see {@link SWHelper::nextStatuslistData} for argument options
38 *
39 * @param CModel the data model attaching a simpleWorkflow behavior
40 * @param array additional options
41 * @return array the list data that can be used in dropDownList and listBox
42 */
43 public static function allStatuslistData($model,$options=array())
44 {
45 return SWHelper::_createListData($model,$model->swGetAllStatus(),$options);
46 }
47 /**
48 * Create an array containing where keys are statusIds in the form workflowId/statusId
49 * and the value is the status label.
50 * Note that by default this method never inserts the status of the model passed as argument.
51 * see {@link SWHelper::nextStatuslistData} for argument options
52 *
53 * @param CModel the data model attaching a simpleWorkflow behavior
54 * @param array $statusList array of string where each value is the statusId
55 * @param array $options the list data that can be used in dropDownList and listBox
56 */
57 public static function statusListData($model,$statusList,$options=array())
58 {
59 $nodeList = array();
60 $w = $model->swGetWorkflowSource();
61 foreach($statusList as $key => $statusId){
62 $nodeList[] = $w->getNodeDefinition($statusId);
63 }
64 $options['includeCurrent'] = (isset($options['includeCurrent'])
65 ? $options['includeCurrent']
66 : false
67 );
68 return SWHelper::_createListData($model,$nodeList,$options);
69 }
70 /**
71 * Returns an array where keys are status id and values are status labels.
72 *
73 * @param array $statusList SWNode list
74 * @param array $options (optional)
75 * @throws CException
76 */
77 private static function _createListData($model,$statusList,$options=array())
78 {
79 $result=array();
80 $exclude=null;
81 $includeCurrent = true;
82
83 $currentStatus = ($model->swHasStatus()
84 ? $model->swGetStatus()
85 : null
86 );
87 if($currentStatus != null)
88 $result[$currentStatus->toString()]=$currentStatus->getLabel();
89
90 $encodeLabel = ( isset($options['encode'])
91 ? (bool) $options['encode']
92 : true
93 );
94
95 // process options
96
97 if(count($options)!=0){
98
99 if(isset($options['prompt'])){
100 $result[''] = $options['prompt'];
101 }
102
103 if(isset($options['exclude']))
104 {
105 if(is_string($options['exclude']))
106 $exclude = array_map('trim',explode(",",$options['exclude']));
107 elseif(is_array($options['exclude']))
108 $exclude = $options['exclude'];
109 else
110 throw new CException('incorrect type for option "exclude" : array or string expected');
111
112 foreach ($exclude as $key => $value) {
113 $node = new SWNode($value, $model->swGetWorkflowId());
114 $exclude[$key] = $node->toString();
115 }
116 }
117 if(isset($options['includeCurrent']) )
118 $includeCurrent = (bool) $options['includeCurrent'];
119
120 if($exclude != null && $currentStatus!= null && in_array($currentStatus->toString(), $exclude))
121 $includeCurrent = false;
122 }
123
124 if(count($statusList)!=0){
125 foreach ( $statusList as $nodeObj ) {
126
127 if( $exclude == null ||
128 ( $exclude != null && !in_array($nodeObj->toString(), $exclude )) )
129 {
130 $result[$nodeObj->toString()]= ($encodeLabel
131 ? CHtml::encode($nodeObj->getLabel())
132 : $nodeObj->getLabel()
133 );
134 }
135 }
136 }
137
138 if($includeCurrent == false && $currentStatus !=null){
139 unset($result[$currentStatus->toString()]);
140 }
141 return $result;
142 }
143 }
144 ?>