Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_action_create_sharepoint_list_item.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/form/form_action/form_action.inc';
19 require_once SQ_PACKAGES_PATH.'/sharepoint/lib/sharepoint_common.inc';
20 
34 class Form_Action_Create_Sharepoint_List_Item extends Form_Action
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 
57  self::_fillDefaults($settings);
58  $create_list = array_get_index($settings, 'create_list', 'None');
59  $sharepoint_fields = array_get_index($settings, 'sharepoint_fields', '');
60  $sharepoint_url = array_get_index($settings, 'url', '');
61  $username = array_get_index($settings, 'username', '');
62  $password = array_get_index($settings, 'password', '');
63  $has_connection = FALSE;
64 
65  require_once SQ_PACKAGES_PATH.'/sharepoint/lib/sharepoint_common.inc';
66 
67  $list_names = Array (
68  'None' => 'None' // We want to keep this option on top
69  );
70  $all_lists = Array();
71  $lists_fields = Array();
72 
73  $wsdl_disabled = (!empty($settings['connector_assetid'])) ? ' disabled="disabled"' : '';
74  if (!empty($settings['connector_assetid'])) {
75  $sharepoint_connector = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['connector_assetid']);
76  $sharepoint_url = $sharepoint_connector->attr('url');
77  $username = $sharepoint_connector->attr('username');
78  $password = $sharepoint_connector->attr('password');
79 
80  }//end if
81 
82  $connection = Array();
83  if (!empty($sharepoint_url) && !empty($username)) {
84  $has_connection = TRUE;
85  $connection = Array (
86  'url' => $sharepoint_url,
87  'authentication' => Array (
88  'login' => $username,
89  'password' => $password,
90  'trace' => TRUE,
91  'exception' => TRUE,
92  ),
93  );
94 
96  if ($valid) {
97  $all_lists = Sharepoint_Common::getListCollections($connection, TRUE);
98  $sp_lists = Sharepoint_Common::getListNames($connection, $all_lists);
99  $list_names += $sp_lists;
100  }//end if
101 
102 
103  foreach ($list_names as $id => $name) {
104  if ($id != 'None') {
105  $available_fields_by_list = Sharepoint_Common::getListAvailableFields($connection, $id);
106  if (isset($available_fields_by_list['FileLeafRef'])) {
107  unset($available_fields_by_list['FileLeafRef']);
108  }//end
109  $lists_fields[$id] = $available_fields_by_list;
110  }//end if
111  }//end foreach
112  }
113 
114  ?>
115  <script type="text/javascript">
116  var description_list = {};
117  var field_list = {};
118  var custom_lists = {};
119 
120  <?php
121  foreach ($all_lists as $id => $list_info) {
122  ?>
123  description_list['<?php echo $id; ?>'] = '<?php echo $list_info['Description']; ?>';
124  <?php
125  if ((int) $list_info['ServerTemplate'] == 100) {
126  ?>
127  custom_lists['<?php echo $id; ?>'] = '<?php echo $list_info['Title']; ?>';
128  <?php
129  }//end if
130  }//end foreach
131 
132  foreach ($lists_fields as $id => $avail_fields) {
133  ?>
134  field_list['<?php echo $id; ?>'] = {};
135  <?php foreach ($avail_fields as $field_name => $display_name) {
136  ?>
137  field_list['<?php echo $id; ?>']['<?php echo $field_name; ?>'] = '<?php echo $display_name; ?>';
138  <?php
139  }
140  ?>
141  <?php
142  }//end foreach ?>
143 
144  function replace_description(id) {
145 
146  var desc = '';
147  if (id == 'None') {
148  desc = '';
149  } else {
150  desc = 'Description: '+description_list[id];
151  }//end else
152 
153  document.getElementById('list_description').innerHTML = desc;
154 
155  }//end replace_description()
156 
157 
158  function cleanTable(table)
159  {
160  if ( table.hasChildNodes() ) {
161  while (table.childNodes.length > 0) {
162  table.removeChild( table.firstChild );
163  }//end while
164  }//end if
165  }//end cleanTable
166 
167  function updateListAvailableFields(id) {
168 
169  var table = document.getElementById('field_input_table');
170  cleanTable(table);
171 
172  new_tbody = document.createElement("tbody");
173  if (id != 'None') {
174  var list_fields = null;
175 
176  list_fields = field_list[id];
177 
178  for (var field_name in list_fields) {
179 
180  new_tr = document.createElement("tr");
181  new_td_1 = document.createElement("td");
182 
183  var new_text = document.createTextNode(list_fields[field_name]);
184  new_td_1.className = 'sq-backend-table-cell';
185  new_td_1.style.width = '180px';
186  new_td_1.style.padding = '8 0 0 15px';
187  new_td_1.appendChild(new_text);
188 
189  new_tr.appendChild(new_td_1);
190 
191  new_td_2 = document.createElement('td');
192  new_td_2.className = "sq-backend-table-cell";
193  new_td_2.style.width = '180px';
194  new_td_2.style.padding = '5 0 0 15px';
195 
196  new_td_2.innerHTML = '<input type="text" id="'+field_name+'" name="sharepoint_fields['+field_name+']" style="width:200px;" />';
197 
198  new_tr.appendChild(new_td_2);
199  new_tbody.appendChild(new_tr);
200  }//end for
201 
202  } else {
203  new_tr = document.createElement("tr");
204  new_td_1 = document.createElement("td");
205 
206  new_td_1.innerHTML = '<strong>No List Selected</strong>';
207  new_tr.appendChild(new_td_1);
208  new_tbody.appendChild(new_tr);
209  }//end else
210 
211  table.appendChild(new_tbody);
212 
213  }//end updateListAvailableFields()
214 
215  function paintAttachmentMapping(id, attachment_question)
216  {
217  var table = document.getElementById('attachment_input_table');
218  cleanTable(table);
219 
220  new_tbody = document.createElement("tbody");
221  if (custom_lists[id] != null) {
222  new_tr = document.createElement("tr");
223  new_td_1 = document.createElement("td");
224 
225  var new_text = document.createTextNode('Attachment Question Asset ID');
226  new_td_1.className = 'sq-backend-table-cell';
227  new_td_1.style.width = '180px';
228  new_td_1.style.padding = '8 0 0 15px';
229  new_td_1.appendChild(new_text);
230 
231  new_tr.appendChild(new_td_1);
232 
233  new_td_2 = document.createElement('td');
234  new_td_2.className = "sq-backend-table-cell";
235  new_td_2.style.width = '180px';
236  new_td_2.style.padding = '5 0 0 15px';
237 
238  new_td_2.innerHTML = '<input type="text" id="sp_attachment" name="sp_attachment" value="'+attachment_question+'" style="width:200px;" />';
239 
240  new_tr.appendChild(new_td_2);
241  new_tbody.appendChild(new_tr);
242 
243  } else {
244  new_tr = document.createElement("tr");
245  new_td_1 = document.createElement("td");
246 
247  new_td_1.innerHTML = '<strong>File attachment not available.</strong>';
248  new_tr.appendChild(new_td_1);
249  new_tbody.appendChild(new_tr);
250  }//end else
251 
252  table.appendChild(new_tbody);
253  }
254 
255  </script>
256 
257  <p class="sq-backend-section-subheading">Connection Details</p>
258 
259  <div id="<?php echo $prefix ?>_connector_div" style="padding: 0.5em 0px 2em 30px;">
260  <table class="sq-backend-table" style="width:auto">
261  <tr>
262  <td width="100"><p>System URL</p></td>
263  <td><p><?php text_box($prefix.'_url', array_get_index($settings, 'url', ''), 120, '', FALSE, $wsdl_disabled); ?></p></td>
264  </tr>
265  <tr>
266  <td><?php echo translate('username'); ?></td>
267  <td><p><?php text_box($prefix.'_username', array_get_index($settings, 'username', ''), 35, '', FALSE, $wsdl_disabled); ?><br />
268  </p></td>
269  </tr>
270  <tr>
271  <td><?php echo translate('password'); ?></td>
272  <td><p><?php password_box($prefix.'_password', array_get_index($settings, 'password', ''), 35, '', $wsdl_disabled); ?></p></td>
273  </tr>
274  </table>
275  </div>
276 
277  <div id="<?php echo $prefix ?>_connector_div" style="padding: 0.5em 0px 2em 30px;">
278  <table class="sq-backend-table" style="width:auto">
279  <tr>
280  <td><p>Sharepoint Connector Asset</p></td>
281  <td><p><?php asset_finder($prefix.'_connector', $settings['connector_assetid'], Array('sharepoint_connector'=>'D')); ?><br/>
282  Note: If a Sharepoint connector is selected, the settings in the 'Connection Setup' section (above) have no effect. <br />Sharepoint Connector allows the Sharepoint connection settings to be reused on other Sharepoint assets..</p></td>
283  </tr>
284  </table>
285  </div>
286 
287  <p class="sq-backend-section-subheading">Create Record Settings</p>
288 
289  <div id="<?php echo $prefix ?>_record_type_selection" style="padding: 0.5em 0px 2em 30px;">
290  <table style="width:auto;padding-left: 30px;">
291  <tr>
292  <td valign="top" width="75"><strong><?php echo 'Select List&nbsp;&nbsp;&nbsp;&nbsp;';?></strong><br /><br /></td>
293  <td>
294  <?php
295  combo_box($prefix.'_create_list', $list_names, FALSE, $create_list, 0, ' onChange="replace_description(this.value);paintAttachmentMapping(this.value, \'\');updateListAvailableFields(this.value);document.getElementById(\'selected_list\').value=(this.options[this.selectedIndex].text);"');
296  hidden_field('selected_list', '');
297  ?>
298  <br /><br />
299  <span id="list_description"></span>
300  </td>
301  </tr>
302 
303  <tr>
304  </tr>
305  </table>
306  </div>
307 
308  <div id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
309 
310  <p class="sq-backend-section-subheading">Available Fields</p>
311 
312  <div id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
313  <table id="field_input_table" name="field_input_table" style="width:auto;padding-left: 30px;">
314  <tr>
315  <td style="width:180px;"><strong>No List Selected</strong></td>
316  </tr>
317  </table>
318  </div>
319 
320  <p class="sq-backend-section-subheading">Attachment</p>
321  <p class="sq-backend-smallprint" style="padding-left:60px;">Only entry created in a Sharepoint custom list can have file attached.</p>
322 
323  <div id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
324  <table id="attachment_input_table" name="attachment_input_table" style="width:auto;padding-left: 30px;">
325  <tr>
326  <td style="width:180px;"><strong>File attachment not available</strong></td>
327  </tr>
328  </table>
329  </div>
330 
331  </div>
332 
333  <?php if ($create_list != 'None' && $has_connection) { ?>
334  <script type="text/javascript">
335  replace_description('<?php echo $create_list; ?>');
336  updateListAvailableFields('<?php echo $create_list; ?>');
337  paintAttachmentMapping('<?php echo $create_list; ?>', '<?php echo array_get_index($settings, 'attachment_assetid', ''); ?>')
338  <?php
339 
340  if (!empty($sharepoint_fields)) {
341  foreach ($sharepoint_fields as $field_id => $value) {
342  ?>
343  var text_box = document.getElementById('<?php echo $field_id; ?>');
344  if (text_box != null) {
345  text_box.value = '<?php echo $value; ?>';
346  }//end if
347  <?php
348  }//end foreach
349  }//end if
350  ?>
351  </script>
352  <?php }
353 
354  }//end paintInlineInterface()
355 
356 
363  public static function processInlineInterface(Form $form, &$settings, Backend_Outputter $o, $prefix)
364  {
365  $settings['connector_assetid'] = $_POST[$prefix.'_connector']['assetid'];
366  $new_create_list = $_POST[$prefix.'_create_list'];
367  $create_list_name = $_POST['selected_list'];
368  $settings['sharepoint_fields'] = Array();
369  $settings['attachment_assetid'] = array_get_index($_POST, 'sp_attachment', '');
370 
371  if (isset($settings['_create_list'])) unset($settings['_create_list']);
372 
373  if (!empty($create_list_name)) {
374  $settings['create_list_name'] = $create_list_name;
375  }//end if
376 
377  if (!(isset($settings['create_list'])) || $new_create_list != $settings['create_list']) {
378  $settings['create_list'] = $new_create_list;
379  $settings['sharepoint_fields'] = Array();
380  }//end if
381 
382  if (isset($_POST['sharepoint_fields'])) {
383  foreach ($_POST['sharepoint_fields'] as $field_id => $value) {
384  $settings['sharepoint_fields'][$field_id] = $value;
385  }//end foreach
386  }//end if
387 
388  $new_url = array_get_index($_POST, $prefix.'_url', NULL);
389  $new_username = array_get_index($_POST, $prefix.'_username', NULL);
390  $new_password = array_get_index($_POST, $prefix.'_password', NULL);
391  $creator_username = array_get_index($_POST, $prefix.'_creator_username', NULL);
392  $session_creator_username = array_get_index($_POST, $prefix.'_session_creator_username', FALSE);
393 
394  if (!is_null($new_url)) {
395  $settings['url'] = $new_url;
396  }//end if
397  if (!is_null($new_username)) {
398  $settings['username'] = $new_username;
399  }//end if
400  if (!is_null($new_password)) {
401  $settings['password'] = $new_password;
402  }//end if
403  if (!is_null($creator_username)) {
404  $settings['creator_username'] = $creator_username;
405  }//end if
406  if (!is_null($session_creator_username)) {
407  $settings['session_creator_username'] = $session_creator_username;
408  }//end if
409 
410  return TRUE;
411 
412  }//end processInlineInterface()
413 
414 
421  public static function paintSummary(Form $form, $settings, Backend_Outputter $o, $prefix)
422  {
423  self::_fillDefaults($settings);
424 
425  ?><table class="no-borders">
426  <colgroup>
427  <col width="80" />
428  <col/>
429  </colgroup>
430  <tbody>
431  <tr>
432  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Data Source:</strong></p></td>
433  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
434  if (!empty($settings['connector_assetid'])) {
435  echo get_asset_tag_line($settings['connector_assetid']);
436  } else if (!empty($settings['url']) && !empty($settings['username'])) {
437  ?><span class="""><?php echo $settings['url']; ?></span><?php
438  } else {
439  ?><span class="sq-backend-warning">No connection details specified.</span><?php
440  }
441  ?></p></td>
442  </tr>
443  <tr>
444  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong>Create In List</strong></p></td>
445  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
446  if (!empty($settings['create_list']) || !empty($settings['create_list_name'])) {
447  $list_name = (!empty($settings['create_list_name']) && $settings['create_list'] != 'None') ? $settings['create_list_name'] : $settings['create_list'];
448  echo ellipsisize(preg_replace('/\\n/', ' ', htmlspecialchars($list_name)), 512);
449  } else {
450  ?><span class="sq-backend-warning">No list has been selected.</span><?php
451  } ?></p></td>
452  </tr>
453  </tbody>
454  </table>
455  <?php
456 
457  }//end paintSummary()
458 
459 
469  public static function execute(Form $form, $settings)
470  {
471  $found = 0;
472 
473  $create_list = array_get_index($settings, 'create_list', 'None');
474  $sharepoint_fields = array_get_index($settings, 'sharepoint_fields', '');
475 
476  foreach ($sharepoint_fields as $field_name => $field_content) {
477  self::getKeywordReplacementsForForm($field_name, $field_content, $sharepoint_fields, $form);
478  }//end foreach
479 
480  $form_sub_id_array = Array();
481  if (!empty($settings['attachment_assetid'])) {
482  $form_sub_id_array = Array (
483  'submission_id' => '%form_submission_id%',
484  );
485  self::getKeywordReplacementsForForm(key($form_sub_id_array), $form_sub_id_array[key($form_sub_id_array)], $form_sub_id_array, $form);
486  $submission_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($form_sub_id_array['submission_id']);
487  $sub_asset_attr = $submission_asset->attr('attributes');
488  }//end if
489 
490 
491  // Handling file upload
492  $base64_content = NULL;
493  if (!empty($sub_asset_attr) && isset($sub_asset_attr['answers'][$settings['attachment_assetid']])) {
494  $extra_data = array_get_index($sub_asset_attr['answers'][$settings['attachment_assetid']], 'extra_data', Array());
495  $uploaded_file_path = NULL;
496  if (isset($extra_data['new_file_assetid'])) {
497  $uploaded_file_assetid = $sub_asset_attr['answers'][$settings['attachment_assetid']]['extra_data']['new_file_assetid'];
498  $file_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($uploaded_file_assetid);
499  $file_info = $file_asset->getExistingFile();
500  if (!empty($file_info) && isset($file_info['path'])) {
501  $uploaded_file_path = $file_info['path'];
502  }//end if
503  } else if (isset($extra_data['filesystem_path'])) {
504  $uploaded_file_path = $extra_data['filesystem_path'];
505  }//end else
506 
507  // I dont know why we dont have to do base64_encode, if we do, the content in Sharepoint will be messed up.
508  // So we leave the content untouched.
509  $base64_content = file_get_contents($uploaded_file_path);
510 
511  }//end if
512 
513  // Construct the connection
514  $username = '';
515  if ($settings['session_creator_username']) {
516  if (!empty($GLOBALS['SQ_SYSTEM']->user)) {
517  $username = $GLOBALS['SQ_SYSTEM']->user->attr('username');
518  }//end if
519  } else if (!empty($settings['creator_username'])) {
520  $username = $settings['creator_username'];
521  }//end if
522 
523  $connection = Array();
524  if (!empty($settings['connector_assetid'])) {
525  $trim_connector = $GLOBALS['SQ_SYSTEM']->am->getAsset($settings['connector_assetid']);
526  $connection['url'] = $trim_connector->attr('url');
527  $connection['authentication'] = Array (
528  'login' => $trim_connector->attr('username'),
529  'password' => $trim_connector->attr('password'),
530  'trace' => TRUE,
531  'exception' => TRUE,
532  );
533  } else {
534  $connection['url'] = $settings['url'];
535  $connection['authentication'] = Array (
536  'login' => $settings['username'],
537  'password' => $settings['password'],
538  'trace' => TRUE,
539  'exception' => TRUE,
540  );
541  }//end if
542 
543  $ows_ID = Sharepoint_Common::addListItem($connection, $settings['create_list'], $sharepoint_fields);
544  if (!is_int($ows_ID)) {
545  trigger_error($ows_ID, E_USER_WARNING);
546  } else {
547  // We are ready for attachment
548  if (!empty($base64_content)) {
549  $attach_resutt = Sharepoint_Common::addAttachment($connection, $settings['create_list'], $ows_ID, $uploaded_file_path, $base64_content);
550  }//end if
551  }//end else
552 
553  return TRUE;
554 
555  }//end execute()
556 
557 
564  public static function getKeywordReplacementsForForm($field_name, $field_content, &$replacement, &$form)
565  {
566  $matches = Array();
567  $found = preg_match_all('/%(response_(\\d+_)?q\\d+(_raw)?)%/U', $field_content, $set_matches, PREG_SET_ORDER);
568  $matches = array_merge($matches, $set_matches);
569  $found = preg_match_all('/%(question_name_(\\d+_)?q\\d+(_raw)?)%/U', $field_content, $set_matches, PREG_SET_ORDER);
570  $matches = array_merge($matches, $set_matches);
571  $found = preg_match_all('/%(form_submission_.*)%/U', $field_content, $set_matches, PREG_SET_ORDER);
572  $matches = array_merge($matches, $set_matches);
573 
574  foreach ($matches as $match) {
575  if (empty($match)) continue;
576  $count = 0;
577  do {
578  $field_content = preg_replace('/%'.$match[1].'%/U', $form->_getThankYouKeywordReplacement($match[1]), $field_content, 1, $count);
579  $replacement[$field_name] = $field_content;
580  } while ($count > 0);
581  }//end foreach
582  }//end getKeywordReplacementsForForm()
583 
584 
591  public static function isValid(Form $form, $settings)
592  {
593  self::_fillDefaults($settings);
594 
595  // Must selected a record type to create
596  if (empty($settings['create_list'])) {
597  return FALSE;
598  }//end if
599 
600 
601  // Must selected a record type to create
602  if (empty($settings['sharepoint_fields'])) {
603  return FALSE;
604  }//end if
605 
606  if (empty($settings['connector_assetid']) && empty($settings['url']) && empty($settings['username'])) {
607  return FALSE;
608  }//end if
609 
610  return TRUE;
611 
612  }//end isValid()
613 
614 
622  private static function _fillDefaults(&$settings) {
623  if (!isset($settings['create_list'])) {
624  $settings['create_list'] = 'None';
625  }//end if
626 
627  if (!isset($settings['connector_assetid'])) {
628  $settings['connector_assetid'] = 0;
629  }//end if
630 
631  }//end _fillDefaults()
632 
633 
634 }//end class
635 
636 ?>