File "competencytype-edit.php"

Full Path: /home/humancap/cl.humancap.com.my/admin/inc/forms/competencytype-edit.php
File size: 5.29 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-edit-competency-type') === true) {
    $validator = Form::validate('form-edit-competency-type', FORMVALIDATION_PHP_LANG);
    $validator->required()->validate('name');
    $validator->maxLength(100)->validate('name');

    // check for errors
    if ($validator->hasErrors()) {
        $_SESSION['errors']['form-edit-competency-type'] = $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'];
        $where = $_SESSION['competency_type_editable_primary_keys'];

        // begin transaction
        $db->transactionBegin();

        try {
            // update competency_type
            if (DEMO !== true && !$db->update('competency_type', $values, $where, DEBUG_DB_QUERIES)) {
                $error = $db->error();
                throw new \Exception($error);
            } else {
                // 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-competency-type');

                    // redirect to list page
                    if (isset($_SESSION['active_list_url'])) {
                        header('Location:' . $_SESSION['active_list_url']);
                    } else {
                        header('Location:' . ADMIN_URL . 'competencytype');
                    }

                    // 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 'competency_type.' . $k;
    }, array_keys($params)),
    $params
);
$_SESSION['competency_type_editable_primary_keys'] = $where_params;

if (!isset($_SESSION['errors']['form-edit-competency-type']) || empty($_SESSION['errors']['form-edit-competency-type'])) { // If no error registered
    $from = 'competency_type';
    $columns = '*';

    $where = $_SESSION['competency_type_editable_primary_keys'];

    // if restricted rights
    if (ADMIN_LOCKED === true && Secure::canUpdateRestricted('competency_type')) {
        $where = array_merge($where, Secure::getRestrictionQuery('competency_type'));
    }

    $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-competency-type']['id'] = $row->id;
    $_SESSION['form-edit-competency-type']['name'] = $row->name;
}
// $params come from data-forms.php
$pk_url_params = http_build_query($params, '', '/');

$form = new Form('form-edit-competency-type', 'horizontal', 'novalidate');
$form->setAction(ADMIN_URL . 'competencytype/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');
$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-competency-type');
$form->addPlugin('formvalidation', '#form-edit-competency-type', 'default', array('language' => FORMVALIDATION_JAVASCRIPT_LANG));