No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LanguageAttr.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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-4-3
  12. */
  13. namespace app\common\model;
  14. use think\Db;
  15. use think\Model;
  16. use think\Request;
  17. /**
  18. * 模型
  19. */
  20. class LanguageAttr extends Model
  21. {
  22. public $language_split = 0;
  23. //初始化
  24. protected function initialize()
  25. {
  26. // 需要调用`Model`的`initialize`方法
  27. parent::initialize();
  28. $this->language_split = tpCache('language.language_split');
  29. }
  30. /**
  31. * 获取关联绑定的变量值
  32. * @param string|array $bind_value 绑定之前的值,或者绑定之后的值
  33. * @param string $group 分组
  34. */
  35. public function getBindValue($bind_value = '', $attr_group = 'arctype', $langvar = '')
  36. {
  37. /*单语言情况下不执行多语言代码*/
  38. if (!is_language()) {
  39. return $bind_value;
  40. }
  41. /*--end*/
  42. // 主语言
  43. $main_lang = get_main_lang();
  44. // 当前语言
  45. $lang = $main_lang;
  46. if ('admin' == request()->module()) {
  47. $lang = get_admin_lang();
  48. } else {
  49. $lang = get_home_lang();
  50. }
  51. if (!empty($bind_value) && $main_lang != $lang) {
  52. switch ($attr_group) {
  53. case 'arctype':
  54. {
  55. if (!is_array($bind_value)) { // 获取关联绑定的栏目ID
  56. $typeidArr = explode(',', $bind_value);
  57. $row = Db::name('language_attr')->field('attr_name')
  58. ->where([
  59. 'attr_value' => ['IN', $typeidArr],
  60. 'attr_group' => $attr_group,
  61. ])->select();
  62. if (!empty($row) && empty($this->language_split)) {
  63. $row2 = Db::name('language_attr')->field('attr_name,attr_value')
  64. ->where([
  65. 'attr_name' => ['IN', get_arr_column($row, 'attr_name')],
  66. 'lang' => $lang,
  67. 'attr_group' => $attr_group,
  68. ])->select();
  69. if (1 < count($typeidArr)) {
  70. $bind_value = implode(',', get_arr_column($row2, 'attr_value'));
  71. } else {
  72. if(empty($row2)) {
  73. $bind_value = empty($this->language_split) ? '' : $bind_value;
  74. } else {
  75. $bind_value = $row2[0]['attr_value'];
  76. }
  77. }
  78. }
  79. }
  80. }
  81. break;
  82. case 'product_attribute':
  83. {
  84. if (is_array($bind_value)) {
  85. !empty($langvar) && $lang = $langvar;
  86. $row = Db::name('language_attr')->field('attr_name, attr_value')
  87. ->where([
  88. 'attr_value' => ['IN', get_arr_column($bind_value, 'attr_id')],
  89. 'attr_group' => $attr_group,
  90. ])->getAllWithIndex('attr_value');
  91. if (!empty($row) && empty($this->language_split)) {
  92. $row2 = Db::name('language_attr')->field('attr_name, attr_value')
  93. ->where([
  94. 'attr_name' => ['IN', get_arr_column($row, 'attr_name')],
  95. 'lang' => $lang,
  96. 'attr_group' => $attr_group,
  97. ])->getAllWithIndex('attr_name');
  98. if (!empty($row2)) {
  99. foreach ($bind_value as $key => $val) {
  100. if (!empty($row[$val['attr_id']])) {
  101. $val['attr_id'] = $row2[$row[$val['attr_id']]['attr_name']]['attr_value'];
  102. }
  103. $bind_value[$key] = $val;
  104. }
  105. }
  106. }
  107. } else { // 获取关联绑定的产品属性ID
  108. $attr_name = 'attr_'.$bind_value;
  109. $attr_value = Db::name('language_attr')->where([
  110. 'attr_name' => $attr_name,
  111. 'lang' => $lang,
  112. 'attr_group' => $attr_group,
  113. ])->getField('attr_value');
  114. if (empty($this->language_split)) {
  115. $bind_value = empty($attr_value) ? '' : $attr_value;
  116. } else {
  117. // $bind_value = empty($attr_value) ? $bind_value : $attr_value;
  118. }
  119. }
  120. }
  121. break;
  122. case 'guestbook_attribute':
  123. {
  124. if (is_array($bind_value)) {
  125. !empty($langvar) && $lang = $langvar;
  126. $row = Db::name('language_attr')->field('attr_name, attr_value')
  127. ->where([
  128. 'attr_value' => ['IN', get_arr_column($bind_value, 'attr_id')],
  129. 'attr_group' => $attr_group,
  130. ])->getAllWithIndex('attr_value');
  131. if (!empty($row) && empty($this->language_split)) {
  132. $row2 = Db::name('language_attr')->field('attr_name, attr_value')
  133. ->where([
  134. 'attr_name' => ['IN', get_arr_column($row, 'attr_name')],
  135. 'lang' => $lang,
  136. 'attr_group' => $attr_group,
  137. ])->getAllWithIndex('attr_name');
  138. if (!empty($row2)) {
  139. foreach ($bind_value as $key => $val) {
  140. if (!empty($row[$val['attr_id']])) {
  141. $val['attr_id'] = $row2[$row[$val['attr_id']]['attr_name']]['attr_value'];
  142. }
  143. $bind_value[$key] = $val;
  144. }
  145. }
  146. }
  147. } else { // 获取关联绑定的留言属性ID
  148. $attr_name = 'attr_'.$bind_value;
  149. $attr_value = Db::name('language_attr')->where([
  150. 'attr_name' => $attr_name,
  151. 'lang' => $lang,
  152. 'attr_group' => $attr_group,
  153. ])->getField('attr_value');
  154. if (empty($this->language_split)) {
  155. $bind_value = empty($attr_value) ? '' : $attr_value;
  156. } else {
  157. // $bind_value = empty($attr_value) ? $bind_value : $attr_value;
  158. }
  159. }
  160. }
  161. break;
  162. case 'ad_position':
  163. {
  164. if (!is_array($bind_value)) {// 获取关联绑定的广告位置ID
  165. $attr_name = 'adp'.$bind_value;
  166. $attr_value = Db::name('language_attr')->where([
  167. 'attr_name' => $attr_name,
  168. 'lang' => $lang,
  169. 'attr_group' => $attr_group,
  170. ])->getField('attr_value');
  171. if (empty($this->language_split)) {
  172. $bind_value = empty($attr_value) ? '' : $attr_value;
  173. } else {
  174. // $bind_value = empty($attr_value) ? $bind_value : $attr_value;
  175. }
  176. }
  177. }
  178. break;
  179. case 'ad':
  180. {
  181. if (!is_array($bind_value)) {// 获取关联绑定的广告ID
  182. $attr_name = 'ad'.$bind_value;
  183. $attr_value = Db::name('language_attr')->where([
  184. 'attr_name' => $attr_name,
  185. 'lang' => $lang,
  186. 'attr_group' => $attr_group,
  187. ])->getField('attr_value');
  188. if (empty($this->language_split)) {
  189. $bind_value = empty($attr_value) ? '' : $attr_value;
  190. } else {
  191. // $bind_value = empty($attr_value) ? $bind_value : $attr_value;
  192. }
  193. }
  194. }
  195. break;
  196. case 'links_group':
  197. {
  198. if (!is_array($bind_value)) {// 获取关联绑定的广告位置ID
  199. $attr_name = 'linksgroup'.$bind_value;
  200. $attr_value = Db::name('language_attr')->where([
  201. 'attr_name' => $attr_name,
  202. 'lang' => $lang,
  203. 'attr_group' => $attr_group,
  204. ])->getField('attr_value');
  205. if (empty($this->language_split)) {
  206. $bind_value = empty($attr_value) ? '' : $attr_value;
  207. } else {
  208. // $bind_value = empty($attr_value) ? $bind_value : $attr_value;
  209. }
  210. }
  211. }
  212. break;
  213. case 'form':
  214. {
  215. if (!is_array($bind_value)) {// 获取关联绑定的表单ID
  216. $attr_name = 'form'.$bind_value;
  217. $attr_value = Db::name('language_attr')->where([
  218. 'attr_name' => $attr_name,
  219. 'lang' => $lang,
  220. 'attr_group' => $attr_group,
  221. ])->getField('attr_value');
  222. if (empty($this->language_split)) {
  223. $bind_value = empty($attr_value) ? '' : $attr_value;
  224. } else {
  225. // $bind_value = empty($attr_value) ? $bind_value : $attr_value;
  226. }
  227. }
  228. }
  229. break;
  230. case 'form_attribute':
  231. {
  232. if (is_array($bind_value)) {
  233. !empty($langvar) && $lang = $langvar;
  234. $row = Db::name('language_attr')->field('attr_name, attr_value')
  235. ->where([
  236. 'attr_value' => ['IN', get_arr_column($bind_value, 'attr_id')],
  237. 'attr_group' => $attr_group,
  238. ])->getAllWithIndex('attr_value');
  239. if (!empty($row) && empty($this->language_split)) {
  240. $row2 = Db::name('language_attr')->field('attr_name, attr_value')
  241. ->where([
  242. 'attr_name' => ['IN', get_arr_column($row, 'attr_name')],
  243. 'lang' => $lang,
  244. 'attr_group' => $attr_group,
  245. ])->getAllWithIndex('attr_name');
  246. if (!empty($row2)) {
  247. foreach ($bind_value as $key => $val) {
  248. if (!empty($row[$val['attr_id']])) {
  249. $val['attr_id'] = $row2[$row[$val['attr_id']]['attr_name']]['attr_value'];
  250. }
  251. $bind_value[$key] = $val;
  252. }
  253. }
  254. }
  255. } else { // 获取关联绑定的表单属性ID
  256. $attr_name = 'attr_'.$bind_value;
  257. $attr_value = Db::name('language_attr')->where([
  258. 'attr_name' => $attr_name,
  259. 'lang' => $lang,
  260. 'attr_group' => $attr_group,
  261. ])->getField('attr_value');
  262. if (empty($this->language_split)) {
  263. $bind_value = empty($attr_value) ? '' : $attr_value;
  264. } else {
  265. // $bind_value = empty($attr_value) ? $bind_value : $attr_value;
  266. }
  267. }
  268. }
  269. break;
  270. default:
  271. # code...
  272. break;
  273. }
  274. }
  275. return $bind_value;
  276. }
  277. /**
  278. * 获取关联绑定的主语言的变量值
  279. * @param string|array $bind_value 绑定之前的值,或者绑定之后的值
  280. * @param string $group 分组
  281. */
  282. public function getBindMainValue($bind_value = '', $attr_group = 'arctype')
  283. {
  284. /*单语言情况下不执行多语言代码*/
  285. if (!is_language()) {
  286. return $bind_value;
  287. }
  288. /*--end*/
  289. $main_lang = get_main_lang();
  290. if (!empty($bind_value)) {
  291. switch ($attr_group) {
  292. case 'ad':
  293. {
  294. if (!is_array($bind_value)) {// 获取关联绑定的广告ID
  295. $attr_name = Db::name('language_attr')->where([
  296. 'attr_value' => $bind_value,
  297. 'attr_group' => $attr_group,
  298. ])->getField('attr_name');
  299. $attr_value = Db::name('language_attr')->where([
  300. 'attr_name' => $attr_name,
  301. 'lang' => $main_lang,
  302. 'attr_group' => $attr_group,
  303. ])->getField('attr_value');
  304. !empty($attr_value) && $bind_value = $attr_value;
  305. }
  306. }
  307. break;
  308. default:
  309. # code...
  310. break;
  311. }
  312. }
  313. return $bind_value;
  314. }
  315. }