StaticBlockMongodb.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\staticblock;
  10. //use fecshop\models\mongodb\cms\StaticBlock;
  11. use Yii;
  12. use fecshop\services\Service;
  13. /**
  14. * staticBlock部分,mongodb的实现部分。
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class StaticBlockMongodb extends Service implements StaticBlockInterface
  19. {
  20. public $numPerPage = 20;
  21. protected $_staticBlockModelName = '\fecshop\models\mongodb\cms\StaticBlock';
  22. protected $_staticBlockModel;
  23. public function init()
  24. {
  25. parent::init();
  26. list($this->_staticBlockModelName, $this->_staticBlockModel) = Yii::mapGet($this->_staticBlockModelName);
  27. }
  28. public function getPrimaryKey()
  29. {
  30. return '_id';
  31. }
  32. public function getByPrimaryKey($primaryKey)
  33. {
  34. if ($primaryKey) {
  35. return $this->_staticBlockModel->findOne($primaryKey);
  36. } else {
  37. return new $this->_staticBlockModelName();
  38. }
  39. }
  40. public function getByIdentify($identify)
  41. {
  42. return $this->_staticBlockModel->find()->asArray()->where([
  43. 'identify' => $identify,
  44. ])->one();
  45. }
  46. /*
  47. * example filter:
  48. * [
  49. * 'numPerPage' => 20,
  50. * 'pageNum' => 1,
  51. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  52. 'where' => [
  53. ['>','price',1],
  54. ['<=','price',10]
  55. * ['sku' => 'uk10001'],
  56. * ],
  57. * 'asArray' => true,
  58. * ]
  59. */
  60. public function coll($filter = '')
  61. {
  62. $query = $this->_staticBlockModel->find();
  63. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  64. return [
  65. 'coll' => $query->all(),
  66. 'count'=> $query->limit(null)->offset(null)->count(),
  67. ];
  68. }
  69. /**
  70. * @param $one|array
  71. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  72. */
  73. public function save($one)
  74. {
  75. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  76. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  77. if (!($this->validateIdentify($one))) {
  78. Yii::$service->helper->errors->add('Static block: identify exit, You must define a unique identify');
  79. return;
  80. }
  81. if ($primaryVal) {
  82. $model = $this->_staticBlockModel->findOne($primaryVal);
  83. if (!$model) {
  84. Yii::$service->helper->errors->add('Static block {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  85. return;
  86. }
  87. } else {
  88. $model = new $this->_staticBlockModelName();
  89. $model->created_at = time();
  90. $model->created_user_id = \fec\helpers\CUser::getCurrentUserId();
  91. $primaryVal = new \MongoDB\BSON\ObjectId();
  92. $model->{$this->getPrimaryKey()} = $primaryVal;
  93. }
  94. $model->updated_at = time();
  95. unset($one['_id']);
  96. $saveStatus = Yii::$service->helper->ar->save($model, $one);
  97. return true;
  98. }
  99. protected function validateIdentify($one)
  100. {
  101. $identify = $one['identify'];
  102. $id = $this->getPrimaryKey();
  103. $primaryVal = isset($one[$id]) ? $one[$id] : '';
  104. $where = ['identify' => $identify];
  105. $query = $this->_staticBlockModel->find()->asArray();
  106. $query->where(['identify' => $identify]);
  107. if ($primaryVal) {
  108. $query->andWhere([$id => ['$ne'=> new \MongoDB\BSON\ObjectId($primaryVal)]]);
  109. }
  110. $one = $query->one();
  111. if (!empty($one)) {
  112. return false;
  113. }
  114. return true;
  115. }
  116. /**
  117. * remove Static Block.
  118. */
  119. public function remove($ids)
  120. {
  121. if (!$ids) {
  122. Yii::$service->helper->errors->add('remove id is empty');
  123. return false;
  124. }
  125. if (is_array($ids) && !empty($ids)) {
  126. foreach ($ids as $id) {
  127. $model = $this->_staticBlockModel->findOne($id);
  128. $model->delete();
  129. }
  130. } else {
  131. $id = $ids;
  132. $model = $this->_staticBlockModel->findOne($id);
  133. $model->delete();
  134. }
  135. return true;
  136. }
  137. }