UrlKey.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 UrlKey extends Service
  18. {
  19. const URLKEY_LABEL_ARR = 'appadmin_urlkey_label_cache_arr';
  20. public $numPerPage = 20;
  21. public $urlKeyTags;
  22. protected $_urlKeyTags;
  23. // 没有在数据库中添加的url key
  24. public $addUrlKeyAndLabelArr = [
  25. '/fecadmin/login/index' => 'Login',
  26. '/fecadmin/logout/index' => 'Logout',
  27. ];
  28. protected $_modelName = '\fecshop\models\mysqldb\admin\UrlKey';
  29. protected $_mode;
  30. /**
  31. * language attribute.
  32. */
  33. protected $_lang_attr = [
  34. ];
  35. public function init()
  36. {
  37. parent::init();
  38. list($this->_modelName, $this->_mode) = Yii::mapGet($this->_modelName);
  39. }
  40. public function getTags($translate = true){
  41. if (!$this->_urlKeyTags) {
  42. if (is_array($this->urlKeyTags)) {
  43. foreach ($this->urlKeyTags as $k => $v) {
  44. if ($translate) {
  45. $this->_urlKeyTags[$k] = Yii::$service->page->translate->__($v);
  46. } else {
  47. $this->_urlKeyTags[$k] = $v;
  48. }
  49. }
  50. }
  51. }
  52. return $this->_urlKeyTags;
  53. }
  54. public function getPrimaryKey()
  55. {
  56. return 'id';
  57. }
  58. public function getByPrimaryKey($primaryKey)
  59. {
  60. if ($primaryKey) {
  61. $one = $this->_mode->findOne($primaryKey);
  62. foreach ($this->_lang_attr as $attrName) {
  63. if (isset($one[$attrName])) {
  64. $one[$attrName] = unserialize($one[$attrName]);
  65. }
  66. }
  67. return $one;
  68. } else {
  69. return new $this->_modelName();
  70. }
  71. }
  72. /*
  73. * example filter:
  74. * [
  75. * 'numPerPage' => 20,
  76. * 'pageNum' => 1,
  77. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  78. 'where' => [
  79. ['>','price',1],
  80. ['<=','price',10]
  81. * ['sku' => 'uk10001'],
  82. * ],
  83. * 'asArray' => true,
  84. * ]
  85. */
  86. public function coll($filter = '')
  87. {
  88. $query = $this->_mode->find();
  89. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  90. $coll = $query->all();
  91. if (!empty($coll)) {
  92. foreach ($coll as $k => $one) {
  93. foreach ($this->_lang_attr as $attr) {
  94. $one[$attr] = $one[$attr] ? unserialize($one[$attr]) : '';
  95. }
  96. $coll[$k] = $one;
  97. }
  98. }
  99. //var_dump($one);
  100. return [
  101. 'coll' => $coll,
  102. 'count'=> $query->limit(null)->offset(null)->count(),
  103. ];
  104. }
  105. /**
  106. * @return array 得到urlKey 和 label 对应的数组。
  107. * 这样可以通过url_key得到当前操作的菜单的name
  108. */
  109. public function getUrlKeyAndLabelArr(){
  110. $arr = Yii::$app->cache->get(self::URLKEY_LABEL_ARR);
  111. if (!$arr) {
  112. $arr = [];
  113. $filter = [
  114. 'fetchAll' => true,
  115. 'asArray' => true,
  116. ];
  117. $data = $this->coll($filter);
  118. if (is_array($data['coll'])) {
  119. $tags = $this->getTags(false);
  120. foreach ($data['coll'] as $one) {
  121. $url_key = $one['url_key'];
  122. $label = $tags[$one['tag']] .' '. $one['name'];
  123. $arr[$url_key] = $label;
  124. }
  125. }
  126. $addArr = $this->getAddUrlKeyAndLabelArr();
  127. if (is_array($addArr)) {
  128. $arr = array_merge($arr, $addArr);
  129. }
  130. Yii::$app->cache->set(self::URLKEY_LABEL_ARR, $arr);
  131. }
  132. return $arr;
  133. }
  134. protected function getAddUrlKeyAndLabelArr(){
  135. return $this->addUrlKeyAndLabelArr;
  136. }
  137. /**
  138. * @return 按照tag,将资源(url_key)进行分组, 按照 tag_sort_order 进行排序
  139. * 根据传递的roleId 获取对应的url_key_id,将资源(url_key)默认选中
  140. */
  141. public function getResourcesWithGroupAndSelected($role_id = 0){
  142. $selectRoleUrlKeys = [];
  143. if ($role_id) {
  144. $filter = [
  145. 'where' => [
  146. ['role_id' => $role_id],
  147. ],
  148. 'asArray' => true,
  149. 'fetchAll' => true,
  150. ];
  151. $data = Yii::$service->admin->roleUrlKey->coll($filter);
  152. if (isset($data['coll']) && is_array($data['coll'])) {
  153. foreach ($data['coll'] as $one) {
  154. $selectRoleUrlKeys[$one['url_key_id']] = $one['url_key_id'];
  155. }
  156. }
  157. }
  158. $filter = [
  159. 'asArray' => true,
  160. 'fetchAll' => true,
  161. ];
  162. $coll = $this->coll($filter);
  163. $arr = [];
  164. if (!empty($coll['coll']) && is_array($coll['coll'])) {
  165. foreach ($coll['coll'] as $one) {
  166. $tag = $one['tag'];
  167. $id = $one['id'];
  168. $one_arr = [
  169. 'id' => $id,
  170. 'name' => $one['name'],
  171. 'tag_sort_order' => $one['tag_sort_order'],
  172. 'url_key' => $one['url_key'],
  173. ];
  174. if (isset($selectRoleUrlKeys[$id]) && $selectRoleUrlKeys[$id]) {
  175. $one_arr['selected'] = true;
  176. } else {
  177. $one_arr['selected'] = false;
  178. }
  179. $arr[$tag][] = $one_arr;
  180. }
  181. }
  182. // 按照 tag_sort_order 进行排序
  183. foreach ($arr as $k => $one) {
  184. $arr[$k] = \fec\helpers\CFunc::array_sort($one, 'tag_sort_order', 'asc', true);
  185. }
  186. //var_dump($arr);
  187. $urlKeyTags = $this->getTags();
  188. $arrSort = [];
  189. foreach ($urlKeyTags as $k => $v) {
  190. if (isset($arr[$k])) {
  191. $arrSort[$k] = $arr[$k];
  192. }
  193. }
  194. return $arrSort;
  195. }
  196. public $can_not_delete = 1;
  197. /**
  198. * @param $one|array
  199. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  200. */
  201. public function save($one)
  202. {
  203. $currentDateTime = \fec\helpers\CDate::getCurrentDateTime();
  204. $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : '';
  205. if (!($this->validateUrlKey($one))) {
  206. Yii::$service->helper->errors->add('The url key exists, you must define a unique url key');
  207. return;
  208. }
  209. if ($primaryVal) {
  210. $model = $this->_mode->findOne($primaryVal);
  211. if (!$model) {
  212. Yii::$service->helper->errors->add('Url key {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  213. return false;
  214. }
  215. if ($model->can_delete == $this->can_not_delete) {
  216. Yii::$service->helper->errors->add('resource(url key) created by system, can not edit and save');
  217. return false;
  218. }
  219. } else {
  220. $model = new $this->_modelName();
  221. $model->created_at = time();
  222. }
  223. $model->updated_at = time();
  224. foreach ($this->_lang_attr as $attrName) {
  225. if (is_array($one[$attrName]) && !empty($one[$attrName])) {
  226. $one[$attrName] = serialize($one[$attrName]);
  227. }
  228. }
  229. $primaryKey = $this->getPrimaryKey();
  230. $model = Yii::$service->helper->ar->save($model, $one);
  231. $primaryVal = $model[$primaryKey];
  232. return true;
  233. }
  234. protected function validateUrlKey($one)
  235. {
  236. $url_key = $one['url_key'];
  237. $id = $this->getPrimaryKey();
  238. $primaryVal = isset($one[$id]) ? $one[$id] : '';
  239. $where = ['url_key' => $url_key];
  240. $query = $this->_mode->find()->asArray();
  241. $query->where($where);
  242. if ($primaryVal) {
  243. $query->andWhere(['<>', $id, $primaryVal]);
  244. }
  245. $one = $query->one();
  246. if (!empty($one)) {
  247. return false;
  248. }
  249. return true;
  250. }
  251. public function remove($ids)
  252. {
  253. if (!$ids) {
  254. Yii::$service->helper->errors->add('remove id is empty');
  255. return false;
  256. }
  257. if (is_array($ids) && !empty($ids)) {
  258. foreach ($ids as $id) {
  259. $model = $this->_mode->findOne($id);
  260. if ($model->can_delete == $this->can_not_delete) {
  261. Yii::$service->helper->errors->add('resource(url key) created by system, can not remove');
  262. return false;
  263. }
  264. $model->delete();
  265. // delete roleUrlKey
  266. Yii::$service->admin->roleUrlKey->removeByUrlKeyId($id);
  267. }
  268. } else {
  269. $id = $ids;
  270. $model = $this->_mode->findOne($id);
  271. if ($model->can_delete == $this->can_not_delete) {
  272. Yii::$service->helper->errors->add('resource(url key) created by system, can not remove');
  273. return false;
  274. }
  275. $model->delete();
  276. // delete roleUrlKey
  277. Yii::$service->admin->roleUrlKey->removeByUrlKeyId($id);
  278. }
  279. return true;
  280. }
  281. }