module.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 CoreModule extends WeModule {
  8. public $modules = array('basic', 'news', 'image', 'music', 'voice', 'video', 'wxcard', 'keyword', 'module');
  9. public $tablename = array(
  10. 'basic' => 'basic_reply',
  11. 'news' => 'news_reply',
  12. 'image' => 'images_reply',
  13. 'music' => 'music_reply',
  14. 'voice' => 'voice_reply',
  15. 'video' => 'video_reply',
  16. 'wxcard' => 'wxcard_reply',
  17. 'keyword' => 'basic_reply'
  18. );
  19. private $options = array(
  20. 'basic' => true,
  21. 'news' => true,
  22. 'image' => true,
  23. 'music' => true,
  24. 'voice' => true,
  25. 'video' => true,
  26. 'wxcard' => true,
  27. 'keyword' => true,
  28. 'module' => true,
  29. );
  30. private $replies = array();
  31. public function fieldsFormDisplay($rid = 0, $option = array()) {
  32. global $_GPC, $_W, $setting_keyword;
  33. load()->model('material');
  34. load()->model('reply');
  35. $replies = array();
  36. switch($_GPC['a']) {
  37. case 'mass':
  38. if (!empty($rid) && $rid > 0) {
  39. $isexists = pdo_get('mc_mass_record', array('id' => $rid), array('media_id', 'msgtype'));
  40. }
  41. if (!empty($isexists['media_id']) && !empty($isexists['msgtype'])) {
  42. $wechat_attachment = material_get($isexists['media_id']);
  43. switch ($isexists['msgtype']) {
  44. case 'news':
  45. if(!empty($wechat_attachment['news'])) {
  46. foreach ($wechat_attachment['news'] as &$item) {
  47. $item['thumb_url'] = tomedia($item['thumb_url']);
  48. $item['media_id'] = $isexists['media_id'];
  49. $item['attach_id'] = $item['attach_id'];
  50. $item['perm'] = $wechat_attachment['model'];
  51. }
  52. unset($item);
  53. }
  54. $replies['news'] = $wechat_attachment['news'];
  55. break;
  56. case 'image':
  57. $replies['image'][0]['img_url'] = tomedia($wechat_attachment['attachment'], true);
  58. $replies['image'][0]['mediaid'] = $isexists['media_id'];
  59. break;
  60. case 'voice':
  61. $replies['voice'][0]['title'] = $wechat_attachment['filename'];
  62. $replies['voice'][0]['mediaid'] = $isexists['media_id'];
  63. break;
  64. case 'video':
  65. $replies['video'][0] = iunserializer($wechat_attachment['tag']);
  66. $replies['video'][0]['mediaid'] = $isexists['media_id'];
  67. break;
  68. }
  69. }
  70. break;
  71. default:
  72. if (!empty($rid)) {
  73. $rule_rid = $rid;
  74. if (in_array($_GPC['m'], array('welcome', 'default'))) {
  75. $rule_rid = pdo_getcolumn('rule_keyword', array('rid' => $rid), 'rid');
  76. }
  77. $isexists = reply_single($rule_rid);
  78. }
  79. if ($_GPC['m'] == 'special') {
  80. $default_setting = uni_setting_load('default_message', $_W['uniacid']);
  81. $default_setting = $default_setting['default_message'] ? $default_setting['default_message'] : array();
  82. $reply_type = $default_setting[$_GPC['type']]['type'];
  83. if (empty($reply_type)) {
  84. if (!empty($default_setting[$_GPC['type']]['keyword'])) {
  85. $reply_type = 'keyword';
  86. }
  87. if (!empty($default_setting[$_GPC['type']]['module'])) {
  88. $reply_type = 'module';
  89. }
  90. if (empty($reply_type)) {
  91. break;
  92. }
  93. }
  94. if ($reply_type == 'module') {
  95. $replies['module'][0]['name'] = $default_setting[$_GPC['type']]['module'];
  96. $module_info = pdo_get('modules', array('name' => $default_setting[$_GPC['type']]['module']));
  97. $replies['module'][0]['title'] = $module_info['title'];
  98. if (file_exists(IA_ROOT. "/addons/". $module_info['name']. "/custom-icon.jpg")) {
  99. $replies['module'][0]['icon'] = "../addons/". $module_info['name']. "/custom-icon.jgp";
  100. } else {
  101. $replies['module'][0]['icon'] = "../addons/". $module_info['name']. "/icon.jpg";
  102. }
  103. } else {
  104. $replies['keyword'][0]['name'] = $isexists['name'];
  105. $replies['keyword'][0]['content'] = $setting_keyword;
  106. $replies['keyword'][0]['rid'] = $rid;
  107. }
  108. break;
  109. }
  110. if (!empty($isexists)) {
  111. $module = $isexists['module'];
  112. $module = $module == 'images' ? 'image' : $module;
  113. if ($_GPC['a'] == 'reply' && (!empty($_GPC['m']) && $_GPC['m'] == 'keyword')) {
  114. foreach ($this->tablename as $key => $tablename) {
  115. if ($key != 'keyword') {
  116. $replies[$key] = pdo_fetchall("SELECT * FROM ".tablename($tablename)." WHERE rid = :rid ORDER BY `id`", array(':rid' => $rid));
  117. switch ($key) {
  118. case 'image':
  119. foreach ($replies[$key] as &$img_value) {
  120. $img = pdo_get('wechat_attachment', array('media_id' => $img_value['mediaid']), array('attachment'));
  121. $img_value['img_url'] = tomedia($img['attachment'], true);
  122. }
  123. unset($img_value);
  124. break;
  125. case 'news' :
  126. foreach ($replies[$key] as &$news_value) {
  127. if (!empty($news_value) && !empty($news_value['media_id'])) {
  128. $news_material = material_get($news_value['media_id']);
  129. if (!is_error($news_material)) {
  130. $news_value['attach_id'] = $news_material['id'];
  131. $news_value['model'] = $news_material['model'];
  132. $news_value['description'] = $news_material['news'][0]['digest'];
  133. $news_value['thumb'] = tomedia($news_material['news'][0]['thumb_url']);
  134. }
  135. } else {
  136. $news_value['thumb'] = tomedia($news_value['thumb']);
  137. }
  138. }
  139. unset($news_value);
  140. break;
  141. }
  142. }
  143. }
  144. } else {
  145. $replies['keyword'][0]['name'] = $isexists['name'];
  146. $replies['keyword'][0]['rid'] = $rid;
  147. $replies['keyword'][0]['content'] = $setting_keyword;
  148. }
  149. }
  150. break;
  151. }
  152. if(!is_array($option)) {
  153. $option = array();
  154. }
  155. $options = array_merge($this->options, $option);
  156. include $this->template('display');
  157. }
  158. public function fieldsFormValidate($rid = 0) {
  159. global $_GPC;
  160. $ifEmpty = 1;
  161. $reply = '';
  162. foreach ($this->modules as $key => $value) {
  163. if(trim($_GPC['reply']['reply_'.$value]) != '') {
  164. $ifEmpty = 0;
  165. }
  166. if( ($value == 'music' || $value == 'video' || $value == 'wxcard' || $value == 'news') && !empty($_GPC['reply']['reply_'.$value])) {
  167. $reply = ltrim($_GPC['reply']['reply_'.$value], '{');
  168. $reply = rtrim($reply, '}');
  169. $reply = explode('},{', $reply);
  170. foreach ($reply as &$val) {
  171. $val = htmlspecialchars_decode('{'.$val.'}');
  172. }
  173. $this->replies[$value] = $reply;
  174. }else {
  175. $this->replies[$value] = htmlspecialchars_decode($_GPC['reply']['reply_'.$value], ENT_QUOTES);
  176. }
  177. }
  178. if($ifEmpty) {
  179. return error(1, '必须填写有效的回复内容.');
  180. }
  181. return '';
  182. }
  183. public function fieldsFormSubmit($rid = 0) {
  184. global $_GPC, $_W;
  185. $delsql = '';
  186. foreach ($this->modules as $k => $val) {
  187. $tablename = $this->tablename[$val];
  188. if (!empty($tablename)) {
  189. pdo_delete($tablename, array('rid' => $rid));
  190. }
  191. }
  192. foreach ($this->modules as $val) {
  193. $replies = array();
  194. $tablename = $this->tablename[$val];
  195. if($this->replies[$val]) {
  196. if(is_array($this->replies[$val])) {
  197. foreach ($this->replies[$val] as $value) {
  198. $replies[] = json_decode($value, true);
  199. }
  200. }else {
  201. $replies = explode(',', $this->replies[$val]);
  202. foreach ($replies as &$v) {
  203. $v = json_decode($v);
  204. }
  205. }
  206. }
  207. switch ($val) {
  208. case 'basic':
  209. if(!empty($replies)) {
  210. foreach($replies as $reply) {
  211. pdo_insert($tablename, array('rid' => $rid, 'content' => $reply));
  212. }
  213. }
  214. break;
  215. case 'news':
  216. if(!empty($replies)) {
  217. $parent_id = 0;
  218. $attach_id = 0;
  219. foreach ($replies as $k=>$reply) {
  220. if (!empty($attach_id) && $reply['attach_id'] == $attach_id) {
  221. $reply['parent_id'] = $parent_id;
  222. }
  223. if ($reply['model'] == 'local') {
  224. $reply['mediaid'] = $reply['attach_id'];
  225. }
  226. pdo_insert ($tablename, array ('rid' => $rid, 'parent_id' => $reply['parent_id'], 'title' => $reply['title'], 'thumb' => tomedia($reply['thumb']), 'createtime' => $reply['createtime'], 'media_id' => $reply['mediaid'], 'displayorder' => $reply['displayorder'], 'description' => $reply['description'], 'url' => $reply['url']));
  227. if (empty($attach_id) || $reply['attach_id'] != $attach_id) {
  228. $parent_id = pdo_insertid();
  229. }
  230. $attach_id = $reply['attach_id'] ? $reply['attach_id'] : 0;
  231. }
  232. }
  233. break;
  234. case 'image':
  235. if(!empty($replies)) {
  236. foreach ($replies as $reply) {
  237. pdo_insert($tablename, array('rid' => $rid, 'mediaid' => $reply, 'createtime' => time()));
  238. }
  239. }
  240. break;
  241. case 'music':
  242. if(!empty($replies)) {
  243. foreach ($replies as $reply) {
  244. pdo_insert($tablename, array('rid' => $rid, 'title' => $reply['title'], 'url' => $reply['url'], 'hqurl' => $reply['hqurl'], 'description' => $reply['description']));
  245. }
  246. }
  247. break;
  248. case 'voice':
  249. if(!empty($replies)) {
  250. foreach ($replies as $reply) {
  251. pdo_insert($tablename, array('rid' => $rid, 'mediaid' => $reply, 'createtime' => time()));
  252. }
  253. }
  254. break;
  255. case 'video':
  256. if(!empty($replies)) {
  257. foreach ($replies as $reply) {
  258. pdo_insert($tablename, array('rid' => $rid, 'mediaid' => $reply['mediaid'], 'title' => $reply['title'], 'description' => $reply['description'], 'createtime' => time()));
  259. }
  260. }
  261. break;
  262. case 'wxcard':
  263. if(!empty($replies)) {
  264. foreach ($replies as $reply) {
  265. pdo_insert($tablename, array('rid' => $rid, 'title' => $reply['title'], 'card_id' => $reply['mediaid'], 'cid' => $reply['cid'], 'brand_name' => $reply['brandname'], 'logo_url' => $reply['logo_url'], 'success' => $reply['success'], 'error' => $reply['error']));
  266. }
  267. }
  268. break;
  269. }
  270. }
  271. return true;
  272. }
  273. public function ruleDeleted($rid = 0) {
  274. $reply_modules = array("basic", "news", "music", "images", "voice", "video", "wxcard");
  275. foreach($this->tablename as $tablename) {
  276. pdo_delete($tablename, array('rid' => $rid));
  277. }
  278. }
  279. }