ArticleMysqldb.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\mysqldb\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 ArticleMysqldb extends Service implements ArticleInterface
  19. {
  20. public $numPerPage = 20;
  21. protected $_articleModelName = '\fecshop\models\mysqldb\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. /**
  29. * language attribute.
  30. */
  31. protected $_lang_attr = [
  32. 'title',
  33. 'meta_description',
  34. 'content',
  35. 'meta_keywords',
  36. ];
  37. public function getPrimaryKey()
  38. {
  39. return 'id';
  40. }
  41. public function getByPrimaryKey($primaryKey)
  42. {
  43. if ($primaryKey) {
  44. $one = $this->_articleModel->findOne($primaryKey);
  45. foreach ($this->_lang_attr as $attrName) {
  46. if (isset($one[$attrName])) {
  47. $one[$attrName] = unserialize($one[$attrName]);
  48. }
  49. }
  50. return $one;
  51. } else {
  52. return new $this->_articleModelName();
  53. }
  54. }
  55. /**
  56. * @param $urlKey | String , 对应表的url_key字段
  57. * 根据url_key 查询得到article model
  58. */
  59. public function getByUrlKey($urlKey)
  60. {
  61. if ($urlKey) {
  62. $model = $this->_articleModel->findOne(['url_key' => '/'.$urlKey]);
  63. if (isset($model['url_key'])) {
  64. $model['content'] = unserialize($model['content']);
  65. $model['title'] = unserialize($model['title']);
  66. $model['meta_keywords'] = unserialize($model['meta_keywords']);
  67. $model['meta_description'] = unserialize($model['meta_description']);
  68. //var_dump($model);
  69. return $model;
  70. }
  71. }
  72. return false;
  73. }
  74. /*
  75. * example filter:
  76. * [
  77. * 'numPerPage' => 20,
  78. * 'pageNum' => 1,
  79. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  80. * 'where' => [
  81. * ['>','price',1],
  82. * ['<=','price',10]
  83. * ['sku' => 'uk10001'],
  84. * ],
  85. * 'asArray' => true,
  86. * ]
  87. */
  88. public function coll($filter = '')
  89. {
  90. $query = $this->_articleModel->find();
  91. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  92. $coll = $query->all();
  93. if (!empty($coll)) {
  94. foreach ($coll as $k => $one) {
  95. foreach ($this->_lang_attr as $attr) {
  96. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  97. }
  98. $coll[$k] = $one;
  99. }
  100. }
  101. //var_dump($coll);
  102. return [
  103. 'coll' => $coll,
  104. 'count'=> $query->limit(null)->offset(null)->count(),
  105. ];
  106. }
  107. /**
  108. * @param $one|array
  109. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  110. */
  111. public function save($one, $originUrlKey)
  112. {
  113. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  114. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  115. if ($primaryVal) {
  116. $model = $this->_articleModel->findOne($primaryVal);
  117. if (!$model) {
  118. Yii::$service->helper->errors->add('article {primaryKey} is not exist' , ['primaryKey' => $this->getPrimaryKey()]);
  119. return;
  120. }
  121. } else {
  122. $model = new $this->_articleModelName();
  123. $model->created_at = time();
  124. $model->created_user_id = \fec\helpers\CUser::getCurrentUserId();
  125. }
  126. $model->updated_at = time();
  127. foreach ($this->_lang_attr as $attrName) {
  128. if (is_array($one[$attrName]) && !empty($one[$attrName])) {
  129. $one[$attrName] = serialize($one[$attrName]);
  130. }
  131. }
  132. unset($one['id']);
  133. $primaryKey = $this->getPrimaryKey();
  134. $saveStatus = Yii::$service->helper->ar->save($model, $one);
  135. $primaryVal = $model[$primaryKey];
  136. $originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $primaryVal;
  137. $originUrlKey = isset($one['url_key']) ? $one['url_key'] : '';
  138. $defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['title'], 'title');
  139. $urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey);
  140. $model->url_key = $urlKey;
  141. $this->initStatus($model);
  142. $model->save();
  143. // 保存的数据格式返回
  144. $model['content'] = unserialize($model['content']);
  145. $model['title'] = unserialize($model['title']);
  146. $model['meta_keywords'] = unserialize($model['meta_keywords']);
  147. $model['meta_description'] = unserialize($model['meta_description']);
  148. return $model->attributes;
  149. }
  150. protected function initStatus($model)
  151. {
  152. $statusArr = [$model::STATUS_ACTIVE, $model::STATUS_DELETED];
  153. if ($model['status']) {
  154. $model['status'] = (int) $model['status'];
  155. if (!in_array($model['status'], $statusArr)) {
  156. $model['status'] = $model::STATUS_ACTIVE;
  157. }
  158. } else {
  159. $model['status'] = $model::STATUS_ACTIVE;
  160. }
  161. }
  162. public function remove($ids)
  163. {
  164. if (!$ids) {
  165. Yii::$service->helper->errors->add('remove id is empty');
  166. return false;
  167. }
  168. if (is_array($ids) && !empty($ids)) {
  169. $innerTransaction = Yii::$app->db->beginTransaction();
  170. try {
  171. foreach ($ids as $id) {
  172. $model = $this->_articleModel->findOne($id);
  173. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  174. $url_key = $model['url_key'];
  175. Yii::$service->url->removeRewriteUrlKey($url_key);
  176. $model->delete();
  177. } else {
  178. // throw new InvalidValueException("ID:$id is not exist.");
  179. Yii::$service->helper->errors->add('Article Remove Errors:ID {id} is not exist.', ['id' => $id]);
  180. $innerTransaction->rollBack();
  181. return false;
  182. }
  183. }
  184. $innerTransaction->commit();
  185. } catch (\Exception $e) {
  186. Yii::$service->helper->errors->add('Article Remove Errors: transaction rollback');
  187. $innerTransaction->rollBack();
  188. return false;
  189. }
  190. } else {
  191. $id = $ids;
  192. $model = $this->_articleModel->findOne($id);
  193. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  194. $innerTransaction = Yii::$app->db->beginTransaction();
  195. try {
  196. $url_key = $model['url_key'];
  197. Yii::$service->url->removeRewriteUrlKey($url_key);
  198. $model->delete();
  199. $innerTransaction->commit();
  200. } catch (\Exception $e) {
  201. Yii::$service->helper->errors->add('Article Remove Errors: transaction rollback');
  202. $innerTransaction->rollBack();
  203. }
  204. } else {
  205. Yii::$service->helper->errors->add('Article Remove Errors:ID {id} is not exist.', ['id' => $id]);
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. }