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

38
    $block = $this->load_block($block_name); 
39
40
41
    $xml =''; 
    if ($block['source'] && $this->access($block['access'])) { 
      $sql = $block['source']; 
metzlerd's avatar
metzlerd committed
42
      $sql = $this->te->replace($sql, $params);
43
44
45
46
47
48
      $rs = db_query($sql); 
      $xml = new SimpleXMLElement('<table/>'); 
      while ($data = db_fetch_object($rs)) { 

          $row_node = $xml->addChild('row'); 
          
metzlerd's avatar
metzlerd committed
49
          foreach ($data as $key => $value) {
50
51
            $row_node->addChild($key, htmlspecialchars($value)); 
            //$row_node->$key = $value; 
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
          }
      }
        
    }

    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
70
    $value =  "'". db_escape_string($value) ."'"; 
71
72
73
    return $value; 
  }
  
74
75
76
77
78
  /**
   * @section 
   * Formatters 
   * Below here are formatting types
   */
79
80
81
82
83
84
85
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
  public function formats() {
    $formats = array('drupal_date_format' => 'Drupal Date', 
                     'drupal_filter' => 'Drupal Input Filter'
 //                    'drupal_node' => 'Node Teaser from node id',
    ); 
    return $formats; 
  }
  
  public function drupal_date_format($value, $format_str) { 
     switch ($format_str) { 
       case 'medium': 
       case 'small':
       case 'large': 
         $type = $format_str;
         $format=''; 
         break; 
       default: 
         $type = 'custom'; 
         $format = $format_str; 
     }
     
     return format_date($value, $type, $format); 
     
  }
  
  public function drupal_filter($nid, $bool) {  	
  	$node = node_load($nid, NULL, TRUE); 
  	if(! $node){
  		//error message goes here
  	}
  	//If true, render the teaser
  	//else render the body
  	$node = node_prepare($node, $bool);
  	
  	if($bool){
      return $node->teaser;  
  	}  	
  	return $node->body;
  }
  

120
121
  
  
122
}