StaticBlock.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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; // = 'StaticBlockMysqldb'; // StaticBlockMongodb | StaticBlockMysqldb 当前的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. // 从数据库配置中得到值, 设置成当前service存储,是Mysqldb 还是 Mongodb
  40. $config = Yii::$app->store->get('service_db', 'article_and_staticblock');
  41. $this->storage = 'StaticBlockMysqldb';
  42. if ($config == Yii::$app->store->serviceMongodbName) {
  43. $this->storage = 'StaticBlockMongodb';
  44. }
  45. $currentService = $this->getStorageService($this);
  46. $this->_static_block = new $currentService();
  47. /*
  48. if ($this->storage == 'mongodb') {
  49. $this->_static_block = new StaticBlockMongodb();
  50. } elseif ($this->storage == 'mysqldb') {
  51. $this->_static_block = new StaticBlockMysqldb();
  52. }
  53. */
  54. }
  55. /**
  56. * get store static block content by identify
  57. * example <?= Yii::$service->cms->staticblock->getStoreContentByIdentify('home-big-img','appfront') ?>.
  58. */
  59. protected function actionGetStoreContentByIdentify($identify, $app = 'common')
  60. {
  61. $staticBlock = $this->_static_block->getByIdentify($identify);
  62. $content = $staticBlock['content'];
  63. $storeContent = Yii::$service->store->getStoreAttrVal($content, 'content');
  64. $_params_ = $this->getStaticBlockVariableArr($app);
  65. ob_start();
  66. ob_implicit_flush(false);
  67. extract($_params_, EXTR_OVERWRITE);
  68. foreach ($_params_ as $k => $v) {
  69. $key = '{{'.$k.'}}';
  70. if (strstr($storeContent, $key)) {
  71. $storeContent = str_replace($key, $v, $storeContent);
  72. }
  73. }
  74. echo $storeContent;
  75. return ob_get_clean();
  76. }
  77. /**
  78. * staticblock中的变量,可以通过{{homeUlr}},来获取下面的值。
  79. */
  80. protected function getStaticBlockVariableArr($app)
  81. {
  82. return [
  83. 'homeUrl' => Yii::$service->url->homeUrl(),
  84. 'imgBaseUrl'=> Yii::$service->image->getBaseImgUrl(),
  85. ];
  86. }
  87. /**
  88. * get artile's primary key.
  89. */
  90. protected function actionGetPrimaryKey()
  91. {
  92. return $this->_static_block->getPrimaryKey();
  93. }
  94. /**
  95. * get artile model by primary key.
  96. */
  97. protected function actionGetByPrimaryKey($primaryKey)
  98. {
  99. return $this->_static_block->getByPrimaryKey($primaryKey);
  100. }
  101. /**
  102. * @param $filter|array
  103. * get artile collection by $filter
  104. * example filter:
  105. * [
  106. * 'numPerPage' => 20,
  107. * 'pageNum' => 1,
  108. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  109. * 'where' => [
  110. * ['>','price',1],
  111. * ['<=','price',10]
  112. * ['sku' => 'uk10001'],
  113. * ],
  114. * 'asArray' => true,
  115. * ]
  116. */
  117. protected function actionColl($filter = '')
  118. {
  119. return $this->_static_block->coll($filter);
  120. }
  121. /**
  122. * @param $one|array , save one data .
  123. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  124. */
  125. protected function actionSave($one)
  126. {
  127. return $this->_static_block->save($one);
  128. }
  129. protected function actionRemove($ids)
  130. {
  131. return $this->_static_block->remove($ids);
  132. }
  133. }