PluginProbe
ShiftController Employee Shift Scheduling / 2.1.2
ShiftController Employee Shift Scheduling v2.1.2
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / application / modules / wordpress / controllers / admin / users.php

users.php in ShiftController Employee Shift Scheduling 2.1.2, at application/modules/wordpress/controllers/admin/users.php

224 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Wordpress_Users_controller extends Backend_controller
4 {
5 function edit( $id )
6 {
7 // redirect to WP admin user edit
8 $link = get_edit_user_link( $id );
9 $this->redirect( $link );
10 exit;
11 }
12
13 function add()
14 {
15 // redirect to WP admin user add
16 $link = admin_url( 'user-new.php' );
17 $this->redirect( $link );
18 exit;
19 }
20
21 function sync()
22 {
23 $wum = new Wordpress_User_Model;
24 $wordpress_roles = $wum->wp_roles();
25 $this->data['wordpress_roles'] = $wordpress_roles;
26 $this->data['wordpress_count_users'] = count_users();
27
28 foreach( $wordpress_roles as $role_value => $role_name )
29 {
30 $field_name = 'role_' . $role_value;
31 $default = $this->app_conf->get( 'wordpress_' . $field_name );
32 $defaults[ $field_name ] = $default;
33 }
34 $this->hc_form->set_defaults( $defaults );
35
36 $this->set_include( 'sync', 'wordpress/admin/users' );
37 $this->load->view( $this->template, $this->data);
38 }
39
40 function syncrun()
41 {
42 $fields = array();
43 $validation = array();
44
45 $wum = new Wordpress_User_Model;
46 $wordpress_roles = $wum->wp_roles();
47 foreach( $wordpress_roles as $role_value => $role_name )
48 {
49 $field_name = 'role_' . $role_value;
50
51 $fields[] = $field_name;
52 $validation[] = array(
53 'field' => $field_name,
54 'label' => $role_name,
55 'rules' => 'trim|required'
56 );
57 }
58 $this->form_validation->set_rules( $validation );
59
60 if( $this->input->post() )
61 {
62 $post = array();
63 reset( $fields );
64 foreach( $fields as $f )
65 {
66 $post[$f] = $this->input->post($f);
67 }
68 $this->hc_form->set_defaults( $post );
69
70 if( $this->form_validation->run() == FALSE )
71 {
72 $errors = array();
73 reset( $fields );
74 foreach( $fields as $f )
75 {
76 $errors[$f] = form_error($f);
77 }
78 $this->hc_form->set_errors( $errors );
79 }
80 else
81 {
82 /* save settings */
83 reset( $post );
84 foreach( $post as $k => $v )
85 {
86 $this->app_conf->set( 'wordpress_' . $k, $v );
87 }
88
89 $setup_ok = TRUE;
90
91 /* users */
92 $count_users = 0;
93 $processed_users = array();
94
95 /* this user */
96 $current_user = wp_get_current_user();
97 $user = new User_Model;
98 $user->get_by_id( $current_user->ID );
99
100 $user->email = $current_user->user_email;
101 if( $current_user->user_firstname )
102 {
103 $user->first_name = $current_user->user_firstname;
104 $user->last_name = $current_user->user_lastname;
105 }
106 else
107 {
108 $user->first_name = $current_user->display_name;
109 }
110 $user->level = USER_MODEL::LEVEL_ADMIN;
111
112 if( $user->save() )
113 {
114 $processed_users[] = $user->id;
115 $count_users++;
116 }
117 else
118 {
119 $setup_ok = FALSE;
120
121 $err_msg = array();
122 $err_msg[] = $current_user->user_email;
123 $err_msg = array_merge($err_msg, array_values($user->error->all) );
124 $this->session->set_flashdata( 'error', $err_msg );
125
126 ci_redirect( 'admin/users' );
127 return;
128 }
129
130 if( $setup_ok )
131 {
132 /* now by roles */
133 reset( $wordpress_roles );
134 foreach( $wordpress_roles as $role_value => $role_name )
135 {
136 $our_level = $post['role_' . $role_value];
137 if( ! $our_level )
138 continue;
139
140 $args = array(
141 'role' => $role_value,
142 'exclude' => $current_user->ID
143 );
144 $wordpress_users = get_users( $args );
145 foreach( $wordpress_users as $wuser )
146 {
147 if( (! $role_value) && $wuser->roles )
148 {
149 continue;
150 }
151
152 $user = new User_Model;
153 $user->get_by_id( $wuser->ID );
154 $is_new = $user->exists() ? FALSE : TRUE;
155
156 $user->email = $wuser->user_email;
157 $user->id = $wuser->ID;
158
159 if( $wuser->user_firstname )
160 {
161 $user->first_name = $wuser->user_firstname;
162 $user->last_name = $wuser->user_lastname;
163 }
164 else
165 {
166 $user->first_name = $wuser->display_name;
167 }
168 if( $is_new )
169 $user->password = hc_random();
170 $user->level = $our_level;
171
172 if(
173 ( $is_new && $user->save_as_new() )
174 OR
175 ( (! $is_new) && $user->save() )
176 )
177 {
178 $processed_users[] = $user->id;
179 $count_users++;
180 }
181 else
182 {
183 $setup_ok = FALSE;
184
185 $err_msg = array();
186 $err_msg[] = $wuser->user_email;
187 $err_msg = array_merge($err_msg, array_values($user->error->all) );
188 $this->session->set_flashdata( 'error', $err_msg );
189 ci_redirect( 'admin/users' );
190 return;
191 }
192 }
193 }
194
195 /* those that are deleted in WordPress make archived */
196 $um = new User_Model;
197 $um->where_not_in( 'id', $processed_users );
198 $um->update( 'active', USER_MODEL::STATUS_ARCHIVE );
199 $archived_count = $um->db->affected_rows();
200 }
201
202 if( $setup_ok )
203 {
204 $msg = 'Synchronized ' . $count_users . ' ';
205 $msg .= ($count_users > 1) ? 'users' : 'user';
206
207 if( $archived_count )
208 {
209 $msg .= '<br>Archived ' . $archived_count . ' ';
210 $msg .= ($archived_count > 1) ? 'users' : 'user';
211 }
212
213 $this->session->set_flashdata( 'message', $msg );
214 ci_redirect( 'admin/users' );
215 return;
216 }
217 }
218 }
219 return $this->sync();
220 }
221 }
222
223 /* End of file customers.php */
224 /* Location: ./application/controllers/admin/categories.php */