function processView() { $GLOBALS['system']->includeDBClass('service_component_category'); $this->category = new Service_Component_Category($_REQUEST['categoryid']); if (!empty($_FILES['datafile']) && !empty($_FILES['datafile']['tmp_name'])) { $GLOBALS['system']->doTransaction('BEGIN'); $GLOBALS['system']->includeDBClass('service_component'); $comp = new Service_Component(); $fp = fopen($_FILES['datafile']['tmp_name'], 'r'); if (!$fp) { trigger_error("Your data file could not be read. Please check the file and try again"); return; } $toprow = fgetcsv($fp, 0, ",", '"'); $rowNum = 1; $all_ccli = Service_Component::getAllByCCLINumber(); while ($row = fgetcsv($fp, 0, ",", '"')) { $comp->populate(0, array()); $this->_captureErrors(); $data = array(); foreach ($row as $k => $v) { $data[strtolower($toprow[$k])] = $v; } if (isset($data['content'])) { $c = trim($data['content']); $c = str_replace("\r", "", $c); $c = str_replace("\n\n", "</p><p>", $c); $c = str_replace("\n", "<br />", $c); $data['content_html'] = '<p>' . $c . '</p>'; unset($data['content']); } if (isset($data['show_in_handout'])) { $val = $data['show_in_handout']; $map = array('y' => 'full', 'n' => 0, 'yes' => 'full', 'no' => 0); $val = array_get($map, strtolower($val), $val); if (!in_array($val, array('0', 'title', 'full'))) { $val = '0'; } $data['show_in_handout'] = $val; } if (!empty($_REQUEST['dupe-match']) && !empty($data['ccli_number']) && isset($all_ccli[$data['ccli_number']])) { $comp->load($all_ccli[$data['ccli_number']]); $comp->fromCSVRow($data); foreach ($_REQUEST['congregationids'] as $congid) { $comp->addCongregation($congid); } $comp->save(); } else { $comp->fromCSVRow($data); $comp->setValue('categoryid', $_REQUEST['categoryid']); if ($errors = $this->_getErrors()) { $this->errors[$rowNum] = $errors; } else { foreach ($_REQUEST['congregationids'] as $congid) { $comp->addCongregation($congid); } $comp->create(); } } $rowNum++; } if (empty($this->errors)) { $GLOBALS['system']->doTransaction('COMMIT'); add_message($rowNum - 1 . ' rows imported successfully'); redirect('services__component_library'); // exits } else { add_message("Errors were found in the CSV file. Import has not been performed. Please correct the errors and try again", 'error'); $GLOBALS['system']->doTransaction('ROLLBACK'); } fclose($fp); } }