RewriteMongodb.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\url\rewrite;
  10. //use fecshop\models\mongodb\url\UrlRewrite;
  11. use Yii;
  12. use fecshop\services\Service;
  13. use yii\base\InvalidValueException;
  14. /**
  15. * Url Rewrite RewriteMongodb service
  16. * @author Terry Zhao <2358269014@qq.com>
  17. * @since 1.0
  18. */
  19. class RewriteMongodb extends Service implements RewriteInterface
  20. {
  21. public $numPerPage = 20;
  22. protected $_urlRewriteModelName = '\fecshop\models\mongodb\url\UrlRewrite';
  23. protected $_urlRewriteModel;
  24. public function init()
  25. {
  26. parent::init();
  27. list($this->_urlRewriteModelName, $this->_urlRewriteModel) = \Yii::mapGet($this->_urlRewriteModelName);
  28. }
  29. /**
  30. * @param $urlKey | string
  31. * 通过重写后的urlkey字符串,去url_rewrite表中查询,找到重写前的url字符串。
  32. */
  33. public function getOriginUrl($urlKey)
  34. {
  35. $UrlData = $this->_urlRewriteModel->find()->where([
  36. 'custom_url_key' => $urlKey,
  37. ])->asArray()->one();
  38. if ($UrlData['custom_url_key']) {
  39. return $UrlData['origin_url'];
  40. }
  41. }
  42. public function getPrimaryKey()
  43. {
  44. return '_id';
  45. }
  46. public function getByPrimaryKey($primaryKey)
  47. {
  48. if ($primaryKey) {
  49. return $this->_urlRewriteModel->findOne($primaryKey);
  50. } else {
  51. return new $this->_urlRewriteModelName();
  52. }
  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->_urlRewriteModel->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)
  82. {
  83. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  84. if ($primaryVal) {
  85. $model = $this->_urlRewriteModel->findOne($primaryVal);
  86. if (!$model) {
  87. Yii::$service->helper->errors->add('UrlRewrite {primaryKey} is not exist', ['primaryKey'=>$this->getPrimaryKey()]);
  88. return;
  89. }
  90. } else {
  91. $model = new $this->_urlRewriteModelName();
  92. }
  93. unset($one['_id']);
  94. $saveStatus = Yii::$service->helper->ar->save($model, $one);
  95. return true;
  96. }
  97. /**
  98. * @param $ids | Array or String
  99. * 删除相应的url rewrite 记录
  100. */
  101. public function remove($ids)
  102. {
  103. if (!$ids) {
  104. Yii::$service->helper->errors->add('remove id is empty');
  105. return false;
  106. }
  107. if (is_array($ids) && !empty($ids)) {
  108. foreach ($ids as $id) {
  109. $model = $this->_urlRewriteModel->findOne($id);
  110. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  111. $url_key = $model['url_key'];
  112. $model->delete();
  113. } else {
  114. //throw new InvalidValueException("ID:$id is not exist.");
  115. Yii::$service->helper->errors->add('UrlRewrite Remove Errors:ID {id} is not exist.', ['id' => $id]);
  116. return false;
  117. }
  118. }
  119. } else {
  120. $id = $ids;
  121. $model = $this->_urlRewriteModel->findOne($id);
  122. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  123. $url_key = $model['url_key'];
  124. $model->delete();
  125. } else {
  126. Yii::$service->helper->errors->add('UrlRewrite Remove Errors:ID:{id} is not exist.', ['id' => $id]);
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. /**
  133. * @param $time | Int
  134. * 根据updated_at 更新时间,删除相应的url rewrite 记录
  135. */
  136. public function removeByUpdatedAt($time)
  137. {
  138. if ($time) {
  139. $this->_urlRewriteModel->deleteAll([
  140. '$or' => [
  141. [
  142. 'updated_at' => [
  143. '$lt' => (int) $time,
  144. ],
  145. ],
  146. [
  147. 'updated_at' => [
  148. '$exists' => false,
  149. ],
  150. ],
  151. ],
  152. ]);
  153. echo "delete complete \n";
  154. }
  155. }
  156. /**
  157. * 返回url rewrite model 对应的query
  158. */
  159. public function find()
  160. {
  161. return $this->_urlRewriteModel->find();
  162. }
  163. /**
  164. * 返回url rewrite 查询结果
  165. */
  166. public function findOne($where)
  167. {
  168. return $this->_urlRewriteModel->findOne($where);
  169. }
  170. /**
  171. * 返回url rewrite model
  172. */
  173. public function newModel()
  174. {
  175. return new $this->_urlRewriteModelName();
  176. }
  177. }