Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_edit_metadata_field_values.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
28 {
29 
30 
36  function HIPO_Job_Edit_Metadata_Field_Values($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  // hipo_job_edit_metadata_schemas.inc will handle this instead, so just do nothing
55  return;
56  }//end paintConfig()
57 
58 
65  public static function getConfigVars()
66  {
67  return Array(
68  'SQ_HIPO_METADATA_THRESHOLD' => Array('editable' => 1, 'default' => 1),
69  );
70 
71  }//end getConfigVars()
72 
73 
85  function getCodeName()
86  {
87  return parent::getCodeName().'-'.md5(implode('-',array_keys($this->_running_vars['assets'])));
88 
89  }//end getCodeName()
90 
91 
98  function getHipoName()
99  {
100  return translate('hipo_name_edit_metadata_field_values');
101 
102  }//end getHipoName()
103 
104 
113  {
114  return Array(
115  Array(
116  'name' => translate('hipo_updating_metadata_field_values'),
117  'function_call' => Array(
118  'process_function' => 'processFieldValues',
119  ),
120  'running_mode' => 'server',
121  'auto_step' => TRUE,
122  'allow_cancel' => TRUE,
123  'percent_done' => 0,
124  'complete' => FALSE,
125  'message' => '',
126  ),
127  );
128 
129  }//end getInitialStepData()
130 
131 
142  {
143  if (SQ_HIPO_METADATA_THRESHOLD == 0) return 0;
144  if (!isset($this->_running_vars['todo_assetids'])) {
145  return 0;
146  }
147  return ((count($this->_running_vars['todo_assetids']) / (SQ_HIPO_METADATA_THRESHOLD / count($this->_running_vars['metadata_field_value_changes']))) * 100);
148 
149  }//end getThresholdPercentageRequired()
150 
151 
158  function freestyle()
159  {
160  while (!empty($this->_running_vars['todo_assetids'])) {
161  if (!$this->processFieldValues($this->_steps[0], get_class($this))) {
162  return FALSE;
163  }
164  }
165  return TRUE;
166 
167  }//end freestyle()
168 
169 
176  function prepare()
177  {
178  // To start editing Metadata Field values, we need to have an asset id as a starting point
179  if (empty($this->_running_vars['assets'])) {
180  trigger_localised_error('HIPO0017', E_USER_WARNING);
181  return FALSE;
182  }
183 
184  // Allow an empty context ID, however it should be discouraged because
185  // running it inside a Squiz Server process could have unpredictable
186  // results
187  if (empty($this->_running_vars['contextid'])) {
188  $this->_running_vars['contextid'] = $GLOBALS['SQ_SYSTEM']->getContextId();
189  }
190 
191  $this->_running_vars['done_assetids'] = Array();
192  if (empty($this->_running_vars['metadata_field_value_changes'])) {
193  // no Metadata Field Value changes supplied, so do nothing
194  $this->_running_vars['todo_assetids'] = Array();
195  } else {
196 
197  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
198  $assets = $this->_running_vars['assets'];
199  $child_assets = Array();
200  for (reset($assets); NULL !== ($assetid = key($assets)); next($assets)) {
201  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
202  if (is_null($asset)) {
203  trigger_localised_error('HIPO0016', E_USER_WARNING, $assetid);
204  return '';
205  }
206  $child_assets += $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid);
207  $child_assets[$assetid] = Array (
208  0 => Array (
209  'type_code' => $asset->type(),
210  ),
211  );
212  unset($asset);
213  }
214  $types_with_assets = Array();
215 
216  foreach ($child_assets as $assetid => $asset_info) {
217  // if this asset does not allow the user to set metadata values then
218  // I think its okay to assume that we dont need to set any schemas
219  // because nothing is ever going to get filled in - thus required
220  if (!$mm->allowsMetadata($assetid)) {
221  unset($child_assets[$assetid]);
222  }//end if
223  }//end foreach
224 
225  $this->_running_vars['todo_assetids'] = $child_assets;
226  }//end else
227 
228  return parent::prepare();
229 
230  }//end prepare()
231 
232 
242  function processFieldValues(&$step_data, $prefix)
243  {
244  $GLOBALS['SQ_SYSTEM']->changeContext($this->_running_vars['contextid']);
245 
246  if (!empty($this->_running_vars['todo_assetids'])) {
247  reset($this->_running_vars['todo_assetids']);
248  $assetid = key($this->_running_vars['todo_assetids']);
249  $asset_type = $this->_running_vars['todo_assetids'][$assetid][0]['type_code'];
250  unset($this->_running_vars['todo_assetids'][$assetid]);
251 
252  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
253  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
254 
255  if (!is_null($asset)) {
256  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($assetid, 'metadata')) {
257  foreach ($this->_running_vars['metadata_field_value_changes'] as $set_data) {
258  $schemata = $mm->getSchemas($assetid);
259 
260  if (!empty($schemata) && $set_data['field_asset_id']) {
261  $metadata_field = $GLOBALS['SQ_SYSTEM']->am->getAsset($set_data['field_asset_id']);
262  if (is_null($metadata_field)) {
263  $field_name = 'Unknown Field';
264  } else {
265  $field_name = $metadata_field->name;
266 
267  $mm->setMetadata($assetid, $set_data['field_value']);
268  // All context metadata files need to be regenerated if this metadata field is non-contextable
269  if ((boolean)$metadata_field->attr('is_contextable') === FALSE || $metadata_field instanceof Metadata_Field_Select) {
270  $all_contexts = $GLOBALS['SQ_SYSTEM']->getAllContexts();
271  foreach ($all_contexts as $contextid => $context_data) {
272  $mm->generateContentFile($assetid, FALSE, $contextid);
273  }
274  } else {
275  $mm->generateContentFile($assetid);
276  }
277  }
278  }
279 
280  }//end foreach
281 
282  $step_data['message'] = translate('hipo_editing_metadata_field_values', htmlentities($asset->name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
283 
284  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'metadata');
285  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
286  unset($asset);
287  } else {
288  // we do not acquire lock
289  trigger_localised_error('SYS0100', E_USER_WARNING, $asset->name);
290  }
291  } else {
292  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
293  $this->_addError(translate('hipo_cannot_edit_schema', $assetid));
294  }
295 
296  // add this assetid to the done array so we dont do it again
297  $this->_running_vars['done_assetids'][] = $assetid;
298  }//end if
299 
300  if (empty($this->_running_vars['todo_assetids'])) {
301  $step_data['percent_done'] = 100;
302  $step_data['complete'] = TRUE;
303  unset($this->_running_vars['done_assetids']);
304  } else {
305  $total = count($this->_running_vars['todo_assetids']) + count($this->_running_vars['done_assetids']);
306  $step_data['percent_done'] =(count($this->_running_vars['done_assetids']) / $total) * 100;
307  $step_data['complete'] = FALSE;
308  }
309 
310  $GLOBALS['SQ_SYSTEM']->restoreContext();
311 
312  return TRUE;
313 
314  }//end processFieldValues()
315 
316 
327  function getLockAssetDetails(&$job_type, &$running_vars, &$options)
328  {
329  $job_type = 'hipo_job_acquire_locks';
330 
331  $options['auto_complete'] = TRUE;
332 
333  $running_vars = Array(
334  'assetids' => array_keys($this->_running_vars['assets']),
335  'lock_type' => Array('metadata'),
336  'forceably_acquire' => FALSE,
337  'dependants_only' => FALSE,
338  );
339 
340  }//end getLockAssetDetails()
341 
342 
352  function releaseLocks(&$step_data, $prefix)
353  {
354  if (!isset($this->_running_vars['re_lock_assets'])) {
355  $this->_running_vars['re_lock_assets'] = $this->_running_vars['assets'];
356  }
357 
358  $assetid = key($this->_running_vars['re_lock_assets']);
359  unset($this->_running_vars['re_lock_assets'][$assetid]);
360 
361  $children = $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid);
362 
363  foreach ($children as $dep_child_id => $dep_child_type_code) {
364  $GLOBALS['SQ_SYSTEM']->am->releaseLock($dep_child_id, 'metadata');
365  }
366 
367  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($assetid));
368 
369  $step_data['message'] = translate('hipo_release_locks', $asset_info[$assetid]['name']).translate('hipo_release_locks_dependents');
370 
371  $GLOBALS['SQ_SYSTEM']->am->releaseLock($assetid, 'all');
372 
373  $this->_running_vars['done_assetids'][] = $assetid;
374 
375  if (empty($this->_running_vars['re_lock_assets'])) {
376  unset($this->_running_vars['done_assetids']);
377  $step_data['percent_done'] = 100;
378  $step_data['complete'] = TRUE;
379  } else {
380  $step_data['percent_done'] = (count($this->_running_vars['done_assetids']) / count($this->_running_vars['assets'])) * 100;
381  $step_data['complete'] = FALSE;
382  }
383 
384  return TRUE;
385 
386  }//end releaseLocks()
387 
388 
389 }//end class
390 
391 ?>