public function save_form($request, $params, $register_user = true, $redirect_as_well = true)
 {
     if (isset($params['is_payment_form']) && $params['is_payment_done']) {
         //user already activated in 'ipn' case, do nothing.
         //register user if it was a 'zero_amount case'.
         if (isset($params['za_user_id']) && $params['za_user_id']) {
             $gopt = new RM_Options();
             $this->user_service->activate_user_by_id($params['za_user_id']);
         }
         $this->after_submission_proc($params);
         return;
     }
     $user_error = false;
     $form_fields = parent::get_all_form_fields($params['form']->form_id);
     $valid_field_ids = $profile_field_id = array();
     $form_type = $params['form']->form_type;
     $is_auto_generate = parent::get_setting('auto_generated_password');
     $email = null;
     $profile = array();
     $reg_data = new stdClass();
     $reg_data->submission_id = null;
     $reg_data->user_id = null;
     foreach ($form_fields as $form_field) {
         $valid_field_ids[] = $form_field->field_id;
         if ($form_field->field_type === 'Email' && $form_field->is_field_primary == 1) {
             $profile_field_id['email'] = $form_field->field_id;
         }
         if ($form_type == 1) {
             if ($form_field->field_type === 'Textbox' && $form_field->is_field_primary == 1) {
                 $profile_field_id['username'] = $form_field->field_id;
             }
             if ($form_field->field_type === 'Password' && $form_field->is_field_primary == 1) {
                 $profile_field_id['password'] = $form_field->field_id;
             }
         }
     }
     $submissions_data = array();
     $attachment = new RM_Attachment_Service();
     $att_ids = $attachment->attach();
     //var_dump($att_ids);
     foreach ($request->req as $key => $value) {
         $key_parts = explode('_', $key);
         $count_parts = count($key_parts);
         if (($count_parts === 2 || $count_parts === 3) && in_array($key_parts[1], $valid_field_ids, true)) {
             $field = new RM_Fields();
             $field->load_from_db($key_parts[1]);
             if ($field->field_type === $key_parts[0]) {
                 if ($field->field_type === "Password") {
                     if ($is_auto_generate === 'yes') {
                         $value = wp_generate_password(8, false);
                     }
                     //$value = md5($value);
                 } elseif ($field->field_type === "Fname" || $field->field_type === "Lname" || $field->field_type === "BInfo") {
                     $profile[$field->field_type] = $value;
                 } else {
                     if ($field->field_type === "File") {
                         $value = null;
                         foreach ($att_ids as $field_name => $att_id) {
                             $value = array();
                             if ($field_name == $key) {
                                 $value['rm_field_type'] = 'File';
                                 if (is_array($att_id)) {
                                     foreach ($att_id as $abc) {
                                         $value[] = $abc;
                                     }
                                 } else {
                                     $value[] = $att_id;
                                 }
                             }
                         }
                     }
                 }
                 if ($field->field_type === "Price") {
                     $paypal_field = new RM_PayPal_Fields();
                     $prices = array();
                     $item_names = array();
                     $paypal_field->load_from_db((int) $key_parts[2]);
                     switch ($paypal_field->get_type()) {
                         case "fixed":
                         case "userdef":
                             $submission_field_rows[] = array('field_id' => $key_parts[1], 'value' => $value, 'form_id' => $params['form']->form_id);
                             $submissions_data[$field->field_id] = new stdClass();
                             $submissions_data[$field->field_id]->label = $field->field_label;
                             $submissions_data[$field->field_id]->value = $value;
                             break;
                         case "multisel":
                             $tmp_v = maybe_unserialize($paypal_field->get_option_price());
                             $tmp_l = maybe_unserialize($paypal_field->get_option_label());
                             $gopt = new RM_Options();
                             $val_arr = array();
                             if ($value) {
                                 foreach ($value as $pf_single_val) {
                                     $index = (int) substr($pf_single_val, 1);
                                     if (!isset($tmp_v[$index])) {
                                         continue;
                                     }
                                     $prices[] = $tmp_v[$index];
                                     $item_names[] = $tmp_l[$index];
                                     $val_arr[] = $tmp_l[$index] . " (" . $gopt->get_formatted_amount($tmp_v[$index]) . ")";
                                 }
                                 $value = $val_arr;
                             }
                             $submission_field_rows[] = array('field_id' => $key_parts[1], 'value' => $value, 'form_id' => $params['form']->form_id);
                             $submissions_data[$field->field_id] = new stdClass();
                             $submissions_data[$field->field_id]->label = $field->field_label;
                             $submissions_data[$field->field_id]->value = $value;
                             break;
                         case "dropdown":
                             $tmp_v = maybe_unserialize($paypal_field->get_option_price());
                             $tmp_l = maybe_unserialize($paypal_field->get_option_label());
                             $gopt = new RM_Options();
                             if ($value) {
                                 $index = (int) substr($value, 1);
                                 if (!isset($tmp_v[$index])) {
                                     break;
                                 }
                                 $prices[] = $tmp_v[$index];
                                 $item_names[] = $tmp_l[$index];
                                 $value = $tmp_l[$index] . " (" . $gopt->get_formatted_amount($tmp_v[$index]) . ")";
                             }
                             $submission_field_rows[] = array('field_id' => $key_parts[1], 'value' => $value, 'form_id' => $params['form']->form_id);
                             $submissions_data[$field->field_id] = new stdClass();
                             $submissions_data[$field->field_id]->label = $field->field_label;
                             $submissions_data[$field->field_id]->value = $value;
                             break;
                     }
                 } else {
                     if ($field->get_field_type() !== 'HTMLH' && $field->get_field_type() !== 'HTMLP') {
                         $submission_field_rows[] = array('field_id' => $key_parts[1], 'value' => $value, 'form_id' => $params['form']->form_id);
                         $submissions_data[$field->field_id] = new stdClass();
                         $submissions_data[$field->field_id]->label = $field->field_label;
                         $submissions_data[$field->field_id]->value = $value;
                     }
                 }
                 foreach ($profile_field_id as $key => $id) {
                     if ($key_parts[1] === $id) {
                         if ($key == 'email') {
                             $email = $value;
                         }
                     }
                 }
             }
         }
     }
     /*
      * Register the user if form is registration type (FormType value is 1)
      */
     if ($form_type == 1 && !is_user_logged_in()) {
         if (isset($params['is_payment_form'])) {
             if ($params['is_payment_done']) {
                 //user already activated in 'ipn' case, do nothing.
             } else {
                 //create user but keep deactivated
                 $user_id = $this->register_user($request, $params['form'], $is_auto_generate, false);
                 $this->update_user_profile($user_id, $profile);
                 $reg_data->user_id = $user_id;
             }
         } else {
             $user_id = $this->register_user($request, $params['form'], $is_auto_generate);
             $this->update_user_profile($user_id, $profile);
             $reg_data->user_id = $user_id;
         }
     } else {
         $this->update_user_profile($email, $profile, true);
     }
     //if ($form_type == 1)
     /*
      * Check if any attachment was with submission only if there is no form error
      */
     $submission_row = array('form_id' => $params['form']->form_id, 'data' => $submissions_data, 'user_email' => $email);
     $submissions = new RM_Submissions();
     $submissions->set($submission_row);
     $submission_id = $submissions->insert_into_db();
     if ($submission_field_rows) {
         foreach ($submission_field_rows as $submission_field_row) {
             $submission_field_row['submission_id'] = $submission_id;
             $submission_field = new RM_Submission_Fields();
             $submission_field->set($submission_field_row);
             //If submission is already in the table update it. (for PayPal cases.)
             if ($submission_field->insert_into_db() === false) {
                 $submission_field->update_into_db();
             }
             unset($submission_field);
         }
     }
     /*
      * Send email notification to admin and other receivers
      */
     $submissions->load_from_db($submission_id);
     $email = $this->prepare_email('to_admin', $submissions, $params['form']);
     RM_Utilities::send_mail($email);
     /*
      * If auto reply option enabled
      */
     if ($params['form']->get_form_should_send_email() == "1") {
         $email = $this->prepare_email('to_registrar', $submissions, $params['form'], $request);
         RM_Utilities::send_mail($email);
     }
     /*
      * Redirecting user as per form configuration after submission
      */
     if ($redirect_as_well) {
         $this->after_submission_proc($params);
     }
     /* echo $params['form']->form_options->form_success_message!=""?$params['form']->form_options->form_success_message:$params['form']->form_name." Submitted ";
     
     
               if(isset($params['form']->form_redirect) && $params['form']->form_redirect!="none" && $params['form']->form_redirect!=""){
               if($params['form']->form_redirect=="page"){
               RM_Utilities::redirect(null,true,$params['form']->get_form_redirect_to_page(),true);
               }else{
               RM_Utilities::redirect($params['form']->get_form_redirect_to_url(),false,0,true);
               }
               }
              */
     //if(isset($request->req['stat_id']))
     $reg_data->submission_id = $submission_id;
     return $reg_data;
 }
 public function save_submission($form_id, $data, $email)
 {
     $submission_row = array('form_id' => $form_id, 'data' => $data, 'user_email' => $email);
     $submissions = new RM_Submissions();
     $submissions->set($submission_row);
     $submission_id = $submissions->insert_into_db();
     $submission_field = new RM_Submission_Fields();
     $submission_field_row['submission_id'] = $submission_id;
     $submission_field_row['form_id'] = $form_id;
     foreach ($data as $field_id => $field_data) {
         $submission_field_row['field_id'] = $field_id;
         $submission_field_row['value'] = $field_data->value;
         $submission_field->set($submission_field_row);
         $submission_field->insert_into_db(true);
     }
     return (object) array('submission_id' => $submission_id, 'token' => null);
 }