Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'common.php';
- class Document_processing extends Common
- {
- public function test_config_data_source()
- {
- $data_source_id = 8;
- $data_source_array = $this->_data_source($data_source_id);
- print_r($data_source_array);
- }
- private function _data_source($data_source_id)
- {
- $data_source_array = array();
- $pages = $this->data_source_model->get_data_source_children($data_source_id);
- foreach($pages->result() as $page)
- {
- if($this->data_source_model->get_data_source_children_count($page->Id) > 0)
- {
- $data_source_array[$page->Id] = $this->_data_source($page->Id);
- }
- else
- {
- $data_source_array[] = array(
- 'data_source_id' => $this->config->item('prefix_thumbnail_id') . $page->Id,
- //'zoom_level_thumbnail' => $this->_data_source_rendition($page)
- );
- }
- //return $data_source_array;
- }
- return $data_source_array;
- }
- private function _data_source_rendition($page)
- {
- $renditions = $this->data_source_model->get_data_source_rendition($page->Id);
- $rendition_array = array();
- foreach($renditions->result() as $rendition)
- {
- $rendition_array[] = array(
- 'img_src' => base_url() . $rendition->Filename,
- 'o' => $page->PageRotation
- );
- }
- return $rendition_array;
- }
- // js zoom configuration will load all DataSourceRendition data in array and format then into json string
- // this json string is then used by zoom.js file to intialize and display thumbnail images of DataSource.
- private function _get_js_zoom_configuration($data_source_id)
- {
- $js_zoom_configuration = array();
- $js_zoom_configuration['width'] = $this->config->item('image_rendition_geometries');
- $js_zoom_configuration['pages'] = array();
- if($data_source_id > 0)
- {
- $pages = $this->data_source_model->get_data_source_children($data_source_id);
- $page_count = 0;
- foreach($pages->result() as $page)
- {
- $renditions = $this->data_source_model->get_data_source_rendition($page->Id);
- $js_zoom_configuration['pages'][$page_count] = array('document_id' => $this->config->item('prefix_thumbnail_id') . $page->Id, 'zoom_level_thumbnail' => array());
- $rendition_count = 0;
- foreach($renditions->result() as $rendition)
- {
- $js_zoom_configuration['pages'][$page_count]['zoom_level_thumbnail'][$rendition_count] = array(
- 'img_src' => base_url() . $rendition->Filename,
- 'o' => $page->PageRotation
- );
- $rendition_count = $rendition_count + 1;
- }
- $page_count = $page_count + 1;
- }
- }
- /* print_r($js_zoom_configuration);
- die(); */
- return (stripslashes(json_encode($js_zoom_configuration)));
- }
- // insert POST field data to FieldData table
- private function _save_field_data(array $posted_fields)
- {
- // remove submit button information from posted fields data
- unset($posted_fields['save']);
- unset($posted_fields['next']);
- $form_id = $this->data_entry_form_model->get_form_id($this->session->userdata('workflow_id'));
- // data to be inserted at form field
- $form_data = array(
- 'DataSourceId' => $this->session->userdata('data_source_id'),
- 'FormId' => $form_id,
- 'UserId' => $this->session->userdata('UserId'),
- 'DateTimeStamp' => gmdate('Y-m-d')
- );
- // insert form field
- $form_data_id = $this->data_entry_form_model->insert_form_data($form_data);
- foreach($posted_fields as $name => $value)
- {
- // if $value is array type then it is either checkbox or multiple select
- // we store these value in json format as they can store multiple values
- $field_value_list = '';
- if(is_array($value) === TRUE)
- {
- //$name = $name . '[]'; // multiple selectable fields have [ ]
- $field_value_list = json_encode($value);
- }
- else
- {
- $field_value_list = $value;
- }
- $field_id = $this->data_entry_form_model->get_field_id($form_id, $name);
- if($field_id > 0)
- {
- $field_data = array(
- 'FieldId' => $field_id,
- 'Value' => $field_value_list,
- 'FormDataId' => $form_data_id
- );
- $this->data_entry_form_model->insert_field_data($field_data);
- }
- }
- return $form_data_id;
- }
- private function _send_email($form_id, $form_data_id)
- {
- // TODO: Aman/KIP, attaching large files could result in time out. set_time_limit(0) ???
- // default of 60 is good enough I say, and I believe u thinking of using cron to send email isn't it so ?
- set_time_limit(0);
- // get field data and field title
- $data = $this->data_source_model->get_field_data($form_id, $form_data_id);
- // send email
- /*$config = Array(
- 'protocol' => 'smtp',
- 'smtp_host' => 'ssl://smtp.gmail.com',
- 'smtp_port' => 465,
- 'smtp_user' => 'teamph.at2@gmail.com',
- 'smtp_pass' => 'zulu0079841',
- 'smtp_timeout' => 120
- );*/
- $config = Array(
- 'protocol' => 'smtp',
- //'smtp_host' => 'smtp.wlink.com.np',
- 'smtp_host' => 'smtp.websurfer.com.np',
- 'smtp_timeout' => 30,
- 'mailtype' => 'html',
- 'charset' => 'iso-8859-1'
- );
- //TODO: this should be in configuration
- /* $config = Array(
- 'protocol' => 'smtp',
- 'smtp_host' => 'mail.xtracta.com',
- 'smtp_port' => 26,
- 'smtp_user' => 'kip.hughes@xtracta.com',
- 'smtp_pass' => 's@mplep@ssword',
- 'smtp_timeout' => 120
- ); */
- $config['mailtype'] = 'html';
- $config['charset'] = 'iso-8859-1';
- $config['wordwrap'] = TRUE;
- $this->load->library('email', $config);
- $this->email->set_newline('\r\n');
- // TODO: this should come from configuration
- $this->email->from('development@xtracta.com', 'Xtracta');
- // TODO: these should come from DB
- $this->email->to('kip.hughes@gmail.com');
- $this->email->cc('aman.tuladhar@gmail.com');
- //$this->email->cc('jonathan.spence@xtracta.com');
- // TODO: this should come from configuration
- $this->email->subject('Data Extraction Result');
- $this->email->message('See attached files.');
- // Create temporary directory for CSV, JSON, and PDF generation
- $temp_directory = get_temp_directory();
- // Attach CSV file
- if(is_csv_output_possible($form_id) === TRUE)
- {
- $csv_file = $temp_directory . $this->config->item('default_filename_csv');
- save_file($csv_file, format_field_data($data, 'csv'));
- $this->email->attach($csv_file);
- }
- // Attach JSON file
- $json_file = $temp_directory . $this->config->item('default_filename_json');
- save_file($json_file, format_field_data($data, 'json'));
- $this->email->attach($json_file);
- // Attach DataSource file
- $data_source = $this->data_source_model->get_data_source($this->session->userdata('data_source_id'))->result_array();
- $data_source_children = $this->data_source_model->get_data_source_children($this->session->userdata('data_source_id'))->result_array();
- $pdf_filename = $temp_directory . $this->config->item('default_filename_pdf');
- $data_source_file = get_output_pdf($data_source, $data_source_children, $pdf_filename);
- $this->email->attach($data_source_file);
- $this->email->send();
- echo $this->email->print_debugger();
- // Clean up; delete temporary files and folders
- print_r($csv_file);
- echo('<br/>');
- print_r($json_file);
- echo('<br/>');
- print_r($data_source_file);
- echo('<br/>');
- unlink($csv_file);
- unlink($json_file);
- unlink($data_source_file);
- rmdir($temp_directory);
- }
- protected function _set_page_content()
- {
- // $this->lang->load('xtracta', 'chinese-traditional');
- $this->lang->load('xtracta', 'english');
- $this->mysmarty->assign('language_button_next', $this->lang->line('button_next'));
- $this->mysmarty->assign('language_button_passed_quality_assurance', $this->lang->line('button_passed_quality_assurance'));
- $this->mysmarty->assign('language_button_reject', $this->lang->line('button_reject'));
- $this->mysmarty->assign('language_button_save_and_exit', $this->lang->line('button_save_and_exit'));
- $this->mysmarty->assign('language_menu_file', $this->lang->line('menu_file'));
- $this->mysmarty->assign('language_menu_file_upload', $this->lang->line('menu_file_upload'));
- $this->mysmarty->assign('language_menu_file_delete', $this->lang->line('menu_file_delete'));
- $this->mysmarty->assign('language_menu_edit', $this->lang->line('menu_edit'));
- $this->mysmarty->assign('language_menu_view', $this->lang->line('menu_view'));
- $this->mysmarty->assign('language_field_label_select_client', $this->lang->line('field_label_select_client'));
- $this->mysmarty->assign('language_field_label_select_job', $this->lang->line('field_label_select_job'));
- $this->mysmarty->assign('language_header_content', $this->lang->line('header_content'));
- $this->mysmarty->assign('language_header_data_entry', $this->lang->line('header_data_entry'));
- $this->mysmarty->assign('language_header_job_filter', $this->lang->line('header_job_filter'));
- $this->mysmarty->assign('language_header_jobs', $this->lang->line('header_jobs'));
- $this->mysmarty->assign('language_header_quality_assurance', $this->lang->line('header_quality_assurance'));
- $this->mysmarty->assign('language_message_success_page_deleted', sprintf($this->lang->line('message_success_page_deleted'), 'Page 1 of 3'));
- }
- protected function _set_validation_rules()
- {
- }
- public function __construct()
- {
- parent::__construct('Document_processing');
- $this->load->model('data_entry_form_model');
- $this->load->model('data_source_model');
- $this->load->library('data_entry_form');
- // TODO: this is temporary and most possible should come from login controller or other.
- // remove it in final code
- // setting up session variables
- /* $workflow_id = 100000;
- $data_source_id = $this->data_source_model->get_next_data_source_id($workflow_id);
- $data = array(
- 'data_source_id' => $data_source_id, //DataSourceId is set each time a document is presented to the user
- 'workflow_id' => $workflow_id //WorkflowId is set when user picks a workflow to work on
- );
- $this->session->set_userdata($data); */
- }
- public function index()
- {
- // print_r($_POST);
- // die();
- if(isset($_POST['workflow_id']) === TRUE OR $this->session->userdata('workflow_id') !== '')
- {
- if(isset($_POST['workflow_id']) === TRUE)
- {
- // Setting up workflow id from POST, this means user has come to this page from dashboard
- $workflow_id = $_POST['workflow_id'];
- }
- else
- {
- // Setting up workflow id from SESSION, meaning user has come to this page by clicking on next button of document processing form page
- $workflow_id = $this->session->userdata('workflow_id');
- }
- $data_source_id = $this->data_source_model->get_next_data_source_id($workflow_id);
- $data = array(
- 'data_source_id' => $data_source_id, //DataSourceId is set each time a document is presented to the user
- 'workflow_id' => $workflow_id //WorkflowId is set when user picks a workflow to work on
- );
- $this->session->set_userdata($data);
- $form_id = $this->data_entry_form_model->get_form_id($this->session->userdata('workflow_id'));
- // TODO: need to put assert make sure fieldname doesn't contain [] in it
- $form_fields = $this->data_entry_form_model->get_data_entry_form_array($form_id);
- $form_field_markup = $this->data_entry_form->get_data_entry_form($form_fields);
- // Get data source JSON strcture for javascript to run in document processing page
- $js_zoom_configuration = $this->_get_js_zoom_configuration($this->session->userdata('data_source_id'));
- /* print_r($js_zoom_configuration);
- die(); */
- $this->mysmarty->assign('form_field_markup', $form_field_markup);
- $this->mysmarty->assign('js_zoom_configuration', $js_zoom_configuration);
- $this->mysmarty->view('main.tpl');
- }
- else
- {
- redirect('dashboard', 'refresh');
- }
- }
- public function submit()
- {
- $form_id = $this->data_entry_form_model->get_form_id($this->session->userdata('workflow_id'));
- $form = $this->data_entry_form_model->get_data_entry_form_array($form_id);
- $post_variables = array();
- $post_variables = $_POST;
- $_prepped_post_variables = array();
- $_prepped_post_variables = $this->_prep_post_variables($post_variables, $form);
- // insert field data
- $form_data_id = $this->_save_field_data($_prepped_post_variables);
- // send email
- // TODO: Aman/KIP, flag the document for sending email instead of doing it here
- $this->_send_email($form_id, $form_data_id);
- if(isset($_POST['save']) === TRUE)
- {
- // redirect to dashborad
- redirect('dashboard', 'refresh');
- }
- elseif (isset($_POST['next']))
- {
- redirect('document_processing', 'refresh');
- }
- }
- /**
- * Return repeating post variable names. For example, "fruits-1", "fruits-2", & "fruits-3" are considered repeating. Return "fruit."
- *
- */
- private function _get_repeating_post_variable_names(array $post_variables, $repeating_field_set_field_names)
- {
- // Get all the post variable names that are part of a repeating field set (this is determine by using $repeating_field_set_field_names)
- // and assign to $repeating_post_variable_names
- $repeating_post_variable_names = array();
- // Loop through each post variable
- foreach($post_variables as $key => $value)
- {
- $post_variable_name = preg_replace('/-[0-9]+$/', '', $key);
- if (in_array($post_variable_name, $repeating_field_set_field_names) === TRUE)
- {
- if(in_array($post_variable_name, $repeating_post_variable_names) === FALSE)
- {
- $repeating_post_variable_names[] = $post_variable_name;
- }
- }
- }
- return $repeating_post_variable_names;
- }
- private function _get_post_variables_grouped_by_repeating_post_variable_name($post_variables, $repeating_post_variable_names)
- {
- $post_variables_grouped_by_repeating_post_variable_name = array();
- foreach($post_variables as $key => $value)
- {
- $post_variable_name = preg_replace('/-[0-9]+$/', '', $key);
- if(in_array($post_variable_name, $repeating_post_variable_names) === TRUE)
- {
- $post_variables_grouped_by_repeating_post_variable_name[$post_variable_name][] = $value;
- }
- else
- {
- $post_variables_grouped_by_repeating_post_variable_name[$key] = $value;
- }
- }
- return $post_variables_grouped_by_repeating_post_variable_name;
- }
- private function _unset_last_repeating_post_variable($post_variables_grouped_by_repeating_post_variable_name)
- {
- foreach($post_variables_grouped_by_repeating_post_variable_name as $key => $value)
- {
- if (is_array($value))
- {
- unset($post_variables_grouped_by_repeating_post_variable_name[$key][count($value) - 1]);
- }
- }
- return $post_variables_grouped_by_repeating_post_variable_name;
- }
- private function _prep_post_variables(array $post_variables, array $form)
- {
- $repeating_field_set_field_names = array();
- $repeating_field_set_field_names = $this->data_entry_form->get_repeating_fieldset_field_names($form);
- $repeating_post_variable_names = array();
- $repeating_post_variable_names = $this->_get_repeating_post_variable_names($post_variables, $repeating_field_set_field_names);
- $post_variables_grouped_by_repeating_post_variable_name = array();
- $post_variables_grouped_by_repeating_post_variable_name = $this->_get_post_variables_grouped_by_repeating_post_variable_name($post_variables, $repeating_post_variable_names);
- $post_variables_grouped_by_repeating_post_variable_name = $this->_unset_last_repeating_post_variable($post_variables_grouped_by_repeating_post_variable_name);
- return $post_variables_grouped_by_repeating_post_variable_name;
- }
- }
- /* End of file */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement