function import($filehandle, $group) { $csvtable = new Csvtable($filehandle); $requiredcolumns = array('INCIDENT TITLE', 'INCIDENT DATE'); foreach ($requiredcolumns as $requiredcolumn) { if (!$csvtable->hasColumn($requiredcolumn)) { $this->errors[] = 'CSV file is missing required column "' . $requiredcolumn . '"'; } } if (count($this->errors)) { return false; } $category_ids_temp = ORM::factory('category')->select_list('category_title', 'id'); // so we can assign category id to incidents, based on category title $group_category_ids_temp = ORM::factory('simplegroups_category')->where("simplegroups_groups_id", $group->id)->select_list('category_title', 'id'); // so we can assign category id to incidents, based on category title //make trim and capitalize the categories so they will match if the case is different. $this->category_ids = array(); foreach ($category_ids_temp as $cat => $id) { $this->category_ids[strtoupper(trim($cat))] = $id; } $this->group_category_ids = array(); foreach ($group_category_ids_temp as $cat => $id) { $this->group_category_ids[strtoupper(trim($cat))] = $id; } $this->incident_ids = ORM::factory('incident')->select_list('id', 'id'); // so we can check if incident already exists in database $this->time = date("Y-m-d H:i:s", time()); $rows = $csvtable->getRows(); $this->totalrows = count($rows); $this->rownumber = 0; foreach ($rows as $row) { $this->rownumber++; if (isset($row['#']) and isset($this->incident_ids[$row['#']])) { $this->notices[] = 'Incident with id #' . $row['#'] . ' already exists.'; } else { if ($this->importreport($row, $group)) { $this->importedrows++; } else { $this->rollback(); return false; } } } // loop through CSV rows return true; }
/** * Function to import CSV file referenced by the file handle * @param string $filehandle * @return bool */ function import($filehandle) { $csvtable = new Csvtable($filehandle); // Set the required columns of the CSV file $requiredcolumns = array('INCIDENT TITLE', 'INCIDENT DATE'); foreach ($requiredcolumns as $requiredcolumn) { // If the CSV file is missing any required column, return an error if (!$csvtable->hasColumn($requiredcolumn)) { $this->errors[] = 'CSV file is missing required column "' . $requiredcolumn . '"'; } } if (count($this->errors)) { return false; } // So we can assign category id to incidents, based on category title $this->category_ids = ORM::factory('category')->select_list('category_title', 'id'); //Since we capitalize the category names from the CSV file, we should also capitlize the //category titles here so we get case insensative behavior. For some reason users don't //always captilize the cateogry names as they enter them in $temp_cat = array(); foreach ($this->category_ids as $key => $value) { $temp_cat[strtoupper($key)] = $value; } $this->category_ids = $temp_cat; // So we can check if incident already exists in database $this->incident_ids = ORM::factory('incident')->select_list('id', 'id'); $this->time = date("Y-m-d H:i:s", time()); $rows = $csvtable->getRows(); $this->totalrows = count($rows); $this->rownumber = 0; // Loop through CSV rows foreach ($rows as $row) { $this->rownumber++; if (isset($row['#']) and isset($this->incident_ids[$row['#']])) { $this->notices[] = 'Incident with id #' . $row['#'] . ' already exists.'; } else { if ($this->importreport($row)) { $this->importedrows++; } else { $this->rollback(); return false; } } } return true; }
/** * Function to import CSV file referenced by the file handle * @param string $filehandle * @return bool */ function import($file) { // Get contents of CSV file $data = file_get_contents($file); // Replace carriage return character $replacedata = preg_replace("/\r\n/", "\n", $data); // Replace file content file_put_contents($file, $replacedata); if ($filehandle = fopen($_FILES['uploadfile']['tmp_name'], 'r')) { $csvtable = new Csvtable($filehandle); // Set the required columns of the CSV file $requiredcolumns = array('INCIDENT TITLE', 'INCIDENT DATE'); foreach ($requiredcolumns as $requiredcolumn) { // If the CSV file is missing any required column, return an error if (!$csvtable->hasColumn($requiredcolumn)) { $this->errors[] = Kohana::lang('import.csv.required_column') . '"' . $requiredcolumn . '"'; } } if (count($this->errors)) { return false; } // So we can assign category id to incidents, based on category title $this->existing_categories = ORM::factory('category')->select_list('category_title', 'id'); //Since we capitalize the category names from the CSV file, we should also capitlize the //category titles here so we get case insensative behavior. For some reason users don't //always captilize the cateogry names as they enter them in $temp_cat = array(); foreach ($this->existing_categories as $title => $id) { $temp_cat[utf8::strtoupper($title)] = $id; } $this->existing_categories = $temp_cat; // So we can check if incident already exists in database $this->incident_ids = ORM::factory('incident')->select_list('id', 'id'); $this->time = date("Y-m-d H:i:s", time()); $rows = $csvtable->getRows(); $this->totalreports = count($rows); $this->rownumber = 0; // Loop through CSV rows foreach ($rows as $row) { $this->rownumber++; if (isset($row['#']) and isset($this->incident_ids[$row['#']])) { $this->notices[] = Kohana::lang('import.incident_exists') . $row['#']; } else { if ($this->import_report($row)) { $this->importedreports++; } else { $this->rollback(); return false; } } } } else { $this->errors[] = Kohana::lang('ui_admin.file_open_error'); } // If we have errors, return FALSE, else TRUE return count($this->errors) === 0; }
/** * Function to import CSV file referenced by the file handle * @param string $filehandle * @return bool */ function import($filehandle) { $csvtable = new Csvtable($filehandle); // Set the required columns of the CSV file $requiredcolumns = array('INCIDENT TITLE', 'INCIDENT DATE'); foreach ($requiredcolumns as $requiredcolumn) { // If the CSV file is missing any required column, return an error if (!$csvtable->hasColumn($requiredcolumn)) { $this->errors[] = 'CSV file is missing required column "' . $requiredcolumn . '"'; } } if (count($this->errors)) { return false; } // So we can assign category id to incidents, based on category title $this->category_ids = ORM::factory('category')->select_list('category_title', 'id'); // So we can check if incident already exists in database $this->incident_ids = ORM::factory('incident')->select_list('id', 'id'); $this->time = date("Y-m-d H:i:s", time()); $rows = $csvtable->getRows(); $this->totalrows = count($rows); $this->rownumber = 0; // Loop through CSV rows foreach ($rows as $row) { $this->rownumber++; if (isset($row['#']) and isset($this->incident_ids[$row['#']])) { $this->notices[] = 'Incident with id #' . $row['#'] . ' already exists.'; } else { if ($this->importreport($row)) { $this->importedrows++; } else { $this->rollback(); return false; } } } return true; }
/** * Function to import CSV file referenced by the file handle * @param string $filehandle * @return bool */ function import($file) { // Get contents of CSV file $data = file_get_contents($file); // Normalize new lines, replace ANY unicode new line with \n (should cover Mac OS9, Unix, Windows, etc) $replacedata = preg_replace('/\\R/u', "\n", mb_convert_encoding($data, 'UTF-8')); // Check for preg error, and fall back to original data if (preg_last_error() !== PREG_NO_ERROR) { $replacedata = $data; } // Replace file content file_put_contents($file, $replacedata); if ($filehandle = fopen($_FILES['uploadfile']['tmp_name'], 'r')) { $csvtable = new Csvtable($filehandle); // Set the required columns of the CSV file $requiredcolumns = array('INCIDENT TITLE', 'INCIDENT DATE'); foreach ($requiredcolumns as $requiredcolumn) { // If the CSV file is missing any required column, return an error if (!$csvtable->hasColumn($requiredcolumn)) { $this->errors[] = Kohana::lang('import.csv.required_column') . '"' . $requiredcolumn . '"'; } } if (count($this->errors)) { return false; } // So we can assign category id to incidents, based on category title $this->existing_categories = ORM::factory('category')->select_list('category_title', 'id'); //Since we capitalize the category names from the CSV file, we should also capitlize the //category titles here so we get case insensative behavior. For some reason users don't //always captilize the cateogry names as they enter them in $temp_cat = array(); foreach ($this->existing_categories as $title => $id) { $temp_cat[utf8::strtoupper($title)] = $id; // Add translated titles too $langs = Category_Lang_Model::category_langs($id); if (isset($langs[$id])) { foreach ($langs[$id] as $l) { $temp_cat[utf8::strtoupper($l['category_title'])] = $id; } } } $this->existing_categories = $temp_cat; // So we can check if incident already exists in database $this->incident_ids = ORM::factory('incident')->select_list('id', 'id'); $this->time = date("Y-m-d H:i:s", time()); $rows = $csvtable->getRows(); $this->totalreports = count($rows); $this->rownumber = 0; // Loop through CSV rows foreach ($rows as $row) { $this->rownumber++; if (isset($row['#']) and isset($this->incident_ids[$row['#']])) { $this->notices[] = Kohana::lang('import.incident_exists') . $row['#']; } else { if ($this->import_report($row)) { $this->importedreports++; } else { $this->rollback(); return false; } } } } else { $this->errors[] = Kohana::lang('ui_admin.file_open_error'); } // If we have errors, return FALSE, else TRUE return count($this->errors) === 0; }