6 * @subpackage classViewer
7 * @author Andrew McMillan <andrew@catalyst.net.nz>
8 * @copyright Catalyst IT Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2
13 * First we need a class for the fields in the viewer
15 * @subpackage classViewer
23 function ViewerField( $field, $sql="" ) {
24 $this->Field
= $field;
28 function Set($value) {
29 $this->Value
= $value;
32 function GetTarget() {
33 if ( $this->Sql
== "" ) return $this->Field
;
34 return "$this->Sql AS $this->Field";
40 * Class for the actual viewer
42 * @subpackage classViewer
57 function Viewer( $title = "", $fields = null ) {
59 $this->Title
= $title;
64 if ( isset($fields) ) {
65 if ( is_array($fields) ) {
66 foreach( $fields AS $k => $v ) {
70 else if ( is_string($fields) ) {
71 // We've been given a table name, so get all fields for it.
72 $this->Joins
= $fields;
73 $field_list = get_fields($fields);
74 foreach( $field_list AS $k => $v ) {
75 $this->AddField($k, "$fields.$k");
80 $session->Log("DBG: New viewer called $title");
83 function AddField( $field, $sql="" ) {
84 $this->Fields
[$field] = new ViewerField( $field, $sql );
85 $this->OrderedFields
[] = $field;
88 function SetJoins( $join_list ) {
89 $this->Joins
= $join_list;
92 function Title( $new_title = null ) {
93 if ( isset($new_title) ) $this->Title
= $new_title;
97 function SetTitle( $new_title ) {
98 $this->Title
= $new_title;
101 function SetWhere( $where_clause ) {
102 $this->Where
= $where_clause;
105 function MoreWhere( $operator, $more_where ) {
106 if ( $this->Where
== "" ) {
107 $this->Where
= $more_where;
110 $this->Where
= "$this->Where $operator $more_where";
113 function AndWhere( $more_where ) {
114 $this->MoreWhere("AND",$more_where);
117 function OrWhere( $more_where ) {
118 $this->MoreWhere("OR",$more_where);
121 function SetTemplate( $template ) {
122 $this->Template
= $template;
127 * Callback function used for replacing parts into the template
129 function ReplaceFieldPart($matches)
131 // $matches[0] is the complete match
132 // $matches[1] the match for the first subpattern
133 // enclosed in '(...)' and so on
134 $field_name = $matches[1];
136 $what_part = (isset($matches[3]) ?
$matches[3] : '');
137 $modifier = (isset($matches[5]) ?
$matches[5] : '');
139 $field_value = (isset($this->Record
->{$field_name}) ?
$this->Record
->{$field_name} : '');
141 switch( $what_part ) {
143 $result = sprintf( $modifier, $field_value );
144 return str_replace( "\n", "<br />", $result );
147 return ( $field_value == 't' ?
'Yes' : 'No' );
150 return rawurlencode($field_value);
153 return str_replace( "\n", "<br />", $field_value );
158 function Value( $value_field_name ) {
159 if ( !isset($this->Record
->{$value_field_name}) ) return null;
160 return $this->Record
->{$value_field_name};
164 function GetRecord() {
166 foreach( $this->Fields
AS $k => $column ) {
167 if ( $target_fields != "" ) $target_fields .= ", ";
168 $target_fields .= $column->GetTarget();
170 $sql = sprintf( "SELECT %s FROM %s WHERE %s %s %s", $target_fields, $this->Joins
, $this->Where
, $this->Order
, $this->Limit
);
171 $this->Query
= new PgQuery( $sql );
172 if ( $this->Query
->Exec("Browse:$this->Title:DoQuery") ) {
173 $this->Record
= $this->Query
->Fetch();
175 return $this->Record
;
179 function Render( $title_tag = null ) {
181 global $ReplaceFields, $ReplaceValues;
183 $session->Log("DBG: Rendering viewer $this->Title");
184 if ( $this->Template
== "" ) $this->DefaultTemplate();
186 $html = '<div id="viewer">';
187 if ( $this->Title
!= "" ) {
188 if ( !isset($title_tag) ) $title_tag = 'h1';
189 $html = "<$title_tag>$this->Title</$title_tag>\n";
192 // Stuff like "##fieldname.part## gets converted to the appropriate value
193 $replaced = preg_replace_callback("/##([^.#]+)(\.([^.#]+)(\.([^#]*))?)?##/", array(&$this,"ReplaceFieldPart"), $this->Template
);