Correcting time entry slowness
[wrms.git] / inc / WorkSystem.class
blobeef87c4e02fd668d857684584f7c7bf6aa506fc9
1 <?php
2 /////////////////////////////////////////////////////////////
3 //   C L A S S   F O R   W O R K S Y S T E M
4 /////////////////////////////////////////////////////////////
5 function clean_system_code( $dirty ) {
6   return str_replace("'","",str_replace("\\","",trim("$dirty")));
9 class WorkSystem {
10   var $system_id;
11   var $new_record;
12   var $chtype;
14   function WorkSystem( $id ) {
15     global $session;
16     if ( !$session->logged_in ) return false;
17     $this->new_record = true;
19     $id = intval("$id");
20     if ( $id > 0 ) {
21       // Try and load it from file
22       $this->ReadWorkSystem($id);
23     }
24     else if ( $session->AllowedTo('Admin') || $session->AllowedTo('Support') ) {
25       $session->Dbg("WorkSystem", "Initialising new work_system values");
26       $GLOBALS['edit'] = 1;
27       $this->new_record = true;
29       $this->active = true;
30       $this->organisation_specific = false;
32       // Assign some template-based defaults
33       if ( isset($_GET['user_template']) ) {
34         // templates aren't done yet :-(
35       }
36     }
37     elseif ( $this->system_id == "" ) {
38       return false;
39     }
40   }
42   /////////////////////////////////////////////////////////////
43   // ReadWorkSystem - Read the work_system
44   /////////////////////////////////////////////////////////////
45   function ReadWorkSystem( $id )  {
46     global $session, $client_messages, $debuggroups;
48     // Try and load it from file
49     $sql = "SELECT work_system.* ";
50     $sql .= " FROM work_system ";
51     $sql .= " WHERE work_system.system_id = $id";
53     if ( $qry = new PgQuery($sql) ) {
54       if ( $qry->Exec("newWorkSystem")
55               && $qry->rows == 1 && $row = $qry->Fetch() ) {
57         $this->system_id = $row->system_id;
58         if ( !$this->AllowedTo('view') ) {
59           unset($GLOBALS['edit']);
60           $this->new_record = true;
61           $this->system_id = "";
62           $client_messages[] = "You may not view this system, or create new systems.";
63           return;
64         }
65         $this->new_record = false;
66         while( list($k,$v) = each($row) ) {
67           if ( isset($debuggroups['WorkSystem']) && $debuggroups['WorkSystem'] ) {
68             $session->Dbg( "WorkSystem", "\$this->{'%-25.25s = %s", $sysabbr, "$k'}", $v );
69           }
70           $this->{$k} = $v;
71         }
72       }
73       else {
74         if ( !$this->AllowedTo('create') ) {
75           unset($GLOBALS['edit']);
76           $client_messages[] = "You may not view this system, or create new systems.";
77         }
78         $this->new_record = true;
79       }
80     }
81   }
83   /////////////////////////////////////////////////////////////
84   // AllowedTo - Can the user do that to this work_system
85   /////////////////////////////////////////////////////////////
86   function AllowedTo( $action ) {
87     global $session, $debuggroups;
89     $answer = false;
90     if ( $session->AllowedTo('Admin') || $session->AllowedTo('Support') ) {
91       $answer = true;  // Of course they can!
92     }
93     elseif ( $action == 'view' && isset($session->system_roles[$this->system_id]) ) {
94       $answer = true;
95     }
96     if ( isset($debuggroups['WorkSystem']) && $debuggroups['WorkSystem'] ) {
97       $roles = "";
98       foreach( $session->system_roles AS $k => $v ) { $roles .= ($roles == "" ? "":",") . "$k=$v"; }
99       $session->Log("WS::AllowedTo: action '%s' to %s, System:%s, SysRole:%s, Roles:%s - result is: %d",
100                         $action, $session->username, $this->system_id, $session->system_roles[$this->system_id],
101                         $roles, $answer );
102     }
103     return $answer;
104   }
106   /////////////////////////////////////////////////////////////
107   // Render - Return HTML to show the W/R
108   //   A separate function is called for each logical area
109   //   on the W/R.
110   /////////////////////////////////////////////////////////////
111   function Render( ) {
113     if ( ! $this->AllowedTo('view') ) {
114       return;
115     }
116     $uri = "/system.php?" . ($GLOBALS['edit']?'edit=1&':'') . "system_id=$this->system_id";
118     $ef = new EntryForm( $uri, $this, $GLOBALS['edit'] );
119     $ef->NoHelp();  // Prefer this style, for the moment
121     $html = "";
122     if ( $ef->EditMode ) {
123       $html .= $ef->StartForm( array( /*"onsubmit" => "return CheckWorkSystemForm();" */ ) );
124     }
126     $html .= "<table width=\"100%\" class=\"data\" cellspacing=\"0\" cellpadding=\"0\">\n";
128     $html .= $this->RenderDetails($ef);
129     $html .= $this->RenderOrganisations($ef);
131     $html .= "</table>\n";
132     if ( $ef->EditMode ) {
133       $html .= '<div id="footer">';
134       $html .= $ef->SubmitButton( "submit", ($this->new_record ? "Create" : "Update") );
135       $html .= '</div>';
136       $html .= $ef->EndForm();
137     }
139     return $html;
140   }
143   function RenderDetails( $ef ) {
144     global $session;
145     $html = "";
146     $html .= $ef->BreakLine("System Details");
148   if ( !$this->new_record ) {
149     $html .= $ef->DataEntryLine( "System ID", "$this->system_id");
150   }
152     $html .= $ef->DataEntryLine( "System Code", "%s", "text", "system_code",
153               array( "size" => 10, "title" => "The code for the system.") );
155     // Name
156     $html .= $ef->DataEntryLine( "Description", "%s", "text", "system_desc",
157               array( "size" => 70, "title" => "The description of the system.") );
159     // Active?
160     $html .= $ef->DataEntryLine( "Active?", ($this->active == 't' ? "Active" : "Inactive"), "checkbox", "active",
161               array("title" => "Is this system active?") );
163     // Organisation Specific?
164     $html .= $ef->DataEntryLine( "Specific Org?", ($this->organisation_specific == 't' ? "Specific" : "General"), "checkbox", "organisation_specific",
165               array("title" => "Is this system specific to a particular organisation?",
166                     "_label" => "This system applies to one particular organisation only") );
168     return $html;
169   }
171   function RenderOrganisations( $ef ) {
172     global $session;
173     $html = "";
174     $sql = "SELECT organisation.*, ";
175     $sql .= "exists( SELECT 1 from org_system WHERE org_system.org_code = organisation.org_code AND system_id = ? ) AS applies ";
176     if ( $ef->EditMode && ($session->AllowedTo('Admin') || $session->AllowedTo('Support')) ) {
177       $sql .= "FROM organisation ";
178       $sql .= "WHERE active ";
179     }
180     elseif ( $session->AllowedTo('Admin') || $session->AllowedTo('Support') ) {
181       $sql .= "FROM organisation JOIN org_system ON ( organisation.org_code = org_system.org_code ) ";
182       $sql .= "WHERE active AND org_system.system_id = ? ";
183     }
184     else {
185       $sql .= "FROM organisation JOIN org_system ON ( organisation.org_code = org_system.org_code ) ";
186       $sql .= "WHERE active AND org_system.org_code = $session->org_code ";
187       $sql .= "AND org_system.system_id = ? ";
188     }
189     $sql .= "ORDER BY org_name";
191     # Select the records
192     $q = new PgQuery($sql, $this->system_id, $this->system_id);
193     $column = 0;
194     if ( $q && $q->Exec("WS::RndrOrgs") && $q->rows ) {
195       $html .= $ef->BreakLine("Active Organisations");
196       $html .= '<tr><td colspan="2"><table width="100%">'."\n";
198       while( $row = $q->Fetch() ) {
199         if ( $column % 3 == 0 ) $html .= "<tr>";
200         if ( trim($row->org_name) == "" ) $row->org_name = "&lt;&lt;&lt;unknown&gt;&gt;&gt;";
201         $html .= "<td width=\"33%\">";
202         if ( $ef->EditMode  && ($session->AllowedTo('Admin') || $session->AllowedTo('Support')) ) {
203           $ef->record->applies[$row->org_code] = "$row->applies";
204           $html .= sprintf("<label style=\"color: %s\">", ( "$row->applies" == "t" || "$row->applies" == "on" || intval("$row->applies") != 0 ? "red" : "black") );
205           $html .= $ef->DataEntryField( "", "checkbox", "applies[$row->org_code]",
206                 array("title" => "Is this organisation active for this work_system?" ) );
207           $html .= " $row->org_name</label>";
208         }
209         else {
210           $html .= "<a href=\"/org.php?org_code=$row->org_code\">$row->org_name</a>";
211         }
212         $html .= "</td>";
213         if ( ++$column % 3 == 0 ) $html .= "</tr>";
214       }
215     }
216     while ( $column % 3 != 0 ) {
217       $html .= "<td></td>";
218       if ( ++$column % 3 == 0 ) $html .= "</tr>";
219     }
220     $html .= '</table></td></tr>'."\n";
222     return $html;
223   }
226   function Validate( ) {
227     global $session, $client_messages;
228     error_log("$system_name: vpw: DBG: Validating work_system");
230     if ( isset($_POST) ) {
231       if ( ! $this->AllowedTo('update') ) {
232         $client_messages[] = "You may not update this system.";
233         return false;
234       }
236       $valid = true;
237       $_POST['active'] = ( isset($_POST['active']) ? $_POST['active'] : 'f' );
239       if ( trim($_POST['system_code']) == "" ) {
240         $client_messages[] = "ERROR: The system code may not be blank.";
241         $valid = false;
242       }
244       if ( trim($_POST['system_desc']) == "" ) {
245         $client_messages[] = "ERROR: The system description may not be blank.";
246         $valid = false;
247       }
249       return $valid;
250     }
252     $client_messages[] = "ERROR: No form data submitted!";
253     return false;
254   }
256   function Write() {
257     global $client_messages, $session;
259     $session->Dbg("WorkSystem", "Writing work_system form details to database");
260     $client_messages[] = "Writing system details to database.";
261     $this->chtype = strtolower($_POST['submit']);
263     $db_errors = false;
264     $qry = new PgQuery("BEGIN");              $qry->Exec("Sys::Write");
266     $sql = sql_from_post( $this->chtype, "work_system", "WHERE system_id=$this->system_id");
267     $qry = new PgQuery($sql);
268     if ( !$qry->Exec("Sys::Write") ) {
269       $client_messages[] = "ERROR: $qry->errorstring";
270       $db_errors = true;
271     }
272     else {
273       if ( "create" == $this->chtype ) {
274         $sql = "SELECT currval('work_system_system_id_seq');";
275         $qry = new PgQuery($sql);                 $qry->Exec("Org::Write");
276         $row = $qry->Fetch(true);    // Fetch results as array
277         $this->system_id = $row[0];
278         $GLOBALS['system_id'] = $this->system_id;
279       }
281       $organisations = "";
282       foreach( $_POST['applies'] AS $k => $v ) {
283         if ( $v && $v != '0' && $v != 'off' ) {
284           $organisations .= ( "$organisations" == "" ? "" : ", " );
285           $organisations .= "'" . str_replace("'","''",str_replace('\\','', $k)) . "'";
286         }
287       }
288       if ( $organisations != "" ) $organisations = "IN ( $organisations )";
290       $sql = "DELETE FROM org_system WHERE system_id = $this->system_id ".($organisations == '' ? '' : "AND org_code NOT $organisations") . "; ";
291       $qry = new PgQuery($sql);
292       if ( !$qry->Exec("Sys::Write") ) $client_messages[] = "ERROR: $qry->errorstring";
294       // And invert that logic for the INSERT
295       $sql = "INSERT INTO org_system (system_id, org_code) ";
296       $sql .= "SELECT $this->system_id AS system_id, org_code ";
297       $sql .= "FROM organisation WHERE NOT EXISTS( SELECT 1 FROM org_system WHERE system_id = $this->system_id ";
298       $sql .= "AND org_system.org_code = organisation.org_code) ".($organisations == '' ? '' : "AND org_code $organisations") . "; ";
299       $qry = new PgQuery($sql);
300       if ( !$qry->Exec("Sys::Write") ) $client_messages[] = "$qry->errorstring";
302     }
303     $qry = new PgQuery("COMMIT; ROLLBACK;");  $qry->Exec("Sys::Write");
305     return true;
306   }