Last active
May 22, 2020 21:30
-
-
Save navt/4dcf25179e56f0c2f94da5cef2633065 to your computer and use it in GitHub Desktop.
простейший компонент для InstantCMS 2.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"] | |
] | |
]) | |
] | |
] | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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