Example #1
0
 /**
  *
  * Process CSV file
  *
  * @param $csv
  * @param $groups
  *
  * @return array()
  */
 private function processCsv($csv, $groups, $language)
 {
     $errorEmail = 0;
     $errorAlreadyExists = 0;
     $successInserted = 0;
     foreach ($csv as $row) {
         set_time_limit(30);
         if (filter_var($row['email'], FILTER_VALIDATE_EMAIL)) {
             //--Get user from e-mail
             $user = BackendMailengineModel::getUserFromEmail($row['email']);
             if (empty($user)) {
                 $data = array();
                 $data['email'] = $row['email'];
                 $data['name'] = !isset($row['name']) || $row['name'] == '' ? $row['email'] : $row['name'];
                 $data['language'] = $language;
                 //--Add user
                 $user = array();
                 $user['id'] = BackendMailengineModel::insertUser($data);
                 //--Add count for ok
                 $successInserted++;
             } else {
                 //--Add count for already exists
                 $errorAlreadyExists++;
             }
             //--Loop all the groups and add the user to the group
             foreach ($groups as $value) {
                 //--Check if user is already linked to the group
                 if (!BackendMailengineModel::existsUserGroup($user['id'], $value)) {
                     $groupUser = array();
                     $groupUser["user_id"] = $user['id'];
                     $groupUser["group_id"] = $value;
                     //--Add user to the group
                     BackendMailengineModel::insertUserToGroup($groupUser);
                 }
             }
         } else {
             $errorEmail++;
         }
     }
     $return = array('errorEmail' => $errorEmail, 'errorAlreadyExists' => $errorAlreadyExists, 'successInserted' => $successInserted);
     return $return;
 }