Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_action_save_xml.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/form/form_action/form_action.inc';
19 
34 {
35 
36 
41  function __construct()
42  {
43  parent::__construct();
44 
45  }//end constructor
46 
47 
53  public static function paintInlineInterface(Form $form, $settings, Backend_Outputter $o, $prefix)
54  {
55  if (!isset($settings['save_xml'])) {
56  $xml_settings = Array();
57  } else {
58  $xml_settings = $settings['save_xml'];
59  }
60 
61  // Paint something here!
62  ?>
63  <table class="no-borders" style="width:auto">
64  <tr>
65  <td><?php echo translate('location');?></td>
66  <td>
67  <?php
68  $location = array_get_index($xml_settings, 'location', '');
69  text_box($prefix.'_location', $location, 50);
70  if (empty($location) || !is_dir($location) || !is_writable($location)) {
71  ?><br />
72  <span class="sq-backend-warning"><?php echo translate('location_warning'); ?></span>
73  <?php
74  }//end if
75  ?>
76  </td>
77  </tr>
78  <tr>
79  <td><?php echo translate('xml_detail');?></td>
80  <td>
81  <?php
82  $detail = array_get_index($xml_settings, 'detail', 'simple');
83  $options = Array(
84  'simple' => 'Simple',
85  'complex' => 'Complex',
86  );
87  combo_box($prefix.'_detail', $options, FALSE, $detail);
88  ?>
89  </td>
90  </tr>
91  <tr>
92  <td><?php echo translate('xml_key');?></td>
93  <td>
94  <?php
95  $key = array_get_index($xml_settings, 'key', 'value');
96  $options = Array(
97  'key' => 'Keys',
98  'value' => 'Values',
99  );
100  combo_box($prefix.'_key', $options, FALSE, $key);
101  ?>
102  </td>
103  </tr>
104  </table>
105  <?php
106 
107  }//end paintInlineInterface()
108 
109 
115  public static function processInlineInterface(Form $form, &$settings, Backend_Outputter $o, $prefix)
116  {
117  if (!isset($settings['save_xml'])) {
118  $xml_settings = Array();
119  } else {
120  $xml_settings = $settings['save_xml'];
121  }
122 
123  $xml_settings['location'] = array_get_index($_POST, $prefix.'_location', '');
124  $xml_settings['detail'] = array_get_index($_POST, $prefix.'_detail', 'simple');
125  $xml_settings['key'] = array_get_index($_POST, $prefix.'_key', 'value');
126 
127  $settings['save_xml'] = $xml_settings;
128 
129  return TRUE;
130 
131  }//end processInlineInterface()
132 
133 
139  public static function paintSummary(Form $form, $settings, Backend_Outputter $o, $prefix)
140  {
141  if (!isset($settings['save_xml'])) {
142  $xml_settings = Array();
143  } else {
144  $xml_settings = $settings['save_xml'];
145  }
146  self::_fillDefaultValues($xml_settings);
147 
148  ?><table class="no-borders">
149  <colgroup>
150  <col width="80" />
151  <col/>
152  </colgroup>
153  <tbody>
154  <tr>
155  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Location:</strong></p></td>
156  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
157  if (!empty($xml_settings['location'])) {
158  echo ellipsisize($xml_settings['location'], 512);
159  } else {
160  ?><span class="sq-backend-warning">No Location specified.</span><?php
161  } ?></p></td>
162  </tr>
163  </tbody>
164  </table>
165  <?php
166 
167  }//end paintSummary()
168 
169 
174  public static function execute(Form $form, $settings)
175  {
176  $xml_settings = $settings['save_xml'];
177 
178  // Double check
179  $check = self::isValid($form, $settings);
180  if (!$check) return FALSE;
181 
182  // Some base variables
183  $location = $xml_settings['location'];
184  $detail = ($xml_settings['detail'] == 'complex') ? TRUE : FALSE;
185  $use_keys = ($xml_settings['key'] == 'key') ? TRUE : FALSE;
186  $questions = $form->getAllQuestions();
187  $submission = $form->getSubmissionAsset();
188  $complete = $submission->attr('complete');
189  $attributes = $submission->attr('attributes');
190  $answers = array_get_index($attributes, 'answers', Array());
191  $file = $location.'/submission-'.$submission->id.'-'.date('YmdHis', $submission->created).'.xml';
192 
193  // Start the submission XML
194  if ($complete && !empty($answers)) {
195  // Process the submission and save as XML
196  $sxml = new SimpleXMLElement('<submission></submission>');
197  $sxml->addAttribute('id', $submission->id);
198  $sxml->addAttribute('form', $form->id);
199  $sxml->addAttribute('time', date('YmdHis', $submission->created));
200  // If detail selected, use the extra attributes
201  if ($detail) {
202  if (isset($attributes['misc']['ip'])) {
203  $sxml->addAttribute('ip', $attributes['misc']['ip']);
204  }//end if
205  if (!empty($submission->updated_userid)) {
206  $sxml->addAttribute('user', $submission->created_userid);
207  }//end if
208  }//end if
209 
210  // Process the answers
211  foreach ($answers as $question_id => $answer) {
212  // Get the question details
213  if (isset($questions[$question_id])) {
214  $type = str_replace('form_question_type_', '', $questions[$question_id]['question_type_code']);
215  $question_name = $questions[$question_id]['attributes']['name'];
216  } else {
217  $type = '';
218  $question_name = '';
219  }//end if
220 
221  // Format the reponse
222  $value = $answer['answer'];
223 
224  // Do not check for an empty value here, as types like an option list use '0' has a valid value
225  if (!empty($type)) {
226  switch ($type) {
227  case 'tickbox_table':
228  $repsonses = Array();
229  // Clean up the table so values can be pulled out accurately
230  if (isset($questions[$question_id])) {
231  $ttable = @unserialize($questions[$question_id]['attributes']['question_contents']);
232  $waste = array_shift($ttable);
233  foreach ($ttable as $id => $options) {
234  unset($ttable[$id]);
235  $another_waste = array_shift($options);
236  $ttable[$id] = $options;
237  }//end foreach
238  }//end if
239  foreach ($value as $qid => $response) {
240  foreach ($response as $ra => $stuff) {
241  if (isset($ttable[$qid][$ra])) {
242  $responses[] = htmlspecialchars($ttable[$qid][$ra]);
243  } else {
244  $responses[] = $ra;
245  }//end if
246  }//end foreach
247  }//end foreach
248  $value = $responses;
249  break;
250  case 'select':
251  case 'tickbox_list':
252  case 'option_list':
253  if (is_array($value)) {
254  if (isset($questions[$question_id]['attributes']['options']) && !$use_keys) {
255  $list_options = @unserialize($questions[$question_id]['attributes']['options']);
256  foreach ($value as $id => $name) {
257  if (isset($list_options[$name])) {
258  unset($value[$id]);
259  $value[$id] = htmlspecialchars($list_options[$name]);
260  }//end if
261  }//end foreach
262  }//end if
263  } else {
264  if (isset($questions[$question_id]['attributes']['options']) && !$use_keys) {
265  $list_options = @unserialize($questions[$question_id]['attributes']['options']);
266  if (isset($list_options[$value])) {
267  $value = htmlspecialchars($list_options[$value]);
268  }//end if
269  }//end if
270  }//end if
271  break;
272  case 'country':
273  require_once SQ_FUDGE_PATH.'/standards_lists/countries.inc';
274  global $standards_lists_countries;
275  if (is_array($value)) {
276  foreach ($value as $id => $name) {
277  if (isset($standards_lists_countries[$name]) && !$use_keys) {
278  unset($value[$id]);
279  $value[$id] = htmlspecialchars($standards_lists_countries[$name]);
280  }//end if
281  }//end foreach
282  } else {
283  if (isset($standards_lists_countries[$value]) && !$use_keys) {
284  $value = htmlspecialchars($standards_lists_countries[$value]);
285  }//end if
286  }//end if
287  break;
288  case 'file_upload':
289  $filename = $value;
290  $value = '';
291  $assetid = 0;
292  $fileid = 0;
293  if (isset($answer['extra_data']['new_file_assetid']) && !empty($answer['extra_data']['new_file_assetid'])) {
294  $assetid = $answer['extra_data']['new_file_assetid'];
295  }//end if
296  if (isset($answer['extra_data']['existing_file_assetid']) && !empty($answer['extra_data']['existing_file_assetid'])) {
297  $assetid = $answer['extra_data']['existing_file_assetid'];
298  }//end if
299 
300  if (!empty($assetid)) {
301  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
302  if (!is_null($asset)) {
303  $file_details = $asset->getExistingFile();
304  $file_to_process = $file_details['path'];
305  $filename = $file_details['filename'];
306  if (file_exists($file_to_process)) {
307  $fhandle = fopen($file_to_process, 'r');
308  $file_content = fread($fhandle, filesize($file_to_process));
309  fclose($fhandle);
310  $value = chunk_split(base64_encode($file_content));
311  }//end if
312  }//end if
313 
314  $fileid = $assetid;
315  }//end if
316  break;
317  case 'datetime':
318  $timestamp = iso8601_ts($value);
319  if ($timestamp !== FALSE) {
320  $value = date('Ymd', $timestamp);
321  }//end if
322  break;
323  }//end switch
324  }//end if
325 
326  // Format the value ready for XML processing
327  if (is_array($value)) {
328  if (!empty($value)) {
329  $value = implode('|', $value);
330  } else {
331  $value = '';
332  }//end if
333  }//end if
334 
335  // Compile the XML
336  $response = $sxml->addChild('response', utf8_encode($value));
337  if (!empty($question_name)) {
338  $response->addAttribute('name', utf8_encode($question_name));
339  }//end if
340  if (!empty($type)) {
341  $response->addAttribute('type', $type);
342  if ($type == 'file_upload' && !empty($filename)) {
343  $response->addAttribute('filename', $filename);
344  if (isset($fileid) && !empty($fileid)) {
345  $response->addAttribute('assetid', $fileid);
346  }//end if
347  }//end if
348  }//end if
349  if ($detail) {
350  $response->addAttribute('id', $question_id);
351  }//end if
352  }//end foreach
353 
354  // Save the XML
355  $xml = $sxml->asXML();
356 
357  $result = file_put_contents($file, $xml);
358  } else {
359  return FALSE;
360  }//end if
361 
362  return $result;
363 
364  }//end execute()
365 
366 
371  public static function isValid(Form $form, $settings)
372  {
373  if (!isset($settings['save_xml'])) {
374  $xml_settings = Array();
375  } else {
376  $xml_settings = $settings['save_xml'];
377  }
378  self::_fillDefaultValues($xml_settings);
379 
380  // Check if valid and return FALSE
381  if (!isset($xml_settings['location']) || empty($xml_settings['location'])) {
382  return FALSE;
383  }//end if
384 
385  if (!is_dir($xml_settings['location']) || !is_writable($xml_settings['location'])) {
386  return FALSE;
387  }//end if
388 
389  return TRUE;
390 
391  }//end isValid()
392 
393 
401  private static function _fillDefaultValues(&$xml_format) {
402  if (!isset($xml_format['location'])) {
403  $xml_format['location'] = '';
404  }
405  if (!isset($xml_format['detail'])) {
406  $xml_format['detail'] = 'simple';
407  } else if (empty($xml_format['detail'])) {
408  $xml_format['detail'] = 'simple';
409  }//end if
410 
411  }//end _fillDefaultValues()
412 
413 
414 }//end class
415 
416 ?>