Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
report_safe_trash_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_safe_trash');
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  fwrite($temp_file, '<?xml version="1.0" encoding="'.SQ_CONF_DEFAULT_CHARACTER_SET.'"?>'."\n");
82  fwrite($temp_file, "<assets>\n");
83  } else {
84  $temp_file = fopen($owner->data_path.'/report.tmp', 'a');
85  if ($temp_file === FALSE) {
86  trigger_localised_error('CORE0201', E_USER_WARNING);
87  return FALSE;
88  }
89  }
90  if (!empty($job->_running_vars['todo'])) {
91 
92  $keys = array_keys($job->_running_vars['todo']);
93  $assetid = array_shift($keys);
94  $asset_type = $job->_running_vars['todo'][$assetid][0]['type_code'];
95  unset($job->_running_vars['todo'][$assetid]);
96  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $asset_type);
97 
98  if (!is_null($asset) && !($asset instanceof Root_Folder)) {
99  // If asset type list is empty, then none were set and therefore we do all asset
100  // types. If set, then it needs to be a key in the $asset_types
101  if (empty($asset_types) || isset($asset_types[$asset->type()])) {
102  $job_link = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_NOTICE, 'cron_job_attempt_safe_trash', TRUE, 'updating', 'minor');
103  if ($job_link) {
104 
105  $cron_job = $GLOBALS['SQ_SYSTEM']->am->getAsset($job_link['majorid']);
106  $step_data['message'] = translate('core_checking_for_metadata', $asset->name);
107 
108  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
109  $field_values = $mm->getMetadataFieldValues($asset->id);
110  $trash_errors = $GLOBALS['SQ_SYSTEM']->am->canSafeTrashAsset($asset->id);
111 
112  flush();
113 
114  $content = "\t".'<asset assetid="'.$asset->id.'" name="'.htmlspecialchars($asset->name).'" short_name="'.htmlSpecialChars($asset->short_name).'" type_code="'.$asset->type().'" status="'.$asset->status.'" marked_since="'.$cron_job->created.'">';
115 
116  if (!empty($trash_errors['links'])) {
117  $content .= '<unsafe_links>';
118  foreach ($trash_errors['links'] as $link) {
119  $content .= ' <assetid>'.$link['majorid'].'</assetid>';
120  }
121  $content .= '</unsafe_links>';
122  }
123 
124  if (!empty($trash_errors['children'])) {
125  $content .= '<unsafe_children>';
126  foreach ($trash_errors['children'] as $link) {
127  $content .= ' <assetid>'.$link['minorid'].'</assetid>';
128  }
129  $content .= '</unsafe_children>';
130  }
131 
132  $content .= '</asset>'."\n";
133 
134  // write out the XML if there were any trash errors
135  if (!empty($trash_errors)) fwrite($temp_file, $content);
136  } else {
137  $step_data['message'] = translate('hipo_skipping_asset', $assetid).' - NO JOB';
138  }
139 
140  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
141  unset($asset);
142  } else {
143  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
144  }
145  } else {
146  $step_data['message'] = translate('hipo_skipping_asset', $assetid);
147  $job->_addError(translate('core_cannot_generate_incomplete_metadata_report', $assetid));
148  }
149 
150  // add this assetid to the done array so we dont do it again
151  $job->_running_vars['done'][] = $assetid;
152 
153  }//end if
154 
155  if (empty($job->_running_vars['todo']) && !$job->_running_vars['complete']) {
156  $step_data['percent_done'] = 100;
157  $step_data['complete'] = TRUE;
158 
159  // move the temp file to become the real report XML file
160  fwrite($temp_file, "</assets>\n");
161  if (!copy($owner->data_path.'/report.tmp', $owner->data_path.'/report.xml')) {
162  trigger_localised_error('CORE0200', E_USER_WARNING);
163  return FALSE;
164  }
165  if (!unlink($owner->data_path.'/report.tmp')) {
166  trigger_localised_error('CORE0199', E_USER_WARNING);
167  }
168  } else {
169  $total = count($job->_running_vars['todo']) + count($job->_running_vars['done']);
170  $step_data['percent_done'] = (count($job->_running_vars['done']) / $total) * 100;
171  $step_data['complete'] = FALSE;
172  }
173  fclose($temp_file);
174  return TRUE;
175 
176  }//end generateReport()
177 
178 
189  public function paintReport(Report_Safe_Trash $asset, Backend_Outputter $o, $prefix)
190  {
191  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
192  $report_path = $asset->data_path.'/report.xml';
193  if (!is_file($report_path)) {
194  echo translate('report_not_generated');
195  return;
196  }
197 
198  try {
199  $root = new SimpleXMLElement($report_path, LIBXML_NOCDATA, TRUE);
200  } catch (Exception $e) {
201  throw new Exception('Unable to parse report file "'.$report_path.'": '.$e->getMessage());
202  // trigger_localised_error('CORE0092', E_USER_WARNING, $report_path, $root->getMessage(), $root->getUserInfo());
203  return;
204  }
205 
206  $missing_fields = Array();
207 
208  foreach ($root->asset as $result) {
209  $id = (int) $result->attributes()->assetid;
210  $missing_fields[$id]['since'] = (string) $result->attributes()->marked_since;
211 
212  foreach ($result->unsafe_children as $unsafe_children) {
213  foreach ($unsafe_children->assetid as $unsafe_assetid) {
214  $missing_fields[$id]['children'][] = (int) $unsafe_assetid;
215  }
216  }
217  foreach ($result->unsafe_links as $unsafe_links) {
218  foreach ($unsafe_links->assetid as $unsafe_assetid) {
219  $missing_fields[$id]['links'][] = (int) $unsafe_assetid;
220  }
221  }
222  }//end foreach
223 
224  if (SQ_IN_BACKEND || SQ_IN_LIMBO) {
225  echo '<b>Note</b> The current safe trash report is shown below. You can regenerate this report on the <a href="'.$asset->getBackendHref('details').'">details screen</a>.';
226  }
227  $o->closeSection();
228 
229  $o->openSection(translate('report_safe_trash_section_status'));
230  $o->openField('');
231  if (empty($missing_fields)) {
232  echo 'NO ASSET FOUND';
233  return;
234  }
235  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
236  ?>
237  <table class="sq-backend-table">
238  <tr>
239  <td class="sq-backend-table-header"><?php echo translate('asset'); ?></td>
240  <td class="sq-backend-table-header" style="width: 180px"><?php echo translate('report_safe_trash_marked_since'); ?></td>
241  <td class="sq-backend-table-header" style="width: 150px"><?php echo translate('status'); ?></td>
242  <td class="sq-backend-table-header" style="width: 150px"><?php echo translate('report_safe_trash_unsafe_links'); ?></td>
243  <td class="sq-backend-table-header" style="width: 150px"><?php echo translate('report_safe_trash_unsafe_children'); ?></td>
244  </tr>
245  <?php
246  foreach ($missing_fields as $assetid => $fields) {
247  $asset_tmp = NULL;
248  if ($assetid) {
249  $asset_tmp = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, '', TRUE);
250  }
251  if (is_null($asset_tmp)) {
252  $asset_type = '';
253  $asset_string = 'Unknown asset #'.$assetid;
254  $asset_bg = 'FFFFFF';
255  $asset_status = '';
256  } else {
257  $asset_type = $asset_tmp->type();
258  $asset_bg = get_asset_status_icon($asset_tmp->status);
259  $asset_status = get_status_description($asset_tmp->status);
260  $asset_url = $asset_tmp->getBackendHref('metadata');
261  $asset_string = '<a href="'.$asset_tmp->getBackendHref('details').'" title="'.$asset_tmp->name.'">'.ellipsisize($asset_tmp->name, 40).'</a>';
262  }
263 
264  ?>
265  <tr>
266  <td class="sq-backend-table-cell" style="padding-right: 1px;">
267  <?php
268  if (!empty($asset_type)) {
269  echo get_asset_tag_line($asset_tmp->id, 'details');
270  }
271  ?>
272  </td>
273  <td class="sq-backend-table-cell">
274  <?php echo easy_datetime((int)$missing_fields[$asset_tmp->id]['since']); ?>
275  </td>
276  <td class="sq-backend-table-cell" style="padding-right: 1px;">
277  <?php
278  if ($asset_tmp->status >= SQ_STATUS_LIVE) {
279  ?>
280  <span class="sq-backend-warning">
281  <?php
282  }
283  ?>
284  <?php echo $asset_bg; ?><?php echo $asset_status; ?>
285  <?php
286  if ($asset_tmp->status >= SQ_STATUS_LIVE) {
287  ?>
288  </span>
289  <?php
290  }
291  ?>
292  </td>
293  <td class="sq-backend-table-cell">
294  <?php
295  if (empty($missing_fields[$asset_tmp->id]['links'])) {
296  echo translate('none');
297  } else {
298  ?>
299  <ul style="list-style-type: none; padding-left: 0; margin-left: 0;">
300  <?php
301  foreach ($missing_fields[$asset_tmp->id]['links'] as $linked_assetid) {
302  ?>
303  <li class="sq-backend-warning"><?php echo get_asset_tag_line($linked_assetid, 'details'); ?></li>
304  <?php
305  }
306  ?>
307  </ul>
308  <?php
309  }
310  ?>
311  </td>
312  <td class="sq-backend-table-cell">
313  <?php
314  if (empty($missing_fields[$asset_tmp->id]['children'])) {
315  echo translate('none');
316  } else {
317  ?>
318  <ul style="list-style-type: none; padding-left: 0; margin-left: 0;">
319  <?php
320  foreach ($missing_fields[$asset_tmp->id]['children'] as $linked_assetid) {
321  ?>
322  <li class="sq-backend-warning"><?php echo get_asset_tag_line($linked_assetid, 'details'); ?></li>
323  <?php
324  }
325  ?>
326  </ul>
327  <?php
328  }
329  ?>
330  </td>
331  </tr>
332  <?php
333 
334  }//end foreach assets
335 
336  ?></table><?php
337 
338  }//end paintReport()
339 
340 
351  public function paintCurrentAssetTypes(Report_Safe_Trash $asset, Backend_Outputter $o, $prefix)
352  {
353  $write_access = $asset->writeAccess('content');
354 
355  $types = $asset->attr('types');
356  if (empty($types)) {
357  echo translate('core_no_types_defined');
358  return FALSE;
359  }
360 
361  ?>
362  <table class="sq-backend-table">
363  <tr>
364  <td class="sq-backend-table-header"><?php echo translate('asset_type'); ?></td>
365  <td class="sq-backend-table-header"><?php echo translate('inherit_types'); ?></td>
366  <?php
367  if ($write_access) {
368  ?>
369  <td class="sq-backend-table-header"><?php echo translate('remove_question'); ?></td>
370  <?php
371  }
372  ?>
373  </tr>
374  <?php
375  foreach ($types as $type => $inherit) {
376  ?>
377  <tr>
378  <td class="sq-backend-table-cell">
379  <?php
380  echo ucwords(str_replace('_',' ', $type));
381  ?>
382  </td>
383  <td class="sq-backend-table-cell">
384  <?php
385  if ($write_access) {
386  check_box($prefix."_inherit_types[$type]", $type, $inherit);
387  } else {
388  echo ($inherit) ? translate('yes') : translate('no');
389  }
390  ?>
391  </td>
392  <?php
393  if ($write_access) {
394  ?>
395  <td class="sq-backend-table-cell">
396  <?php
397  check_box($prefix."_remove[$type]", $type, FALSE);
398  ?>
399  </td>
400  <?php
401  }
402  ?>
403  </tr>
404  <?php
405  }
406  ?>
407  </table>
408  <?php
409 
410  }//end paintCurrentAssetTypes()
411 
412 
423  public function processCurrentAssetTypes(Report_Safe_Trash $asset, Backend_Outputter $o, $prefix)
424  {
425  $types = $asset->attr('types');
426 
427  // we are looking to see if any types have been removed
428  if (isset($_POST[$prefix.'_remove'])) {
429 
430  foreach ($_POST[$prefix.'_remove'] as $type) {
431  if (isset($types[$type])) unset($types[$type]);
432  }
433  }
434 
435  $inherit_types = array_get_index($_POST, $prefix.'_inherit_types', Array());
436  foreach ($types as $key => $val) {
437  $types[$key] = isset($inherit_types[$key]) ? TRUE : FALSE;
438  }
439 
440  $asset->setAttrValue('types', $types);
441 
442  return TRUE;
443 
444  }//end processCurrentAssetTypes()
445 
446 
457  public function paintAddAssetType(Report_Safe_Trash $asset, Backend_Outputter $o, $prefix)
458  {
459  if (!$asset->writeAccess('content')) return FALSE;
460 
461  $types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
462  $types = array_diff($types, $asset->attr('types'));
463  asort($types);
464 
465  $uc_types = Array('' => '');
466  foreach ($types as $type) {
467  $uc_types[$type] = ucwords(str_replace('_',' ', $type));
468  }
469  combo_box($prefix.'_new_type', $uc_types, FALSE, '');
470 
471  }//end paintAddAssetType()
472 
473 
484  public function processAddAssetType(Report_Safe_Trash $asset, Backend_Outputter $o, $prefix)
485  {
486  if (isset($_POST[$prefix.'_new_type'])) {
487  $type = trim($_POST[$prefix.'_new_type']);
488 
489  if (empty($type)) return FALSE;
490 
491  $types = $asset->attr('types');
492 
493  if (!isset($types[$type])) {
494  $types[$type] = TRUE;
495  $asset->setAttrValue('types', $types);
496  return TRUE;
497  }
498  }
499  return FALSE;
500 
501  }//end processAddAssetType()
502 
503 
504 }//end class
505 
506 ?>