RoleUrlKey.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 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 RoleUrlKey extends Service
  18. {
  19. public $numPerPage = 20;
  20. protected $_modelName = '\fecshop\models\mysqldb\admin\RoleUrlKey';
  21. protected $_model;
  22. /**
  23. * language attribute.
  24. */
  25. protected $_lang_attr = [
  26. ];
  27. public function init()
  28. {
  29. parent::init();
  30. list($this->_modelName, $this->_model) = Yii::mapGet($this->_modelName);
  31. }
  32. public function getPrimaryKey()
  33. {
  34. return 'id';
  35. }
  36. public function getByPrimaryKey($primaryKey)
  37. {
  38. if ($primaryKey) {
  39. $one = $this->_model->findOne($primaryKey);
  40. foreach ($this->_lang_attr as $attrName) {
  41. if (isset($one[$attrName])) {
  42. $one[$attrName] = unserialize($one[$attrName]);
  43. }
  44. }
  45. return $one;
  46. } else {
  47. return new $this->_modelName();
  48. }
  49. }
  50. /*
  51. * example filter:
  52. * [
  53. * 'numPerPage' => 20,
  54. * 'pageNum' => 1,
  55. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  56. 'where' => [
  57. ['>','price',1],
  58. ['<=','price',10]
  59. * ['sku' => 'uk10001'],
  60. * ],
  61. * 'asArray' => true,
  62. * ]
  63. */
  64. public function coll($filter = '')
  65. {
  66. $query = $this->_model->find();
  67. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  68. $coll = $query->all();
  69. if (!empty($coll)) {
  70. foreach ($coll as $k => $one) {
  71. foreach ($this->_lang_attr as $attr) {
  72. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  73. }
  74. $coll[$k] = $one;
  75. }
  76. }
  77. //var_dump($one);
  78. return [
  79. 'coll' => $coll,
  80. 'count'=> $query->limit(null)->offset(null)->count(),
  81. ];
  82. }
  83. /**
  84. * @param $one|array
  85. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  86. */
  87. public function save($one)
  88. {
  89. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  90. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  91. if (!($this->validateUrlKeyRoleId($one))) {
  92. Yii::$service->helper->errors->add('The url key && role id exists, you must define a unique url key && role id');
  93. return;
  94. }
  95. if ($primaryVal) {
  96. $model = $this->_model->findOne($primaryVal);
  97. if (!$model) {
  98. Yii::$service->helper->errors->add('Role Url Key {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  99. return;
  100. }
  101. } else {
  102. $model = new $this->_modelName();
  103. $model->created_at = time();
  104. }
  105. $model->updated_at = time();
  106. foreach ($this->_lang_attr as $attrName) {
  107. if (is_array($one[$attrName]) && !empty($one[$attrName])) {
  108. $one[$attrName] = serialize($one[$attrName]);
  109. }
  110. }
  111. $primaryKey = $this->getPrimaryKey();
  112. $model = Yii::$service->helper->ar->save($model, $one);
  113. $primaryVal = $model[$primaryKey];
  114. return true;
  115. }
  116. /**
  117. * @param int $roleId
  118. * @param array $url_key_ids
  119. * 先删除该role_id 对应的所有的数据,然后将这些数据依次插入到表中
  120. */
  121. public function repeatSaveRoleUrlKey($roleId, $url_key_ids){
  122. if ($roleId && is_array($url_key_ids) && !empty($url_key_ids)) {
  123. $this->_model->deleteAll([
  124. 'role_id' => $roleId
  125. ]);
  126. foreach ($url_key_ids as $url_key_id) {
  127. $model = new $this->_modelName();
  128. $model->created_at = time();
  129. $model->updated_at = time();
  130. $model->url_key_id = $url_key_id;
  131. $model->role_id = $roleId;
  132. $model->save();
  133. }
  134. return true;
  135. }
  136. return false;
  137. }
  138. protected function validateUrlKeyRoleId($one)
  139. {
  140. $url_key_id = $one['url_key_id'];
  141. $role_id = $one['role_id'];
  142. $id = $this->getPrimaryKey();
  143. $primaryVal = isset($one[$id]) ? $one[$id] : '';
  144. $where = [
  145. 'url_key_id' => $url_key_id,
  146. 'role_id' => $role_id,
  147. ];
  148. $query = $this->_model->find()->asArray();
  149. $query->where($where);
  150. if ($primaryVal) {
  151. $query->andWhere(['<>', $id, $primaryVal]);
  152. }
  153. $one = $query->one();
  154. if (!empty($one)) {
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. * @param $url_key_id int
  161. * @return bool
  162. * 按照$url_key_id为条件进行删除,一般是url_key进行删除操作的时候,删除这里的数据
  163. */
  164. public function removeByUrlKeyId($url_key_id){
  165. $this->_model->deleteAll(['url_key_id' => $url_key_id]);
  166. return true;
  167. }
  168. /**
  169. * @param $url_key_id int
  170. * @return bool
  171. * 按照$role_id为条件进行删除,一般是role进行删除操作的时候,删除这里的数据
  172. */
  173. public function removeByRoleId($role_id){
  174. $this->_model->deleteAll(['role_id' => $role_id]);
  175. return true;
  176. }
  177. public function remove($ids)
  178. {
  179. if (!$ids) {
  180. Yii::$service->helper->errors->add('remove id is empty');
  181. return false;
  182. }
  183. if (is_array($ids) && !empty($ids)) {
  184. foreach ($ids as $id) {
  185. $model = $this->_model->findOne($id);
  186. $model->delete();
  187. }
  188. } else {
  189. $id = $ids;
  190. $model = $this->_model->findOne($id);
  191. $model->delete();
  192. }
  193. return true;
  194. }
  195. }