fa978a0c by Marty Penner

Refactored merge users functionality to search in more places; changed output HTML to tables

1 parent df223fbc
...@@ -967,10 +967,10 @@ class Actions { ...@@ -967,10 +967,10 @@ class Actions {
967 } 967 }
968 968
969 if ($do_search == 'yes') { 969 if ($do_search == 'yes') {
970 $username = $_POST['search_user_' . $direction]; 970 $username = $wpdb->escape($_POST['search_user_' . $direction]);
971 971
972 // Don't search for an empty string as it will return all results 972 // Don't search for an empty string as it will return all results
973 if (empty($_POST['search_user_' . $direction])) { 973 if (empty($username)) {
974 die(json_encode(array( 974 die(json_encode(array(
975 'success' => 'false', 975 'success' => 'false',
976 'msg' => 'You must enter a value to search for.' 976 'msg' => 'You must enter a value to search for.'
...@@ -978,34 +978,43 @@ class Actions { ...@@ -978,34 +978,43 @@ class Actions {
978 } 978 }
979 979
980 // Initialize 980 // Initialize
981 $html = '<div><ul class="user-list"><h4>Username</h4><h4>Member ID</h4><h4>Email</h4>'; 981 $html = '<table><thead><tr><th>First Name</th><th>Last Name</th><th>Member ID</th><th>Email</th></tr></thead><tbody>';
982 $i = 0; 982 $i = 0;
983 983
984 $query = "SELECT ID, user_login, user_email FROM $wpdb->users WHERE user_login LIKE '" . $wpdb->escape($username) . "%'"; 984 // Check for search term in first_name, last_name, user_login, and user_email, but only if user status is not 'terminated'
985 $query = "
986 SELECT ID, user_email
987 FROM $wpdb->users
988 WHERE
989 (user_login LIKE '%$username%')
990 OR (user_email LIKE '%$username%')
991 OR (ID IN (
992 SELECT user_id
993 FROM $wpdb->usermeta
994 WHERE (meta_key = 'first_name' OR meta_key = 'last_name')
995 AND meta_value LIKE '%$username%'
996 ))
997 AND (ID NOT IN (
998 SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'status' AND meta_value = 'terminated'
999 ))
1000 ORDER BY user_email";
985 $result = $wpdb->get_results($query, ARRAY_A); 1001 $result = $wpdb->get_results($query, ARRAY_A);
986 1002
987 if (! empty($result)) { 1003 if (! empty($result)) {
988 foreach ($result as $user) { 1004 foreach ($result as $user) {
989 // Check if user has status of 'terminated' 1005 // Get the member ID as well
990 $user_status = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key LIKE 'status'", $user['ID'])); 1006 $member_id = get_user_meta($user['ID'], 'member_id', TRUE);
991 if ($user_status == 'terminated') { 1007 $first_name = get_user_meta($user['ID'], 'first_name', TRUE);
992 continue; 1008 $last_name = get_user_meta($user['ID'], 'last_name', TRUE);
993 }
994
995 // Get the member ID as well, but don't display it if it's the same as 'user_login'
996 $member_id = get_user_meta($user['ID'], 'member_id', TRUE);
997 if ($member_id == $user['user_login']) {
998 $member_id = '""';
999 }
1000 1009
1001 $html .= <<<HTML 1010 $html .= <<<HTML
1002 1011
1003 <li> 1012 <tr>
1004 <input type="radio" name="merge_user_{$direction}" value="{$user['user_login']}" /> 1013 <td><input type="radio" name="merge_user_{$direction}" value="{$user['ID']}" />$first_name</td>
1005 <label for="merge_user_{$direction}">{$user['user_login']}</label> 1014 <td>$last_name</td>
1006 <label for="merge_user_{$direction}">{$member_id}</label> 1015 <td>$member_id</td>
1007 <label for="merge_user_{$direction}">{$user['user_email']}</label> 1016 <td>{$user['user_email']}</td>
1008 </li> 1017 </tr>
1009 HTML; 1018 HTML;
1010 1019
1011 $i++; 1020 $i++;
...@@ -1013,10 +1022,10 @@ HTML; ...@@ -1013,10 +1022,10 @@ HTML;
1013 } else { 1022 } else {
1014 die(json_encode(array( 1023 die(json_encode(array(
1015 'success' => 'false', 1024 'success' => 'false',
1016 'msg' => 'No users found matching that criteria.' 1025 'msg' => 'No users found matching that term.'
1017 ))); 1026 )));
1018 } 1027 }
1019 $html .= '</ul></div>'; 1028 $html .= '</tbody></table>';
1020 1029
1021 $return = array( 1030 $return = array(
1022 'success' => 'true', 1031 'success' => 'true',
...@@ -1040,12 +1049,45 @@ HTML; ...@@ -1040,12 +1049,45 @@ HTML;
1040 $html = ''; 1049 $html = '';
1041 $from_user = new User\Account($_POST['merge_user_from']); 1050 $from_user = new User\Account($_POST['merge_user_from']);
1042 $to_user = new User\Account($_POST['merge_user_to']); 1051 $to_user = new User\Account($_POST['merge_user_to']);
1052
1053 // These fields are skipped ONLY IN DISPLAY
1043 $skipped_fields = array( 1054 $skipped_fields = array(
1044 'ID', 1055 'ID',
1045 'id', 1056 'id',
1046 'user_login', 1057 'user_login',
1047 'user_email', 1058 'user_email',
1048 'member_id' 1059 'member_id',
1060 'caps',
1061 'cap_key',
1062 'roles',
1063 'allcaps',
1064 'filter',
1065 'user_pass',
1066 'user_activation_key',
1067 'status',
1068 'four_security_expiration',
1069 'course_4q_approvals',
1070 'user_level',
1071 'validated',
1072 'posts_per_page',
1073 'overload_ppp',
1074 'rich_editing',
1075 'comment_shortcuts',
1076 'admin_color',
1077 'use_ssl',
1078 'wp_capabilities',
1079 'wp_user_level',
1080 'x_cicbv.person',
1081 'tz_profile_pub',
1082 'events',
1083 'four_security_questions',
1084 'special_statuses',
1085 'mainmenu',
1086 'cehours',
1087 'subscription',
1088 'notices',
1089 'invoices',
1090 'courses_registered',
1049 ); 1091 );
1050 // Get rid of the unnecessary data object in the current user object 1092 // Get rid of the unnecessary data object in the current user object
1051 unset($from_user->data); 1093 unset($from_user->data);
...@@ -1063,26 +1105,8 @@ HTML; ...@@ -1063,26 +1105,8 @@ HTML;
1063 // Make sure we're updating the right user ID and perform update 1105 // Make sure we're updating the right user ID and perform update
1064 update_user_meta($to_user->ID, $key, $val); 1106 update_user_meta($to_user->ID, $key, $val);
1065 1107
1066 // Build a set of nested html lists that display the fields (goes 3 levels deep; bloody ugly, but works) 1108 // Build a table of data
1067 $html_l2 = ''; 1109 $html .= "\n\t<tr><td>$key: $val</td></tr>";
1068 if (is_array($val)) {
1069 // Rinse and repeat
1070 foreach ($val as $subk => $subv) {
1071 $html_l3 = '';
1072 if (is_array($subv)) {
1073 // And yet again...
1074 foreach ($subv as $k3 => $v3) {
1075 $html_l3 .= "<li>$k3 = $v3</li>";
1076 }
1077 $html_l2 .= "<li><ul>$subk: [$html_l3]</ul></li>";
1078 } else {
1079 $html_l2 .= "<li>$subk = $subv</li>";
1080 }
1081 }
1082 $html .= "<li><ul>$key: [$html_l2]</ul></li>";
1083 } else {
1084 $html .= "<li>$key: $val</li>";
1085 }
1086 } 1110 }
1087 1111
1088 // Set a few meta values for the old user 1112 // Set a few meta values for the old user
...@@ -1090,9 +1114,9 @@ HTML; ...@@ -1090,9 +1114,9 @@ HTML;
1090 update_user_meta($from_user->ID, 'merged_to', $to_user->ID); 1114 update_user_meta($from_user->ID, 'merged_to', $to_user->ID);
1091 update_user_meta($from_user->ID, 'datetime_merged', time()); 1115 update_user_meta($from_user->ID, 'datetime_merged', time());
1092 1116
1093 $html = '<h6>Changed:</h6><ul>' . 1117 $html = '<thead><tr><th>Changed:</th></tr></thead><tbody>' .
1094 $html . 1118 $html .
1095 '</ul>'; 1119 '</tbody>';
1096 1120
1097 $return = array( 1121 $return = array(
1098 'success' => 'true', 1122 'success' => 'true',
......
...@@ -30,64 +30,10 @@ if (! isset($_POST['search_user_to'])) { ...@@ -30,64 +30,10 @@ if (! isset($_POST['search_user_to'])) {
30 padding: 10px; 30 padding: 10px;
31 } 31 }
32 32
33 h4 { 33 #field-list {
34 float: left;
35 padding-right: 1em;
36 }
37
38 .merge-users {
39 float: left;
40 padding-right: 2em;
41 }
42
43 .merge-users label {
44 float: left;
45 font-weight: bold;
46 padding-right: 1em;
47 }
48
49 .merge-users input {
50 float: left;
51 }
52
53 .clear {
54 float: left;
55 clear: both;
56 }
57
58 .user-list, .user-list li {
59 float: left;
60 clear: both;
61 }
62
63 .changed {
64 float: left;
65 width: 250px;
66 margin-top: 5px;
67 border: 1px solid green;
68 clear: both;
69 display: none; 34 display: none;
70 } 35 }
71 36
72 .changed h6 {
73 padding: 0px;
74 margin: 0px 0px 10px 0px;
75 font-size: 11px;
76 text-transform: uppercase;
77 }
78
79 .changed ul {
80 margin: 0;
81 padding: 0;
82 }
83
84 .changed ul li {
85 margin: 0px 0px 3px 0px;
86 padding: 0px;
87 font-size: 11px;
88 background: none;
89 }
90
91 </style> 37 </style>
92 38
93 <div class="wrap"> 39 <div class="wrap">
...@@ -119,9 +65,9 @@ h4 { ...@@ -119,9 +65,9 @@ h4 {
119 <div id="user-list-to"></div> 65 <div id="user-list-to"></div>
120 </div> 66 </div>
121 67
122 <div id="field-list" class="changed"> 68 <div id="field-list">
123 <h6>Success!</h6> 69 <h6>Success!</h6>
124 <ul></ul> 70 <table></table>
125 </div> 71 </div>
126 72
127 <div style="clear:both;"></div> 73 <div style="clear:both;"></div>
...@@ -190,7 +136,7 @@ $(function() { ...@@ -190,7 +136,7 @@ $(function() {
190 } else { 136 } else {
191 $('#user-list-from').html(""); 137 $('#user-list-from').html("");
192 $('#user-list-to').html(""); 138 $('#user-list-to').html("");
193 $('ul', $field_list).html(data.fields); 139 $('table', $field_list).html(data.fields);
194 $field_list.show(); 140 $field_list.show();
195 } 141 }
196 } 142 }
......