File "positionbi-create-20250813204943.php"
Full Path: /home/humancap/cl.humancap.com.my/admin/inc/forms/positionbi-create-20250813204943.php
File size: 8.26 KB
MIME-type: text/x-php
Charset: utf-8
<?php
use phpformbuilder\Form;
use phpformbuilder\Validator\Validator;
use phpformbuilder\database\DB;
use common\Utils;
use secure\Secure;
include_once ADMIN_DIR . 'secure/class/secure/Secure.php';
$debug_content = '';
/* =============================================
validation if posted
============================================= */
if ($_SERVER["REQUEST_METHOD"] == "POST" && Form::testToken('form-create-position-bi') === true) {
$validator = Form::validate('form-create-position-bi', FORMVALIDATION_PHP_LANG);
$validator->required()->validate('position_id');
$validator->integer()->validate('position_id');
$validator->min(-99999999999)->validate('position_id');
$validator->max(99999999999)->validate('position_id');
$validator->required()->validate('bi_id');
$validator->integer()->validate('bi_id');
$validator->min(-99999999999)->validate('bi_id');
$validator->max(99999999999)->validate('bi_id');
// check for errors
if ($validator->hasErrors()) {
$_SESSION['errors']['form-create-position-bi'] = $validator->getAllErrors();
} else {
require_once CLASS_DIR . 'phpformbuilder/database/db-connect.php';
require_once CLASS_DIR . 'phpformbuilder/database/DB.php';
$db = new DB(DEBUG);
$db->setDebugMode('register');
// begin transaction
$db->transactionBegin();
$values = array();
$values['id'] = null;
if (is_array($_POST['position_id'])) {
$json_values = json_encode($_POST['position_id'], JSON_UNESCAPED_UNICODE);
$values['position_id'] = $json_values;
} else {
$values['position_id'] = intval($_POST['position_id']);
}
if (is_array($_POST['bi_id'])) {
$json_values = json_encode($_POST['bi_id'], JSON_UNESCAPED_UNICODE);
$values['bi_id'] = $json_values;
} else {
$values['bi_id'] = intval($_POST['bi_id']);
}
try {
// insert into position_bi
if (DEMO !== true && $db->insert('position_bi', $values, DEBUG_DB_QUERIES) === false) {
$error = $db->error();
throw new \Exception($error);
} else {
// ALL OK
if (!DEBUG_DB_QUERIES) {
$db->transactionCommit();
$_SESSION['msg'] = Utils::alert(INSERT_SUCCESS_MESSAGE, 'alert-success has-icon');
// reset form values
Form::clear('form-create-position-bi');
// redirect to list page
if (isset($_SESSION['active_list_url'])) {
header('Location:' . $_SESSION['active_list_url']);
} else {
header('Location:' . ADMIN_URL . 'positionbi');
}
// if we don't exit here, $_SESSION['msg'] will be unset
exit();
} else {
$debug_content .= $db->getDebugContent();
$db->transactionRollback();
$_SESSION['msg'] = Utils::alert(INSERT_SUCCESS_MESSAGE . '<br>(' . DEBUG_DB_QUERIES_ENABLED . ')', 'alert-success has-icon');
}
}
} catch (\Exception $e) {
$db->transactionRollback();
$msg_content = DB_ERROR;
if (DEBUG) {
$msg_content .= '<br>' . $e->getMessage() . '<br>' . $db->getLastSql();
}
$_SESSION['msg'] = Utils::alert($msg_content, 'alert-danger has-icon');
}
} // END else
} // END if POST
$form = new Form('form-create-position-bi', 'horizontal', 'novalidate');
$form->setAction(ADMIN_URL . 'positionbi/create');
$form->startFieldset();
// id --
$form->setCols(2, 10);
$form->addInput('hidden', 'id', '');
// position_id --
$form->setCols(2, 10);
$from = 'position';
$columns = 'position.id, position.name';
$where = array();
$extras = array(
'select_distinct' => true,
'order_by' => 'position.name'
);
// restrict if relationship table is the users table OR if the relationship table is used in the restriction query
if (ADMIN_LOCKED === true && Secure::canCreateRestricted('position_bi')) {
$secure_restriction_query = Secure::getRestrictionQuery('position_bi');
if (!empty($secure_restriction_query)) {
if ('position' == USERS_TABLE) {
$restriction_query = 'position.id = ' . $_SESSION['secure_user_ID'];
$where[] = $restriction_query;
} elseif (preg_match('/position\./', $secure_restriction_query[0])) {
$restriction_query = 'position_bi' . $secure_restriction_query[0];
$where[] = $restriction_query;
}
}
}
// default value if no record exist
$value = '';
$display_value = '';
// set the selected value if it has been sent in URL query parameters
if (isset($_GET['position_id'])) {
$_SESSION['form-create-position-bi']['position_id'] = addslashes($_GET['position_id']);
}
$db = new DB(DEBUG);
$db->setDebugMode('register');
$db->select($from, $columns, $where, $extras, DEBUG_DB_QUERIES);
if (DEBUG_DB_QUERIES) {
$debug_content .= $db->getDebugContent();
}
$db_count = $db->rowCount();
if (!empty($db_count)) {
while ($row = $db->fetch()) {
$value = $row->id;
$display_value = $row->name;
if ($db_count > 1) {
$form->addOption('position_id', $value, $display_value);
}
}
}
if ($db_count > 1) {
$form->addSelect('position_id', 'Position Id', 'required, data-slimselect=true');
} else {
// for display purpose
$form->addInput('text', 'position_id-display', $display_value, 'Position Id', 'readonly');
// for send purpose
$form->addInput('hidden', 'position_id', $value);
}
// bi_id --
$from = 'behavioral_indicator';
$columns = 'behavioral_indicator.id, behavioral_indicator.name';
$where = array();
$extras = array(
'select_distinct' => true,
'order_by' => 'behavioral_indicator.name'
);
// restrict if relationship table is the users table OR if the relationship table is used in the restriction query
if (ADMIN_LOCKED === true && Secure::canCreateRestricted('position_bi')) {
$secure_restriction_query = Secure::getRestrictionQuery('position_bi');
if (!empty($secure_restriction_query)) {
if ('behavioral_indicator' == USERS_TABLE) {
$restriction_query = 'behavioral_indicator.id = ' . $_SESSION['secure_user_ID'];
$where[] = $restriction_query;
} elseif (preg_match('/behavioral_indicator\./', $secure_restriction_query[0])) {
$restriction_query = 'position_bi' . $secure_restriction_query[0];
$where[] = $restriction_query;
}
}
}
// default value if no record exist
$value = '';
$display_value = '';
// set the selected value if it has been sent in URL query parameters
if (isset($_GET['bi_id'])) {
$_SESSION['form-create-position-bi']['bi_id'] = addslashes($_GET['bi_id']);
}
$db = new DB(DEBUG);
$db->setDebugMode('register');
$db->select($from, $columns, $where, $extras, DEBUG_DB_QUERIES);
if (DEBUG_DB_QUERIES) {
$debug_content .= $db->getDebugContent();
}
$db_count = $db->rowCount();
if (!empty($db_count)) {
while ($row = $db->fetch()) {
$value = $row->id;
$display_value = $row->name;
if ($db_count > 1) {
$form->addOption('bi_id', $value, $display_value);
}
}
}
if ($db_count > 1) {
$form->addSelect('bi_id', 'Bi Id', 'required, data-slimselect=true');
} else {
// for display purpose
$form->addInput('text', 'bi_id-display', $display_value, 'Bi Id', 'readonly');
// for send purpose
$form->addInput('hidden', 'bi_id', $value);
}
$form->addBtn('button', 'cancel', 0, '<i class="' . ICON_BACK . ' prepend"></i>' . CANCEL, 'class=btn btn-warning, data-ladda-button=true, data-style=zoom-in, onclick=history.go(-1)', 'btn-group');
$form->addBtn('submit', 'submit-btn', 1, SUBMIT . '<i class="' . ICON_CHECKMARK . ' append"></i>', 'class=btn btn-success, data-ladda-button=true, data-style=zoom-in', 'btn-group');
$form->setCols(0, 12);
$form->centerContent();
$form->printBtnGroup('btn-group');
$form->endFieldset();
$form->addPlugin('pretty-checkbox', '#form-create-position-bi');
$form->addPlugin('formvalidation', '#form-create-position-bi', 'default', array('language' => FORMVALIDATION_JAVASCRIPT_LANG));