module.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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', 'wxapp');
  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. 'wxapp' => 'wxapp_reply',
  19. );
  20. private $options = array(
  21. 'basic' => true,
  22. 'news' => true,
  23. 'image' => true,
  24. 'music' => true,
  25. 'voice' => true,
  26. 'video' => true,
  27. 'wxcard' => true,
  28. 'keyword' => true,
  29. 'module' => true,
  30. 'wxapp' => true,
  31. );
  32. private $replies = array();
  33. public function fieldsFormDisplay($rid = 0, $option = array()) {
  34. global $_GPC, $_W, $setting_keyword;
  35. load()->model('material');
  36. load()->model('reply');
  37. $module_name = empty($_GPC['module_name']) ? empty($_GPC['m']) ? '' : safe_gpc_string($_GPC['m']) : safe_gpc_string($_GPC['module_name']);
  38. $replies = array();
  39. switch ($_GPC['a']) {
  40. case 'mass':
  41. if (!empty($rid) && $rid > 0) {
  42. $isexists = table('mc_mass_record')->getById($rid);
  43. }
  44. if (!empty($isexists['media_id']) && !empty($isexists['msgtype'])) {
  45. $wechat_attachment = material_get($isexists['media_id']);
  46. switch ($isexists['msgtype']) {
  47. case 'news':
  48. if (!empty($wechat_attachment['news'])) {
  49. foreach ($wechat_attachment['news'] as &$item) {
  50. $item['thumb_url'] = tomedia($item['thumb_url']);
  51. $item['media_id'] = $isexists['media_id'];
  52. $item['attach_id'] = $item['attach_id'];
  53. $item['perm'] = $wechat_attachment['model'];
  54. }
  55. unset($item);
  56. }
  57. $replies['news'] = $wechat_attachment['news'];
  58. break;
  59. case 'image':
  60. $replies['image'][0]['img_url'] = tomedia($wechat_attachment['attachment']);
  61. $replies['image'][0]['mediaid'] = $isexists['media_id'];
  62. break;
  63. case 'voice':
  64. $replies['voice'][0]['title'] = $wechat_attachment['filename'];
  65. $replies['voice'][0]['mediaid'] = $isexists['media_id'];
  66. break;
  67. case 'video':
  68. $replies['video'][0] = iunserializer($wechat_attachment['tag']);
  69. $replies['video'][0]['mediaid'] = $isexists['media_id'];
  70. break;
  71. }
  72. }
  73. break;
  74. default:
  75. if (!empty($rid)) {
  76. $rule_rid = $rid;
  77. if (in_array($module_name, array('welcome', 'default'))) {
  78. $rule_rid = table('rule_keyword')->where(array('rid' => $rid))->getcolumn('rid');
  79. }
  80. $isexists = reply_single($rule_rid);
  81. }
  82. if ('special' == $module_name) {
  83. $default_setting = uni_setting_load('default_message', $_W['uniacid']);
  84. $default_setting = $default_setting['default_message'] ? $default_setting['default_message'] : array();
  85. $reply_type = $default_setting[$_GPC['type']]['type'];
  86. if (empty($reply_type)) {
  87. if (!empty($default_setting[$_GPC['type']]['keyword'])) {
  88. $reply_type = 'keyword';
  89. }
  90. if (!empty($default_setting[$_GPC['type']]['module'])) {
  91. $reply_type = 'module';
  92. }
  93. if (empty($reply_type)) {
  94. break;
  95. }
  96. }
  97. if ('module' == $reply_type) {
  98. $modules = explode(',', $default_setting[$_GPC['type']]['module']);
  99. foreach ($modules as $k => $module_name) {
  100. $replies['module'][$k]['name'] = $module_name;
  101. $module_info = table('modules')->getByName($module_name);
  102. $replies['module'][$k]['title'] = $module_info['title'];
  103. if (!empty($module_info['logo'])) {
  104. $replies['module'][$k]['icon'] = tomedia($module_info['logo']);
  105. } else {
  106. if (file_exists(IA_ROOT . '/addons/' . $module_info['name'] . '/custom-icon.jpg')) {
  107. $replies['module'][$k]['icon'] = '../addons/' . $module_info['name'] . '/custom-icon.jgp';
  108. } else {
  109. $replies['module'][$k]['icon'] = '../addons/' . $module_info['name'] . '/icon.jpg';
  110. }
  111. }
  112. }
  113. } else {
  114. $replies['keyword'][0]['name'] = empty($isexists['name']) ? '' : $isexists['name'];
  115. $replies['keyword'][0]['content'] = $setting_keyword;
  116. $replies['keyword'][0]['rid'] = $rid;
  117. $replies['keyword'][0]['id'] = empty($isexists['keywords'][0]['id']) ? '' : $isexists['keywords'][0]['id'];
  118. }
  119. break;
  120. }
  121. if (!empty($isexists)) {
  122. $module = $isexists['module'];
  123. $module = 'images' == $module ? 'image' : $module;
  124. if ('reply' == safe_gpc_string($_GPC['a']) && (!empty($module_name) && 'keyword' == $module_name)) {
  125. foreach ($this->tablename as $key => $tablename) {
  126. if ('keyword' != $key) {
  127. $replies[$key] = table($tablename)->where(array('rid' => $rid))->orderby('id')->getall();
  128. switch ($key) {
  129. case 'image':
  130. foreach ($replies[$key] as &$img_value) {
  131. $img = table('wechat_attachment')->getByMediaId($img_value['mediaid']);
  132. $img_value['img_url'] = tomedia($img['attachment'], true);
  133. }
  134. unset($img_value);
  135. break;
  136. case 'news':
  137. foreach ($replies[$key] as &$news_value) {
  138. if (!empty($news_value) && !empty($news_value['media_id'])) {
  139. $news_material = material_get($news_value['media_id']);
  140. if (!is_error($news_material)) {
  141. $news_value['attach_id'] = $news_material['id'];
  142. $news_value['model'] = $news_material['model'];
  143. $news_value['description'] = $news_material['news'][0]['digest'];
  144. $news_value['thumb'] = tomedia($news_material['news'][0]['thumb_url']);
  145. }
  146. } else {
  147. $news_value['thumb'] = tomedia($news_value['thumb']);
  148. }
  149. }
  150. unset($news_value);
  151. break;
  152. case 'video':
  153. foreach ($replies[$key] as &$video_value) {
  154. $video_material = material_get($video_value['mediaid']);
  155. $video_value['filename'] = $video_material['filename'];
  156. }
  157. unset($video_value);
  158. break;
  159. case 'voice':
  160. foreach ($replies[$key] as &$voice_value) {
  161. $voice_material = material_get($voice_value['mediaid']);
  162. $voice_value['title'] = $voice_material['filename'];
  163. }
  164. unset($voice_value);
  165. break;
  166. case 'wxapp':
  167. foreach ($replies[$key] as &$wxapp_value) {
  168. $wxapp_material = material_get($wxapp_value['mediaid']);
  169. $wxapp_value['thumb_url'] = empty($wxapp_material['attachment']) ? '' : $wxapp_material['attachment'];
  170. }
  171. unset($wxapp_value);
  172. break;
  173. }
  174. }
  175. }
  176. } else {
  177. $replies['keyword'][0]['name'] = $isexists['name'];
  178. $replies['keyword'][0]['rid'] = $rid;
  179. $replies['keyword'][0]['content'] = $setting_keyword;
  180. $replies['keyword'][0]['id'] = empty($isexists['keywords'][0]['id']) ? '' : $isexists['keywords'][0]['id'];
  181. }
  182. }
  183. break;
  184. }
  185. if (!is_array($option)) {
  186. $option = array();
  187. }
  188. $options = array_merge($this->options, $option);
  189. include $this->template('display');
  190. }
  191. public function fieldsFormValidate($rid = 0) {
  192. global $_GPC;
  193. $ifEmpty = 1;
  194. $reply = '';
  195. foreach ($this->modules as $key => $value) {
  196. if ('' != safe_gpc_string($_GPC['reply']['reply_' . $value])) {
  197. $ifEmpty = 0;
  198. }
  199. if (('music' == $value || 'video' == $value || 'wxcard' == $value || 'news' == $value || 'wxapp' == $value) && !empty($_GPC['reply']['reply_' . $value])) {
  200. $reply = ltrim($_GPC['reply']['reply_' . $value], '{');
  201. $reply = rtrim($reply, '}');
  202. $reply = explode('},{', $reply);
  203. foreach ($reply as &$val) {
  204. $val = htmlspecialchars_decode('{' . $val . '}');
  205. }
  206. $this->replies[$value] = $reply;
  207. } else {
  208. $this->replies[$value] = htmlspecialchars_decode($_GPC['reply']['reply_' . $value], ENT_QUOTES);
  209. }
  210. }
  211. if ($ifEmpty) {
  212. return error(1, '必须填写有效的回复内容.');
  213. }
  214. return '';
  215. }
  216. public function fieldsFormSubmit($rid = 0) {
  217. global $_GPC, $_W;
  218. permission_check_account_user('platform_reply_keyword');
  219. $delsql = '';
  220. foreach ($this->modules as $k => $val) {
  221. $tablename = $this->tablename[$val];
  222. if (!empty($tablename)) {
  223. table($tablename)->where(array('rid' => $rid))->delete();
  224. }
  225. }
  226. foreach ($this->modules as $val) {
  227. $replies = array();
  228. $tablename = $this->tablename[$val];
  229. if ($this->replies[$val]) {
  230. if (is_array($this->replies[$val])) {
  231. foreach ($this->replies[$val] as $value) {
  232. $replies[] = json_decode($value, true);
  233. }
  234. } else {
  235. $replies = explode(',', $this->replies[$val]);
  236. foreach ($replies as &$v) {
  237. $v = json_decode($v);
  238. }
  239. }
  240. }
  241. switch ($val) {
  242. case 'basic':
  243. if (!empty($replies)) {
  244. foreach ($replies as $reply) {
  245. table($tablename)->fill(array('rid' => $rid, 'content' => $reply))->save();
  246. }
  247. }
  248. break;
  249. case 'news':
  250. if (!empty($replies)) {
  251. $parent_id = 0;
  252. $attach_id = 0;
  253. foreach ($replies as $k => $reply) {
  254. if (!empty($attach_id) && $reply['attach_id'] == $attach_id) {
  255. $reply['parent_id'] = $parent_id;
  256. }
  257. if ('local' == $reply['model']) {
  258. $reply['mediaid'] = $reply['attach_id'];
  259. }
  260. table($tablename)
  261. ->fill(array(
  262. 'rid' => $rid,
  263. 'parent_id' => $reply['parent_id'],
  264. 'title' => $reply['title'],
  265. 'thumb' => tomedia($reply['thumb']),
  266. 'createtime' => time(),
  267. 'media_id' => $reply['attach_id'],
  268. 'displayorder' => $reply['displayorder'],
  269. 'description' => $reply['description'],
  270. 'url' => $reply['url']
  271. ))
  272. ->save();
  273. if (empty($attach_id) || $reply['attach_id'] != $attach_id) {
  274. $parent_id = pdo_insertid();
  275. }
  276. $attach_id = $reply['attach_id'] ? $reply['attach_id'] : 0;
  277. }
  278. }
  279. break;
  280. case 'image':
  281. if (!empty($replies)) {
  282. foreach ($replies as $reply) {
  283. table($tablename)
  284. ->fill(array(
  285. 'rid' => $rid,
  286. 'mediaid' => $reply,
  287. 'createtime' => time()
  288. ))
  289. ->save();
  290. }
  291. }
  292. break;
  293. case 'music':
  294. if (!empty($replies)) {
  295. foreach ($replies as $reply) {
  296. table($tablename)
  297. ->fill(array(
  298. 'rid' => $rid,
  299. 'title' => $reply['title'],
  300. 'url' => $reply['url'],
  301. 'hqurl' => $reply['hqurl'],
  302. 'description' => $reply['description']
  303. ))
  304. ->save();
  305. }
  306. }
  307. break;
  308. case 'voice':
  309. if (!empty($replies)) {
  310. foreach ($replies as $reply) {
  311. table($tablename)
  312. ->fill(array(
  313. 'rid' => $rid,
  314. 'mediaid' => $reply,
  315. 'createtime' => time()
  316. ))
  317. ->save();
  318. }
  319. }
  320. break;
  321. case 'video':
  322. if (!empty($replies)) {
  323. foreach ($replies as $reply) {
  324. table($tablename)
  325. ->fill(array(
  326. 'rid' => $rid,
  327. 'mediaid' => $reply['mediaid'],
  328. 'title' => $reply['title'],
  329. 'description' => $reply['description'],
  330. 'createtime' => time()
  331. ))
  332. ->save();
  333. }
  334. }
  335. break;
  336. case 'wxcard':
  337. if (!empty($replies)) {
  338. foreach ($replies as $reply) {
  339. table($tablename)
  340. ->fill(array(
  341. 'rid' => $rid,
  342. 'title' => $reply['title'],
  343. 'card_id' => $reply['mediaid'],
  344. 'cid' => $reply['cid'],
  345. 'brand_name' => $reply['brandname'],
  346. 'logo_url' => $reply['logo_url'],
  347. 'success' => $reply['success'],
  348. 'error' => $reply['error']
  349. ))
  350. ->save();
  351. }
  352. }
  353. break;
  354. case 'wxapp':
  355. if (!empty($replies)) {
  356. foreach ($replies as $reply) {
  357. table($tablename)
  358. ->fill(array(
  359. 'rid' => $rid,
  360. 'title' => $reply['title'],
  361. 'appid' => $reply['appid'],
  362. 'pagepath' => $reply['pagepath'],
  363. 'mediaid' => $reply['mediaid'],
  364. 'createtime' => time()
  365. ))
  366. ->save();
  367. }
  368. }
  369. break;
  370. }
  371. }
  372. return true;
  373. }
  374. public function ruleDeleted($rid = 0) {
  375. global $_W;
  376. permission_check_account_user('platform_reply_keyword');
  377. $reply_modules = array('basic', 'news', 'music', 'images', 'voice', 'video', 'wxcard', 'wxapp');
  378. foreach ($this->tablename as $tablename) {
  379. table($tablename)
  380. ->where(array(
  381. 'rid' => $rid,
  382. 'uniacid' => $_W['uniacid']
  383. ))
  384. ->delete();
  385. }
  386. }
  387. }