<?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-project') === true) {
$validator = Form::validate('form-create-project', FORMVALIDATION_PHP_LANG);
$validator->required()->validate('name');
$validator->maxLength(100)->validate('name');
$validator->required()->validate('nama');
$validator->maxLength(100)->validate('nama');
$validator->required()->validate('company_id');
$validator->integer()->validate('company_id');
$validator->min(-99999999999)->validate('company_id');
$validator->max(99999999999)->validate('company_id');
$validator->required()->validate('username');
$validator->maxLength(50)->validate('username');
$validator->required()->validate('pwd');
$validator->maxLength(255)->validate('pwd');
$validator->required()->validate('email_subject');
$validator->maxLength(1000)->validate('email_subject');
$validator->required()->validate('email_template');
$validator->required()->validate('email_subject_third');
$validator->maxLength(1000)->validate('email_subject_third');
$validator->required()->validate('email_template_third');
// check for errors
if ($validator->hasErrors()) {
$_SESSION['errors']['form-create-project'] = $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['company_id'])) {
$json_values = json_encode($_POST['company_id'], JSON_UNESCAPED_UNICODE);
$values['company_id'] = $json_values;
} else {
$values['company_id'] = intval($_POST['company_id']);
if ($values['company_id'] < 1) {
$values['company_id'] = null;
}
}
$values['username'] = $_POST['username'];
$values['pwd'] = $_POST['pwd'];
$values['email_subject'] = $_POST['email_subject'];
$values['email_template'] = $_POST['email_template'];
$values['email_subject_third'] = $_POST['email_subject_third'];
$values['email_template_third'] = $_POST['email_template_third'];
try {
// insert into project
if (DEMO !== true && $db->insert('project', $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-project');
// redirect to list page
if (isset($_SESSION['active_list_url'])) {
header('Location:' . $_SESSION['active_list_url']);
} else {
header('Location:' . ADMIN_URL . 'project');
}
// 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-project', 'horizontal', 'novalidate');
$form->setAction(ADMIN_URL . 'project/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', 'required');
// company_id --
$from = 'company';
$columns = 'company.id';
$where = array();
$extras = array(
'select_distinct' => true,
'order_by' => 'company.id'
);
// 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('project')) {
$secure_restriction_query = Secure::getRestrictionQuery('project');
if (!empty($secure_restriction_query)) {
if ('company' == USERS_TABLE) {
$restriction_query = 'company.id = ' . $_SESSION['secure_user_ID'];
$where[] = $restriction_query;
} elseif (preg_match('/company\./', $secure_restriction_query[0])) {
$restriction_query = 'project' . $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['company_id'])) {
$_SESSION['form-create-project']['company_id'] = addslashes($_GET['company_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->id;
if ($db_count > 1) {
$form->addOption('company_id', $value, $display_value);
}
}
}
if ($db_count > 1) {
$form->addSelect('company_id', 'Company Id', 'required, data-slimselect=true');
} else {
// for display purpose
$form->addInput('text', 'company_id-display', $display_value, 'Company Id', 'readonly');
// for send purpose
$form->addInput('hidden', 'company_id', $value);
}
// username --
$form->addInput('text', 'username', '', 'Username', 'required');
// pwd --
$form->addInput('text', 'pwd', '', 'Pwd', 'required');
// email_subject --
$form->addInput('text', 'email_subject', '', 'Email Subject', 'required');
// email_template --
$form->addPlugin('tinymce', '#email_template');
$form->addTextarea('email_template', '', 'Email Template', 'required, class=tinyMce, cols=10, rows=16');
// email_subject_third --
$form->addInput('text', 'email_subject_third', '', 'Email Subject Third', 'required');
// email_template_third --
$form->addPlugin('tinymce', '#email_template_third');
$form->addTextarea('email_template_third', '', 'Email Template Third', 'required, class=tinyMce, cols=10, rows=16');
$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-project');
$form->addPlugin('formvalidation', '#form-create-project', 'default', array('language' => FORMVALIDATION_JAVASCRIPT_LANG));