globals.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Class global_helper
  4. *
  5. * 全局方法,主要使用在模板中
  6. */
  7. namespace helper;
  8. class globals
  9. {
  10. public static function rules($rule, $name, $validate, $invalidMessage = array(), $extRules = array())
  11. {
  12. $ruleText = isset($validate[$rule]) ? $validate[$rule] : $rule;
  13. $message = isset($invalidMessage[$name]) && isset($invalidMessage[$name][$rule]) ? $invalidMessage[$name][$rule] : '';
  14. $ruleValue = isset($extRules[$name]) && isset($extRules[$name][$rule]) ? $extRules[$name][$rule] : '';
  15. if (in_array($rule, array('length', 'minlength', 'maxlength', 'rangeof', 'sets'))) {
  16. return '<label class="w160">' . $ruleText . '&nbsp;<input type="text" class="scinput display_left" name="rules[extRules][' . $name . '][' . $rule . ']" id="rules[extRules][' . $name . '][' . $rule . ']" placeholder="' . $ruleText . '规则" value="' . $ruleValue . '" />&nbsp;<input type="text" class="scinput" name="rules[invalidMessage][' . $name . '][' . $rule . ']" id="rules[invalidMessage][' . $name . '][' . $rule . ']" placeholder="' . $ruleText . '错误提示" value="' . $message . '" /></label>';
  17. }
  18. return '<label class="w160">' . $ruleText . '&nbsp;<input type="text" class="scinput" name="rules[invalidMessage][' . $name . '][' . $rule . ']" id="rules[invalidMessage][' . $name . '][' . $rule . ']" placeholder="' . $ruleText . '错误提示" value="' . $message . '" /></label>';
  19. }
  20. /**
  21. * 判断string是否包含html标签
  22. *
  23. * @param $string
  24. * @return bool
  25. */
  26. public static function hasHtmlTag($string)
  27. {
  28. return $string != strip_tags($string);
  29. }
  30. /**
  31. * 生成带参数的URL
  32. *
  33. * @param String $url
  34. * @param Array $param
  35. * @return String
  36. */
  37. public static function createURL($url, $param = array())
  38. {
  39. return \Qii::getInstance('Request')->url($url, $param);
  40. }
  41. /**
  42. * 跳转连接
  43. *
  44. */
  45. public static function location($url, $param = array())
  46. {
  47. $url = self::createURL($url, $param);
  48. header('Location: ' . $url);
  49. die();
  50. }
  51. /**
  52. * POST数据的时候需要将此参数带上
  53. *
  54. */
  55. public static function safeForm()
  56. {
  57. return;
  58. }
  59. /**
  60. * 设置Cookie
  61. *
  62. */
  63. public static function cookie($key, $value = null, $expired = 86400)
  64. {
  65. if ($value === null) return isset($_COOKIE[$key]) ? $_COOKIE[$key] : '';
  66. setcookie($key, $value, time() + $expired, '/');
  67. }
  68. /**
  69. * 获取get值
  70. *
  71. */
  72. public static function get($key)
  73. {
  74. return \Qii::segment($key);
  75. }
  76. /**
  77. * 获取当前连接地址
  78. *
  79. */
  80. public static function getCURL()
  81. {
  82. return \Qii::instance('URI')->getCurrentURL();
  83. }
  84. /**
  85. * 获取网站路径的URL
  86. * @return string
  87. */
  88. public static function pathUrl()
  89. {
  90. return \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Request\Http')->getWebHost() . \Qii\Autoloader\Psr4::getInstance()->loadClass('\Qii\Request\Http')->getPath();
  91. }
  92. /**
  93. * 获取文件的路径
  94. * @param String $file
  95. * @return String
  96. */
  97. public static function getFile($file)
  98. {
  99. return self::pathUrl() . '/' . ltrim($file, '/');
  100. }
  101. /**
  102. * 将绝对地址补成全路径URL
  103. * @param String $url
  104. * @return string
  105. */
  106. public static function getFullUrl($url = 'index', $ext = '.html')
  107. {
  108. if (is_array($url)) {
  109. $url = join('/', $url);
  110. }
  111. $url = str_replace('//', '/', $url);
  112. $query = parse_url($url);
  113. $url = $query['path'];
  114. $params = array();
  115. if (isset($query['query'])) {
  116. parse_str($query['query'], $params);
  117. }
  118. if ($url == '/') $url = 'index';
  119. if ($ext == null) {
  120. $ext = '';
  121. }
  122. $query = count($params) > 0 ? '?' . http_build_query($params) : '';
  123. //去掉url中末尾的扩展名,避免重复
  124. $url = preg_replace('/(\.' . $ext . ')$/', '', $url);
  125. return rtrim(self::pathUrl(), '/') . '/' . ltrim($url, '/') . $ext . (count($params) > 0 ? '?' . http_build_query($params) : '');
  126. }
  127. /**
  128. * 获取Image全路径
  129. * @param String $image
  130. * @return string
  131. */
  132. public static function getImage($image)
  133. {
  134. $image = explode('.', $image);
  135. $ext = '.' . array_pop($image);
  136. return self::getFullUrl('/static/images/' . join('.', $image), $ext);
  137. }
  138. /**
  139. * 获取Css全路径
  140. * @param String $css
  141. * @return string
  142. */
  143. public static function getCss($css)
  144. {
  145. return self::getFullUrl('/static/css/' . $css, '.css');
  146. }
  147. /**
  148. * 获取JS全路径
  149. * @param String $js
  150. * @return string
  151. */
  152. public static function getJS($js)
  153. {
  154. return self::getFullUrl('/static/js/' . $js, '.js');
  155. }
  156. /**
  157. * 通过制定url路径,不局限于static/images目录返回全路径url
  158. * @param $image
  159. * @return string
  160. */
  161. public static function getImageFullUrl($image)
  162. {
  163. return self::getSourceFullUrl($image);
  164. }
  165. /**
  166. * 通过path直接返回全路径
  167. *
  168. * @param $url
  169. * @return string
  170. */
  171. public static function getSourceFullUrl($url)
  172. {
  173. $ext = pathinfo($url, PATHINFO_EXTENSION);
  174. $url = preg_replace('/' . $ext . '$/', '', $url);
  175. return self::getFullUrl($url, $ext);
  176. }
  177. }