説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Language.php 63KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-06-28
  12. */
  13. namespace app\admin\controller;
  14. use app\admin\logic\ArchivesLogic;
  15. use think\Page;
  16. use think\Db;
  17. use app\common\logic\ArctypeLogic;
  18. use think\Cache;
  19. /**
  20. * 插件的控制器
  21. */
  22. class Language extends Base
  23. {
  24. /**
  25. * 语言库模型
  26. */
  27. public $langModel;
  28. /**
  29. * 国家语言模型
  30. */
  31. public $langMarkModel;
  32. /**
  33. * 语言包模型
  34. */
  35. public $langPackModel;
  36. /**
  37. * 语言模板变量模型
  38. */
  39. public $langAttributeModel;
  40. /**
  41. * 语言模板变量关联绑定的数据模型
  42. */
  43. public $langAttrModel;
  44. /**
  45. * 构造方法
  46. */
  47. public function __construct(){
  48. parent::__construct();
  49. $this->langModel = model('Language');
  50. $this->langMarkModel = model('LanguageMark');
  51. $this->langAttributeModel = model('LanguageAttribute');
  52. $this->langAttrModel = model('LanguageAttr');
  53. $this->langPackModel = model('LanguagePack');
  54. $system_use_language = tpCache('global.system_use_language');
  55. if (empty($system_use_language) && empty($this->php_servicemeal)) {
  56. $str = '6K+l5Yqf6IO95LuF6ZmQ5LqO5o6I5p2D5Z+f5ZCN5Y+v55So77yB';
  57. $this->error(base64_decode($str));
  58. }
  59. }
  60. /**
  61. * 多语言 - 列表
  62. */
  63. public function index()
  64. {
  65. function_exists('set_time_limit') && set_time_limit(0); //防止备份数据过程超时
  66. /*修复多语言之前的坑,删除索引,兼容多语言的重名变量*/
  67. try {
  68. Db::execute('ALTER TABLE `ey_config` DROP INDEX `name`');
  69. } catch (\Exception $e) {
  70. }
  71. /*--end*/
  72. /*同步数据到模板变量*/
  73. $this->syn_langattr();
  74. /*--end*/
  75. $list = array();
  76. $keywords = input('keywords/s');
  77. $map = array();
  78. if (!empty($keywords)) {
  79. $map['cn_title'] = array('LIKE', "%{$keywords}%");
  80. }
  81. $language_db = Db::name('language');
  82. $count = $language_db->where($map)->count('id');// 查询满足要求的总记录数
  83. $pageObj = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  84. $list = $language_db->where($map)
  85. ->order('id asc')
  86. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  87. ->select();
  88. if (!empty($list)) {
  89. $marks = get_arr_column($list, 'mark');
  90. $languagemarkList = Db::name('language_mark')->field('mark,cn_title')
  91. ->where([
  92. 'mark' => ['IN', $marks]
  93. ])->getAllWithIndex('mark');
  94. $this->assign('languagemarkList', $languagemarkList);
  95. }
  96. $pageStr = $pageObj->show(); // 分页显示输出
  97. $this->assign('list', $list); // 赋值数据集
  98. $this->assign('pageStr', $pageStr); // 赋值分页输出
  99. $this->assign('pageObj', $pageObj); // 赋值分页对象
  100. return $this->fetch();
  101. }
  102. /**
  103. * 多语言 - 功能配置
  104. */
  105. public function conf()
  106. {
  107. if (IS_POST) {
  108. $post = input('post.');
  109. $tpCacheData = [];
  110. $language_split = 0;
  111. foreach ($post as $key => $val) {
  112. if (in_array($key, ['language'])) {
  113. $language_split = $val['language_split'];
  114. $tpCacheData[$key] = $val;
  115. }
  116. }
  117. if (!empty($tpCacheData)) {
  118. foreach ($tpCacheData as $key => $val) {
  119. if (is_language()) {
  120. $langRow = Db::name('language')->order('id asc')
  121. ->cache(true, EYOUCMS_CACHE_TIME, 'language')
  122. ->select();
  123. foreach ($langRow as $_k => $_v) {
  124. tpCache($key, $val, $_v['mark']);
  125. }
  126. } else { // 单语言
  127. tpCache($key, $val);
  128. }
  129. }
  130. adminLog("多语言分离:".($language_split ? '开启' : '关闭'));
  131. }
  132. $this->success('保存成功');
  133. }
  134. return $this->fetch();
  135. }
  136. /**
  137. * 多语言 - 新增
  138. */
  139. public function add()
  140. {
  141. //防止php超时
  142. function_exists('set_time_limit') && set_time_limit(0);
  143. $this->language_access(); // 多语言功能操作权限
  144. if (IS_POST) {
  145. $post = input('post.');
  146. $mark = trim($post['mark']);
  147. $is_home_default = intval($post['is_home_default']);
  148. $count = $this->langModel->where('mark',$mark)->count();
  149. if (!empty($count)) {
  150. $this->error('该语言已存在,请检查');
  151. }
  152. if (!empty($post['url']) && !is_http_url($post['url'])) {
  153. $post['url'] = 'http://'.$post['url'];
  154. }
  155. /*组装存储数据*/
  156. $nowData = array(
  157. 'is_home_default' => $is_home_default,
  158. 'add_time' => getTime(),
  159. 'update_time' => getTime(),
  160. );
  161. $saveData = array_merge($post, $nowData);
  162. /*--end*/
  163. $this->langModel->delLangData([$mark]); // 创建时删除多余的语言数据
  164. $this->langModel->save($saveData);
  165. $insertId = $this->langModel->id;
  166. if (false !== $insertId) {
  167. //设置其他语言为前台默认语言,执行模式切换操作
  168. if ($is_home_default && $mark != 'cn'){
  169. $seo_pseudo = tpCache('global.seo_pseudo');
  170. //前台默认语言不是中文的时候,不允许使用静态模式,强制转换为伪静态
  171. if ($seo_pseudo == 2){
  172. tpCache('seo',['seo_pseudo'=>3],'cn');
  173. tpCache('seo',['seo_pseudo'=>3],$mark);
  174. }
  175. //删除静态文件
  176. del_html_dirpath();
  177. }
  178. $syn_status = $this->langModel->afterAdd($insertId, $post);
  179. if (false !== $syn_status) {
  180. adminLog('新增多语言:'.$post['title']); // 写入操作日志
  181. $this->success("新增成功,正在同步官方语言包数据……", url('Language/add_lang_syn_pack', ['mark'=>$mark, 'c_lang'=>$post['copy_lang']]), '', 2);
  182. } else {
  183. $id_arr = [$insertId];
  184. $lang_list = [$mark];
  185. $this->langModel->where("id",'IN',$id_arr)->delete();
  186. $this->langModel->afterDel($id_arr, $lang_list);
  187. $this->error("同步数据失败,请重新操作");
  188. }
  189. }else{
  190. $this->error("操作失败");
  191. }
  192. exit;
  193. }
  194. $assign_data = [];
  195. $assign_data['languagemark'] = $this->langMarkModel
  196. ->field('title,mark,cn_title')
  197. ->order('sort_order asc, pinyin asc')
  198. ->select(); // 多国语言列表
  199. $assign_data['main_lang'] = get_main_lang(); // 主体语言(第一个存在的语言)
  200. $this->assign($assign_data);
  201. return $this->fetch();
  202. }
  203. /**
  204. * 多语言 - 编辑
  205. */
  206. public function edit()
  207. {
  208. if (IS_POST) {
  209. $post = input('post.');
  210. $post['id'] = eyIntval($post['id']);
  211. if(!empty($post['id'])){
  212. $is_home_default = intval($post['is_home_default']);
  213. $mark = trim($post['mark']);
  214. $count = $this->langModel->where([
  215. 'mark'=>$mark,
  216. 'id'=>['NEQ', $post['id']]
  217. ])->count();
  218. if (!empty($count)) {
  219. $this->error('该语言已存在,请检查');
  220. }
  221. // 至少要保留一种语言是启用状态
  222. if (empty($post['status'])) {
  223. $return = model('Language')->isValidateStatus('status', $post['status']);
  224. if (is_array($return)) {
  225. $this->error($return['msg']);
  226. }
  227. }
  228. /*组装存储数据*/
  229. $nowData = array(
  230. 'is_home_default' => $is_home_default,
  231. 'update_time' => getTime(),
  232. );
  233. $saveData = array_merge($post, $nowData);
  234. /*--end*/
  235. $r = $this->langModel->save($saveData, ['id'=>$post['id']]);
  236. if ($r !== false) {
  237. /*默认语言的设置*/
  238. if (1 == $is_home_default) { // 设置默认语言,只允许有一个是默认,其他取消
  239. $this->langModel->where('id','NEQ',$post['id'])->update([
  240. 'is_home_default' => 0,
  241. 'update_time' => getTime(),
  242. ]);
  243. /*多语言 设置默认前台语言*/
  244. if (is_language()) {
  245. $langRow = \think\Db::name('language')->order('id asc')
  246. ->cache(true, EYOUCMS_CACHE_TIME, 'language')
  247. ->select();
  248. foreach ($langRow as $key => $val) {
  249. tpCache('system', ['system_home_default_lang'=>$mark], $val['mark']);
  250. }
  251. } else { // 单语言
  252. tpCache('system', ['system_home_default_lang'=>$mark]);
  253. }
  254. //设置其他语言为前台默认语言,执行模式切换操作
  255. if ($mark != 'cn'){
  256. $seo_pseudo = tpCache('global.seo_pseudo');
  257. //前台默认语言不是中文的时候,不允许使用静态模式,强制转换为伪静态
  258. if ($seo_pseudo == 2){
  259. tpCache('seo',['seo_pseudo'=>3],'cn');
  260. tpCache('seo',['seo_pseudo'=>3],$mark);
  261. }
  262. del_html_dirpath();
  263. }
  264. /*--end*/
  265. } else { // 默认语言取消之后,自动将第一个语言设置为默认
  266. $count = Db::name('language')->where(['is_home_default'=>1])->count();
  267. if (empty($count)) {
  268. $langInfo = Db::name('language')->field('id,mark')->order('id asc')->limit(1)->find();
  269. $this->langModel->where('id','eq',$langInfo['id'])->update([
  270. 'is_home_default' => 1,
  271. 'update_time' => getTime(),
  272. ]);
  273. /*多语言 设置默认前台语言*/
  274. if (is_language()) {
  275. $langRow = \think\Db::name('language')->order('id asc')
  276. ->cache(true, EYOUCMS_CACHE_TIME, 'language')
  277. ->select();
  278. foreach ($langRow as $key => $val) {
  279. tpCache('system', ['system_home_default_lang'=>$langInfo['mark']], $val['mark']);
  280. }
  281. } else { // 单语言
  282. tpCache('system', ['system_home_default_lang'=>$langInfo['mark']]);
  283. }
  284. /*--end*/
  285. }
  286. }
  287. /*--end*/
  288. /*统计多语言数量*/
  289. model('Language')->setLangNum();
  290. adminLog('编辑多语言:'.$post['title']); // 写入操作日志
  291. $this->success("操作成功", url('Language/index'));
  292. }
  293. }
  294. $this->error("操作失败");
  295. }
  296. $id = input('id/d', 0);
  297. $row = $this->langModel->find($id);
  298. $row['cn_title'] = Db::name('language_mark')->where([
  299. 'mark' => $row['mark']
  300. ])->getField('cn_title');
  301. if (empty($row)) {
  302. $this->error('数据不存在,请联系管理员!');
  303. exit;
  304. }
  305. $this->assign('row',$row);
  306. $count = Db::name('language')->count();
  307. $this->assign('count', $count);
  308. return $this->fetch();
  309. }
  310. /**
  311. * 多语言 - 删除文档
  312. */
  313. public function del()
  314. {
  315. //防止php超时
  316. function_exists('set_time_limit') && set_time_limit(0);
  317. $this->language_access(); // 多语言功能操作权限
  318. $id_arr = input('del_id/a');
  319. $id_arr = eyIntval($id_arr);
  320. if(!empty($id_arr)){
  321. /*不允许删除默认语言*/
  322. $count = $this->langModel->where([
  323. 'id' => ['IN', $id_arr],
  324. ])->where("is_home_default=1 OR mark='{$this->main_lang}'")
  325. ->count();
  326. if (!empty($count)) {
  327. $this->error('禁止删除前台默认语言');
  328. }
  329. /*--end*/
  330. $result = Db::name('language')->where("id",'IN',$id_arr)->select();
  331. $title_list = get_arr_column($result, 'title');
  332. $lang_list = get_arr_column($result, 'mark');
  333. $r = $this->langModel->where("id",'IN',$id_arr)->delete();
  334. if($r){
  335. $this->langModel->afterDel($id_arr, $lang_list);
  336. adminLog('删除多语言:'.implode(',', $title_list));
  337. $this->success("删除成功!");
  338. }else{
  339. $this->error("删除失败!");
  340. }
  341. }else{
  342. $this->error("参数有误!");
  343. }
  344. }
  345. /**
  346. * 模板栏目变量 - 列表
  347. */
  348. public function customvar_arctype()
  349. {
  350. if (!empty($this->globalConfig['language_split'])) {
  351. $this->error('语言分离不支持模板栏目变量');
  352. }
  353. /*同步数据到模板变量*/
  354. $this->syn_langattr();
  355. /*--end*/
  356. $list = array();
  357. $keywords = input('keywords/s');
  358. $map = array('attr_group'=>'arctype','is_del'=>0);
  359. if (!empty($keywords)) {
  360. $map['attr_name'] = array('LIKE', "%{$keywords}%");
  361. }
  362. $langAttribute_db = Db::name('language_attribute');
  363. $count = $langAttribute_db->where($map)->count('attr_id');// 查询满足要求的总记录数
  364. $pageObj = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  365. $list = $langAttribute_db->where($map)->order('attr_id asc')->limit($pageObj->firstRow.','.$pageObj->listRows)->select();
  366. $bindAttrName = []; // 当前页被绑定的所有栏目ID
  367. foreach ($list as $key => $val) {
  368. if ('arctype' == $val['attr_group']) {
  369. array_push($bindAttrName, $val['attr_name']);
  370. }
  371. }
  372. $pageStr = $pageObj->show(); // 分页显示输出
  373. $this->assign('list', $list); // 赋值数据集
  374. $this->assign('pageStr', $pageStr); // 赋值分页输出
  375. $this->assign('pageObj', $pageObj); // 赋值分页对象
  376. $arctypeList = [];
  377. $bindAttrList = [];
  378. if (!empty($bindAttrName)) {
  379. $bindAttrName = array_unique($bindAttrName);
  380. /*获取每个栏目ID关联的栏目列表*/
  381. $bindAttrList =Db::name('language_attr')->field('attr_name,attr_value,lang')
  382. ->where('attr_name','IN',$bindAttrName)
  383. ->select();
  384. $arctypeids = get_arr_column($bindAttrList, 'attr_value');
  385. $bindAttrList = group_same_key($bindAttrList, 'attr_name');
  386. /*--end*/
  387. /*获取指定栏目的信息*/
  388. $arctypeList = Db::name('arctype')->field('id,typename')
  389. ->where('id','IN',$arctypeids)
  390. ->getAllWithIndex('id');
  391. /*--end*/
  392. }
  393. $this->assign('bindAttrList', $bindAttrList);
  394. $this->assign('arctypeList', $arctypeList);
  395. $this->assign('main_lang', $this->main_lang);
  396. $this->assign('admin_lang', $this->admin_lang);
  397. return $this->fetch();
  398. }
  399. /**
  400. * 同步数据到多语言模板变量表
  401. */
  402. private function syn_langattr()
  403. {
  404. $this->syn_langattr_arctype();
  405. $this->syn_langattr_gbookattribute();
  406. $this->syn_langattr_proattribute();
  407. $this->syn_langattr_ad();
  408. $this->syn_langattr_ad_position();
  409. $this->syn_langattr_links_group();
  410. $this->syn_langattr_form();
  411. $this->syn_langattr_formattribute();
  412. }
  413. /**
  414. * 同步栏目到多语言模板变量表
  415. */
  416. private function syn_langattr_arctype()
  417. {
  418. $attr_group = 'arctype';
  419. $arctype_arr = array(); // 栏目数组
  420. $addData = array(); // 新增数据存储
  421. $updateData = array(); // 更新数据存储
  422. $addAttrData = array(); // 新增模板栏目变量数据存储
  423. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  424. $result = Db::name('arctype')->where('lang',$this->main_lang)->order('id asc')->select();
  425. // 栏目表ey_arctype对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  426. foreach($result as $k=>$v){
  427. // 对比结果:栏目表有,多语言模板变量表没有
  428. $attr_name = 'tid'.$v['id'];
  429. $arctype_arr[] = $attr_name;
  430. $data = array(
  431. 'attr_title' => $v['typename'],
  432. 'attr_name' => $attr_name,
  433. 'attr_group' => $attr_group,
  434. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  435. );
  436. if(empty($langAttributeRow[$attr_name])){ // 新增栏目之后进行同步到多语言模板变量
  437. $data['add_time'] = getTime();
  438. $data['update_time'] = getTime();
  439. $addData[] = $data;
  440. $addAttrData[] = [
  441. 'attr_name' => $attr_name,
  442. 'attr_value' => $v['id'],
  443. 'lang' => $this->main_lang,
  444. 'attr_group' => $attr_group,
  445. 'add_time' => getTime(),
  446. 'update_time' => getTime(),
  447. ];
  448. } else { // 更新
  449. if ($langAttributeRow[$attr_name]['attr_title'] != $v['typename']) {
  450. $updateData[] = [
  451. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  452. 'attr_title' => $v['typename'],
  453. 'is_del' => $v['is_del'],
  454. 'update_time' => getTime(),
  455. ];
  456. }
  457. }
  458. }
  459. if (!empty($addData)) {
  460. $this->langAttributeModel->saveAll($addData);
  461. }
  462. if (!empty($addAttrData)) {
  463. $this->langAttrModel->saveAll($addAttrData);
  464. }
  465. if (!empty($updateData)) {
  466. $this->langAttributeModel->saveAll($updateData);
  467. }
  468. //多语言模板变量表有,栏目表没有,清除多余的模板变量
  469. /*foreach($langAttributeRow as $k => $v){
  470. $attr_name = $v['attr_name'];
  471. if (!in_array($attr_name, $arctype_arr)) {
  472. $this->langAttributeModel->where('attr_name',$attr_name)->delete();
  473. $this->langAttrModel->where('attr_name',$v['attr_name'])->delete();
  474. }
  475. }*/
  476. }
  477. /**
  478. * 同步留言属性到多语言模板变量表
  479. */
  480. private function syn_langattr_gbookattribute()
  481. {
  482. $attr_group = 'guestbook_attribute';
  483. $gbookAttr_arr = array(); // 留言属性数组
  484. $addData = array(); // 新增数据存储
  485. $updateData = array(); // 更新数据存储
  486. $addAttrData = array(); // 新增模板留言属性变量数据存储
  487. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  488. $result = Db::name('guestbook_attribute')->where(['lang'=>$this->main_lang,'form_type'=>0])->order('attr_id asc')->select();
  489. // 栏目表ey_arctype对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  490. foreach($result as $k=>$v){
  491. // 对比结果:栏目表有,多语言模板变量表没有
  492. $attr_name = 'attr_'.$v['attr_id'];
  493. $gbookAttr_arr[] = $attr_name;
  494. $data = array(
  495. 'attr_title' => $v['attr_name'],
  496. 'attr_name' => $attr_name,
  497. 'attr_group' => $attr_group,
  498. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  499. );
  500. if(empty($langAttributeRow[$attr_name])){ // 新增留言属性之后进行同步到多语言模板变量
  501. $data['add_time'] = getTime();
  502. $data['update_time'] = getTime();
  503. $addData[] = $data;
  504. $addAttrData[] = [
  505. 'attr_name' => $attr_name,
  506. 'attr_value' => $v['attr_id'],
  507. 'lang' => $this->main_lang,
  508. 'attr_group' => $attr_group,
  509. 'add_time' => getTime(),
  510. 'update_time' => getTime(),
  511. ];
  512. } else { // 更新
  513. if ($langAttributeRow[$attr_name]['attr_title'] != $v['attr_name']) {
  514. $updateData[] = [
  515. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  516. 'attr_title' => $v['attr_name'],
  517. 'is_del' => $v['is_del'],
  518. 'update_time' => getTime(),
  519. ];
  520. }
  521. }
  522. }
  523. if (!empty($addData)) {
  524. $this->langAttributeModel->saveAll($addData);
  525. }
  526. if (!empty($addAttrData)) {
  527. $this->langAttrModel->saveAll($addAttrData);
  528. }
  529. if (!empty($updateData)) {
  530. $this->langAttributeModel->saveAll($updateData);
  531. }
  532. }
  533. /**
  534. * 同步表单到多语言模板变量表
  535. */
  536. private function syn_langattr_form()
  537. {
  538. $attr_group = 'form';
  539. $form_arr = array(); // 表单数组
  540. $addData = array(); // 新增数据存储
  541. $updateData = array(); // 更新数据存储
  542. $addAttrData = array(); // 新增模板广告变量数据存储
  543. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  544. $result = Db::name('form')->where('lang',$this->main_lang)->order('form_id asc')->select();
  545. // 表单表ey_form对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  546. foreach($result as $k=>$v){
  547. // 对比结果:表单表有,多语言模板变量表没有
  548. $attr_name = 'form'.$v['form_id'];
  549. $form_arr[] = $attr_name;
  550. $data = array(
  551. 'attr_title' => $v['form_name'],
  552. 'attr_name' => $attr_name,
  553. 'attr_group' => $attr_group,
  554. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  555. );
  556. if(empty($langAttributeRow[$attr_name])){ // 新增表单之后进行同步到多语言模板变量
  557. $data['add_time'] = getTime();
  558. $data['update_time'] = getTime();
  559. $addData[] = $data;
  560. $addAttrData[] = [
  561. 'attr_name' => $attr_name,
  562. 'attr_value' => $v['form_id'],
  563. 'lang' => $this->main_lang,
  564. 'attr_group' => $attr_group,
  565. 'add_time' => getTime(),
  566. 'update_time' => getTime(),
  567. ];
  568. } else { // 更新
  569. if ($langAttributeRow[$attr_name]['attr_title'] != $v['form_name']) {
  570. $updateData[] = [
  571. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  572. 'attr_title' => $v['form_name'],
  573. 'is_del' => $v['is_del'],
  574. 'update_time' => getTime(),
  575. ];
  576. }
  577. }
  578. }
  579. if (!empty($addData)) {
  580. $this->langAttributeModel->saveAll($addData);
  581. }
  582. if (!empty($addAttrData)) {
  583. $this->langAttrModel->saveAll($addAttrData);
  584. }
  585. if (!empty($updateData)) {
  586. $this->langAttributeModel->saveAll($updateData);
  587. }
  588. }
  589. /**
  590. * 同步表单属性到多语言模板变量表
  591. */
  592. private function syn_langattr_formattribute()
  593. {
  594. $attr_group = 'form_attribute';
  595. $formAttr_arr = array(); // 留言属性数组
  596. $addData = array(); // 新增数据存储
  597. $updateData = array(); // 更新数据存储
  598. $addAttrData = array(); // 新增模板留言属性变量数据存储
  599. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  600. $result = Db::name('guestbook_attribute')->where(['lang'=>$this->main_lang,'form_type'=>1])->order('attr_id asc')->select();
  601. // 栏目表ey_form对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  602. foreach($result as $k=>$v){
  603. // 对比结果:栏目表有,多语言模板变量表没有
  604. $attr_name = 'attr_'.$v['attr_id'];
  605. $formAttr_arr[] = $attr_name;
  606. $data = array(
  607. 'attr_title' => $v['attr_name'],
  608. 'attr_name' => $attr_name,
  609. 'attr_group' => $attr_group,
  610. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  611. );
  612. if(empty($langAttributeRow[$attr_name])){ // 新增留言属性之后进行同步到多语言模板变量
  613. $data['add_time'] = getTime();
  614. $data['update_time'] = getTime();
  615. $addData[] = $data;
  616. $addAttrData[] = [
  617. 'attr_name' => $attr_name,
  618. 'attr_value' => $v['attr_id'],
  619. 'lang' => $this->main_lang,
  620. 'attr_group' => $attr_group,
  621. 'add_time' => getTime(),
  622. 'update_time' => getTime(),
  623. ];
  624. } else { // 更新
  625. if ($langAttributeRow[$attr_name]['attr_title'] != $v['attr_name']) {
  626. $updateData[] = [
  627. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  628. 'attr_title' => $v['attr_name'],
  629. 'is_del' => $v['is_del'],
  630. 'update_time' => getTime(),
  631. ];
  632. }
  633. }
  634. }
  635. if (!empty($addData)) {
  636. $this->langAttributeModel->saveAll($addData);
  637. }
  638. if (!empty($addAttrData)) {
  639. $this->langAttrModel->saveAll($addAttrData);
  640. }
  641. if (!empty($updateData)) {
  642. $this->langAttributeModel->saveAll($updateData);
  643. }
  644. }
  645. /**
  646. * 同步产品属性到多语言模板变量表
  647. */
  648. private function syn_langattr_proattribute()
  649. {
  650. $attr_group = 'product_attribute';
  651. $proAttr_arr = array(); // 产品属性数组
  652. $addData = array(); // 新增数据存储
  653. $updateData = array(); // 更新数据存储
  654. $addAttrData = array(); // 新增模板留言属性变量数据存储
  655. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  656. $result = Db::name('product_attribute')->where('lang',$this->main_lang)->order('attr_id asc')->select();
  657. // 栏目表ey_arctype对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  658. foreach($result as $k=>$v){
  659. // 对比结果:栏目表有,多语言模板变量表没有
  660. $attr_name = 'attr_'.$v['attr_id'];
  661. $proAttr_arr[] = $attr_name;
  662. $data = array(
  663. 'attr_title' => $v['attr_name'],
  664. 'attr_name' => $attr_name,
  665. 'attr_group' => $attr_group,
  666. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  667. );
  668. if(empty($langAttributeRow[$attr_name])){ // 新增产品属性之后进行同步到多语言模板变量
  669. $data['add_time'] = getTime();
  670. $data['update_time'] = getTime();
  671. $addData[] = $data;
  672. $addAttrData[] = [
  673. 'attr_name' => $attr_name,
  674. 'attr_value' => $v['attr_id'],
  675. 'lang' => $this->main_lang,
  676. 'attr_group' => $attr_group,
  677. 'add_time' => getTime(),
  678. 'update_time' => getTime(),
  679. ];
  680. } else { // 更新
  681. if ($langAttributeRow[$attr_name]['attr_title'] != $v['attr_name']) {
  682. $updateData[] = [
  683. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  684. 'attr_title' => $v['attr_name'],
  685. 'is_del' => $v['is_del'],
  686. 'update_time' => getTime(),
  687. ];
  688. }
  689. }
  690. }
  691. if (!empty($addData)) {
  692. $this->langAttributeModel->saveAll($addData);
  693. }
  694. if (!empty($addAttrData)) {
  695. $this->langAttrModel->saveAll($addAttrData);
  696. }
  697. if (!empty($updateData)) {
  698. $this->langAttributeModel->saveAll($updateData);
  699. }
  700. }
  701. /**
  702. * 同步广告到多语言模板变量表
  703. */
  704. private function syn_langattr_ad()
  705. {
  706. $attr_group = 'ad';
  707. $ad_arr = array(); // 广告数组
  708. $addData = array(); // 新增数据存储
  709. $updateData = array(); // 更新数据存储
  710. $addAttrData = array(); // 新增模板广告变量数据存储
  711. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  712. $result = Db::name('ad')->where('lang',$this->main_lang)->order('id asc')->select();
  713. // 广告表ey_ad对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  714. foreach($result as $k=>$v){
  715. // 对比结果:广告表有,多语言模板变量表没有
  716. $attr_name = 'ad'.$v['id'];
  717. $ad_arr[] = $attr_name;
  718. $data = array(
  719. 'attr_title' => $v['title'],
  720. 'attr_name' => $attr_name,
  721. 'attr_group' => $attr_group,
  722. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  723. );
  724. if(empty($langAttributeRow[$attr_name])){ // 新增广告之后进行同步到多语言模板变量
  725. $data['add_time'] = getTime();
  726. $data['update_time'] = getTime();
  727. $addData[] = $data;
  728. $addAttrData[] = [
  729. 'attr_name' => $attr_name,
  730. 'attr_value' => $v['id'],
  731. 'lang' => $this->main_lang,
  732. 'attr_group' => $attr_group,
  733. 'add_time' => getTime(),
  734. 'update_time' => getTime(),
  735. ];
  736. } else { // 更新
  737. if ($langAttributeRow[$attr_name]['attr_title'] != $v['title']) {
  738. $updateData[] = [
  739. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  740. 'attr_title' => $v['title'],
  741. 'is_del' => $v['is_del'],
  742. 'update_time' => getTime(),
  743. ];
  744. }
  745. }
  746. }
  747. if (!empty($addData)) {
  748. $this->langAttributeModel->saveAll($addData);
  749. }
  750. if (!empty($addAttrData)) {
  751. $this->langAttrModel->saveAll($addAttrData);
  752. }
  753. if (!empty($updateData)) {
  754. $this->langAttributeModel->saveAll($updateData);
  755. }
  756. }
  757. /**
  758. * 同步广告到多语言模板变量表
  759. */
  760. private function syn_langattr_ad_position()
  761. {
  762. $attr_group = 'ad_position';
  763. $adposition_arr = array(); // 广告位置数组
  764. $addData = array(); // 新增数据存储
  765. $updateData = array(); // 更新数据存储
  766. $addAttrData = array(); // 新增模板广告变量数据存储
  767. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  768. $result = Db::name('ad_position')->where('lang',$this->main_lang)->order('id asc')->select();
  769. // 广告位置表ey_ad对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  770. foreach($result as $k=>$v){
  771. // 对比结果:广告位置表有,多语言模板变量表没有
  772. $attr_name = 'adp'.$v['id'];
  773. $adposition_arr[] = $attr_name;
  774. $data = array(
  775. 'attr_title' => $v['title'],
  776. 'attr_name' => $attr_name,
  777. 'attr_group' => $attr_group,
  778. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  779. );
  780. if(empty($langAttributeRow[$attr_name])){ // 新增广告位置之后进行同步到多语言模板变量
  781. $data['add_time'] = getTime();
  782. $data['update_time'] = getTime();
  783. $addData[] = $data;
  784. $addAttrData[] = [
  785. 'attr_name' => $attr_name,
  786. 'attr_value' => $v['id'],
  787. 'lang' => $this->main_lang,
  788. 'attr_group' => $attr_group,
  789. 'add_time' => getTime(),
  790. 'update_time' => getTime(),
  791. ];
  792. } else { // 更新
  793. if ($langAttributeRow[$attr_name]['attr_title'] != $v['title']) {
  794. $updateData[] = [
  795. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  796. 'attr_title' => $v['title'],
  797. 'is_del' => $v['is_del'],
  798. 'update_time' => getTime(),
  799. ];
  800. }
  801. }
  802. }
  803. if (!empty($addData)) {
  804. $this->langAttributeModel->saveAll($addData);
  805. }
  806. if (!empty($addAttrData)) {
  807. $this->langAttrModel->saveAll($addAttrData);
  808. }
  809. if (!empty($updateData)) {
  810. $this->langAttributeModel->saveAll($updateData);
  811. }
  812. }
  813. /**
  814. * 同步友情链接分组到多语言模板变量表
  815. */
  816. private function syn_langattr_links_group()
  817. {
  818. $attr_group = 'links_group';
  819. $linksgroup_arr = array(); // 友情分组数组
  820. $addData = array(); // 新增数据存储
  821. $updateData = array(); // 更新数据存储
  822. $addAttrData = array(); // 新增模板广告变量数据存储
  823. $langAttributeRow = $this->langAttributeModel->where('attr_group',$attr_group)->getAllWithIndex('attr_name');
  824. $result = Db::name('links_group')->where('lang',$this->main_lang)->order('id asc')->select();
  825. // 友情分组表ey_links_group对比多语言模板变量表ey_language_attribute,检测两边数据是否一致
  826. foreach($result as $k=>$v){
  827. // 对比结果:友情分组表有,多语言模板变量表没有
  828. $attr_name = 'linksgroup'.$v['id'];
  829. $linksgroup_arr[] = $attr_name;
  830. $data = array(
  831. 'attr_title' => $v['group_name'],
  832. 'attr_name' => $attr_name,
  833. 'attr_group' => $attr_group,
  834. 'is_del' => isset($v['is_del']) ? $v['is_del'] : 0,
  835. );
  836. if(empty($langAttributeRow[$attr_name])){ // 新增友情分组之后进行同步到多语言模板变量
  837. $data['add_time'] = getTime();
  838. $data['update_time'] = getTime();
  839. $addData[] = $data;
  840. $addAttrData[] = [
  841. 'attr_name' => $attr_name,
  842. 'attr_value' => $v['id'],
  843. 'lang' => $this->main_lang,
  844. 'attr_group' => $attr_group,
  845. 'add_time' => getTime(),
  846. 'update_time' => getTime(),
  847. ];
  848. } else { // 更新
  849. if ($langAttributeRow[$attr_name]['attr_title'] != $v['group_name']) {
  850. $updateData[] = [
  851. 'attr_id' => $langAttributeRow[$attr_name]['attr_id'],
  852. 'attr_title' => $v['group_name'],
  853. 'is_del' => $v['is_del'],
  854. 'update_time' => getTime(),
  855. ];
  856. }
  857. }
  858. }
  859. if (!empty($addData)) {
  860. $this->langAttributeModel->saveAll($addData);
  861. }
  862. if (!empty($addAttrData)) {
  863. $this->langAttrModel->saveAll($addAttrData);
  864. }
  865. if (!empty($updateData)) {
  866. $this->langAttributeModel->saveAll($updateData);
  867. }
  868. }
  869. /**
  870. * 关联绑定栏目
  871. */
  872. public function customvar_bind()
  873. {
  874. if (IS_POST) {
  875. $attr_id = input('post.attr_id/d', '');
  876. $typeid = input('post.typeid/d', '');
  877. $row = $this->langAttributeModel->where('attr_group','arctype')->find($attr_id);
  878. if (!empty($row)) {
  879. $attr_name = $row['attr_name'];
  880. $row2 = $this->langAttrModel->where([
  881. 'attr_name' => $attr_name,
  882. 'attr_group' => 'arctype',
  883. 'lang' => $this->admin_lang,
  884. ])->find();
  885. if (!empty($row2)) {
  886. $r = $this->langAttrModel->where('id', $row2['id'])
  887. ->update([
  888. 'attr_value' => $typeid,
  889. 'update_time' => getTime(),
  890. ]);
  891. } else {
  892. $r = $this->langAttrModel->add([
  893. 'attr_name' => $attr_name,
  894. 'attr_value' => $typeid,
  895. 'lang' => $this->admin_lang,
  896. 'add_time' => getTime(),
  897. 'update_time' => getTime(),
  898. ]);
  899. }
  900. if (false != $r) {
  901. $this->success('操作成功');
  902. }
  903. }
  904. $this->error('操作失败');
  905. }
  906. $attr_id = input('param.attr_id/d', '');
  907. $this->assign('attr_id',$attr_id);
  908. /*所有栏目列表*/
  909. $arctype_max_level = intval(config('global.arctype_max_level'));
  910. $arctypeLogic = new ArctypeLogic();
  911. $select_html = $arctypeLogic->arctype_list(0, 0, true, $arctype_max_level);
  912. $this->assign('select_html',$select_html);
  913. /*--end*/
  914. return $this->fetch();
  915. }
  916. /**
  917. * 模板语言变量
  918. */
  919. public function pack_index()
  920. {
  921. $list = array();
  922. $param = input('param.');
  923. $keywords = input('keywords/s');
  924. $condition = array();
  925. // 应用搜索条件
  926. foreach (['keywords'] as $key) {
  927. if (isset($param[$key]) && $param[$key] !== '') {
  928. if ($key == 'keywords') {
  929. $condition['a.name|a.value'] = array('LIKE', "%{$param[$key]}%");
  930. } else {
  931. $tmp_key = 'a.'.$key;
  932. $condition[$tmp_key] = array('eq', $param[$key]);
  933. }
  934. }
  935. }
  936. // 多语言
  937. $condition['a.lang'] = $this->admin_lang;
  938. $condition['a.is_syn'] = 0;
  939. $pack_db = Db::name('language_pack');
  940. $count = $pack_db->alias('a')->where($condition)->count();// 查询满足要求的总记录数
  941. $pageObj = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  942. $list = $pack_db->alias('a')
  943. ->where($condition)
  944. ->order('id desc')
  945. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  946. ->select();
  947. $this->assign('pageStr',$pageObj->show());// 赋值分页输出
  948. $this->assign('list',$list);// 赋值数据集
  949. $this->assign('pageObj',$pageObj);// 赋值分页对象
  950. return $this->fetch();
  951. }
  952. /**
  953. * 模板语言变量 - 新增
  954. */
  955. public function pack_add()
  956. {
  957. if (IS_POST) {
  958. $name = strtolower(input('post.name/s'));
  959. $values = input('post.value/a', '', 'strip_sql');
  960. // 检测变量名
  961. if (preg_match('/^(sys)(\d+)$/i', $name)) {
  962. $this->error('禁止使用sys+数字的变量名,请更换');
  963. }
  964. $count = Db::name('language_pack')->where([
  965. 'name' => $name
  966. ])->count();
  967. if (!empty($count)) {
  968. $this->error('该变量名已存在,请检查');
  969. }
  970. $saveData = [];
  971. $languageRow = Db::name('language')->field('mark')
  972. ->order('sort_order asc,id asc')
  973. ->select();
  974. foreach ($languageRow as $key => $val) {
  975. $saveData[] = [
  976. 'name' => $name,
  977. 'value' => !empty($values[$val['mark']]) ? $values[$val['mark']] : '',
  978. 'lang' => $val['mark'],
  979. 'sort_order' => 100,
  980. 'add_time' => getTime(),
  981. 'update_time' => getTime(),
  982. ];
  983. }
  984. $languagePack_M = model('LanguagePack');
  985. $r = $languagePack_M->saveAll($saveData);
  986. if (false !== $r) {
  987. $this->createLangFile(); // 生成语言包文件
  988. adminLog('新增模板语言变量:'.$name); // 写入操作日志
  989. $this->success("操作成功", url('Language/pack_index'));
  990. }else{
  991. $this->error("操作失败");
  992. }
  993. }
  994. $languageRow = Db::name('language')->field('mark,title')
  995. ->order('sort_order asc,id asc')
  996. ->select();
  997. $this->assign('languageRow', $languageRow);
  998. return $this->fetch();
  999. }
  1000. /**
  1001. * 模板语言变量 - 批量新增
  1002. */
  1003. public function pack_batch_add()
  1004. {
  1005. //防止php超时
  1006. function_exists('set_time_limit') && set_time_limit(0);
  1007. if (IS_POST) {
  1008. $admin_lang = $this->admin_lang;
  1009. $main_lang = $this->main_lang;
  1010. $content = input('post.content/s', '', 'strip_sql');
  1011. $languagePack_M = model('LanguagePack');
  1012. $tmp_content = trim(str_replace(PHP_EOL,"",$content)); //去掉回车换行符号
  1013. if (empty($tmp_content)) {
  1014. $this->error('数据不能为空!');
  1015. }
  1016. // 语言列表
  1017. $languageRow = Db::name('language')->field('mark')
  1018. ->order('id asc')
  1019. ->select();
  1020. $r = false;
  1021. $time = getTime();
  1022. $data = explode(PHP_EOL, $content);
  1023. foreach ($data as $key => $val) {
  1024. if (empty($val) || "" === trim($val)) {
  1025. continue;
  1026. }
  1027. $saveData = [];
  1028. $values = explode('=', str_replace('=', '=', $val));
  1029. $saveData = [
  1030. 'value' => !empty($values[0]) ? $values[0] : '',
  1031. 'lang' => $main_lang,
  1032. 'sort_order' => 100,
  1033. 'add_time' => $time,
  1034. 'update_time' => getTime(),
  1035. ];
  1036. $r = $languagePack_M->insertGetId($saveData);
  1037. $insertId = $r;
  1038. if (false != $insertId) {
  1039. $name = 'lang'.$insertId;
  1040. $r = $languagePack_M->update([
  1041. // 'name' => Db::raw("CONCAT('lang',id)"),
  1042. 'name' => $name,
  1043. ], ['id' => $insertId]);
  1044. if (false != $r) {
  1045. $saveDataAll = [];
  1046. foreach ($languageRow as $k2 => $v2) {
  1047. if (0 < intval($k2)) {
  1048. $saveDataAll[] = [
  1049. 'name' => $name,
  1050. 'value' => !empty($values[$k2]) ? $values[$k2] : '',
  1051. 'lang' => $v2['mark'],
  1052. 'sort_order' => 100,
  1053. 'add_time' => $time,
  1054. 'update_time' => getTime(),
  1055. ];
  1056. }
  1057. }
  1058. !empty($saveDataAll) && $languagePack_M->saveAll($saveDataAll);
  1059. }
  1060. }
  1061. }
  1062. if (false !== $r) {
  1063. $this->createLangFile(); // 生成语言包文件
  1064. adminLog('新增模板语言变量:'.str_replace(PHP_EOL, '|', $content)); // 写入操作日志
  1065. $this->success("操作成功", url('Language/pack_index'));
  1066. }else{
  1067. model('LanguagePack')->where(['add_time'=>$time])->delete();
  1068. $this->error("操作失败");
  1069. }
  1070. }
  1071. $languageRow = Db::name('language')->field('mark,title')
  1072. ->order('id asc')
  1073. ->select();
  1074. $languageStr = implode('=', get_arr_column($languageRow, 'title')).PHP_EOL;
  1075. $this->assign('languageStr', $languageStr);
  1076. return $this->fetch();
  1077. }
  1078. /**
  1079. * 模板语言变量 - 批量新增
  1080. */
  1081. // public function pack_batch_add()
  1082. // {
  1083. // if (IS_POST) {
  1084. // $admin_lang = $this->admin_lang;
  1085. // $content = input('post.content/s', '', 'strip_sql');
  1086. // $tmp_content = trim(str_replace(PHP_EOL,"",$content)); //去掉回车换行符号
  1087. // if (empty($tmp_content)) {
  1088. // $this->error('数据不能为空!');
  1089. // }
  1090. // $time = getTime();
  1091. // $saveData = [];
  1092. // $data = explode(PHP_EOL, $content);
  1093. // foreach ($data as $key => $val) {
  1094. // if (empty($val) || "" === trim($val)) {
  1095. // continue;
  1096. // }
  1097. // $saveData[] = [
  1098. // 'value' => $val,
  1099. // 'lang' => $admin_lang,
  1100. // 'sort_order' => 100,
  1101. // 'add_time' => $time,
  1102. // 'update_time' => getTime(),
  1103. // ];
  1104. // }
  1105. // $r = false;
  1106. // $languagePack_M = model('LanguagePack');
  1107. // if (!empty($saveData)) {
  1108. // $r = $languagePack_M->saveAll($saveData);
  1109. // if (false != $r) {
  1110. // $r = $languagePack_M->update([
  1111. // 'name' => Db::raw("CONCAT('language',id)"),
  1112. // ], ['add_time' => $time]);
  1113. // }
  1114. // }
  1115. // if (false !== $r) {
  1116. // /*同步到其他多语言*/
  1117. // $languageRow = Db::name('language')->field('mark')
  1118. // ->where('mark','NEQ',$admin_lang)
  1119. // ->order('sort_order asc,id asc')
  1120. // ->select();
  1121. // if (!empty($languageRow)) {
  1122. // $synData = Db::name('language_pack')->field('id,lang', true)
  1123. // ->where([
  1124. // 'add_time' => $time,
  1125. // 'lang' => $admin_lang,
  1126. // ])->select();
  1127. // $synSaveData = [];
  1128. // foreach ($languageRow as $k1 => $v1) {
  1129. // foreach ($synData as $k2 => $v2) {
  1130. // $v2['lang'] = $v1['mark'];
  1131. // $synSaveData[] = $v2;
  1132. // }
  1133. // }
  1134. // $languagePack_M->saveAll($synSaveData);
  1135. // }
  1136. // /*--end*/
  1137. // $this->createLangFile(); // 生成语言包文件
  1138. // adminLog('新增模板语言变量:'.str_replace(PHP_EOL, '|', $content)); // 写入操作日志
  1139. // $this->success("操作成功", url('Language/pack_index'));
  1140. // }else{
  1141. // model('LanguagePack')->where(['add_time'=>$time])->delete();
  1142. // $this->error("操作失败");
  1143. // }
  1144. // }
  1145. // return $this->fetch();
  1146. // }
  1147. /**
  1148. * 模板语言变量 - 编辑
  1149. */
  1150. public function pack_edit()
  1151. {
  1152. $official = input('official');
  1153. if (IS_POST) {
  1154. $id = input('post.id/d');
  1155. $name = strtolower(input('post.name/s'));
  1156. $values = input('post.value/a', '', 'strip_sql');
  1157. $languagepack_db = Db::name('language_pack');
  1158. // 旧的变量名
  1159. $old_name = $languagepack_db->where([
  1160. 'id' => $id,
  1161. 'lang' => $this->admin_lang,
  1162. ])->getField('name');
  1163. // 检测变量名
  1164. if ($old_name != $name && preg_match('/^(sys)(\d+)$/i', $name)) {
  1165. $this->error('禁止使用sys+数字的变量名,请更换');
  1166. }
  1167. $count = $languagepack_db->where([
  1168. 'name' => $name,
  1169. 'id' => ['NEQ', $id],
  1170. 'lang' => $this->admin_lang,
  1171. ])->count();
  1172. if (!empty($count)) {
  1173. $this->error('该变量名已存在,请检查');
  1174. }
  1175. // 所有语言对应此变量的id
  1176. $idRow = $languagepack_db->field('id,lang')
  1177. ->where(['name'=>$old_name])
  1178. ->getAllWithIndex('lang');
  1179. // 更新变量值
  1180. $updateData = [];
  1181. foreach ($values as $key => $val) {
  1182. $updateData[] = [
  1183. 'id' => $idRow[$key]['id'],
  1184. 'name' => $name,
  1185. 'lang' => $key,
  1186. 'value' => $val,
  1187. 'update_time' => getTime(),
  1188. ];
  1189. }
  1190. $r = model('LanguagePack')->saveAll($updateData);
  1191. if (false !== $r) {
  1192. $this->createLangFile(); // 生成语言包文件
  1193. adminLog('编辑模板语言变量:'.$name); // 写入操作日志
  1194. if (1 == $official) {
  1195. $gourl = url('Language/official_pack_index');
  1196. } else {
  1197. $gourl = url('Language/pack_index');
  1198. }
  1199. $this->success("操作成功", $gourl);
  1200. }else{
  1201. $this->error("操作失败");
  1202. }
  1203. }
  1204. $id = input('id/d');
  1205. $row = Db::name('language_pack')->where([
  1206. 'id' => $id,
  1207. 'lang' => $this->admin_lang,
  1208. ])->find();
  1209. if (empty($row)) {
  1210. $this->error('数据不存在,请联系管理员!');
  1211. exit;
  1212. }
  1213. $this->assign('row',$row);
  1214. // 语言列表
  1215. $languageRow = Db::name('language')->field('mark,title')
  1216. ->order('sort_order asc,id asc')
  1217. ->select();
  1218. $this->assign('languageRow', $languageRow);
  1219. // 变量值列表
  1220. $values = Db::name('language_pack')->field('lang,value')->where([
  1221. 'name' => $row['name'],
  1222. ])->getAllWithIndex('lang');
  1223. $this->assign('values', $values);
  1224. $this->assign('official', $official);
  1225. return $this->fetch();
  1226. }
  1227. /**
  1228. * 模板语言变量 - 删除
  1229. */
  1230. public function pack_del()
  1231. {
  1232. if (IS_POST) {
  1233. $id_arr = input('del_id/a');
  1234. $id_arr = eyIntval($id_arr);
  1235. if(!empty($id_arr)){
  1236. $languagepack_db = Db::name('language_pack');
  1237. $count = $languagepack_db->where([
  1238. 'id' => ['IN', $id_arr],
  1239. 'lang' => $this->admin_lang,
  1240. 'is_syn'=> 1,
  1241. ])->count();
  1242. if (!empty($count)) {
  1243. $this->error('官方同步语言包,禁止删除');
  1244. }
  1245. $names = $languagepack_db->where([
  1246. 'id' => ['IN', $id_arr],
  1247. 'lang' => $this->admin_lang,
  1248. ])->column('name');
  1249. $r = $languagepack_db->where([
  1250. 'name' => ['IN', $names],
  1251. ])->delete();
  1252. if($r){
  1253. $this->createLangFile(); // 生成语言包文件
  1254. adminLog('删除模板语言变量:'.implode(',', $names));
  1255. $this->success('删除成功');
  1256. }else{
  1257. $this->error('删除失败');
  1258. }
  1259. } else {
  1260. $this->error('参数有误');
  1261. }
  1262. }
  1263. $this->error('非法访问');
  1264. }
  1265. /**
  1266. * 官方语言包变量
  1267. */
  1268. public function official_pack_index()
  1269. {
  1270. $list = array();
  1271. $param = input('param.');
  1272. $keywords = input('keywords/s');
  1273. $condition = array();
  1274. // 应用搜索条件
  1275. foreach (['keywords'] as $key) {
  1276. if (isset($param[$key]) && $param[$key] !== '') {
  1277. if ($key == 'keywords') {
  1278. $condition['a.name|a.value'] = array('LIKE', "%{$param[$key]}%");
  1279. } else {
  1280. $tmp_key = 'a.'.$key;
  1281. $condition[$tmp_key] = array('eq', $param[$key]);
  1282. }
  1283. }
  1284. }
  1285. // 多语言
  1286. $condition['a.lang'] = $this->admin_lang;
  1287. $condition['a.is_syn'] = 1;
  1288. $pack_db = Db::name('language_pack');
  1289. $count = $pack_db->alias('a')->where($condition)->count();// 查询满足要求的总记录数
  1290. $pageObj = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  1291. $list = $pack_db->alias('a')
  1292. ->where($condition)
  1293. ->order('id desc')
  1294. ->limit($pageObj->firstRow.','.$pageObj->listRows)
  1295. ->select();
  1296. $this->assign('pageStr',$pageObj->show());// 赋值分页输出
  1297. $this->assign('list',$list);// 赋值数据集
  1298. $this->assign('pageObj',$pageObj);// 赋值分页对象
  1299. return $this->fetch();
  1300. }
  1301. /**
  1302. * 生成语言包文件
  1303. */
  1304. private function createLangFile()
  1305. {
  1306. $result = [];
  1307. $packRow = Db::name('language_pack')->field('name,value,lang')->order('lang asc, id asc')->select();
  1308. foreach ($packRow as $key => $val) {
  1309. $result[$val['lang']][$val['name']] = $val['value'];
  1310. }
  1311. foreach ($result as $key => $val) {
  1312. file_put_contents( APP_PATH."lang/{$key}.php", "<?php\r\n\r\n"."return ".var_export($val,true).";" );
  1313. }
  1314. }
  1315. /**
  1316. * 自动同步官方语言包
  1317. */
  1318. public function official_pack_syn($lang = '')
  1319. {
  1320. $this->pack_syn(true);
  1321. $this->success('同步成功', url('Language/official_pack_index'));
  1322. }
  1323. /**
  1324. * 新增语言,系统自动同步官方语言包
  1325. */
  1326. public function add_lang_syn_pack($mark = '', $c_lang = '')
  1327. {
  1328. if (!empty($mark) && !empty($c_lang)) {
  1329. $values = array(
  1330. 'lang'=>$mark,
  1331. );
  1332. $upgradeLogic = new \app\admin\logic\UpgradeLogic;
  1333. $upgradeLogic->GetKeyData($values);
  1334. $url = $upgradeLogic->getServiceUrl().'/index.php?m=api&c=Language&a=syn_pack_single';
  1335. $response = @httpRequest2($url, 'POST', $values, [], 5);
  1336. $params = json_decode($response,true);
  1337. if (is_array($params) && !empty($params)) {
  1338. if ($params['code'] === 0) {
  1339. $mark_id = Db::name('language')->where(['mark'=>$mark])->value('id');
  1340. $this->langModel->where("id",$mark_id)->delete();
  1341. $this->langModel->afterDel([$mark_id], [$mark]);
  1342. $this->error($params['msg'], url('Language/index'));
  1343. }
  1344. $saveData = [];
  1345. foreach ($params as $key => $val) {
  1346. $saveData[] = [
  1347. 'name' => $val['name'],
  1348. 'value' => $val['value'],
  1349. 'is_syn' => 1,
  1350. 'lang' => $mark,
  1351. 'sort_order' => 100,
  1352. 'add_time' => getTime(),
  1353. 'update_time' => getTime(),
  1354. ];
  1355. }
  1356. if (!empty($saveData)) {
  1357. $r = $this->langPackModel->saveAll($saveData);
  1358. if ($r) {
  1359. /*同步官方语言包最后一次同步的ID*/
  1360. $language_db = Db::name('language');
  1361. $syn_pack_id = $language_db->where([
  1362. 'mark' => $c_lang,
  1363. ])->getField('syn_pack_id');
  1364. $language_db->where([
  1365. 'mark' => $mark,
  1366. ])->update([
  1367. 'syn_pack_id' => $syn_pack_id,
  1368. 'update_time' => getTime(),
  1369. ]);
  1370. /*--end*/
  1371. $this->createLangFile(); // 生成语言包文件
  1372. $this->success('操作成功', url('Language/index'));
  1373. }
  1374. }
  1375. }
  1376. }
  1377. $this->error("同步失败,稍后系统默认同步");
  1378. }
  1379. /**
  1380. * 同步全部语言的官方语言包
  1381. */
  1382. public function pack_syn($return = false)
  1383. {
  1384. //防止超时
  1385. function_exists('set_time_limit') && set_time_limit(0);
  1386. $syn_pack_id = Db::name('language')->max('syn_pack_id');
  1387. $values = array(
  1388. 'pack_id'=>$syn_pack_id,
  1389. );
  1390. $upgradeLogic = new \app\admin\logic\UpgradeLogic;
  1391. $upgradeLogic->GetKeyData($values);
  1392. $url = $upgradeLogic->getServiceUrl().'/index.php?m=api&c=Language&a=syn_pack_list';
  1393. $response = @httpRequest2($url, 'POST', $values, [], 5);
  1394. $params = json_decode($response,true);
  1395. $list = !empty($params['list']) ? $params['list'] : [];
  1396. if (false != $response && empty($list)) {
  1397. $this->success("已是最新语言包");
  1398. }
  1399. if (!empty($list) && is_array($list)) {
  1400. $new_syn_pack_id = $params['max_pack_id'];
  1401. $saveData = [];
  1402. $languageRow = Db::name('language')->field('mark')
  1403. ->order('sort_order asc,id asc')
  1404. ->select();
  1405. foreach ($languageRow as $k1 => $v1) {
  1406. foreach ($list['cn'] as $k2 => $v2) {
  1407. $name = !empty($list[$v1['mark']]) ? $list[$v1['mark']][$k2]['name'] : $v2['name'];
  1408. $value = !empty($list[$v1['mark']]) ? $list[$v1['mark']][$k2]['value'] : $v2['value'];
  1409. $lang = !empty($list[$v1['mark']]) ? $list[$v1['mark']][$k2]['lang'] : $v1['mark'];
  1410. $saveData[] = [
  1411. 'name' => $name,
  1412. 'value' => $value,
  1413. 'is_syn' => 1,
  1414. 'lang' => $lang,
  1415. 'sort_order' => 100,
  1416. 'add_time' => getTime(),
  1417. 'update_time' => getTime(),
  1418. ];
  1419. }
  1420. }
  1421. if (!empty($saveData)) {
  1422. $r = $this->langPackModel->saveAll($saveData);
  1423. if (false !== $r) {
  1424. $this->langModel->where(['id'=>['gt',0]])->update([
  1425. 'syn_pack_id' => $new_syn_pack_id,
  1426. 'update_time' => getTime(),
  1427. ]);
  1428. $this->createLangFile(); // 生成语言包文件
  1429. if (false === $return) {
  1430. $this->success('操作成功');
  1431. } else {
  1432. return true;
  1433. }
  1434. }
  1435. }
  1436. }
  1437. if (false === $return) {
  1438. $this->error("操作失败");
  1439. } else {
  1440. return false;
  1441. }
  1442. }
  1443. /**
  1444. * 检测单个语言是否同步最新官方语言包
  1445. */
  1446. public function check_pack_syn()
  1447. {
  1448. //防止超时
  1449. function_exists('set_time_limit') && set_time_limit(0);
  1450. $syn_pack_id = Db::name('language')->max('syn_pack_id');
  1451. $values = array(
  1452. 'pack_id'=>$syn_pack_id,
  1453. );
  1454. $upgradeLogic = new \app\admin\logic\UpgradeLogic;
  1455. $upgradeLogic->GetKeyData($values);
  1456. $url = $upgradeLogic->getServiceUrl().'/index.php?m=api&c=Language&a=check_all_new_pack';
  1457. $response = @httpRequest2($url, 'POST', $values, [], 5);
  1458. $params = json_decode($response,true);
  1459. if (false != $params && 0 < intval($params)) {
  1460. $this->error("有新版本语言包");
  1461. } else {
  1462. $this->success("已是最新语言包");
  1463. }
  1464. }
  1465. /**
  1466. * 同步
  1467. * @return [type] [description]
  1468. */
  1469. public function sync()
  1470. {
  1471. function_exists('set_time_limit') && set_time_limit(0);
  1472. if (IS_AJAX_POST) {
  1473. $id = input('post.id/d');
  1474. $is_jump = input('post.is_jump/d');
  1475. $mark = Db::name('language')->where('id',$id)->value('mark');
  1476. if (empty($mark)) $this->error('语言不存在!');
  1477. $ArchivesLogic = new ArchivesLogic;
  1478. $res = $ArchivesLogic->batch_copy_all($mark,$is_jump);
  1479. if (false !== $res){
  1480. $this->success('同步成功!');
  1481. }
  1482. }
  1483. $this->error('同步失败!');
  1484. }
  1485. }