public function manage($model, $service, $request, $params)
 {
     if (!isset($request->req['form_name'])) {
         Form::clearErrors('rm_form_quick_add');
     }
     $sort_by = isset($request->req['rm_sortby']) ? $request->req['rm_sortby'] : null;
     $descending = isset($request->req['rm_descending']) ? false : true;
     $req_page = isset($request->req['rm_reqpage']) && $request->req['rm_reqpage'] > 0 ? $request->req['rm_reqpage'] : 1;
     $items_per_page = 9;
     $forms = $service->get_all(null, ($req_page - 1) * $items_per_page, $items_per_page, '*', $sort_by, $descending);
     $i = 0;
     $data = array();
     if (is_array($forms) || is_object($forms)) {
         foreach ($forms as $form) {
             $data[$i] = new stdClass();
             $data[$i]->form_id = $form->form_id;
             $data[$i]->form_name = $form->form_name;
             $data[$i]->count = $service->count(RM_Submissions::get_identifier(), array('form_id' => $form->form_id));
             if ($data[$i]->count > 0) {
                 $data[$i]->submissions = $service->get(RM_Submissions::get_identifier(), array('form_id' => $form->form_id), array('%d'), 'results', 0, 3, '*', 'submitted_on', true);
                 $j = 0;
                 foreach ($data[$i]->submissions as $submission) {
                     $data[$i]->submissions[$j++]->gravatar = get_avatar($submission->user_email);
                 }
             }
             $data[$i]->field_count = $service->count(RM_Fields::get_identifier(), array('form_id' => $form->form_id));
             $data[$i]->last_sub = $service->get(RM_Submissions::get_identifier(), array('form_id' => $form->form_id), array('%d'), 'var', 0, 1, 'submitted_on', 'submitted_on', true);
             //$data[$i]->last_sub = date('H',strtotime($this->service->get(RM_Submissions::get_identifier(), array('form_id' => $data_single->form_id), array('%d'), 'var', 0, 1, 'submitted_on', 'submitted_on', true)));
             $data[$i]->expiry_details = $service->get_form_expiry_stats($form, false);
             $i++;
         }
     }
     $total_forms = $service->count($model->get_identifier(), 1);
     //New object to consolidate data for view.
     $view_data = new stdClass();
     $view_data->data = $data;
     $view_data->curr_page = $req_page;
     $view_data->total_pages = (int) ($total_forms / $items_per_page) + ($total_forms % $items_per_page == 0 ? 0 : 1);
     $view_data->rm_slug = $request->req['page'];
     $view_data->sort_by = $sort_by;
     $view_data->descending = $descending;
     $view_data->done_with_review_banner = $service->get_setting('done_with_review_banner') === 'no' ? false : true;
     $view_data->def_form_id = $service->get_setting('default_form_id');
     if (function_exists('is_multisite') && is_multisite()) {
         $nl_subscribed = get_site_option('rm_option_newsletter_subbed', false);
     } else {
         $nl_subscribed = get_site_option('rm_option_newsletter_subbed', false);
     }
     if (!$nl_subscribed) {
         $view_data->newsletter_sub_link = RM_UI_Strings::get('NEWSLETTER_SUB_MSG');
     } else {
         $view_data->newsletter_sub_link = null;
     }
     $view = $this->mv_handler->setView('form_manager');
     $view->render($view_data);
 }
 public function notify_users($note)
 {
     $gopt = new RM_Options();
     if ($gopt->get_value_of('user_notification_for_notes') == "yes") {
         if ($note->get_status() != 'publish') {
             return;
         }
         $submission = new RM_Submissions();
         $submission->load_from_db($note->get_submission_id());
         $email = new stdClass();
         //echo '<pre>';
         //print_r($submission); die;
         $email->to = $submission->get_user_email();
         $from_email = $gopt->get_value_of('senders_email_formatted');
         $header = "From: {$from_email}\r\n";
         $header .= "MIME-Version: 1.0\r\n";
         $header .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
         $email->subject = get_bloginfo('name', 'display') . " Notification from Admin ";
         $email->message = RM_UI_Strings::get('MSG_NOTE_FROM_ADMIN') . $note->get_notes();
         $email->header = $header;
         $email->attachments = array();
         RM_Utilities::send_mail($email);
     }
 }
 public function process($model, RM_Front_Form_Service $service, $request, $params)
 {
     $data = new stdClass();
     if (isset($params['form_id']) && $params['form_id']) {
         $form = new RM_Forms();
         $form->load_from_db($params['form_id']);
         //echo "<pre>",var_dump($form),die();
     } else {
         return;
     }
     //Called from PayPal, straight to processing.
     if (isset($request->req['rm_pproc'])) {
         if ($request->req['rm_pproc'] == 'success' || $request->req['rm_pproc'] == 'cancel' || $request->req['rm_pproc'] == 'ipn') {
             //error_log('rm_pproc = '.$request->req['rm_pproc']);
             $paystate = $service->process_payment(null, null, $service, $request);
             $params['form'] = $form;
             $params['is_payment_form'] = true;
             $params['is_payment_done'] = true;
             /*else //Pending or cancelation cases
               {
                   $params['form'] = $form;
                   $params['is_payment_form'] = true;
                   $params['is_payment_done'] = false;                    
               }*/
             $service->save_form($request, $params);
             return;
         }
     }
     /*
      * If register form type then check if user exists
      */
     $user_exists = false;
     if ($form->form_type == 1 && !is_user_logged_in()) {
         if ($service->user_exists($form, $request)) {
             $user_exists = true;
         } else {
             Form::clearErrors('form_' . $form->form_id);
         }
     }
     if (isset($request->req['stat_id'])) {
         $stat_id = $request->req['stat_id'];
     } else {
         $stat_id = null;
     }
     /*
      * Validates the form in case form is not expired.
      */
     if ($this->mv_handler->validateForm('form_' . $form->form_id) && !$service->is_form_expired($form) && !$user_exists && !$service->is_browser_reload_duplication($stat_id)) {
         $params['form'] = $form;
         // echo "<pre>",var_dump($form),die();
         if ($service->is_off_limit_submission($form->form_id)) {
             die(RM_UI_Strings::get("ALERT_SUBMISSIOM_LIMIT"));
         }
         $service->update_stat_entry($stat_id);
         if ($service->get_setting('enable_mailchimp') == 'yes') {
             $form_options_mc = $form->get_form_options();
             //die;
             if ($form_options_mc->form_is_opt_in_checkbox == 1) {
                 $should_subscribe = isset($request->req['rm_subscribe_mc']) && $request->req['rm_subscribe_mc'][0] == 1 ? 'yes' : 'no';
             } else {
                 $should_subscribe = 'yes';
             }
             if ($should_subscribe == 'yes') {
                 $mailchimp = new RM_MailChimp_Service();
                 $mc_member = new stdClass();
                 if (isset($request->req[$form_options_mc->mailchimp_mapped_email])) {
                     $mc_member->email = $request->req[$form_options_mc->mailchimp_mapped_email];
                     if (isset($request->req[$form_options_mc->mailchimp_mapped_first_name])) {
                         $mc_member->first_name = $request->req[$form_options_mc->mailchimp_mapped_first_name];
                     } else {
                         $mc_member->first_name = NULL;
                     }
                     if (isset($request->req[$form_options_mc->mailchimp_mapped_last_name])) {
                         $mc_member->last_name = $request->req[$form_options_mc->mailchimp_mapped_last_name];
                     } else {
                         $mc_member->last_name = NULL;
                     }
                     $mailchimp->subscribe($mc_member, $form_options_mc->mailchimp_list);
                 }
             }
         }
         if (isset($request->req['rm_payment_form'])) {
             //Do not register user if the payment+registration type form, wait for payment confirmation.
             $params['is_payment_form'] = true;
             $params['is_payment_done'] = false;
             $rd = $service->save_form($request, $params, true, false);
             //die("xx");
             $res = $service->process_payment($form->get_form_id(), $rd, $service, $request);
             if ($res == 'zero_amount') {
                 $params['is_payment_form'] = true;
                 $params['is_payment_done'] = true;
                 $params['za_user_id'] = $rd->user_id;
                 $rd = $service->save_form($request, $params);
                 //$submission_id = $rd->submission_id;
             }
         } else {
             $rd = $service->save_form($request, $params);
             $submission_id = $rd->submission_id;
         }
     } else {
         if (isset($request->req['rm_submission_id'])) {
             $submissions = new RM_Submissions();
             $submissions->load_from_db($request->req['rm_submission_id']);
         }
         /*
          * Get all the fields associated with the Form
          */
         $data->fields_data = $service->get_all_form_fields($params['form_id']);
         $i = 0;
         if (is_array($data->fields_data) || is_object($data->fields_data)) {
             foreach ($data->fields_data as $field_data) {
                 $data->fields_data[$i++]->properties = $service->set_properties(maybe_unserialize($field_data->field_options));
             }
         }
         $data->form = $form;
         $data->is_auto_generate = false;
         /*
          * Checking if password generation is configured
          */
         if ($form->get_form_type() == 1) {
             $auto_generate = $service->get_setting('auto_generated_password');
             if ($auto_generate == "yes") {
                 $data->is_auto_generate = true;
             }
         }
         /*
          * Check if mailchimp is enabled in options
          */
         if ($service->get_setting('enable_mailchimp') == 'yes') {
             $data->is_mailchimp_enabled = true;
         } else {
             $data->is_mailchimp_enabled = false;
         }
         /*
          * If user role are to be choosen by registrar
          */
         /*           echo '<pre>';
                      print_r($form); */
         if (!empty($form->form_options->form_should_user_pick) || !(isset($form->form_user_role) && !empty($form->form_user_role))) {
             $role_pick = $form->form_options->form_should_user_pick;
             if ($role_pick) {
                 global $wp_roles;
                 $allowed_roles = array();
                 $default_wp_roles = $wp_roles->get_names();
                 $form_roles = $form->get_form_user_role();
                 if (is_array($form_roles) && count($form_roles) > 0) {
                     foreach ($form_roles as $val) {
                         if (array_key_exists($val, $default_wp_roles)) {
                             $allowed_roles[$val] = $default_wp_roles[$val];
                         }
                     }
                 }
                 $data->allowed_roles = $allowed_roles;
                 $data->role_as = empty($form->form_options->form_user_field_label) ? RM_UI_Strings::get('LABEL_ROLE_AS') : $form->form_options->form_user_field_label;
             }
         }
         /*
          * Checking if form is expired
          */
         $data->expired = $service->is_form_expired($form);
         $data->user_exists = $user_exists;
         if (isset($submissions)) {
             $data->submissions = $submissions;
         }
         $data->curr_symbol = $service->get_setting('currency_symbol');
         $data->currency_pos = $service->get_setting('currency_symbol_position');
         $data->stat_id = $service->create_stat_entry($params);
         if ($service->get_setting('theme') == 'matchmytheme') {
             $data->submit_btn_fgcolor = $form->form_options->form_submit_btn_color;
             $data->submit_btn_bgcolor = $form->form_options->form_submit_btn_bck_color;
         } else {
             $data->submit_btn_fgcolor = null;
             $data->submit_btn_bgcolor = null;
         }
         $data->expiry_details = $service->get_form_expiry_stats($form);
         $view = $this->mv_handler->setView("user_form", true);
         return $view->read($data);
     }
 }
 public function submissions($model, RM_Front_Service $service, $request, $params)
 {
     $i = 0;
     $user_email = $service->get_user_email();
     //var_dump($user_email);die;
     if (null != $user_email) {
         if (isset($request->req['submission_id'])) {
             $submission = new RM_Submissions();
             $submission->load_from_db($request->req['submission_id']);
             if ($submission->get_user_email() == $user_email) {
                 $view = $this->mv_handler->setView('front_submission_data', true);
                 $data = new stdClass();
                 $settings = new RM_Options();
                 $data->is_authorized = true;
                 $data->submission = $submission;
                 $data->payment = $service->get('PAYPAL_LOGS', array('submission_id' => $submission->get_submission_id()), array('%d'), 'row', 0, 99999);
                 if ($data->payment != null) {
                     $data->payment->total_amount = $settings->get_formatted_amount($data->payment->total_amount, $data->payment->currency);
                     if ($data->payment->log) {
                         $data->payment->log = maybe_unserialize($data->payment->log);
                     }
                 }
                 $data->notes = $service->get('NOTES', array('submission_id' => $submission->get_submission_id(), 'status' => 'publish'), array('%d', '%s'), 'results', 0, 99999, '*', null, true);
                 $i = 0;
                 if (is_array($data->notes)) {
                     foreach ($data->notes as $note) {
                         $data->notes[$i]->author = get_userdata($note->published_by)->display_name;
                         if ($note->last_edited_by) {
                             $data->notes[$i++]->editor = get_userdata($note->last_edited_by)->display_name;
                         } else {
                             $data->notes[$i++]->editor = null;
                         }
                     }
                 }
                 /*
                  * Check submission type
                  */
                 $form = new RM_Forms();
                 $form->load_from_db($submission->get_form_id());
                 $form_type = $form->get_form_type() == "1" ? "Registration" : "Contact";
                 $data->form_type = $form_type;
                 $data->form_type_status = $form->get_form_type();
                 $data->form_name = $form->get_form_name();
                 $data->form_is_unique_token = $form->get_form_is_unique_token();
                 /*
                  * User details if form is registration type
                  */
                 if ($form->get_form_type() == "1") {
                     $email = $submission->get_user_email();
                     if ($email != "") {
                         $user = get_user_by('email', $email);
                         $data->user = $user;
                     }
                 }
                 return $view->read($data);
             } else {
                 $view = $this->mv_handler->setView('not_authorized', true);
             }
             $msg = RM_UI_Strings::get('MSG_INVALID_SUBMISSION_ID_FOR_EMAIL');
             return $view->read($msg);
         } else {
             $data = new stdClass();
             $data->is_authorized = true;
             $data->submissions = array();
             $data->form_names = array();
             $data->submission_exists = false;
             //data for user page
             $user = get_user_by('email', $user_email);
             if ($user instanceof WP_User) {
                 $data->is_user = true;
                 $data->user = $user;
                 $data->custom_fields = $service->get_custom_fields($user_email);
             } else {
                 $data->is_user = false;
             }
             //For pagination of submissions
             $entries_per_page_sub = 20;
             $req_page_sub = isset($request->req['rm_reqpage_sub']) && $request->req['rm_reqpage_sub'] > 0 ? $request->req['rm_reqpage_sub'] : 1;
             $offset_sub = ($req_page_sub - 1) * $entries_per_page_sub;
             $total_entries_sub = $service->get_submission_count($user_email);
             $submissions = $service->get_submissions_by_email($user_email, $entries_per_page_sub, $offset_sub);
             $submission_ids = array();
             if ($submissions) {
                 $data->submission_exists = true;
                 foreach ($submissions as $submission) {
                     $form_name = $service->get('FORMS', array('form_id' => $submission->form_id), array('%d'), 'var', 0, 1, 'form_name');
                     $data->submissions[$i] = new stdClass();
                     $data->submissions[$i]->submission_ids = array();
                     $data->submissions[$i]->submission_id = $submission->submission_id;
                     $submission_ids[$i] = $submission->submission_id;
                     $data->submissions[$i]->submitted_on = $submission->submitted_on;
                     $data->submissions[$i]->form_name = $form_name;
                     $data->form_names[$submission->submission_id] = $form_name;
                     $i++;
                 }
                 $settings = new RM_Options();
                 $data->date_format = get_option('date_format');
                 $data->payments = $service->get_payments_by_submission_id($submission_ids, 999999, 0, null, true);
                 $i = 0;
                 if ($data->payments) {
                     foreach ($data->payments as $p) {
                         $data->payments[$i]->total_amount = $settings->get_formatted_amount($data->payments[$i]->total_amount, $data->payments[$i]->currency);
                         $i++;
                     }
                 }
                 //For pagination of payments
                 $entries_per_page_pay = 20;
                 $req_page_pay = isset($request->req['rm_reqpage_pay']) && $request->req['rm_reqpage_pay'] > 0 ? $request->req['rm_reqpage_pay'] : 1;
                 $data->offset_pay = $offset_pay = ($req_page_pay - 1) * $entries_per_page_pay;
                 $total_entries_pay = $i;
                 $data->total_pages_pay = (int) ($total_entries_pay / $entries_per_page_pay) + ($total_entries_pay % $entries_per_page_pay == 0 ? 0 : 1);
                 $data->curr_page_pay = $req_page_pay;
                 $data->starting_serial_number_pay = $offset_pay + 1;
                 $data->end_offset_this_page = $data->curr_page_pay < $data->total_pages_pay ? $data->offset_pay + $entries_per_page_pay : $total_entries_pay;
                 //Pagination Ends payments
                 //$data->rm_slug = $request->req['page'];
                 //$data->stat_data = $service->get_form_stats($data->current_form_id, $offset, $entries_per_page);
                 $data->total_pages_sub = (int) ($total_entries_sub / $entries_per_page_sub) + ($total_entries_sub % $entries_per_page_sub == 0 ? 0 : 1);
                 $data->curr_page_sub = $req_page_sub;
                 $data->starting_serial_number_sub = $offset_sub + 1;
                 //Pagination Ends submissions
                 $data->active_tab_index = isset($request->req['rm_tab']) ? (int) $request->req['rm_tab'] : 0;
                 $view = $this->mv_handler->setView('front_submissions', true);
                 return $view->read($data);
             } elseif ($data->is_user === true) {
                 $data->payments = false;
                 $data->submissions = false;
                 $view = $this->mv_handler->setView('front_submissions', true);
                 return $view->read($data);
             } else {
                 $view = $this->mv_handler->setView('not_authorized', true);
                 $msg = RM_UI_Strings::get('MSG_NO_SUBMISSION_FRONT');
                 return $view->read($msg);
             }
         }
     } else {
         $view = $this->mv_handler->setView('not_authorized', true);
         $msg = RM_UI_Strings::get('MSG_NOT_AUTHORIZED');
         return $view->read($msg);
     }
 }
 public function process($model, $service, $request, $params)
 {
     if ($service->is_ip_banned()) {
         return $this->banned_view();
     }
     global $rm_form_diary;
     if (count($rm_form_diary) > 0 && !isset($params['force_enable_multiform'])) {
         return;
     }
     if (isset($params['form_id']) && $params['form_id']) {
         $form_id = $params['form_id'];
         $fe_form = $this->form_factory->create_form($form_id);
         $form_name = 'form_' . $fe_form->get_form_id();
     } else {
         return;
     }
     $fopts = $fe_form->get_form_options();
     if ($fe_form->is_expired() && $fopts->post_expiry_action == 'switch_to_another_form') {
         $form_id = $fopts->post_expiry_form_id;
         if ($form_id) {
             $fe_form = $this->form_factory->create_form($form_id);
             $form_name = 'form_' . $fe_form->get_form_id();
         } else {
             return;
         }
     }
     if (isset($request->req['rm_pproc'])) {
         $paypal_service = new RM_Paypal_Service();
         ob_start();
         $resp = $paypal_service->callback($request->req['rm_pproc'], isset($request->req['rm_pproc_id']) ? $request->req['rm_pproc_id'] : null);
         $paypal_callback_msg = ob_get_clean();
         $x = new stdClass();
         $x->form_options = $fe_form->get_form_options();
         $x->form_name = $fe_form->get_form_name();
         $after_sub_msg = $service->after_submission_proc($x);
         return $paypal_callback_msg . '<br><br>' . $after_sub_msg;
     }
     //Call form specific processing before submission.
     $form_preproc_response = $fe_form->pre_sub_proc($request->req, $params);
     if (isset($request->req['stat_id'])) {
         $stat_id = $request->req['stat_id'];
     } else {
         $stat_id = null;
     }
     if (isset($request->req['rm_form_sub_no']) && $request->req['rm_form_sub_no']) {
         $subbed_form_no = $request->req['rm_form_sub_no'];
     } else {
         $subbed_form_no = null;
     }
     if ($subbed_form_no && $fe_form->get_form_number() == $subbed_form_no && $form_preproc_response && $this->mv_handler->validateForm($form_name . "_" . $subbed_form_no) && !$service->is_browser_reload_duplication($stat_id)) {
         if ($service->is_off_limit_submission($form_id, $fe_form->get_form_options())) {
             die(RM_UI_Strings::get("ALERT_SUBMISSIOM_LIMIT"));
         }
         $primary_data = $fe_form->get_prepared_data($request->req, 'primary');
         $service->update_stat_entry($stat_id);
         $db_data = $fe_form->get_prepared_data($request->req, 'dbonly');
         $sub_detail = $service->save_submission($form_id, $db_data, $primary_data['user_email']->value);
         $form_options = $fe_form->get_form_options();
         if ((int) $form_options->should_export_submissions === 1) {
             $service->export_to_external_url($form_options->export_submissions_to_url, $db_data);
         }
         if ($form_options->form_is_unique_token) {
             $token = $sub_detail->token;
         } else {
             $token = null;
         }
         if ($form_options->form_should_send_email) {
             $parameters = new stdClass();
             //This is different then the $params in the argument of this function!
             $parameters->req = $request->req;
             $parameters->email = $primary_data['user_email']->value;
             $parameters->email_content = $form_options->form_email_content;
             $parameters->email_subject = $form_options->form_email_subject;
             $email = $service->prepare_email('to_registrar', $token, $parameters);
             RM_Utilities::send_mail($email);
         }
         $submissions = new RM_Submissions();
         $submissions->load_from_db($sub_detail->submission_id);
         $parameters = new stdClass();
         $parameters->sub_data = $submissions->get_data();
         $parameters->form_name = $fe_form->get_form_name();
         $email = $service->prepare_email('to_admin', $token, $parameters);
         RM_Utilities::send_mail($email);
         $params['sub_detail'] = $sub_detail;
         /*
          * Check for payment
          */
         //also call Form specific method after submission
         $prevent_redirection = false;
         if ($fe_form->has_price_field()) {
             $params['paystate'] = 'pre_payment';
             $fe_form->post_sub_proc($request->req, $params, false);
             $params['is_paid'] = $service->process_payment($fe_form, $request, $params);
             if ($params['is_paid'] === 'do_not_redirect') {
                 $params['paystate'] = 'post_payment';
                 $fe_form->post_sub_proc($request->req, $params, false);
                 $this->update_user_profile($primary_data['user_email']->value, $db_data, $service);
                 $prevent_redirection = true;
             } else {
                 $params['paystate'] = 'post_payment';
                 $fe_form->post_sub_proc($request->req, $params, $params['is_paid']);
                 $this->update_user_profile($primary_data['user_email']->value, $db_data, $service);
             }
         } else {
             $params['paystate'] = 'na';
             $fe_form->post_sub_proc($request->req, $params);
             $this->update_user_profile($primary_data['user_email']->value, $db_data, $service);
         }
         unset($parameters->sub_data);
         $parameters->form_options = $form_options;
         if (!$prevent_redirection) {
             return $service->after_submission_proc($parameters);
         }
         //This must be returned as there is no ob_start here at work.
     } else {
         $data = new stdClass();
         $data->stat_id = $service->create_stat_entry($params);
         $data->fe_form = $fe_form;
         $force_multiple_form = isset($params['force_enable_multiform']) ? true : false;
         $view = $this->mv_handler->setView("user_form_nexgen", true);
         return $view->read($data, $force_multiple_form);
     }
 }
 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);
 }