18 require_once SQ_PACKAGES_PATH.
'/cms/form/form_action/form_action.inc';
19 require_once SQ_PACKAGES_PATH.
'/sharepoint/lib/sharepoint_common.inc';
34 class Form_Action_Create_Sharepoint_List_Item
extends Form_Action
44 parent::__construct();
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;
65 require_once SQ_PACKAGES_PATH.
'/sharepoint/lib/sharepoint_common.inc';
71 $lists_fields = Array();
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');
82 $connection = Array();
83 if (!empty($sharepoint_url) && !empty($username)) {
84 $has_connection = TRUE;
86 'url' => $sharepoint_url,
87 'authentication' => Array (
89 'password' => $password,
99 $list_names += $sp_lists;
103 foreach ($list_names as $id => $name) {
106 if (isset($available_fields_by_list[
'FileLeafRef'])) {
107 unset($available_fields_by_list[
'FileLeafRef']);
109 $lists_fields[$id] = $available_fields_by_list;
115 <script type=
"text/javascript">
116 var description_list = {};
118 var custom_lists = {};
121 foreach ($all_lists as $id => $list_info) {
123 description_list[
'<?php echo $id; ?>'] =
'<?php echo $list_info['Description
']; ?>';
125 if ((
int) $list_info[
'ServerTemplate'] == 100) {
127 custom_lists[
'<?php echo $id; ?>'] =
'<?php echo $list_info['Title
']; ?>';
132 foreach ($lists_fields as $id => $avail_fields) {
134 field_list[
'<?php echo $id; ?>'] = {};
135 <?php
foreach ($avail_fields as $field_name => $display_name) {
137 field_list[
'<?php echo $id; ?>'][
'<?php echo $field_name; ?>'] =
'<?php echo $display_name; ?>';
144 function replace_description(
id) {
150 desc =
'Description: '+description_list[id];
153 document.getElementById(
'list_description').innerHTML = desc;
158 function cleanTable(table)
160 if ( table.hasChildNodes() ) {
161 while (table.childNodes.length > 0) {
162 table.removeChild( table.firstChild );
167 function updateListAvailableFields(
id) {
169 var table = document.getElementById(
'field_input_table');
172 new_tbody = document.createElement(
"tbody");
174 var list_fields = null;
176 list_fields = field_list[id];
178 for (var field_name in list_fields) {
180 new_tr = document.createElement(
"tr");
181 new_td_1 = document.createElement(
"td");
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);
189 new_tr.appendChild(new_td_1);
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';
196 new_td_2.innerHTML =
'<input type="text" id="'+field_name+
'" name="sharepoint_fields['+field_name+
']" style="width:200px;" />';
198 new_tr.appendChild(new_td_2);
199 new_tbody.appendChild(new_tr);
203 new_tr = document.createElement(
"tr");
204 new_td_1 = document.createElement(
"td");
206 new_td_1.innerHTML =
'<strong>No List Selected</strong>';
207 new_tr.appendChild(new_td_1);
208 new_tbody.appendChild(new_tr);
211 table.appendChild(new_tbody);
215 function paintAttachmentMapping(
id, attachment_question)
217 var table = document.getElementById(
'attachment_input_table');
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");
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);
231 new_tr.appendChild(new_td_1);
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';
238 new_td_2.innerHTML =
'<input type="text" id="sp_attachment" name="sp_attachment" value="'+attachment_question+
'" style="width:200px;" />';
240 new_tr.appendChild(new_td_2);
241 new_tbody.appendChild(new_tr);
244 new_tr = document.createElement(
"tr");
245 new_td_1 = document.createElement(
"td");
247 new_td_1.innerHTML =
'<strong>File attachment not available.</strong>';
248 new_tr.appendChild(new_td_1);
249 new_tbody.appendChild(new_tr);
252 table.appendChild(new_tbody);
257 <p
class=
"sq-backend-section-subheading">Connection Details</p>
259 <div
id=
"<?php echo $prefix ?>_connector_div" style=
"padding: 0.5em 0px 2em 30px;">
260 <table
class=
"sq-backend-table" style=
"width:auto">
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>
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 />
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>
277 <div
id=
"<?php echo $prefix ?>_connector_div" style=
"padding: 0.5em 0px 2em 30px;">
278 <table
class=
"sq-backend-table" style=
"width:auto">
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>
287 <p class="sq-backend-section-subheading">Create Record Settings</p>
289 <div
id="<?php echo $prefix ?>_record_type_selection" style="padding: 0.5em 0px 2em 30px;">
290 <table style="width:auto;padding-left: 30px;">
292 <td valign="top" width="75"><strong><?php echo 'Select List ';?></strong><br /><br /></td>
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', '');
299 <span
id="list_description"></span>
308 <div
id="<?php echo $prefix ?>_mandatory_fields" style="padding: 0.5em 0px 2em 30px;">
310 <p class="sq-backend-section-subheading">Available Fields</p>
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;">
315 <td style="width:180px;"><strong>No List Selected</strong></td>
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>
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;">
326 <td style="width:180px;"><strong>
File attachment not available</strong></td>
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
', ''); ?>')
340 if (!empty($sharepoint_fields)) {
341 foreach ($sharepoint_fields as $field_id => $value) {
343 var text_box = document.getElementById(
'<?php echo $field_id; ?>');
344 if (text_box != null) {
345 text_box.value =
'<?php echo $value; ?>';
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',
'');
371 if (isset($settings[
'_create_list'])) unset($settings[
'_create_list']);
373 if (!empty($create_list_name)) {
374 $settings[
'create_list_name'] = $create_list_name;
377 if (!(isset($settings[
'create_list'])) || $new_create_list != $settings[
'create_list']) {
378 $settings[
'create_list'] = $new_create_list;
379 $settings[
'sharepoint_fields'] = Array();
382 if (isset($_POST[
'sharepoint_fields'])) {
383 foreach ($_POST[
'sharepoint_fields'] as $field_id => $value) {
384 $settings[
'sharepoint_fields'][$field_id] = $value;
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);
394 if (!is_null($new_url)) {
395 $settings[
'url'] = $new_url;
397 if (!is_null($new_username)) {
398 $settings[
'username'] = $new_username;
400 if (!is_null($new_password)) {
401 $settings[
'password'] = $new_password;
403 if (!is_null($creator_username)) {
404 $settings[
'creator_username'] = $creator_username;
406 if (!is_null($session_creator_username)) {
407 $settings[
'session_creator_username'] = $session_creator_username;
423 self::_fillDefaults($settings);
425 ?><table
class=
"no-borders">
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
439 ?><span class="sq-backend-warning
">No connection details specified.</span><?php
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);
450 ?><span class="sq-backend-warning
">No list has been selected.</span><?php
457 }//end paintSummary()
469 public static function execute(Form $form, $settings)
473 $create_list = array_get_index($settings, 'create_list', 'None');
474 $sharepoint_fields = array_get_index($settings, 'sharepoint_fields', '');
476 foreach ($sharepoint_fields as $field_name => $field_content) {
477 self::getKeywordReplacementsForForm($field_name, $field_content, $sharepoint_fields, $form);
480 $form_sub_id_array = Array();
481 if (!empty($settings['attachment_assetid'])) {
482 $form_sub_id_array = Array (
483 'submission_id' => '%form_submission_id%',
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');
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'];
503 } else if (isset($extra_data['filesystem_path'])) {
504 $uploaded_file_path = $extra_data['filesystem_path'];
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);
513 // Construct the connection
515 if ($settings['session_creator_username']) {
516 if (!empty($GLOBALS['SQ_SYSTEM']->user)) {
517 $username = $GLOBALS['SQ_SYSTEM']->user->attr('username');
519 } else if (!empty($settings['creator_username'])) {
520 $username = $settings['creator_username'];
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'),
534 $connection['url'] = $settings['url'];
535 $connection['authentication'] = Array (
536 'login' => $settings['username'],
537 'password' => $settings['password'],
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);
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);
564 public static function getKeywordReplacementsForForm($field_name, $field_content, &$replacement, &$form)
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);
574 foreach ($matches as $match) {
575 if (empty($match)) continue;
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);
582 }//end getKeywordReplacementsForForm()
591 public static function isValid(Form $form, $settings)
593 self::_fillDefaults($settings);
595 // Must selected a record type to create
596 if (empty($settings['create_list'])) {
601 // Must selected a record type to create
602 if (empty($settings['sharepoint_fields'])) {
606 if (empty($settings['connector_assetid']) && empty($settings['url']) && empty($settings['username'])) {
622 private static function _fillDefaults(&$settings) {
623 if (!isset($settings['create_list'])) {
624 $settings['create_list'] = 'None';
627 if (!isset($settings['connector_assetid'])) {
628 $settings['connector_assetid'] = 0;
631 }//end _fillDefaults()