← All changes
|
application/modules/wordpress/controllers/setup.php
+168
-151
3.2.4
→
2.2.3
View file →
| @@ -1,130 +1,59 @@ | ||
| 1 | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | 2 | include_once( NTS_SYSTEM_APPPATH . 'controllers/setup.php' ); |
| 3 | 3 | |
| 4 | -class Setup_Wordpress_HC_Controller extends Setup_HC_Controller | |
| 4 | +class Wordpress_setup_controller extends Setup_controller | |
| 5 | 5 | { |
| 6 | - var $form = NULL; | |
| 7 | - | |
| 8 | - function __construct() | |
| 9 | - { | |
| 10 | - parent::__construct(); | |
| 11 | - $this->form = HC_Lib::form() | |
| 12 | - ; | |
| 13 | - | |
| 14 | - $wum = HC_App::model('wordpress_user'); | |
| 15 | - $wordpress_roles = $wum->wp_roles(); | |
| 16 | - | |
| 17 | - foreach( $wordpress_roles as $role_value => $role_name ){ | |
| 18 | - $field_name = 'role_' . $role_value; | |
| 19 | - $field_name = str_replace(' ', '_', $field_name ); | |
| 20 | - $this->form | |
| 21 | - ->set_input( $field_name, 'dropdown' ) | |
| 22 | - ; | |
| 23 | - } | |
| 24 | - | |
| 25 | - $this->form | |
| 26 | - ->set_input( 'append_role_name', 'checkbox' ) | |
| 27 | - ; | |
| 28 | - } | |
| 29 | - | |
| 30 | - private function _get_old_version() | |
| 31 | - { | |
| 32 | - $return = NULL; | |
| 33 | - | |
| 34 | - $dbprefix_version = $this->config->item('nts_dbprefix_version'); | |
| 35 | - if( strlen($dbprefix_version) ){ | |
| 36 | - $old_prefixes = array(); | |
| 37 | - $db = clone $this->db; | |
| 38 | - | |
| 39 | - $core_prefix = substr($db->dbprefix, 0, -(strlen($dbprefix_version)+1)); | |
| 40 | - | |
| 41 | - $my_version = substr($dbprefix_version, 1); | |
| 42 | - $old_version = $my_version - 1; | |
| 43 | - while( $old_version >= 1 ){ | |
| 44 | - $old_prefixes[] = 'v' . $old_version; | |
| 45 | - $old_version--; | |
| 46 | - } | |
| 47 | - $old_prefixes[] = ''; | |
| 48 | - | |
| 49 | - foreach( $old_prefixes as $op ){ | |
| 50 | - $test_prefix = strlen($op) ? $core_prefix . $op . '_' : $core_prefix; | |
| 51 | - $db->dbprefix = $test_prefix; | |
| 52 | - if( $this->check_setup($db) ){ | |
| 53 | - $return = $op; | |
| 54 | - break; | |
| 55 | - } | |
| 56 | - } | |
| 57 | - } | |
| 58 | - return $return; | |
| 59 | - } | |
| 60 | - | |
| 61 | 6 | function index() |
| 62 | 7 | { |
| 63 | - $app_title = $this->config->item('nts_app_title'); | |
| 64 | - | |
| 65 | - /* check if we have an older version installed */ | |
| 66 | - $offer_upgrade = 0; | |
| 67 | - $old_version = $this->_get_old_version(); | |
| 68 | - if( $old_version !== NULL ){ | |
| 69 | - $offer_upgrade = 1; | |
| 70 | - } | |
| 71 | - | |
| 72 | - if( $offer_upgrade ){ | |
| 73 | - $this->redirect('setup/upgrade'); | |
| 74 | - return; | |
| 75 | - } | |
| 76 | - | |
| 77 | - $wum = HC_App::model('wordpress_user'); | |
| 8 | + $wum = new Wordpress_User_Model; | |
| 78 | 9 | $wordpress_roles = $wum->wp_roles(); |
| 79 | - $wordpress_count_users = count_users(); | |
| 80 | - $is_setup = 1; | |
| 81 | - | |
| 82 | - $this->layout->set_partial( | |
| 83 | - 'content', | |
| 84 | - $this->render( | |
| 85 | - 'wordpress/admin/users/sync', | |
| 86 | - array( | |
| 87 | - 'post_to' => 'wordpress/setup/run', | |
| 88 | - 'app_title' => $app_title, | |
| 89 | - 'form' => $this->form, | |
| 90 | - 'wordpress_roles' => $wordpress_roles, | |
| 91 | - 'wordpress_count_users' => $wordpress_count_users, | |
| 92 | - 'offer_upgrade' => $offer_upgrade, | |
| 93 | - 'is_setup' => $is_setup, | |
| 94 | - ) | |
| 95 | - ) | |
| 96 | - ); | |
| 97 | - $this->layout(); | |
| 10 | + $this->data['wordpress_roles'] = $wordpress_roles; | |
| 11 | + $this->data['wordpress_count_users'] = count_users(); | |
| 12 | + return parent::index(); | |
| 98 | 13 | } |
| 99 | 14 | |
| 100 | 15 | function run() |
| 101 | 16 | { |
| 102 | - $validator = new HC_Validator; | |
| 103 | - $wum = HC_App::model('wordpress_user'); | |
| 17 | + $fields = array(); | |
| 18 | + $validation = array(); | |
| 19 | + | |
| 20 | + $wum = new Wordpress_User_Model; | |
| 104 | 21 | $wordpress_roles = $wum->wp_roles(); |
| 105 | - $wordpress_count_users = count_users(); | |
| 106 | - | |
| 107 | - foreach( $wordpress_roles as $role_value => $role_name ){ | |
| 22 | + foreach( $wordpress_roles as $role_value => $role_name ) | |
| 23 | + { | |
| 108 | 24 | $field_name = 'role_' . $role_value; |
| 109 | - $validator->set_rules( $field_name, 'trim|required' ); | |
| 25 | + $fields[] = $field_name; | |
| 26 | + $validation[] = array( | |
| 27 | + 'field' => $field_name, | |
| 28 | + 'label' => $role_name, | |
| 29 | + 'rules' => 'trim|required' | |
| 30 | + ); | |
| 110 | 31 | } |
| 32 | + $this->form_validation->set_rules( $validation ); | |
| 111 | 33 | |
| 112 | - $post = $this->input->post(); | |
| 113 | - $this->form->grab( $post ); | |
| 114 | - $values = $this->form->values(); | |
| 34 | + if( $this->input->post() ) | |
| 35 | + { | |
| 36 | + $post = array(); | |
| 37 | + reset( $fields ); | |
| 38 | + foreach( $fields as $f ) | |
| 39 | + { | |
| 40 | + $post[$f] = $this->input->post($f); | |
| 41 | + } | |
| 42 | + $this->hc_form->set_defaults( $post ); | |
| 115 | 43 | |
| 116 | - if( $post ){ | |
| 117 | - if( $validator->run($values) == FALSE ){ | |
| 118 | - $errors = $validator->error(); | |
| 119 | - | |
| 120 | - $this->form->set_values( $values ); | |
| 121 | - $this->form->set_errors( $errors ); | |
| 122 | - | |
| 123 | - /* render view */ | |
| 124 | - return $this->index(); | |
| 44 | + if( $this->form_validation->run() == FALSE ) | |
| 45 | + { | |
| 46 | + $errors = array(); | |
| 47 | + reset( $fields ); | |
| 48 | + foreach( $fields as $f ) | |
| 49 | + { | |
| 50 | + $errors[$f] = form_error($f); | |
| 51 | + } | |
| 52 | + $this->hc_form->set_errors( $errors ); | |
| 125 | 53 | } |
| 126 | - else { | |
| 54 | + else | |
| 55 | + { | |
| 127 | 56 | /* run setup */ |
| 128 | 57 | /* reset tables */ |
| 129 | 58 | $this->_drop_tables(); |
| 130 | 59 | |
| @@ -129,80 +58,168 @@ | ||
| 129 | 58 | $this->_drop_tables(); |
| 130 | 59 | |
| 131 | 60 | /* setup tables */ |
| 132 | 61 | $this->load->library('migration'); |
| 133 | - if ( ! $this->migration->current()){ | |
| 62 | + if ( ! $this->migration->current()) | |
| 63 | + { | |
| 134 | 64 | show_error($this->migration->error_string()); |
| 135 | 65 | return false; |
| 136 | 66 | } |
| 67 | + $this->load->library( 'conf/app_conf' ); | |
| 137 | 68 | |
| 138 | - $app_conf = HC_App::app_conf(); | |
| 139 | - $app_conf->init(); | |
| 140 | 69 | $setup_ok = TRUE; |
| 141 | 70 | |
| 142 | - $append_role_name = $values['append_role_name']; | |
| 71 | + /* save settings */ | |
| 72 | + reset( $post ); | |
| 73 | + foreach( $post as $k => $v ) | |
| 74 | + { | |
| 75 | + $this->app_conf->set( 'wordpress_' . $k, $v ); | |
| 76 | + } | |
| 143 | 77 | |
| 144 | - /* save settings */ | |
| 145 | - reset( $values ); | |
| 146 | - foreach( $values as $k => $v ){ | |
| 147 | - $app_conf->set( 'wordpress_' . $k, $v ); | |
| 78 | + /* users */ | |
| 79 | + $append_role_name = $this->input->post('append_role_name'); | |
| 80 | + $count_users = 0; | |
| 81 | + | |
| 82 | + /* this user */ | |
| 83 | + $current_user = wp_get_current_user(); | |
| 84 | + $user = new User_Model; | |
| 85 | + $user->email = $current_user->user_email; | |
| 86 | + $user->id = $current_user->ID; | |
| 87 | + | |
| 88 | + if( $current_user->user_firstname ) | |
| 89 | + { | |
| 90 | + $user->first_name = $current_user->user_firstname; | |
| 91 | + $user->last_name = $current_user->user_lastname; | |
| 148 | 92 | } |
| 93 | + else | |
| 94 | + { | |
| 95 | + $user->first_name = $current_user->display_name; | |
| 96 | + } | |
| 97 | + if( $append_role_name ) | |
| 98 | + { | |
| 99 | + $wp_role = ( $current_user->roles && is_array($current_user->roles) && isset($current_user->roles[0]) ) ? $current_user->roles[0] : ''; | |
| 100 | + if( strlen($wp_role) ) | |
| 101 | + $user->first_name = '[' . $wp_role . '] ' . $user->first_name; | |
| 102 | + } | |
| 149 | 103 | |
| 150 | - $um = HC_App::model('user'); | |
| 104 | + $user->password = hc_random(); | |
| 105 | + $user->level = USER_MODEL::LEVEL_ADMIN; | |
| 151 | 106 | |
| 152 | - /* all users */ | |
| 153 | - $result = $wum->sync_all( $values ); | |
| 154 | - if( $result !== TRUE ){ | |
| 155 | - $msg = array(); | |
| 156 | - foreach( $result as $uid => $user_result ){ | |
| 157 | - foreach( $user_result as $k => $v ){ | |
| 158 | - $msg[] = 'User ID=' . $uid . ': ' . $k . ': ' . $v; | |
| 159 | - } | |
| 160 | - } | |
| 161 | - $msg = join( '<br>', $msg ); | |
| 162 | - $this->session->set_flashdata( 'error', $msg ); | |
| 107 | + | |
| 108 | + if( $user->save_as_new() ) | |
| 109 | + { | |
| 110 | + $count_users++; | |
| 111 | + } | |
| 112 | + else | |
| 113 | + { | |
| 163 | 114 | $setup_ok = FALSE; |
| 115 | + $this->_drop_tables(); | |
| 116 | + | |
| 117 | + $err_msg = array(); | |
| 118 | + $err_msg[] = $current_user->user_email; | |
| 119 | + $err_msg = array_merge($err_msg, array_values($user->error->all) ); | |
| 120 | + $this->session->set_flashdata( 'error', $err_msg ); | |
| 121 | + | |
| 122 | + ci_redirect( 'wordpress/setup' ); | |
| 123 | + return; | |
| 164 | 124 | } |
| 165 | 125 | |
| 166 | - $success_count = $wum->get_last_count('success'); | |
| 126 | + if( $setup_ok ) | |
| 127 | + { | |
| 128 | + /* now by roles */ | |
| 129 | + reset( $wordpress_roles ); | |
| 130 | + foreach( $wordpress_roles as $role_value => $role_name ) | |
| 131 | + { | |
| 132 | + $our_level = $post['role_' . $role_value]; | |
| 133 | + if( ! $our_level ) | |
| 134 | + continue; | |
| 167 | 135 | |
| 168 | - /* this user */ | |
| 169 | - $current_user = wp_get_current_user(); | |
| 170 | - $result = $wum->sync( $current_user->ID, $um->_const('LEVEL_ADMIN') ); | |
| 171 | - if( $result !== TRUE ){ | |
| 172 | - $msg = array(); | |
| 173 | - foreach( $result as $k => $v ){ | |
| 174 | - $msg[] = $k . ': ' . $v; | |
| 136 | + $args = array( | |
| 137 | + 'role' => $role_value, | |
| 138 | + 'exclude' => $current_user->ID | |
| 139 | + ); | |
| 140 | + $wordpress_users = get_users( $args ); | |
| 141 | + foreach( $wordpress_users as $wuser ) | |
| 142 | + { | |
| 143 | + if( (! $role_value) && $wuser->roles ) | |
| 144 | + { | |
| 145 | + continue; | |
| 146 | + } | |
| 147 | + | |
| 148 | + $user = new User_Model; | |
| 149 | + $user->email = $wuser->user_email; | |
| 150 | + $user->id = $wuser->ID; | |
| 151 | + | |
| 152 | + if( $wuser->user_firstname ) | |
| 153 | + { | |
| 154 | + $user->first_name = $wuser->user_firstname; | |
| 155 | + $user->last_name = $wuser->user_lastname; | |
| 156 | + } | |
| 157 | + else | |
| 158 | + { | |
| 159 | + $user->first_name = $wuser->display_name; | |
| 160 | + } | |
| 161 | + | |
| 162 | + if( $append_role_name ) | |
| 163 | + { | |
| 164 | + $wp_role = ( $wuser->roles && is_array($wuser->roles) && isset($wuser->roles[0]) ) ? $wuser->roles[0] : ''; | |
| 165 | + if( strlen($wp_role) ) | |
| 166 | + $user->first_name = '[' . $wp_role . '] ' . $user->first_name; | |
| 167 | + } | |
| 168 | + | |
| 169 | + $user->password = hc_random(); | |
| 170 | + $user->level = $our_level; | |
| 171 | + | |
| 172 | + if( $user->save_as_new() ) | |
| 173 | + { | |
| 174 | + $count_users++; | |
| 175 | + } | |
| 176 | + else | |
| 177 | + { | |
| 178 | + $setup_ok = FALSE; | |
| 179 | + $this->_drop_tables(); | |
| 180 | + | |
| 181 | + $err_msg = array(); | |
| 182 | + $err_msg[] = $wuser->user_email; | |
| 183 | + $err_msg = array_merge($err_msg, array_values($user->error->all) ); | |
| 184 | + $this->session->set_flashdata( 'error', $err_msg ); | |
| 185 | + ci_redirect( 'wordpress/setup' ); | |
| 186 | + return; | |
| 187 | + } | |
| 188 | + } | |
| 175 | 189 | } |
| 176 | - $msg = join( '<br>', $msg ); | |
| 177 | - $this->session->set_flashdata( 'error', $msg ); | |
| 178 | - $setup_ok = FALSE; | |
| 179 | 190 | } |
| 180 | - // $success_count++; | |
| 181 | 191 | |
| 182 | - if( $setup_ok ){ | |
| 192 | + if( $setup_ok ) | |
| 193 | + { | |
| 183 | 194 | /* default settings */ |
| 184 | 195 | $email_from = get_bloginfo('admin_email'); |
| 185 | 196 | $email_from_name = get_bloginfo('name'); |
| 186 | 197 | |
| 187 | - $app_conf->set( 'email_from', $email_from ); | |
| 188 | - $app_conf->set( 'email_from_name', $email_from_name ); | |
| 198 | + $this->app_conf->set( 'email_from', $email_from ); | |
| 199 | + $this->app_conf->set( 'email_from_name', $email_from_name ); | |
| 189 | 200 | |
| 190 | - $msg = 'Imported ' . $success_count . ' '; | |
| 201 | + $msg = 'Imported ' . $count_users . ' '; | |
| 191 | 202 | $msg .= ($count_users > 1) ? 'users' : 'user'; |
| 192 | 203 | |
| 193 | 204 | $this->session->set_flashdata( 'message', $msg ); |
| 194 | - $this->redirect('setup/ok'); | |
| 205 | + ci_redirect( 'setup/ok' ); | |
| 195 | 206 | return; |
| 196 | 207 | } |
| 197 | - else { | |
| 198 | - $this->_drop_tables(); | |
| 199 | - $this->redirect('wordpress/setup'); | |
| 200 | - } | |
| 201 | 208 | } |
| 202 | 209 | } |
| 203 | - return $this->index(); | |
| 210 | + | |
| 211 | + $this->data['include'] = 'setup'; | |
| 212 | + $this->load->view( '_layout/index_no_menu', $this->data ); | |
| 213 | + return; | |
| 214 | + } | |
| 215 | + | |
| 216 | + function ok() | |
| 217 | + { | |
| 218 | + $this->data['include'] = 'setup_ok'; | |
| 219 | + $this->load->view( '_layout/index_no_menu', $this->data); | |
| 220 | + return; | |
| 204 | 221 | } |
| 205 | 222 | } |
| 206 | 223 | |
| 207 | 224 | /* End of file setup.php */ |
| 208 | 225 | /* Location: ./application/controllers/setup.php */ |