Skip to content

Instantly share code, notes, and snippets.

@navt
Last active May 22, 2020 21:30
Show Gist options
  • Save navt/4dcf25179e56f0c2f94da5cef2633065 to your computer and use it in GitHub Desktop.
Save navt/4dcf25179e56f0c2f94da5cef2633065 to your computer and use it in GitHub Desktop.
простейший компонент для InstantCMS 2.x
<?php
// system/controllers/division/forms/form_division.php
class formDivisionDivision extends cmsForm {
public function init() {
return [
[
"type" => "fieldset",
"childs" => [
new fieldNumber("source", [
"title" => "Исходное число",
"rules" => [
["required"]
]
]),
new fieldNumber("divider", [
"title" => "Делитель",
"rules" => [
["required"]
]
])
]
]
];
}
}
<?php
// system/controllers/division/frontend.php
class division extends cmsFrontend {
public function actionIndex() {
$form = $this->getForm("division");
$errors = false;
$numbers = [];
$template = cmsTemplate::getInstance();
$template->render("index", [
"form" => $form,
"errors" => $errors,
"numbers" => $numbers
]);
}
public function actionCalc() {
if ($this->request->isAjax() === false) {
exit;
}
$post = (object)$_POST;
if (is_numeric($post->source) && is_numeric($post->divider) && $post->divider != 0) {
$reply["result"] = (float)$post->source / (float)$post->divider;
$reply["msg"] = "Расчет выполнен.";
} else {
$reply["result"] = "-";
$reply["msg"] = "Данные некорректны. Расчет не выполнен.";
}
$s = json_encode($reply);
header("Content-type: application/json");
echo $s;
exit;
}
<?php
// templates/default/controllers/division/index.tpl.php
$d = "Деление";
$this->setPageTitle($d);
$this->addBreadcrumb($d);
?>
<h1><?php echo $d; ?></h1>
<?php
$formID = md5(microtime(true));
$numbers = ["source" => 0, "divider" => 1];
$this->renderForm($form, $numbers, [
"form_id" => $formID,
"submit" => ["title" => "Рассчитать", "id" => "calc"]
], $errors);
?>
<div id="result"></div>
<div id="msg"></div>
<script>
$('<?php echo "#".$formID; ?>').submit(function(e) {
$.ajax({
url: 'calc',
data: $(this).serialize(),
type: 'post',
success: function(response){
$('#result').html(response.result);
$('#msg').html(response.msg);
}
});
e.preventDefault();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment