['historic']); db_save_filter($flt, 208); $action = ACT_DEFAULT; break; case 'insert': $p[':compname'] = gpc_get_string($_POST, 'compname'); $p[':comptype'] = gpc_get_int($_POST, 'comptype'); $id = db_exec_insert('company', $p); db_set_revision('table', TBL_COMPANIES); $action = ACT_VIEW; break; case 'update': $p[':compid'] = $id; $p[':compname'] = gpc_get_string($_POST, 'compname'); $p[':shortname'] = gpc_get_string($_POST, 'shortname', 20); $p[':comptype'] = gpc_get_int($_POST, 'comptype'); $p[':comptype2'] = gpc_get_int($_POST, 'comptype2'); if ($p[':comptype2'] <= 0) $p[':comptype2'] = NULL; $p[':street'] = gpc_get_string($_POST, 'street', 40); $p[':zip'] = gpc_get_string($_POST, 'zip', 10); $p[':city'] = gpc_get_string($_POST, 'city', 30); $p[':country'] = gpc_get_string($_POST, 'country', 30); $p[':contact'] = gpc_get_string($_POST, 'contact', 30); $p[':phone'] = gpc_get_string($_POST, 'phone', 30); $p[':email'] = gpc_get_string($_POST, 'email', 50); $p[':web'] = gpc_get_string($_POST, 'web', 40); $p[':customerno'] = gpc_get_string($_POST, 'customerno', 20); $p[':contractno'] = gpc_get_string($_POST, 'contractno', 20); $p[':remarks'] = gpc_get_string($_POST, 'remarks', 150); $allowed = db_load_enum('company', 'flags'); $p[':flags'] = gpc_get_set($_POST, 'flags', $allowed, true, NULL); db_exec_update('company', $p, 'compid'); $action = ACT_VIEW; break; case 'delete': // Security token needed! if (gpc_get_string($_POST, 'token', 16) != $_SESSION['token']) { $g_error->Add(_('Delete prohibited, invalid security token!')); $action = ACT_VIEW; break; } unset($_SESSION['token']); $sth = $pdo->prepare("DELETE FROM company WHERE compid=?"); $sth->execute([$id]); $g_message->Add(sprintf(_('Deleted company no. %d'), $id)); $action = ACT_DEFAULT; break; default: $g_error->Add(sprintf(_('Unknown function!'), $submit)); $valid = FALSE; } // ========== ACTIONS END ===================================================== require 'header.php'; // ========== PAGE CONTENT ==================================================== if ($action == ACT_DEFAULT): // ========== VARIANT: default behavior ======================================= page_caption_search($pagetitle); // load filter from db $flt = db_get_filter($user->id, 208, ['type','txt','flags']); $w = array(); $p = array(); if ($flt->type > 0) { $w[] = 'comptype=:comptype'; $p[':comptype'] = $flt->type; } if (strlen($flt->txt) > 1) { $w[] = '(compname LIKE :txt OR remarks LIKE :txt)'; $p[':txt'] = '%'.$flt->txt.'%'; } // condition for multiple flags is OR $parts = ["flags=''", 'flags IS NULL']; foreach ($flt->flags as $f) { $parts[] = "FIND_IN_SET('$f', flags)"; } if (!empty($parts)) { if (count($parts) > 1) { $w[] = '(' . join(' OR ', $parts) . ')'; } else { $w[] = $parts[0]; } } $where = join(' AND ', $w); $order = ' ORDER BY comptype, compname'; // grand total of records for current vessel $grandtotal = $pdo->query("SELECT COUNT(*) FROM company")->fetchColumn(); // get total record count for pagination and limit $sql = "SELECT COUNT(*) FROM company"; if ($where) { $sql .= " WHERE $where"; } $sth = $pdo->prepare($sql); try { $sth->execute($p); } catch(PDOException $e) { $g_error->Add($e->getMessage()); $g_error->Add($sql); $g_error->Add(print_r($p, true)); $g_error->PrintOut(); } $numrows = $sth->fetchColumn(); $page = gpc_get_int($_REQUEST, 'p', 1); $rows_pp = gpc_get_int($_REQUEST, 'n', $user->rows_pp); $lastpage = ceil($numrows/$rows_pp); $sql = "SELECT compid, compname, comptype, remarks, flags " . "FROM company"; if ($where) $sql .= ' WHERE ' . $where; $sql .= $order; // if pagination: $sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp; $sth = $pdo->prepare($sql); try { $sth->execute($p); } catch(PDOException $e) { $g_error->Add($e->getMessage()); $g_error->Add($sql); $g_error->Add(print_r($p, true)); } $res = $sth->fetchAll(); // Filter ?>
Filter
type); filter_create_text('flt_txt', _('Text'), $flt->txt, 15, 30); $opt_flags = db_load_enum('company', 'flags', true, true, false); filter_create_checks('flt_flags', _('Also show '), $opt_flags, $flt->flags ?? []); ?>
', "\n"; echo "\n"; echo ""; echo '#'; echo "" . _('Name') . ""; echo "" . _('Type') . ""; echo "" . _('Remarks') . ""; echo " "; echo "\n"; echo "\n"; $i = ($page - 1) * $rows_pp; $i0 = $i + 1; foreach ($res as $row) { $i++; // record number echo "\n"; echo '', $i; echo '', "\n"; // icons for some flags $flags = explode(',', $row['flags']); if (in_array('historic', $flags)) { echo ''; } if (in_array('deleted', $flags)) { echo ''; } echo "\n"; echo "", $row['compname'], "\n"; echo "", $opt_comptype[$row['comptype']], "\n"; echo "", $row['remarks'], "\n"; // Edit current record button echo ""; echo ''; echo "\n"; echo "\n"; } // Table summary table_rec_summary($i0, $i, $numrows, 6); echo "\n"; echo $pagination; form_add_button($g_scriptname); elseif ($action == ACT_ADD): // ========== VARIANT: add record ============================================= echo '

', _('Add Company'), "

\n"; ?>
prepare($sql); $sth->execute([$id]); $company = $sth->fetch(PDO::FETCH_OBJ); echo "

", $company->compname, "

\n"; echo '', "\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo "
', _('Name'),"", h($company->compname), "
', _('Short name'),"", h($company->shortname), "
', _('Type'),"", $opt_comptype[$company->comptype], "
', _('Secondary type'),"", $company->comptype2 ? $opt_comptype[$company->comptype2] : '-', "
', _('Street'),"", h($company->street), "
', _('Zip, City'),"", $company->zip, ' ', $company->city, "
', _('Country'),"", h($company->country), "
', _('Contact'),"", h($company->contact), "
', _('Phone'),""; echo make_phonelink($company->phone),' ', h($company->phone), "
', _('Email'),""; echo make_maillink($company->email),' ', h($company->email), "
', _('Web'),""; // echo '
', _('Customer no.'),"", h($company->customerno), "
', _('Contract no.'),"", h($company->contractno), "
', _('Remarks'),"", h($company->remarks, br:true), "
', _('Flags'),"", $company->flags, "
\n"; form_view_buttons($g_scriptname, $id); // show referenced equipment for current vessel $sql = "SELECT eid, ename FROM equipment WHERE vid=:vid AND (supplier=:id OR manufacturer=:id)"; $sth = $pdo->prepare($sql); $sth->execute([':vid' => $user->vid, ':id' => $id]); if ($sth->rowCount() > 0) { echo '

', _('Referenced in equipment'), ":

\n"; echo "\n"; } else { echo '

', _('Not referenced in any equipment'), "

\n"; } // show referenced invoices $sql = "SELECT docid, refid, filename, title " . "FROM docref AS r JOIN document AS d USING (docid) " . "WHERE r.reftype='company' AND r.refid=? AND d.doctype='invoice'"; $sth = $pdo->prepare($sql); $sth->execute([$id]); if ($sth->rowCount() > 0) { echo "

", _('Invoices'), ":

\n"; echo "\n"; } else { echo '

', _('No invoices'), "

\n"; } elseif ($action == ACT_EDIT): // ========== VARIANT: edit single record ===================================== echo '

', _('Edit Company'), "

\n"; $sql = "SELECT compname, comptype, comptype2, street, zip, city, country," . " contact, phone, email, web, customerno, contractno, remarks, flags " . "FROM company " . "WHERE compid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); $company = $sth->fetch(PDO::FETCH_OBJ); ?>
compname); form_create_text('shortname', _('Short name'), 20, $company->shortname); form_create_select('comptype', _('Company type'), $opt_comptype, $company->comptype); form_create_select('comptype2', _('Secondary company type'), $g_opt_none + $opt_comptype, $company->comptype2); form_create_text('street', _('Street'), 40, $company->street); form_create_text('zip', _('Zip'), 10, $company->zip); form_create_text('city', _('City'), 30, $company->city); form_create_text('country', _('Country'), 30, $company->country); form_create_text('contact', _('Contact'), 30, $company->contact); form_create_text('phone', _('Phone'), 30, $company->phone); form_create_text('email', _('Email'), 50, $company->email); form_create_text('web', _('Web'), 40, $company->web); form_create_text('customerno', _('Customer no.'), 20, $company->customerno); form_create_text('contractno', _('Contract no.'), 20, $company->contracno); form_create_textarea('remarks', _('Remarks'), 3, 150, $company->remarks); $opt_flags = db_load_enum('company', 'flags', true, true, false); form_create_checks('flags', _('Flags'), $opt_flags, explode(',', $company->flags)); ?>
prepare($sql); $sth->execute([$id]); $company = $sth->fetch(PDO::FETCH_OBJ); echo '

', _('Delete Company'), "

\n"; echo '

', sprintf(_('Record no. %d'), $id), "

\n"; echo '

Name: ', h($company->compname), "

"; echo '

Remarks: ', h($company->remarks, br:true), "

"; // Still used as a supplier or manufacturer for equipment? $sql = "SELECT COUNT(*) FROM equipment WHERE supplier=:id OR manufacturer=:id"; $sth = $pdo->prepare($sql); $sth->bindValue(':id', $id, PDO::PARAM_INT); $sth->execute(); $usecount = $sth->fetchColumn(); if ($usecount > 0) { echo '

', _('Company can not be deleted as there are equipment-references!'), "

\n"; echo '', _('Back'), "\n"; } else { echo '

', _('Deleting a company is final. There is no way back. Only delete if you are absolute sure.'), "

\n"; $_SESSION['token'] = bin2hex(random_bytes(8)); form_delete_buttons($g_scriptname, $id, $_SESSION['token']); } else: // ========== ERROR UNKNOWN VARIANT =========================================== echo '

', _('Unknown function call: Please report to system development!'), "

\n"; endif; // $action == ... // ========== END OF VARIANTS ================================================= include 'footer.php';