Skip to content

Commit

Permalink
add form api
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggngyv committed Aug 6, 2017
1 parent 76b7f31 commit 7efe57b
Show file tree
Hide file tree
Showing 4 changed files with 1,488 additions and 0 deletions.
110 changes: 110 additions & 0 deletions core/includes/common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use Hunter\Core\Utility\Timer;
use Hunter\Core\Password\Password;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response\RedirectResponse;
use Hunter\Core\FormApi\Layui;
use Hunter\Core\FormApi\NoHtml;

//file scan
function file_scan($dir, $regx, $options = array(), $depth = 1) {
Expand Down Expand Up @@ -257,6 +259,114 @@ function hunter_convert_to_utf8($data) {
return $out;
}

/**
* Converts an associative array to attribute string.
*/
function hunter_attributes(array $attributes = array()) {
foreach ($attributes as $attribute => &$data) {
$data = implode(' ', (array) $data);
$data = $attribute . '="' . htmlspecialchars($data, ENT_QUOTES, 'UTF-8') . '"';
}
return $attributes ? ' ' . implode(' ', $attributes) : '';
}

/**
* Sets HTML attributes based on element properties.
*/
function element_set_attributes(array &$field, array $map) {
foreach ($map as $property => $attribute) {
// If the key is numeric, the attribute name needs to be taken over.
if (is_int($property)) {
$property = '#' . $attribute;
}
// Do not overwrite already existing attributes.
if (isset($field[$property]) && !isset($field['#attributes'][$attribute])) {
$field['#attributes'][$attribute] = $field[$property];
}
}
}

/**
* Forms an associative array from a linear array.
*/
function hunter_map_assoc($array, $function = NULL) {
$array = !empty($array) ? array_combine($array, $array) : array();
if (is_callable($function)) {
$array = array_map($function, $array);
}
return $array;
}

/**
* render_form api
*/
function render_form($type, $fields, $redirect = '#') {
if($type == 'layui'){
$form = Layui::create()->start($redirect);
}else{
$form = NoHtml::create()->start($redirect);
}

foreach ($fields as $name => $field) {
if (!isset($field['#name'])) {
$field['#name'] = $name;
}

if(!isset($field['#attributes'])){
$field['#attributes'] = array();
}

if(!isset($field['#value'])){
$field['#value'] = isset($field['#default_value']) ? $field['#default_value'] : '';
}

switch ($field['#type'])
{
case 'textfield':
$field['#attributes']['type'] = 'text';
element_set_attributes($field, array('id', 'name', 'value', 'size', 'maxlength'));
$form->input($name, $field);
break;
case 'radios':
$field['#attributes']['type'] = 'radio';
element_set_attributes($field, array('id', 'name'));
$form->radio($name, $field);
break;
case 'checkboxes':
$field['#attributes']['type'] = 'checkbox';
element_set_attributes($field, array('id', 'name'));
$form->checkbox($name, $field);
break;
case 'file':
element_set_attributes($field, array('id', 'name', 'value', 'size'));
$field['#attributes']['class'][] = 'layui-input layui-input-inline';
$form->file($name, $field);
break;
case 'select':
element_set_attributes($field, array('id', 'name', 'size'));
$form->select($name, $field);
break;
case 'textarea':
element_set_attributes($field, array('id', 'name', 'cols', 'rows'));
$field['#attributes']['class'][] = 'layui-textarea';
$form->textarea($name, $field);
break;
case 'hidden':
$form->hidden($name, $field['#value']);
break;
case 'img':
$form->img($name, $field);
break;
default:
$field['#attributes']['type'] = 'submit';
$form->submit($name, $field);
break;
}
}

return $form->end();
}

if (!function_exists('e')) {
/**
* Escape HTML entities in a string.
Expand Down
142 changes: 142 additions & 0 deletions core/lib/Hunter/FormApi/Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Hunter\Core\FormApi;


abstract class Form {
/**
* to create a form with post method
*/
const POST = 'post';

/**
* to create a form with get method
*/
const GET = 'post';

/**
* to create a submit button or input
*/
const SUBMIT = 'submit';

/**
* to create a button
*/
const BUTTON = 'button';

/**
* to create a number input
*/
const NUMBER = 'number';

/**
* to create an input hidden
*/
const HIDDEN = 'hidden';

/**
* to create a text input
*/
const TEXT = 'text';

/**
* to create a password input
*/
const PASSWORD = 'password';

/**
* to create an email input
*/
const EMAIL = 'email';

/**
* to create a date input
*/
const DATE = 'date';

/**
* to create a datetime input
*/
const DATETIME = 'datetime';

/**
* to create a phone input
*/
const TEL = 'tel';

/**
* to create a url input
*/
const URL = 'url';

/**
* to create a time input
*/
const TIME = 'time';

/**
* to create a text input
*/
const RANGE = 'range';

/**
* to create a color input
*/
const COLOR = 'color';

/**
* to create a search input
*/
const SEARCH = 'search';

/**
* to create a week input
*/
const WEEK = 'week';

/**
* to create a checkbox input
*/
const CHECKBOX = 'checkbox';

/**
* to create a radio input
*/
const RADIO = 'radio';

/**
* option to get the result
*/
const GET_TIME = 0;

/**
* option to have a datetime input
*/
const GET_DATETIME = 1;

/**
* to create a file input
*/
const FILE = 'file';

/**
* to create a reset input
*/
const RESET = 'reset';

/**
* to create a datetime-local input
*/
const DATETIME_LOCAL = 'datetime-local';

/**
* to create a image input
*/
const IMAGE = 'image';

/**
* to create a month input
*/
const MONTH = 'month';

}
Loading

0 comments on commit 7efe57b

Please sign in to comment.