public function post($params)
{
$adm = $this->app->user_manager->is_admin();
if (defined('MW_API_CALL')) {
// $validate_token = $this->app->user_manager->csrf_validate($params);
// if (!$adm) {
// if ($validate_token == false) {
// return array('error' => 'Invalid token!');
// }
// }
}
$before_process = $this->app->event_manager->trigger('mw.forms_manager.before_post', $params);
if (is_array($before_process) and !empty($before_process)) {
foreach ($before_process as $before_process_item) {
if ($before_process_item === false) {
return;
}
}
}
$table = MW_DB_TABLE_FORMS_DATA;
mw_var('FORCE_SAVE', $table);
if (isset($params['id'])) {
if ($adm == false) {
return array('error' => 'Error: Only admin can edit forms!');
}
}
$for = 'module';
if (isset($params['for'])) {
$for = $params['for'];
}
if (isset($params['for_id'])) {
$for_id = $params['for_id'];
} elseif (isset($params['data-id'])) {
$for_id = $params['data-id'];
} elseif (isset($params['id'])) {
$for_id = $params['id'];
}
if (isset($params['rel_id'])) {
$for_id = $params['rel_id'];
}
if (!isset($for_id)) {
return array('error' => 'Please provide for_id parameter with module id');
}
$dis_cap = $this->app->option_manager->get('disable_captcha', $for_id) == 'y';
if ($dis_cap == false) {
if (!isset($params['captcha'])) {
return array('error' => 'Please enter the captcha answer!');
} else {
if ($for_id != false) {
$validate_captcha = mw()->captcha->validate($params['captcha'], $for_id);
} else {
$validate_captcha = mw()->captcha->validate($params['captcha']);
}
if (!$validate_captcha) {
if ($adm == false) {
return array('error' => 'Invalid captcha answer!', 'captcha_error' => true);
}
}
}
}
// if ($for=='module'){
$list_id = $this->app->option_manager->get('list_id', $for_id);
// }
$email_to = $this->app->option_manager->get('email_to', $for_id);
$email_bcc = $this->app->option_manager->get('email_bcc', $for_id);
$email_autorespond = $this->app->option_manager->get('email_autorespond', $for_id);
$email_autorespond_subject = $this->app->option_manager->get('email_autorespond_subject', $for_id);
$email_notification_subject = $this->app->option_manager->get('email_notification_subject', $for_id);
if (!isset($list_id) or $list_id == false) {
$list_id = 0;
}
$to_save = array();
$fields_data = array();
$get_fields = array();
$get_fields['rel_type'] = $for;
$get_fields['rel_id'] = $for_id;
$get_fields['return_full'] = true;
$more = $this->app->fields_manager->get($get_fields);
$cf_to_save = array();
if (!empty($more)) {
foreach ($more as $item) {
if (isset($item['name'])) {
$cfn = $item['name'];
$cfn2 = str_replace(' ', '_', $cfn);
if (isset($params[$cfn2]) and $params[$cfn2] != false) {
$fields_data[$cfn2] = $params[$cfn2];
$item['value'] = $params[$cfn2];
$cf_to_save[$cfn] = $item;
} elseif (isset($params[$cfn]) and $params[$cfn] != false) {
$fields_data[$cfn] = $params[$cfn];
$item['value'] = $params[$cfn2];
$cf_to_save[$cfn] = $item;
}
}
}
} else {
$cf_to_save = $params;
}
$to_save['list_id'] = $list_id;
$to_save['rel_id'] = $for_id;
$to_save['rel_type'] = $for;
$to_save['user_ip'] = MW_USER_IP;
if (isset($params['module_name'])) {
$to_save['module_name'] = $params['module_name'];
}
if (!empty($fields_data)) {
$to_save['form_values'] = json_encode($fields_data);
} else {
$to_save['form_values'] = json_encode($params);
}
$save = $this->app->database_manager->save($table, $to_save);
$event_params = $params;
$event_params['saved_form_entry_id'] = $save;
$this->app->event_manager->trigger('mw.forms_manager.after_post', $event_params);
if (isset($params['module_name'])) {
$pp_arr = $params;
$pp_arr['ip'] = MW_USER_IP;
unset($pp_arr['module_name']);
if (isset($pp_arr['rel_type'])) {
unset($pp_arr['rel_type']);
}
if (isset($pp_arr['rel_id'])) {
unset($pp_arr['rel_id']);
}
if (isset($pp_arr['list_id'])) {
unset($pp_arr['list_id']);
}
if (isset($pp_arr['for'])) {
unset($pp_arr['for']);
}
if (isset($pp_arr['for_id'])) {
unset($pp_arr['for_id']);
}
$notif = array();
$notif['module'] = $params['module_name'];
$notif['rel_type'] = 'forms_lists';
$notif['rel_id'] = $list_id;
$notif['title'] = 'New form entry';
$notif['description'] = $email_notification_subject ?: 'You have new form entry';
$notif['content'] = 'You have new form entry from ' . $this->app->url_manager->current(1) . '<br />' . $this->app->format->array_to_ul($pp_arr);
$this->app->notifications_manager->save($notif);
if ($email_to == false) {
$email_to = $this->app->option_manager->get('email_from', 'email');
}
$admin_user_mails = array();
if ($email_to == false) {
$admins = $this->app->user_manager->get_all('is_admin=1');
if (is_array($admins) and !empty($admins)) {
foreach ($admins as $admin) {
if (isset($admin['email']) and filter_var($admin['email'], FILTER_VALIDATE_EMAIL)) {
$admin_user_mails[] = $admin['email'];
$email_to = $admin['email'];
}
}
}
}
if ($email_to != false) {
$mail_autoresp = 'Thank you for your request!';
if ($email_autorespond != false) {
$mail_autoresp = $email_autorespond;
}
$user_mails = array();
if (isset($admin_user_mails) and !empty($admin_user_mails)) {
$user_mails = $admin_user_mails;
}
$user_mails[] = $email_to;
if (isset($email_bcc) and filter_var($email_bcc, FILTER_VALIDATE_EMAIL)) {
$user_mails[] = $email_bcc;
}
$email_from = false;
if (isset($cf_to_save) and !empty($cf_to_save)) {
foreach ($cf_to_save as $value) {
if (is_array($value) and isset($value['value'])) {
$to = $value['value'];
} else {
$to = $value;
}
if (isset($to) and filter_var($to, FILTER_VALIDATE_EMAIL)) {
$user_mails[] = $to;
$email_from = $to;
}
}
} else {
}
if (!empty($user_mails)) {
array_unique($user_mails);
$sender = new \Microweber\Utils\MailSender();
foreach ($user_mails as $value) {
if ($value == $email_to || $value == $email_bcc) {
$msg = $notif['content'];
$subj = $notif['description'];
$from = $email_from;
} else {
$msg = $mail_autoresp;
$subj = $email_autorespond_subject ?: 'Thank you!';
$from = false;
}
$sender->send($value, $subj, $msg, $from);
}
}
}
}
return $save;
}