ArticleService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2020-01-30 14:40
  7. */
  8. namespace common\services;
  9. use Yii;
  10. use backend\models\search\ArticleSearch;
  11. use common\libs\Constants;
  12. use common\models\Article;
  13. use common\models\ArticleContent;
  14. use common\models\Comment;
  15. use yii\base\Exception;
  16. use yii\helpers\ArrayHelper;
  17. use yii\web\NotFoundHttpException;
  18. class ArticleService extends Service implements ArticleServiceInterface
  19. {
  20. public function getSearchModel(array $options = [])
  21. {
  22. return new ArticleSearch();
  23. }
  24. public function getModel($id, array $options = [])
  25. {
  26. $model = Article::findOne($id);
  27. if( isset($options['scenario']) && !empty($options['scenario']) ){
  28. if($model !== null) {
  29. $model->setScenario($options['scenario']);
  30. }
  31. }
  32. return $model;
  33. }
  34. public function newModel(array $options = [])
  35. {
  36. $type = Article::ARTICLE;
  37. isset($options['scenario']) && $type = $options['scenario'];
  38. $model = new Article(['scenario' => $type]);
  39. $model->loadDefaultValues();
  40. return $model;
  41. }
  42. public function newArticleContentModel(array $options= [])
  43. {
  44. return new ArticleContent();
  45. }
  46. public function getArticleContentDetail($id, array $options = [])
  47. {
  48. $model = ArticleContent::findOne(['aid'=>$id]);
  49. if( empty($model) ){
  50. throw new NotFoundHttpException("Id " . $id . " not exists");
  51. }
  52. return $model;
  53. }
  54. public function create(array $postData, array $options = [])
  55. {
  56. $articleModel = new Article(['scenario'=>$options['scenario']]);
  57. $articleContentModel = new ArticleContent();
  58. if( !$articleModel->load($postData) || !$articleContentModel->load($postData) ){
  59. return [
  60. 'articleModel' => $articleModel,
  61. 'articleContentModel' => $articleContentModel,
  62. ];
  63. }
  64. $db = Yii::$app->getDb();
  65. $transaction = $db->beginTransaction();
  66. try{
  67. if ( !$articleModel->save() ){
  68. throw new Exception("save article error");
  69. }
  70. $articleContentModel->setAttribute("aid", $articleModel->id);
  71. if( !$articleContentModel->save() ){
  72. throw new Exception("save article content error");
  73. }
  74. $transaction->commit();
  75. }catch (Exception $exception){
  76. Yii::error("create article failed:" . $exception->getFile() . "(" . $exception->getLine() . ")" . $exception->getMessage() );
  77. $transaction->rollBack();
  78. return [
  79. 'articleModel' => $articleModel,
  80. 'articleContentModel' => $articleContentModel,
  81. ];
  82. }
  83. return true;
  84. }
  85. public function update($id, array $postData, array $options = [])
  86. {
  87. /** @var Article $articleModel */
  88. $articleModel = $this->getDetail($id, $options);
  89. /** @var ArticleContent $articleContentModel */
  90. $articleContentModel = $this->getArticleContentDetail($id);
  91. if ( isset($postData[$articleModel->formName()]) && !$articleModel->load($postData) ) {
  92. return [
  93. 'articleModel' => $articleModel,
  94. 'articleContentModel' => $articleContentModel,
  95. ];
  96. }
  97. if( isset($postData[$articleContentModel->formName()]) && !$articleContentModel->load($postData) ){
  98. return [
  99. 'articleModel' => $articleModel,
  100. 'articleContentModel' => $articleContentModel,
  101. ];
  102. }
  103. $db = Yii::$app->getDb();
  104. $transaction = $db->beginTransaction();
  105. try {
  106. if (!$articleModel->save()) {
  107. throw new Exception("save article error");
  108. }
  109. if (!$articleContentModel->save()) {
  110. throw new Exception("save article content error");
  111. }
  112. $transaction->commit();
  113. } catch (Exception $exception) {
  114. $transaction->rollBack();
  115. return [
  116. 'articleModel' => $articleModel,
  117. 'articleContentModel' => $articleContentModel,
  118. ];
  119. }
  120. return true;
  121. }
  122. public function delete($id, array $options = [])
  123. {
  124. /** @var Article $articleModel */
  125. $articleModel = $this->getDetail($id, $options);
  126. /** @var ArticleContent $articleContentModel */
  127. $articleContentModel = $this->getArticleContentDetail($id);
  128. $db = Yii::$app->getDb();
  129. $transaction = $db->beginTransaction();
  130. try{
  131. if( $articleModel->delete() === false || $articleContentModel->delete() === false || !is_int( Comment::deleteAll(['aid'=>$id]) ) ){
  132. throw new \Exception("delete article failed");
  133. }
  134. $transaction->commit();
  135. }catch (Exception $exception){
  136. $transaction->rollBack();
  137. $articleModel->addError("id", $exception->getMessage());
  138. return $articleModel;
  139. };
  140. return true;
  141. }
  142. public function getFlagHeadLinesArticles($limit, $sort = SORT_DESC)
  143. {
  144. return Article::find()->limit($limit)->where(['flag_headline'=>Constants::YesNo_Yes])->limit($limit)->with('category')->orderBy(["sort"=>$sort])->all();
  145. }
  146. public function getArticleSubTitle($subTitle)
  147. {
  148. return Article::findOne(['type' => Article::SINGLE_PAGE, 'sub_title' => $subTitle]);
  149. }
  150. public function getArticleById($aid)
  151. {
  152. return Article::find()->where(['id'=>$aid, "status"=>Constants::YesNo_Yes, 'type'=>Article::ARTICLE])->one();
  153. }
  154. public function getArticlesCountByPeriod($startAt=null, $endAt=null)
  155. {
  156. $model = Article::find();
  157. $model->andWhere(["type"=>Article::ARTICLE]);
  158. if( $startAt != null && $endAt != null ){
  159. $model->andWhere(["between", "created_at", $startAt, $endAt]);
  160. }else if ($startAt != null){
  161. $model->andwhere([">", "created_at", $startAt]);
  162. } else if($endAt != null){
  163. $model->andWhere(["<", "created_at", $endAt]);
  164. }
  165. return $model->count('id');
  166. }
  167. public function getFrontendURLManager()
  168. {
  169. $localConfig = [];
  170. if (file_exists(Yii::getAlias("@frontend/config/main-local.php"))) {
  171. $localConfig = require Yii::getAlias("@frontend/config/main-local.php");
  172. }
  173. $config = ArrayHelper::merge(
  174. require Yii::getAlias("@frontend/config/main.php"),
  175. $localConfig
  176. );
  177. $properties = [];
  178. if( isset( $config['components']['urlManager']) ){
  179. $properties = $config['components']['urlManager'];
  180. }
  181. $urlManager = Yii::$app->getUrlManager();
  182. $frontendURLManager = clone $urlManager;
  183. Yii::configure($frontendURLManager, $properties);
  184. return $frontendURLManager;
  185. }
  186. public function getSinglePages()
  187. {
  188. return Article::find()->where(['type' => Article::SINGLE_PAGE])->all();
  189. }
  190. }