Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_edit_workflow_schemas.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
28 {
29 
30 
36  function HIPO_Job_Edit_Workflow_Schemas($code_name='')
37  {
38  $this->HIPO_Job($code_name);
39 
40  }//end constructor
41 
42 
52  public static function paintConfig(&$o, $class, $write_access)
53  {
54  // metadata regeneration threshhold HIPO config entry
55  $o->openField(translate('workflow_schemas_threshold'));
56 
57  if ($write_access) {
58  text_box($class.'[SQ_HIPO_WORKFLOW_SCHEMAS_THRESHOLD]', SQ_HIPO_WORKFLOW_SCHEMAS_THRESHOLD, 5);
59  echo ' '.translate('assets');
60  $o->note(translate('hipo_value_divided_by_schema'));
61  } else {
62  echo SQ_HIPO_WORKFLOW_SCHEMAS_THRESHOLD;
63  echo ' '.translate('assets');
64  }
65 
66  $o->closeField();
67 
68  }//end paintConfig()
69 
70 
77  public static function getConfigVars()
78  {
79  return Array(
80  'SQ_HIPO_WORKFLOW_SCHEMAS_THRESHOLD' => Array('editable' => 1, 'default' => 1),
81  );
82 
83  }//end getConfigVars()
84 
85 
97  function getCodeName()
98  {
99  return parent::getCodeName().'-'.md5(serialize($this->_running_vars['schema_changes']));
100 
101  }//end getCodeName()
102 
103 
110  function getHipoName()
111  {
112  return translate('hipo_name_edit_workflow_schemas');
113 
114  }//end getHipoName()
115 
116 
125  {
126  return Array(
127  Array(
128  'name' => translate('hipo_updating_workflow_schemas'),
129  'function_call' => Array(
130  'process_function' => 'processSchemas',
131  ),
132  'running_mode' => 'server',
133  'auto_step' => true,
134  'allow_cancel' => true,
135  'percent_done' => 0,
136  'complete' => false,
137  'message' => '',
138  ),
139  );
140 
141  }//end getInitialStepData()
142 
143 
154  {
155  if (SQ_HIPO_WORKFLOW_SCHEMAS_THRESHOLD == 0) return 0;
156  if (!isset($this->_running_vars['todo_assetids'])) {
157  return 0;
158  }
159  return ($this->_running_vars['total_changes'] / SQ_HIPO_WORKFLOW_SCHEMAS_THRESHOLD) * 100;
160 
161  }//end getThresholdPercentageRequired()
162 
163 
170  function freestyle()
171  {
172  while (!empty($this->_running_vars['todo_assetids'])) {
173  if (!$this->processSchemas($this->_steps[0], get_class($this))) {
174  return FALSE;
175  }
176  }
177  return TRUE;
178 
179  }
180 
181 
188  function prepare()
189  {
190  // to start editing schemas, we need to have an asset id to start editing schemas at
191  if (empty($this->_running_vars['schema_changes'])) {
192  trigger_localised_error('HIPO0010', E_USER_WARNING);
193  return false;
194  }
195 
196  $this->_running_vars['done_changes'] = 0;
197  $this->_running_vars['total_changes'] = 0;
198 
199  if (empty($this->_running_vars['schema_changes'])) {
200  // no schema changes, so do nothing
201  $this->_running_vars['todo_assetids'] = Array();
202  } else {
203  $this->_running_vars['todo_assetids'] = Array();
204  foreach ($this->_running_vars['schema_changes'] as $schema_change) {
205  $assetids = $schema_change['assetids'];
206  foreach ($assetids as $assetid) {
207  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
208 
209  if (is_null($asset)) {
210  trigger_localised_error('HIPO0009', E_USER_WARNING, $assetid);
211  return '';
212  }
213 
214  if ((!array_get_index($schema_change, 'cascades')) || array_get_index($schema_change, 'dependants_only')) {
215  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
216  } else {
217  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid);
218  }
219 
220  // add the parent asset to the list too
221  $child_assets[$assetid] = Array (
222  0 => Array (
223  'type_code' => $asset->type(),
224  ),
225  );
226 
227  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
228  // Now assign schema changes to each asset
229  foreach ($child_assets as $child_assetid => $child_asset) {
230  if (!$wfm->allowsWorkflow($child_assetid)) {
231  continue;
232  }//end if
233  $type_code = $child_asset[0]['type_code'];
234 
235  $this_todo =& $this->_running_vars['todo_assetids'][$child_assetid];
236  $this_todo['type_code'] = $type_code;
237  $this_todo['schema_changes'][] = $schema_change;
238  $this->_running_vars['total_changes']++;
239  }
240 
241  unset($asset);
242  }
243 
244  }
245  }
246 
247  return parent::prepare();
248 
249  }//end prepare()
250 
251 
261  function processSchemas(&$step_data, $prefix)
262  {
263  if (!empty($this->_running_vars['todo_assetids'])) {
264  reset($this->_running_vars['todo_assetids']);
265  $assetid = key($this->_running_vars['todo_assetids']);
266  $asset_type = $this->_running_vars['todo_assetids'][$assetid]['type_code'];
267  $schema_changes = $this->_running_vars['todo_assetids'][$assetid]['schema_changes'];
268  unset($this->_running_vars['todo_assetids'][$assetid]);
269 
270  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
271 
272  if (!is_null($asset)) {
273  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($assetid, 'workflow')) {
274  foreach ($schema_changes as $set_data) {
275  $this->_running_vars['done_changes']++;
276 
277  if ($set_data['schemaid']) {
278  $schema = $GLOBALS['SQ_SYSTEM']->am->getAsset($set_data['schemaid']);
279  if (is_null($schema)) {
280  $schema_name = 'Unknown Schema';
281  } else {
282  $schema_name = $schema->name;
283  }
284  } else {
285  continue;
286  }
287 
288  $wfm = $GLOBALS['SQ_SYSTEM']->getWorkflowManager();
289 
290  if ($set_data['previous_access'] === null) {
291  // to edit this schema, there must not currently be a schema set
292  // at all - either grant OR deny - for this asset
293  $schemas = $wfm->getSchemas($asset->id, $set_data['granted']);
294  if (isset($schemas[$set_data['schemaid']])) {
295  $new_access = ($set_data['granted']) ? translate('apply') : translate('deny');
296  $current_access = ($schemas[$set_data['schemaid']]) ? translate('applied') : translate('denied');
297  $this->_addError(translate('hipo_cannot_modify_schema', $new_access, $schema_name, $asset->name, $current_access), true);
298  continue;
299  }
300  } else {
301  // we need to have a schema set and the access level be the same
302  $schemas = $wfm->getSchemas($asset->id);
303 
304  if (!isset($schemas[$set_data['schemaid']])) {
305  continue;
306  } else if ($schemas[$set_data['schemaid']] != $set_data['previous_access']) {
307  $new_access = ($set_data['previous_access']) ? translate('revoke') : translate('apply');
308  $current_access = ($set_data['previous_access']) ? translate('applied') : translate('denied');
309  $this->_addError(translate('hipo_cannot_modify_schema', $new_access, $schema_name, $asset->name, $current_access), true);
310  continue;
311  }
312  }
313 
314  switch ($set_data['granted']) {
315  case -1 :
316  // delete a schema
317  $step_data['message'] = htmlentities('Deleting "'.$schema->name.'" (#'.$schema->id.') from "'.$asset->name.'" (#'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
318  $wfm->deleteSchema($asset->id, $set_data['schemaid']);
319  break;
320 
321  case 0 :
322  case 1 :
323  // deny a schema
324  // apply a permission
325  $step_data['message'] = htmlentities(($set_data['granted'] ? 'Apply' : 'Deny').'ing "'.$schema->name.'" (#'.$schema->id.') to "'.$asset->name.'" (#'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
326  $wfm->setSchema($asset->id, $set_data['schemaid'], $set_data['granted'], $set_data['cascades']);
327  break;
328  }
329  }
330 
331  // If there are multiple schema changes to make, say we are
332  // (otherwise use more specific message set above)
333  if (count($schema_changes) > 1) {
334  $step_data['message'] = htmlentities('Making '.count($schema_changes).' schema changes to '.$asset->name.' (Id: #'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
335  } else if (count($schema_changes) == 0) {
336  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
337  }
338 
339  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'workflow');
340  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
341  unset($asset);
342  } else {
343  // we do not acquire lock
344  trigger_localised_error('SYS0100', E_USER_WARNING, $asset->name);
345  }
346 
347  } else {
348  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
349  $this->_addError(translate('hipo_cannot_edit_schema', $assetid));
350  }
351  }
352 
353  if (empty($this->_running_vars['todo_assetids'])) {
354  $step_data['percent_done'] = 100;
355  $step_data['complete'] = TRUE;
356  } else {
357  $step_data['percent_done'] = ($this->_running_vars['done_changes'] / $this->_running_vars['total_changes']) * 100;
358  $step_data['complete'] = FALSE;
359  }
360 
361  return true;
362 
363  }//end processSchemas()
364 
365 
366 }//end class
367 
368 ?>