StaticBlockMysqldb.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\staticblock;
  10. //use fecshop\models\mysqldb\cms\StaticBlock;
  11. use Yii;
  12. use fecshop\services\Service;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class StaticBlockMysqldb extends Service implements StaticBlockInterface
  18. {
  19. public $numPerPage = 20;
  20. protected $_staticBlockModelName = '\fecshop\models\mysqldb\cms\StaticBlock';
  21. protected $_staticBlockModel;
  22. /**
  23. * language attribute.
  24. */
  25. protected $_lang_attr = [
  26. 'title',
  27. 'content',
  28. ];
  29. public function init()
  30. {
  31. parent::init();
  32. list($this->_staticBlockModelName, $this->_staticBlockModel) = Yii::mapGet($this->_staticBlockModelName);
  33. }
  34. public function getPrimaryKey()
  35. {
  36. return 'id';
  37. }
  38. public function getByPrimaryKey($primaryKey)
  39. {
  40. if ($primaryKey) {
  41. $one = $this->_staticBlockModel->findOne($primaryKey);
  42. foreach ($this->_lang_attr as $attrName) {
  43. if (isset($one[$attrName])) {
  44. $one[$attrName] = unserialize($one[$attrName]);
  45. }
  46. }
  47. return $one;
  48. } else {
  49. return new $this->_staticBlockModelName();
  50. }
  51. }
  52. public function getByIdentify($identify)
  53. {
  54. $one = $this->_staticBlockModel->find()->asArray()->where([
  55. 'identify' => $identify,
  56. ])->one();
  57. foreach ($this->_lang_attr as $attrName) {
  58. if (isset($one[$attrName])) {
  59. $one[$attrName] = unserialize($one[$attrName]);
  60. }
  61. }
  62. return $one;
  63. }
  64. /*
  65. * example filter:
  66. * [
  67. * 'numPerPage' => 20,
  68. * 'pageNum' => 1,
  69. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  70. 'where' => [
  71. ['>','price',1],
  72. ['<=','price',10]
  73. * ['sku' => 'uk10001'],
  74. * ],
  75. * 'asArray' => true,
  76. * ]
  77. */
  78. public function coll($filter = '')
  79. {
  80. $query = $this->_staticBlockModel->find();
  81. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  82. $coll = $query->all();
  83. if (!empty($coll)) {
  84. foreach ($coll as $k => $one) {
  85. foreach ($this->_lang_attr as $attr) {
  86. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  87. }
  88. $coll[$k] = $one;
  89. }
  90. }
  91. //var_dump($one);
  92. return [
  93. 'coll' => $coll,
  94. 'count'=> $query->limit(null)->offset(null)->count(),
  95. ];
  96. }
  97. /**
  98. * @param $one|array
  99. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  100. */
  101. public function save($one)
  102. {
  103. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  104. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  105. if (!($this->validateIdentify($one))) {
  106. Yii::$service->helper->errors->add('Static block: identify exit, You must define a unique identify');
  107. return;
  108. }
  109. if ($primaryVal) {
  110. $model = $this->_staticBlockModel->findOne($primaryVal);
  111. if (!$model) {
  112. Yii::$service->helper->errors->add('Static block {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  113. return;
  114. }
  115. } else {
  116. $model = new $this->_staticBlockModelName();
  117. $model->created_at = time();
  118. $model->created_user_id = \fec\helpers\CUser::getCurrentUserId();
  119. }
  120. $model->updated_at = time();
  121. foreach ($this->_lang_attr as $attrName) {
  122. if (is_array($one[$attrName]) && !empty($one[$attrName])) {
  123. $one[$attrName] = serialize($one[$attrName]);
  124. }
  125. }
  126. $primaryKey = $this->getPrimaryKey();
  127. $model = Yii::$service->helper->ar->save($model, $one);
  128. $primaryVal = $model[$primaryKey];
  129. return true;
  130. }
  131. protected function validateIdentify($one)
  132. {
  133. $identify = $one['identify'];
  134. $id = $this->getPrimaryKey();
  135. $primaryVal = isset($one[$id]) ? $one[$id] : '';
  136. $where = ['identify' => $identify];
  137. $query = $this->_staticBlockModel->find()->asArray();
  138. $query->where(['identify' => $identify]);
  139. if ($primaryVal) {
  140. $query->andWhere(['<>', $id, $primaryVal]);
  141. }
  142. $one = $query->one();
  143. if (!empty($one)) {
  144. return false;
  145. }
  146. return true;
  147. }
  148. public function remove($ids)
  149. {
  150. if (!$ids) {
  151. Yii::$service->helper->errors->add('remove id is empty');
  152. return false;
  153. }
  154. if (is_array($ids) && !empty($ids)) {
  155. foreach ($ids as $id) {
  156. $model = $this->_staticBlockModel->findOne($id);
  157. $model->delete();
  158. }
  159. } else {
  160. $id = $ids;
  161. $model = $this->_staticBlockModel->findOne($id);
  162. $model->delete();
  163. }
  164. return true;
  165. }
  166. }