web_config.model.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * 页面模块
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class web_configModel extends Model
  11. {
  12. /**
  13. * 读取模块内容记录
  14. *
  15. * @param
  16. * @return array 数组格式的返回结果
  17. */
  18. public function getCodeRow($code_id,$web_id){
  19. $param = array();
  20. $param['code_id'] = $code_id;
  21. $param['web_id'] = $web_id;
  22. $result = $this->table('web_code')->where($param)->find();
  23. return $result;
  24. }
  25. /**
  26. * 读取模块内容记录列表
  27. *
  28. * @param
  29. * @return array 数组格式的返回结果
  30. */
  31. public function getCodeList($condition = array()){
  32. $result = $this->table('web_code')->where($condition)->order('web_id')->select();
  33. return $result;
  34. }
  35. /**
  36. * 更新模块内容信息
  37. *
  38. * @param
  39. * @return bool 布尔类型的返回结果
  40. */
  41. public function updateCode($condition,$data){
  42. $code_id = $condition['code_id'];
  43. if (intval($code_id) < 1){
  44. return false;
  45. }
  46. if (is_array($data)){
  47. $result = $this->table('web_code')->where($condition)->update($data);
  48. return $result;
  49. } else {
  50. return false;
  51. }
  52. }
  53. /**
  54. * 读取记录列表
  55. *
  56. * @param
  57. * @return array 数组格式的返回结果
  58. */
  59. public function getWebList($condition = array('web_page' => 'index'),$page = ''){
  60. $result = $this->table('web')->where($condition)->order('web_sort')->page($page)->select();
  61. return $result;
  62. }
  63. /**
  64. * 更新模块信息
  65. *
  66. * @param
  67. * @return bool 布尔类型的返回结果
  68. */
  69. public function updateWeb($condition,$data){
  70. $web_id = $condition['web_id'];
  71. if (intval($web_id) < 1){
  72. return false;
  73. }
  74. if (is_array($data)){
  75. $result = $this->table('web')->where($condition)->update($data);
  76. return $result;
  77. } else {
  78. return false;
  79. }
  80. }
  81. /**
  82. * 更新模块html信息
  83. *
  84. */
  85. public function updateWebHtml($web_id = 1,$style_name = 'orange')
  86. {
  87. $web_html = '';
  88. $code_list = $this->getCodeList(array('web_id'=>"$web_id"));
  89. if(!empty($code_list) && is_array($code_list)) {
  90. Language::read('web_config,home_index_index');
  91. $lang = Language::getLangContent();
  92. $output = array();
  93. $output['style_name'] = $style_name;
  94. foreach ($code_list as $key => $val) {
  95. $var_name = $val['var_name'];
  96. $code_info = $val['code_info'];
  97. $code_type = $val['code_type'];
  98. $val['code_info'] = $this->get_array($code_info,$code_type);
  99. $output['code_'.$var_name] = $val;
  100. }
  101. switch ($web_id) {
  102. case 101:
  103. $style_file = BASE_DATA_PATH.DS.'resource'.DS.'web_config'.DS.'focus.php';
  104. break;
  105. case 121:
  106. $style_file = BASE_DATA_PATH.DS.'resource'.DS.'web_config'.DS.'sale_goods.php';
  107. break;
  108. default:
  109. $style_file = BASE_DATA_PATH.DS.'resource'.DS.'web_config'.DS.'default.php';
  110. break;
  111. }
  112. if (file_exists($style_file)) {
  113. ob_start();
  114. include $style_file;
  115. $web_html = ob_get_contents();
  116. ob_end_clean();
  117. }
  118. $web_array = array();
  119. $web_array['web_html'] = addslashes($web_html);
  120. $web_array['update_time'] = time();
  121. $this->updateWeb(array('web_id'=>$web_id),$web_array);
  122. }
  123. return $web_html;
  124. }
  125. /**
  126. * 模块html信息
  127. *
  128. */
  129. public function getWebHtml($web_page = 'index',$update_all = 0)
  130. {
  131. $web_array = array();
  132. $web_list = $this->getWebList(array('web_show'=>1,'web_page'=> array('like',$web_page.'%')));
  133. if(!empty($web_list) && is_array($web_list)) {
  134. foreach($web_list as $k => $v){
  135. $key = $v['web_page'];
  136. if ($update_all == 1 || empty($v['web_html'])) {//强制更新或内容为空时查询数据库
  137. $web_array[$key] .= $this->updateWebHtml($v['web_id'],$v['style_name']);
  138. } else {
  139. $web_array[$key] .= $v['web_html'];
  140. }
  141. }
  142. }
  143. return $web_array;
  144. }
  145. /**
  146. * 读取广告位记录列表
  147. *
  148. */
  149. public function getAdvList($type = 'screen')
  150. {
  151. $condition = array();
  152. $condition['screen'] = array(
  153. 'ap_class' => '0',//图片
  154. 'is_use' => '1',//启用
  155. 'ap_width' => '1920',//宽度
  156. 'ap_height' => '481'//高度
  157. );
  158. $condition['focus'] = array(
  159. 'ap_class' => '0',//图片
  160. 'is_use' => '1',//启用
  161. 'ap_width' => '259',//宽度
  162. 'ap_height' => '180'//高度
  163. );
  164. $result = $this->table('adv_position')->where($condition[$type])->order('ap_id desc')->select();
  165. return $result;
  166. }
  167. /**
  168. * 主题样式名称
  169. *
  170. */
  171. public function getStyleList($style_id = 'index'){
  172. $style_data = array(
  173. 'red' => Language::get('web_config_style_red'),
  174. 'pink' => Language::get('web_config_style_pink'),
  175. 'orange' => Language::get('web_config_style_orange'),
  176. 'green' => Language::get('web_config_style_green'),
  177. 'blue' => Language::get('web_config_style_blue'),
  178. 'purple' => Language::get('web_config_style_purple'),
  179. 'brown' => Language::get('web_config_style_brown'),
  180. 'default' => Language::get('web_config_style_default')
  181. );
  182. $result['index'] = $style_data;
  183. return $result[$style_id];
  184. }
  185. /**
  186. * 转换字符串
  187. */
  188. public function get_array($code_info,$code_type){
  189. $data = '';
  190. switch ($code_type) {
  191. case "array":
  192. if(is_string($code_info)) $code_info = unserialize($code_info);
  193. if(!is_array($code_info)) $code_info = array();
  194. $data = $code_info;
  195. break;
  196. case "html":
  197. if(!is_string($code_info)) $code_info = '';
  198. $data = $code_info;
  199. break;
  200. default:
  201. $data = '';
  202. break;
  203. }
  204. return $data;
  205. }
  206. /**
  207. * 转换数组
  208. */
  209. public function get_str($code_info,$code_type){
  210. $str = '';
  211. switch ($code_type) {
  212. case "array":
  213. if(!is_array($code_info)) $code_info = array();
  214. $code_info = $this->stripslashes_deep($code_info);
  215. $str = serialize($code_info);
  216. $str = addslashes($str);
  217. break;
  218. case "html":
  219. if(!is_string($code_info)) $code_info = '';
  220. $str = $code_info;
  221. break;
  222. default:
  223. $str = '';
  224. break;
  225. }
  226. return $str;
  227. }
  228. /**
  229. * 递归去斜线
  230. */
  231. public function stripslashes_deep($value){
  232. $value = is_array($value) ? array_map(array($this,'stripslashes_deep'), $value) : stripslashes($value);
  233. return $value;
  234. }
  235. /**
  236. * 商品列表,价格以促销价显示
  237. *
  238. * @param
  239. * @return array 数组格式的返回结果
  240. */
  241. public function getGoodsList($condition = array(),$order = 'goods_id desc',$page = ''){
  242. $list = array();
  243. $model_goods = Model('goods');
  244. $field = 'goods_id,goods_commonid,goods_name,goods_image,goods_price,goods_marketprice';
  245. $goods_list = $model_goods->getGoodsListByColorDistinct($condition,$field,$order,$page);
  246. if (!empty($goods_list) && is_array($goods_list)) {
  247. $goods_commonlist = array();//商品公共ID关联商品ID数组
  248. foreach ($goods_list as $key => $value) {
  249. $goods_id = $value['goods_id'];
  250. $goods_commonid = $value['goods_commonid'];
  251. $goods_commonlist[$goods_commonid][] = $goods_id;
  252. $value['goods_type'] = 1;
  253. $list[$goods_id] = $value;
  254. }
  255. $goods_ids = array_keys($list);//商品ID数组
  256. if (C('promotion_allow')) {//限时折扣
  257. $xianshi_list = Model('p_xianshi_goods')->getXianshiGoodsListByGoodsString(implode(',', $goods_ids));
  258. if (!empty($xianshi_list) && is_array($xianshi_list)) {
  259. foreach ($xianshi_list as $key => $value) {
  260. $goods_id = $value['goods_id'];
  261. $goods_price = $value['xianshi_price'];
  262. $list[$goods_id]['goods_price'] = $goods_price;
  263. $list[$goods_id]['goods_type'] = 3;
  264. }
  265. }
  266. }
  267. $common_ids = array_keys($goods_commonlist);//商品公共ID数组
  268. if (C('groupbuy_allow')) {//最终以抢购价为准
  269. $groupbuy_list = Model('groupbuy')->getGroupbuyListByGoodsCommonIDString(implode(',', $common_ids));
  270. if (!empty($groupbuy_list) && is_array($groupbuy_list)) {
  271. foreach ($groupbuy_list as $key => $value) {
  272. $goods_commonid = $value['goods_commonid'];
  273. $goods_price = $value['groupbuy_price'];
  274. foreach ($goods_commonlist[$goods_commonid] as $k => $v) {
  275. $goods_id = $v;
  276. $list[$goods_id]['goods_price'] = $goods_price;
  277. $list[$goods_id]['goods_type'] = 2;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. return $list;
  284. }
  285. /**
  286. * 更新商品价格信息
  287. *
  288. */
  289. public function updateWebGoods($condition = array('web_show' => '1')){
  290. $web_style_array = array();
  291. $web_list = $this->getWebList($condition);//板块列表
  292. if(!empty($web_list) && is_array($web_list)) {
  293. foreach($web_list as $k => $v){
  294. $web_id = $v['web_id'];
  295. $web_style_array[$web_id] = $v['style_name'];
  296. }
  297. $goods_ids = array();//商品ID数组
  298. $condition = array();
  299. $condition['web_id'] = array('in', array_keys($web_style_array));
  300. $condition['var_name'] = array('in', array('recommend_list','sale_list'));
  301. $code_list = $this->getCodeList($condition);//有商品内容记录列表
  302. if(!empty($code_list) && is_array($code_list)) {
  303. $update_list = array();
  304. foreach ($code_list as $key => $val) {
  305. $code_id = $val['code_id'];
  306. $code_info = $val['code_info'];
  307. $code_type = $val['code_type'];
  308. $val['code_info'] = $this->get_array($code_info,$code_type);//输出变量数组
  309. $recommend_list = $val['code_info'];
  310. if (!empty($recommend_list) && is_array($recommend_list)) {
  311. foreach ($recommend_list as $k => $v) {
  312. if (!empty($v['goods_list']) && is_array($v['goods_list'])) {//商品列表
  313. $goods_id_array = array_keys($v['goods_list']);//商品ID
  314. $goods_ids = array_merge($goods_ids, $goods_id_array);
  315. $update_list[$code_id] = $val;
  316. }
  317. }
  318. }
  319. }
  320. if (!empty($goods_ids) && is_array($goods_ids)) {
  321. $condition = array();
  322. $condition['goods_id'] = array('in', $goods_ids);
  323. $goods_list = $this->getGoodsList($condition);//最新商品
  324. }
  325. foreach ($update_list as $key => $val) {
  326. $update = 0;//商品价格是否有变化
  327. foreach ($val['code_info'] as $k => $v) {
  328. if (!empty($v['goods_list']) && is_array($v['goods_list'])) {
  329. foreach ($v['goods_list'] as $k3 => $v3) {//单个商品
  330. $goods_id = $v3['goods_id'];
  331. $goods_price = $v3['goods_price'];
  332. if (!empty($goods_list[$goods_id]) && ($goods_list[$goods_id]['goods_price'] != $goods_price)) {
  333. $val['code_info'][$k]['goods_list'][$goods_id]['goods_price'] = $goods_list[$goods_id]['goods_price'];
  334. $update++;
  335. }
  336. }
  337. }
  338. }
  339. if ($update > 0) {//更新对应内容
  340. $code_id = $val['code_id'];
  341. $web_id = $val['web_id'];
  342. $code_type = $val['code_type'];
  343. $code_info = $this->get_str($val['code_info'],$code_type);
  344. $this->updateCode(array('code_id'=> $code_id),array('code_info'=> $code_info));
  345. $this->updateWebHtml($web_id,$web_style_array[$web_id]);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }