PHP自動生成前端的表單框架


<?php
/**
 * 為當前所在菜單項樣式
 * @param  string $controller_name
 * @param  string $action_name
 * @param  string $style
 * @return string
 */
function activedLink($menu_item,  $style) {
    if(isset($menu_item["checked"]) )
    {
        return $style;
    }

}

/**
 * 得到gravatar頭像
 * @param  string $email
 * @return string
 */
function getGravatar($email) {
    return '';
}

/**
 * 生成input文本框
 * @param  string $name  文本框的name
 * @param  int    $size  文本框大小
 * @param  string $value 文本框默認值
 * @param  string $class css類
 * @return string
 */
function genText($name, $size, $value, $class) {
    return "<input type='text' class='{$class}' "
           . "size='{$size}' name='{$name}' value='{$value}' />";
}

/**
 * 生成input密碼框
 * @param  string $name  密碼框的name
 * @param  string $size  密碼框大小
 * @param  string $value 密碼框默認值
 * @param  string $class css類
 * @return string
 */
function genPassword($name, $size, $value, $class) {
    return "<input type='password' class='{$class}' "
           . "size='{$size}' name='{$name}' value='{$value}' />";
}

/**
 * 生成select下拉框
 * @param  string $name    下拉框的name
 * @param  array  $list    下拉框的可選項
 * @param  int    $seleced 默認項
 * @param  string $class   css類
 * @return string
 */
function genSelect($name, array $list, $selected = 0, $class = '') {
    $html = "<select name='{$name}' class='{$class}'>";

    $i = 0;
    foreach ($list as $text => $value) {
        $html .= indent() . "<option value='{$value}' ";
        if ($i == $selected) {
            $html .= " selected='selected' ";
        }
        $html .= ">{$text}</option>";

        $i++;
    }

    $html .= "</select>";

    return $html;
}

/**
 * 生成radio單選框
 * @param  string  $name    單選框的name
 * @param  string  $text    單選框顯示文本
 * @param  string  $value   單選框的值
 * @param  boolean $checked 是否選中
 * @param  string $class    css類
 * @return string
 */
function genRadio($name, $text, $value, $checked = false, $class = '') {
    $html = "<input type='radio' name='{$name}' "
            . "value='{$value}' class='{$class}' ";
    if ($checked) {
        $html .= "checked='checked'";
    }
    $html .= " /> {$text} ";

    return $html;
}

/**
 * 生成radio單選框組
 * @param  string  $name    單選框的name
 * @param  array   $list    單選框列表
 * @param  int     $checked 是否選中
 * @param  string  $class   css類
 * @return string
 */
function genRadios($name, array $list, $checked = 0, $class = '') {
    $html = '';

    $i = 0;
    foreach ($list as $text => $value) {
        $html .= $i == $checked ? genRadio($name, $text, $value, true, $class)
                                : genRadio($name, $text, $value);
        $i++;
    }

    return $html;
}

/**
 * 生成checkbox復選框
 * @param  string  $name    復選框的name
 * @param  string  $text    復選框顯示文本
 * @param  string  $value   復選框的值
 * @param  boolean $checked 是否選中
 * @param  string  $class   css類
 * @return string
 */
function genCheckbox($name, $text, $value, $checked = false, $class = '') {
    $html = "<input type='checkbox' name='{$name}[]' "
            . "value='{$value}' class='{$class}'";
    if ($checked) {
        $html .= "checked='checked'";
    }
    $html .= " /> {$text} ";

    return $html;
}

/**
 * 生成checkbox復選框組
 * @param  string  $name    復選框的name
 * @param  array   $list    復選框列表
 * @param  string  $checked 是否選中,','隔開
 * @param  string  $class   css類
 * @return string
 */
function genCheckboxs($name, array $list, $checked, $class = '') {
    $html = '';
    $checked = array_filter(explode(',', $checked), function($pos) {
        return !(empty($pos) && 0 !== $pos && '0' !== $pos);
    });

    $i = 0;
    foreach ($list as $text => $value) {
        $html .= in_array($i, $checked) ?
                     genCheckbox($name, $text, $value, true, $class)
                     : genCheckbox($name, $text, $value);
        $i++;
    }

    return $html;
}

/**
 * 生成file文件上傳
 * @param  string $name 文件域的名稱
 * @return string
 */
function genFile($name, $class = '') {
    return "<input type='file' name='{$name}' class='{$class}' />";
}

/**
 * 生成datepicker
 * @param  string $name  表單域名稱
 * @param  string $class css類
 * @return string
 */
function genDate($name, $value, $class = '') {
    $src = __APP__ . '/../Public/javascripts/admin/datepicker/images2/cal.gif';
    $id = rand_code(8);

    return "<input type='text' id='{$id}' "
           . "value='{$value}' class='{$class}' name='{$name}' />"
           . "<img src='{$src}' style='cursor:pointer; margin-left:2px' "
           . "onclick='javascript:NewCssCal(\"{$id}\", \"YYYYMMDD\")'/>";
}

/**
 * 生成textarea文本域
 * @param  string $name        文本域name
 * @param  string $value       文本域value
 * @param  int    $rows        文本域rows
 * @param  int    $cols        文本域cols
 * @param  string $placeholder 文本域holder
 * @param  string $class       css類
 * @return string
 */
function genTextarea($name, $value, $cols, $rows, $placeholder = '', $class) {
    $html = "<textarea name='{$name}' class='{$class}' "
            . "rows='{$rows}' cols='{$cols}' ";
    if (isset($value) && !empty($value)) {
        $html .= ">{$value}</textarea>";
    } else if ('' != $placeholder) {
        $html .= "placeholder='{$placeholder}'></textarea>";
    } else {
        $html .= "></textarea>";
    }

    return $html;
}

/**
 * 生成編輯器
 * @param  string $name   文本域name
 * @param  string $value  文本域value
 * @param  int    $rows   文本域rows
 * @param  int    $cols   文本域cols
 * @param  string $type   編輯器類型
 * @return string
 */
function genEditor($name, $value, $cols, $rows, $type = 'simple') {
    $id = rand_code(8);
    $html = "<textarea name='{$name}' id='{$id}' "
            . "rows='{$rows}' cols='{$cols}' ";

    if ('simple' == $type) {
        $js = "<script type='text/javascript'>$(function(){KindEditor.ready(function(K) {K.create('#{$id}',{resizeType:1,items:['fontname','fontsize','|','forecolor','hilitecolor','bold','italic','underline','removeformat','|','justifyleft','justifycenter','justifyright','insertorderedlist','insertunorderedlist','|','emoticons','image','link'],afterBlur:function(){this.sync();}});});});</script>";
    } else {
        $js = "<script type='text/javascript'>$(function(){KindEditor.ready(function(K) {K.create('#{$id}',{resizeType:1,afterBlur:function(){this.sync();}});});});</script>";
    }

    if (isset($value) && !empty($value)) {
        $html .= ">{$value}</textarea>";
    } else {
        $html .= "></textarea>";
    }

    return $html . $js;
}

/**
 * 縮進
 * @param  integer $space 縮進空格的數量
 * @return string
 */
function indent($space = 4) {
    $indent = '';
    for ($i = 0; $i < $space; $i++) {
        $indent .= ' ';
    }

    return $indent;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM