FrxDrupal.inc 2.49 KB
Newer Older
metzlerd's avatar
metzlerd committed
1
<?php
metzlerd's avatar
metzlerd committed
2
// $Id$
metzlerd's avatar
metzlerd committed
3
4
/**
 * @file
metzlerd's avatar
metzlerd committed
5
 * Provides data blocks for native drupal connections using the default
metzlerd's avatar
metzlerd committed
6
7
8
 * drupal connections.
 *
 */
metzlerd's avatar
metzlerd committed
9
class FrxDrupal extends FrxDataSource {
metzlerd's avatar
metzlerd committed
10
  /**
11
12
	 * Implements hooks into the drupal applications
	 */
metzlerd's avatar
metzlerd committed
13

14
  private $db;
metzlerd's avatar
metzlerd committed
15

16
  /**
17
   * Object constructor
18
   *
19
20
   * @param unknown_type $uri Database connection string.
   * @param string $repos_path Path to location of data block definitions
21
   */
22
23
  public function __construct($conf, $repos_path) {
    parent::__construct($conf, $repos_path);
metzlerd's avatar
metzlerd committed
24
25
26

    // Set up the stuff required to translate.
    $this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this);
27
  }
28
  /**
metzlerd's avatar
metzlerd committed
29
   * Get data based on file data block in the repository.
30
31
32
33
34
   *
   * @param String $block_name
   * @param Array $parm_data
   * @param Query $subQuery
   */
metzlerd's avatar
metzlerd committed
35
  public function sqlData($block_name) {
36
    // Load the block from the file
metzlerd's avatar
metzlerd committed
37
    $block = $this->loadBlock($block_name);
metzlerd's avatar
metzlerd committed
38
    $xml ='';
metzlerd's avatar
metzlerd committed
39
    if (trim($block['source']) && $this->access($block['access'])) {
metzlerd's avatar
metzlerd committed
40
      $sql = $block['source'];
metzlerd's avatar
metzlerd committed
41
      if ($clause) {
metzlerd's avatar
metzlerd committed
42
43
        $sql = 'SELECT * FROM (' . trim($sql, ' ;') . ') forena_table ' . $clause;
      }
44
      $sql = $this->te->replace($sql);
metzlerd's avatar
metzlerd committed
45
      $rs = db_query($sql);
metzlerd's avatar
metzlerd committed
46
47

      $xml = new SimpleXMLElement('<table/>');
metzlerd's avatar
metzlerd committed
48
      foreach ($rs as $data) {
metzlerd's avatar
metzlerd committed
49
50
          $row_node = $xml->addChild('row');

metzlerd's avatar
metzlerd committed
51
          foreach ($data as $key => $value) {
metzlerd's avatar
metzlerd committed
52
53
            $row_node->addChild($key, htmlspecialchars($value));
            //$row_node->$key = $value;
54
55
          }
      }
metzlerd's avatar
metzlerd committed
56

57
58
    }

metzlerd's avatar
metzlerd committed
59
    return $xml;
60
  }
metzlerd's avatar
metzlerd committed
61

62
63
  /**
   * Implement custom SQL formatter to make sure that strings are properly escaped.
metzlerd's avatar
metzlerd committed
64
65
   * Ideally we'd replace this with something that handles prepared statements, but it
   * wouldn't work for
66
67
68
69
70
   *
   * @param unknown_type $value
   * @param unknown_type $key
   * @param unknown_type $data
   */
metzlerd's avatar
metzlerd committed
71
  public function format($value, $key, $data) {
metzlerd's avatar
metzlerd committed
72
73
    $db = Database::getConnection('default');
    if ($db) {
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	    if ($value=='')
	      $value = 'NULL';
	    else {
	      if (is_array($value)) {
	      	if ($value == array()) {
	      		$value = 'NULL';
	      	}
	      	else {
	      		// Build a array of values string
		      	$i=0;
		      	$val = '';
		      	foreach ($value as $v) {
		      		$i++;
		      	  if ($i>1) {
		    			  $val .= ',';
		     		  }
		    		$val .=   $db->quote($v);
		    	  }
		    	  $value = $val;
	        }
	      }
	      else {
	        $value = trim($value);
	        $value =    $db->quote($value) ;
	      }
	    }
metzlerd's avatar
metzlerd committed
100
    }
metzlerd's avatar
metzlerd committed
101
    return $value;
102
  }
metzlerd's avatar
metzlerd committed
103

104
}
metzlerd's avatar
metzlerd committed
105
106
107
108
109
110
111
112
113
114
115
116
117