Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!defined('IN_CONTEXT')) die('access violation error!');
- /**
- * Toolkits
- *
- * @package tool
- */
- class Toolkit {
- /**
- * Generate table name according to object name
- *
- * @access public
- * @static
- * @param string $class_name The class name to be processed
- * @return string
- */
- public static function transformClassName($class_name) {
- $t_class_name = '';
- $up_case_idx =& self::_getUpCaseIdx($class_name);
- if (sizeof($up_case_idx) == 0) {
- $t_class_name = $class_name;
- } else {
- $start_idx = 0;
- for($i = 0; $i < sizeof($up_case_idx); $i++) {
- $t_class_name .= '_'.substr($class_name,
- $start_idx, $up_case_idx[$i] - $start_idx);
- $start_idx = $up_case_idx[$i];
- }
- $t_class_name .= '_'.substr($class_name,
- $start_idx);
- $t_class_name = substr($t_class_name, 1);
- }
- return strtolower($t_class_name);
- }
- /**
- * Get index of up-cased character in a string
- *
- * @access private
- * @static
- * @param string $class_name The class name to be processed
- * @return array
- */
- private static function &_getUpCaseIdx($class_name) {
- $up_case_idx = array();
- for ($i = 1; $i < strlen($class_name); $i++) {
- if (ord($class_name[$i]) >= 65 &&
- ord($class_name[$i]) <= 90) {
- $up_case_idx[] = $i;
- }
- }
- return $up_case_idx;
- }
- /**
- * Make plural word
- *
- * @access public
- * @static
- * @param string $noun The word to be processed
- * @return string
- */
- public static function pluralize($noun) {
- if (preg_match('/(s|sh|ch|x)$/i', $noun)) {
- $pl_noun = $noun.'es';
- } else if (preg_match('/y$/i', $noun)) {
- if (preg_match('/([aeiou]y)$/i', $noun)) {
- $pl_noun = $noun.'s';
- } else {
- $pl_noun = substr($noun, 0, strlen($noun) - 1).'ies';
- }
- } else {
- $pl_noun = $noun.'s';
- }
- return $pl_noun;
- }
- /**
- * Display text of the value
- *
- * @access public
- * @static
- * @param string $value The target value
- * @param array $arr_text The text collection for values
- * @return string
- */
- public static function switchText($value, $arr_text) {
- if(empty($value)){
- $value=0;
- }
- return $arr_text[$value];
- }
- /**
- * Build an array for HTML select options from the array of selected records
- *
- * @access public
- * @static
- * @param array &$record_array The array of selected records
- * @param string $value_field The name of the field whose value will be used as option value
- * @param string $text_field The name of the field whose value will be used as option text
- * @param array $first_option The first option in select list
- * @param boolean $translate Whether translate the text field using __()
- * @return array
- */
- public static function &toSelectArray(&$record_array, $value_field,
- $text_field, $first_option = array(), $translate = false) {
- $select_array = array();
- if (sizeof($record_array) > 0) {
- if (sizeof($first_option) == 2) {
- $select_array[$first_option[0]] = $first_option[1];
- }
- foreach ($record_array as $record) {
- $select_array[$record->$value_field] =
- $translate?__($record->$text_field):$record->$text_field;
- }
- }
- return $select_array;
- }
- /**
- * Build an array for switchText() function from the array of selected records
- *
- * @access public
- * @static
- * @param array &$record_array The array of selected records
- * @param string $value_field The name of the field whose value will be used to match the given value
- * @param string $text_field The name of the field whose value will be displayed for matched value
- * @return array
- */
- public static function &toSwitchArray(&$record_array, $value_field,
- $text_field) {
- $switch_array = array();
- if (sizeof($record_array) > 0) {
- foreach($record_array as $record) {
- $switch_array[$record->$value_field] = $record->$text_field;
- }
- }
- return $switch_array;
- }
- /**
- * Get all language names
- *
- * @access public
- * @static
- * @return array
- */
- public static function &loadAllLangs() {
- $o_language = new Language();
- $all_langs =& $o_language->findAll();
- return $all_langs;
- }
- /**
- * Get all user roles
- *
- * @access public
- * @static
- * @return array
- */
- public static function &loadAllRoles($ignore = array()) {
- $where = false;
- $params = false;
- if (sizeof($ignore) > 0) {
- $ign_str = '';
- $params = array();
- foreach ($ignore as $ign_role) {
- $ign_str .= ", ?";
- $params[] = $ign_role;
- }
- $where = "name NOT IN (".substr($ign_str, 2).")";
- }
- $o_role = new Role();
- $all_roles =& $o_role->findAll($where, $params);
- return $all_roles;
- }
- /**
- * Arrange template position information into select array
- *
- * @access public
- * @static
- */
- public static function &reformPositions() {
- $positions = array();
- foreach (TplInfo::$positions as $position) {
- $positions[$position] = __($position);
- }
- return $positions;
- }
- function wrap_MB($str, $slen, $break) {
- mb_internal_encoding(__('charset'));
- $start = 0;
- $length = mb_strlen($str);
- $lines = array();
- while ($start <= $length) {
- $lines[] = mb_substr($str, $start, $slen);
- $start += $slen;
- }
- return implode($break, $lines);
- }
- /**
- * Multibyte sub string wrapper
- *
- * @access public
- * @static
- * @param string $str The full string
- * @param int $start Start index
- * @param int $length Sub-string length
- * @return string
- */
- public static function substr_MB($str, $start, $length, $wrap = false, $len = 75, $break = "<br />\n") {
- if(function_exists("mb_substr")){
- mb_internal_encoding(__('charset'));
- if (!$wrap) {
- return mb_substr($str, $start, $length);
- } else {
- return self::wrap_MB(mb_substr($str, $start, $length), $len, $break);
- }
- } else {
- $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
- $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
- $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
- $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
- preg_match_all($re["utf-8"], $str, $match);
- if(count($match[0]) <= $length) return $str;
- $slice = join("",array_slice($match[0], $start, $length));
- return $slice;
- }
- }
- /**
- * get string length
- */
- public static function strlen_MB($str, $charset = 'UTF-8') {
- if(function_exists("mb_substr")){
- return mb_strlen($str, $charset);
- } else {
- $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
- $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
- $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
- $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
- preg_match_all($re["utf-8"], $str, $match);
- return sizeof($match[0]);
- }
- }
- /**
- * Generate OK result in json format
- *
- * @access public
- * @static
- * @param array $params Parameters should be included in result
- * @return string
- */
- public static function jsonOK($params = array()) {
- $result = array();
- $result['result'] = 'OK';
- if (sizeof($params) > 0) {
- $result = array_merge($result, $params);
- }
- return json_encode($result);
- }
- /**
- * Generate error result in json format
- *
- * @access public
- * @static
- * @param string $errmsg Error message
- * @param array $params Parameters should be included in result
- * @return string
- */
- public static function jsonERR($errmsg, $params = array()) {
- $result = array();
- $result['result'] = 'ERROR';
- $result['errmsg'] = $errmsg;
- if (sizeof($params) > 0) {
- $result = array_merge($result, $params);
- }
- return json_encode($result);
- }
- /**
- * Generate random string
- *
- * @access public
- * @static
- * @param int $len Length of random string
- * @param boolean $alphanum Only use letters and digits
- * @return string
- */
- public static function randomStr($len = 6, $alphanum = true) {
- $chars = 'abcdefghijklmnopqrstuvwxyz'
- .'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- .'1234567890';
- if (!$alphanum) {
- $chars .= '~!@#$%^&*()_-`[]{}|";:,.<>/?';
- }
- $randstr = '';
- if (!is_integer($len) || $len < 6) {
- $len = 6;
- }
- for ($i = 0; $i < $len; $i++) {
- $idx = mt_rand(0, strlen($chars) - 1);
- $randstr .= substr($chars, $idx, 1);
- }
- return $randstr;
- }
- ...........................................................................
- ..........................................
- ..................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement