You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
6.6 KiB
180 lines
6.6 KiB
<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
use Sirprize\PostalCodeValidator\Validator;
|
|
use app\core\utils\Response;
|
|
use app\core\auth\User as UserAuth;
|
|
use app\core\auth\Unregister;
|
|
use app\core\utils\Helper;
|
|
|
|
class MyAccount extends MY_Controller {
|
|
|
|
public function __construct()
|
|
{
|
|
$this->frontoffice_parent_controller();
|
|
$this->load_language_frontoffice();
|
|
$this->lang->load('frontoffice/my_account', 'fr');
|
|
$this->load->model('user_subscriber_model');
|
|
$this->load->model('user_model');
|
|
$this->load->model("user_activity_log_model");
|
|
$this->load->helper('htmlpurifier');
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
Response::handleSessionTimeout("fo");
|
|
|
|
$user = $this->security->xss_clean(html_escape($this->input->post()));
|
|
|
|
try {
|
|
|
|
$this->validateData('create', $user);
|
|
|
|
$userId = $this->user_model->create_new_user([
|
|
'first_name' => $user['first_name'] ? Helper::ucWords($user['first_name']) : null,
|
|
'last_name' => $user['last_name'] ? Helper::ucWords($user['last_name']) : null,
|
|
'email_address' => $user['email_address'] ?? null,
|
|
'role_id' => USER_ROLES['regular'],
|
|
'status' => 1
|
|
]);
|
|
|
|
if (!$userId)
|
|
output_to_json($this, [
|
|
'mtype' => 'error',
|
|
'message' => 'Unable to register account!'
|
|
]);
|
|
|
|
$this->user_subscriber_model->add_subscriber_data([
|
|
'subscriber' => $userId,
|
|
'civility' => isset($user['civility']) ? Helper::ucWords($user['civility']) : null,
|
|
'address' => isset($user['address']) ? Helper::ucWords($user['address']) : null,
|
|
'postal_code' => $user['postal_code'] ?? null,
|
|
'city' => $user['city'] ? Helper::ucWords($user['city'] ) : null,
|
|
'phone' => $user['phone'] ?? null,
|
|
]);
|
|
|
|
|
|
$this->user_activity_log_model->add_activity_log(array(
|
|
"description" => "Add new user - ".$user['email_address'],
|
|
"user_id" => $userId,
|
|
"action" => "ADD",
|
|
"table_origin" => "user",
|
|
"reference_id" => $userId
|
|
));
|
|
|
|
output_to_json($this, [
|
|
'mtype' => 'success',
|
|
'message' => 'User registered successfully!'
|
|
]);
|
|
} catch(\Exception $e) {
|
|
output_to_json($this, [
|
|
'mtype' => 'error',
|
|
'message' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function update(int $userId = 0)
|
|
{
|
|
Response::handleSessionTimeout("fo");
|
|
|
|
if ((int) $this->data['logged_in']['user_id'] !== $userId)
|
|
output_to_json($this, [
|
|
'mtype' => 'error',
|
|
'message' => 'Unable to update user profile, user id not found.'
|
|
]);
|
|
|
|
$user = $this->input->post();
|
|
|
|
try {
|
|
$this->validateData('update', $user);
|
|
|
|
$cleanUserData = [
|
|
'first_name' => $user['first_name'] ? Helper::ucWords(strip_tags($user['first_name'])) : null,
|
|
'last_name' => $user['last_name'] ? Helper::ucWords(strip_tags($user['last_name'])) : null,
|
|
'civility' => isset($user['civility']) ? Helper::ucWords(strip_tags($user['civility'])) : null,
|
|
'address' => isset($user['address']) ? Helper::ucWords(strip_tags($user['address'])) : null,
|
|
'postal_code' => strip_tags($user['postal_code']) ?? null,
|
|
'city' => strip_tags($user['city']) ? Helper::ucWords($user['city'] ) : null,
|
|
'phone' => strip_tags($user['phone']) ?? null,
|
|
];
|
|
|
|
$this->user_model->put_subscribers($userId, $cleanUserData);
|
|
|
|
// Update name in session
|
|
UserAuth::updateSession('first_name', $cleanUserData['first_name']);
|
|
UserAuth::updateSession('last_name', $cleanUserData['last_name']);
|
|
|
|
$this->user_activity_log_model->add_activity_log(array(
|
|
"description" => "EDIT account - ".$user['email_address'],
|
|
"user_id" => $userId,
|
|
"action" => "EDIT",
|
|
"table_origin" => "user",
|
|
"reference_id" => $userId
|
|
));
|
|
|
|
output_to_json($this, [
|
|
'mtype' => 'success',
|
|
'message' => 'User account updated successfully!'
|
|
]);
|
|
} catch(\Exception $e) {
|
|
output_to_json($this, [
|
|
'mtype' => 'error',
|
|
'message' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function getPhoneNumberFormat($countryCode){
|
|
$phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
|
|
$phoneObj = ($phoneNumberUtil->getExampleNumberForType($countryCode, \libphonenumber\PhoneNumberType::MOBILE));
|
|
$countryCode = "";
|
|
$number = "";
|
|
if ($phoneObj !== null) {
|
|
$countryCode = $phoneObj->getCountryCode();
|
|
$number = $phoneObj->getNationalNumber();
|
|
}
|
|
$phoneFormat = [
|
|
"code" => $countryCode,
|
|
"format" => str_repeat("X", strlen($number)),
|
|
];
|
|
output_to_json($this, $phoneFormat);
|
|
}
|
|
|
|
public function validateFormat($type = "postal") {
|
|
switch ($type) {
|
|
case 'postal':
|
|
$postal = $this->input->get('postal');
|
|
$countryCode = $this->input->get('countryCode');
|
|
output_to_json($this, ['isValid' => validatePostalCode($countryCode, $postal)]);
|
|
break;
|
|
case 'phone':
|
|
$phone = $this->input->get('phone');
|
|
$countryCode = $this->input->get('countryCode');
|
|
output_to_json($this, ['isValid' => validatePhoneNumber($countryCode, $phone)]);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Generate success message
|
|
*/
|
|
public function genereateSuccessMessage($data)
|
|
{
|
|
// mise = female/general, mis = male
|
|
$salutation = strtolower($data['salut']) === 'monsieur' ? 'mis':'mise';
|
|
return str_replace('{gender}', $salutation, $this->lang->line(strtolower($data['message'])));
|
|
}
|
|
|
|
private function validateData($action = 'create', $data) {
|
|
|
|
// Check if email exists
|
|
if ($action === 'create' && $this->user_subscriber_model->is_email_exists($data['email_address']))
|
|
output_to_json($this, [
|
|
'mtype' => 'error',
|
|
'message' => 'Email already registered!'
|
|
]);
|
|
}
|
|
}
|
|
|