Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
condition_keyword_regexp_edit_fns.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/conditions/condition/condition_edit_fns.inc';
18 require_once SQ_ATTRIBUTES_PATH.'/selection/selection.inc';
19 
33 {
34 
35 
41  function __construct()
42  {
43  parent::__construct();
44 
45  }//end constructor
46 
47 
60  public static function paintEditInterface(Array $condition_data, Backend_Outputter $o, $prefix, $write_access, Array $allowed_keywords)
61  {
62  if (empty($condition_data)) {
63  $condition_data = Array('keyword' => '', 'operator' => 'ALL');
64  }
65  ?>
66  <table style="width: auto" class="no-borders">
67  <tr>
68  <td><strong><?php echo translate('keyword'); ?></strong> </td>
69  <td>
70  <?php
71  $selected_keyword = '';
72  if (isset($condition_data['keyword'])) {
73  $selected_keyword = $condition_data['keyword'];
74  }
75 
76  if (empty($allowed_keywords)) {
77  // we don't know the type of the asset here so we add the keywords of the generic Asset object
78  $dummy_asset = new Asset();
79  $allowed_keywords = array_keys($dummy_asset->getAvailableKeywords());
80  }
81 
82  $keyword_options = Array();
83  $allowed_keywords = array_merge($allowed_keywords, array_keys(get_available_global_keywords()));
84  foreach ($allowed_keywords as $keyword) {
85  $keyword_options[$keyword] = $keyword;
86  }
87 
88  $i = 0;
89  if (!empty($allowed_keywords)) {
90 
91  $keyword_info = self::_getKeywordInfo($selected_keyword, $allowed_keywords);
92  if ($write_access) {
93  combo_box($prefix.'[keyword]', array_merge($keyword_options), FALSE, $keyword_info['constant_part']);
94  // This is a special case for generic/variable keywords (like asset_metadata_)
95  // which name must be completed.
96  // For those keywords, we show an additional box to type the rest of the name
97  if ($keyword_info['is_variable']) {
98  text_box($prefix.'[keyword_extension]', $keyword_info['variable_part'], 30);
99  }
100  } else {
101  echo ($keyword_info['constant_part']).array_get_index($keyword_info, 'variable_part', '');
102  }
103  } else {
104  echo(translate('core_keyword_list_empty'));
105  }
106  ?>
107  </td>
108  </tr>
109  <tr>
110  <td><strong><?php echo translate('pattern'); ?></strong> </td>
111  <td>
112  <?php
113  if ($write_access) {
114  text_box($prefix.'[keyword_match]', array_get_index($condition_data, 'keyword_match'), 30);
115  } else {
116  echo array_get_index($condition_data, 'keyword_match', '');
117  }
118  ?>
119  </td>
120  </tr>
121  <tr>
122  <td colspan="2">
123  <?php
124  check_box($prefix.'[replace_keyword_in_match]', '1', array_get_index($condition_data, 'replace_keyword_in_match', '0'), '', ($write_access ? '' : 'disabled="disabled"'));
125  label('Replace keywords in pattern', $prefix.'[replace_keyword_in_match]');
126  ?>
127  </td>
128  </tr>
129  </table>
130  <?php // echo translate('condition_server_variable_is_regular_expression_enabled'); ?>
131  <?php
132 
133  }//end paintEditInterface()
134 
135 
145  public static function processEditInterface(Backend_Outputter $o, $prefix)
146  {
147  $results = Array(
148  'keyword' => $_POST[$prefix]['keyword'],
149  'keyword_match' => $_POST[$prefix]['keyword_match'],
150  'replace_keyword_in_match' => array_get_index($_POST[$prefix], 'replace_keyword_in_match', '0'),
151  );
152 
153  if (!empty($_POST[$prefix]['keyword_extension'])) {
154  $name_extension = trim($_POST[$prefix]['keyword_extension']);
155  if (!empty($name_extension)) {
156  $results['keyword'] .= $name_extension;
157  }
158  }
159 
160  return $results;
161 
162  }//end processEditInterface()
163 
164 
186  protected static function _getKeywordInfo($keyword_name, Array &$allowed_keywords)
187  {
188  $ret = Array('is_variable' => FALSE, 'constant_part' => $keyword_name, 'variable_part' => '');
189 
190  $keyword_parts = Array();
191  foreach ($allowed_keywords as $allowed_keyword) {
192  if (preg_match('/_$/', $allowed_keyword)) {
193  // $allowed_keyword is a variable keyword
194  if (preg_match('/^'.$allowed_keyword.'(.*)$/', $keyword_name, $keyword_parts)) {
195  // $keyword_name is a variable keyword
196  $ret = Array('is_variable' => TRUE, 'constant_part' => $allowed_keyword, 'variable_part' => $keyword_parts[1]);
197  }
198  }
199  }
200 
201  return $ret;
202 
203  }//end _getKeywordInfo()
204 
205 }//end class
206 
207 ?>