Subversion Repositories php_library

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
<?php
2
 
3
   class QueryParameters {
4
      protected $parameter;
5
      protected $prompt;
6
      protected $query;
7
      protected $displayQuery;
8
      protected $value;
9
      private $parametersDelimiter = '++';
10
 
11
      function __constructor ( $parameter = '', $prompt = '', $query = '' ) {
12
         $this->parameter = $parameter;
13
         $this->prompt = $prompt;
14
         $this->query = $query;
15
      }
16
 
17
      function stringToParameter( $string ) {
18
         $temp = explode( $this->parametersDelimiter, $string );
19
         $this->parameter = $temp[0];
20
         $this->prompt = $temp[1];
21
         $this->query = $temp[2];
22
         $this->displayQuery = $temp[3];
23
      }
24
 
25
      function name ( $newParameter = '' ) {
26
         $oldParameter = $this->parameter;
27
         if ($newParameter) {
28
            $this->parameter = $newParameter;
29
         }
30
         return $oldParameter;
31
      }
32
 
33
      function value ( $newValue = '' ) {
34
         $oldValue = $this->value;
35
         if ($newValue) {
36
            $this->value = $newValue;
37
         }
38
         return $oldValue;
39
      }
40
 
41
      function prompt ( $newPrompt = '' ) {
42
         $oldPrompt = $this->prompt;
43
         if ($newPrompt) {
44
            $this->prompt = $newPrompt;
45
         }
46
         return $oldPrompt;
47
      }
48
 
49
      function query ( $newQuery = '' ) {
50
         $oldQuery = $this->query;
51
         if ($newQuery) {
52
            $this->query = $newQuery;
53
         }
54
         return $oldQuery;
55
      }
56
 
57
      function displayQuery ( $newQuery = '' ) {
58
         $oldQuery = $this->displayQuery;
59
         if ($newQuery) {
60
            $this->displayQuery = $newQuery;
61
         }
62
         return $oldQuery;
63
      }
64
 
65
      function toHTML () {
66
         $result = "<td>$this->prompt</td><td>";
67
         if ( $this->query ) {
68
            $result .= "<select name='report_param_$this->parameter'>" . queryToSelect( $this->query ) . '</select>';
69
         } else {
70
            $result .= "<input type='text' name='$this->parameter'>";
71
         }
72
         $result .= '</td>';
73
         return $result;
74
      }
75
   } // class QueryParameters
76
 
77
   class Report {
78
      protected $name;
79
      protected $title;
80
      protected $query;
81
      protected $parameters = array();
82
      private $tableName = 'report';
83
      private $nameField = 'name';
84
      private $queryField = 'query';
85
      private $parametersField = 'parameters';
86
      private $idField = 'report_id';
87
 
88
 
89
      static public function listAllReports( $tableName = 'report', $nameField = 'name', $idField = 'report_id' ) {
90
         return queryToSelect( "select $idField, $nameField from $tableName" );
91
      }
92
 
93
      function __constructor ($name = '', $title = '', $query = '', $parameters = array() ) {
94
         $this->name = $name;
95
         $this->title = $title;
96
         $this->query = $query;
97
         $this->parameters = $parameters;
98
      }
99
 
100
      function loadFromDatabase ( $id ) {
101
         $sql = "select $this->nameField, $this->queryField, $this->parametersField from $this->tableName where $this->idField = $id";
102
         $result = queryDatabaseExtended( $sql );
103
         $result = $result['data'][0];
104
         $this->name = $result[$this->nameField];
105
         $this->title = $result[$this->nameField];
106
         $this->query = $result[$this->queryField];
107
         $parameters = $result[$this->parametersField];
108
         $parameters = explode("\n", $parameters);
109
         foreach ( $parameters as $thisParam ) {
110
            if (! $thisParam ) {
111
               continue;
112
            }
113
            $temp = new QueryParameters();
114
            $temp->stringToParameter($thisParam);
115
            $this->parameters[] = $temp;
116
         }
117
      }
118
 
119
      function name ( $newName = '' ) {
120
         $oldName = $this->name;
121
         if ($newName) {
122
            $this->name = $newName;
123
         }
124
         return $oldName;
125
      }
126
 
127
      function query ( $newQuery = '' ) {
128
         $oldQuery = $this->query;
129
         if ($newQuery) {
130
            $this->query = $newQuery;
131
         }
132
         return $oldQuery;
133
      }
134
 
135
      function parameters ( $newParameters = array() ) {
136
         $oldParameters = $this->parameters;
137
         if ($newParameters) {
138
            $this->parameters = $newParameters;
139
         }
140
         return $oldParameters;
141
      }
142
 
143
      function addParameter ( $newParameter = array() ) {
144
         $this->parameters[] = $newParameter;
145
      }
146
 
147
      function run( $parameters = '') {
148
         $conditions = array();
149
         if ( ! $parameters ) { // try to get them from the POST line
150
            foreach ( $this->parameters as $parameter ) {
151
               if ($_POST['report_param_' . $parameter->name()]) {
152
                  $parameter->value($_POST['report_param_' . $parameter->name()]);
153
               }
154
            }
155
         }
156
         // print "<pre>" . print_r( $this ); print "</pre>";
157
         foreach ( $this->parameters as $parameter ) {
158
            $toFind = '<' . $parameter->name() . '>';
159
            $this->query = preg_replace( "/$toFind/", $parameter->value(), $this->query );
160
            if ( $parameter->displayQuery() ) {
161
               //$result = preg_replace( '/<value>/', $parameter->value(), $parameter->displayQuery() );
162
               $result = queryDatabaseExtended(preg_replace( '/<value>/', $parameter->value(), $parameter->displayQuery() ), false);
163
               //print "<pre>" . print_r( $result ); print "</pre>";
164
               $conditions[] = $parameter->name() . ' - '  . $result['data'][0][0];
165
            } else {
166
               $conditions[] = $parameter->name() . ' - ' . $parameter->value();
167
            }
168
         }
169
         return '<h1>' . $this->title . '</h1>' . '<h2>' . implode('</h2><h2>', $conditions) . '</h2>' . queryToTable($this->query);
170
      }
171
 
172
      function toHTML( ) {
173
         $result = "<table border='1'>\n";
174
         $result .= "<tr><td colspan='2'>$this->name</td></tr>";
175
         foreach ( $this->parameters as $parameter ) {
176
            $result .= '<tr>' . $parameter->toHTML() . '</tr>';
177
         }
178
         return $result . '<table>';
179
      }
180
 
181
   } // class Report
182
 
183
?>