web_config.model.php 12 KB

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