UrlKey.php 9.3 KB

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