FrxDrupal.inc 6.06 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, $options = array()) {
37
38
39
    if ($this->database != 'default') {
      db_set_active($this->database);
    }
40
41
42
    // Load the types array based on data
    $this->types = isset($options['type']) ? $options['type'] : array();

43
    // Load the block from the file
metzlerd's avatar
metzlerd committed
44
    $xml ='';
45
46
    $sql = $this->te->replace($sql);
    $rs = db_query($sql);
47
    $entity_map = array();
48
    $select_fields = array();
49
50
    $rownum = 0;

51
    $xml = new SimpleXMLElement('<table/>');
52
    if ($rs && $rs->columnCount()) foreach ($rs as $data) {
53
      $rownum++;
metzlerd's avatar
metzlerd committed
54
      $row_node = $xml->addChild('row');
55
        $row_node['num'] = $rownum;
56
57
58
59
      /* If we are querying entities, we will store away IDs
       * for later querying and XML processing in the
       * loadEntitys method
       */
60
61
62
      if (@$options['entity_type']  && @$options['entity_id'] ) {
        $id_key = $options['entity_id'];
        $type = $options['entity_type'];
63
64
        $id = $data->$id_key;
        if ($id) {
65
          $entity_map[$id] = $row_node;
66
          $select_fields[$id] = $data;
67
        }
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
      } else {
        foreach ($data as $key => $value) {
          $row_node->addChild($key, htmlspecialchars($value));
          //$row_node->$key = $value;
        }
      }

    }


    if ($this->database != 'default') {
      db_set_active();
    }
    if ($entity_map) {
83
      $this->loadEntities($type, $entity_map, $select_fields);
84
85
86
87
88
89
90
91
92
93
94
95
96
    }
    if ($this->debug) {
      $d = ($xml) ? htmlspecialchars($xml->asXML()) : '';
      $this->debug('SQL: ' . $sql, '<pre> SQL:' . $sql . "\n XML: " . $d . "\n</pre>");
    }
    return $xml;
  }

  /**
   * Perform a bulk load of entities 100 at a time and then join data back onto the simplexml already created.
   * @param $type Type of entity being loaded.
   * @param $entity_map Map of entity ids to nodes.
   */
97
  private function loadEntities($type, $entity_map, $select_fields) {
98
99
100
101
102
103
104
105
106
107
    // Do these 100 at a time for performance reasons
    $chunks = array_chunk($entity_map, 100, TRUE);
    foreach ($chunks as $chunk) {
      $ids = array_keys($chunk);

      $data = entity_load($type, $ids);

      if ($data) foreach ($data as $id => $e) {
        if (isset($entity_map[$id])) {
          $row_node = $entity_map[$id];
108
          $lang = isset($e->language) ? $e->language : 'und';
109
          foreach ($e as $key => $val) if ($val) {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            if (strpos($key, 'field_') === 0) {
              //$fields = field_get_items('node', $node, $key);
              $field = field_view_field($type, $e, $key);
              $field['#theme'] = array('forena_inline_field');
              $value  = drupal_render($field);
              $f = $row_node->addChild($key, $value);
              if (isset($field['#field_type'])) $f['type'] = $field['#field_type'];
              if (isset($field['#field_name'])) $f['name'] = $field['#field_name'];
            } else if (is_array($val) && isset($val[$lang])) {
              $tmp = $val[$lang][0];
              if (isset($tmp['safe_value'])) {
                $row_node->addChild($key, $tmp['safe_value']);
              } else if (isset($tmp['value'])) {
                $row_node->addChild($key, $tmp['value']);
              }
125
126
127
128
129
130
131
132
            } else if (is_array($val) && isset($val['und'])) {
              $tmp = $val['und'][0];
              if (isset($tmp['safe_value'])) {
                $row_node->addChild($key, $tmp['safe_value']);
              } else if (isset($tmp['value'])) {
                $row_node->addChild($key, $tmp['value']);
              }
            }else if (is_scalar($val)) {
133
134
135
              $row_node->addChild($key, $val);
            }
          }
136
137
138
139
140
141
          // Now add on select fields
          if ($select_fields[$id]) foreach ($select_fields[$id] as $key=>$val) {
            if (!isset($row_node->$key)) {
              $row_node->addChild($key, $val);
            }
          }
142
        }
metzlerd's avatar
metzlerd committed
143
      }
144
    }
145

146
  }
metzlerd's avatar
metzlerd committed
147

148
149
  /**
   * Implement custom SQL formatter to make sure that strings are properly escaped.
metzlerd's avatar
metzlerd committed
150
151
   * Ideally we'd replace this with something that handles prepared statements, but it
   * wouldn't work for
152
153
154
155
156
   *
   * @param unknown_type $value
   * @param unknown_type $key
   * @param unknown_type $data
   */
157
  public function format($value, $key) {
metzlerd's avatar
metzlerd committed
158
    $db = Database::getConnection('default');
159
    $value = $this->parmConvert($key, $value);
metzlerd's avatar
metzlerd committed
160
    if ($db) {
161
      if ($value==='' || $value===NULL)
metzlerd's avatar
metzlerd committed
162
      $value = 'NULL';
metzlerd's avatar
metzlerd committed
163
164
165
166
167
168
169
170
171
172
173
174
175
      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
176
177
              }
              $val .=   $db->quote($v);
metzlerd's avatar
metzlerd committed
178
179
180
181
            }
            $value = $val;
          }
        }
182
183
184
185
186
187
188
189
        elseif (is_int($value)) {
          $value = (int)$value;
          $value = (string)$value;
        }
        elseif (is_float($value)) {
          $value = (float)$value;
          $value = (string)$value;
        }
metzlerd's avatar
metzlerd committed
190
191
192
193
194
        else {
          $value = trim($value);
          $value =    $db->quote($value) ;
        }
      }
metzlerd's avatar
metzlerd committed
195
    }
metzlerd's avatar
metzlerd committed
196
    return $value;
197
  }
metzlerd's avatar
metzlerd committed
198

199
}
metzlerd's avatar
metzlerd committed
200
201
202
203
204
205
206
207
208
209
210
211
212