/** * Displays the form * Inherits from TagManager_Form the form displaying methods * so the view can be slightly the same than the one used for one classical form * * @param FTL_Binding $tag * * @return string */ public static function tag_form(FTL_Binding $tag) { $ajax = $tag->getAttribute('ajax'); // Ajax form if ($ajax == TRUE) { // Get form string $str = parent::tag_form($tag); // No JS The user will add the JS part in his own JS script $nojs = $tag->getAttribute('nojs'); $form_name = $tag->getAttribute('name'); $form_submit_id = $tag->getAttribute('submit'); // $error_tag = $tag->getAttribute('error_tag'); // $error_tag_class = $tag->getAttribute('error_tag_class'); // Module settings self::$config = Modules()->get_module_config('Ajaxform'); if (!$nojs) { // Add the JS part of the module if ($form_name && $form_submit_id) { $data = array('form_name' => $form_name, 'form_submit_id' => $form_submit_id, 'url' => base_url() . Settings::get_lang() . '/' . self::$config['uri'] . '/post'); $str .= self::$ci->load->view('ajaxform_js', $data, TRUE); } else { log_message('error', 'Ajaxform ERROR : Set the name & submit attributes of the <ion:form name="formName" submit="submitButtonID"> tag'); } } return $str; } else { return parent::tag_form($tag); } }
/** * 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; } } }
/** * Returns the config settings for one form name * * @param $form_name * * @return null */ public static function get_form_settings($form_name = NULL) { if (is_null(self::$forms)) { // Get forms settings $forms = config_item('forms'); if (is_file($file = Theme::get_theme_path() . 'config/forms.php')) { include $file; if (!empty($config['forms'])) { $forms = array_merge($forms, $config['forms']); unset($config); } } self::$forms = $forms; } if (!is_null($form_name) && isset(self::$forms[$form_name])) { return self::$forms[$form_name]; } return self::$forms; }
/** * 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 ... } */ }
/** * Sends Emails as defined in the forms.php config file. * Important : This method receives the "form" tag * * @param FTL_Binding Form tag * @param string * @param array Array of data send to the Email view. * Each key of this array will be available in the view by : * <ion:data:key /> * * The passed array should look like this : * array( * 'email' => 'user email', // Email of the user (POST, DB...) * 'key1' => 'value1, // Makes <ion:data:key1 /> available in the Email view * ); * */ public static function send_form_emails(FTL_Binding $tag, $form_name, $data = array()) { // Set the 'data' tag from the received data array self::$context->define_tag('data', array(__CLASS__, 'tag_expand')); foreach ($data as $key => $value) { if (!is_array($value) && !is_object($value)) { self::$context->define_tag('data:' . $key, array(__CLASS__, 'tag_simple_value')); } } // Get all declared emails configuration data from forms.php config file $emails = TagManager_Form::get_form_emails($form_name); // Get the 'sender' email : Must be set in Ionize : Settings > Advanced settings > Email $website_email = Settings::get('site_email') ? Settings::get('site_email') : NULL; // Send all defined emails foreach ($emails as $email_setting) { $email = $email_setting['email']; $reply_to = isset($email_setting['reply_to']) ? $email_setting['reply_to'] : NULL; // Get potential website / form email switch ($email) { case 'site': $email = Settings::get('site_email') != '' ? Settings::get('site_email') : NULL; break; case 'form': $email = isset($data['email']) ? $data['email'] : self::$ci->input->post('email'); break; case $email == 'contact' || $email == 'technical' || $email == 'info': $email = Settings::get('email_' . $email) != '' ? Settings::get('email_' . $email) : NULL; break; default: $email = $email; $_email = explode('::', $email); if (!empty($_email[1])) { $email = self::$ci->input->post($_email[1]); } break; } if (!is_null($reply_to)) { switch ($reply_to) { case 'site': $reply_to = Settings::get('site_email') != '' ? Settings::get('site_email') : NULL; break; case 'form': $reply_to = isset($data['email']) ? $data['email'] : self::$ci->input->post('email'); break; default: $reply_to = Settings::get('email_' . $email) != '' ? Settings::get('email_' . $email) : NULL; break; } } // Send the email if ($email) { // Subject, adds the website title as swap text : displayed in title if the %s key is used. $subject = lang($email_setting['subject'], Settings::get('site_title')); $data['subject'] = $subject; // Set the "data tag" array of data. $tag->set('data', $data); // Email Lib if (!isset(self::$ci->email)) { self::$ci->load->library('email'); } self::$ci->email->clear(); // Subject / From / To self::$ci->email->subject($subject); self::$ci->email->from($website_email, Settings::get("site_title")); self::$ci->email->to($email); if (!is_null($reply_to)) { self::$ci->email->reply_to($reply_to); } // View & Message content $view_content = $tag->parse_as_standalone(self::$tag_prefix, Theme::load($email_setting['view'])); self::$ci->email->message($view_content); // Send silently $result = @self::$ci->email->send(); if (!$result) { log_message('error', 'Error : Tagmanager/Email->send_form_emails() : Email was not sent.'); } } else { log_message('error', 'Error : Tagmanager/Email->send_form_emails() : Email not found'); } } }