人人商城

module.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. class NewsModule extends WeModule {
  8. public $tablename = 'news_reply';
  9. public $replies = array();
  10. public function fieldsFormDisplay($rid = 0) {
  11. global $_W;
  12. load()->func('tpl');
  13. $replies = array();
  14. $replies = pdo_fetchall("SELECT * FROM ".tablename($this->tablename)." WHERE rid = :rid AND parent_id = -1 ORDER BY `displayorder` DESC, id ASC", array(':rid' => $rid));
  15. if(!empty($replies)) {
  16. $parent_id = $replies[0]['id'];
  17. pdo_update($this->tablename, array('parent_id' => $parent_id), array('rid' => $rid));
  18. pdo_update($this->tablename, array('parent_id' => 0), array('rid' => $rid, 'id' => $parent_id));
  19. }
  20. $rows = pdo_fetchall("SELECT * FROM ".tablename($this->tablename)." WHERE rid = :rid ORDER BY `parent_id` ASC, `id` ASC", array(':rid' => $rid));
  21. $replies = array();
  22. foreach($rows as &$row) {
  23. if(!empty($row['thumb'])) {
  24. $row['thumb'] = tomedia($row['thumb']);
  25. }
  26. if (empty($row['parent_id'])) {
  27. $replies[$row['id']][] = $row;
  28. } else {
  29. $replies[$row['parent_id']][] = $row;
  30. }
  31. }
  32. $replies = array_values($replies);
  33. include $this->template('display');
  34. }
  35. public function fieldsFormValidate($rid = 0) {
  36. global $_GPC, $_W;
  37. $this->replies = @json_decode(htmlspecialchars_decode($_GPC['replies']), true);
  38. if(empty($this->replies)) {
  39. return '必须填写有效的回复内容.';
  40. }
  41. $column = array('id', 'parent_id', 'title', 'author', 'displayorder', 'thumb', 'description', 'content', 'url', 'incontent', 'createtime');
  42. foreach($this->replies as $i => &$group) {
  43. foreach($group as $k => &$v) {
  44. if(empty($v)) {
  45. unset($group[$k]);
  46. continue;
  47. }
  48. if (trim($v['title']) == '') {
  49. return '必须填写有效的标题.';
  50. }
  51. if (trim($v['thumb']) == '') {
  52. return '必须填写有效的封面链接地址.';
  53. }
  54. $v['thumb'] = str_replace($_W['attachurl'], '', $v['thumb']);
  55. $v['content'] = htmlspecialchars_decode($v['content']);
  56. $v['createtime'] = TIMESTAMP;
  57. $v = array_elements($column, $v);
  58. }
  59. if(empty($group)) {
  60. unset($i);
  61. }
  62. }
  63. if(empty($this->replies)) {
  64. return '必须填写有效的回复内容.';
  65. }
  66. return '';
  67. }
  68. public function fieldsFormSubmit($rid = 0) {
  69. $sql = 'SELECT `id` FROM ' . tablename($this->tablename) . " WHERE `rid` = :rid";
  70. $replies = pdo_fetchall($sql, array(':rid' => $rid), 'id');
  71. $replyids = array_keys($replies);
  72. $indexs = array();
  73. foreach($this->replies as &$group) {
  74. $parent_id = -1;
  75. foreach($group as $reply) {
  76. if($parent_id <= 0) {
  77. if($reply['parent_id'] == 0) {
  78. $parent_id = $reply['id'];
  79. } elseif($reply['parent_id'] > 0) {
  80. $parent_id = $reply['parent_id'];
  81. }
  82. }
  83. }
  84. if($parent_id == -1) {
  85. $i = 0;
  86. foreach($group as $reply) {
  87. if(!$i) {
  88. $i++;
  89. $reply['rid'] = $rid;
  90. $reply['parent_id'] = 0;
  91. pdo_insert($this->tablename, $reply);
  92. $parent_id = pdo_insertid();
  93. } else {
  94. $reply['parent_id'] = $parent_id;
  95. $reply['rid'] = $rid;
  96. pdo_insert($this->tablename, $reply);
  97. }
  98. }
  99. pdo_update($this->tablename, array('parent_id' => 0), array('id' => $parent_id));
  100. } else {
  101. $i = 0;
  102. foreach($group as $reply) {
  103. if(!$i) {
  104. $new_parent_id = $reply['id'];
  105. $i++;
  106. }
  107. $arr[] = $reply['id'];
  108. $reply['parent_id'] = $parent_id;
  109. if (in_array($reply['id'], $replyids)) {
  110. pdo_update($this->tablename, $reply, array('id' => $reply['id']));
  111. $index = array_search($reply['id'], $replyids);
  112. unset($replyids[$index]);
  113. } else {
  114. $reply['rid'] = $rid;
  115. pdo_insert($this->tablename, $reply);
  116. }
  117. }
  118. if(!in_array($parent_id, $arr)) {
  119. $parent_id = $new_parent_id;
  120. }
  121. pdo_update($this->tablename, array('parent_id' => $new_parent_id), array('parent_id' => $parent_id));
  122. pdo_update($this->tablename, array('parent_id' => 0), array('id' => $new_parent_id));
  123. }
  124. }
  125. if (!empty($replyids)) {
  126. $replies = array_values($replyids);
  127. $replyids = implode(',', $replyids);
  128. $sql = 'DELETE FROM '. tablename($this->tablename) . " WHERE `id` IN ({$replyids})";
  129. pdo_query($sql);
  130. }
  131. return true;
  132. }
  133. public function ruleDeleted($rid = 0) {
  134. pdo_delete($this->tablename, array('rid' => $rid));
  135. return true;
  136. }
  137. }