<?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') === true) {
$validator = Form::validate('form-create-position', FORMVALIDATION_PHP_LANG);
$validator->required()->validate('name');
$validator->maxLength(100)->validate('name');
$validator->maxLength(100)->validate('nama');
$validator->required()->validate('industry_id');
$validator->integer()->validate('industry_id');
$validator->min(-99999999999)->validate('industry_id');
$validator->max(99999999999)->validate('industry_id');
// check for errors
if ($validator->hasErrors()) {
$_SESSION['errors']['form-create-position'] = $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;
$values['name'] = $_POST['name'];
$values['nama'] = $_POST['nama'];
if (is_array($_POST['industry_id'])) {
$json_values = json_encode($_POST['industry_id'], JSON_UNESCAPED_UNICODE);
$values['industry_id'] = $json_values;
} else {
$values['industry_id'] = intval($_POST['industry_id']);
if ($values['industry_id'] < 1) {
$values['industry_id'] = null;
}
}
try {
// insert into position
if (DEMO !== true && $db->insert('position', $values, DEBUG_DB_QUERIES) === false) {
$error = $db->error();
throw new \Exception($error);
} else {
$position_last_insert_ID = $db->getLastInsertID();
// insert records in position_competency
foreach ($_POST['ext_competency'] as $value) {
$values = array();
$values['id'] = null;
$values['position_id'] = $position_last_insert_ID;
$values['competency_id'] = $value;
if (DEMO !== true && $db->insert('position_competency', $values, DEBUG_DB_QUERIES) === false) {
$error = $db->error();
throw new \Exception($error);
}
}
// 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');
// redirect to list page
if (isset($_SESSION['active_list_url'])) {
header('Location:' . $_SESSION['active_list_url']);
} else {
header('Location:' . ADMIN_URL . 'position');
}
// 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', 'horizontal', 'novalidate');
$form->setAction(ADMIN_URL . 'position/create');
$form->startFieldset();
// id --
$form->setCols(2, 10);
$form->addInput('hidden', 'id', '');
// name --
$form->setCols(2, 10);
$form->addInput('text', 'name', '', 'Name', 'required');
// nama --
$form->addInput('text', 'nama', '', 'Nama', '');
// industry_id --
$from = 'industry';
$columns = 'industry.id, industry.name';
$where = array();
$extras = array(
'select_distinct' => true,
'order_by' => 'industry.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')) {
$secure_restriction_query = Secure::getRestrictionQuery('position');
if (!empty($secure_restriction_query)) {
if ('industry' == USERS_TABLE) {
$restriction_query = 'industry.id = ' . $_SESSION['secure_user_ID'];
$where[] = $restriction_query;
} elseif (preg_match('/industry\./', $secure_restriction_query[0])) {
$restriction_query = 'position' . $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['industry_id'])) {
$_SESSION['form-create-position']['industry_id'] = addslashes($_GET['industry_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('industry_id', $value, $display_value);
}
}
}
if ($db_count > 1) {
$form->addSelect('industry_id', 'Industry', 'required, data-slimselect=true');
} else {
// for display purpose
$form->addInput('text', 'industry_id-display', $display_value, 'Industry', 'readonly');
// for send purpose
$form->addInput('hidden', 'industry_id', $value);
}
// external relation: position => position_competency => competency;
$from = 'competency';
$columns = 'name, nama, id';
$where = false;
$extras = array(
'select_distinct' => true
);
$db = new DB();
$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)) {
$values = array();
$display_values = array();
while ($row = $db->fetch()) {
$values[] = $row->id;
$display_values[] = $row->name . ' - ' . $row->nama;
}
for ($i=0; $i < $db_count; $i++) {
$form->addOption('ext_competency[]', $values[$i], $display_values[$i]);
}
$form->addSelect('ext_competency[]', 'Competency', 'data-slimselect=true, multiple, data-close-on-select=false');
}
$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');
$form->addPlugin('formvalidation', '#form-create-position', 'default', array('language' => FORMVALIDATION_JAVASCRIPT_LANG));