Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
condition_logical_edit_fns.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/conditions/condition/condition_edit_fns.inc';
18 
32 {
33 
34 
40  function __construct()
41  {
42  parent::__construct();
43 
44  }//end constructor
45 
46 
59  public static function paintEditInterface(Array $condition_data, Backend_Outputter $o, $prefix, $write_access, Array $allowed_keywords)
60  {
61  if (empty($condition_data)) {
62  $condition_data = Array('logical_keywords' => Array('logical_keyword_1' => ''), 'operator' => 'ALL');
63  }
64 
65  if (empty($allowed_keywords)) {
66  // we don't know the type of the asset here so we add the keywords of the generic Asset object
67  $dummy_asset = new Asset();
68  $allowed_keywords = array_keys($dummy_asset->getAvailableKeywords());
69  }
70  $allowed_keywords = array_merge($allowed_keywords, array_keys(get_available_global_keywords()));
71 
72  ?>
73  <table class="no-borders">
74  <?php
75  $selected_keywords = array();
76  if (isset($condition_data['logical_keywords'])) {
77  $selected_keywords = array_keys($condition_data['logical_keywords']);
78  }
79  if ($write_access) {
80  $selected_keywords['-- none --'] = '-- '.strtolower(translate('none')).' --';
81  }
82 
83  $keyword_options = Array();
84  foreach ($allowed_keywords as $keyword) {
85  $keyword_options[$keyword] = $keyword;
86  }
87 
88  $i = 0;
89 
90  if (!empty($allowed_keywords)) {
91  foreach ($selected_keywords as $keyword) {
92  $special_option = Array();
93  if ($i == (count($selected_keywords) - 1)) {
94  $special_option = Array('none' => '-- '.strtolower(translate('none')).' --');
95  } else if ($i > 0) {
96  $special_option = Array('none' => '-- '.strtolower(translate('remove')).' --');
97  }
98 
99  $keyword_info = self::_getKeywordInfo($keyword, $allowed_keywords);
100  ?>
101  <tr>
102  <td><strong><?php echo translate('logical_keyword').' '.$i; ?></strong>&nbsp;</td>
103  <td>
104  <?php
105  if ($write_access) {
106  combo_box($prefix.'[keyword'.$i.']', array_merge($special_option, $keyword_options), FALSE, $keyword_info['constant_part']);
107  // This is a special case for generic/variable keywords (like asset_metadata_)
108  // which name must be completed.
109  // For those keywords, we show an additional box to type the rest of the name
110  if ($keyword_info['is_variable']) {
111  text_box($prefix.'[name_extension_'.$i.']', $keyword_info['variable_part'], 30);
112  }
113  } else {
114  echo ($keyword_info['constant_part']).array_get_index($keyword_info, 'variable_part', '');
115  }
116  ?>
117  </td>
118  </tr>
119  <?php
120  $i++;
121  }
122  } else {
123  echo(translate('core_keyword_list_empty'));
124  }
125  ?>
126  <tr>
127  <td><strong><?php echo translate('operator'); ?></strong></td>
128  <td>
129  <?php
130  $operator = '';
131  if(isset($condition_data['operator'])) {
132  $operator = $condition_data['operator'];
133  }
134  if ($write_access) {
135  combo_box($prefix.'[operator]', Array('ALL' => 'AND', 'ANY' => 'OR'), FALSE, $operator);
136  } else {
137  echo $operator;
138  }
139  ?>
140  </td>
141  </tr>
142  </table>
143  Keywords are considered TRUE if their value is non-empty and nonzero. Keywords are considered FALSE if their value is empty or zero.
144  <?php
145 
146  }//end paintEditInterface()
147 
148 
159  public static function processEditInterface(Backend_Outputter $o, $prefix, Array $allowed_keywords = Array())
160  {
161  if (empty($allowed_keywords)) {
162  // we don't know the type of the asset here so we add the keywords of the generic Asset object
163  $dummy_asset = new Asset();
164  $allowed_keywords = array_keys($dummy_asset->getAvailableKeywords());
165  }
166  $allowed_keywords = array_merge($allowed_keywords, array_keys(get_available_global_keywords()));
167 
168  $logical_keywords = Array();
169 
170  for ($i = 0; $i < 100; $i++) {
171  if (isset($_POST[$prefix]['keyword'.$i])) {
172  $keyword_name = $_POST[$prefix]['keyword'.$i];
173  if ($keyword_name != 'none') {
174  $keyword_info = self::_getKeywordInfo($keyword_name, $allowed_keywords);
175  if ($keyword_info['is_variable']) {
176  if (!empty($_POST[$prefix]['name_extension_'.$i])) {
177  $name_extension = trim($_POST[$prefix]['name_extension_'.$i]);
178  if (!empty($name_extension)) {
179  $keyword_name = $keyword_info['constant_part'].$name_extension;
180  }
181  }
182  }
183  $logical_keywords[$keyword_name] = '';
184  }
185  }
186  }
187 
188  if (empty($logical_keywords)) {
189  $logical_keywords = Array('logical_keyword_1' => '');
190  }
191 
192  $results = Array(
193  'logical_keywords' => $logical_keywords,
194  'operator' => $_POST[$prefix]['operator'],
195  );
196 
197  return $results;
198 
199  }//end processEditInterface()
200 
201 
223  protected static function _getKeywordInfo($keyword_name, Array &$allowed_keywords)
224  {
225  $ret = Array('is_variable' => FALSE, 'constant_part' => $keyword_name, 'variable_part' => '');
226 
227  $keyword_parts = Array();
228  foreach ($allowed_keywords as $allowed_keyword) {
229  if (preg_match('/_$/', $allowed_keyword)) {
230  // $allowed_keyword is a variable keyword
231  if (preg_match('/^'.$allowed_keyword.'(.*)$/', $keyword_name, $keyword_parts)) {
232  // $keyword_name is a variable keyword
233  $ret = Array('is_variable' => TRUE, 'constant_part' => $allowed_keyword, 'variable_part' => $keyword_parts[1]);
234  }
235  }
236  }
237 
238  return $ret;
239 
240  }//end _getKeywordInfo()
241 
242 }//end class
243 
244 ?>