/** * Processes the form POST data. * This method is declared as form "process" method in /application/config/forms.php for each form. * We could declare one method / form, but we decided to process all user's form with this one. * * @param FTL_Binding 'init' tag (not the user one because this method is run before any tag parsing) * This tag is supposed to be only used to send Emails. * With this tag, Emails views have access to the global tags, but not to any other * object tag. * @return void * */ public static function process_data(FTL_Binding $tag) { $form_name = self::$ci->input->post('form'); if ($form_name) { switch ($form_name) { // Logout case 'logout': if (User()->logged_in()) { // Potentially redirect to the page setup in /application/config/forms.php $redirect = TagManager_Form::get_form_redirect(); User()->logout($redirect); } break; // Login // Login case 'login': if (TagManager_Form::validate('login')) { if (!User()->logged_in()) { $email = self::$ci->input->post('email'); $db_user = self::$ci->user_model->find_user(array('email' => $email)); if ($db_user) { // Account not allowed to login if ($db_user['role_level'] < 100) { $message = TagManager_Form::get_form_message('not_activated'); TagManager_Form::set_additional_error('login', $message); } else { $user = array('email' => $email, 'password' => self::$ci->input->post('password')); $result = User()->login($user); if ($result) { // Potentially redirect to the page setup in /application/config/forms.php $redirect = TagManager_Form::get_form_redirect(); if ($redirect !== FALSE) { redirect($redirect); } // If redirect is commented, this success message will be available. $message = TagManager_Form::get_form_message('success'); TagManager_Form::set_additional_success('login', $message); } else { $message = TagManager_Form::get_form_message('error'); TagManager_Form::set_additional_error('login', $message); } } } else { $message = TagManager_Form::get_form_message('not_found'); TagManager_Form::set_additional_error('login', $message); } } } break; // Register // Register case 'register': if (TagManager_Form::validate('register')) { // Get user's allowed fields $fields = TagManager_Form::get_form_fields('register'); if (is_null($fields)) { show_error('No definition for the form "register"'); } $fields = array_fill_keys($fields, FALSE); $user = array_merge($fields, self::$ci->input->post()); // Compliant with User, based on username $user['username'] = $user['email']; $user['join_date'] = date('Y-m-d H:i:s'); // Fire returns an array $results = Event::fire('User.register.check.before', $user); // Empty $result : No method registered to 'User.register.check.before' => No test // Result == TRUE : The user can register if (self::isResultTrue($results)) { if (!User()->register($user)) { $message = User()->error(); if (empty($message)) { $message = TagManager_Form::get_form_message('error'); } TagManager_Form::set_additional_error('register', $message); } else { // Get the user saved in DB $user = self::$ci->user_model->find_user($user['username']); if (is_array($user)) { // Must be set before set the clear password $user['activation_key'] = User()->calc_activation_key($user); $user['password'] = User()->decrypt($user['password'], $user); // Merge POST data for email template $user = array_merge($user, self::$ci->input->post()); // Create data array and Send Emails $user['ip'] = self::$ci->input->ip_address(); TagManager_Email::send_form_emails($tag, 'register', $user); $message = TagManager_Form::get_form_message('success'); TagManager_Form::set_additional_success('register', $message); // Potentially redirect to the page setup in /application/config/forms.php $redirect = TagManager_Form::get_form_redirect(); if ($redirect !== FALSE) { redirect($redirect); } } else { $message = TagManager_Form::get_form_message('error'); TagManager_Form::set_additional_error('register', $message); } } } else { Event::fire('User.register.check.fail', $user); $message = TagManager_Form::get_form_message('success'); TagManager_Form::set_additional_success('register', $message); $redirect = TagManager_Form::get_form_redirect(); if ($redirect !== FALSE) { redirect($redirect); } } } break; // Get new password // Get new password case 'password': if (TagManager_Form::validate('password')) { $user = self::$ci->user_model->find_user(array('email' => self::$ci->input->post('email'))); if ($user) { // Save the user with this new password $new_password = User()->get_random_password(8); $user['password'] = $new_password; if (!User()->update($user)) { $message = TagManager_Form::get_form_message('error'); TagManager_Form::set_additional_error('password', $message); } else { // Get the user again, to calculate his activation key $user = self::$ci->user_model->find_user(array('email' => self::$ci->input->post('email'))); $activation_key = User()->calc_activation_key($user); // Put the clear password to the user's data, for the email //$user['password'] = $new_password; $data['activation_key'] = $activation_key; // Send Emails $data['ip'] = self::$ci->input->ip_address(); $data['username'] = $user['username']; $data['firstname'] = $user['firstname']; $data['email'] = $user['email']; $data['password'] = $new_password; $data['activation_key'] = $activation_key; $data['level'] = $user['role_level']; TagManager_Email::send_form_emails($tag, 'password', $data); $message = TagManager_Form::get_form_message('success'); TagManager_Form::set_additional_success('password', $message); // Potentially redirect to the page setup in /application/config/forms.php $redirect = TagManager_Form::get_form_redirect(); if ($redirect !== FALSE) { redirect($redirect); } } } else { $message = TagManager_Form::get_form_message('not_found'); TagManager_Form::set_additional_error('password', $message); } } break; // Activate account // Activate account case 'activation': // Done through one old plain CI controller for the moment. // Adding tags for this task adds more complexity for nothing // (create one page, set the page in Ionize... this all is not needed for account activation) break; // Save profile // Save profile case 'profile': // Lost connection if (($current_user = User()->get_user()) == NULL) { $message = TagManager_Form::get_form_message('not_logged'); TagManager_Form::set_additional_error('profile', $message); return FALSE; } // Delete the profile if (self::$ci->input->post('delete')) { $result = User()->delete($current_user); $message = TagManager_Form::get_form_message('deleted'); TagManager_Form::set_additional_success('profile', $message); // Potentially redirect to the page setup in /application/config/forms.php $redirect = TagManager_Form::get_form_redirect(); User()->logout($redirect); } else { if (TagManager_Form::validate('profile')) { $fields = TagManager_Form::get_form_fields('profile'); if (is_null($fields)) { show_error('No definition for the form "profile"'); } $fields = array_fill_keys($fields, FALSE); $user = array_merge($fields, self::$ci->input->post()); // Compliant with User, based on username $user['username'] = $user['email']; $user['id_user'] = $current_user['id_user']; // Checkboxes and multiselect foreach ($user as $key => $data) { if (is_array($data)) { $user[$key] = implode(',', $data); } } $result = User()->update($user); // If error here, it can only be on the email, which already exists in the DB if (!$result) { $message = TagManager_Form::get_form_message('error'); TagManager_Form::set_additional_error('email', $message); } else { $message = TagManager_Form::get_form_message('success'); TagManager_Form::set_additional_success('profile', $message); // Potentially redirect to the page setup in /application/config/forms.php $redirect = TagManager_Form::get_form_redirect(); if ($redirect !== FALSE) { redirect($redirect); } } } else { } } break; } } }
/** * Processes the form POST data. * * @param FTL_Binding 'init' tag (not the user one because this method is run before any tag parsing) * This tag is supposed to be only used to send Emails. * With this tag, Emails views have access to the global tags, but not to any other * object tag. * * @return void * */ public static function process_data(FTL_Binding $tag) { // Name of the form : Must be send to identify the form. $form_name = self::$ci->input->post('form'); // Because Form are processed before any tag rendering, we have to run the validation if (TagManager_Form::validate($form_name)) { // // ... Here you do what you want with the data ... // // For the example, we will send one email to the address the user gave in the form // // Posted data // To see the posted array, uncomment trace($posted) // If you prefer to see these data through one log file, // uncomment log_message(...) and be sure /application/config/config.php contains : // $config['log_threshold'] = 1; // The log files are located in : /application/logs/log-YYYY-MM-DD.php // We prefer to log our 'dev' data as 'error' to not see the all CodeIgniter 'debug' messages. $post = self::$ci->input->post(); // trace($posted); // log_message('error', print_r($posted, TRUE)); // SFS : Fires the event declared in Stop Form Spam module config // Do we go further in the form processing ? Yes by default. $go_further = TRUE; $results = Event::fire('Form.contact.check', $post); if (is_array($results)) { foreach ($results as $result) { if (!$result) { $go_further = FALSE; } } } if ($go_further) { // Send the posted data to the Email library and send the Email // as defined in /themes/your_theme/config/forms.php TagManager_Email::send_form_emails($tag, $form_name, $post); // Add one custom Success message // Get the messages key defined in : /themes/your_theme/config/forms.php // You can also set directly one lang translated key $message = TagManager_Form::get_form_message('success'); TagManager_Form::set_additional_success($form_name, $message); // Alternative : Set the message by using directly one lang translated key : // TagManager_Form::set_additional_success($form_name, lang('form_message_success')); } // Use of the 'redirect' option of the form config. // If no redirect after processing, the form data can be send again if the user refreshes the page // To avoid that, we use the redirection directive as set in the config file: // /themes/your_theme/config/forms.php $redirect = TagManager_Form::get_form_redirect(); if ($redirect !== FALSE) { redirect($redirect); } } /* // Normally, nothing should be done here, because the validation process refill the form // and doesn't redirect, so the user's filled in data can be used to fill the form again. // Remember : If you redirect here, the form refill will not be done, as the data are lost // (no access to the posted data anymore after redirection) else { // ... Do something here ... } */ }