Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_action_trim_submit.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/form/form_action/form_action.inc';
19 require_once SQ_PACKAGES_PATH.'/trim/lib/trim_common.inc';
20 
35 {
36 
37 
42  function __construct()
43  {
44  parent::__construct();
45 
46  }//end constructor
47 
48 
54  public static function paintInlineInterface(Form $form, $settings, Backend_Outputter $o, $prefix)
55  {
56  self::_fillDefaults($settings);
57  $record_type = array_get_index($settings, 'record_type', 'document');
58  $additional_fields = array_get_index($settings, 'additional_fields', '');
59  ?>
60 
61  <p class="sq-backend-section-subheading">Connection Details</p>
62 
63  <div id="<?php echo $prefix ?>_connector_div" style="padding: 0.5em 0px 2em 30px;">
64  <table class="sq-backend-table" style="width:auto">
65  <tr>
66  <td width="100"><p>WSDL</p></td>
67  <td><p><?php text_box($prefix.'_wsdl', array_get_index($settings, 'wsdl', ''), 120); ?></p></td>
68  </tr>
69  <tr>
70  <td><?php echo translate('username'); ?></td>
71  <td><p><?php text_box($prefix.'_username', array_get_index($settings, 'username', ''), 35); ?><br />
72  </p></td>
73  </tr>
74  <tr>
75  <td><?php echo translate('password'); ?></td>
76  <td><p><?php password_box($prefix.'_password', array_get_index($settings, 'password', ''), 35); ?></p></td>
77  </tr>
78  <tr>
79  <td colspan="2"><?php echo translate('use_proxy_connector'); ?></td>
80  </tr>
81  </table>
82  </div>
83 
84  <div id="<?php echo $prefix ?>_connector_div" style="padding: 0.5em 0px 2em 30px;">
85  <table class="sq-backend-table" style="width:auto">
86  <tr>
87  <td><p>TRIM Connector Asset</p></td>
88  <td><p><?php asset_finder($prefix.'_connector', $settings['connector_assetid'], Array('trim_connector'=>'D')); ?><br/>
89  Note: If a TRIM connector is selected, the settings in the 'Connection Setup' section (above) have no effect. <br />TRIM Connector allows the TRIM settings to be reused on other TRIM assets..</p></td>
90  </tr>
91  </table>
92  </div>
93 
94  <p class="sq-backend-section-subheading">Create Record Settings</p>
95 
96  <div id="<?php echo $prefix ?>_record_type_selection" style="padding: 0.5em 0px 2em 30px;">
97  <table style="width:auto;padding-left: 30px;">
98  <tr>
99  <td><strong><?php echo translate('record_type');?></strong><br /><br /></td>
100  <td>
101  <?php
102  $options = Array (
103  'document' => 'Document',
104  );
105  combo_box($prefix.'_record_type', $options, FALSE, $record_type);
106  ?>
107  <br /><br />
108  </td>
109  </tr>
110  <tr>
111  <td><span style="padding-top:10px;"><strong><?php echo translate('creator_username');?></strong></span></td>
112  <td>
113  <?php
114  text_box($prefix.'_creator_username', array_get_index($settings,'creator_username', ''), 35, '', FALSE, ((boolean) array_get_index($settings, 'session_creator_username', FALSE)) ? ' disabled=true' :'');
115  ?>
116  <br />Use current logged in Matrix User
117  <?php
118  check_box($prefix.'_session_creator_username', '1', (boolean) array_get_index($settings, 'session_creator_username', FALSE), '
119  if (checked ==1) {
120  document.getElementById(\''.$prefix.'_creator_username\').disabled = true;
121  } else {
122  document.getElementById(\''.$prefix.'_creator_username\').disabled = false;
123  }
124  ', ' style="padding-top:2px;"');
125  ?>
126  <br />
127  </td>
128  </tr>
129  <tr>
130  </tr>
131  </table>
132  </div>
133 
134  <div id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
135 
136  <p class="sq-backend-section-subheading">Mandatory Fields Mapping</p>
137 
138  <div id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
139  <table style="width:auto;padding-left: 30px;">
140  <?php
141  $required_fields = self::getRecordTypeFields($record_type);
142  foreach ($required_fields as $field_name) {
143  ?>
144  <tr>
145  <td class="sq-backend-table-cell" style="width:180px;padding-left:15px;"><strong><?php echo ucwords(str_replace('_', ' ', $field_name)); ?></strong></td>
146  <td class="sq-backend-table-cell">
147  <?php
148  $security_levels = TRIM_Common::getSecurityLevel();
149  if ($field_name == 'security_level') {
150  combo_box($prefix.'_'.$record_type.'_security_level', $security_levels, FALSE, isset($settings[$record_type.'_security_level']) ? $settings[$record_type.'_security_level'] : 'Unrestricted');
151  } else {
152  text_box($prefix.'_'.$record_type.'_'.$field_name, array_get_index($settings, $record_type.'_'.$field_name, ''), 53);
153  }//end if
154  ?>
155  </td>
156  </tr>
157  <?php }//end foreach
158  ?>
159  </table>
160  </div>
161 
162  <p class="sq-backend-section-subheading">Additional Fields Mapping</p>
163 
164  <?php
165 
166  if (!empty($additional_fields)) {
167  require_once SQ_PACKAGES_PATH.'/trim/lib/trim_common.inc';
168  $record_properties = TRIM_Common::getRecordProperties();
169  ?>
170  <div id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
171  <table style="width:auto;padding-left: 30px;">
172  <tr>
173  <td><?php echo translate('field_name') ?></td>
174  <td><?php echo translate('field_value') ?></td>
175  <td><?php echo translate('delete_question'); ?></td>
176  </tr>
177  <?php
178  foreach ($additional_fields as $field_name => $field_value) {
179  ?>
180  <tr>
181  <td class="sq-backend-table-cell" style="width:180px;padding-left:15px;"><strong><?php echo $record_properties[$field_name]; ?></strong></td>
182  <td class="sq-backend-table-cell" style="width:165px;padding-left:5px;">
183  <?php
184  text_area($prefix.'_addtional_field_value_edit['.$field_name.']', $field_value, 50, 3);
185  ?>
186  </td>
187  <td class="sq-backend-table-cell">
188  <?php
189  check_box($prefix.'_addtional_field_remove[]', $field_name, FALSE);
190  ?>
191  </td>
192  </tr>
193  <?php }//end foreach
194  ?>
195  </table>
196  </div>
197  <?php
198  }//end if
199 
200  require_once SQ_PACKAGES_PATH.'/trim/lib/trim_common.inc';
201  $record_properties[''] = 'None';
202  $record_properties = array_merge($record_properties, TRIM_Common::getRecordProperties());
203  ksort($record_properties);
204 
205  ?>
206  <div id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
207  <table style="width:auto;padding-left: 30px;">
208  <tr>
209  <td>New Field Name</td>
210  <td>New Field Value</td>
211  </tr>
212  <tr>
213  <td class="sq-backend-table-cell"><strong><?php
214  combo_box($prefix.'_addtional_field_name', $record_properties, FALSE, 'None');
215  ?></strong></td>
216  <td class="sq-backend-table-cell">
217  <?php
218  text_area($prefix.'_addtional_field_value', '', 50, 3);
219  ?>
220  </td>
221  </tr>
222  </table>
223  </div>
224  </div>
225 
226  <?php
227  //self::execute($form, $settings);
228 
229  }//end paintInlineInterface()
230 
231 
238  public static function processInlineInterface(Form $form, &$settings, Backend_Outputter $o, $prefix)
239  {
240  $settings['connector_assetid'] = $_POST[$prefix.'_connector']['assetid'];
241  $settings['record_type'] = $_POST[$prefix.'_record_type'];
242  $required_fields = self::getRecordTypeFields($settings['record_type']);
243 
244  $new_wsdl = array_get_index($_POST, $prefix.'_wsdl', '');
245  $new_username = array_get_index($_POST, $prefix.'_username', '');
246  $new_password = array_get_index($_POST, $prefix.'_password', '');
247  $creator_username = array_get_index($_POST, $prefix.'_creator_username', NULL);
248  $session_creator_username = array_get_index($_POST, $prefix.'_session_creator_username', FALSE);
249 
250  if (!is_null($new_wsdl)) {
251  $settings['wsdl'] = $new_wsdl;
252  }//end if
253  if (!is_null($new_username)) {
254  $settings['username'] = $new_username;
255  }//end if
256  if (!is_null($new_password)) {
257  $settings['password'] = $new_password;
258  }//end if
259  if (!is_null($creator_username)) {
260  $settings['creator_username'] = $creator_username;
261  }//end if
262  if (!is_null($session_creator_username)) {
263  $settings['session_creator_username'] = $session_creator_username;
264  }//end if
265 
266  foreach ($required_fields as $field_name) {
267  if (isset($_POST[$prefix.'_'.$settings['record_type'].'_'.$field_name])) {
268  $settings[$settings['record_type'].'_'.$field_name] = $_POST[$prefix.'_'.$settings['record_type'].'_'.$field_name];
269  }//end if
270  }//end foreach
271 
272  if (isset($_POST[$prefix.'_addtional_field_remove'])) {
273  $remove_field_names = $_POST[$prefix.'_addtional_field_remove'];
274  foreach ($remove_field_names as $remove_field_name) {
275  if (isset($settings['additional_fields'][$remove_field_name])) {
276  unset($settings['additional_fields'][$remove_field_name]);
277  }//end if
278  }//end foreach
279  }//end if
280  if (isset($_POST[$prefix.'_addtional_field_name']) && !empty($_POST[$prefix.'_addtional_field_name'])) {
281  $settings['additional_fields'][$_POST[$prefix.'_addtional_field_name']] = $_POST[$prefix.'_addtional_field_value'];
282  }//end if
283 
284  if (isset($_POST[$prefix.'_addtional_field_value_edit']) && !empty($_POST[$prefix.'_addtional_field_value_edit'])) {
285  $editing_list = $_POST[$prefix.'_addtional_field_value_edit'];
286  foreach ($editing_list as $field_name => $field_value) {
287  if (isset($settings['additional_fields'][$field_name]) && $settings['additional_fields'][$field_name] != $field_value) {
288  $settings['additional_fields'][$field_name] = $field_value;
289  }//end if
290  }//end foreach
291 
292  }//end if
293 
294  return TRUE;
295 
296  }//end processInlineInterface()
297 
298 
305  public static function paintSummary(Form $form, $settings, Backend_Outputter $o, $prefix)
306  {
307  self::_fillDefaults($settings);
308 
309  ?><table class="no-borders">
310  <colgroup>
311  <col width="80" />
312  <col/>
313  </colgroup>
314  <tbody>
315  <tr>
316  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Data Source:</strong></p></td>
317  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
318  if (!empty($settings['connector_assetid'])) {
319  echo get_asset_tag_line($settings['connector_assetid']);
320  } else {
321  ?><span class="sq-backend-warning">No connection details specified.</span><?php
322  }
323  ?></p></td>
324  </tr>
325  <tr>
326  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Create Record Type</strong></p></td>
327  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
328  if (!empty($settings['record_type'])) {
329  echo ellipsisize(preg_replace('/\\n/', ' ', htmlspecialchars(ucwords($settings['record_type']))), 512);
330  } else {
331  ?><span class="sq-backend-warning">No record type selected.</span><?php
332  } ?></p></td>
333  </tr>
334  </tbody>
335  </table>
336  <?php
337 
338  }//end paintSummary()
339 
340 
350  public static function execute(Form $form, $settings)
351  {
352  $found = 0;
353  $required_fields = self::getRecordTypeFields($settings['record_type']);
354  $additional_fields = array_get_index($settings, 'additional_fields', Array());
355 
356  $new_record_info['record_type'] = array_get_index($settings, 'record_type', '');
357 
358  foreach ($required_fields as $field_name) {
359  if (isset($settings[$settings['record_type'].'_'.$field_name])) {
360  $new_record_info[$field_name] = $settings[$settings['record_type'].'_'.$field_name];
361  // We will replace all responses, including raw values.
362  // We will also replace keywords relating to the form submission asset
363  // (eg. submission assetid, time, IP address)
364  $field_content = $settings[$settings['record_type'].'_'.$field_name];
365  self::getKeywordReplacementsForForm($field_name, $field_content, $new_record_info, $form);
366  }//end if
367  }//end foreach
368 
369  foreach ($additional_fields as $field_name => $field_content) {
370  self::getKeywordReplacementsForForm($field_name, $field_content, $additional_fields, $form);
371  }//end foreach
372 
373  $username = '';
374  if ($settings['session_creator_username']) {
375  if (!empty($GLOBALS['SQ_SYSTEM']->user)) {
376  $username = $GLOBALS['SQ_SYSTEM']->user->attr('username');
377  }//end if
378  } else if (!empty($settings['creator_username'])) {
379  $username = $settings['creator_username'];
380  }//end if
381 
382  $new_record_info['additional_fields'] = $additional_fields;
383 
384  $connection = Array();
385  if (!empty($settings['connector_assetid'])) {
386  $trim_connector = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['connector_assetid']);
387  $connection['wsdl'] = $trim_connector->attr('wsdl');
388  $connection['authentication'] = Array (
389  'login' => $trim_connector->attr('username'),
390  'password' => $trim_connector->attr('password'),
391  'trace' => TRUE,
392  'exceptions'=> TRUE,
393  );
394  // add proxy
395  $proxy = $trim_connector->getProxy();
396  $connection['authentication'] = array_merge($connection['authentication'], $proxy);
397  } else {
398  $connection['wsdl'] = $settings['wsdl'];
399  $connection['authentication'] = Array (
400  'login' => $settings['username'],
401  'password' => $settings['password'],
402  'trace' => TRUE,
403  'exceptions'=> TRUE,
404  );
405  }//end if
406 
407  $new_record_uri = TRIM_Common::executeCreateRequest($connection, $username, $new_record_info);
408 
409  return TRUE;
410 
411  }//end execute()
412 
413 
420  public static function getKeywordReplacementsForForm($field_name, $field_content, &$replacement, &$form)
421  {
422  $matches = Array();
423  $found = preg_match_all('/%(response_(\\d+_)?q\\d+(_raw)?)%/U', $field_content, $set_matches, PREG_SET_ORDER);
424  $matches = array_merge($matches, $set_matches);
425  $found = preg_match_all('/%(question_name_(\\d+_)?q\\d+(_raw)?)%/U', $field_content, $set_matches, PREG_SET_ORDER);
426  $matches = array_merge($matches, $set_matches);
427  $found = preg_match_all('/%(form_submission_.*)%/U', $field_content, $set_matches, PREG_SET_ORDER);
428  $matches = array_merge($matches, $set_matches);
429 
430  foreach ($matches as $match) {
431  if (empty($match)) continue;
432  $count = 0;
433  do {
434  $field_content = preg_replace('/%'.$match[1].'%/U', $form->_getThankYouKeywordReplacement($match[1]), $field_content, 1, $count);
435  $replacement[$field_name] = $field_content;
436  } while ($count > 0);
437  }//end foreach
438  }//end getKeywordReplacementsForForm()
439 
440 
447  public static function isValid(Form $form, $settings)
448  {
449  self::_fillDefaults($settings);
450 
451  // Must have a TRIM Bridge to be valid
452  if (empty($settings['connector_assetid'])) {
453  return FALSE;
454  }
455 
456  // Must selected a record type to create
457  if (empty($settings['record_type'])) {
458  return FALSE;
459  }//end if
460 
461  // All the mandatory fields needs to be filled in
462  if (!empty($settings['record_type'])) {
463  $required_fields = self::getRecordTypeFields($settings['record_type']);
464 
465  foreach ($required_fields as $field_name) {
466  if (empty($settings[$settings['record_type'].'_'.$field_name])) {
467  return FALSE;
468  }//end if
469  }//end foreach
470  }//end if
471 
472  if (empty($settings['connector_assetid']) && empty($settings['wsdl']) && empty($settings['username'])) {
473  return FALSE;
474  }//end if
475 
476  if (!$settings['session_creator_username'] && empty($settings['creator_username'])) {
477  return FALSE;
478  }//end if
479 
480  return TRUE;
481 
482  }//end isValid()
483 
484 
492  private static function _fillDefaults(&$settings) {
493  if (!isset($settings['record_type'])) {
494  $settings['record_type'] = 'document';
495  }//end if
496 
497  if (!isset($settings['connector_assetid'])) {
498  $settings['connector_assetid'] = 0;
499  }//end if
500 
501  }//end _fillDefaults()
502 
503 
504  private static function getRecordTypeFields($name)
505  {
506  $func_name = 'get'.$name.'TypeFields';
507  $fields = self::$func_name();
508 
509  return $fields;
510  }//end getRecordTypeFields()
511 
512 
513  private static function getBoxTypeFields()
514  {
515  $field_list = Array (
516  'Name', 'Val'
517  );
518  return $field_list;
519 
520  }//end getBoxTypeFields()
521 
522  private static function getDocumentTypeFields()
523  {
524  $field_list = Array (
525  'record_title', 'parent_record_number', 'security_level',
526  );
527 
528  return $field_list;
529 
530  }//end getBoxTypeFields()
531 
532  private static function getFileTypeFields()
533  {
534  $field_list = Array (
535  'Name', 'FileSize'
536  );
537  return $field_list;
538 
539  }//end getBoxTypeFields()
540 
541 
542 }//end class
543 
544 ?>