/** * Import users * * This function is used to import users from the given CSV * file. * <br/>Example: * <code> * $file = new EfrontFile(/var/www/efront/upload/admin/temp/users.csv); * EfrontSystem :: importUsers($file); * </code> * * @param mixed $file The CVS file with the users, either an EfrontFile object or the full path to the file * @param boolean $replaceUsers Whether to replace existing users having the same name as the ones imported * @return array The imported users in an array of EfrontUser objects * @since 3.5.0 * @access public */ public static function importUsers($file, $replaceUsers = false) { if (!$file instanceof EfrontFile) { $file = new EfrontFile($file); } $usersTable = eF_getTableData("users", "*", ""); $tableFields = array_keys($usersTable[0]); // Get user types to check if they exist $userTypesTable = eF_getTableData("user_types", "*", ""); // Set the userTypesTable to find in O(1) the existence or not of a user-type according to its name foreach ($userTypesTable as $key => $userType) { $userTypesTable[$userType['name']] = $userType; } // If we work on the enterprise version we need to distinguish between users and module_hcd_employees tables fields //$userFields = array('login', 'password','email','languages_NAME','name','surname','active','comments','user_type','timestamp','avatar','pending','user_types_ID'); $userFields = eF_getTableFields('users'); $existingUsers = eF_getTableDataFlat("users", "login"); $fileContents = file_get_contents($file['path']); $fileContents = explode("\n", trim($fileContents)); $separator = ";"; //$fields = explode($separator, trim($fileContents[0])); $fields = str_getcsv(trim($fileContents[0]), $separator); if (sizeof($fields) == 1) { $separator = ","; //$fields = explode($separator, $fileContents[0]); $fields = str_getcsv(trim($fileContents[0]), $separator); if (sizeof($fields) == 1) { throw new Exception(_UNKNOWNSEPARATOR, EfrontSystemException::ILLEGAL_CSV); } } foreach ($fields as $key => $value) { if (empty($value)) { $unused = $key; unset($fields[$key]); } } $inserted = 0; $matched = array_intersect($fields, $tableFields); $newUsers = array(); $messages = array(); // The check here is removed to offer interoperability between enterprise and educational versions // throw new Exception (_PLEASECHECKYOURCSVFILEFORMAT, EfrontSystemException::ILLEGAL_CSV); for ($i = 1; $i < sizeof($fileContents); $i++) { //$csvUser = explode($separator, $fileContents[$i]); $csvUser = str_getcsv($fileContents[$i], $separator); unset($csvUser[$unused]); if (sizeof($csvUser) != sizeof($fields)) { throw new Exception(_PLEASECHECKYOURCSVFILEFORMAT . ': ' . _NUMBEROFFIELDSMUSTBE . ' ' . sizeof($fields) . ' ' . _BUTFOUND . ' ' . sizeof($csvUser), EfrontSystemException::ILLEGAL_CSV); } $csvUser = array_combine($fields, $csvUser); array_walk($csvUser, create_function('&$v, $k', '$v=trim($v);')); if (in_array($csvUser['login'], $existingUsers['login']) && $replaceUsers) { $existingUser = EfrontUserFactory::factory($csvUser['login']); $existingUser->delete(); } if (!in_array($csvUser['login'], $existingUsers['login']) || $replaceUsers) { if (!isset($csvUser['password']) || !$csvUser['password']) { $csvUser['password'] = $csvUser['login']; } // Check the user-type existence by name if ($csvUser['user_type_name'] != "" && isset($userTypesTable[$csvUser['user_type_name']])) { // If there is a mismatch between the imported custom type basic type and the current basic type // then set no custom type if ($userTypesTable[$csvUser['user_type_name']]['basic_user_type'] != $csvUser['user_type']) { $csvUser['user_types_ID'] = 0; } else { $csvUser['user_types_ID'] = $userTypesTable[$csvUser['user_type_name']]['id']; } } else { $csvUser['user_types_ID'] = 0; } unset($csvUser['user_type_name']); if (!$csvUser['user_type']) { $csvUser['user_type'] = 'student'; } //If user type is not valid, don't insert that user if ($csvUser['user_type'] != "administrator" && $csvUser['user_type'] != "professor" && $csvUser['user_type'] != "student") { $messages[] = '"' . $csvUser['login'] . '": ' . _INVALIDUSERTYPE; unset($csvUser); continue; } // If we are not in enterprise version then $csvEmployeeProperties is used as a buffer // This is done to enable enterprise <-> Enteprise, educational <-> educational, enterprise <-> educational imports/exports $csvEmployeeProperties = $csvUser; if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE // Copy all fields and remove the user ones -> leaving only employee related fields $csvEmployeeProperties['users_login'] = $csvUser['login']; } #cpp#endif // Delete and recreate $csvUser to keep only the fields in userFields unset($csvUser); foreach ($userFields as $field) { if (isset($csvEmployeeProperties[$field])) { $csvUser[$field] = $csvEmployeeProperties[$field]; if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE unset($csvEmployeeProperties[$field]); } #cpp#endif } } try { if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE $user = EfrontUser::createUser($csvUser); if (isset($csvEmployeeProperties['branch_name'])) { $result = eF_getTableData("module_hcd_branch", "branch_ID", "name='" . $csvEmployeeProperties['branch_name'] . "'"); if ($result[0]['branch_ID']) { $branchId = $result[0]['branch_ID']; } unset($csvEmployeeProperties['branch_name']); } if (isset($csvEmployeeProperties['job_name'])) { $result = eF_getTableData("module_hcd_job_description", "job_description_ID", "description='" . $csvEmployeeProperties['job_name'] . "'"); if ($result[0]['job_description_ID']) { $jobId = $result[0]['job_description_ID']; } unset($csvEmployeeProperties['job_name']); } if (isset($csvEmployeeProperties['job_role'])) { $csvEmployeeProperties['job_role'] ? $jobRole = 1 : ($jobRole = 0); unset($csvEmployeeProperties['job_role']); } $user->aspects['hcd'] = EfrontHcdUser::createUser($csvEmployeeProperties); if (isset($branchId) && isset($jobId) && isset($jobRole)) { $user->aspects['hcd']->addJob($user, $jobId, $branchId, $jobRole); } $newUsers[] = $user; } else { #cpp#else $newUsers[] = EfrontUser::createUser($csvUser); } #cpp#endif } catch (Exception $e) { $messages[] = '"' . $csvUser['login'] . '": ' . $e->getMessage() . ' (' . $e->getCode() . ')'; } } } return array($newUsers, $messages); }
} } $newUser = EfrontUser::createUser($user_data); $encrypted = true; //needed for autologin EfrontEvent::triggerEvent(array("type" => EfrontEvent::SYSTEM_REGISTER, "users_LOGIN" => $user_data['login'], "users_name" => $user_data['name'], "users_surname" => $user_data['surname'], "entity_name" => $user_data['password'])); // send not-visited notifications for the newly registered user //EfrontEvent::triggerEvent(array("type" => (-1) * EfrontEvent::SYSTEM_VISITED, "users_LOGIN" => $user_data['login'], "users_name" => $user_data['name'], "users_surname" => $user_data['surname'])); if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE try { if (isset($groupToAdd)) { $groupToAdd->addUsers($values['login']); } $new_employees_content['users_login'] = $values['login']; EfrontHcdUser::createUser($new_employees_content); foreach ($self_registered_jobs as $job_assigned) { if ($job_assigned['branch_ID'] != 0 || $job_assigned['mandatory']) { if (!$job_assigned['job_description']) { $job_assigned['job_description'] = _NOSPECIFICJOB; } // If the selected pair of branch-job description doesn't exist then insert it $new_job_description_ID = eF_getJobDescriptionId($job_assigned['job_description'], $job_assigned['branch_ID']); // Only assigned as employees $employee = new EfrontEmployee($new_employees_content, array()); $newUser = EfrontUserFactory::factory($values['login']); //$encrypted = false; //the factory above changed the user password mode if ($configuration['supervisor_mail_activation'] == 1 && $job_assigned['supervisor'] != "") { $activating_supervisor_found = true; $employee->addJob($newUser, $new_job_description_ID, $job_assigned['branch_ID'], 0, false, array("manager" => $job_assigned['supervisor'], "timestamp" => $newUser->user['timestamp'])); } else {
} } if (isset($selectedAvatar)) { $selectedAvatar = $avatarsFileSystemTree->seekNode(G_SYSTEMAVATARSPATH . $selectedAvatar); $newList = FileSystemTree::importFiles($selectedAvatar['path']); //Import the file to the database, so we can access it with view_file $editedUser->user['avatar'] = key($newList); } } EfrontEvent::triggerEvent(array("type" => EfrontEvent::AVATAR_CHANGE, "users_LOGIN" => $editedUser->user['login'], "users_name" => $editedUser->user['name'], "users_surname" => $editedUser->user['surname'], "lessons_ID" => 0, "lessons_name" => "", "entity_ID" => $editedUser->user['avatar'])); } $editedUser->persist(); if (G_VERSIONTYPE == 'enterprise') { #cpp#ifdef ENTERPRISE if (isset($_GET['add_user'])) { $editedEmployee = EfrontHcdUser::createUser(array('users_login' => $editedUser->user['login'])); if ($currentEmployee->isSupervisor() && !EfrontUser::isOptionVisible('show_unassigned_users_to_supervisors')) { //if supervisors can't see unassigned users, then attach this new user to the supervisor's firts branch and job $branch = new EfrontBranch(current($currentEmployee->getSupervisedBranchesRecursive())); $nospecific = false; foreach ($branch->getJobDescriptions() as $value) { if ($value['description'] == _NOSPECIFICJOB) { $nospecific = $value['job_description_ID']; } } if (!$nospecific) { $nospecific = EfrontJob::createJob(array('description' => _NOSPECIFICJOB, 'branch_ID' => $branch->branch['branch_ID'])); } $editedEmployee->addJob($editedUser, $nospecific, $branch->branch['branch_ID'], 0); } }