Advertisement
Guest User

Untitled

a guest
Feb 18th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.76 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('IN_CONTEXT')) die('access violation error!');
  4.  
  5. /**
  6. * Toolkits
  7. *
  8. * @package tool
  9. */
  10. class Toolkit {
  11. /**
  12. * Generate table name according to object name
  13. *
  14. * @access public
  15. * @static
  16. * @param string $class_name The class name to be processed
  17. * @return string
  18. */
  19. public static function transformClassName($class_name) {
  20. $t_class_name = '';
  21.  
  22. $up_case_idx =& self::_getUpCaseIdx($class_name);
  23.  
  24. if (sizeof($up_case_idx) == 0) {
  25. $t_class_name = $class_name;
  26. } else {
  27. $start_idx = 0;
  28. for($i = 0; $i < sizeof($up_case_idx); $i++) {
  29. $t_class_name .= '_'.substr($class_name,
  30. $start_idx, $up_case_idx[$i] - $start_idx);
  31. $start_idx = $up_case_idx[$i];
  32. }
  33. $t_class_name .= '_'.substr($class_name,
  34. $start_idx);
  35.  
  36. $t_class_name = substr($t_class_name, 1);
  37. }
  38.  
  39. return strtolower($t_class_name);
  40. }
  41.  
  42. /**
  43. * Get index of up-cased character in a string
  44. *
  45. * @access private
  46. * @static
  47. * @param string $class_name The class name to be processed
  48. * @return array
  49. */
  50. private static function &_getUpCaseIdx($class_name) {
  51. $up_case_idx = array();
  52.  
  53. for ($i = 1; $i < strlen($class_name); $i++) {
  54. if (ord($class_name[$i]) >= 65 &&
  55. ord($class_name[$i]) <= 90) {
  56. $up_case_idx[] = $i;
  57. }
  58. }
  59.  
  60. return $up_case_idx;
  61. }
  62.  
  63. /**
  64. * Make plural word
  65. *
  66. * @access public
  67. * @static
  68. * @param string $noun The word to be processed
  69. * @return string
  70. */
  71. public static function pluralize($noun) {
  72. if (preg_match('/(s|sh|ch|x)$/i', $noun)) {
  73. $pl_noun = $noun.'es';
  74. } else if (preg_match('/y$/i', $noun)) {
  75. if (preg_match('/([aeiou]y)$/i', $noun)) {
  76. $pl_noun = $noun.'s';
  77. } else {
  78. $pl_noun = substr($noun, 0, strlen($noun) - 1).'ies';
  79. }
  80. } else {
  81. $pl_noun = $noun.'s';
  82. }
  83.  
  84. return $pl_noun;
  85. }
  86.  
  87. /**
  88. * Display text of the value
  89. *
  90. * @access public
  91. * @static
  92. * @param string $value The target value
  93. * @param array $arr_text The text collection for values
  94. * @return string
  95. */
  96. public static function switchText($value, $arr_text) {
  97. if(empty($value)){
  98. $value=0;
  99. }
  100. return $arr_text[$value];
  101. }
  102.  
  103. /**
  104. * Build an array for HTML select options from the array of selected records
  105. *
  106. * @access public
  107. * @static
  108. * @param array &$record_array The array of selected records
  109. * @param string $value_field The name of the field whose value will be used as option value
  110. * @param string $text_field The name of the field whose value will be used as option text
  111. * @param array $first_option The first option in select list
  112. * @param boolean $translate Whether translate the text field using __()
  113. * @return array
  114. */
  115. public static function &toSelectArray(&$record_array, $value_field,
  116. $text_field, $first_option = array(), $translate = false) {
  117. $select_array = array();
  118. if (sizeof($record_array) > 0) {
  119. if (sizeof($first_option) == 2) {
  120. $select_array[$first_option[0]] = $first_option[1];
  121. }
  122. foreach ($record_array as $record) {
  123. $select_array[$record->$value_field] =
  124. $translate?__($record->$text_field):$record->$text_field;
  125. }
  126. }
  127.  
  128. return $select_array;
  129. }
  130.  
  131. /**
  132. * Build an array for switchText() function from the array of selected records
  133. *
  134. * @access public
  135. * @static
  136. * @param array &$record_array The array of selected records
  137. * @param string $value_field The name of the field whose value will be used to match the given value
  138. * @param string $text_field The name of the field whose value will be displayed for matched value
  139. * @return array
  140. */
  141. public static function &toSwitchArray(&$record_array, $value_field,
  142. $text_field) {
  143. $switch_array = array();
  144. if (sizeof($record_array) > 0) {
  145. foreach($record_array as $record) {
  146. $switch_array[$record->$value_field] = $record->$text_field;
  147. }
  148. }
  149.  
  150. return $switch_array;
  151. }
  152.  
  153. /**
  154. * Get all language names
  155. *
  156. * @access public
  157. * @static
  158. * @return array
  159. */
  160. public static function &loadAllLangs() {
  161. $o_language = new Language();
  162. $all_langs =& $o_language->findAll();
  163. return $all_langs;
  164. }
  165.  
  166. /**
  167. * Get all user roles
  168. *
  169. * @access public
  170. * @static
  171. * @return array
  172. */
  173. public static function &loadAllRoles($ignore = array()) {
  174. $where = false;
  175. $params = false;
  176. if (sizeof($ignore) > 0) {
  177. $ign_str = '';
  178. $params = array();
  179. foreach ($ignore as $ign_role) {
  180. $ign_str .= ", ?";
  181. $params[] = $ign_role;
  182. }
  183. $where = "name NOT IN (".substr($ign_str, 2).")";
  184. }
  185. $o_role = new Role();
  186. $all_roles =& $o_role->findAll($where, $params);
  187. return $all_roles;
  188. }
  189.  
  190. /**
  191. * Arrange template position information into select array
  192. *
  193. * @access public
  194. * @static
  195. */
  196. public static function &reformPositions() {
  197. $positions = array();
  198. foreach (TplInfo::$positions as $position) {
  199. $positions[$position] = __($position);
  200. }
  201. return $positions;
  202. }
  203.  
  204. function wrap_MB($str, $slen, $break) {
  205. mb_internal_encoding(__('charset'));
  206. $start = 0;
  207. $length = mb_strlen($str);
  208. $lines = array();
  209. while ($start <= $length) {
  210. $lines[] = mb_substr($str, $start, $slen);
  211. $start += $slen;
  212. }
  213. return implode($break, $lines);
  214. }
  215.  
  216. /**
  217. * Multibyte sub string wrapper
  218. *
  219. * @access public
  220. * @static
  221. * @param string $str The full string
  222. * @param int $start Start index
  223. * @param int $length Sub-string length
  224. * @return string
  225. */
  226. public static function substr_MB($str, $start, $length, $wrap = false, $len = 75, $break = "<br />\n") {
  227. if(function_exists("mb_substr")){
  228. mb_internal_encoding(__('charset'));
  229. if (!$wrap) {
  230. return mb_substr($str, $start, $length);
  231. } else {
  232. return self::wrap_MB(mb_substr($str, $start, $length), $len, $break);
  233. }
  234. } else {
  235. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  236. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  237. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  238. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  239. preg_match_all($re["utf-8"], $str, $match);
  240. if(count($match[0]) <= $length) return $str;
  241. $slice = join("",array_slice($match[0], $start, $length));
  242. return $slice;
  243. }
  244. }
  245.  
  246. /**
  247. * get string length
  248. */
  249. public static function strlen_MB($str, $charset = 'UTF-8') {
  250. if(function_exists("mb_substr")){
  251. return mb_strlen($str, $charset);
  252. } else {
  253. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  254. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  255. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  256. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  257. preg_match_all($re["utf-8"], $str, $match);
  258. return sizeof($match[0]);
  259. }
  260. }
  261.  
  262. /**
  263. * Generate OK result in json format
  264. *
  265. * @access public
  266. * @static
  267. * @param array $params Parameters should be included in result
  268. * @return string
  269. */
  270. public static function jsonOK($params = array()) {
  271. $result = array();
  272. $result['result'] = 'OK';
  273. if (sizeof($params) > 0) {
  274. $result = array_merge($result, $params);
  275. }
  276. return json_encode($result);
  277. }
  278.  
  279. /**
  280. * Generate error result in json format
  281. *
  282. * @access public
  283. * @static
  284. * @param string $errmsg Error message
  285. * @param array $params Parameters should be included in result
  286. * @return string
  287. */
  288. public static function jsonERR($errmsg, $params = array()) {
  289. $result = array();
  290. $result['result'] = 'ERROR';
  291. $result['errmsg'] = $errmsg;
  292. if (sizeof($params) > 0) {
  293. $result = array_merge($result, $params);
  294. }
  295. return json_encode($result);
  296. }
  297.  
  298. /**
  299. * Generate random string
  300. *
  301. * @access public
  302. * @static
  303. * @param int $len Length of random string
  304. * @param boolean $alphanum Only use letters and digits
  305. * @return string
  306. */
  307. public static function randomStr($len = 6, $alphanum = true) {
  308. $chars = 'abcdefghijklmnopqrstuvwxyz'
  309. .'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  310. .'1234567890';
  311. if (!$alphanum) {
  312. $chars .= '~!@#$%^&*()_-`[]{}|";:,.<>/?';
  313. }
  314. $randstr = '';
  315. if (!is_integer($len) || $len < 6) {
  316. $len = 6;
  317. }
  318. for ($i = 0; $i < $len; $i++) {
  319. $idx = mt_rand(0, strlen($chars) - 1);
  320. $randstr .= substr($chars, $idx, 1);
  321. }
  322.  
  323. return $randstr;
  324. }
  325. ...........................................................................
  326. ..........................................
  327. ..................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement