public function control() { $this->redirectToSternIndiaEndPoint(); $config = Config::getInstance(); if (isset($_POST['upload']) && $_POST['upload'] == 'Upload') { $target_dir = new FileSystem('upload/'); $file = new File('foo', $target_dir); $name = date('D_d_m_Y_H_m_s_'); $name = $name . $file->getName(); $file->setName($name); $config = Config::getInstance(); $file->addValidations(array(new Mimetype($config->getMimeTypes()), new Size('5M'))); $data = array('name' => $file->getNameWithExtension(), 'extension' => $file->getExtension(), 'mime' => $file->getMimetype(), 'size' => $file->getSize(), 'md5' => $file->getMd5()); try { // /Profiler::debugPoint(true,__METHOD__, __FILE__, __LINE__,$data); $file->upload(); //Profiler::debugPoint(true,__METHOD__, __FILE__, __LINE__,$data); } catch (Exception $e) { $errors = $file->getErrors(); } $csvReader = new CSVReader(); $destinationFile = $target_dir->directory . $file->getNameWithExtension(); $data = $csvReader->parse_file($destinationFile); //$country= DAOFactory::getDAO('LocationDAO'); foreach ($data as $loc_arr) { Utils::processLocation($loc_arr); } //Profiler::debugPoint(true,__METHOD__, __FILE__, __LINE__); $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if (isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if ($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } } return $this->generateView(); }
/** * @expectedException Exception * @expectedExceptionMessage Invalid CSV header. Make sure your column header is "phone_number". */ public function test__survey_respondents_import_check__file_no_header() { // Load CSVReader library. $csv = new CSVReader(); $csv->separator = ','; $rows = $csv->parse_file(ROOT_PATH . 'tests/test_resources/respondents_csv/10_valid_no_header.csv'); // From DB $db_call_tasks = array(); $respondents = self::$CI->_survey_respondents_import_check($rows, $db_call_tasks); }
/** * Summary page to add respondents to a given survey. * @param $sid * * Route - /survey/:sid/respondents/(file|input) */ public function survey_respondents_add($sid, $type) { $survey = $this->survey_model->get($sid); if (!$survey) { show_404(); } else { if (!has_permission('manage respondents any survey')) { show_403(); } } if (!$survey->status_allows('import respondents any survey')) { show_403(); } // Form validation based on import type. switch ($type) { case 'file': // Config data for the file upload. $file_upload_config = array('upload_path' => '/tmp/', 'allowed_types' => 'csv', 'file_name' => 'respondents_' . md5(microtime(true))); // Load needed libraries. $this->load->library('upload', $file_upload_config); $this->form_validation->set_rules('survey_respondents_file', 'Respondents File', 'callback__cb_survey_respondents_add_file_handle'); break; case 'direct': $this->form_validation->set_rules('survey_respondents_text', 'Respondents Text', 'trim|required|xss_clean'); break; } $this->form_validation->set_error_delimiters('<small class="error">', '</small>'); // If the import has invalid respondents they are added to the textarea // to allow the user to correct them. $invalid_respondents = array(); if (isset($_SESSION['respondents_numbers']['invalid'])) { $invalid_respondents = $_SESSION['respondents_numbers']['invalid']; // Unset so they don't bother anymore. unset($_SESSION['respondents_numbers']); } // If no data submitted show the form. if ($this->form_validation->run() == FALSE) { $this->load->view('base/html_start'); $this->load->view('components/navigation', array('active_menu' => 'surveys')); $this->load->view('surveys/survey_respondents_add', array('survey' => $survey, 'import_type' => $type, 'invalid_respondents' => $invalid_respondents)); $this->load->view('base/html_end'); } else { // Processing based on import type. switch ($type) { case 'file': // Read file. $file = $this->input->post('survey_respondents_file'); if (isset($file['full_path'])) { // Load CSVReader library. $this->load->helper('csvreader'); $csv = new CSVReader(); $csv->separator = ','; $rows = $csv->parse_file($file['full_path']); } break; case 'direct': $rows = explode("\n", $this->input->post('survey_respondents_text')); break; } if (!isset($rows)) { Status_msg::error('An error occurred when processing the numbers. Try again.'); redirect($survey->get_url_respondents()); } // Load all call_tasks from DB to check for duplicates. $db_call_tasks = $this->call_task_model->get_all($sid); try { $respondents_numbers = $this->_survey_respondents_import_check($rows, $db_call_tasks); } catch (Exception $e) { Status_msg::error($e->getMessage()); redirect($survey->get_url_respondents()); } // Create a call_task for each respondent number. $success_saves = array(); foreach ($respondents_numbers['valid'] as $number) { // Prepare survey data to construct a new survey_entity $call_task_data = array(); $call_task_data['survey_sid'] = (int) $sid; $call_task_data['number'] = (string) $number; // Construct survey. $new_call_task = Call_task_entity::build($call_task_data); // Save call task. if ($this->call_task_model->save($new_call_task)) { $success_saves[] = $number; } else { // If an error occurred flag as invalid to be imported again. // This should not happen, but you never know. $respondents_numbers['invalid'][] = $number; } } $respondents_numbers['valid'] = $success_saves; // Setting messages. $total_valid = count($respondents_numbers['valid']); $total_invalid = count($respondents_numbers['invalid']); $total_dups = count($respondents_numbers['dups']); $total_numbers = $total_valid + $total_invalid + $total_dups; if ($total_valid) { Status_msg::success("{$total_valid} of {$total_numbers} respondents were successfully imported.", TRUE); } else { Status_msg::warning("No respondents were imported."); } if ($total_invalid) { Status_msg::error("{$total_invalid} of {$total_numbers} respondents were not in a valid format."); } if ($total_dups) { Status_msg::notice("{$total_dups} of {$total_numbers} respondents have duplicate phone numbers."); } // Store invalid in any. if ($total_invalid) { $_SESSION['respondents_numbers']['invalid'] = $respondents_numbers['invalid']; redirect($survey->get_url_respondents_add('direct')); } redirect($survey->get_url_respondents()); } }