discuz_model.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. if(!defined('IN_DISCUZ')) {
  7. exit('Access Denied');
  8. }
  9. abstract class discuz_model extends discuz_base
  10. {
  11. public $data;
  12. public $methods = array();
  13. public $showmessage = 'showmessage';
  14. public $app;
  15. public $member;
  16. public $group;
  17. public $setting;
  18. public $param = array();
  19. public function __construct() {
  20. $this->app = C::app();
  21. $this->setting = &$this->app->var['setting'];
  22. $this->group = &$this->app->var['group'];
  23. $this->member = &$this->app->var['member'];
  24. parent::__construct();
  25. }
  26. public function config($name) {
  27. return getglobal('config/'.$name);
  28. }
  29. public function setting($name = null, $val = null) {
  30. if(isset($val)) {
  31. return $this->setvar($this->setting, $name, $val);
  32. }
  33. return $this->getvar($this->setting, $name);
  34. }
  35. public function table($name) {
  36. return C::t($name);
  37. }
  38. public function cache($name, $val = null) {
  39. if(isset($val)) {
  40. savecache($name, $val);
  41. $this->app->var['cache'][$name] = $val;
  42. return true;
  43. } else {
  44. if (!isset($this->app->var['cache'][$name])) {
  45. loadcache($name);
  46. }
  47. if($this->app->var['cache'][$name] === null) {
  48. return null;
  49. } else {
  50. return getglobal('cache/'.$name);
  51. }
  52. }
  53. }
  54. public function member($name = null, $val = null){
  55. if(isset($val)) {
  56. return $this->setvar($this->member, $name, $val);
  57. } else {
  58. return $this->getvar($this->member, $name);
  59. }
  60. }
  61. public function group($name = null, $val = null){
  62. if(isset($val)) {
  63. return $this->setvar($this->group, $name, $val);
  64. } else {
  65. return $this->getvar($this->group, $name);
  66. }
  67. }
  68. public function param($name = null, $val = null){
  69. if(isset($val)) {
  70. return $this->setvar($this->param, $name, $val);
  71. }
  72. return $this->getvar($this->param, $name);
  73. }
  74. public function setvar(&$var, $key, $value) {
  75. if(isset($key)) {
  76. $key = explode('/', $key);
  77. $p = &$var;
  78. foreach ($key as $k) {
  79. if(!isset($p[$k]) || !is_array($p[$k])) {
  80. $p[$k] = array();
  81. }
  82. $p = &$p[$k];
  83. }
  84. $p = $value;
  85. } else {
  86. $var = $value;
  87. }
  88. return true;
  89. }
  90. public function getvar(&$var, $key = null) {
  91. if(isset($key)) {
  92. $key = explode('/', $key);
  93. foreach ($key as $k) {
  94. if (!isset($var[$k])) {
  95. return null;
  96. }
  97. $var = &$var[$k];
  98. }
  99. }
  100. return $var;
  101. }
  102. public function showmessage() {
  103. if(!empty($this->showmessage) && is_callable($this->showmessage)) {
  104. $p = func_get_args();
  105. if(is_string($this->showmessage)) {
  106. $fn = $this->showmessage;
  107. switch (func_num_args()) {
  108. case 0: return $fn();break;
  109. case 1: return $fn($p[0]);break;
  110. case 2: return $fn($p[0], $p[1]);break;
  111. case 3: return $fn($p[0], $p[1], $p[2]);exit;break;
  112. case 4: return $fn($p[0], $p[1], $p[2], $p[3]);break;
  113. case 5: return $fn($p[0], $p[1], $p[2], $p[3], $p[4]);break;
  114. default: return call_user_func_array($this->showmessage, $p);break;
  115. }
  116. } else {
  117. return call_user_func_array($this->showmessage, $p);
  118. }
  119. } else {
  120. return func_get_args();
  121. }
  122. }
  123. public function attach_before_method($name, $fn) {
  124. $this->methods[$name][0][] = $fn;
  125. }
  126. public function attach_after_method($name, $fn) {
  127. $this->methods[$name][1][] = $fn;
  128. }
  129. public function attach_before_methods($name, $methods){
  130. if(!empty($methods)) {
  131. foreach($methods as $method) {
  132. $this->methods[$name][0][] = $method;
  133. }
  134. }
  135. }
  136. public function attach_after_methods($name, $methods){
  137. if(!empty($methods)) {
  138. foreach($methods as $method) {
  139. $this->methods[$name][1][] = $method;
  140. }
  141. }
  142. }
  143. abstract protected function _init_parameters($parameters);
  144. }
  145. ?>