Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
soap_form_based_common.inc
1 <?php
31 {
32 
39  public static function getKeywordReplacementsFromForm($content, &$form)
40  {
41  $matches = Array();
42  $found = preg_match_all('/%(response_(\d+_)?q\d+[^%]*)%/U', $content, $set_matches, PREG_SET_ORDER);
43  $matches = array_merge($matches, $set_matches);
44  $found = preg_match_all('/%(question_name_(\d+_)?q\d+[^%]*)%/U', $content, $set_matches, PREG_SET_ORDER);
45  $matches = array_merge($matches, $set_matches);
46  $found = preg_match_all('/%(form_submission_[^%]*)%/U', $content, $set_matches, PREG_SET_ORDER);
47  $matches = array_merge($matches, $set_matches);
48 
49  foreach ($matches as $match) {
50  if (empty($match)) continue;
51  $count = 0;
52  do {
53  // Apply keyword modifiers, if any
54  $full_keyword = $match[1];
55  $keyword = parse_keyword($full_keyword, $modifiers);
56 
57  $replacement = $form->_getThankYouKeywordReplacement($keyword);
58  if ($replacement == '%'.$keyword.'%') {
59  break;
60  }
61 
62  if (!empty($modifiers)) {
63  $replace_keywords = Array(
64  'assetid' => $form->id,
65  'call_fns' => Array ('_getThankYouKeywordReplacement', 'getKeywordReplacement'),
66  );
67  apply_keyword_modifiers($replacement, $modifiers, $replace_keywords);
68  }
69  $content = preg_replace('/%'.str_replace('^', '\^', $match[1]).'%/U', str_replace('$', '\$', $replacement), $content, 1, $count);
70 
71  } while ($count > 0);
72  }//end foreach
73 
74  // Carryout global keywords replacement
75  replace_global_keywords($content);
76 
77  return $content;
78 
79  }//end getKeywordReplacementsFromForm()
80 
81 
88  public static function getFileContentReplacementsFromForm($content, &$form)
89  {
90  preg_match_all('|%file_upload_([0-9]*_)?q([0-9]+)[^%]*%|U', $content, $matches);
91 
92  foreach($matches[1] as $key => $section_match) {
93  $question_match = $matches[2][$key];
94  if (empty($section_match)) {
95  // One asset ID
96  $section_assetid = $form->id;
97  $question_assetid = $question_match;
98  } else {
99  $section_assetid = str_replace('_', '', $section_match);
100  $question_assetid = $question_match;
101  }
102  $question_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($section_assetid.':q'.$question_assetid);
103  if(empty($question_asset) || get_class($question_asset) != 'Form_Question_Type_File_Upload') return $content;
104  // is the file an asset or just raw file in data dir?
105 
106  $replacement = '';
107  if(isset($question_asset->extra_data['filesystem_path'])) {
108  $replacement = file_get_contents($question_asset->extra_data['filesystem_path']);
109  }
110  else {
111  $uploaded_assetid = array_get_index($question_asset->extra_data, 'existing_file_assetid', array_get_index($question_asset->extra_data, 'new_file_assetid', NULL));
112  if (!empty($uploaded_assetid)) {
113  $file = $GLOBALS['SQ_SYSTEM']->am->getAsset($uploaded_assetid);
114  $info = $file->getExistingFile();
115  if(isset($info['path']))
116  $replacement = file_get_contents($info['path']);
117  }
118  }
119 
120  // Apply keyword modifiers, if any
121  $full_keyword = trim($matches[0][$key], '%');
122  $keyword = parse_keyword($full_keyword, $modifiers);
123  if (!empty($modifiers)) {
124  apply_keyword_modifiers($replacement, $modifiers);
125 
126  $keyword = $full_keyword;
127  }
128 
129  if (empty($section_match)) {
130  $content = str_replace('%'.$keyword.'%', $replacement, $content);
131  } else {
132  $content = str_replace('%'.$keyword.'%', $replacement, $content);
133  }
134  }//end foreach
135 
136  return $content;
137 
138  }//end getFileContentReplacementsFromForm()
139 
140 
148  public static function validationRules()
149  {
150  return Array(
151  'must_contain' => 'Must contain',
152  'must_contain_casesensitive' => 'Must contain (Case sensitive)',
153  'must_not_contain' => 'Must NOT contain',
154  'must_not_contain_casesensitive' => 'Must NOT contain (Case sensitive)',
155  'equals' => 'Must be equal to',
156  'equals_not' => 'Must NOT be equal to',
157  'less_than' => 'Must be less than',
158  'less_than_or_equal' => 'Must be less than or equal to',
159  'more_than' => 'Must be more than',
160  'more_than_or_equal' => 'Must be more than or equal to',
161  );
162 
163  }// end validationRules()
164 
165 
175  public static function validateValue($value, $rule)
176  {
177  $valid = FALSE;
178  $rule_type = array_get_index($rule, 'type', '');
179  $rule_value = array_get_index($rule, 'value', '');
180 
181  switch ($rule_type) {
182 
183  case 'must_contain':
184  $valid = stripos($value, $rule_value) !== FALSE;
185  break;
186 
187  case 'must_not_contain':
188  $valid = stripos($value, $rule_value) === FALSE;
189  break;
190 
191  case 'must_contain_casesensitive':
192  $valid = strpos($value, $rule_value) !== FALSE;
193  break;
194 
195  case 'must_not_contain_casesensitive':
196  $valid = strpos($value, $rule_value) === FALSE;
197  break;
198 
199  case 'equals':
200  $valid = $value == $rule_value;
201  break;
202 
203  case 'equals_not':
204  $valid = !($value == $rule_value);
205  break;
206 
207  case 'less_than':
208  $valid = $value < $rule_value;
209  break;
210 
211  case 'more_than':
212  $valid = $value > $rule_value;
213  break;
214 
215  case 'less_than_or_equal':
216  $valid = $value <= $rule_value;
217  break;
218 
219  case 'more_than_or_equal':
220  $valid = $value >= $rule_value;
221  break;
222  }//end switch
223 
224  return $valid;
225 
226  }// end validateValue()
227 
228 
236  public static function toArray($data, &$form)
237  {
238  if (is_object($data)) $data = get_object_vars($data);
239 
240  // Don't play with empty object or array
241  if (empty($data) && (is_array($data) || is_object($data))) return '';
242 
243  if(is_array($data)) {
244  $result = Array();
245  foreach ($data as $index => $element) {
246  $result[$index] = self::toArray($element, $form);
247  }
248  return $result;
249  }else {
250  // Also replace file content keyword with actual binary file
251  return (self::getFileContentReplacementsFromForm($data, $form));
252  }
253 
254  }//end toArray
255 
256 
257 }//end class
258 
259 ?>