407 lines
14 KiB
PHP
407 lines
14 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
class WebAppRaiserForm {
|
||
|
|
|
||
|
|
protected $form;
|
||
|
|
protected $form_id;
|
||
|
|
|
||
|
|
public function __construct($form_id) {
|
||
|
|
$this->form_id = $form_id;
|
||
|
|
$this->form = $this->form($form_id);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function getForm($form_id)
|
||
|
|
{
|
||
|
|
$classes = array(
|
||
|
|
// 'form_id' => 'ClassName'
|
||
|
|
'order_create_form' => 'OrderCreateForm',
|
||
|
|
'order_docs_form' => 'OrderDocsForm',
|
||
|
|
'order_delete_form' => 'OrderDeleteForm',
|
||
|
|
'cost_analice_form' => 'CostAnaliceForm',
|
||
|
|
'api_settings_form' => 'ApiSettingsForm',
|
||
|
|
'api_pincode_form' => 'ApiPincodeForm',
|
||
|
|
'message_chat_form' => 'MessageChatForm',
|
||
|
|
'order_getreport_form' => 'OrderGetReportForm',
|
||
|
|
);
|
||
|
|
if(isset($classes[$form_id])) {
|
||
|
|
$ClassName = $classes[$form_id];
|
||
|
|
$file = __DIR__ .'/forms/'. $ClassName.'.php';
|
||
|
|
if(is_file($file)){
|
||
|
|
include_once $file;
|
||
|
|
$form = new $ClassName($form_id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
$form = new WebAppRaiserForm($form_id);
|
||
|
|
}
|
||
|
|
return $form;
|
||
|
|
}
|
||
|
|
|
||
|
|
// метод формирует массив формы, переопределяется в дочерних классах
|
||
|
|
public function form(){
|
||
|
|
$form = array();
|
||
|
|
$form['form_attributes'] = array(
|
||
|
|
'method'=> 'post',
|
||
|
|
);
|
||
|
|
$form['form_id'] = array(
|
||
|
|
'inputs' => array(
|
||
|
|
'#type'=> 'hidden',
|
||
|
|
'#value'=> $this->form_id,
|
||
|
|
),
|
||
|
|
);
|
||
|
|
return $form;
|
||
|
|
}
|
||
|
|
|
||
|
|
// метод валидирует поля формы, переопределяется в дочерних классах
|
||
|
|
public function validate()
|
||
|
|
{
|
||
|
|
$status = array(
|
||
|
|
'code'=> 'success',
|
||
|
|
'message'=> array(),
|
||
|
|
'post' => clearInputData($_POST),
|
||
|
|
);
|
||
|
|
foreach($this->form as $field => $element){
|
||
|
|
if(isset($status['post'][$field]) && isset($element['inputs']['#required'])){
|
||
|
|
if(empty($status['post'][$field]) && $element['inputs']['#required'] === true){
|
||
|
|
$status['status'] = 'error';
|
||
|
|
$label = (is_array($element['label']))?$element['label']['#text']:$element['label'];
|
||
|
|
$status['message'][] = 'Поле '.$label.' обязательно для заполнения';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $status;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// метод обрабатывает отправку формы, переопределяется в дочерных классах
|
||
|
|
public function submit($status, &$page, WebAppRaiserAPI $webAppRaiserAPI)
|
||
|
|
{
|
||
|
|
if(isset($status['post']['destination'])) {
|
||
|
|
$page['destination'] = str_replace('&', '&', $status['post']['destination']);
|
||
|
|
}
|
||
|
|
if(isset($status['post']['fragment'])) {
|
||
|
|
$page['fragment'] = $status['post']['fragment'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function redirect(&$page)
|
||
|
|
{
|
||
|
|
if(!empty($page['destination'])) {
|
||
|
|
if(!empty($page['msg'])){
|
||
|
|
$page['destination'] .= (strpos($page['destination'], '?'))?'&status='.$page['status']:'?status='.$page['status'];
|
||
|
|
$page['destination'] .= '&msg='. implode(' ', $page['msg']);
|
||
|
|
}
|
||
|
|
if(!empty($page['fragment'])){
|
||
|
|
$page['destination'] .= '#'.$page['fragment'];
|
||
|
|
}
|
||
|
|
header("location:/" . $page['destination']);
|
||
|
|
exit();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function render($fields, $modal = false, $modal_id = '')
|
||
|
|
{
|
||
|
|
$form = $this->form;
|
||
|
|
$formID = $this->form_id;
|
||
|
|
$post = clearInputData($_POST);
|
||
|
|
$get = clearInputData($_GET);
|
||
|
|
$output = '';
|
||
|
|
//$form_id = (isset($form['form_id']['inputs']['#value']))?' id="'.$form['form_id']['inputs']['#value'].'"':'';
|
||
|
|
if(isset($form['form_attributes'])){
|
||
|
|
$form_attributes = $form['form_attributes'];
|
||
|
|
unset($form['form_attributes']);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
$form_attributes = array(
|
||
|
|
'method' => 'post',
|
||
|
|
'class'=> 'form',
|
||
|
|
);
|
||
|
|
}
|
||
|
|
if(isset($form_attributes['js'])){
|
||
|
|
foreach($form_attributes['js'] as $js){
|
||
|
|
$output .= '<script type="text/javascript" src="/'.$js.'"></script>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
unset($form_attributes['js']);
|
||
|
|
}
|
||
|
|
$attr = '';
|
||
|
|
foreach($form_attributes as $key => $value){
|
||
|
|
$attr .= ' '.$key.'="'.$value.'"';
|
||
|
|
}
|
||
|
|
|
||
|
|
// Modal wrap
|
||
|
|
if($modal){
|
||
|
|
if(empty($modal_id)){
|
||
|
|
$modal_id = $formID.'_modal';
|
||
|
|
}
|
||
|
|
$output .= '
|
||
|
|
<div class="full_modal-bg" id="'.$modal_id.'__bg" style="display: none">
|
||
|
|
<span class="pseudo-link closer '.$modal_id.'_close"><i class="ti-close"></i></span>
|
||
|
|
<div class="full_modal" id="'.$modal_id.'" style="display: none;">
|
||
|
|
<div class="modal-inner">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Start form output
|
||
|
|
$output .= '<form'.$attr.' id='.$formID.'>'.PHP_EOL;
|
||
|
|
|
||
|
|
foreach ($form as $field => $fdata) {
|
||
|
|
if(!isset($fdata['inputs']['#type']) && isset($fdata['#markup'])){
|
||
|
|
$fdata['inputs']['#type'] = 'markup';
|
||
|
|
$fdata['inputs']['#value'] = $fdata['#markup'];
|
||
|
|
}
|
||
|
|
$form_block = !in_array($fdata['inputs']['#type'], ['hidden', 'markup', 'submit']);
|
||
|
|
|
||
|
|
if(isset($fdata['#prefix']))
|
||
|
|
$output .= $fdata['#prefix'];
|
||
|
|
if($form_block)
|
||
|
|
$output .= '<div class="form-block" id="'.$formID.'_'.$field.'-block">'.PHP_EOL;
|
||
|
|
|
||
|
|
// Label (prepend)
|
||
|
|
if(!empty($fdata['label']) && $form_block && empty($fdata['label']['#append'])) {
|
||
|
|
$required_str = ($fdata['inputs']['#required'])?' <span style="color:red">*</span>':'';
|
||
|
|
if(is_string($fdata['label'])) {
|
||
|
|
$output .= '<div class="label"><label for="'.$formID.'_'.$field.'-edit">'.$fdata['label'].$required_str.'</label></div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
elseif(is_array($fdata['label'])) {
|
||
|
|
$class = '';
|
||
|
|
if(isset($fdata['label']['#class'])) {
|
||
|
|
$class = ' class="'.implode(' ', $fdata['label']['#class']).'"';
|
||
|
|
}
|
||
|
|
if(isset($fdata['label']['#style'])) {
|
||
|
|
$style = ' style="'.implode('; ', $fdata['label']['#style']).'"';
|
||
|
|
}
|
||
|
|
$output .= '<div'.$class.$style.'><label for="'.$formID.'_'.$field.'-edit">'.$fdata['label']['#text'].$required_str.'</label></div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Inputs
|
||
|
|
$value = '';
|
||
|
|
if(isset($fdata['inputs']['#value']))
|
||
|
|
$value = $fdata['inputs']['#value'];
|
||
|
|
if(isset($fields[$field]))
|
||
|
|
$value = $fields[$field];
|
||
|
|
if(isset($get[$field]))
|
||
|
|
$value = $get[$field];
|
||
|
|
if(isset($post[$field]))
|
||
|
|
$value = $post[$field];
|
||
|
|
$class = '';
|
||
|
|
$style = '';
|
||
|
|
$disabled = '';
|
||
|
|
$readonly = '';
|
||
|
|
$required = '';
|
||
|
|
$checked = '';
|
||
|
|
$multiple = '';
|
||
|
|
$accept = '';
|
||
|
|
$attributes = '';
|
||
|
|
if(isset($fdata['inputs']['#class'])) {
|
||
|
|
$class = ' class="'.implode(' ', $fdata['inputs']['#class']).'"';
|
||
|
|
}
|
||
|
|
if(isset($fdata['inputs']['#style'])) {
|
||
|
|
$style = ' style="'.implode('; ', $fdata['inputs']['#style']).'"';
|
||
|
|
}
|
||
|
|
if(isset($fdata['inputs']['#disabled']) && $fdata['inputs']['#disabled']) {
|
||
|
|
$disabled = ' disabled';
|
||
|
|
}
|
||
|
|
if(isset($fdata['inputs']['#readonly']) && $fdata['inputs']['#readonly']) {
|
||
|
|
$readonly = ' readonly';
|
||
|
|
}
|
||
|
|
if(isset($fdata['inputs']['#required']) && $fdata['inputs']['#required']) {
|
||
|
|
$required = ' required';
|
||
|
|
}
|
||
|
|
if(isset($fdata['inputs']['#checked']) && $fdata['inputs']['#checked']) {
|
||
|
|
$checked = ' checked';
|
||
|
|
}
|
||
|
|
if(isset($fdata['inputs']['#multiple']) && $fdata['inputs']['#multiple']) {
|
||
|
|
$multiple = ' multiple';
|
||
|
|
}
|
||
|
|
if(isset($fdata['inputs']['#attributes']) && !empty($fdata['inputs']['#attributes'])) {
|
||
|
|
foreach($fdata['inputs']['#attributes'] as $attr_key => $attr_value){
|
||
|
|
$attributes .= ' '.$attr_key.'="'.$attr_value.'"';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$field_name = $field;
|
||
|
|
$inputs_style = (isset($fdata['inputs']['#inputs_style']))?' style="'.$fdata['inputs']['#inputs_style'].'"':'';
|
||
|
|
$inputs_class = ($fdata['inputs']['#type'] == 'checkbox')?'checkbox':'inputs';
|
||
|
|
|
||
|
|
if($form_block)
|
||
|
|
$output .= '<div class="'.$inputs_class.'"'.$inputs_style.'>'.PHP_EOL;
|
||
|
|
|
||
|
|
switch($fdata['inputs']['#type']) {
|
||
|
|
case 'markup':
|
||
|
|
$output .= $value.PHP_EOL;
|
||
|
|
break;
|
||
|
|
case 'hidden':
|
||
|
|
case 'reset':
|
||
|
|
case 'text':
|
||
|
|
case 'password':
|
||
|
|
|
||
|
|
case 'button':
|
||
|
|
case 'color':
|
||
|
|
case 'date':
|
||
|
|
case 'email':
|
||
|
|
case 'file':
|
||
|
|
case 'number':
|
||
|
|
case 'url':
|
||
|
|
if(!empty($multiple)){ $field_name = $field.'[]'; }
|
||
|
|
$output .= '<input type="'.$fdata['inputs']['#type'].'" id="'.$formID.'_'.$field.'-edit" name="'.$field_name.'" value="'.$value.'"'.$class.$style.$disabled.$readonly.$required.$checked.$multiple.$accept.$attributes.'>'.PHP_EOL;
|
||
|
|
break;
|
||
|
|
|
||
|
|
case 'textarea':
|
||
|
|
$rows = (isset($fdata['inputs']['#rows'])) ? ' rows="'.$fdata['inputs']['#rows'].'"':'';
|
||
|
|
$cols = (isset($fdata['inputs']['#cols'])) ? ' cols="'.$fdata['inputs']['#cols'].'"':'';
|
||
|
|
$output .= '<textarea id="'.$formID.'_'.$field.'-edit" name="'.$field.'"'.$rows.$cols.$class.$style.$disabled.$readonly.$required.$attributes.'>'.$value.'</textarea>'.PHP_EOL;
|
||
|
|
break;
|
||
|
|
|
||
|
|
case 'checkbox':
|
||
|
|
$check_option = $fdata['inputs']['#check_option'];
|
||
|
|
if($value == $check_option)
|
||
|
|
$checked = ' checked';
|
||
|
|
$output .= '<input type="'.$fdata['inputs']['#type'].'" id="'.$formID.'_'.$field.'-edit" name="'.$field.'" value="'.$check_option.'"'.$class.$style.$disabled.$readonly.$required.$checked.$attributes.'>'.PHP_EOL;
|
||
|
|
$output .= '<label for="'.$formID.'_'.$field.'-edit"></label>'.PHP_EOL;
|
||
|
|
break;
|
||
|
|
|
||
|
|
case 'radio':
|
||
|
|
$i = 1;
|
||
|
|
$output .= '<div'.$class.$style.'>'.PHP_EOL;
|
||
|
|
foreach ($fdata['inputs']['#options'] as $radio_key => $radio_value) {
|
||
|
|
$output .= '<div><input type="radio" id="'.$formID.'_'.$field.'-edit-'.$i.'" name="'.$field.'" value="'.$radio_key.'">'.PHP_EOL;
|
||
|
|
$output .= '<label for="'.$formID.'_'.$field.'-edit-'.$i.'">'.$radio_value.'</label></div>'.PHP_EOL;
|
||
|
|
$i++;
|
||
|
|
}
|
||
|
|
$output .= '</div>'.PHP_EOL;
|
||
|
|
break;
|
||
|
|
|
||
|
|
case 'select':
|
||
|
|
$output .= '<select id="'.$formID.'_'.$field.'-edit" name="'.$field.'"'.$class.$style.$disabled.$readonly.$required.$attributes.'>'.PHP_EOL;
|
||
|
|
foreach ($fdata['inputs']['#options'] as $s_key => $s_value) {
|
||
|
|
$selected = ($value == $s_key)?' selected':'';
|
||
|
|
$output .= '<option value="'.$s_key.'"'.$selected.'>'.$s_value.'</option>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
$output .= '</select>'.PHP_EOL;
|
||
|
|
break;
|
||
|
|
|
||
|
|
case 'submit':
|
||
|
|
if($modal){
|
||
|
|
$output .= '<div class="modal-submit" style="margin-top: 15px; margin-left: -52px; margin-right: -52px; margin-bottom: 50px;">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
$output .= '<button id="'.$formID.'_'.$field.'-edit" class="button-fl-left big-button" type="submit"'.$style.'>'.$value.'</button>'.PHP_EOL;
|
||
|
|
if($modal){
|
||
|
|
$output .= '</div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
$output .= '<div class="load-bar hidden">
|
||
|
|
<div class="bar"></div>
|
||
|
|
<div class="bar"></div>
|
||
|
|
<div class="bar"></div>
|
||
|
|
<div class="load-bar__center">
|
||
|
|
<span class="load-bar__loading">Сохраняем...</span>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
<div class="clear"></div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
if($form_block)
|
||
|
|
$output .= '</div>'.PHP_EOL;
|
||
|
|
|
||
|
|
// Label (append)
|
||
|
|
if(!empty($fdata['label_append']) && $form_block) {
|
||
|
|
$required_str = ($fdata['inputs']['#required'])?' <span style="color:red">*</span>':'';
|
||
|
|
if(is_string($fdata['label_append'])) {
|
||
|
|
$output .= '<div class="label"><label for="'.$formID.'_'.$field.'-edit">'.$fdata['label'].$required_str.'</label></div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
elseif(is_array($fdata['label_append'])) {
|
||
|
|
$class = '';
|
||
|
|
if(isset($fdata['label_append']['#class'])) {
|
||
|
|
$class = ' class="'.implode(' ', $fdata['label_append']['#class']).'"';
|
||
|
|
}
|
||
|
|
if(isset($fdata['label_append']['#style'])) {
|
||
|
|
$style = ' style="'.implode('; ', $fdata['label_append']['#style']).'"';
|
||
|
|
}
|
||
|
|
$output .= '<div'.$class.$style.'><label for="'.$formID.'_'.$field.'-edit">'.$fdata['label_append']['#text'].$required_str.'</label></div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if(isset($fdata['#suffix']))
|
||
|
|
$output .= $fdata['#suffix'].PHP_EOL;
|
||
|
|
// Description
|
||
|
|
if(!empty($fdata['description']) && $form_block) {
|
||
|
|
if(is_string($fdata['description'])) {
|
||
|
|
$output .= '<div class="description">'.$fdata['description'].'</div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
elseif(is_array($fdata['description'])) {
|
||
|
|
$class = '';
|
||
|
|
if(isset($fdata['description']['#class'])) {
|
||
|
|
$class = implode(' ', $fdata['description']['#class']);
|
||
|
|
}
|
||
|
|
$output .= '<div class='.$class.'">'.$fdata['description']['#text'].'</div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if($form_block){
|
||
|
|
$output .= '<div class="clear"></div>'.PHP_EOL;
|
||
|
|
$output .= '</div>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(isset($get['destination'])) {
|
||
|
|
$output .= '<input type="hidden" name="destination" value="'.$get['destination'].'">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
elseif(isset($post['destination'])) {
|
||
|
|
$output .= '<input type="hidden" name="destination" value="'.$post['destination'].'">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
elseif(isset($fields['destination'])) {
|
||
|
|
$output .= '<input type="hidden" name="destination" value="'.$fields['destination'].'">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
|
||
|
|
if(isset($get['fragment'])) {
|
||
|
|
$output .= '<input type="hidden" name="fragment" value="'.$get['fragment'].'">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
elseif(isset($post['fragment'])) {
|
||
|
|
$output .= '<input type="hidden" name="fragment" value="'.$post['fragment'].'">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
elseif(isset($fields['fragment'])) {
|
||
|
|
$output .= '<input type="hidden" name="fragment" value="'.$fields['fragment'].'">'.PHP_EOL;
|
||
|
|
}
|
||
|
|
|
||
|
|
$output .= '</form>'.PHP_EOL;
|
||
|
|
if($modal){
|
||
|
|
$output .= '
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</div>'.PHP_EOL;
|
||
|
|
$output .= '<script type="text/javascript">
|
||
|
|
$(document).ready(function() {
|
||
|
|
$(".'.$modal_id.'_open").click(function(event){
|
||
|
|
event.preventDefault();
|
||
|
|
$("#'.$modal_id.'__bg").show();
|
||
|
|
$("#'.$modal_id.'").show();
|
||
|
|
});
|
||
|
|
$(".'.$modal_id.'_close").click(function(event){
|
||
|
|
event.preventDefault();
|
||
|
|
$("#'.$modal_id.'__bg").hide();
|
||
|
|
$("#'.$modal_id.'").hide();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
</script>'.PHP_EOL;
|
||
|
|
}
|
||
|
|
return $output;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public function get($name){
|
||
|
|
if(isset($this->$name)) {
|
||
|
|
return $this->$name;
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|