index.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. class index {
  4. private $setting, $catid, $contentid, $siteid, $mood_id;
  5. public function __construct() {
  6. $this->setting = getcache('mood_program', 'commons');
  7. $this->mood_id = isset($_GET['id']) ? $_GET['id'] : '';
  8. if(!preg_match("/^[a-z0-9_\-]+$/i",$this->mood_id)) showmessage((L('illegal_parameters')));
  9. if (empty($this->mood_id)) {
  10. showmessage(L('id_cannot_be_empty'));
  11. }
  12. list($this->catid, $this->contentid, $this->siteid) = id_decode($this->mood_id);
  13. $this->setting = isset($this->setting[$this->siteid]) ? $this->setting[$this->siteid] : array();
  14. foreach ($this->setting as $k=>$v) {
  15. if (empty($v['use'])) unset($this->setting[$k]);
  16. }
  17. define('SITEID', $this->siteid);
  18. }
  19. //显示心情
  20. public function init() {
  21. $mood_id =& $this->mood_id;
  22. $setting =& $this->setting;
  23. $mood_db = pc_base::load_model('mood_model');
  24. $data = $mood_db->get_one(array('catid'=>$this->catid, 'siteid'=>$this->siteid, 'contentid'=>$this->contentid));
  25. foreach ($setting as $k=>$v) {
  26. $setting[$k]['fields'] = 'n'.$k;
  27. if (!isset($data[$setting[$k]['fields']])) $data[$setting[$k]['fields']] = 0;
  28. if (isset($data['total']) && !empty($data['total'])) {
  29. $setting[$k]['per'] = ceil(($data[$setting[$k]['fields']]/$data['total']) * 60);
  30. } else {
  31. $setting[$k]['per'] = 0;
  32. }
  33. }
  34. ob_start();
  35. include template('mood', 'index');
  36. $html = ob_get_contents();
  37. ob_clean();
  38. echo format_js($html);
  39. }
  40. //提交选中
  41. public function post() {
  42. if (isset($_GET['callback']) && !preg_match('/^[a-zA-Z_][a-zA-Z0-9_]+$/', $_GET['callback'])) unset($_GET['callback']);
  43. $mood_id =& $this->mood_id;
  44. $setting =& $this->setting;
  45. $cookies = param::get_cookie('mood_id');
  46. $cookie = explode(',', $cookies);
  47. if (in_array($this->mood_id, $cookie)) {
  48. $this->_show_result(0, L('expressed'));
  49. } else {
  50. $mood_db = pc_base::load_model('mood_model');
  51. $key = isset($_GET['k']) && intval($_GET['k']) ? intval($_GET['k']) : '';
  52. if (!in_array($key, array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)))
  53. $this->_show_result(0, L('illegal_parameters'));
  54. $fields = 'n'.$key;
  55. if ($data = $mood_db->get_one(array('catid'=>$this->catid, 'siteid'=>$this->siteid, 'contentid'=>$this->contentid))) {
  56. $mood_db->update(array('total'=>'+=1', $fields=>'+=1', 'lastupdate'=>SYS_TIME), array('id'=>$data['id']));
  57. $data['total']++;
  58. $data[$fields]++;
  59. } else {
  60. $mood_db->insert(array('total'=>'1', $fields=>'1', 'catid'=>$this->catid, 'siteid'=>$this->siteid, 'contentid'=>$this->contentid,'
  61. lastupdate'=>SYS_TIME));
  62. $data['total'] = 1;
  63. $data[$fields] = 1;
  64. }
  65. param::set_cookie('mood_id', $cookies.','.$mood_id);
  66. foreach ($setting as $k=>$v) {
  67. $setting[$k]['fields'] = 'n'.$k;
  68. if (!isset($data[$setting[$k]['fields']])) $data[$setting[$k]['fields']] = 0;
  69. if (isset($data['total']) && !empty($data['total'])) {
  70. $setting[$k]['per'] = ceil(($data[$setting[$k]['fields']]/$data['total']) * 60);
  71. } else {
  72. $setting[$k]['per'] = 0;
  73. }
  74. }
  75. ob_start();
  76. include template('mood', 'index');
  77. $html = ob_get_contents();
  78. ob_clean();
  79. $this->_show_result(1,$html);
  80. }
  81. }
  82. //显示AJAX结果
  83. protected function _show_result($status = 0, $msg = '') {
  84. if(CHARSET != 'utf-8') {
  85. $msg = iconv(CHARSET, 'utf-8', $msg);
  86. }
  87. exit(trim_script($_GET['callback']).'('.json_encode(array('status'=>$status, 'data'=>$msg)).')');
  88. }
  89. }