Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
report_asset_counter_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';
20 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
21 
22 
35 {
36 
37  public $report_name = 'report.xml';
38  public $temp_report = 'report.tmp';
39 
40 
45  function __construct()
46  {
47  parent::__construct();
48 
49  }//end constructor
50 
51 
63  public function generateReport(HIPO_Job $job, Array &$step_data, $prefix)
64  {
65 
66  $am = $GLOBALS['SQ_SYSTEM']->am;
67  $owner = $am->getAsset($job->_running_vars['assetid'], 'report_asset_counter');
68  $job_vars =& $job->_running_vars;
69 
70  $report_contents = '';
71  $report_dir = $owner->data_path;
72 
73  $xml_file_name = $report_dir.'/'.$this->report_name;
74  $types = $owner->attr('types');
75 
76  if (empty($job_vars['unique_types'])) {
77  // setup initial report parameters
78 
79  $step_data['message'] = translate('core_obtaining_asset_list');
80 
81  $children = Array();
82  if (!empty($types)) {
83  foreach ($types as $type => $inherit_bool) {
84  $children_one = $am->getChildren($this->getRootAssetid($owner), $type, !$inherit_bool, NULL, NULL, NULL, TRUE, NULL, NULL, TRUE, NULL, Array(), TRUE);
85  $children = array_merge($children, $children_one);
86  }
87  } else {
88  // if types are not provided we search for all type codes
89  $children = $am->getChildren($this->getRootAssetid($owner), '', TRUE, NULL, NULL, NULL, TRUE, NULL, NULL, TRUE, NULL, Array(), TRUE);
90  }
91 
92  // Get the missing types for shadow assets if any
93  foreach($children as $child_id => $child_info) {
94  $id_parts = explode(':', $child_id);
95  // If its a shadow asset and has missing type info, get it from the bridge
96  if (isset($id_parts[1]) && empty($child_info[0]['type_code'])) {
97  $shdw_asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($child_id, Array(), TRUE, 'type_code');
98  if (!empty($shdw_asset_info[$child_id])) {
99  $children[$child_id][0]['type_code'] = $shdw_asset_info[$child_id];
100  } else {
101  $children[$child_id][0]['type_code'] = 'unknown_asset_type';
102  }
103  }//end if
104  }//end foreach
105 
106  // check if there is anything to do
107  if (empty($children)) {
108  $step_data['percent_done'] = 100;
109  $step_data['complete'] = TRUE;
110  $job->_addError(translate('report_not_generated'),TRUE);
111  return FALSE;
112  }
113 
114  // get count and type code
115  foreach ($children as $i => $info) {
116  if (!isset($job_vars['type_counts'][$info[0]['type_code']])) {
117  $job_vars['type_counts'][$info[0]['type_code']] = 1;
118  } else {
119  $job_vars['type_counts'][$info[0]['type_code']]++;
120  }
121  }
122  $job_vars['unique_types'] = array_keys($job_vars['type_counts']);
123 
124  if (!is_dir($report_dir)) {
125  if (!create_directory($report_dir)) {
126  trigger_localised_error('CORE0011', E_USER_WARNING);
127  return FALSE;
128  }
129  }
130 
131  $step_data['percent_done'] = 50;
132  $step_data['complete'] = FALSE;
133 
134  } else {
135  // provide human descriptions for each type of Asset
136  // and save the report
137 
138  $step_data['message'] = translate('core_get_readable_asset_descriptions');
139 
140  $report_contents .= '<?xml version="1.0" encoding="'.SQ_CONF_DEFAULT_CHARACTER_SET.'"?>'."\n";
141  // generate the report
142  $report_contents .= '<asset_types>';
143 
144  foreach ($job_vars['unique_types'] as $asset_id => $type_code) {
145  $name = $am->getTypeInfo($type_code, 'name');
146  $count = $job_vars['type_counts'][$type_code];
147 
148  $report_contents .= '<asset_type code="'.$type_code.'">';
149  $report_contents .= '<name>'.$name.'</name>';
150  $report_contents .= '<count>'.$count.'</count>';
151  $report_contents .= '</asset_type>';
152  }
153 
154  $report_contents .= '</asset_types>';
155 
156  // file manipulation section
157  $file_handler = fopen($xml_file_name, 'w');
158  if ($file_handler === FALSE) {
159  trigger_localised_error('CORE0017', E_USER_WARNING);
160  return FALSE;
161  }
162  fwrite($file_handler, $report_contents);
163  fclose($file_handler);
164 
165  $step_data['percent_done'] = 100;
166  $step_data['complete'] = TRUE;
167 
168  }//end else
169 
170  return TRUE;
171 
172  }//end generateReport()
173 
174 
185  public function paintReport(Report_Asset_Counter $asset, Backend_Outputter $o, $prefix)
186  {
187  $am = $GLOBALS['SQ_SYSTEM']->am;
188  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
189 
190  $report_path = $asset->data_path.'/'.$this->report_name;
191  if (!is_file($report_path)) {
192  echo translate('report_not_generated');
193  return;
194  }
195 
196  // @see: unit_tests/by_file/core/assets/reports/asset_counter/test_asset_counter_report.test
197  // use simple xml to parse the report file
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  // build the info array for printing
207  $asset_types = Array();
208  foreach ($root->asset_type as $node) {
209  $type = (string)$node->attributes()->code;
210  $asset_types[$type] = Array(
211  'name' => (string)$node->name,
212  'count' => (int)$node->count,
213  );
214  }
215 
216  if (SQ_IN_BACKEND || SQ_IN_LIMBO) {
217  echo '<b>Note</b> The current asset counter report is shown below. You can regenerate this report on the details screen.';
218  }
219 
220  $o->closeSection();
221 
222  $o->openSection(translate('asset_type_counts'));
223  $o->openField('');
224  if (empty($asset_types)) {
225  echo 'Report is empty';
226  return;
227  }
228 
229  $sort_by_attr = $asset->getAttribute('sort_by');
230  $sort_by = $sort_by_attr->value;
231 
232  $row_format =
233  '
234  <tr>
235  <td class="sq-backend-table-cell">
236  <script language="JavaScript" type="text/javascript">sq_print_icon("%s", "16", "16", "");</script>
237  </td>
238  <td class="sq-backend-table-cell" style="width: 10px;">
239  %s
240  </td>
241  <td class="sq-backend-table-cell">
242  %s
243  </td>
244  <td class="sq-backend-table-cell">
245  %s
246  </td>
247  </tr>
248  ';
249 
250  $rows_string = '';
251  $rows = Array();
252  foreach ($asset_types as $typecode => $data) {
253  $counts[$typecode] = $data['count'];
254  $names[$typecode] = $data['name'];
255  $types[$typecode] = $typecode;
256 
257  $rows[$typecode] = sprintf($row_format,$am->getAssetIconURL($typecode),$data['count'],$typecode,$data['name']);
258  }
259 
260 
261  // sort the table
262  switch ($sort_by) {
263  case('count'):
264  $target_array = $counts;
265  break;
266 
267  case('name'):
268  $target_array = $names;
269  break;
270 
271  case('type'):
272  default:
273  $sort_by = 'type';
274  $target_array = $types;
275 
276  }
277 
278  if ($asset->attr('reverse_sort')) {
279  arsort($target_array);
280  } else {
281  asort($target_array);
282  }
283 
284  $sort_by_name = $sort_by_attr->_params['options'][$sort_by];
285 
286  $table_format =
287  '
288  <h3>Sorted by: <span style="color: red">'.$sort_by_name.'</span></h3>
289  <table class="sq-backend-table">
290  <tr>
291  <td class="sq-backend-table-header" style="width: 10ex">Icon</td>
292  <td class="sq-backend-table-header" style="width: 10ex">Count</td>
293  <td class="sq-backend-table-header">Code</td>
294  <td class="sq-backend-table-header" width="70%%">Type</td>
295  </tr>
296  %s
297  </table>
298  ';
299 
300  foreach ($target_array as $typecode => $data) {
301  $rows_string .= $rows[$typecode];
302  }
303  $table = sprintf($table_format,$rows_string);
304 
305  echo $table;
306 
307  }//end paintReport()
308 
309 
320  public function paintCurrentAssetTypes(Report_Asset_Counter $asset, Backend_Outputter $o, $prefix)
321  {
322  $write_access = $asset->writeAccess('content');
323 
324  $types = $asset->attr('types');
325  if (empty($types)) {
326  echo translate('core_no_types_defined');
327  return FALSE;
328  }
329 
330  ?>
331  <table class="sq-backend-table">
332  <tr>
333  <td class="sq-backend-table-header"><?php echo translate('asset_type'); ?></td>
334  <td class="sq-backend-table-header"><?php echo translate('inherit_types'); ?></td>
335  <?php
336  if ($write_access) {
337  ?>
338  <td class="sq-backend-table-header"><?php echo translate('remove_question'); ?></td>
339  <?php
340  }
341  ?>
342  </tr>
343  <?php
344  foreach ($types as $type => $inherit) {
345  ?>
346  <tr>
347  <td class="sq-backend-table-cell">
348  <?php
349  echo ucwords(str_replace('_',' ', $type));
350  ?>
351  </td>
352  <td class="sq-backend-table-cell">
353  <?php
354  if ($write_access) {
355  check_box($prefix."_inherit_types[$type]", $type, $inherit);
356  } else {
357  echo ($inherit) ? translate('yes') : translate('no');
358  }
359  ?>
360  </td>
361  <?php
362  if ($write_access) {
363  ?>
364  <td class="sq-backend-table-cell">
365  <?php
366  check_box($prefix."_remove[$type]", $type, FALSE);
367  ?>
368  </td>
369  <?php
370  }
371  ?>
372  </tr>
373  <?php
374  }
375  ?>
376  </table>
377  <?php
378 
379  return TRUE;
380 
381  }//end paintCurrentAssetTypes()
382 
383 
395  {
396  $types = $asset->attr('types');
397 
398  // we are looking to see if any types have been removed
399  if (isset($_POST[$prefix.'_remove'])) {
400 
401  foreach ($_POST[$prefix.'_remove'] as $type) {
402  if (isset($types[$type])) unset($types[$type]);
403  }
404  }
405 
406  $inherit_types = array_get_index($_POST, $prefix.'_inherit_types', Array());
407  foreach ($types as $key => $val) {
408  $types[$key] = isset($inherit_types[$key]) ? TRUE : FALSE;
409  }
410 
411  $asset->setAttrValue('types', $types);
412 
413  return TRUE;
414 
415  }//end processCurrentAssetTypes()
416 
417 
428  public function paintAddAssetType(Report_Asset_Counter $asset, Backend_Outputter $o, $prefix)
429  {
430  if (!$asset->writeAccess('content')) return FALSE;
431 
432  $types = $GLOBALS['SQ_SYSTEM']->am->getTypeList();
433  $types = array_diff($types, $asset->attr('types'));
434  asort($types);
435 
436  $uc_types = Array('' => '');
437  foreach ($types as $type) {
438  $uc_types[$type] = ucwords(str_replace('_',' ', $type));
439  }
440  combo_box($prefix.'_new_type', $uc_types, FALSE, '');
441 
442  return TRUE;
443 
444  }//end paintAddAssetType()
445 
446 
457  public function processAddAssetType(Report_Asset_Counter $asset, Backend_Outputter $o, $prefix)
458  {
459  if (isset($_POST[$prefix.'_new_type'])) {
460  $type = trim($_POST[$prefix.'_new_type']);
461 
462  if (empty($type)) return FALSE;
463 
464  $types = $asset->attr('types');
465 
466  if (!isset($types[$type])) {
467  $types[$type] = TRUE;
468  $asset->setAttrValue('types', $types);
469  return TRUE;
470  }
471  }
472  return FALSE;
473 
474  }//end processAddAssetType()
475 
476 
477 }//end class
478 
479 ?>