ArticleMongodb.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\article;
  10. //use fecshop\models\mongodb\cms\Article;
  11. use Yii;
  12. use yii\base\InvalidValueException;
  13. use fecshop\services\Service;
  14. /**
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class ArticleMongodb extends Service implements ArticleInterface
  19. {
  20. public $numPerPage = 20;
  21. protected $_articleModelName = '\fecshop\models\mongodb\cms\Article';
  22. protected $_articleModel;
  23. public function init()
  24. {
  25. parent::init();
  26. list($this->_articleModelName, $this->_articleModel) = Yii::mapGet($this->_articleModelName);
  27. }
  28. public function getPrimaryKey()
  29. {
  30. return '_id';
  31. }
  32. public function getByPrimaryKey($primaryKey)
  33. {
  34. if ($primaryKey) {
  35. return $this->_articleModel->findOne($primaryKey);
  36. } else {
  37. return new $this->_articleModelName;
  38. }
  39. }
  40. /**
  41. * @param $urlKey | String , 对应表的url_key字段
  42. * 根据url_key 查询得到article model
  43. */
  44. public function getByUrlKey($urlKey)
  45. {
  46. if ($urlKey) {
  47. $model = $this->_articleModel->findOne(['url_key' => '/'.$urlKey]);
  48. if (isset($model['url_key'])) {
  49. return $model;
  50. }
  51. }
  52. return false;
  53. }
  54. /*
  55. * example filter:
  56. * [
  57. * 'numPerPage' => 20,
  58. * 'pageNum' => 1,
  59. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  60. * 'where' => [
  61. ['>','price',1],
  62. ['<=','price',10]
  63. * ['sku' => 'uk10001'],
  64. * ],
  65. * 'asArray' => true,
  66. * ]
  67. */
  68. public function coll($filter = '')
  69. {
  70. $query = $this->_articleModel->find();
  71. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  72. return [
  73. 'coll' => $query->all(),
  74. 'count'=> $query->limit(null)->offset(null)->count(),
  75. ];
  76. }
  77. /**
  78. * @param $one|array
  79. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  80. */
  81. public function save($one, $originUrlKey)
  82. {
  83. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  84. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  85. if ($primaryVal) {
  86. $model = $this->_articleModel->findOne($primaryVal);
  87. if (!$model) {
  88. Yii::$service->helper->errors->add('article {primaryKey} is not exist' , ['primaryKey' => $this->getPrimaryKey()]);
  89. return;
  90. }
  91. } else {
  92. $model = new $this->_articleModelName;
  93. $model->created_at = time();
  94. $model->created_user_id = \fec\helpers\CUser::getCurrentUserId();
  95. $primaryVal = new \MongoDB\BSON\ObjectId();
  96. $model->{$this->getPrimaryKey()} = $primaryVal;
  97. }
  98. $model->updated_at = time();
  99. unset($one['_id']);
  100. $saveStatus = Yii::$service->helper->ar->save($model, $one);
  101. $originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $primaryVal;
  102. $originUrlKey = isset($one['url_key']) ? $one['url_key'] : '';
  103. $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['title'], 'title');
  104. $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey);
  105. $model->url_key = $urlKey;
  106. $this->initStatus($model);
  107. $model->save();
  108. $model['_id'] = (string)$model['_id'];
  109. return $model->attributes;
  110. }
  111. protected function initStatus($model)
  112. {
  113. $statusArr = [$model::STATUS_ACTIVE, $model::STATUS_DELETED];
  114. if ($model['status']) {
  115. $model['status'] = (int) $model['status'];
  116. if (!in_array($model['status'], $statusArr)) {
  117. $model['status'] = $model::STATUS_ACTIVE;
  118. }
  119. } else {
  120. $model['status'] = $model::STATUS_ACTIVE;
  121. }
  122. }
  123. /**
  124. * remove article.
  125. */
  126. public function remove($ids)
  127. {
  128. if (!$ids) {
  129. Yii::$service->helper->errors->add('remove id is empty');
  130. return false;
  131. }
  132. if (is_array($ids) && !empty($ids)) {
  133. $deleteAll = true;
  134. foreach ($ids as $id) {
  135. $model = $this->_articleModel->findOne($id);
  136. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  137. $url_key = $model['url_key'];
  138. Yii::$service->url->removeRewriteUrlKey($url_key);
  139. $model->delete();
  140. } else {
  141. //throw new InvalidValueException("ID:$id is not exist.");
  142. Yii::$service->helper->errors->add('Article Remove Errors:ID {id} is not exist.', ['id' => $id]);
  143. $deleteAll = false;
  144. }
  145. }
  146. return $deleteAll;
  147. } else {
  148. $id = $ids;
  149. $model = $this->_articleModel->findOne($id);
  150. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  151. $url_key = $model['url_key'];
  152. Yii::$service->url->removeRewriteUrlKey($url_key);
  153. $model->delete();
  154. } else {
  155. Yii::$service->helper->errors->add('Article Remove Errors:ID:{id} is not exist', ['id' => $id]);
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. }