FrxDrupal.inc 2.71 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 FrxDataProvider {
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 data($block_name, $params=array(), $clause='') {
36
37
    // Load the block from the file

metzlerd's avatar
metzlerd committed
38
39
40
41
    $block = $this->load_block($block_name, $clause);
    $xml ='';
    if ($block['source'] && $this->access($block['access'])) {
      $sql = $block['source'];
metzlerd's avatar
metzlerd committed
42
      if ($clause) {
metzlerd's avatar
metzlerd committed
43
44
45
        $sql = 'SELECT * FROM (' . trim($sql, ' ;') . ') forena_table ' . $clause;
      }
      $sql = $this->te->replace($sql, $params);
metzlerd's avatar
metzlerd committed
46
      $rs = db_query($sql);
metzlerd's avatar
metzlerd committed
47
48

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

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

58
59
    }

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

63
64
  /**
   * Implement custom SQL formatter to make sure that strings are properly escaped.
metzlerd's avatar
metzlerd committed
65
66
   * Ideally we'd replace this with something that handles prepared statements, but it
   * wouldn't work for
67
68
69
70
71
   *
   * @param unknown_type $value
   * @param unknown_type $key
   * @param unknown_type $data
   */
metzlerd's avatar
metzlerd committed
72
  public function format($value, $key, $data) {
metzlerd's avatar
metzlerd committed
73
74
75
76
    $db = Database::getConnection('default');
    if ($db) {
      if ($value=='') $value = 'NULL'; else  $value =  $db -> quote($value);
    }
metzlerd's avatar
metzlerd committed
77
    return $value;
78
  }
metzlerd's avatar
metzlerd committed
79

80
  /**
metzlerd's avatar
metzlerd committed
81
82
   * @section
   * Formatters
83
84
   * Below here are formatting types
   */
85
  public function formats() {
metzlerd's avatar
metzlerd committed
86
87
88
    $formats = array('drupal_filter' => 'Drupal Input Filter'
                     );
    return $formats;
89
  }
metzlerd's avatar
metzlerd committed
90

metzlerd's avatar
metzlerd committed
91

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  public function drupal_filter($nid, $format) {
    $node = node_load($nid, NULL, TRUE);
    if (!$node) {
      return 'N/A';
    }
    if ((strcasecmp($format, 'teaser') == 0) || (!$format)) {
      $bool = TRUE;
    }
    else {
      $bool = FALSE;
    }
    //If true, render the teaser
    //else render the body
    $node = node_prepare($node, $bool);
    if ($bool) {
metzlerd's avatar
metzlerd committed
107
      return $node->teaser;
108
109
    }
    return $node->body;
metzlerd's avatar
metzlerd committed
110
  }
111
}
metzlerd's avatar
metzlerd committed
112
113
114
115
116
117
118
119
120
121
122
123
124