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 SetTitle( $new_title ) {
93 $this->Title
= $new_title;
96 function SetWhere( $where_clause ) {
97 $this->Where
= $where_clause;
100 function MoreWhere( $operator, $more_where ) {
101 if ( $this->Where
== "" ) {
102 $this->Where
= $more_where;
105 $this->Where
= "$this->Where $operator $more_where";
108 function AndWhere( $more_where ) {
109 $this->MoreWhere("AND",$more_where);
112 function OrWhere( $more_where ) {
113 $this->MoreWhere("OR",$more_where);
116 function SetTemplate( $template ) {
117 $this->Template
= $template;
122 * Callback function used for replacing parts into the template
124 function ReplaceFieldPart($matches)
126 // $matches[0] is the complete match
127 // $matches[1] the match for the first subpattern
128 // enclosed in '(...)' and so on
129 $field_name = $matches[1];
131 $what_part = (isset($matches[3]) ?
$matches[3] : '');
132 $modifier = (isset($matches[5]) ?
$matches[5] : '');
134 $field_value = (isset($this->Record
->{$field_name}) ?
$this->Record
->{$field_name} : '');
136 switch( $what_part ) {
138 $result = sprintf( $modifier, $field_value );
139 return str_replace( "\n", "<br />", $result );
142 return ( $field_value == 't' ?
'Yes' : 'No' );
145 return rawurlencode($field_value);
148 return str_replace( "\n", "<br />", $field_value );
153 function Value( $value_field_name ) {
154 if ( !isset($this->Record
->{$value_field_name}) ) return null;
155 return $this->Record
->{$value_field_name};
159 function GetRecord() {
161 foreach( $this->Fields
AS $k => $column ) {
162 if ( $target_fields != "" ) $target_fields .= ", ";
163 $target_fields .= $column->GetTarget();
165 $sql = sprintf( "SELECT %s FROM %s WHERE %s %s %s", $target_fields, $this->Joins
, $this->Where
, $this->Order
, $this->Limit
);
166 $this->Query
= new PgQuery( $sql );
167 if ( $this->Query
->Exec("Browse:$this->Title:DoQuery") ) {
168 $this->Record
= $this->Query
->Fetch();
170 return $this->Record
;
174 function Render( $title_tag = null ) {
176 global $ReplaceFields, $ReplaceValues;
178 $session->Log("DBG: Rendering viewer $this->Title");
179 if ( $this->Template
== "" ) $this->DefaultTemplate();
181 $html = '<div id="viewer">';
182 if ( $this->Title
!= "" ) {
183 if ( !isset($title_tag) ) $title_tag = 'h1';
184 $html = "<$title_tag>$this->Title</$title_tag>\n";
187 // Stuff like "##fieldname.part## gets converted to the appropriate value
188 $replaced = preg_replace_callback("/##([^.#]+)(\.([^.#]+)(\.([^#]*))?)?##/", array(&$this,"ReplaceFieldPart"), $this->Template
);