Remove Quanta cruft.
[capital-apms.git] / inc / classViewer.php
blobcfc02cded21280940ca5ee2862734804a5952135
1 <?php
2 /**
3 * APMS Record Views
5 * @package apms
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
12 /**
13 * First we need a class for the fields in the viewer
14 * @package apms
15 * @subpackage classViewer
17 class ViewerField
19 var $Field;
20 var $Sql;
21 var $Value;
23 function ViewerField( $field, $sql="" ) {
24 $this->Field = $field;
25 $this->Sql = $sql;
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";
39 /**
40 * Class for the actual viewer
41 * @package apms
42 * @subpackage classViewer
44 class Viewer
46 var $Title;
47 var $Fields;
48 var $OrderedFields;
49 var $Joins;
50 var $Where;
51 var $Order;
52 var $Limit;
53 var $Query;
54 var $Template;
55 var $Record;
57 function Viewer( $title = "", $fields = null ) {
58 global $c, $session;
59 $this->Title = $title;
60 $this->Order = "";
61 $this->Limit = "";
62 $this->Template = "";
64 if ( isset($fields) ) {
65 if ( is_array($fields) ) {
66 foreach( $fields AS $k => $v ) {
67 $this->AddField($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;
103 return;
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];
130 $what_part = '';
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 ) {
137 case 'format':
138 $result = sprintf( $modifier, $field_value );
139 return str_replace( "\n", "<br />", $result );
141 case 'yesno':
142 return ( $field_value == 't' ? 'Yes' : 'No' );
144 case 'urlencode':
145 return rawurlencode($field_value);
147 default:
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() {
160 $target_fields = "";
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 ) {
175 global $c, $session;
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);
189 $html .= $replaced;
191 $html .= '</div>';
192 return $html;