18 require_once SQ_PACKAGES_PATH.
'/cms/form/form_action/form_action.inc';
43 parent::__construct();
55 self::_fillDefaults($settings);
57 <script
type=
"text/javascript">
58 updateDbMethod =
function(form) {
59 method_db = form.<?php echo $prefix ?>_method_db;
61 el_list = document.getElementById(
"<?php echo $prefix ?>_conn_details_div").getElementsByTagName(
"select");
62 for (i = 0; i < el_list.length; i++) {
63 el_list[i].disabled = !method_db[0].checked;
66 el_list = document.getElementById(
"<?php echo $prefix ?>_conn_details_div").getElementsByTagName(
"input");
67 for (i = 0; i < el_list.length; i++) {
68 el_list[i].disabled = !method_db[0].checked;
71 el_list = document.getElementById(
"<?php echo $prefix ?>_conn_string_div").getElementsByTagName(
"input");
72 for (i = 0; i < el_list.length; i++) {
73 el_list[i].disabled = !method_db[1].checked;
77 el_list = document.getElementById(
"<?php echo $prefix ?>_login_section").getElementsByTagName(
"input");
78 for (i = 0; i < el_list.length; i++) {
79 el_list[i].disabled = method_db[2].checked;
82 el_list = document.getElementById(
"<?php echo $prefix ?>_connector_div").getElementsByTagName(
"input");
83 for (i = 0; i < el_list.length; i++) {
85 if ((el_list[i].type ==
'button') && (el_list[i].value ==
'Cancel')) {
88 el_list[i].disabled = !method_db[2].checked;
92 <?php $o->
addOnLoad(
'updateDbMethod(document.main_form);'); ?>
94 <p
class=
"sq-backend-section-subheading">Database Details</p>
96 <div
id=
"<?php echo $prefix ?>_dsn_section" style=
"padding: 0.5em 0px 2em 30px;">
99 radio_button($prefix.
'_method_db',
'table', ($settings[
'method'] ==
'table') ? TRUE : FALSE,
'updateDbMethod(this.form); return true;');
100 ?><strong>Specify database details</strong></p>
102 <div
id=
"<?php echo $prefix ?>_conn_details_div" style=
"padding-left: 30px">
106 $db_type = array_get_index($settings,
'db_type',
'');
107 $database = array_get_index($settings,
'database',
'');
108 $host = array_get_index($settings,
'host',
'');
111 <table
class=
"sq-backend-table" style=
"width:auto">
113 <td><?php echo translate(
'database_type');?></td>
116 $options = Array(
'pgsql' =>
'PostgreSQL',
'oci' =>
'Oracle',
'mysql' =>
'MySQL');
117 combo_box($prefix.
'_dsn[db_type]', $options, FALSE, $db_type);
122 <td><?php echo translate(
'database_name');?></td>
125 text_box($prefix.
'_dsn[database]', $database, 35);
130 <td><?php echo translate(
'host_name');?></td>
133 text_box($prefix.
'_dsn[host]', $host, 35);
145 radio_button($prefix.
'_method_db',
'string', ($settings[
'method'] ==
'string') ? TRUE : FALSE,
'updateDbMethod(this.form); return true;');
146 ?><strong>Use a complete
DSN</strong></p>
147 <div
id=
"<?php echo $prefix ?>_conn_string_div" style=
"padding-left: 30px;">
148 <table
class=
"sq-backend-table" style=
"width:auto">
150 <td><p>Complete
DSN</p></td>
151 <td><p><?php text_box($prefix.
'_complete_dsn', ($settings[
'method'] ==
'string') ? $settings[
'dsn'] :
'', 70); ?></p></td>
159 radio_button($prefix.
'_method_db',
'connector', ($settings[
'method'] ==
'connector') ? TRUE : FALSE,
'updateDbMethod(this.form); return true;');
160 ?> <strong>Use the contents of a DB Connector asset</strong></p><?php
163 <div
id=
"<?php echo $prefix ?>_connector_div" style=
"padding-left: 30px;">
164 <table
class=
"sq-backend-table" style=
"width:auto">
166 <td><p>DB Connector
Asset</p></td>
167 <td><p><?php asset_finder($prefix.
'_connector', $settings[
'connector_assetid'], Array(
'db_connector'=>
'D')); ?><br/>
168 Note: If a DB Connector asset is used, the user name and password fields below will be ignored, and the settings from that asset used instead.</p></td>
175 <p
class=
"sq-backend-section-subheading">Login Details</p>
177 <div
id=
"<?php echo $prefix ?>_login_section" style=
"padding: 0.5em 0px 2em 30px;">
178 <table style=
"width:auto">
180 <td><strong><?php echo translate(
'user_name');?></strong></td>
183 text_box($prefix.
'_username', $settings[
'username'], 35);
188 <td><strong><?php echo translate(
'password');?></strong></td>
191 password_box($prefix.
'_password', $settings[
'password'], 35);
199 <p
class=
"sq-backend-section-subheading">SQL
Query</p>
201 <div
id=
"<?php echo $prefix ?>_query_section" style=
"padding: 0.5em 0px 2em 30px;"><?php
204 text_area($prefix.
'_sql', $settings[
'sql'], 100, 40, 0,
'style="width: 80%; height: 30em; font-family: \'Courier New\', Courier, monospace; display: block"');
220 $settings[
'method'] = array_get_index($_POST, $prefix.
'_method_db',
'');
222 switch(array_get_index($_POST, $prefix.
'_method_db',
'')) {
224 $settings[
'dsn'] = $_POST[$prefix.
'_dsn'][
'db_type'].
':';
225 if (array_get_index($_POST[$prefix.
'_dsn'],
'host',
'') !==
'') {
226 $settings[
'dsn'] .=
'host='.$_POST[$prefix.
'_dsn'][
'host'].
';';
228 if (array_get_index($_POST[$prefix.
'_dsn'],
'database',
'') !==
'') {
229 $settings[
'dsn'] .=
'dbname='.$_POST[$prefix.
'_dsn'][
'database'].
';';
232 $settings[
'dsn'] = trim($settings[
'dsn'],
';');
234 $settings[
'db_type'] = array_get_index($_POST[$prefix.
'_dsn'],
'db_type',
'');
235 $settings[
'database'] = array_get_index($_POST[$prefix.
'_dsn'],
'database',
'');
236 $settings[
'host'] = array_get_index($_POST[$prefix.
'_dsn'],
'host',
'');
238 $settings[
'username'] = array_get_index($_POST, $prefix.
'_username',
'');
239 $settings[
'password'] = array_get_index($_POST, $prefix.
'_password',
'');
243 $settings[
'dsn'] = $_POST[$prefix.
'_complete_dsn'];
244 $settings[
'username'] = array_get_index($_POST, $prefix.
'_username',
'');
245 $settings[
'password'] = array_get_index($_POST, $prefix.
'_password',
'');
249 $settings[
'connector_assetid'] = $_POST[$prefix.
'_connector'][
'assetid'];
253 $settings[
'sql'] = array_get_index($_POST, $prefix.
'_sql',
'');
267 self::_fillDefaults($settings);
269 ?><table
class=
"no-borders">
276 <td
class=
"sq-backend-table-cell" style=
"vertical-align: top"><p><strong>Data Source:</strong></p></td>
277 <td
class=
"sq-backend-table-cell" style=
"vertical-align: top"><p><?php
278 if (!empty($settings[
'dsn'])) {
279 echo htmlspecialchars($settings[
'dsn']);
280 }
else if (!empty($settings[
'connector_assetid'])) {
281 echo get_asset_tag_line($settings[
'connector_assetid']);
283 ?><span
class=
"sq-backend-warning">No data source specified.</span><?php
287 if (!empty($settings[
'dsn'])) {
289 <td
class=
"sq-backend-table-cell" style=
"vertical-align: top"><p><strong>
User Name:</strong></p></td>
290 <td
class=
"sq-backend-table-cell" style=
"vertical-align: top"><p><?php
291 if (!empty($settings[
'username'])) {
292 echo htmlspecialchars($settings[
'username']);
293 if (!empty($settings[
'password'])) {
294 ?> <em>(password supplied)</em><?php
297 ?>No user name specified.<?php
303 <td
class=
"sq-backend-table-cell" style=
"vertical-align: top"><p><strong>SQL
Query:</strong></p></td>
304 <td
class=
"sq-backend-table-cell" style=
"vertical-align: top"><p><?php
305 if (!empty($settings[
'sql'])) {
306 echo ellipsisize(preg_replace(
'/\\n/',
' ', htmlspecialchars($settings[
'sql'])), 512);
308 ?><span
class=
"sq-backend-warning">No SQL query specified.</span><?php
326 $db = self::_connectToDB($form, $settings);
327 if ($db === FALSE)
return FALSE;
328 $sql = trim($settings[
'sql'],
';');
336 $found = preg_match_all(
'/%(response_(\\d+_)?q\\d+(_raw)?)%/U', $sql, $set_matches, PREG_SET_ORDER);
337 $matches = array_merge($matches, $set_matches);
338 $found = preg_match_all(
'/%(form_submission_.*)%/U', $sql, $set_matches, PREG_SET_ORDER);
339 $matches = array_merge($matches, $set_matches);
342 $bind_vars = Array();
343 foreach ($matches as $match) {
344 if (empty($match))
continue;
347 $sql = preg_replace(
'/%'.$match[1].
'%/U',
':response_'.$i, $sql, 1, $count);
352 }
while ($count > 0);
358 foreach ($bind_vars as $bind_var => $bind_value) {
363 }
catch (Exception $e) {
364 trigger_localised_error(
'DATA0001', E_USER_WARNING, $e->getMessage(), $sql);
379 self::_fillDefaults($settings);
382 if (empty($settings[
'dsn']) && empty($settings[
'connector_assetid'])) {
387 if (empty($settings[
'sql'])) {
403 private static function _fillDefaults(&$settings) {
404 if (!isset($settings[
'method'])) {
405 $settings[
'method'] =
'table';
408 if (!isset($settings[
'db_type'])) {
409 $settings[
'db_type'] =
'';
412 if (!isset($settings[
'database'])) {
413 $settings[
'database'] =
'';
416 if (!isset($settings[
'host'])) {
417 $settings[
'host'] =
'';
420 if (!isset($settings[
'dsn'])) {
421 $settings[
'dsn'] =
'';
424 if (!isset($settings[
'username'])) {
425 $settings[
'username'] =
'';
428 if (!isset($settings[
'password'])) {
429 $settings[
'password'] =
'';
432 if (!isset($settings[
'sql'])) {
433 $settings[
'sql'] =
'';
436 if (!isset($settings[
'connector_assetid'])) {
437 $settings[
'connector_assetid'] = 0;
448 private static function _connectToDB(
Form $form, $settings)
450 if (!empty($settings[
'dsn'])) {
451 $dsn_string = $settings[
'dsn'];
452 $username = $settings[
'username'];
453 $password = $settings[
'password'];
455 $db_type = substr($dsn_string, 0, strpos($dsn_string,
':'));
458 'DSN' => $dsn_string,
460 'password' => $password,
464 }
else if (!empty($settings[
'connector_assetid'])) {
465 $connector = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($settings[
'connector_assetid']);
466 $dsn = $connector->getDSN();