Config.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\admin;
  10. use Yii;
  11. use fecshop\services\Service;
  12. /**
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class Config extends Service
  17. {
  18. protected $_modelName = '\fecshop\models\mysqldb\admin\Config';
  19. protected $_model;
  20. /**
  21. * language attribute.
  22. */
  23. protected $_lang_attr = [
  24. ];
  25. public function init()
  26. {
  27. parent::init();
  28. list($this->_modelName, $this->_model) = Yii::mapGet($this->_modelName);
  29. }
  30. public function getPrimaryKey()
  31. {
  32. return 'id';
  33. }
  34. public function getByPrimaryKey($primaryKey)
  35. {
  36. if ($primaryKey) {
  37. $one = $this->_model->findOne($primaryKey);
  38. foreach ($this->_lang_attr as $attrName) {
  39. if (isset($one[$attrName])) {
  40. $one[$attrName] = unserialize($one[$attrName]);
  41. }
  42. }
  43. return $one;
  44. } else {
  45. return new $this->_modelName();
  46. }
  47. }
  48. /*
  49. * example filter:
  50. * [
  51. * 'numPerPage' => 20,
  52. * 'pageNum' => 1,
  53. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  54. 'where' => [
  55. ['>','price',1],
  56. ['<=','price',10]
  57. * ['sku' => 'uk10001'],
  58. * ],
  59. * 'asArray' => true,
  60. * ]
  61. */
  62. public function coll($filter = '')
  63. {
  64. $query = $this->_model->find();
  65. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  66. $coll = $query->all();
  67. if (!empty($coll)) {
  68. foreach ($coll as $k => $one) {
  69. foreach ($this->_lang_attr as $attr) {
  70. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  71. }
  72. $coll[$k] = $one;
  73. }
  74. }
  75. return [
  76. 'coll' => $coll,
  77. 'count'=> $query->limit(null)->offset(null)->count(),
  78. ];
  79. }
  80. /**
  81. * @param $one|array
  82. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  83. * @return mix, int or null
  84. */
  85. public function save($one)
  86. {
  87. $primaryKey = $this->getPrimaryKey();
  88. if ($one[$primaryKey]) {
  89. $this->_model = $this->_model->findOne($one[$primaryKey]);
  90. }
  91. $this->_model->attributes = $one;
  92. if ($this->_model->validate()) {
  93. $this->_model->save();
  94. return $this->_model[$primaryKey];
  95. } else {
  96. $errors = $this->_model->errors;
  97. Yii::$service->helper->errors->addByModelErrors($errors);
  98. }
  99. return null;
  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. $this->_model->findOne($id)->delete();
  110. }
  111. } else {
  112. $id = $ids;
  113. $this->_model->findOne($id)->delete();
  114. }
  115. return true;
  116. }
  117. }