Skip to content

userImport class

Início / Optitravel / Administration / User Import

UserImport

This class has the function of validating the user data to be imported, as well as the function of importing users. It receives the the module class object and the system_id per parameter in the constructor.

Class Path

includes/classes/system/Administration/UserImport.php

Methods

public function validateUser

This method receives a user, an administration class object and an array of stores per parameter. The function will iterate all user data and perform the respective validations to understand whether the data is valid or not. In the end it returns an OK or a KO.

public function validateUser($user,$administration,$stores){

        $rs_valid  = $administration->get_users(['*'], ['username'   => $user['Utilizador'],
        'system_id'   => strtoupper($_SESSION['kt_system_id']) ], null, false);

        $totalRows_valid = $rs_valid->RecordCount();

        if ($totalRows_valid < 1) {

            $message = '';
            $valid = 1;

            //Check is name in not empty
            if (empty($user['Nome'])) {
                $valid = 0; 
                $message .='O Nome n&atilde;o pode ser vazio|';   
            }

            //Check if name is administrador
            if ( $user['Nome'] == 'administrador') {
                $valid = 0;
                $message .='O Nome n&atilde;o pode ter a designa&ccedil;&atilde;o indicada|';    
            }

            //Check if username in not empty
            if (empty($user['Utilizador'])) {
                $valid = 0;
                $message .='O Utilizador n&atilde;o pode ser vazio|';    
            }

            //Check if username is admin
            if ( $user['Utilizador'] == 'admin') {
                $valid = 0;
                $message .='O Utilizador n&atilde;o pode ter a designa&ccedil;&atilde;o indicada|';    
            }

            //Check if email is valid
            if (!filter_var($user['Email'], FILTER_VALIDATE_EMAIL)) {
                $valid = 0;
                $message .='O Email n&atilde;o &eacute; v&aacute;lido|';    
            }

            //Check if Perfil is a valid perfil
            if (!in_array($user['Perfil'], $this->perfis)) {
                $valid = 0;
                $message .='O Perfil n&atilde;o &eacute; v&aacute;lido|';     
            }

            //Check if Balcao is a valid Balcao
            if (!in_array($user['Balcao'], $stores)) {
                $valid = 0;
                $message .='O Balc&atilde;o n&atilde;o &eacute; v&aacute;lido|';     
            }

            $this->$message = array(
                'status' => ($valid == 1 ? 'OK' : 'KO'),
                'msg' => $message
            );
            return $this->$message;

        } else {

            $this->$message = array(
                'status' => 'KO',
                'msg'    => 'O Utilizador j&aacute; existe|'
            );
            return $this->$message;

        }

    }

public function import

This method receives a user and an administration class object per parameter. The function will map attributes and call the insert_users function of the administration class to insert the user.

public function import($user,$administration){

        $user_template = '';
        $access_level = 0;

        $user_template = $this->perfisInfo[$user['Perfil']]['template'];
        $access_level = $this->perfisInfo[$user['Perfil']]['access_level'];

        $store_default = $user['Balcao'];
        $username      = $user['Utilizador'];
        $name          = $user['Nome'];
        $email         = $user['Email'];
        $user_location = 0;
        $user_series   = 0;

        $administration->insert_users(
            [
                'username'          => $username,
                'name'              => $name,
                'email'             => $email,
                'password'          => $username,
                'access_level'      => $access_level,
                'user_location'     => $user_location,
                'user_template'     => $user_template,
                'status'            => 'A',
                'system_id'         => strtoupper($_SESSION['kt_system_id']),
                'user_series'       => $user_series,
                'operator_block'    => 'NO',
                'store_default'     => $store_default,
                'contact'           => null
            ],
            false
        );

    }

private function setPerfisInfo

This method will fetch the information contained in PROFILESINFO from modules_settings and will fill in the profilesInfo and profiles variables.

private function setPerfisInfo(){

        $settings = $this->module->get_settings();
        $profiles_raw_data = explode(";", $settings['PROFILESINFO']);

        foreach ($profiles_raw_data as $raw_profile) {

            $pefil_data = explode(":",$raw_profile);
            $pefil_info = explode("|",$pefil_data[1]);

            //Add profile info to perfisInfo array
            $this->perfisInfo[$pefil_data[0]] = array(
                'template'     => $pefil_info[0],
                'access_level' => $pefil_info[1]
            );

            //Add profile name to perfis array
            array_push($this->perfis, $pefil_data[0]);

        }

    }

(Última atualização: 13/01/2025)