seo.model.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * SEO
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class seoModel extends Model{
  11. /**
  12. * 存放SEO信息
  13. *
  14. * @var obj
  15. */
  16. private $seo;
  17. public function __construct(){
  18. parent::__construct('seo');
  19. }
  20. /**
  21. * 取得SEO信息
  22. *
  23. * @param array/string $type
  24. * @return obj
  25. */
  26. public function type($type){
  27. if (is_array($type)){ //商品分类
  28. $this->seo['title'] = $type[1];
  29. $this->seo['keywords'] = $type[2];
  30. $this->seo['description'] = $type[3];
  31. }else{
  32. $this->seo = $this->getSeo($type);
  33. }
  34. if (!is_array($this->seo)) return $this;
  35. foreach ($this->seo as $key=>$value) {
  36. $this->seo[$key] = str_replace(array('{sitename}'),array(C('site_name')),$value);
  37. }
  38. return $this;
  39. }
  40. /**
  41. * 生成SEO缓存并返回
  42. *
  43. * @param string $type
  44. * @return array
  45. */
  46. private function getSeo($type){
  47. $list = rkcache('seo',true);
  48. return $list[$type];
  49. }
  50. /**
  51. * 传入参数替换SEO中的标签
  52. *
  53. * @param array $array
  54. * @return obj
  55. */
  56. public function param($array = null){
  57. if (!is_array($this->seo)) return $this;
  58. if (is_array($array)){
  59. $array_key = array_keys($array);
  60. array_walk($array_key,array(self,'addTag'));
  61. foreach ($this->seo as $key=>$value) {
  62. $this->seo[$key] = str_replace($array_key,array_values($array),$value);
  63. }
  64. }
  65. return $this;
  66. }
  67. /**
  68. * 抛出SEO信息到模板
  69. *
  70. */
  71. public function show(){
  72. $this->seo['title'] = preg_replace("/{.*}/siU",'',$this->seo['title']);
  73. $this->seo['keywords'] = preg_replace("/{.*}/siU",'',$this->seo['keywords']);
  74. $this->seo['description'] = preg_replace("/{.*}/siU",'',$this->seo['description']);
  75. Tpl::output('html_title',$this->seo['title'] ? $this->seo['title'] : C('site_name'));
  76. Tpl::output('seo_keywords',$this->seo['keywords'] ? $this->seo['keywords'] : C('site_name'));
  77. Tpl::output('seo_description',$this->seo['description'] ? $this->seo['description'] : C('site_name'));
  78. }
  79. private function addTag(&$key){
  80. $key ='{'.$key.'}';
  81. }
  82. }