Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
workflow_stream_edit_fns.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
19 
34 {
35 
36 
41  function __construct()
42  {
43  parent::__construct();
44 
45  }//end constructor
46 
47 
58  public function paintNewStep(Workflow_Stream $asset, Backend_Outputter $o, $prefix)
59  {
60  // need to have write access to make any changes
61  if (!$asset->writeAccess('links')) return FALSE;
62 
63  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
64  echo translate('create_new_step_called').' ';
65  text_box($prefix.'_new_step', '', 20);
66  return TRUE;
67 
68  }//end paintNewStep()
69 
70 
81  public function processNewStep(Workflow_Stream $asset, Backend_Outputter $o, $prefix)
82  {
83  // need to have write access to make any changes
84  if (!$asset->writeAccess('links')) return FALSE;
85 
86  if (isset($_POST[$prefix.'_new_step']) && trim($_POST[$prefix.'_new_step'])) {
87  $new_step_name = trim($_POST[$prefix.'_new_step']);
88  $GLOBALS['SQ_SYSTEM']->am->includeAsset('workflow_step');
89  $new_step = new Workflow_Step();
90  $schema_link = Array('asset' => &$asset, 'link_type' => SQ_LINK_TYPE_2, 'sort_order' => -1, 'is_dependant' => 1);
91  $new_step->setAttrValue('name', $new_step_name);
92  if (!$new_step->create($schema_link)) return FALSE;
93  }
94 
95  return TRUE;
96 
97  }//end processNewStep()
98 
99 
111  public function paintWorkflowSummary(Workflow_Stream $asset, Backend_Outputter $o, $prefix)
112  {
113  $o->addCssInclude(sq_web_path('data').'/asset_types/'.$asset->type().'/css/workflow_stream_edit_fns.css');
114  $o->addJsInclude(sq_web_path('data').'/asset_types/'.$asset->type().'/js/workflow_stream_edit_fns.js');
115  $am = $GLOBALS['SQ_SYSTEM']->am;
116 
117  $write_access = $asset->writeAccess('links');
118  if ($write_access) {
119  $o->addOnLoad('MatrixWorkflowSchema.onLoad();');
120  }
121 
122  // show the workflow summary, and allow us to reorder it
123  $steps_to_process = Array(
124  Array(
125  'assetid' => $asset->id,
126  'depth' => -1,
127  )
128  );
129 
130  $step_order = Array();
131 
132  while (count($steps_to_process) > 0) {
133  $current_step = array_shift($steps_to_process);
134  if ($current_step['depth'] >= 0) {
135  $step_order[] = $current_step;
136  }
137  $current_links = array_reverse($am->getLinks($current_step['assetid'], SQ_LINK_TYPE_2, 'workflow_step', TRUE, 'major', NULL, TRUE, FALSE));
138  foreach ($current_links as $current_link) {
139  $new_step = Array(
140  'assetid' => $current_link['minorid'],
141  'depth' => $current_step['depth'] + 1,
142  );
143  array_unshift($steps_to_process, $new_step);
144  }
145  }
146 
147  if (count($step_order) > 0) {
148  $step_nums = Array(0);
149  $current_depth = 0;
150  foreach ($step_order as $step) {
151  if ($step['depth'] > $current_depth) {
152  // This step is deeper, ie. starting a new escalation step
153  array_push($step_nums, 0);
154  echo '<div class="sq-workflow-step-escalation-indent">';
155  $current_depth = $step['depth'];
156  }
157 
158  while ($step['depth'] < $current_depth) {
159  // exiting an escalation step... but we need to be able to exit more than one at once
160  array_pop($step_nums);
161  $current_depth--;
162  echo '<div class="sq-workflow-step-divider"></div>';
163  echo '</div>';
164  }
165 
166  echo '<div class="sq-workflow-step-divider"></div>';
167 
168  // Okay, now at current depth, so increment step number;
169  $current_step_num = array_pop($step_nums);
170  $current_step_num++;
171  array_push($step_nums, $current_step_num);
172 
173  $step_assetid = $step['assetid'];
174  $step_asset =& $am->getAsset($step_assetid);
175 
176  echo '<div id="'.$prefix.'workflow-step-'.$step_assetid.'" class="sq-workflow-step">';
177  if ($write_access) {
178  hidden_field($prefix.'_step_order[]', $step_assetid);
179  }
180 
181  echo '<div class="sq-workflow-step-drag">';
182  if ($write_access) {
183  sq_print_icon(sq_web_path('data').'/asset_types/'.$asset->type().'/files/wf_drag_handle.png', 16, 16, 'Re-Order', 'Re-Order Step', 'align="absmiddle" class="sq-workflow-step-drag-handle"');
184  }
185  echo 'Step '.implode('.', $step_nums).'</div>';
186  echo '<div class="sq-workflow-step-tag-line">'.$step_asset->name.'</div>';
187  echo '<div class="sq-workflow-step-condition"><strong>Conditions:</strong> ';
188  $conditions = $am->getLinks($step_assetid, SQ_LINK_TYPE_2, 'workflow_step_condition', TRUE, 'major', NULL, TRUE);
189  if (count($conditions) === 1) {
190  $condition = reset($conditions);
191  $userids = $am->getAttributeValuesByName('userid', 'workflow_step_condition', Array($condition['minorid']));
192  $userid = $userids[$condition['minorid']];
193  echo get_asset_tag_line($userid);
194  } else if (count($conditions) === 0) {
195  echo '<span class="sq-backend-warning">None selected</span>';
196  } else {
197  if (($step_asset->attr('logic') === 'all') || ($step_asset->attr('logic_num') == count($conditions))) {
198  echo 'All '.count($conditions);
199  } else {
200  echo 'At least '.$step_asset->attr('logic_num').' of '.count($conditions);
201  }
202  }
203  echo '</div>';
204 
205  echo '<div class="sq-workflow-step-expiry"><strong>Expiry:</strong> ';
206  $expiry = (int)$step_asset->attr('expiry_time');
207  if ($expiry === 0) {
208  echo 'None set';
209  } else {
210  if ($step_asset->attr('escalate')) {
211  echo '<span class="sq-backend-warning">Escalates</span>';
212  } else {
213  echo 'Reminder';
214  }
215  echo ' after ';
216 
217  $hours = ($expiry / 3600);
218 
219  echo easy_time_total($expiry);
220  }
221 
222  echo '</div>';
223 
224  if ($step['depth'] === 0) {
225  $step_url = '#step-'.$step_assetid;
226  } else {
227  $step_url = $step_asset->getBackendHref('details');
228  }
229  echo '<div class="sq-workflow-step-edit-link">[ <a href="'.$step_url.'">Edit this step</a> ]</div>';
230  echo '<div style="clear: both"></div>';
231  echo '</div>';
232  }
233 
234  // Do we have any overhanging escalation steps for the last main step?
235  while ($current_depth > 0) {
236  echo '<div class="sq-workflow-step-divider"></div>';
237  echo '</div>';
238  $current_depth--;
239  }
240 
241  // Add a drop point after the last main step
242  echo '<div class="sq-workflow-step-divider"></div>';
243  }
244 
245 
246  return $write_access;
247 
248  }//end paintWorkflowSummary()
249 
250 
262  public function processWorkflowSummary(Workflow_Stream $asset, Backend_Outputter $o, $prefix)
263  {
264  if (!$asset->writeAccess('links')) return FALSE;
265 
266  $new_step_order = array_get_index($_POST, $prefix.'_step_order', Array());
267  $am = $GLOBALS['SQ_SYSTEM']->am;
268 
269  $links_to_build = Array($asset->id);
270  $link_assetids = Array();
271 
272  while (count($links_to_build) > 0) {
273  $assetid = array_shift($links_to_build);
274  $current_links = $am->getLinks($assetid, SQ_LINK_TYPE_2, 'workflow_step', TRUE, 'major', NULL, TRUE, FALSE);
275 
276  // If we have at least 2 links, we can at least move it
277  // Even if only 1 though, we need to see if it has multiple
278  // escalation steps
279  foreach ($current_links as $link) {
280  $links_to_build[] = $link['minorid'];
281  if (count($current_links) >= 2) {
282  $new_index = array_search($link['minorid'], $new_step_order);
283  $link_assetids[$assetid][$new_index] = $link['minorid'];
284  }
285  }
286  }
287 
288  foreach (array_keys($link_assetids) as $link_key) {
289  // Sort keys, then use PHP's rule about numeric indices to get the
290  // proper sort order for each parent
291  ksort($link_assetids[$link_key]);
292  $link_assetids[$link_key] = array_values($link_assetids[$link_key]);
293  foreach ($link_assetids[$link_key] as $sort_order => $minor_assetid) {
294  $link = $am->getLinkByAsset($link_key, $minor_assetid, SQ_LINK_TYPE_2);
295  if ($sort_order != $link['sort_order']) {
296  // If the sort order is different, then update it
297  $am->moveLinkPos($link['linkid'], $sort_order);
298  }
299  }
300  }
301 
302  return TRUE;
303 
304  }//end processWorkflowSummary()
305 
306 
317  public function paintSteps(Workflow_Stream $asset, Backend_Outputter $o, $prefix)
318  {
319  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
320  $write_access = $asset->writeAccess('links');
321  $ok = $write_access;
322 
323  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'workflow_step', TRUE, 'major', NULL, TRUE, FALSE);
324  $i = 1;
325  foreach ($current_links as $link_data) {
326  $step = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_data['minorid']);
327  if (is_null($step)) continue;
328 
329  $o->openSection('Step '.$i.' - '.$step->name);
330  if ($write_access) {
331  $o->openField('Delete ?');
332  check_box($prefix.'_delete_steps['.$link_data['linkid'].']');
333  label(translate('delete_step_and_conditions_question'), $prefix.'_delete_steps['.$link_data['linkid'].']');
334  $o->closeField();
335  }
336  $edit_fns = $step->getEditFns();
337  if (!$edit_fns->paintStep($step, $o)) $ok = FALSE;
338  $o->closeSection();
339 
340  $i++;
341  }
342 
343  return $ok;
344 
345  }//end paintSteps()
346 
347 
358  public function processSteps(Workflow_Stream $asset, Backend_Outputter $o, $prefix)
359  {
360  // need to have write access to make any changes
361  if (!$asset->writeAccess('links')) return FALSE;
362  $updated = FALSE;
363 
364  // process current steps
365  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'workflow_step', TRUE, 'major', NULL, TRUE, FALSE);
366  foreach ($current_links as $link_data) {
367  $step = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_data['minorid']);
368  if (is_null($step)) continue;
369  $edit_fns = $step->getEditFns();
370  if ($edit_fns->processStep($step, $o)) $updated = TRUE;
371  }
372 
373  // delete any steps that need deleting
374  if (isset($_POST[$prefix.'_delete_steps'])) {
375  $delete_steps = $_POST[$prefix.'_delete_steps'];
376  foreach ($delete_steps as $linkid => $on) {
377  if ($asset->deleteLink($linkid)) $updated = TRUE;
378  }
379  }
380 
381  return $updated;
382 
383  }//end processSteps()
384 
385 
397  public function generateWorkflowArray(Workflow_Stream $asset)
398  {
399  $workflow_array['steps'] = Array();
400  $workflow_array['current_step'] = Array('1');
401  $workflow_array['complete'] = FALSE;
402  $workflow_array['schema_name'] = $asset->name;
403  $workflow_array['started'] = time();
404  $workflow_array['started_by'] = $GLOBALS['SQ_SYSTEM']->currentUserId();
405 
406  $i = 1;
407  $current_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($asset->id, SQ_LINK_TYPE_2, 'workflow_step', TRUE, 'major', NULL, TRUE, FALSE);
408  foreach ($current_links as $link_data) {
409  $step = $GLOBALS['SQ_SYSTEM']->am->getAsset($link_data['minorid']);
410  if (is_null($step)) continue;
411  $edit_fns = $step->getEditFns();
412  $workflow_array['steps'][$i] = Array();
413  if (!$edit_fns->generateWorkflowArray($step, $workflow_array['steps'][$i])) {
414  return FALSE;
415  }
416  if ($i == 1) {
417  $workflow_array['steps'][$i]['started'] = time();
418  }
419  $i++;
420  }
421  return $workflow_array;
422 
423  }//end generateWorkflowArray()
424 
425 
426 }//end class
427 
428 ?>