FrxDrupal.inc 2.73 KB
Newer Older
metzlerd's avatar
metzlerd committed
1
<?php
metzlerd's avatar
metzlerd committed
2
3
4
5
6
7
/**
 * @file
 * Provides data blocks for native drupal connections using the default 
 * drupal connections.
 *
 */
metzlerd's avatar
metzlerd committed
8
class FrxDrupal extends FrxDataProvider {
metzlerd's avatar
metzlerd committed
9
  /**
10
11
	 * Implements hooks into the drupal applications
	 */
metzlerd's avatar
metzlerd committed
12
 
13
14
  private $db;
  
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
23
24
  public function __construct($conf, $repos_path) {
    parent::__construct($conf, $repos_path);
     
    // Set up the stuff required to translate. 
metzlerd's avatar
metzlerd committed
25
    $this->te = new FrxSyntaxEngine(FRX_SQL_TOKEN, ':', $this); 
26
  }
27
28
29
30
31
32
33
  /**
   * Get data based on file data block in the repository. 
   *
   * @param String $block_name
   * @param Array $parm_data
   * @param Query $subQuery
   */
metzlerd's avatar
metzlerd committed
34
  public function data($block_name, $params=array(), $clause='') {
35
36
    // Load the block from the file

metzlerd's avatar
metzlerd committed
37
    $block = $this->load_block($block_name, $clause); 
38
39
40
    $xml =''; 
    if ($block['source'] && $this->access($block['access'])) { 
      $sql = $block['source']; 
metzlerd's avatar
metzlerd committed
41
42
      if ($clause) {
        $sql = 'SELECT * FROM ('. trim($sql, ' ;') . ') forena_table '. $clause; 
metzlerd's avatar
metzlerd committed
43
      }      
metzlerd's avatar
metzlerd committed
44
      $sql = $this->te->replace($sql, $params);       
metzlerd's avatar
metzlerd committed
45
46
      $rs = db_query($sql);
      	
47
      $xml = new SimpleXMLElement('<table/>'); 
metzlerd's avatar
metzlerd committed
48
      while ($data = db_fetch_object($rs)) {
49
50
          $row_node = $xml->addChild('row'); 
          
metzlerd's avatar
metzlerd committed
51
          foreach ($data as $key => $value) {
52
53
            $row_node->addChild($key, htmlspecialchars($value)); 
            //$row_node->$key = $value; 
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
          }
      }
        
    }

    return $xml;   
  }
  
  /**
   * Implement custom SQL formatter to make sure that strings are properly escaped.
   * Ideally we'd replace this with something that handles prepared statements, but it 
   * wouldn't work for  
   *
   * @param unknown_type $value
   * @param unknown_type $key
   * @param unknown_type $data
   */
  public function format($value, $key, $data) { 
metzlerd's avatar
metzlerd committed
72
    $value =  "'". db_escape_string($value) ."'"; 
73
74
75
    return $value; 
  }
  
76
77
78
79
80
  /**
   * @section 
   * Formatters 
   * Below here are formatting types
   */
81
  public function formats() {
82
83
    $formats = array('drupal_filter' => 'Drupal Input Filter' 
                     ); 
84
85
    return $formats; 
  }
metzlerd's avatar
metzlerd committed
86

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  
  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) {
      return $node->teaser;  
    }
    return $node->body;
  }  
}