Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
data_source_edit_fns.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
19 
32 {
33 
34 
39  function __construct()
40  {
41  parent::__construct();
42 
43  }//end constructor
44 
45 
56  public function paintShadow(Data_Source $asset, Backend_Outputter $o, $prefix)
57  {
58  $name = $asset->attr('shadow_name');
59  $wa = $asset->writeAccess('attributes');
60  if ($wa) {
61  text_box('shadow_name', !empty($name) ? $name : '', 80);
62  } else {
63  if (!empty($name)) echo $name;
64  }
65 
66  return $wa;
67 
68  }//end paintShadow()
69 
70 
81  public function processShadow(Data_Source $asset, Backend_Outputter $o, $prefix)
82  {
83  if (!$asset->writeAccess('attributes')) return FALSE;
84 
85  $new_name = NULL;
86  $name = $asset->attr('shadow_name');
87  if (!empty($_POST['shadow_name'])) {
88  $new_name = $_POST['shadow_name'];
89  }
90  if ($name != $new_name) {
91  $res = $asset->setAttrValue('shadow_name', $new_name);
92  return TRUE;
93  } else {
94  return FALSE;
95  }
96 
97  }//end processShadow()
98 
99 
110  public function paintKeywords(Data_Source $asset, Backend_Outputter $o, $prefix)
111  {
112  $shadow_asset = $asset->getAsset($asset->id.':0');
113  if (is_null($shadow_asset)) {
114  echo '<strong>'.translate('data_source_no_keywords').'</strong>';
115  return;
116  }
117  $keywords = $shadow_asset->getAvailableKeywords();
118  $write_access = $asset->writeAccess('attributes');
119  ?>
120  <table class="sq-backend-table">
121  <tr>
122  <th><?php echo translate('keyword');?></th>
123  <th><?php echo translate('description');?></th>
124  </tr>
125  <?php
126  foreach ($keywords as $key => $desc) {
127  // all the keywords other than the shadow asset name are to be displayed for the user to choose the
128  // keywords or a combination of keywords and strings to set the shadow asset name
129  if ($key == 'data_source_record_set_shadow_asset_name' || $key == 'ds__shadow_asset_name') {
130  unset($keywords[$key]);
131  continue;
132  } else {
133  if (strpos($key, 'data_source_record_set') === FALSE && strpos($key, 'ds__') === FALSE) {
134  unset($keywords[$key]);
135  continue;
136  }
137  }
138  ?>
139  <tr>
140  <td><?php echo '%'.$key.'%';?> </td>
141  <td><?php echo $desc; ?> </td>
142  </tr>
143  <?php
144  }
145  ?>
146  </table>
147  <?php
148 
149  return $write_access;
150 
151  }//end paintKeywords()
152 
153 
154 
155 
156 
167  public function processKeywords(Data_Source $asset, Backend_Outputter $o, $prefix)
168  {
169  return FALSE;
170 
171  }//end processKeywords()
172 
173 
184  public function paintRecordFilter(&$asset, &$o, $prefix)
185  {
186  $filters = $asset->attr('record_filters');
187  $wa = $asset->writeAccess('attributes');
188  ?>
189  <table class ="sq-backend-table" border="1" style="margin-bottom: 7px">
190  <tr>
191  <th width="20%"><?php echo translate('data_source_filter_name'); ?></th>
192  <th width="45%"><?php echo translate('data_source_value_to_match'); ?></th>
193  <th><?php echo translate('data_source_match_type'); ?></th>
194  <th><?php echo translate('data_source_match_case'); ?></th>
195  <?php
196  if ($wa) {
197  ?>
198  <th><?php echo translate('delete'); ?></th>
199  <?php
200  }
201  ?>
202  </tr>
203  <?php
204 
205  $match_options = Array(
206  'exact' => 'String match - Exact',
207  'contains' => 'String match - Contains',
208  'regex' => 'Regex match',
209  );
210  $case_options = Array(
211  'ignore_case' => 'No',
212  'match_case' => 'Yes',
213  );
214 
215  foreach ($filters as $index => $field_data) {
216  $field_name = $field_data['name'];
217  $field_value = $field_data['value'];
218  $match_type = $field_data['match_type'];
219  $match_case = $field_data['match_case'];
220 
221  ?>
222  <tr>
223  <td>
224  <?php
225  if ($wa) {
226  text_box($prefix.'_filter['.$index.'][name]', $field_name, 30);
227  } else {
228  echo $field_name;
229  }
230  ?>
231  </td>
232  <td>
233  <?php
234  if ($wa) {
235  text_box($prefix.'_filter['.$index.'][value]', $field_value, 80);
236  } else {
237  echo $field_value;
238  }
239  ?>
240  </td>
241  <td>
242  <?php
243  if ($wa) {
244  combo_box($prefix.'_filter['.$index.'][match_type]', $match_options, FALSE, $match_type);
245  } else {
246  echo $match_options[$match_type];
247  }
248  ?>
249  </td>
250  <td>
251  <?php
252  if ($wa) {
253  combo_box($prefix.'_filter['.$index.'][match_case]', $case_options, FALSE, $match_case);
254  } else {
255  echo $case_options[$match_case];
256  }
257  ?>
258  </td>
259  <?php
260  if ($wa) {
261  echo '<td>';
262  check_box($prefix.'_filter['.$index.'][delete]');
263  echo '</td>';
264  }
265  ?>
266 
267 
268  </tr>
269  <?php
270  }
271  ?>
272  </table>
273  <?php
274  if ($wa) {
275  label(translate('data_source_new_filter'), $prefix.'_new_filter_name');
276  echo '&nbsp;';
277  text_box($prefix.'_new_filter_name', '', 25);
278  }
279 
280  $o->sectionNote(translate('data_source_dynamic_keywords_node'));
281 
282  return $wa;
283 
284  }//end paintRecordFilter()
285 
286 
297  public function processRecordFilter(&$asset, &$o, $prefix)
298  {
299  if (!$asset->writeAccess('attributes')) return FALSE;
300 
301  $updated_filters = Array();
302  if (isset($_POST[$prefix.'_filter'])) {
303  foreach($_POST[$prefix.'_filter'] as $index => $filter_data) {
304  if (isset($filter_data['delete'])) {
305  continue;
306  }
307  $filter_name = trim($filter_data['name']);
308  $filter_value = $filter_data['value'];
309  $match_type = $filter_data['match_type'];
310  $match_case = $filter_data['match_case'];
311 
312  // Validate the regex string
313  if ($match_type == 'regex' && !empty($filter_value)) {
314  $valid_regex = @preg_match('/'.str_replace('/', '\/',$filter_value).'/', 'test');
315  if ($valid_regex === FALSE) {
316  trigger_localised_error('DATA0007', E_USER_WARNING, $filter_value);
317  $filter_value = '';
318  }
319  }
320 
321  $updated_filters[] = Array(
322  'name' => $filter_name,
323  'value' => $filter_value,
324  'match_type' => $match_type,
325  'match_case' => $match_case,
326  );
327  }//end foreach
328  }//end if
329 
330  if (!empty($_POST[$prefix.'_new_filter_name'])) {
331  $updated_filters[] = Array(
332  'name' => trim($_POST[$prefix.'_new_filter_name']),
333  'value' => '',
334  'match_type' => 'contains',
335  'match_case' => 'ignore_case',
336  );
337  }
338 
339  return $asset->setAttrValue('record_filters', $updated_filters);
340 
341  }//end processRecordFilter()
342 
343 
353  public function resultSetExists(&$asset, $prefix)
354  {
355  $result = $asset->getResultSet();
356  return !empty($result);
357 
358  }//end resultSetExists()
359 
360 
371  function paintDynamicData(&$asset, &$o, $prefix)
372  {
373  $write_access = $asset->writeAccess('attributes');
374 
375  $var_list = $asset->attr('dynamic_vars');
376 
377  $o->openRaw();
378 
379  echo '<table class="sq-backend-table">';
380  echo '<tr>';
381  echo '<th>Name</th>';
382  echo '<th>Default Value</th>';
383  if ($write_access) {
384  echo '<th width="10%" align="right" style="border-left: dotted 1px grey;">Delete</th>';
385  }
386  echo '</tr>';
387  foreach ($var_list as $var => $value) {
388  $var_id = base64_encode($var);
389 
390  echo '<tr>';
391  echo '<td>';
392  if ($write_access) {
393  text_box($prefix.'_var_name['.$var_id.']', $var);
394  } else {
395  echo $var;
396  }
397  echo '</td>';
398  echo '<td valign="top" align="left">';
399  $value = urldecode($value);
400  if ($write_access) {
401  text_box($prefix.'_var_value['.$var_id.']', urlencode($value));
402  } else {
403  echo urlencode($value);
404  }
405  echo '</td>';
406  if ($write_access) {
407  echo '<td style="border-left: dotted 1px grey;">';
408  check_box($prefix.'_delete_var['.$var_id.']');
409  echo '</td>';
410  }
411  echo '</tr>';
412  }
413 
414  if ($write_access) {
415  echo '<tr class="new-data-row">';
416  echo '<th colspan="3" align="left">';
417  echo '<em style="color:#666">Add New Variables Below</em>';
418  echo '</th>';
419  echo '</tr>';
420 
421  echo '<tr class="new-data-row">';
422  echo '<td>';
423  text_box($prefix.'_new_dynamic_data_name[]', NULL);
424  echo '</td>';
425  echo '<td>';
426  text_box($prefix.'_new_dynamic_data_value[]', NULL);
427  echo '</td>';
428  echo '<td style="border-left: dotted 1px grey;">';
429  echo '</td>';
430  echo '</tr>';
431  }
432  echo '</table>';
433 
434  $o->closeRaw();
435 
436  $o->sectionNote(translate('data_source_variable_section_note'));
437 
438  $o->closeSection();
439 
440  $o->openSection('Data Mappings');
441  $o->openRaw();
442  $map = $asset->getAttribute('parameter_map');
443 
444  $parameter_names = $map->getParameters();
445 
446  foreach ($var_list as $var_name => $default) {
447  $map->setParameter($var_name, 'Variable: '.$var_name);
448  }
449 
450  $map->paint($prefix.'_parameter_map', !$write_access);
451  $o->closeRaw();
452 
453  $o->sectionNote(translate('data_source_mappings_section_note'));
454 
455  return $write_access;
456 
457  }//end paintDynamicData()
458 
459 
470  function processDynamicData(&$asset, &$o, $prefix)
471  {
472  $var_name_pattern = '/^[a-zA-Z_\-0-9\.]+$/';
473 
474  $write_access = $asset->writeAccess('attributes');
475  if (!$write_access) return FALSE;
476 
477  $to_delete = array_get_index($_REQUEST, $prefix.'_delete_var', Array());
478  $var_value = array_get_index($_REQUEST, $prefix.'_var_value', Array());
479  $var_name = array_get_index($_REQUEST, $prefix.'_var_name', Array());
480 
481  $var_list = $asset->attr('dynamic_vars');
482 
483  $map = $asset->getAttribute('parameter_map');
484 
485  $parameter_names = $map->getParameters();
486  foreach ($var_list as $name => $default) {
487  $map->setParameter($name, 'Variable: '.$name);
488  }
489 
490  $map->process($prefix.'_parameter_map');
491 
492  foreach ($var_value as $hash => $value) {
493  $name = base64_decode($hash);
494  if (isset($to_delete[$hash])) {
495  unset($var_list[$name]);
496  // also need to delete it from the PARAMETER MAP
497  if (in_array($name, $parameter_names)) {
498  $map->unsetParameter($name);
499  }
500 
501  continue;
502  }
503  $value = urldecode($value);
504  $var_list[$name] = urlencode($value);
505  }
506 
507  $new_var_list = Array();
508  $rename_success = FALSE;
509 
510  // now we rename variables
511  foreach ($var_name as $hash => $new_name) {
512  $new_name = trim($new_name);
513 
514  if (empty($new_name)) {
515  $rename_success = FALSE;
516  trigger_error('Variable name cannot be empty. No variables were renamed.', E_USER_NOTICE);
517  break;
518  }
519 
520  // check new name against regex
521  $new_name_is_valid = preg_match($var_name_pattern, $new_name);
522  if (!$new_name_is_valid) {
523  $rename_success = FALSE;
524  trigger_error('Variable name ['.htmlentities($new_name).'] contains characters that are not allowed. Can only use letters (A-Za-z), digits(0-9), underscore (_), dash (-) and dot (.)', E_USER_NOTICE);
525  break;
526  }
527 
528  $name = base64_decode($hash);
529 
530  // skip the deleted vars
531  if (!isset($var_list[$name])) continue;
532 
533  if (isset($new_var_list[$new_name])) {
534  $rename_success = FALSE;
535  trigger_error('Could not rename variables because variable name "'.htmlentities($new_name).'" already exists', E_USER_NOTICE);
536  break;
537  }
538 
539  if ($new_name != $name) {
540  if (in_array($name, $parameter_names)) {
541  $map->moveParameter($name, $new_name);
542  }
543  $rename_success = TRUE;
544  }
545 
546  $new_var_list[$new_name] = $var_list[$name];
547 
548  }//end foreach
549 
550 
551  if ($rename_success) $var_list = $new_var_list;
552 
553  $new_names = array_get_index($_REQUEST, $prefix.'_new_dynamic_data_name', Array());
554  if (!empty($new_names)) {
555 
556  foreach ($new_names as $seq_number => $new_var_name) {
557  $new_var_name = trim($new_var_name);
558 
559  $new_var_value = array_get_index($_REQUEST[$prefix.'_new_dynamic_data_value'], $seq_number, '');
560  if (empty($new_var_name)) {
561  if (!empty($new_var_value)) {
562  trigger_error('New variable name cannot be empty', E_USER_WARNING);
563  }
564  continue;
565  }
566 
567  // check new name against regex
568  $new_name_is_valid = preg_match($var_name_pattern, $new_var_name);
569  if (!$new_name_is_valid) {
570  trigger_error('Variable name "'.htmlentities($new_var_name).'" contains characters that are not allowed. Can only use letters (A-Za-z), digits(0-9), underscore (_), dash (-) and dot (.)', E_USER_NOTICE);
571  continue;
572  }
573 
574  if (isset($var_list[$new_var_name])) {
575  trigger_error('Variable Name "'.htmlentities($new_var_name).'" is already in use', E_USER_WARNING);
576  continue;
577  }
578  $new_var_value = urldecode($new_var_value);
579  $var_list[$new_var_name] = urlencode($new_var_value);
580  $map->setParameter($new_var_name, 'Variable: '.$new_var_name);
581  }
582  }
583 
584  $asset->setAttrValue('dynamic_vars', $var_list);
585  $asset->setAttrValue('parameter_map', unserialize($map->value));
586 
587  return TRUE;
588 
589  }//end processDynamicData()
590 
591 
592 }//end class
593 
594 ?>