Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_condition_keyword.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_condition_types/trigger_condition_asset_type/trigger_condition_asset_type.inc';
18 
31 {
32 
39  protected static function getMatchOptions()
40  {
41  return Array(
42  'exact' => translate('exact_match'),
43  'partial' => translate('partial_match'),
44  'regex' => translate('regex_match'),
45  );
46 
47  }//end getMatchOptions()
48 
49 
69  public static function evaluate($settings, &$state)
70  {
71  if (empty($settings['keyword'])) return FALSE;
72  if (empty($settings['match_type'])) return FALSE;
73 
74  if (empty($state['asset'])) {
75  if (empty($state['assetid'])) return FALSE;
76  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
77  }
78 
79  // try replacing the asset keyword
80  // if the keyword is a globals keyword, this function simply returns the globals keyword as it is
81  // note: we are currently allowing only one keyword to be specified here, so, we strip '%' from the
82  // specified keyword, in case multiple keywords are set here
83  $keyword = str_replace('%', '', $settings['keyword']);
84  $asset_keyword_replacement = $state['asset']->getKeywordReplacement($keyword);
85 
86  // now try replacing the globals keyword
87  // we need to make sure that the triggering asset is used to repalce global keywords,
88  // so, temporarily switch the 'current asset' to the triggering asset
89  $original_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
90  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $state['asset']);
91  replace_global_keywords($asset_keyword_replacement);
92 
93  // we are done with the temporary current asset, so, let's put it back to the original state
94  if (is_null($original_current_asset)) {
95  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
96  } else {
97  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $original_current_asset);
98  }
99  // now we have a replacement value - test the replacement value against the value specified to compare
100  switch ($settings['match_type']) {
101  case 'exact':
102  return ($asset_keyword_replacement === $settings['comparison_value']);
103  case 'partial':
104  return (strpos($asset_keyword_replacement, $settings['comparison_value']) !== FALSE);
105  case 'regex':
106  return (preg_match('/'.str_replace('/', '\/', $settings['comparison_value']).'/', $asset_keyword_replacement) > 0);
107  }
108 
109  // we shouldn't come this far but if we did, return FALSE so that the trigger action wouldn't run on this asset
110  return FALSE;
111 
112  }//end evaluate()
113 
114 
125  public static function getInterface($settings, $prefix, $write_access=FALSE)
126  {
127  // check if we already have values set so that we can pre-set the values where possible below
128  $keyword = isset($settings['keyword']) ? $settings['keyword'] : '';
129  $replacement_value = isset($settings['comparison_value']) ? $settings['comparison_value'] : '';
130  $operator = array_get_index($settings, 'match_type', 'exact');
131 
132  $am = $GLOBALS['SQ_SYSTEM']->am;
133 
134  // keyword input box
135  ob_start();
136  if ($write_access) {
137  text_box($prefix.'[keyword]', $keyword, '30px');
138  } else {
139  echo '<b>'.$keyword.'</b>';
140  }
141  $keyword_component = ob_get_contents();
142  ob_end_clean();
143 
144  // value to compare
145  ob_start();
146  if ($write_access) {
147  text_box($prefix.'[comparison_value]', $replacement_value , '50px');
148  } else {
149  echo '<b>'.$replacement_value.'</b>';
150  }
151 
152  $replacement_value_component = ob_get_contents();
153  ob_end_clean();
154 
155  // operator selector
156  ob_start();
157  $operator_options = Trigger_Condition_Keyword::getMatchOptions();
158 
159  if ($write_access) {
160  combo_box($prefix.'[match_type]', $operator_options, FALSE, $operator);
161  } else {
162  echo '<b>'.$operator_options[$operator].'</b>';
163  }
164  $operator_component = ob_get_contents();
165  ob_end_clean();
166 
167 
168  return translate('trigger_condition_keyword', $keyword_component, $operator_component, $replacement_value_component);
169 
170  }//end getInterface()
171 
172 
183  public static function processInterface(&$settings, $request_data)
184  {
185  if (empty($request_data)) {
186  return translate('trigger_input_data_missing');
187  }
188 
189  $settings['keyword'] = array_get_index($request_data, 'keyword', '');
190  $settings['comparison_value'] = array_get_index($request_data, 'comparison_value', '');
191  $settings['match_type'] = array_get_index($request_data, 'match_type', 'exact');
192 
193  return FALSE;
194 
195  }//end processInterface()
196 
197 
198 }//end class
199 
200 ?>