Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
report_incomplete_metadata_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/report/report_edit_fns.inc';
19 require_once SQ_FUDGE_PATH.'/general/general.inc';
32 {
33 
34 
39  function __construct()
40  {
41  parent::__construct();
42 
43  }//end constructor
44 
45 
57  public function generateReport(HIPO_Job $job, Array &$step_data, $prefix)
58  {
59  require_once SQ_FUDGE_PATH.'/general/text.inc';
60  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
61  $owner = $GLOBALS['SQ_SYSTEM']->am->getAsset($job->_running_vars['assetid'], 'report_incomplete_metadata');
62 
63  $asset_types = $owner->getAllowedAssetTypeMap();
64 
65  if (!isset($job->_running_vars['results'])) {
66  $job->_running_vars['todo'] = $GLOBALS['SQ_SYSTEM']->am->getChildren($this->getRootAssetid($owner));
67  $job->_running_vars['done'] = Array();
68  $job->_running_vars['results'] = Array();
69 
70  if (!is_dir($owner->data_path)) {
71  if (!create_directory($owner->data_path)) {
72  trigger_localised_error('CORE0198', E_USER_WARNING);
73  return FALSE;
74  }
75  }
76  $temp_file = fopen($owner->data_path.'/report.tmp', 'w');
77  if ($temp_file === FALSE) {
78  trigger_localised_error('CORE0201', E_USER_WARNING);
79  return FALSE;
80  }
81 
82 
83  fwrite($temp_file, '<?xml version="1.0" encoding="'.SQ_CONF_DEFAULT_CHARACTER_SET.'"?>'."\n");
84  fwrite($temp_file, "<assets>\n");
85  } else {
86  $temp_file = fopen($owner->data_path.'/report.tmp', 'a');
87  if ($temp_file === FALSE) {
88  trigger_localised_error('CORE0201', E_USER_WARNING);
89  return FALSE;
90  }
91  }
92 
93  if (!empty($job->_running_vars['todo'])) {
94  $keys = array_keys($job->_running_vars['todo']);
95  $assetid = array_shift($keys);
96  $asset_type = $job->_running_vars['todo'][$assetid][0]['type_code'];
97  unset($job->_running_vars['todo'][$assetid]);
98  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
99 
100  if (!is_null($asset)) {
101  // If asset type list is empty, then none were set and therefore we do all asset
102  // types. If set, then it needs to be a key in the $asset_types
103  if (empty($asset_types) || isset($asset_types[$asset->type()])) {
104  $step_data['message'] = translate('core_checking_for_metadata', $asset->name);
105 
106  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
107  $field_values = $mm->getMetadataFieldValues($asset->id);
108 
109  $empty_fields = FALSE;
110  $content = "\t".'<asset assetid="'.$asset->id.'" name="'.htmlSpecialChars($asset->name).'" short_name="'.htmlSpecialChars($asset->short_name).'" type_code="'.$asset->type().'" status="'.$asset->status.'">'."\n";
111  foreach ($field_values as $field_name => $value) {
112  $value = trim($value);
113  if (empty($value)) {
114  $content .= "\t\t".'<field>'.$field_name.'</field>'."\n";
115  $empty_fields = TRUE;
116  }
117  }
118  $content .= "\t".'</asset>'."\n";
119 
120  // write out the XML if there were empty fields
121  if ($empty_fields) fwrite($temp_file, $content);
122 
123  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
124  unset($asset);
125  } else {
126  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
127  }
128  } else {
129  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
130  $job->_addError(translate('core_cannot_generate_incomplete_metadata_report', $assetid));
131  }
132 
133  // add this assetid to the done array so we dont do it again
134  $job->_running_vars['done'][] = $assetid;
135 
136  }//end if
137 
138  if (empty($job->_running_vars['todo'])) {
139  $step_data['percent_done'] = 100;
140  $step_data['complete'] = TRUE;
141  $job->_running_vars['complete'] = TRUE;
142 
143  // move the temp file to become the real report XML file
144  fwrite($temp_file, "</assets>\n");
145  if (!copy($owner->data_path.'/report.tmp', $owner->data_path.'/report.xml')) {
146  trigger_localised_error('CORE0200', E_USER_WARNING);
147  return FALSE;
148  }
149  if (!unlink($owner->data_path.'/report.tmp')) {
150  trigger_localised_error('CORE0199', E_USER_WARNING);
151  }
152  } else {
153  $total = count($job->_running_vars['todo']) + count($job->_running_vars['done']);
154  $step_data['percent_done'] = (count($job->_running_vars['done']) / $total) * 100;
155  $step_data['complete'] = FALSE;
156  }
157  fclose($temp_file);
158  return TRUE;
159 
160  }//end generateReport()
161 
162 
173  public function paintReport(Report_Incomplete_Metadata $asset, Backend_Outputter $o, $prefix)
174  {
175  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
176 
177  $report_path = $asset->data_path.'/report.xml';
178  if (!is_file($report_path)) {
179  echo translate('report_not_generated');
180  return;
181  }
182 
183  // use simple xml to parse the report file
184  try {
185  $root = new SimpleXMLElement($report_path, LIBXML_NOCDATA, TRUE);
186  } catch (Exception $e) {
187  throw new Exception('Unable to parse report file "'.$report_path.'": '.$e->getMessage());
188  // trigger_localised_error('CORE0092', E_USER_WARNING, $report_path, $root->getMessage(), $root->getUserInfo());
189  return;
190  }
191 
192  // build the info array for printing
193  $missing_fields = Array();
194  foreach ($root->asset as $node) {
195  $assetid = (int) $node->attributes()->assetid;
196  foreach ($node->field as $field) {
197  $missing_fields[$assetid][] = (string) $field;
198  }
199  }
200 
201  if (SQ_IN_BACKEND || SQ_IN_LIMBO) {
202  echo '<b>Note</b> The current incomplete metadata report is shown below. You can regenerate this report on the details screen.';
203  }
204  $o->closeSection();
205 
206  $o->openSection(translate('missing_metadata_fields'));
207  $o->openField('');
208  if (empty($missing_fields)) {
209  echo translate('core_no_missing_metadata_fields_found');
210  return;
211  }
212 
213  ?>
214  <table class="sq-backend-table">
215  <tr>
216  <td colspan="4" class="sq-backend-table-header" style="width: 226px;"><?php echo translate('found_in'); ?></td>
217  <td class="sq-backend-table-header"><?php echo translate('empty_fields'); ?></td>
218  </tr>
219  <?php
220  foreach ($missing_fields as $assetid => $fields) {
221  $asset = NULL;
222  if ($assetid) {
223  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, '', TRUE);
224  }
225  if (is_null($asset)) {
226  $asset_type = '';
227  $asset_string = 'Unknown asset #'.$assetid;
228  $asset_bg = 'FFFFFF';
229  $asset_status = '';
230  } else {
231  $asset_type = $asset->type();
232  $asset_bg = get_asset_status_icon($asset->status);
233  $asset_status = get_status_description($asset->status);
234  $asset_url = $asset->getBackendHref('metadata');
235  $asset_string = '<u title="'.$asset->name.'">'.ellipsisize($asset->name, 40).'</u>';
236  }
237 
238  ?>
239  <tr>
240  <td class="sq-backend-table-cell" style="width: 10px; padding-right: 1px; text-align: right;">
241  <?php
242  if (!empty($asset_type)) {
243  ?>
244  <a href="<?php echo $asset_url; ?>" title="Edit this asset's metadata">#<?php echo (int) $asset->id; ?></a>
245  <?php
246  }
247  ?>
248  </td>
249 
250  <td class="sq-backend-table-cell" style="width: 16px; padding-right: 2px; padding-left: 15px;">
251  <?php
252  if (!empty($asset_type)) {
253  echo get_asset_type_icon($asset_type);
254  }
255  ?>
256  </td>
257  <td class="sq-backend-table-cell" style="padding-left: 2px;">
258  <?php echo $asset_string; ?>
259  </td>
260  <td class="sq-backend-table-cell" style="padding-right: 1px;">
261  <?php echo $asset_bg; ?><?php echo $asset_status; ?>
262  </td>
263  <td class="sq-backend-table-cell">
264  <?php
265  echo implode('<br />', $fields);
266  ?>
267  </td>
268  </tr>
269  <?php
270 
271  }//end foreach assets
272 
273  ?></table><?php
274 
275  }//end paintReport()
276 
277 
289  {
290  $write_access = $asset->writeAccess('content');
291 
292  $types = $asset->attr('types');
293  if (empty($types)) {
294  echo translate('core_no_types_defined');
295  return FALSE;
296  }
297 
298  ?>
299  <table class="sq-backend-table">
300  <tr>
301  <td class="sq-backend-table-header"><?php echo translate('asset_type'); ?></td>
302  <td class="sq-backend-table-header"><?php echo translate('inherit_types'); ?></td>
303  <?php
304  if ($write_access) {
305  ?>
306  <td class="sq-backend-table-header"><?php echo translate('remove_question'); ?></td>
307  <?php
308  }
309  ?>
310  </tr>
311  <?php
312  foreach ($types as $type => $inherit) {
313  ?>
314  <tr>
315  <td class="sq-backend-table-cell">
316  <?php
317  echo ucwords(str_replace('_',' ', $type));
318  ?>
319  </td>
320  <td class="sq-backend-table-cell">
321  <?php
322  if ($write_access) {
323  check_box($prefix."_inherit_types[$type]", $type, $inherit);
324  } else {
325  echo ($inherit) ? translate('yes') : translate('no');
326  }
327  ?>
328  </td>
329  <?php
330  if ($write_access) {
331  ?>
332  <td class="sq-backend-table-cell">
333  <?php
334  check_box($prefix."_remove[$type]", $type, FALSE);
335  ?>
336  </td>
337  <?php
338  }
339  ?>
340  </tr>
341  <?php
342  }
343  ?>
344  </table>
345  <?php
346 
347  }//end paintCurrentAssetTypes()
348 
349 
361  {
362  $types = $asset->attr('types');
363 
364  // we are looking to see if any types have been removed
365  if (isset($_POST[$prefix.'_remove'])) {
366 
367  foreach ($_POST[$prefix.'_remove'] as $type) {
368  if (isset($types[$type])) unset($types[$type]);
369  }
370  }
371 
372  $inherit_types = array_get_index($_POST, $prefix.'_inherit_types', Array());
373  foreach ($types as $key => $val) {
374  $types[$key] = isset($inherit_types[$key]) ? TRUE : FALSE;
375  }
376 
377  $asset->setAttrValue('types', $types);
378 
379  return TRUE;
380 
381  }//end processCurrentAssetTypes()
382 
383 
395  {
396  if (!$asset->writeAccess('content')) return FALSE;
397 
398  $types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
399  $types = array_diff($types, $asset->attr('types'));
400  asort($types);
401 
402  $uc_types = Array('' => '');
403  foreach ($types as $type) {
404  $uc_types[$type] = ucwords(str_replace('_',' ', $type));
405  }
406  combo_box($prefix.'_new_type', $uc_types, FALSE, '');
407 
408  }//end paintAddAssetType()
409 
410 
422  {
423  if (isset($_POST[$prefix.'_new_type'])) {
424  $type = trim($_POST[$prefix.'_new_type']);
425 
426  if (empty($type)) return FALSE;
427 
428  $types = $asset->attr('types');
429 
430  if (!isset($types[$type])) {
431  $types[$type] = TRUE;
432  $asset->setAttrValue('types', $types);
433  return TRUE;
434  }
435  }
436  return FALSE;
437 
438  }//end processAddAssetType()
439 
440 
441 }//end class
442 
443 ?>