RewriteMysqldb.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\mysqldb\url\UrlRewrite;
  11. use Yii;
  12. use fecshop\services\Service;
  13. use yii\base\InvalidValueException;
  14. /**
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class RewriteMysqldb extends Service implements RewriteInterface
  19. {
  20. public $numPerPage = 20;
  21. /**
  22. * language attribute.
  23. */
  24. protected $_lang_attr = [
  25. ];
  26. protected $_urlRewriteModelName = '\fecshop\models\mysqldb\url\UrlRewrite';
  27. protected $_urlRewriteModel;
  28. public function init()
  29. {
  30. parent::init();
  31. list($this->_urlRewriteModelName, $this->_urlRewriteModel) = \Yii::mapGet($this->_urlRewriteModelName);
  32. }
  33. /**
  34. * @param $urlKey | string
  35. * 通过重写后的urlkey字符串,去url_rewrite表中查询,找到重写前的url字符串。
  36. */
  37. public function getOriginUrl($urlKey)
  38. {
  39. $UrlData = $this->_urlRewriteModel->find()->where([
  40. 'custom_url_key' => $urlKey,
  41. ])->asArray()->one();
  42. if ($UrlData['custom_url_key']) {
  43. return $UrlData['origin_url'];
  44. }
  45. }
  46. public function getPrimaryKey()
  47. {
  48. return 'id';
  49. }
  50. public function getByPrimaryKey($primaryKey)
  51. {
  52. if ($primaryKey) {
  53. $one = $this->_urlRewriteModel->findOne($primaryKey);
  54. if (!empty($this->_lang_attr)) {
  55. foreach ($this->_lang_attr as $attrName) {
  56. if (isset($one[$attrName])) {
  57. $one[$attrName] = unserialize($one[$attrName]);
  58. }
  59. }
  60. }
  61. return $one;
  62. } else {
  63. return new $this->_urlRewriteModelName();
  64. }
  65. }
  66. /*
  67. * example filter:
  68. * [
  69. * 'numPerPage' => 20,
  70. * 'pageNum' => 1,
  71. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  72. * where' => [
  73. ['>','price',1],
  74. ['<=','price',10]
  75. * ['sku' => 'uk10001'],
  76. * ],
  77. * 'asArray' => true,
  78. * ]
  79. */
  80. public function coll($filter = '')
  81. {
  82. $query = $this->_urlRewriteModel->find();
  83. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  84. $coll = $query->all();
  85. if (!empty($coll)) {
  86. foreach ($coll as $k => $one) {
  87. if (!empty($this->_lang_attr)) {
  88. foreach ($this->_lang_attr as $attr) {
  89. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  90. }
  91. }
  92. $coll[$k] = $one;
  93. }
  94. }
  95. //var_dump($one);
  96. return [
  97. 'coll' => $coll,
  98. 'count'=> $query->limit(null)->offset(null)->count(),
  99. ];
  100. }
  101. /**
  102. * @param $one|array
  103. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  104. */
  105. public function save($one)
  106. {
  107. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  108. if ($primaryVal) {
  109. $model = $this->_urlRewriteModel->findOne($primaryVal);
  110. if (!$model) {
  111. Yii::$service->helper->errors->add('UrlRewrite {primaryKey} is not exist', ['primaryKey'=>$this->getPrimaryKey()]);
  112. return;
  113. }
  114. } else {
  115. $model = new $this->_urlRewriteModelName();
  116. }
  117. unset($one['_id']);
  118. $saveStatus = Yii::$service->helper->ar->save($model, $one);
  119. return true;
  120. }
  121. /**
  122. * @param $ids | Array or Int
  123. * 删除相应的url rewrite 记录
  124. */
  125. public function remove($ids)
  126. {
  127. if (!$ids) {
  128. Yii::$service->helper->errors->add('remove id is empty');
  129. return false;
  130. }
  131. if (is_array($ids) && !empty($ids)) {
  132. $innerTransaction = Yii::$service->db->beginTransaction();
  133. try {
  134. foreach ($ids as $id) {
  135. $model = $this->_urlRewriteModel->findOne($id);
  136. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  137. $url_key = $model['url_key'];
  138. $model->delete();
  139. } else {
  140. //throw new InvalidValueException("ID:$id is not exist.");
  141. Yii::$service->helper->errors->add('UrlRewrite Remove Errors:ID {id} is not exist.', ['id' => $id]);
  142. $innerTransaction->rollBack();
  143. return false;
  144. }
  145. }
  146. $innerTransaction->commit();
  147. } catch (\Exception $e) {
  148. Yii::$service->helper->errors->add('UrlRewrite Remove Errors: transaction rollback');
  149. $innerTransaction->rollBack();
  150. return false;
  151. }
  152. } else {
  153. $id = $ids;
  154. $model = $this->_urlRewriteModel->findOne($id);
  155. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  156. $innerTransaction = Yii::$service->db->beginTransaction();
  157. try {
  158. $url_key = $model['url_key'];
  159. $model->delete();
  160. $innerTransaction->commit();
  161. } catch (\Exception $e) {
  162. Yii::$service->helper->errors->add('UrlRewrite Remove Errors: transaction rollback');
  163. $innerTransaction->rollBack();
  164. }
  165. } else {
  166. Yii::$service->helper->errors->add('UrlRewrite Remove Errors:ID:{id} is not exist.', ['id' => $id]);
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. /**
  173. * @param $time | Int
  174. * 根据updated_at 更新时间,删除相应的url rewrite 记录
  175. */
  176. public function removeByUpdatedAt($time)
  177. {
  178. if ($time) {
  179. $this->_urlRewriteModel->deleteAll([
  180. '<', 'updated_at', $time,
  181. ]);
  182. }
  183. }
  184. /**
  185. * 返回url rewrite model 对应的query
  186. */
  187. public function find()
  188. {
  189. return $this->_urlRewriteModel->find();
  190. }
  191. /**
  192. * 返回url rewrite 查询结果
  193. */
  194. public function findOne($where)
  195. {
  196. return $this->_urlRewriteModel->findOne($where);
  197. }
  198. /**
  199. * 返回url rewrite model
  200. */
  201. public function newModel()
  202. {
  203. return new $this->_urlRewriteModelName();
  204. }
  205. }