Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
data_source_db_edit_fns.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/data_source/data_source/data_source_edit_fns.inc';
19 
32 {
33 
34  public $dsn = NULL;
35  public $result = NULL;
36  public $fields = Array();
37  public $selected_fields = Array();
38  public $group_by = Array();
39  public $conn = NULL;
40 
41 
46  function __construct()
47  {
48  parent::__construct();
49 
50  }//end constructor
51 
52 
63  function paintConnectorLink(&$asset, &$o, $prefix)
64  {
65  // get root links
66  $connector_link = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_NOTICE, 'db_connector', FALSE, 'db_connector');
67  if (empty($connector_link)) {
68  $connector_asset_id = NULL;
69  } else {
70  $connector_asset_id = $connector_link['minorid'];
71  }
72 
73  if ($asset->writeAccess('links')) {
74  asset_finder($prefix.'_connector_node', $connector_asset_id, Array('db_connector'=>'D'));
75  } else {
76  if (empty($connector_asset_id)) {
77  echo 'No connector selected';
78  } else {
79  echo get_asset_tag_line($connector_asset_id);
80  }
81  }
82  return TRUE;
83 
84  }//end paintConnectorLink()
85 
86 
97  function processConnectorLink(&$asset, &$o, $prefix)
98  {
99  $new_connector_id = @$_REQUEST[$prefix.'_connector_node']['assetid'];
100 
101  if (!$asset->writeAccess('links')) {
102  trigger_error('You do not have write access to change the connector link', E_USER_WARNING);
103  return FALSE;
104  }
105 
106  $current_connector_link = $GLOBALS['SQ_SYSTEM']->am->getLink($asset->id, SQ_LINK_NOTICE, 'db_connector', FALSE, 'db_connector');
107  $current_connector_asset_id = @$current_connector_link['minorid'];
108 
109  if (!empty($current_connector_link)) {
110  // if the new connector id is not null and is equal to the current
111  // connector asset id then return
112  if ((!empty($new_connector_id)) && ($current_connector_asset_id === $new_connector_id)) {
113  return TRUE;
114  } else {
115  // else means that we want to create a new link or we want to
116  // clear the link
117  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($current_connector_link['linkid']);
118 
119  // if the new connector id is empty means we want to clear the
120  // link we got nothing else to do so return TRUE
121  if (empty($new_connector_id))return TRUE;
122  }
123  }
124 
125  if (!empty($new_connector_id)) {
126  $new_connector_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($new_connector_id);
127  $result_id = $GLOBALS['SQ_SYSTEM']->am->createAssetLink($asset, $new_connector_asset, SQ_LINK_NOTICE, 'db_connector');
128 
129  if (empty($result_id)) {
130  trigger_error('Could not link the connector to this asset', E_USER_WARNING);
131  return FALSE;
132  }
133 
134  }
135 
136  return TRUE;
137 
138  }//end processConnectorLink()
139 
140 
141  //-- DSN --//
142 
143 
154  function paintDSN(&$asset, &$o, $prefix)
155  {
156  $wa = $asset->writeAccess('attributes');
157  $db_type = $asset->attr('db_type');
158  $database = $asset->attr('database');
159  $host = $asset->attr('host');
160  ?>
161  <table class="sq-backend-table">
162  <tr>
163  <td><?php echo translate('database_type');?></td>
164  <td>
165  <?php
166  $options = Array('postgres' => 'PostgreSQL', 'oracle' => 'Oracle', 'mysql' => 'MySQL');
167  if ($wa) {
168  combo_box('dsn[db_type]', $options, FALSE, $db_type);
169  } else {
170  if (empty($db_type)) {
171  ?><em>Ignored due to Complete DSN entry</em><?php
172  } else {
173  echo $options[$db_type];
174  }
175  }
176  ?>
177  </td>
178  </tr>
179  <tr>
180  <td><?php echo translate('database_name');?></td>
181  <td>
182  <?php
183  if ($wa) {
184  text_box('dsn[database]', $database, 35);
185  } else {
186  echo $database;
187  }
188  ?>
189  </td>
190  </tr>
191  <tr>
192  <td><?php echo translate('host_name');?></td>
193  <td>
194  <?php
195  if ($wa) {
196  text_box('dsn[host]', $host, 35);
197  } else {
198  echo $host;
199  }
200  ?>
201  </td>
202  </tr>
203  </table>
204  <?php
205  if ($wa) {
206  return TRUE;
207  } else {
208  return FALSE;
209  }
210 
211  }//end paintDSN()
212 
213 
224  function processDSN(&$asset, &$o, $prefix)
225  {
226  $dsn = $asset->attr('dsn');
227  $new_dsn_string = NULL;
228  $new_dsn = $_POST['dsn'];
229  if (!empty($_POST['dsn']['dsn'])) {
230  $new_dsn_string = $_POST['dsn']['dsn'];
231  $new_dsn['db_type'] = NULL;
232  $new_dsn['host'] = NULL;
233  $new_dsn['database'] = NULL;
234  } else {
235  switch ($new_dsn['db_type']) {
236  case 'postgres':
237  $new_dsn_string = "pgsql:";
238  break;
239  case 'mysql':
240  $new_dsn_string = "mysql:";
241  break;
242  case 'oracle':
243  $new_dsn_string = "oci:";
244  break;
245  }
246 
247  if ($new_dsn['db_type'] == 'oracle') {
248  // PDO_OCI compatible DSN is either:
249  // oci:dbname=[db]...
250  // oci:dbname=[hostname]/[db]...
251 
252  // Let's provide a PDO_OCI-like DSN and work it for PHP OCI out when connecting
253  $new_dsn_string .= 'dbname=';
254 
255  // Start with the hostname...
256  if (!empty($new_dsn['host'])) {
257  $new_dsn_string .= "$new_dsn[host]/";
258  }
259 
260  // Unless that isn't supplied, in which case we will be using a tnsnames-defined DB only
261  $new_dsn_string .= "$new_dsn[database]";
262  } else {
263  $new_dsn_string .= "dbname=$new_dsn[database];host=$new_dsn[host]";
264  }
265  }
266  if (strcasecmp($dsn, $new_dsn_string) != 0) {
267  foreach ($new_dsn as $key => $value) {
268  $asset->setAttrValue($key, $value);
269  }
270 
271  $asset->setAttrValue('dsn', $new_dsn_string);
272  return TRUE;
273  }
274  return FALSE;
275 
276  }//end processDSN()
277 
278 
289  function paintDSNAlone(&$asset, &$o, $prefix)
290  {
291  $dsn = $asset->attr('dsn');
292  $wa = $asset->writeAccess('attributes');
293  $database = $asset->attr('database');
294  if ($wa) {
295  text_area('dsn[dsn]', empty($database) ? $dsn : '', 70, 5);
296  return TRUE;
297  } else {
298  if (!empty($dsn)) echo $dsn;
299  return FALSE;
300  }
301 
302  }//end paintDSNAlone()
303 
304 
315  function processDSNAlone(&$asset, &$o, $prefix)
316  {
317  return FALSE;
318 
319  }//end processDSNAlone()
320 
321 
322 //-- QUERY --//
323 
324 
335  function paintQuerySoFar(&$asset, &$o, $prefix)
336  {
337  $wa = $asset->writeAccess('attributes');
338  $sql = $asset->attr('sql');
339 
340  if ($wa) {
341  $dynamic_vars = $asset->attr('dynamic_vars');
342  if (!empty($dynamic_vars)) {
343  foreach ($dynamic_vars as $var => $value) {
344  $keywords['%%'.$var.'%%'] = $var;
345  }
346  keyword_selector($keywords, $prefix.'_sql');
347  }
348  text_area($prefix.'_sql', $sql, 100, 40, 0, 'style="width: 100%; height: 30em; font-family: \'Courier New\', Courier, monospace; display: block"');
349  } else {
350  if (!empty($sql)) {
351  echo nl2br($sql);
352  hidden_field($prefix.'_sql', $sql);
353  }
354  }
355  echo('&nbsp;');
356 
357  return $wa;
358 
359  }//end paintQuerySoFar()
360 
361 
372  function processQuerySoFar(&$asset, &$o, $prefix)
373  {
374  if (isset($_POST[$prefix.'_sql'])) {
375  return $asset->setAttrValue('sql', $_POST[$prefix.'_sql']);
376  }
377 
378  return FALSE;
379 
380  }//end processQuerySoFar()
381 
382 
393  function paintDebugInfo(&$asset, &$o, $prefix)
394  {
395  if ($asset->connectToDB()) {
396  echo '<div style="color: green"><b>Connected</b></div>';
397  } else {
398  echo '<div style="color: red"><b>Not connected</b></div>';
399  }
400 
401  if (isset($asset->_tmp['db_conection_exception'])) {
402  echo "<div>Connection failed: ".$asset->_tmp['db_conection_exception']->getMessage()."</div>";
403  }
404 
405  return TRUE;
406 
407  }//end paintDebugInfo()
408 
409 
410 }//end class
411 ?>