_('unknown'))); $opt_manufacturer = db_get_opt_manuf(array(-1 => _('unknown'))); $opt_ecat = db_get_options(4); // Equipment categories // ========== ACTIONS START =================================================== switch ($submit = form_get_action()) { case NULL: break; case 'add': $action = ACT_ADD; break; case 'view': $action = ACT_VIEW; break; case 'edit': $action = ACT_EDIT; break; case 'del': $action = ACT_DELETE; break; case 'insert': $ename = gpc_get_string($_POST, 'ename'); $ecat = gpc_get_int($_POST, 'category'); $manufacturer = gpc_get_int($_POST, 'manufacturer'); $model = gpc_get_string($_POST, 'model'); $remarks = gpc_get_string($_POST, 'remarks', 150); $sql = "INSERT INTO equipment " . " (vid, ename, manufacturer, model, ecat, remarks) " . "VALUES " . " (:vid, :ename, :manufacturer, :model, :ecat, :remarks)"; $sth = $pdo->prepare($sql); $sth->bindValue(':vid', $user->vid, PDO::PARAM_INT); $sth->bindValue(':ename', $ename, PDO::PARAM_STR); $sth->bindValue(':model', $model, PDO::PARAM_STR); $sth->bindValue(':ecat', $ecat, PDO::PARAM_INT); $sth->bindValue(':manufacturer', $manufacturer, PDO::PARAM_INT); $sth->bindValue(':remarks', $remarks, PDO::PARAM_STR); try { $sth->execute(); } catch (PDOException $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); } $id = $pdo->lastInsertId(); $action = ACT_VIEW; break; case 'update': $ename = gpc_get_string($_POST, 'ename'); $model = gpc_get_string($_POST, 'model'); $serial = gpc_get_string($_POST, 'serial'); $weight = min(gpc_get_float($_POST, 'weight'), 999.99); // limit max value $price = gpc_get_currency($_POST, 'price'); $purchdate = gpc_get_date($_POST, 'purchdate'); $supplier = gpc_get_int($_POST, 'supplier'); $manufacturer = gpc_get_int($_POST, 'manufacturer'); $ecat = gpc_get_int($_POST, 'category'); $remarks = gpc_get_string($_POST, 'remarks', 150); $sql = "UPDATE equipment " . "SET ename=:ename," . " model=:model," . " serial=:serial," . " weight=:weight," . " price=:price," . " purchdate=:purchdate," . " supplier=:supplier," . " manufacturer=:manufacturer," . " ecat=:ecat," . " remarks=:remarks " . "WHERE eid=:eid"; $sth = $pdo->prepare($sql); $sth->bindValue(':eid', $id, PDO::PARAM_INT); $sth->bindValue(':ename', $ename, PDO::PARAM_STR); $sth->bindValue(':model', $model, PDO::PARAM_STR); $sth->bindValue(':serial', $serial, PDO::PARAM_STR); $sth->bindValue(':weight', $weight); $sth->bindValue(':price', $price); $sth->bindValue(':purchdate', $purchdate, PDO::PARAM_STR); $sth->bindValue(':supplier', $supplier, PDO::PARAM_INT); $sth->bindValue(':manufacturer', $manufacturer, PDO::PARAM_INT); $sth->bindValue(':ecat', $ecat, PDO::PARAM_INT); $sth->bindValue(':remarks', $remarks, PDO::PARAM_STR); try { $sth->execute(); } catch (PDOException $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); } $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 docref WHERE reftype='equipment' AND refid=?"); try { $sth->execute([$id]); } catch (PDOException $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); } $refcount = $sth->rowCount(); $sth = $pdo->prepare("DELETE FROM equipment WHERE eid=?"); try { $sth->execute([$id]); } catch (PDOException $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); } $g_message->Add(sprintf(_('Deleted equipment no. %d'), $id)); if ($refcount > 0) { $g_message->Add(sprintf(_('%d document links were removed'), $refcount)); } $action = ACT_DEFAULT; break; case 'upload': $action = ACT_VIEW; if (!isset($_FILES['files'])) { $g_warning->Add(_('No files for upload submitted')); break; } $extensions = ['jpg', 'png']; $mimetypes = ['image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf']; $all_files = count($_FILES ["files"]["tmp_name"]); $nerr = 0; for ($i = 0; $i < $all_files; $i++) { $file_name = $_FILES['files']['name'][$i]; $file_tmp = $_FILES['files']['tmp_name'][$i]; $file_type = $_FILES['files']['type'][$i]; $file_size = $_FILES['files']['size'][$i]; $file_ext = strtolower(end(explode('.', $_FILES['files']['name'][$i]))); // $file = $g_doc_basepath . '/' . $file_name; $file_mimetype = mime_content_type($file_tmp); if (!in_array($file_mimetype, $mimetypes)) { $g_error->Add(sprintf(_("Mimetype '%s' not allowed for upload."), $file_mimetype)); $nerr += 1; } if (!in_array($file_ext, $extensions)) { $g_error->Add(_('Filetype not allowed for upload:') . ' ' . $file_type); $nerr += 1; } if ($file_size > 2097152) { $g_error->Add(sprintf(_('File to big: %s.%s'), $file_name, $file_type)); $nerr += 1; } if ($nerr > 0) { break; } $file_hash = md5_file($file_tmp); $file_timestamp = date('Y-m-d H:i:s', filemtime($file_tmp)); // check whether the file already exists, in this case issue // a message and just create a link to the already known file $sql = "SELECT docid FROM document WHERE hash=?"; $sth = $pdo->prepare($sql); $sth->execute([$file_hash]); $row = $sth->fetch(); if (!$row) { $sql = "INSERT INTO document" . " (doctype, filename, extension, hash, doctime, docsize, mimetype) " . "VALUES" . " (?, ?, ?, ?, ?, ?, ?)"; $sth = $pdo->prepare($sql); $sth->execute(['picture', $file_name, $file_ext, $file_hash, $file_timestamp, $file_size, $file_mimetype]); $docid = $pdo->lastInsertId(); $file_techname = $g_doc_basepath . '/pic-'.$file_hash.'.'.$file_ext; move_uploaded_file($file_tmp, $file_techname); } else { // A document record definitely exists here, now just // connect it to the selected equipment $docid = $row['docid']; } $sql = "INSERT INTO docref (docid, refid) VALUES (?, ?)"; $sth = $pdo->prepare($sql); // Reference may already exist! try { $sth->execute([$docid, $id]); } catch (PDOException $e) { $g_error->Add('SQL-Error: '. $e->getMessage()); } } // for 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 ======================================= $sql = "SELECT eid, ename, manufacturer, model, serial, remarks," . " TIMESTAMPDIFF(MONTH, purchdate, NOW()) AS age_mon " . "FROM equipment WHERE vid=? " . "ORDER BY eid"; $sth = $pdo->prepare($sql); $sth->execute([$user->vid]); $res = $sth->fetchAll(); echo "

$pagetitle

\n"; // Filter ?>
Filter
', "\n"; echo "\n"; echo ""; echo "" . _('Description') . ""; echo "" . _('Manufacturer') . ""; echo "" . _('Model') . ""; echo "" . _('Serial') . ""; echo "" . _('Age') . ""; echo "" . _('Remarks') . ""; echo " "; echo "\n"; echo "\n"; foreach ($res as $row) { // calc nice age display if (!isset($row['age_mon'])) { $age = ''; } elseif ($row['age_mon'] < 12) { $age = sprintf(_('%dm'), $row['age_mon']); } else { $age = sprintf(_('%dy'), intdiv($row['age_mon'], 12)); } echo "\n"; echo "". $row['ename'] ."\n"; echo "". $opt_manufacturer[$row['manufacturer']] ."\n"; echo "". $row['model'] ."\n"; echo "". $row['serial'] ."\n"; echo "". $age ."\n"; echo "". $row['remarks'] ."\n"; // Action buttons form_action_buttons($g_scriptname, $row['eid']); } ?>

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

", $equipment->ename, "

\n"; // Bootstrap grid echo '
'; echo '
'; echo '
'; echo '', "\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo '\n"; echo "
', _('Manufacturer'),"", $opt_manufacturer[$equipment->manufacturer], "
', _('Model'),"", $equipment->model, "
', _('Serial'),"", $equipment->serial, "
', _('Weight'),"", format_float($equipment->weight, 2, 'kg'), "
', _('Price'),"", format_currency($equipment->price), "
', _('Purchase date'),"", $equipment->purchdate, "
', _('Supplier'),"", $opt_supplier[$equipment->supplier], "
', _('Category'),"", $opt_ecat[$equipment->ecat], "
', _('Remarks'),"", $equipment->remarks, "
\n"; echo '
'; // Column break echo '
'; ?>

Images and documents go here

prepare($sql); $sth->execute([$id]); $res = $sth->fetchAll(); foreach ($res as $row) { if ($row['doctype'] == 'picture') { echo ''; // echo ''; echo '', $row['title'], ''; echo "\n"; } elseif ($row['doctype'] == 'generic') { echo ''; echo $row['title']; echo "\n"; } } ?>
'; // col echo '
'; // row echo '
'; // container // Buttons at bottom of data area form_view_buttons($g_scriptname, $id); elseif ($action == ACT_EDIT): // ========== VARIANT: edit single record ===================================== $sql = "SELECT compid, compname FROM company WHERE comptype=1 ORDER BY compname"; $sth = $pdo->query($sql); $supplier = array(); foreach ($sth->fetchAll() as $row) { $supplier[$row['compid']] = $row['compname']; } $sql = "SELECT eid, ename, model, serial, weight, price, purchdate," . " supplier, manufacturer, ecat, remarks " . "FROM equipment " . "WHERE eid=?"; $sth = $pdo->prepare($sql); $sth->execute([$id]); $equipment = $sth->fetch(PDO::FETCH_OBJ); ?>

manufacturer); ?>
supplier); form_create_select('category', _('Category'), $opt_ecat, $equipment->ecat); ?>
prepare($sql); $sth->execute([$id]); $equipment = $sth->fetch(PDO::FETCH_OBJ); echo '

', _('Delete equipment'),"

\n"; echo '

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

\n"; echo '

Name: ', $equipment->ename, "

"; echo '

Remarks: ', $equipment->remarks, "

"; echo '

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

\n"; 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';