Current code.
[capital-apms.git] / inc / classViewer.php
blob3bf18883430b0845cf747a04796d041b4896c86d
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 protected $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 Title( $new_title = null ) {
93 if ( isset($new_title) ) $this->Title = $new_title;
94 return $this->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;
108 return;
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];
135 $what_part = '';
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 ) {
142 case 'format':
143 $result = sprintf( $modifier, $field_value );
144 return str_replace( "\n", "<br />", $result );
146 case 'yesno':
147 return ( $field_value == 't' ? 'Yes' : 'No' );
149 case 'urlencode':
150 return rawurlencode($field_value);
152 default:
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() {
165 $target_fields = "";
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 ) {
180 global $c, $session;
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);
194 $html .= $replaced;
196 $html .= '</div>';
197 return $html;