model.sys.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. class Model
  3. {
  4. var $db_engine = "db";
  5. var $db_type = "mysql";
  6. var $langid = "zh";//指定语言包
  7. function Model($db=false)
  8. {
  9. $db ? $this->run_db("database","db") : $this->load_db();
  10. //指定langid
  11. if($_SESSION["sys_lang_id"])
  12. {
  13. $this->langid = $_SESSION["sys_lang_id"];
  14. }
  15. }
  16. function load_db()
  17. {
  18. $database_config_file = APP_ROOT.'database.config.php';
  19. $_db_type["prefix"] = "qinggan_";
  20. if(file_exists($database_config_file))
  21. {
  22. include($database_config_file);
  23. }
  24. $this->db->prefix = $_db_type["prefix"];
  25. return true;
  26. }
  27. //加载model层
  28. function load_model($model_name,$db)
  29. {
  30. if(is_array($model_name))
  31. {
  32. foreach($model_name AS $key=>$value)
  33. {
  34. $this->load_model($value,$db);
  35. }
  36. }
  37. else
  38. {
  39. $this->_load_model($model_name,$db);
  40. }
  41. return true;
  42. }
  43. //用于内部使用的加载模块
  44. function _load_model($model_name,$db)
  45. {
  46. if(!$model_name)
  47. {
  48. return false;
  49. }
  50. $model_name = strtolower($model_name);
  51. if(file_exists(LIBS."models/".$model_name.".php") || file_exists(LIBS."models/".$this->db_type."/".$model_name.".php"))
  52. {
  53. $model_file = file_exists(LIBS."models/".$this->db_type."/".$model_name.".php") ? LIBS."models/".$this->db_type."/".$model_name.".php" : LIBS."models/".$model_name.".php";
  54. $set_name = $model_name;
  55. }
  56. else
  57. {
  58. $model_file = file_exists(APP.'models_'.$this->db_type.'/'.$model_name.'.php') ? APP.'models_'.$this->db_type.'/'.$model_name.'.php' : APP.'models/'.$model_name.'.php';
  59. if(!file_exists($model_file))
  60. {
  61. exit('error: unable to load the model: '.basename($model_file));
  62. }
  63. $set_name = $model_name.'_m';
  64. }
  65. //加载模块文件
  66. include_once($model_file);
  67. $this->$set_name = new $set_name();
  68. $this->$set_name->db = $db;
  69. unset($set_name,$model_file,$model_name);
  70. return true;
  71. }
  72. //运行连接数据库引挈
  73. function run_db($data_file='database',$engine='db')
  74. {
  75. if(!$engine) $engine = 'db';
  76. $this->db_engine = $engine;
  77. if($this->$engine && is_object($this->$engine))
  78. {
  79. return true;
  80. }
  81. if(!file_exists(APP_ROOT.$data_file.'.config.php'))
  82. {
  83. exit('error: unable to load the database: '.$data_file.'.config.php');
  84. }
  85. //加载数据库配置文件
  86. include(APP_ROOT.$data_file.'.config.php');
  87. $this->db_type = $_db_config['type'];//计算要加载的Model层对应的数据库类型
  88. //加载数据库引挈
  89. $db_file = LIBS.'db_engine/'.($_db_config['engine'] ? $_db_config['engine'] : $_db_config['type']).'.php';
  90. if(!file_exists($db_file))
  91. {
  92. exit("error: unable to load the database engine file");
  93. }
  94. include_once($db_file);
  95. $class_name = 'db_'.($_db_config['engine'] ? $_db_config['engine'] : $_db_config['type']);
  96. $this->$engine = new $class_name($_db_config);
  97. return true;
  98. }
  99. //加载数据库里的语言包
  100. //rs:初始数组,且要求初始数据中必须包含langid和id两个值
  101. //tbl:指定是属于哪个表的
  102. function sys_merge_lang($rs,$tbl="")
  103. {
  104. //如果没有传值过来,返回false
  105. if(!$rs)
  106. {
  107. return false;
  108. }
  109. //如果传过来的值不是数组,则返回原值
  110. if(!is_array($rs) || count($rs)<1)
  111. {
  112. return $rs;
  113. }
  114. //如果没有指定表名,则返回原值
  115. if(!$tbl)
  116. {
  117. return $rs;
  118. }
  119. $engine = $this->db_engine;
  120. $this->db_sql = $this->$engine;
  121. if(!$rs["langid"] && !$rs["id"])
  122. {
  123. $t_rs = array();
  124. foreach($rs AS $key=>$value)
  125. {
  126. if(!$value["langid"] || !$value["id"])
  127. {
  128. return $rs;
  129. }
  130. if($value["langid"] == $this->langid)
  131. {
  132. return $rs;
  133. }
  134. $t_rs[$value["id"]] = $value;
  135. }
  136. unset($rs);
  137. $rs = $t_rs;
  138. ksort($rs);
  139. //读取数据
  140. $sql = "SELECT tbl_id,keys,val FROM ".$this->db_sql->prefix."langs WHERE langid='".$this->langid."' AND tbl='".$tbl."' ORDER BY tbl_id ASC";
  141. $rslist = $this->db_sql->get_all($sql);
  142. if(!$rslist)
  143. {
  144. return $rs;
  145. }
  146. $tmp_rslist = array();
  147. foreach($rslist AS $key=>$value)
  148. {
  149. $tmp_rslist[$value["tbl_id"]][$value["keys"]] = $value["val"];
  150. }
  151. unset($rslist);
  152. $rslist = $tmp_rslist;
  153. if(!$rslist || count($rslist)<1)
  154. {
  155. return $rs;
  156. }
  157. foreach($rslist AS $key=>$value)
  158. {
  159. $rs[$key] = array_merge($rs[$key],$value);
  160. }
  161. return $rs;
  162. }
  163. else
  164. {
  165. if(!$rs["id"] || !$rs["langid"])
  166. {
  167. return $rs;
  168. }
  169. $sql = "SELECT keys,val FROM ".$this->db_sql->prefix."langs WHERE langid='".$this->langid."' AND tbl='".$tbl."' AND tpl_id='".$rs["id"]."'";
  170. $rslist = $this->db_sql->get_all($sql);
  171. if(!$rslist)
  172. {
  173. return $rs;
  174. }
  175. foreach($rslist AS $key=>$value)
  176. {
  177. $rs[$value["keys"]] = $value["val"];
  178. }
  179. return $rs;
  180. }
  181. }
  182. }
  183. ?>