StaticBlock.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\cms;
  10. use fecshop\services\cms\staticblock\StaticBlockMongodb;
  11. use fecshop\services\cms\staticblock\StaticBlockMysqldb;
  12. use fecshop\services\Service;
  13. use Yii;
  14. /**
  15. * Cms StaticBlock services. 静态块部分,譬如首页的某个区块,类似走马灯图,广告图等经常需要改动的部分,可以在后台进行改动。
  16. * @author Terry Zhao <2358269014@qq.com>
  17. * @since 1.0
  18. */
  19. class Staticblock extends Service
  20. {
  21. /**
  22. * $storagePrex , $storage , $storagePath 为找到当前的storage而设置的配置参数
  23. * 可以在配置中更改,更改后,就会通过容器注入的方式修改相应的配置值
  24. */
  25. public $storage = 'StaticBlockMongodb'; // 当前的storage,如果在config中配置,那么在初始化的时候会被注入修改
  26. /**
  27. * 设置storage的path路径,
  28. * 如果不设置,则系统使用默认路径
  29. * 如果设置了路径,则使用自定义的路径
  30. */
  31. public $storagePath = '';
  32. protected $_static_block;
  33. /**
  34. * init static block db.
  35. */
  36. public function init()
  37. {
  38. parent::init();
  39. $currentService = $this->getStorageService($this);
  40. $this->_static_block = new $currentService();
  41. /*
  42. if ($this->storage == 'mongodb') {
  43. $this->_static_block = new StaticBlockMongodb();
  44. } elseif ($this->storage == 'mysqldb') {
  45. $this->_static_block = new StaticBlockMysqldb();
  46. }
  47. */
  48. }
  49. /**
  50. * get store static block content by identify
  51. * example <?= Yii::$service->cms->staticblock->getStoreContentByIdentify('home-big-img','appfront') ?>.
  52. */
  53. protected function actionGetStoreContentByIdentify($identify, $app = 'common')
  54. {
  55. $staticBlock = $this->_static_block->getByIdentify($identify);
  56. $content = $staticBlock['content'];
  57. $storeContent = Yii::$service->store->getStoreAttrVal($content, 'content');
  58. $_params_ = $this->getStaticBlockVariableArr($app);
  59. ob_start();
  60. ob_implicit_flush(false);
  61. extract($_params_, EXTR_OVERWRITE);
  62. foreach ($_params_ as $k => $v) {
  63. $key = '{{'.$k.'}}';
  64. if (strstr($storeContent, $key)) {
  65. $storeContent = str_replace($key, $v, $storeContent);
  66. }
  67. }
  68. echo $storeContent;
  69. return ob_get_clean();
  70. }
  71. /**
  72. * staticblock中的变量,可以通过{{homeUlr}},来获取下面的值。
  73. */
  74. protected function getStaticBlockVariableArr($app)
  75. {
  76. return [
  77. 'homeUrl' => Yii::$service->url->homeUrl(),
  78. 'imgBaseUrl'=> Yii::$service->image->getBaseImgUrl($app),
  79. ];
  80. }
  81. /**
  82. * get artile's primary key.
  83. */
  84. protected function actionGetPrimaryKey()
  85. {
  86. return $this->_static_block->getPrimaryKey();
  87. }
  88. /**
  89. * get artile model by primary key.
  90. */
  91. protected function actionGetByPrimaryKey($primaryKey)
  92. {
  93. return $this->_static_block->getByPrimaryKey($primaryKey);
  94. }
  95. /**
  96. * @param $filter|array
  97. * get artile collection by $filter
  98. * example filter:
  99. * [
  100. * 'numPerPage' => 20,
  101. * 'pageNum' => 1,
  102. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  103. * 'where' => [
  104. * ['>','price',1],
  105. * ['<=','price',10]
  106. * ['sku' => 'uk10001'],
  107. * ],
  108. * 'asArray' => true,
  109. * ]
  110. */
  111. protected function actionColl($filter = '')
  112. {
  113. return $this->_static_block->coll($filter);
  114. }
  115. /**
  116. * @param $one|array , save one data .
  117. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  118. */
  119. protected function actionSave($one)
  120. {
  121. return $this->_static_block->save($one);
  122. }
  123. protected function actionRemove($ids)
  124. {
  125. return $this->_static_block->remove($ids);
  126. }
  127. }