BaseLogic.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. abstract class BaseLogic {
  3. public function db() {
  4. return Doo::db ();
  5. }
  6. /**
  7. * 获得分页数据
  8. * @param unknown_type $table
  9. * @param unknown_type $condition
  10. * @param unknown_type $on_page
  11. * @param unknown_type $page_size
  12. */
  13. function get_page($table = "", $condition = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
  14. $page_c = "";
  15. $page ['previous'] = get_previous ( $on_page );
  16. $page ['on_page'] = $on_page;
  17. $total_count = $this->get_table_count ( $table, $condition );
  18. $total = intval ( $total_count / $page_size );
  19. $page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
  20. $page ['total_data'] = $total_count;
  21. $page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
  22. $i = 1;
  23. $page_max = 1;
  24. if ($on_page > 10) {
  25. $page_max = intval ( $on_page / 10 ) + 1;
  26. $i = intval ( $on_page / 10 ) * 10 - 1;
  27. }
  28. for(; $i <= $page ['total_page']; $i ++) {
  29. if ($i == $on_page) {
  30. if ($other == "page")
  31. $page_c .= '<a href="javascript:void(0);" class="current">' . $i . '</a>';
  32. else
  33. $page_c .= '&nbsp;<span class="current">' . $i . '</span>&nbsp;';
  34. } else if ($other == "page")
  35. $page_c .= '<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>';
  36. else
  37. $page_c .= '&nbsp;<a href="' . $action . $i . $get . '" class="paginate">' . $i . '</a>&nbsp;';
  38. if ($i == (10 * $page_max))
  39. break;
  40. }
  41. $page ['page'] = $page_c;
  42. $page ['lower'] = (-- $on_page) * $page_size;
  43. return $page;
  44. }
  45. /**
  46. * 获取总页数
  47. * @param unknown_type $table
  48. * @param unknown_type $condition
  49. */
  50. public function get_table_count($table = "", $condition = "") {
  51. $sql = "select count(*) as count from " . $table . " where 1 " . $condition;
  52. $query = $this->db()->query ( $sql );
  53. $result = $query->fetch ();
  54. return $result ['count'];
  55. }
  56. /**
  57. * 获取数据
  58. * @param unknown_type $data 一般选择条件
  59. * @param unknown_type $limit
  60. * @param unknown_type $condition
  61. * @param unknown_type $table
  62. */
  63. function get_list($data = array(), $condition = "", $limit = "", $table = "") {
  64. foreach ( $data as $key => $value ) {
  65. if (is_numeric ( $value ))
  66. $condition .= " and " . $key . " = " . $value;
  67. else
  68. $condition .= " and " . $key . " like '%" . $value . "%' ";
  69. }
  70. $condition = ' where 1 ' . $condition . $limit;
  71. $sql = "select * from " . $table . $condition;
  72. $query = Doo::db ()->query ( $sql );
  73. $result = $query->fetchAll ();
  74. return $result;
  75. }
  76. /**
  77. * 批量更新数据
  78. * @param unknown_type $params 一个字段为一个数组
  79. * @param unknown_type $table
  80. * @param unknown_type $id
  81. */
  82. function update_list($params = array(), $table = "", $id = "") {
  83. $pa = array ();
  84. $co = array ();
  85. $wh = array ();
  86. $count = count ( $params [$id] );
  87. foreach ( $params as $key => $value ) {
  88. array_push ( $pa, $key );
  89. if ($key != $id) {
  90. array_push ( $wh, $key . "=VALUES(" . $key . ")" );
  91. }
  92. }
  93. //获取更新内容
  94. for($i = 0; $i < $count; $i ++) {
  95. $tm = array ();
  96. foreach ( $pa as $k ) {
  97. array_push ( $tm, "'" . $params [$k] [$i] . "'" );
  98. }
  99. $tm = implode ( ",", $tm );
  100. array_push ( $co, "(" . $tm . ")" );
  101. }
  102. $pa = implode ( ",", $pa );
  103. $wh = implode ( ",", $wh );
  104. $co = implode ( ",", $co );
  105. $sql = "INSERT INTO " . $table . " (" . $pa . ") VALUES " . $co . " ON DUPLICATE KEY UPDATE " . $wh;
  106. $query = Doo::db ()->query ( $sql );
  107. }
  108. /**
  109. * 获取通行证用户信息
  110. * @param unknown_type $username
  111. */
  112. function get_sso_user_info_by_name($username = "") {
  113. require_once (SITE_PATH . '/protected/plugin/client.php');
  114. $client = new client ( ZHSSO );
  115. $userinfo = $client->zhsso_getUserbyName ( $username );
  116. $userinfo = explode ( "\r", $userinfo );
  117. $ts = json_decode ( $userinfo ['1'], true );
  118. return $ts;
  119. }
  120. }
  121. ?>