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")));
14 function WorkSystem( $id ) {
16 if ( !$session->logged_in ) return false;
17 $this->new_record = true;
21 // Try and load it from file
22 $this->ReadWorkSystem($id);
24 else if ( $session->AllowedTo('Admin') || $session->AllowedTo('Support') ) {
25 $session->Dbg("WorkSystem", "Initialising new work_system values");
27 $this->new_record = true;
30 $this->organisation_specific = false;
32 // Assign some template-based defaults
33 if ( isset($_GET['user_template']) ) {
34 // templates aren't done yet :-(
37 elseif ( $this->system_id == "" ) {
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.";
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 );
74 if ( !$this->AllowedTo('create') ) {
75 unset($GLOBALS['edit']);
76 $client_messages[] = "You may not view this system, or create new systems.";
78 $this->new_record = true;
83 /////////////////////////////////////////////////////////////
84 // AllowedTo - Can the user do that to this work_system
85 /////////////////////////////////////////////////////////////
86 function AllowedTo( $action ) {
87 global $session, $debuggroups;
90 if ( $session->AllowedTo('Admin') || $session->AllowedTo('Support') ) {
91 $answer = true; // Of course they can!
93 elseif ( $action == 'view' && isset($session->system_roles[$this->system_id]) ) {
96 if ( isset($debuggroups['WorkSystem']) && $debuggroups['WorkSystem'] ) {
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],
106 /////////////////////////////////////////////////////////////
107 // Render - Return HTML to show the W/R
108 // A separate function is called for each logical area
110 /////////////////////////////////////////////////////////////
113 if ( ! $this->AllowedTo('view') ) {
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
122 if ( $ef->EditMode ) {
123 $html .= $ef->StartForm( array( /*"onsubmit" => "return CheckWorkSystemForm();" */ ) );
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") );
136 $html .= $ef->EndForm();
143 function RenderDetails( $ef ) {
146 $html .= $ef->BreakLine("System Details");
148 if ( !$this->new_record ) {
149 $html .= $ef->DataEntryLine( "System ID", "$this->system_id");
152 $html .= $ef->DataEntryLine( "System Code", "%s", "text", "system_code",
153 array( "size" => 10, "title" => "The code for the system.") );
156 $html .= $ef->DataEntryLine( "Description", "%s", "text", "system_desc",
157 array( "size" => 70, "title" => "The description of the system.") );
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") );
171 function RenderOrganisations( $ef ) {
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 ";
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 = ? ";
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 = ? ";
189 $sql .= "ORDER BY org_name";
192 $q = new PgQuery($sql, $this->system_id, $this->system_id);
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 = "<<<unknown>>>";
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>";
210 $html .= "<a href=\"/org.php?org_code=$row->org_code\">$row->org_name</a>";
213 if ( ++$column % 3 == 0 ) $html .= "</tr>";
216 while ( $column % 3 != 0 ) {
217 $html .= "<td></td>";
218 if ( ++$column % 3 == 0 ) $html .= "</tr>";
220 $html .= '</table></td></tr>'."\n";
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.";
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.";
244 if ( trim($_POST['system_desc']) == "" ) {
245 $client_messages[] = "ERROR: The system description may not be blank.";
252 $client_messages[] = "ERROR: No form data submitted!";
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']);
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";
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;
282 foreach( $_POST['applies'] AS $k => $v ) {
283 if ( $v && $v != '0' && $v != 'off' ) {
284 $organisations .= ( "$organisations" == "" ? "" : ", " );
285 $organisations .= "'" . str_replace("'","''",str_replace('\\','', $k)) . "'";
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";
303 $qry = new PgQuery("COMMIT; ROLLBACK;"); $qry->Exec("Sys::Write");