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

13
  private $database = 'default';
metzlerd's avatar
metzlerd committed
14

15
  /**
16
   * Object constructor
17
   *
18
19
   * @param unknown_type $uri Database connection string.
   * @param string $repos_path Path to location of data block definitions
20
   */
21
22
  public function __construct($conf, $repos_path, $name) {
    parent::__construct($conf, $repos_path, $name);
23
    if (@$conf['database'] && @$conf['database'] != 'default')  {
24
25
      $this->database = $conf['database'];
    }
metzlerd's avatar
metzlerd committed
26
27
    // Set up the stuff required to translate.
    $this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this);
28
  }
29
  /**
metzlerd's avatar
metzlerd committed
30
   * Get data based on file data block in the repository.
31
32
33
34
35
   *
   * @param String $block_name
   * @param Array $parm_data
   * @param Query $subQuery
   */
36
  public function sqlData($sql) {
37
38
39
    if ($this->database != 'default') {
      db_set_active($this->database);
    }
40
    // Load the block from the file
metzlerd's avatar
metzlerd committed
41
    $xml ='';
42
43
    $sql = $this->te->replace($sql);
    $rs = db_query($sql);
metzlerd's avatar
metzlerd committed
44

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

metzlerd's avatar
metzlerd committed
49
50
51
52
      foreach ($data as $key => $value) {
        $row_node->addChild($key, htmlspecialchars($value));
        //$row_node->$key = $value;
      }
53
    }
54
55
56
    if ($this->database != 'default') {
      db_set_active();
    }
57
58
59
60
    if ($this->debug) {
      $d = ($xml) ? htmlspecialchars($xml->asXML()) : '';
      $this->debug('SQL: ' . $sql, '<pre> SQL:' . $sql . "\n XML: " . $d . "\n</pre>");
    }
metzlerd's avatar
metzlerd committed
61
    return $xml;
62
  }
metzlerd's avatar
metzlerd committed
63

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

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