123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- /**
- * [WeEngine System] Copyright (c) 2014 WE7.CC
- * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
- */
- defined('IN_IA') or exit('Access Denied');
- class CoreModule extends WeModule {
- public $modules = array('basic', 'news', 'image', 'music', 'voice', 'video', 'wxcard', 'keyword', 'module', 'wxapp');
- public $tablename = array(
- 'basic' => 'basic_reply',
- 'news' => 'news_reply',
- 'image' => 'images_reply',
- 'music' => 'music_reply',
- 'voice' => 'voice_reply',
- 'video' => 'video_reply',
- 'wxcard' => 'wxcard_reply',
- 'keyword' => 'basic_reply',
- 'wxapp' => 'wxapp_reply',
- );
-
- private $options = array(
- 'basic' => true,
- 'news' => true,
- 'image' => true,
- 'music' => true,
- 'voice' => true,
- 'video' => true,
- 'wxcard' => true,
- 'keyword' => true,
- 'module' => true,
- 'wxapp' => true,
- );
- private $replies = array();
- public function fieldsFormDisplay($rid = 0, $option = array()) {
- global $_GPC, $_W, $setting_keyword;
- load()->model('material');
- load()->model('reply');
- $module_name = empty($_GPC['module_name']) ? empty($_GPC['m']) ? '' : safe_gpc_string($_GPC['m']) : safe_gpc_string($_GPC['module_name']);
- $replies = array();
- switch ($_GPC['a']) {
- case 'mass':
- if (!empty($rid) && $rid > 0) {
- $isexists = table('mc_mass_record')->getById($rid);
- }
- if (!empty($isexists['media_id']) && !empty($isexists['msgtype'])) {
- $wechat_attachment = material_get($isexists['media_id']);
- switch ($isexists['msgtype']) {
- case 'news':
- if (!empty($wechat_attachment['news'])) {
- foreach ($wechat_attachment['news'] as &$item) {
- $item['thumb_url'] = tomedia($item['thumb_url']);
- $item['media_id'] = $isexists['media_id'];
- $item['attach_id'] = $item['attach_id'];
- $item['perm'] = $wechat_attachment['model'];
- }
- unset($item);
- }
- $replies['news'] = $wechat_attachment['news'];
- break;
- case 'image':
- $replies['image'][0]['img_url'] = tomedia($wechat_attachment['attachment']);
- $replies['image'][0]['mediaid'] = $isexists['media_id'];
- break;
- case 'voice':
- $replies['voice'][0]['title'] = $wechat_attachment['filename'];
- $replies['voice'][0]['mediaid'] = $isexists['media_id'];
- break;
- case 'video':
- $replies['video'][0] = iunserializer($wechat_attachment['tag']);
- $replies['video'][0]['mediaid'] = $isexists['media_id'];
- break;
- }
- }
- break;
-
- default:
- if (!empty($rid)) {
- $rule_rid = $rid;
- if (in_array($module_name, array('welcome', 'default'))) {
- $rule_rid = table('rule_keyword')->where(array('rid' => $rid))->getcolumn('rid');
- }
- $isexists = reply_single($rule_rid);
- }
- if ('special' == $module_name) {
- $default_setting = uni_setting_load('default_message', $_W['uniacid']);
- $default_setting = $default_setting['default_message'] ? $default_setting['default_message'] : array();
- $reply_type = $default_setting[$_GPC['type']]['type'];
- if (empty($reply_type)) {
- if (!empty($default_setting[$_GPC['type']]['keyword'])) {
- $reply_type = 'keyword';
- }
- if (!empty($default_setting[$_GPC['type']]['module'])) {
- $reply_type = 'module';
- }
- if (empty($reply_type)) {
- break;
- }
- }
- if ('module' == $reply_type) {
- $modules = explode(',', $default_setting[$_GPC['type']]['module']);
- foreach ($modules as $k => $module_name) {
- $replies['module'][$k]['name'] = $module_name;
- $module_info = table('modules')->getByName($module_name);
- $replies['module'][$k]['title'] = $module_info['title'];
- if (!empty($module_info['logo'])) {
- $replies['module'][$k]['icon'] = tomedia($module_info['logo']);
- } else {
- if (file_exists(IA_ROOT . '/addons/' . $module_info['name'] . '/custom-icon.jpg')) {
- $replies['module'][$k]['icon'] = '../addons/' . $module_info['name'] . '/custom-icon.jgp';
- } else {
- $replies['module'][$k]['icon'] = '../addons/' . $module_info['name'] . '/icon.jpg';
- }
- }
- }
- } else {
- $replies['keyword'][0]['name'] = empty($isexists['name']) ? '' : $isexists['name'];
- $replies['keyword'][0]['content'] = $setting_keyword;
- $replies['keyword'][0]['rid'] = $rid;
- $replies['keyword'][0]['id'] = empty($isexists['keywords'][0]['id']) ? '' : $isexists['keywords'][0]['id'];
- }
- break;
- }
- if (!empty($isexists)) {
- $module = $isexists['module'];
- $module = 'images' == $module ? 'image' : $module;
-
- if ('reply' == safe_gpc_string($_GPC['a']) && (!empty($module_name) && 'keyword' == $module_name)) {
- foreach ($this->tablename as $key => $tablename) {
- if ('keyword' != $key) {
- $replies[$key] = table($tablename)->where(array('rid' => $rid))->orderby('id')->getall();
- switch ($key) {
- case 'image':
- foreach ($replies[$key] as &$img_value) {
- $img = table('wechat_attachment')->getByMediaId($img_value['mediaid']);
- $img_value['img_url'] = tomedia($img['attachment'], true);
- }
- unset($img_value);
- break;
- case 'news':
- foreach ($replies[$key] as &$news_value) {
- if (!empty($news_value) && !empty($news_value['media_id'])) {
- $news_material = material_get($news_value['media_id']);
- if (!is_error($news_material)) {
- $news_value['attach_id'] = $news_material['id'];
- $news_value['model'] = $news_material['model'];
- $news_value['description'] = $news_material['news'][0]['digest'];
- $news_value['thumb'] = tomedia($news_material['news'][0]['thumb_url']);
- }
- } else {
- $news_value['thumb'] = tomedia($news_value['thumb']);
- }
- }
- unset($news_value);
- break;
- case 'video':
- foreach ($replies[$key] as &$video_value) {
- $video_material = material_get($video_value['mediaid']);
- $video_value['filename'] = $video_material['filename'];
- }
- unset($video_value);
- break;
- case 'voice':
- foreach ($replies[$key] as &$voice_value) {
- $voice_material = material_get($voice_value['mediaid']);
- $voice_value['title'] = $voice_material['filename'];
- }
- unset($voice_value);
- break;
- case 'wxapp':
- foreach ($replies[$key] as &$wxapp_value) {
- $wxapp_material = material_get($wxapp_value['mediaid']);
- $wxapp_value['thumb_url'] = empty($wxapp_material['attachment']) ? '' : $wxapp_material['attachment'];
- }
- unset($wxapp_value);
- break;
- }
- }
- }
-
- } else {
- $replies['keyword'][0]['name'] = $isexists['name'];
- $replies['keyword'][0]['rid'] = $rid;
- $replies['keyword'][0]['content'] = $setting_keyword;
- $replies['keyword'][0]['id'] = empty($isexists['keywords'][0]['id']) ? '' : $isexists['keywords'][0]['id'];
- }
- }
- break;
- }
- if (!is_array($option)) {
- $option = array();
- }
- $options = array_merge($this->options, $option);
- include $this->template('display');
- }
- public function fieldsFormValidate($rid = 0) {
- global $_GPC;
-
- $ifEmpty = 1;
- $reply = '';
- foreach ($this->modules as $key => $value) {
- if ('' != safe_gpc_string($_GPC['reply']['reply_' . $value])) {
- $ifEmpty = 0;
- }
- if (('music' == $value || 'video' == $value || 'wxcard' == $value || 'news' == $value || 'wxapp' == $value) && !empty($_GPC['reply']['reply_' . $value])) {
- $reply = ltrim($_GPC['reply']['reply_' . $value], '{');
- $reply = rtrim($reply, '}');
- $reply = explode('},{', $reply);
- foreach ($reply as &$val) {
- $val = htmlspecialchars_decode('{' . $val . '}');
- }
- $this->replies[$value] = $reply;
- } else {
- $this->replies[$value] = htmlspecialchars_decode($_GPC['reply']['reply_' . $value], ENT_QUOTES);
- }
- }
- if ($ifEmpty) {
- return error(1, '必须填写有效的回复内容.');
- }
- return '';
- }
- public function fieldsFormSubmit($rid = 0) {
- global $_GPC, $_W;
- permission_check_account_user('platform_reply_keyword');
- $delsql = '';
- foreach ($this->modules as $k => $val) {
- $tablename = $this->tablename[$val];
- if (!empty($tablename)) {
- table($tablename)->where(array('rid' => $rid))->delete();
- }
- }
- foreach ($this->modules as $val) {
- $replies = array();
- $tablename = $this->tablename[$val];
- if ($this->replies[$val]) {
- if (is_array($this->replies[$val])) {
- foreach ($this->replies[$val] as $value) {
- $replies[] = json_decode($value, true);
- }
- } else {
- $replies = explode(',', $this->replies[$val]);
- foreach ($replies as &$v) {
- $v = json_decode($v);
- }
- }
- }
- switch ($val) {
- case 'basic':
- if (!empty($replies)) {
- foreach ($replies as $reply) {
- table($tablename)->fill(array('rid' => $rid, 'content' => $reply))->save();
- }
- }
- break;
- case 'news':
- if (!empty($replies)) {
- $parent_id = 0;
- $attach_id = 0;
- foreach ($replies as $k => $reply) {
- if (!empty($attach_id) && $reply['attach_id'] == $attach_id) {
- $reply['parent_id'] = $parent_id;
- }
-
- if ('local' == $reply['model']) {
- $reply['mediaid'] = $reply['attach_id'];
- }
- table($tablename)
- ->fill(array(
- 'rid' => $rid,
- 'parent_id' => $reply['parent_id'],
- 'title' => $reply['title'],
- 'thumb' => tomedia($reply['thumb']),
- 'createtime' => time(),
- 'media_id' => $reply['attach_id'],
- 'displayorder' => $reply['displayorder'],
- 'description' => $reply['description'],
- 'url' => $reply['url']
- ))
- ->save();
- if (empty($attach_id) || $reply['attach_id'] != $attach_id) {
- $parent_id = pdo_insertid();
- }
- $attach_id = $reply['attach_id'] ? $reply['attach_id'] : 0;
- }
- }
- break;
- case 'image':
- if (!empty($replies)) {
- foreach ($replies as $reply) {
- table($tablename)
- ->fill(array(
- 'rid' => $rid,
- 'mediaid' => $reply,
- 'createtime' => time()
- ))
- ->save();
- }
- }
- break;
- case 'music':
- if (!empty($replies)) {
- foreach ($replies as $reply) {
- table($tablename)
- ->fill(array(
- 'rid' => $rid,
- 'title' => $reply['title'],
- 'url' => $reply['url'],
- 'hqurl' => $reply['hqurl'],
- 'description' => $reply['description']
- ))
- ->save();
- }
- }
- break;
- case 'voice':
- if (!empty($replies)) {
- foreach ($replies as $reply) {
- table($tablename)
- ->fill(array(
- 'rid' => $rid,
- 'mediaid' => $reply,
- 'createtime' => time()
- ))
- ->save();
- }
- }
- break;
- case 'video':
- if (!empty($replies)) {
- foreach ($replies as $reply) {
- table($tablename)
- ->fill(array(
- 'rid' => $rid,
- 'mediaid' => $reply['mediaid'],
- 'title' => $reply['title'],
- 'description' => $reply['description'],
- 'createtime' => time()
- ))
- ->save();
- }
- }
- break;
- case 'wxcard':
- if (!empty($replies)) {
- foreach ($replies as $reply) {
- table($tablename)
- ->fill(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']
- ))
- ->save();
- }
- }
- break;
- case 'wxapp':
- if (!empty($replies)) {
- foreach ($replies as $reply) {
- table($tablename)
- ->fill(array(
- 'rid' => $rid,
- 'title' => $reply['title'],
- 'appid' => $reply['appid'],
- 'pagepath' => $reply['pagepath'],
- 'mediaid' => $reply['mediaid'],
- 'createtime' => time()
- ))
- ->save();
- }
- }
- break;
- }
- }
- return true;
- }
- public function ruleDeleted($rid = 0) {
- global $_W;
- permission_check_account_user('platform_reply_keyword');
- $reply_modules = array('basic', 'news', 'music', 'images', 'voice', 'video', 'wxcard', 'wxapp');
- foreach ($this->tablename as $tablename) {
- table($tablename)
- ->where(array(
- 'rid' => $rid,
- 'uniacid' => $_W['uniacid']
- ))
- ->delete();
- }
- }
- }
|