Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_condition_metadata_value.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_condition/trigger_condition.inc';
18 
32 {
33 
34 
53  public static function evaluate($settings, &$state)
54  {
55  if (empty($settings['asset_id'])) return FALSE;
56  if (!isset($settings['value']) || ($settings['value'] == '')) return FALSE;
57  if (!isset($state['asset']) || is_null($state['asset'])) $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getasset($state['assetid']);
58 
59  $compared_value = $settings['value'];
60  replace_global_keywords($compared_value);
61 
62  $unfiltered_metadata_value = $GLOBALS['SQ_SYSTEM']->getMetadataManager()->getMetadataValueByAssetid($state['assetid'], $settings['asset_id'], TRUE);
63  $keywords = extract_keywords($unfiltered_metadata_value);
64 
65  $replacement = Array();
66  foreach ($keywords as $keyword) {
67  $replacement[$keyword] = $state['asset']->getKeywordReplacement($keyword);
68  }
69 
70  $metadata_value = replace_keywords( $unfiltered_metadata_value, $replacement);
71 
72  if (!is_null($metadata_value)) {
73  switch (array_get_index($settings, 'match', 'exact')) {
74  case 'exact':
75  return ($compared_value == $metadata_value);
76  case 'partial':
77  if (empty($compared_value)) {
78  return ($compared_value == $metadata_value);
79  }
80  return (strpos($metadata_value, $compared_value) !== FALSE);
81  case 'regex':
82  return (preg_match('/'.str_replace('/', '\/', $compared_value).'/', $metadata_value) > 0);
83  }
84  }
85 
86  return FALSE;
87 
88  }//end evaluate()
89 
90 
101  public static function getInterface($settings, $prefix, $write_access=FALSE)
102  {
103  $selected_asset_id = array_get_index($settings, 'asset_id', 0);
104  $am = $GLOBALS['SQ_SYSTEM']->am;
105  if (!empty($selected_asset_id)) {
106  $selected_asset = $am->getAsset($selected_asset_id);
107  } else {
108  $selected_asset = '';
109  }
110 
111  $type_of_search = Array(
112  'exact' => translate('exact_match'),
113  'partial' => translate('partial_match'),
114  'regex' => translate('regex_match'),
115  );
116 
117  // Check if not set
118  if (!array_key_exists('match', $settings)) {
119  $settings['match'] = 'exact';
120  }
121 
122  // Check if not set
123  if (!array_key_exists('value', $settings)) {
124  $settings['value'] = '';
125  }
126 
127  // asset finder - to pick the metadata field
128  ob_start();
129  if ($write_access) {
130  $restricted_types['metadata_field_text'] = 'I';
131  $restricted_types['metadata_field_thesaurus'] = 'I';
132  $restricted_types['metadata_field_select'] = 'I';
133  asset_finder($prefix.'[asset_id]', $selected_asset_id, $restricted_types);
134  } else {
135  if (!empty($selected_asset_id)) {
136  echo get_asset_tag_line($selected_asset_id);
137  } else {
138  echo 'None selected';
139  }
140  }
141  $asset_id_component = ob_get_contents();
142  ob_end_clean();
143 
144  // match field
145  ob_start();
146  if ($write_access) {
147  combo_box($prefix.'[match]', $type_of_search, FALSE, $settings['match']);
148  } else {
149  echo translate($settings['match'].'_match');
150  }
151  $match_component = ob_get_contents();
152  ob_end_clean();
153 
154  // value field
155  ob_start();
156  if ($write_access) {
157  text_box($prefix.'[value]', array_get_index($settings, 'value'));
158  } else {
159  echo $settings['value'];
160  }
161  $value_component = ob_get_contents();
162  ob_end_clean();
163 
164  return translate('trigger_metadata_value', $asset_id_component, $match_component, $value_component);
165 
166  }//end getInterface()
167 
168 
179  public static function processInterface(&$settings, $request_data)
180  {
181  if (empty($request_data)) {
182  return translate('trigger_input_data_error');
183  }
184 
185  $asset_id = array_get_index($request_data, 'asset_id', FALSE);
186  if (!$asset_id) {
187  return translate('trigger_asset_not_supplied');
188  }
189 
190  if (!empty($asset_id['assetid'])) {
191  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id['assetid']);
192 
193  $settings['asset_id'] = $asset_id['assetid'];
194  $settings['match'] = array_get_index($request_data, 'match', FALSE);
195  $settings['value'] = array_get_index($request_data, 'value', FALSE);
196  }
197 
198  return FALSE;
199 
200  }//end processInterface()
201 
202 
210  public static function allowMultiple()
211  {
212  // default to to disallow
213  return TRUE;
214 
215  }//end allowMultiple()
216 
217 
218 }//end class
219 
220 ?>