PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/code/apps/admin/Lib/Action/UserAction.class.php

http://thinksns-2.googlecode.com/
PHP | 556 lines | 429 code | 76 blank | 51 comment | 89 complexity | 0f3e8fd93911d6914c4a3066c3264115 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class UserAction extends AdministratorAction {
  3. /** ?? **/
  4. //????
  5. public function user() {
  6. $dao = model('User');
  7. $res = $dao->getUserList('', true, true);
  8. $this->assign($res);
  9. $this->display();
  10. }
  11. //????
  12. public function addUser() {
  13. $credit_type = X('Credit')->getCreditType();
  14. $this->assign('credit_type',$credit_type);
  15. $this->assign('type', 'add');
  16. $this->display('editUser');
  17. }
  18. public function doAddUser() {
  19. //???????
  20. $required_field = array(
  21. 'email' => 'Email',
  22. 'password' => '??',
  23. 'uname' => '??',
  24. );
  25. foreach ($required_field as $k => $v) {
  26. if ( empty($_POST[$k]) ) $this->error($v . '????');
  27. }
  28. if ( ! isValidEmail($_POST['email']) ) {
  29. $this->error('Email??????????');
  30. }
  31. if ( strlen($_POST['password']) < 6 || strlen($_POST['password']) > 16 ) {
  32. $this->error('?????6-16?');
  33. }
  34. if ( ! isEmailAvailable($_POST['email']) ) {
  35. $this->error('Email???????????');
  36. }
  37. if ( mb_strlen($_POST['uname'],'UTF8') > 10 ) {
  38. $this->error('??????10???');
  39. }
  40. //????
  41. $_POST['uname'] = escape($_POST['uname']);
  42. $_POST['password'] = md5($_POST['password']);
  43. $_POST['ctime'] = time();
  44. $_POST['is_active'] = intval($_POST['is_active']);
  45. $_POST['sex'] = intval($_POST['sex']);
  46. $uid = M('user')->add($_POST);
  47. if (!$uid) {
  48. $this->error('?????????????');
  49. exit;
  50. }
  51. //???????
  52. model('UserGroup')->addUserToUserGroup( $uid, t($_POST['user_group_id']) );
  53. $this->success('????');
  54. }
  55. //????
  56. public function editUser() {
  57. $_GET['uid'] = intval($_GET['uid']);
  58. if ($_GET['uid'] <= 0) $this->error('????');
  59. $map['uid'] = $_GET['uid'];
  60. $user = M('user')->where($map)->find();
  61. if(!$user) $this->error('????');
  62. $credit = X('Credit');
  63. $credit_type = $credit->getCreditType();
  64. $user_credit = $credit->getUserCredit($map['uid']);
  65. $this->assign($user);
  66. $this->assign('credit_type',$credit_type);
  67. $this->assign('user_credit',$user_credit);
  68. $this->assign('type', 'edit');
  69. $this->display();
  70. }
  71. public function doEditUser() {
  72. //???????
  73. $_POST['uid'] = intval($_POST['uid']);
  74. $required_field = array(
  75. 'uid' => '????',
  76. 'email' => 'Email',
  77. 'uname' => '??',
  78. );
  79. foreach ($required_field as $k => $v) {
  80. if ( empty($_POST[$k]) ) $this->error($v . '????');
  81. }
  82. if ( ! isValidEmail($_POST['email']) ) {
  83. $this->error('Email??????????');
  84. }
  85. if ( !empty($_POST['password']) && strlen($_POST['password']) < 6 || strlen($_POST['password']) > 16 ) {
  86. $this->error('?????6-16?');
  87. }
  88. if ( ! isEmailAvailable($_POST['email'], $_POST['uid']) ) {
  89. $this->error('Email???????????');
  90. }
  91. if ( mb_strlen($_POST['uname'],'UTF8') > 10 ) {
  92. $this->error('??????10???');
  93. }
  94. //????
  95. $_POST['uname'] = escape($_POST['uname']);
  96. $key = array('email','uname','sex','is_active','domain');
  97. $value = array($_POST['email'], $_POST['uname'], intval($_POST['sex']), intval($_POST['is_active']),h($_POST['domain']));
  98. if ( !empty($_POST['password']) ) {
  99. $key[] = 'password';
  100. $value[] = md5($_POST['password']);
  101. }
  102. $map['uid'] = $_POST['uid'];
  103. $res = M('user')->where($map)->setField($key, $value);
  104. //??????
  105. $credit = X('Credit');
  106. $credit_type = $credit->getCreditType();
  107. foreach($credit_type as $v){
  108. $credit_action[$v['name']] = intval($_POST[$v['name']]);
  109. }
  110. $credit->setUserCredit($map['uid'],$credit_action,'reset');
  111. //???????
  112. model('UserGroup')->addUserToUserGroup( $_POST['uid'], t($_POST['user_group_id']) );
  113. $this->assign('jumpUrl', U('admin/User/user'));
  114. $this->success('????');
  115. }
  116. //????
  117. public function doDeleteUser() {
  118. $_POST['uid'] = t($_POST['uid']);
  119. $_POST['uid'] = explode(',', $_POST['uid']);
  120. //ts_user
  121. $res = model('User')->deleteUser($_POST['uid']);
  122. if($res) {echo 1; }
  123. else {echo 0; return ;}
  124. }
  125. //????
  126. public function doSearchUser() {
  127. //??????????????????????SESSION?
  128. if ( !empty($_POST) ) {
  129. $_SESSION['admin_searchUser'] = serialize($_POST);
  130. }else if ( isset($_GET[C('VAR_PAGE')]) ) {
  131. $_POST = unserialize($_SESSION['admin_searchUser']);
  132. }else {
  133. unset($_SESSION['admin_searchUser']);
  134. }
  135. //??????
  136. $fields = array('email','uid','sex','is_active');
  137. $map = array();
  138. foreach($fields as $v)
  139. if ( isset($_POST[$v]) && $_POST[$v] != '' )
  140. $map[$v] = array('in', explode(',', $_POST[$v]));
  141. //????????
  142. if ( isset($_POST['uname']) && $_POST['uname'] != '' ) {
  143. $map['uname'] = array('exp', 'LIKE "%'.$_POST['uname'].'%"');
  144. }
  145. //??????
  146. if ( !empty($_POST['user_group_id']) ) {
  147. $uids = model('UserGroup')->getUidByUserGroup($_POST['user_group_id']);
  148. $uids = array_unique( $uids );
  149. //???????????????
  150. $uids = empty($map['uid']) && !empty($uids) ? $uids : array_intersect($uids, $map['uid'][1]);
  151. $map['uid'] = array('in', $uids);
  152. }
  153. $res = model('User')->getUserList($map, true, true);
  154. $this->assign($res);
  155. $this->assign('type', 'searchUser');
  156. $this->assign($_POST);
  157. $this->display('user');
  158. }
  159. //????
  160. public function setField() {
  161. $data['list'] = D('UserSet')->getFieldList();
  162. $this->assign( $data );
  163. $this->display();
  164. }
  165. //????
  166. public function addfield() {
  167. if( $_POST ){
  168. if( D('UserSet')->addfield() ){
  169. $this->success('????');
  170. }else{
  171. $this->error( D('UserSet')->getError() );
  172. }
  173. }else{
  174. $this->display();
  175. }
  176. }
  177. public function deleteField() {
  178. echo D('UserSet')->deleteField(intval($_POST['ids'])) ? '1' : '0';
  179. }
  180. //????
  181. public function message() {
  182. // ?????
  183. $user_group_list = model('UserGroup')->field('`user_group_id`,`title`')->findAll();
  184. $this->assign('user_group_list', $user_group_list);
  185. $this->display();
  186. }
  187. public function doSendMessage() {
  188. $_POST['user_group_id'] = intval($_POST['user_group_id']);
  189. $_POST['type'] = intval($_POST['type']);
  190. // ???
  191. if ($_POST['user_group_id'] == 0) {
  192. // ????
  193. $_POST['to'] = M('user')->where('`is_active`=1 AND `is_init`=1')->field('`uid`,`email`')->findAll();
  194. $_POST['to'] = $_POST['type'] == 1 ? getSubByKey($_POST['to'], 'email') : getSubByKey($_POST['to'], 'uid');
  195. }else {
  196. // ?????
  197. $_POST['to'] = model('UserGroup')->getUidByUserGroup($_POST['user_group_id']);
  198. if ($_POST['type'] == 1) {
  199. $map['uid'] = array('in', $_POST['to']);
  200. $_POST['to'] = M('user')->where($map)->field('email')->findAll();
  201. $_POST['to'] = getSubByKey($_POST['to'],'email');
  202. }
  203. }
  204. unset($_POST['user_group_id']);
  205. $res = false;
  206. if ( $_POST['type'] == 0 ) {
  207. // ???
  208. $res = model('Message')->postMessage($_POST, $this->mid);
  209. $res = !empty($res);
  210. }else {
  211. // Email
  212. $service = service('Mail');
  213. $_POST['title'] = t($_POST['title']);
  214. $_POST['content'] = t($_POST['content']);
  215. $_POST['to'] = array('desheng.young@qq.com');
  216. foreach($_POST['to'] as $v)
  217. $res = $res || $service->send_email($v, $_POST['title'], $_POST['content']);
  218. }
  219. if ($res)
  220. $this->success('????');
  221. else
  222. $this->error('????');
  223. }
  224. private function __sendMessage() {
  225. }
  226. //????
  227. public function level() {
  228. echo '<h2>???????</h2>';
  229. //$this->display();
  230. }
  231. /** ?? **/
  232. //?????
  233. public function userGroup() {
  234. $user_groups = model('UserGroup')->getUserGroupByMap();
  235. $this->assign('user_groups', $user_groups);
  236. $this->display();
  237. }
  238. //??or?????
  239. public function editUserGroup() {
  240. $_GET['gid'] = intval($_GET['gid']);
  241. if ($_GET['gid'] > 0) {
  242. //????????????
  243. $user_group = model('UserGroup')->getUserGroupById($_GET['gid']);
  244. $this->assign('user_group', $user_group[0]);
  245. }
  246. $this->display();
  247. }
  248. public function doAddUserGroup() {
  249. $_POST['title'] = escape($_POST['title']);
  250. if ( empty($_POST['title']) ) {
  251. echo 0;
  252. return ;
  253. }
  254. $dao = model('UserGroup');
  255. if ( $dao->isUserGroupExist($_POST['title']) ) {
  256. echo -1; // ??????
  257. }else {
  258. $res = $dao->addUserGroup($_POST['title'],$_POST['icon']);
  259. if($res) echo intval($res);
  260. else echo 0;
  261. }
  262. }
  263. public function doEditUserGroup() {
  264. $gid = intval($_POST['gid']);
  265. $dao = model('UserGroup');
  266. $data['title'] = escape($_POST['title']);
  267. $data['icon'] = escape($_POST['icon']);
  268. if ( $dao->isUserGroupExist($data['title'], $gid) ) {
  269. echo -1; // ??????
  270. }else {
  271. $res = M('user_group')->where('user_group_id='.$gid)->data($data)->save();
  272. $res = M('user_group_link')->where('user_group_id='.$gid)->setField('user_group_title', $data['title']) && $res;
  273. if($res) echo 1;
  274. else echo 0;
  275. }
  276. }
  277. //?????
  278. public function changeUserGroup() {
  279. $_GET['uids'] = explode(',', t($_GET['uids']));
  280. foreach($_GET['uids'] as $k => $v)
  281. if( ! is_numeric($v) || intval($v) <= 0 )
  282. unset($_GET['uids'][$k]);
  283. $count = count($_GET['uids']);
  284. $_GET['uids'] = implode(',', $_GET['uids']);
  285. $this->assign('uids', $_GET['uids']);
  286. $map['uid'] = array('in', $_GET['uids']);
  287. $users = model('User')->getUserList($map, false, false, 'uname', '', $count>3?3:$count);
  288. $users = implode(', ', getSubByKey($users['data'], 'uname'));
  289. $users = $count > 3 ? "$users ??{$count}?" : "$users ?{$count}?";
  290. $this->assign('unames', $users);
  291. $this->display();
  292. }
  293. public function doChangeUserGroup() {
  294. $_POST['gid'] = explode(',', t($_POST['gid']));
  295. $_POST['uid'] = explode(',', t($_POST['uid']));
  296. if ( empty($_POST['gid']) || empty($_POST['uid']) ) {
  297. echo 0;
  298. return ;
  299. }
  300. if ( model('UserGroup')->addUserToUserGroup($_POST['uid'], $_POST['gid']) ) {
  301. echo 1;
  302. }else {
  303. echo 0;
  304. }
  305. }
  306. public function doDeleteUserGroup() {
  307. $_POST['gid'] = t($_POST['gid']);
  308. //??????????
  309. if ( ! model('UserGroup')->isUserGroupEmpty($_POST['gid']) ) {
  310. echo 0;
  311. return ;
  312. }
  313. //????
  314. $res = model('UserGroup')->deleteUserGroup( $_POST['gid'] );
  315. if($res) echo 1;
  316. else echo 0;
  317. }
  318. public function isUserGroupExist() {
  319. $res = model('UserGroup')->isUserGroupExist( $_POST['title'], intval($_POST['gid']));
  320. if($res) echo 1;
  321. else echo 0;
  322. }
  323. public function isUserGroupEmpty() {
  324. $res = model('UserGroup')->isUserGroupEmpty( $_POST['gid'] );
  325. if($res) echo 1;
  326. else echo 0;
  327. }
  328. public function node() {
  329. $node = D('Node')->getAllNode();
  330. $this->assign($node);
  331. $this->display();
  332. }
  333. public function addNode() {
  334. $this->assign('type', 'add');
  335. $this->display('editNode');
  336. }
  337. public function doAddNode($old_nid = 0) {
  338. //module?*??action???
  339. $_POST['act_name'] = $_POST['mod_name'] == '*' ? $_POST['mod_name'] : $_POST['act_name'];
  340. if (!$this->__isValidRequest('app_name,mod_name,act_name'))
  341. $this->error('?????');
  342. //action?*??subAction???
  343. $_POST['subAction'] = ($_POST['act_name'] == '*') ? array() : $_POST['subAction'];
  344. foreach($_POST['subAction'] as $k => $v) {
  345. if (empty($v)) unset($_POST['subAction'][$k]);
  346. if ($v == '*') $this->error('?????????????“*”??????????“*”');
  347. }
  348. $_POST['parent_node_id'] = 0;
  349. unset($_POST['node_id']);
  350. $res = D('Node')->add($_POST);
  351. $nid = $res;
  352. //??????
  353. if ( !empty($_POST['subAction']) ) {
  354. $prefix = C('DB_PREFIX');
  355. $sql = "INSERT INTO `{$prefix}node` (`app_name`,`app_alias`,`mod_name`,`mod_alias`,`act_name`,`act_alias`,`description`,`parent_node_id`) VALUES";
  356. foreach ($_POST['subAction'] as $v) {
  357. $sql .= " ('{$_POST['app_name']}','{$_POST['app_alias']}','{$_POST['mod_name']}','{$_POST['mod_alias']}','{$v}','{$_POST['act_alias']}_????','{$_POST['description']}','{$nid}'),";
  358. }
  359. $sql = rtrim($sql, ',');
  360. $res = $nid && M('')->execute($sql);
  361. }
  362. //?????????
  363. if ($res && $old_nid) {
  364. D('Node')->where("`node_id`=$old_nid OR `parent_node_id`=$old_nid")->delete();
  365. //?????
  366. M('user_group_popedom')->where("`node_id`=$old_nid")->setField('node_id', $nid);
  367. }
  368. if ($res) {
  369. //????????????
  370. $old_nid && $this->assign('jumpUrl', U('admin/User/node'));
  371. $this->success('????');
  372. }else {
  373. $this->error('????');
  374. }
  375. }
  376. public function editNode() {
  377. $nid = intval($_GET['nid']);
  378. $node = D('Node')->getNodeDetailById($nid);
  379. if (!$node) $this->error('??????');
  380. $this->assign($node);
  381. $this->assign('type', 'edit');
  382. $this->display();
  383. }
  384. public function doEditNode() {
  385. //???????????
  386. $this->doAddNode( intval($_POST['node_id']) );
  387. exit;
  388. }
  389. public function doDeleteNode() {
  390. $_POST['ids'] = t($_POST['ids']);
  391. //??????????
  392. if ( ! D('Node')->isNodeEmpty($_POST['ids']) ) {
  393. echo 0;
  394. return ;
  395. }
  396. //????
  397. $res = D('Node')->deleteNode( $_POST['ids'] );
  398. if($res) echo 1;
  399. else echo 0;
  400. }
  401. public function popedom() {
  402. //?????
  403. $node = D('Node')->getNodeByMap('`parent_node_id`=0', 'app_name ASC, mod_name ASC, act_name ASC, node_id ASC');
  404. //?????????????
  405. $nids = getSubByKey($node['data'], 'node_id');
  406. $prefix = C('DB_PREFIX');
  407. $where = 'p.node_id IN ( ' . implode(',', $nids) . ' )';
  408. $sql = "SELECT p.node_id,g.title FROM {$prefix}user_group_popedom AS p INNER JOIN {$prefix}user_group AS g ON p.user_group_id = g.user_group_id WHERE $where";
  409. $res = M('')->query($sql);
  410. $node_usergroup = array();
  411. foreach ($res as $v) {
  412. $node_usergroup[$v['node_id']][] = $v['title'];
  413. }
  414. $this->assign($node);
  415. $this->assign('node_usergroup', $node_usergroup);
  416. $this->display();
  417. }
  418. public function setPopedom() {
  419. $_GET['nids'] = t($_GET['nids']);
  420. $_GET['nids'] = explode(',', $_GET['nids']);
  421. foreach ($_GET['nids'] as $k => $v) {
  422. if ( !is_numeric($v) )
  423. unset($_GET['nids'][$k]);
  424. }
  425. $count = count($_GET['nids']);
  426. $this->assign('nids', implode(',', $_GET['nids']));
  427. $this->assign('count', $count);
  428. if ($count == 1) {
  429. $map['node_id'] = array('in', $_GET['nids']);
  430. $user_group = M('user_group_popedom')->where($map)->findAll();
  431. $user_group = getSubByKey($user_group, 'user_group_id');
  432. $this->assign('user_group', $user_group);
  433. }
  434. $this->display();
  435. }
  436. public function doSetPopedom() {
  437. $_POST['gid'] = explode(',', $_POST['gid']);
  438. $_POST['nid'] = explode(',', $_POST['nid']);
  439. foreach ($_POST['gid'] as $k => $v)
  440. if ( !is_numeric($v) || intval($v) <= 0 )
  441. unset($_POST['gid'][$k]);
  442. if (empty($_POST['gid'])) {echo 0; return ;}
  443. foreach ($_POST['nid'] as $k => $v)
  444. if ( !is_numeric($v) || intval($v) <= 0 )
  445. unset($_POST['nid'][$k]);
  446. if (empty($_POST['nid'])) {echo 0; return ;}
  447. //?????????ID
  448. $map['parent_node_id'] = array('in', $_POST['nid']);
  449. $nids = D('Node')->where($map)->field('node_id')->findAll();
  450. $nids = getSubByKey($nids, 'node_id');
  451. $nids = array_merge($nids, $_POST['nid']);
  452. if (empty($nids)) {echo 0; return ;}
  453. //?????
  454. M('user_group_popedom')->where('`node_id` IN ( '.implode(',', $nids).' )')->delete();
  455. //????SQL
  456. $sql = "INSERT INTO `" . C('DB_PREFIX') . "user_group_popedom` (`user_group_id`,`node_id`) VALUES ";
  457. foreach($nids as $nid) {
  458. foreach($_POST['gid'] as $gid) {
  459. $sql .= "('$gid', '$nid'),";
  460. }
  461. }
  462. $sql = rtrim($sql, ',');
  463. $res = M('')->execute($sql);
  464. if ($res) {
  465. echo 1;
  466. }else {
  467. echo 0;
  468. }
  469. }
  470. private function __isValidRequest($field, $array = 'post') {
  471. $field = is_array($field) ? $field : explode(',', $field);
  472. $array = $array == 'post' ? $_POST : $_GET;
  473. foreach ($field as $v){
  474. $v = trim($v);
  475. if ( !isset($array[$v]) || $array[$v] == '' ) return false;
  476. }
  477. return true;
  478. }
  479. }
  480. ?>