Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
gilour
/
admin
/
inc
/
forms
/
336829
:
position-edit.php
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?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-edit-position') === true) { $validator = Form::validate('form-edit-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-edit-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'); $values = array(); $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; } } $where = $_SESSION['position_editable_primary_keys']; // begin transaction $db->transactionBegin(); try { // update position if (DEMO !== true && !$db->update('position', $values, $where, DEBUG_DB_QUERIES)) { $error = $db->error(); throw new \Exception($error); } else { // get records from position_competency $position_competency_current_records = array(); // Array with competency.competency_id $position_competency_records_to_add = array(); // Array with position_competency.position_id $position_competency_records_to_delete = array(); $from = 'position_competency'; $columns = array('competency_id'); $where = array('position_id' => $_SESSION['position_editable_primary_keys']['position.id']); $db->select($from, $columns, $where, array(), DEBUG_DB_QUERIES); $db_count = $db->rowCount(); if (!empty($db_count)) { while ($row = $db->fetch()) { $position_competency_current_records[] = $row->competency_id; } } foreach ($_POST['ext_competency'] as $competency_value) { if (!in_array($competency_value, $position_competency_current_records)) { $position_competency_records_to_add[] = $competency_value; } } foreach ($position_competency_current_records as $competency_value) { if (!in_array($competency_value, $_POST['ext_competency'])) { $position_competency_records_to_delete[] = $competency_value; } } // insert records in position_competency foreach ($position_competency_records_to_add as $value) { $values = array(); $values['id'] = ''; $values['position_id'] = $_SESSION['position_editable_primary_keys']['position.id']; $values['competency_id'] = $value; if (DEMO !== true && $db->insert('position_competency', $values, DEBUG_DB_QUERIES) === false) { $error = $db->error(); throw new \Exception($error); } } // delete records from position_competency foreach ($position_competency_records_to_delete as $competency_id_value) { $where = array(); $where['position_id'] = $_SESSION['position_editable_primary_keys']['position.id']; $where['competency_id'] = $competency_id_value; if (DEMO !== true && !$db->delete('position_competency', $where, DEBUG_DB_QUERIES)) { $error = $db->error(); throw new \Exception($error); } } // ALL OK if (!DEBUG_DB_QUERIES) { $db->transactionCommit(); $_SESSION['msg'] = Utils::alert(UPDATE_SUCCESS_MESSAGE, 'alert-success has-icon'); // reset form values Form::clear('form-edit-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(UPDATE_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 // register editable primary keys, which are NOT posted and will be the query update 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 'position.' . $k; }, array_keys($params)), $params ); $_SESSION['position_editable_primary_keys'] = $where_params; if (!isset($_SESSION['errors']['form-edit-position']) || empty($_SESSION['errors']['form-edit-position'])) { // If no error registered $from = 'position LEFT JOIN industry ON position.industry_id=industry.id'; $columns = 'position.id, position.name, position.nama, position.industry_id'; $where = $_SESSION['position_editable_primary_keys']; // if restricted rights if (ADMIN_LOCKED === true && Secure::canUpdateRestricted('position')) { $where = array_merge($where, Secure::getRestrictionQuery('position')); } $db = new DB(DEBUG); $db->setDebugMode('register'); $db->select($from, $columns, $where, array(), DEBUG_DB_QUERIES); if ($db->rowCount() < 1) { if (DEBUG) { exit($db->getLastSql() . ' : No Record Found'); } else { exit('No Record Found'); } } if (DEBUG_DB_QUERIES) { $debug_content .= $db->getDebugContent(); } $row = $db->fetch(); $_SESSION['form-edit-position']['id'] = $row->id; $_SESSION['form-edit-position']['name'] = $row->name; $_SESSION['form-edit-position']['nama'] = $row->nama; $_SESSION['form-edit-position']['industry_id'] = $row->industry_id; } $_SESSION['form-edit-position']['ext_competency'] = array(); $from = 'position_competency'; $columns = array('competency_id'); $where = array('position_id' => $_SESSION['position_editable_primary_keys']['position.id']); $db = new DB(); $db->select($from, $columns, $where, array(), DEBUG_DB_QUERIES); if (DEBUG_DB_QUERIES) { $debug_content .= $db->getDebugContent(); } $db_count = $db->rowCount(); if (!empty($db_count)) { while ($row = $db->fetch()) { $_SESSION['form-edit-position']['ext_competency'][] = $row->competency_id; } } // $params come from data-forms.php $pk_url_params = http_build_query($params, '', '/'); $form = new Form('form-edit-position', 'horizontal', 'novalidate'); $form->setAction(ADMIN_URL . 'position/edit/' . $pk_url_params); $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 = ''; $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-edit-position'); $form->addPlugin('formvalidation', '#form-edit-position', 'default', array('language' => FORMVALIDATION_JAVASCRIPT_LANG));