Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_edit_metadata_schemas.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
28 {
29 
30 
36  function HIPO_Job_Edit_Metadata_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('edit_metadata_threshold'));
56 
57  if ($write_access) {
58  text_box($class.'[SQ_HIPO_METADATA_THRESHOLD]', SQ_HIPO_METADATA_THRESHOLD, 5);
59  echo ' '.translate('assets');
60  $o->note(translate('hipo_value_divided_by_schema'));
61  } else {
62  echo SQ_HIPO_METADATA_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_METADATA_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_metadata_schemas');
113 
114  }//end getHipoName()
115 
116 
125  {
126  return Array(
127  Array(
128  'name' => translate('hipo_updating_metadata_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  }//end freestyle()
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('HIPO0017', E_USER_WARNING);
193  return false;
194  }
195 
196  $this->_running_vars['done_changes'] = 0;
197  $this->_running_vars['total_changes'] = 0;
198  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
199 
200  if (empty($this->_running_vars['schema_changes'])) {
201  // no schema changes, so do nothing
202  $this->_running_vars['todo_assetids'] = Array();
203  } else {
204  $this->_running_vars['todo_assetids'] = Array();
205  foreach ($this->_running_vars['schema_changes'] as $schema_change) {
206  $assetids = $schema_change['assetids'];
207  foreach ($assetids as $assetid) {
208  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
209 
210  if (is_null($asset)) {
211  trigger_localised_error('HIPO0016', E_USER_WARNING, $assetid);
212  return '';
213  }
214 
215  if ($schema_change['cascades']) {
216  $child_assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid);
217  } else {
218  $child_assets = Array();
219  }
220 
221  // add the parent asset to the list too
222  $child_assets[$assetid] = Array (
223  0 => Array (
224  'type_code' => $asset->type(),
225  ),
226  );
227 
228  foreach ($child_assets as $assetid => $asset_info) {
229  // if this asset does not allow the user to set metadata values then
230  // I think its okay to assume that we dont need to set any schemas
231  // because nothing is ever going to get filled in - thus required
232  if (!$mm->allowsMetadata($assetid)) {
233  unset($child_assets[$assetid]);
234  }//end if
235  }//end foreach
236 
237  // Now assign schema changes to each asset
238  foreach ($child_assets as $child_assetid => $child_asset) {
239  $type_code = $child_asset[0]['type_code'];
240 
241  $this_todo =& $this->_running_vars['todo_assetids'][$child_assetid];
242  $this_todo['type_code'] = $type_code;
243  $this_todo['schema_changes'][] = $schema_change;
244  $this->_running_vars['total_changes']++;
245  }
246 
247  unset($asset);
248  }
249 
250  }
251  }
252 
253  return parent::prepare();
254 
255  }//end prepare()
256 
257 
267  function processSchemas(&$step_data, $prefix)
268  {
269  if (!empty($this->_running_vars['todo_assetids'])) {
270  reset($this->_running_vars['todo_assetids']);
271  $assetid = key($this->_running_vars['todo_assetids']);
272  $asset_type = $this->_running_vars['todo_assetids'][$assetid]['type_code'];
273  $schema_changes = $this->_running_vars['todo_assetids'][$assetid]['schema_changes'];
274  unset($this->_running_vars['todo_assetids'][$assetid]);
275 
276  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
277  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
278 
279  if (!is_null($asset)) {
280  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($assetid, 'metadata')) {
281  foreach ($schema_changes as $set_data) {
282  $this->_running_vars['done_changes']++;
283 
284  if ($set_data['schemaid']) {
285  $schema = $GLOBALS['SQ_SYSTEM']->am->getAsset($set_data['schemaid']);
286  if (is_null($schema)) {
287  $schema_name = 'Unknown Schema';
288  } else {
289  $schema_name = $schema->name;
290  }
291  } else {
292  continue;
293  }
294 
295  if ($set_data['previous_access'] === NULL) {
296  // to edit this schema, there must not currently be a schema set
297  // at all - either grant OR deny - for this asset
298  $schemas = $mm->getSchemas($asset->id, $set_data['granted']);
299  if (isset($schemas[$set_data['schemaid']])) {
300  $new_access = ($set_data['granted']) ? translate('apply') : translate('deny');
301  $current_access = ($schemas[$set_data['schemaid']]) ? translate('applied') : translate('denied');
302  $this->_addError(translate('hipo_cannot_modify_schema', $new_access, $schema_name, $asset->name, $current_access), TRUE);
303  continue;
304  }
305  } else {
306  // we need to have a schema set and the access level be the same
307  $schemas = $mm->getSchemas($asset->id);
308  if (!isset($schemas[$set_data['schemaid']])) {
309  continue;
310  } else if ($schemas[$set_data['schemaid']] != $set_data['previous_access']) {
311  $new_access = ($set_data['previous_access']) ? translate('revoke') : translate('apply');
312  $current_access = ($set_data['previous_access']) ? translate('applied') : translate('denied');
313  $this->_addError(translate('hipo_cannot_modify_schema', $new_access, $schema_name, $asset->name, $current_access), TRUE);
314  continue;
315  }
316  }
317 
318  switch ($set_data['granted']) {
319  case -1 :
320  // delete a schema
321  $step_data['message'] = htmlentities('Removing "'.$schema->name.'" (#'.$schema->id.') from "'.$asset->name.'" (#'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
322  $mm->deleteSchema($asset->id, $set_data['schemaid']);
323  break;
324 
325  case 0 :
326  case 1 :
327  // apply a schema
328  $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);
329  $mm->setSchema($asset->id, $set_data['schemaid'], $set_data['granted'], $set_data['cascades']);
330  break;
331  }
332  }//end foreach
333 
334  // If there are multiple schema changes to make, say we are
335  // (otherwise use more specific message set above)
336  if (count($schema_changes) > 1) {
337  $step_data['message'] = htmlentities('Making '.count($schema_changes).' schema changes to '.$asset->name.' (Id: #'.$asset->id.')', ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
338  } else if (count($schema_changes) == 0) {
339  $step_data['message'] = translate('hipo_skipping_asset', $asset->id);
340  }
341 
342  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'metadata');
343  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
344  unset($asset);
345  } else {
346  // we do not acquire lock
347  trigger_localised_error('SYS0100', E_USER_WARNING, $asset->name);
348  }
349  } else {
350  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
351  $this->_addError(translate('hipo_cannot_edit_schema', $assetid));
352  }
353 
354  // add this assetid to the done array so we dont do it again
355  $this->_running_vars['done_assetids'][] = $assetid;
356  }//end if
357 
358  if (empty($this->_running_vars['todo_assetids'])) {
359  $step_data['percent_done'] = 100;
360  $step_data['complete'] = TRUE;
361  } else {
362  $step_data['percent_done'] = ($this->_running_vars['done_changes'] / $this->_running_vars['total_changes']) * 100;
363  $step_data['complete'] = FALSE;
364  }
365 
366  return TRUE;
367 
368  }//end processSchemas()
369 
370 
371 }//end class
372 
373 ?>