<?php
use phpformbuilder\Form;
use phpformbuilder\Validator\Validator;
use phpformbuilder\database\DB;
use common\Utils;
$debug_content = '';
// get referer pagination
$page_url_qry = '';
if (isset($_SESSION['competency-page']) && is_numeric($_SESSION['competency-page'])) {
$page_url_qry = '/p' . $_SESSION['competency-page'];
}
/* =============================================
delete if posted
============================================= */
if ($_SERVER["REQUEST_METHOD"] == "POST" && Form::testToken('form-delete-competency') === true) {
$validator = Form::validate('form-delete-competency', FORMVALIDATION_PHP_LANG);
// check for errors
if ($validator->hasErrors()) {
$_SESSION['errors']['form-delete-competency'] = $validator->getAllErrors();
} else {
if ($_POST['delete-competency'] > 0) {
$db = new DB(DEBUG);
$db->setDebugMode('register');
try {
// begin transaction
$db->transactionBegin();
// Delete from target table
$where = $_SESSION['competency_editable_primary_keys'];
if (DEMO === true || $db->delete('competency', $where, DEBUG_DB_QUERIES)) {
// ALL OK
if (!DEBUG_DB_QUERIES) {
$db->transactionCommit();
$_SESSION['msg'] = Utils::alert(DELETE_SUCCESS_MESSAGE, 'alert-success has-icon');
// reset form values
Form::clear('form-delete-competency');
// unset the search string
if (isset($_SESSION['rp_search_string']['competency'])) {
unset($_SESSION['rp_search_string']['competency']);
}
// redirect to list page
if (isset($_SESSION['active_list_url'])) {
header('Location:' . $_SESSION['active_list_url']);
} else {
header('Location:' . ADMIN_URL . 'competency');
}
// if we don't exit here, $_SESSION['msg'] will be unset
exit();
} else {
$debug_content = $db->getDebugContent();
$db->transactionRollback();
$_SESSION['msg'] = Utils::alert(DELETE_SUCCESS_MESSAGE . '<br>(' . DEBUG_DB_QUERIES_ENABLED . ')', 'alert-success has-icon');
}
} else {
throw new \Exception($db->error());
}
} catch (\Exception $e) {
if (DEBUG_DB_QUERIES) {
$debug_content = $db->getDebugContent();
}
$msg_content = DB_ERROR;
if (ENVIRONMENT == 'development') {
$msg_content .= '<br>' . $e->getMessage() . '<br>' . $db->getLastSql();
}
$_SESSION['msg'] = Utils::alert($msg_content, 'alert-danger has-icon');
}
}
}
} // END if POST
// register editable primary keys, which are NOT posted and will be the query delete filter
// $params come from data-forms.php
// replace 'fieldname' with 'table.fieldname' to avoid ambigous query
$where_params = array_combine(
array_map(function ($k) {
return 'competency.' . $k;
}, array_keys($params)),
$params
);
$_SESSION['competency_editable_primary_keys'] = $where_params;
if (!isset($db)) {
$db = new DB(DEBUG);
$db->setDebugMode('register');
}
// select name to display for confirmation
$from = 'competency';
$columns = array('name');
$where = $_SESSION['competency_editable_primary_keys'];
$extras = array('limit' => 1);
$db->select($from, $columns, $where, $extras, DEBUG_DB_QUERIES);
if (DEBUG_DB_QUERIES) {
$debug_content = $db->getDebugContent();
}
$count = $db->rowCount();
if ($count > 0) {
$row = $db->fetch();
$display_value = $row->name;
} else {
// this should never happen
// echo $db->getLastSql();
header("X-Robots-Tag: noindex", true);
exit('QRY ERROR');
}
$form = new Form('form-delete-competency', 'vertical', 'novalidate');
// $params come from data-forms.php
$pk_url_params = http_build_query($params, '', '/');
$form->setAction(ADMIN_URL . 'competency/delete/' . $pk_url_params);
$form->startFieldset();
$form->addHtml('<div class="text-center p-md">');
$form->addRadio('delete-competency', NO, 0);
$form->addRadio('delete-competency', YES, 1);
$form->printRadioGroup('delete-competency', '<span class="me-20">' . DELETE_CONST . ' "' . $display_value . '" ?</span>', true, 'required');
$form->addBtn('button', 'cancel', 0, '<i class="' . ICON_BACK . ' prepend"></i>' . CANCEL, 'class=btn btn-warning, onclick=history.go(-1)', 'btn-group');
$form->addBtn('submit', 'submit-btn', 1, SUBMIT . '<i class="' . ICON_CHECKMARK . ' append"></i>', 'class=btn btn-success', 'btn-group');
$form->setCols(0, 12);
$form->centerContent();
$form->printBtnGroup('btn-group');
$form->addHtml('
</div>');
$form->endFieldset();
$form->addPlugin('pretty-checkbox', '#form-delete-competency');
$form->addPlugin('formvalidation', '#form-delete-competency', 'default', array('language' => FORMVALIDATION_JAVASCRIPT_LANG));