/** * Import users from CSV file. * @author Josep Ferràndiz Farré (jferran6@xtec.cat) * **CSV File header** Allowed fields: uname,new_uname,email,password,forcechgpass,activated,firstname,lastname1,lastname2,birthdate,gender,code,in,out * Fields "in" and "out" contains group IDs where the user will be added or removed, separated by "|" char * Deletions will be made before insertions. * */ public function import(){ $this->throwForbiddenUnless(SecurityUtil::checkPermission('IWusers::', '::', ACCESS_ADMIN)); // Sincronize tables users & IWUsers ModUtil::apiFunc($this->name, 'admin', 'sincronize'); $view = Zikula_View::getInstance($this->name); if ($this->request->isPost()) { $this->checkCsrfToken(); $importFile = $this->request->files->get('importFile', null); $delimiter = $this->request->request->get('delimiter', ';'); // 1. Validate file: header & structure if (is_null($importFile)) { LogUtil::registerError($this->__('File error. Probably exceeds max. file size.')); } else { // Header fields table equivalents $headerTrans =array('uname' => 'uname', 'new_uname' => 'new_uname', 'email' => 'email', 'code' => 'code', 'password' => 'password', 'forcechgpass'=> 'forcechgpass', 'activated' => 'activated', 'firstname' => 'nom', 'lastname1' => 'cognom1', 'lastname2' => 'cognom2', 'description' => 'description', 'birthdate' => 'naixement', 'gender' => 'sex', 'in' => 'in', 'out' => 'out' ); $import = new CsvImporter($importFile['tmp_name'],true, $headerTrans, $delimiter); $header = $import->getHeader(); $oHeader = $import->getOriHeader(); $check = ModUtil::apiFunc($this->name, 'admin', 'checkCsvHeader', $header); // Check CSV file header if (!$check['correcte']) { // CSV header incorrect LogUtil::registerError($check['msg']); } else { // Get CSV file content $data = $import->get(); // Initialize $update = array(); $insert = array(); $optimizeGroups = ((in_array('in', $header)) || (in_array('out', $header))); $forcechgpass = in_array('forcechgpass', $header); $iErrors = array(); // Check file info foreach ($data as &$line) { $uid = UserUtil::getIdFromName($line['uname']); if ($optimizeGroups) { $res = ModUtil::apiFunc($this->name, 'admin', 'optimizeGroups', array('uid' => $uid, 'data'=> $line)); $line['in'] = $res['in']; $line['out'] = $res['out']; } if ($uid){ $line['uid'] = $uid; // Update if (isset($line['activated'])){ if ($line['activated']!=""){ $line['activated'] = $line['activated'] == '0' ? 0 : 1; } else { // Mantain actual value $line['activated'] = DBUtil::selectField('users', 'activated', 'uid='.$uid); } } $update[] = $line; } else { // Insert new user if ($line['uname'] !=""){ if (!isset($line['activated']) || (isset($line['activated']) && $line['activated']!='0')) $line['activated'] = 1; $insert[] = $line; } else { // Error. No username $line['error'] = $this->__('No username.'); $iErrors[] = $line; } } } // Process file information $allChanges = ModUtil::apiFunc($this->name, 'admin', 'applyCsvValues', array('update' => $update, 'insert' =>$insert)); unset($update); $update = array(); unset($insert); $insert = array(); foreach ($allChanges as $user){ switch ($user['action']){ case 'm': $update[] = $user; break; default: $insert[] = $user; } if (isset($user['error'])){ // There are errors $iErrors[] = $user; } } $view->assign('insert', $insert); $view->assign('update', $update); $view->assign('iErrors', $iErrors); $view->assign('header', $oHeader); $view->assign('hc', count($oHeader)); return $view->fetch('IWusers_admin_importResults.tpl'); } // File header is correct } // exist import file } // Is POST // Get zikula groups name and gid $g = UserUtil::getGroups('','name'); foreach ($g as $key => $value) { $groupInfo[] = array_slice($value,0,2); } $view->assign('post_max_size', ini_get('post_max_size')); $view->assign('groupInfo', $groupInfo); return $view->fetch('IWusers_admin_import.tpl'); }