Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
report_asset_statuses_edit_fns.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/report/report_edit_fns.inc';
18 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
19 
33 {
34 
35 
40  function __construct()
41  {
42  parent::__construct();
43 
44  }//end constructor
45 
46 
58  function generateReport(&$job, &$step_data, $prefix)
59  {
60  $am = $GLOBALS['SQ_SYSTEM']->am;
61  $db = MatrixDAL::getDb();
62  $owner = $am->getAsset($job->_running_vars['assetid'], 'report_asset_statuses');
63  $where = Array();
64  $tables = Array('sq_ast a');
65 
66  $root_link = $GLOBALS['SQ_SYSTEM']->am->getLink($owner->id, SQ_LINK_NOTICE, '', FALSE, 'root');
67  if (!empty($root_link)) {
68  // need to restrict results according to link tree position too
69  try {
70  $bind_vars['root_link_minorid'] = $root_link['minorid'];
71  $parent_treeid = MatrixDAL::executeOne('cms_package', 'getTreeidFromMinor', $bind_vars);
72  } catch (Exception $e) {
73  throw new Exception('Unable to get tree id from minor id: '.$e->getMessage());
74  }
75  $where[] = 'SUBSTR(t.treeid, 1, '.strlen($parent_treeid).') = '.MatrixDAL::quote($parent_treeid);
76  $tables[] = 'sq_ast_lnk l';
77  $tables[] = 'sq_ast_lnk_tree t';
78  $where[] = 'l.linkid = t.linkid';
79  $where[] = 'a.assetid = l.minorid';
80  }
81 
82  $search_status = str_replace('|', ', ', $owner->attr('search_status'));
83  if ($search_status != SQ_SC_STATUS_ALL) {
84  $where[] = 'a.status in ('.$search_status.')';
85  }
86 
87  $period_length = $owner->attr('status_change_period');
88  if ($period_length) {
89  $operator = $owner->attr('status_change_comparator_more_than') ? '<' : '>';
90  $timestamp = time() - $period_length;
91  $where[] = 'a.status_changed '.$operator.' '.db_extras_todate(MatrixDAL::getDbType(), ts_iso8601($timestamp));
92  }
93 
94  $sql = 'SELECT
95  a.assetid, a.type_code, a.name, a.status, a.status_changed, a.status_changed_userid
96  FROM
97  '.implode(', ', $tables).'
98  WHERE
99  ('.implode(') AND (', $where).')
100  ORDER BY
101  a.type_code';
102  $query = MatrixDAL::preparePdoQuery($sql);
103  $result = MatrixDAL::executePdoAssoc($query);
104 
105  if (empty($result)) {
106  $step_data['percent_done'] = 100;
107  $step_data['complete'] = TRUE;
108  $job->_addError('No assets were found matching your criteria', TRUE);
109  return FALSE;
110  }
111  // find the users' names
112  $userids = Array();
113  foreach ($result as $r) {
114  if (!empty($r['status_changed_userid']) && (FALSE === strpos($r['status_changed_userid'], ':'))) {
115  $userids[] = $r['status_changed_userid'];
116  }
117  }
118  $names = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($userids, Array(), FALSE, 'name');
119  foreach ($result as $i => $r) {
120  if (isset($names[$r['status_changed_userid']])) {
121  $result[$i]['user_name'] = array_get_index($names, $r['status_changed_userid'], 'Unknown');
122  } else if (!empty($r['status_changed_userid'])) {
123  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($r['status_changed_userid']);
124  $result[$i]['user_name'] = $asset->name;
125  } else {
126  $result[$i]['user_name'] = 'Unknown';
127  }
128  }
129 
130  // generate XML contents
131  $contents = '<?xml version="1.0" encoding="'.SQ_CONF_DEFAULT_CHARACTER_SET.'"?>'."\n";
132  $contents .= '<asset_list>'."\n";
133  foreach ($result as $values) {
134  $contents .= '<asset assetid="'.htmlSpecialChars($values['assetid']).'" name="'.htmlSpecialChars($values['name']).'" type_code="'.$values['type_code'].'">'."\n";
135  $contents .= ' <status value="'.$values['status'].'" changed="'.$values['status_changed'].'" changed_userid="'.htmlSpecialChars($values['status_changed_userid']).'" changed_user_name="'.htmlSpecialChars($values['user_name']).'"/>'."\n";
136  $contents .= '</asset>'."\n";
137  }
138  $contents .= '</asset_list>';
139 
140  $report_dir = $owner->data_path;
141  $xml_file_name = $report_dir.'/report.xml';
142 
143  if (!is_dir($report_dir)) {
144  if (!create_directory($report_dir)) {
145  trigger_localised_error('CORE0198', E_USER_WARNING);
146  return FALSE;
147  }
148  }
149 
150  // file manipulation section
151  $file_handler = fopen($xml_file_name, 'w');
152  if ($file_handler === FALSE) {
153  trigger_localised_error('CORE0017', E_USER_WARNING);
154  return FALSE;
155  }
156  fwrite($file_handler, $contents);
157  fclose($file_handler);
158 
159  $step_data['percent_done'] = 100;
160  $step_data['complete'] = TRUE;
161 
162  unset($owner->_tmp['getAssetMapLinks']);
163  unset($owner->_tmp['getLinks']);
164  return TRUE;
165 
166  }//end generateReport()
167 
168 
179  function paintLastStatusChange(&$asset, &$o, $prefix)
180  {
181  $write_access = $asset->writeAccess('attributes');
182 
183  $comparator_attr = $asset->getAttribute('status_change_comparator_more_than');
184  $comparator_attr->setEditParam('true_text', 'more');
185  $comparator_attr->setEditParam('false_text', 'less');
186  $comparator_attr->paint($prefix.'_comparator', !$write_access);
187  echo ' than ';
188  $comparator_attr = $asset->getAttribute('status_change_period');
189  $comparator_attr->paint($prefix.'_period', !$write_access);
190  echo ' ago';
191  return TRUE;
192 
193  }//end paintLastStatusChange()
194 
195 
206  function processLastStatusChange(&$asset, &$o, $prefix)
207  {
208  $comparator_attr = $asset->getAttribute('status_change_comparator_more_than');
209  $comparator_attr->process($prefix.'_comparator');
210  $asset->setAttrValue('status_change_comparator_more_than', $comparator_attr->value);
211  $period_attr = $asset->getAttribute('status_change_period');
212  $period_attr->process($prefix.'_period');
213  $asset->setAttrValue('status_change_period', $period_attr->value);
214  return TRUE;
215 
216  }//end processLastStatusChange()
217 
218 
229  function paintReport(&$asset, &$o, $prefix)
230  {
231  $am = $GLOBALS['SQ_SYSTEM']->am;
232 
233  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
234  $report_path = $asset->data_path.'/report.xml';
235 
236  if (!is_file($report_path)) {
237  echo 'This report has not been generated';
238  return;
239  }
240 
241  try {
242  $root = new SimpleXMLElement($report_path, LIBXML_NOCDATA, TRUE);
243  } catch (Exception $e) {
244  throw new Exception('Unable to parse report file "'.$report_path.'": '.$e->getMessage());
245  // trigger_localised_error('CORE0092', E_USER_WARNING, $report_path, $root->getMessage(), $root->getUserInfo());
246  return;
247  }
248 
249  ?>
250  <table class="sq-backend-table">
251  <tr>
252  <th class="sq-backend-table-header" style="width: 10ex"><?php echo translate('cms_reports_asset_statuses_asset_icon'); ?></th>
253  <th class="sq-backend-table-header" style="width: 10ex"><?php echo translate('assetid'); ?></th>
254  <th class="sq-backend-table-header"><?php echo translate('name'); ?></th>
255  <th class="sq-backend-table-header"><?php echo translate('status'); ?></th>
256  <th class="sq-backend-table-header"><?php echo translate('cms_reports_asset_statuses_last_status_change'); ?></th>
257  <th class="sq-backend-table-header"><?php echo translate('cms_reports_asset_statuses_last_status_changer'); ?></th>
258  </tr>
259  <?php
260 
261  foreach ($root->asset as $asset_node) {
262  $assetid = (int) $asset_node->attributes()->assetid;
263  $name = (string) $asset_node->attributes()->name;
264  $type_code = (string) $asset_node->attributes()->type_code;
265  $type_icon = $am->getAssetIconURL($type_code);
266 
267  $status = (int) $asset_node->status->attributes()->value;
268  $status_changed = (string) $asset_node->status->attributes()->changed;
269  $status_changed_userid = (int) $asset_node->status->attributes()->changed_userid;
270  $status_changed_user_name = (string) $asset_node->status->attributes()->changed_user_name;
271 
272  ?>
273  <tr>
274  <td class="sq-backend-table-cell">
275  <script type="text/javascript">sq_print_icon("<?php echo $type_icon; ?>", "16", "16", "");</script>
276  </td>
277  <td class="sq-backend-table-cell">
278  <?php echo htmlspecialchars($assetid); ?>
279  </td>
280  <td class="sq-backend-table-cell">
281  <?php echo htmlspecialchars($name); ?>
282  </td>
283  <td class="sq-backend-table-cell">
284  <?php echo get_asset_status_icon($status); ?><?php echo get_status_description($status); ?>
285  </td>
286  <td class="sq-backend-table-cell">
287  <?php echo $status_changed; ?>
288  </td>
289  <td class="sq-backend-table-cell">
290  <?php echo htmlspecialchars($status_changed_user_name); ?> [<?php echo htmlspecialchars($status_changed_userid); ?>]
291  </td>
292  </tr>
293  <?php
294  }//end foreach
295  ?>
296  </table>
297  <?php
298 
299  }//end paintReport()
300 
301 
312  function paintSearchStatuses(&$asset, &$o, $prefix)
313  {
314  $write_access = $asset->writeAccess('attributes');
315  $current_statuses = explode('|',$asset->attr('search_status'));
316 
317  if ($write_access) {
318  $statuses = get_constant_values('SQ_STATUS');
319  $options = Array();
320  foreach ($statuses as $constant => $value) {
321  $options[$value] = get_status_description($value);
322  }
323  combo_box($prefix.'_statuses', $options, TRUE, $current_statuses);
324  } else {
325  if (empty($current_statuses)) {
326  echo translate('cms_listing_no_status_selected');
327  } else {
328  ?><ul style="margin: 1px 15px;"><?php
329  foreach ($current_statuses as $status) {
330  echo '<li>';
331  echo get_asset_status_icon($status);
332  echo get_status_description($status);
333  echo '</li>';
334  }
335  ?></ul><?php
336  }
337  }
338 
339  }//end paintSearchStatuses()
340 
341 
352  function processSearchStatuses(&$asset, &$o, $prefix)
353  {
354  $new_statuses = Array();
355  if (isset($_REQUEST[$prefix.'_statuses'])) {
356  $new_statuses = implode('|', $_REQUEST[$prefix.'_statuses']);
357  $asset->setAttrValue('search_status', $new_statuses);
358  }
359 
360  }//end processSearchStatuses()
361 
362 
363 }//end class
364 
365 ?>