Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.34 KB | None | 0 0
  1. <?php
  2.     require_once 'common.php';
  3.  
  4.     class Document_processing extends Common
  5.     {
  6.         // js zoom configuration will load all DataSourceRendition data in array and format then into json string
  7.         // this json string is then used by zoom.js file to intialize and display thumbnail images of DataSource.
  8.         private function _get_js_zoom_configuration($data_source_id)
  9.         {
  10.             $this->load->model('data_source_model');
  11.  
  12.             $js_zoom_configuration = array();
  13.             $js_zoom_configuration['width'] = $this->config->item('image_rendition_geometries');
  14.             $js_zoom_configuration['pages'] = array();
  15.  
  16.             if($data_source_id > 0)
  17.             {
  18.                 $pages = $this->data_source_model->get_data_source_children($data_source_id);
  19.  
  20.                 $page_count = 0;
  21.                 foreach($pages->result() as $page)
  22.                 {
  23.                     $renditions = $this->data_source_model->get_data_source_rendition($page->Id);
  24.  
  25.                     $js_zoom_configuration['pages'][$page_count] = array('document_id' => $this->config->item('prefix_thumbnail_id') . $page->Id, 'zoom_level_thumbnail' => array());
  26.  
  27.                     $rendition_count = 0;
  28.                     foreach($renditions->result() as $rendition)
  29.                     {
  30.                         $js_zoom_configuration['pages'][$page_count]['zoom_level_thumbnail'][$rendition_count] = array(
  31.                                 'img_src' => base_url() . $rendition->Filename,
  32.                                 'o' => $page->PageRotation
  33.                             );
  34.  
  35.                         $rendition_count = $rendition_count + 1;
  36.                     }
  37.  
  38.                     $page_count = $page_count + 1;
  39.                 }
  40.             }
  41.  
  42.             return (stripslashes(json_encode($js_zoom_configuration)));
  43.         }
  44.  
  45.         // insert POST field data to FieldData table
  46.         private function _save_field_data(array $posted_fields)
  47.         {
  48.             // remove submit button information from posted fields data
  49.             unset($posted_fields['save']);
  50.             unset($posted_fields['next']);
  51.  
  52.             $form_id = $this->data_entry_form_model->get_form_id($this->session->userdata('WorkflowId'));
  53.  
  54.             // data to  be inserted at form field
  55.             $form_data = array(
  56.                 'DataSourceId' => $this->session->userdata('DataSourceId'),
  57.                 'FormId' => $form_id,
  58.                 'UserId' => $this->session->userdata('UserId'),
  59.                 'DateTimeStamp' => gmdate('Y-m-d')
  60.             );
  61.  
  62.             // insert form field
  63.             $form_data_id = $this->data_entry_form_model->insert_form_data($form_data);
  64.  
  65.             foreach($posted_fields as $name => $value)
  66.             {
  67.                 // if $value is array type then it is either checkbox or multiple select
  68.                 // we store these value in json format as they can store multiple values
  69.                 $field_value_list = '';
  70.  
  71.                 if(is_array($value) === TRUE)
  72.                 {
  73.                     //$name = $name . '[]'; // multiple selectable fields have [ ]
  74.                     $field_value_list = json_encode($value);
  75.                 }
  76.                 else
  77.                 {
  78.                     $field_value_list = $value;
  79.                 }
  80.  
  81.                 $field_id = $this->data_entry_form_model->get_field_id($form_id, $name);
  82.  
  83.                 if($field_id > 0)
  84.                 {
  85.                     $field_data = array(
  86.                         'FieldId' => $field_id,
  87.                         'Value' => $field_value_list,
  88.                         'FormDataId' => $form_data_id
  89.                     );
  90.  
  91.                     $this->data_entry_form_model->insert_field_data($field_data);
  92.                 }
  93.             }
  94.  
  95.             return $form_data_id;
  96.         }
  97.  
  98.         private function _send_email($form_id, $form_data_id)
  99.         {
  100.             // TODO: Aman/KIP, attaching large files could result in time out. set_time_limit(0) ???
  101.             // default of 60 is good enough I say, and I believe u thinking of using cron to send email isn't it so ?
  102.             set_time_limit(0);
  103.  
  104.             // get field data and field title
  105.             $data = $this->data_source_model->get_field_data($form_id, $form_data_id);
  106.  
  107.             // send email
  108.             /*$config = Array(
  109.               'protocol' => 'smtp',
  110.               'smtp_host' => 'ssl://smtp.gmail.com',
  111.               'smtp_port' => 465,
  112.               'smtp_user' => 'teamph.at2@gmail.com',
  113.               'smtp_pass' => 'zulu0079841',
  114.               'smtp_timeout' => 120
  115.             );*/
  116.            
  117.             $config = Array(
  118.               'protocol' => 'smtp',
  119.               'smtp_host' => 'smtp.wlink.com.np',
  120.               'smtp_timeout' => 30,
  121.               'mailtype' => 'html',
  122.               'charset' => 'iso-8859-1'
  123.             );
  124.  
  125.              //TODO: this should be in configuration
  126.              /* $config = Array(
  127.                'protocol' => 'smtp',
  128.                'smtp_host' => 'mail.xtracta.com',
  129.                'smtp_port' => 26,
  130.                'smtp_user' => 'kip.hughes@xtracta.com',
  131.                'smtp_pass' => 's@mplep@ssword',
  132.                'smtp_timeout' => 120
  133.              ); */
  134.  
  135.             $config['mailtype'] = 'html';
  136.             $config['charset'] = 'iso-8859-1';
  137.             $config['wordwrap'] = TRUE;
  138.  
  139.             $this->load->library('email', $config);
  140.             $this->email->set_newline('\r\n');
  141.  
  142.             // TODO: this should come from configuration
  143.             $this->email->from('development@xtracta.com', 'Xtracta');
  144.  
  145.             // TODO: these should come from DB
  146.             $this->email->to('kip.hughes@gmail.com');
  147.             $this->email->cc('aman.tuladhar@gmail.com');
  148.             //$this->email->cc('jonathan.spence@xtracta.com');
  149.  
  150.             // TODO: this should come from configuration
  151.             $this->email->subject('Data Extraction Result');
  152.             $this->email->message('See attached files.');
  153.  
  154.             // Create temporary directory for CSV, JSON, and PDF generation
  155.             $temp_directory = get_temp_directory();        
  156.            
  157.             // Attach CSV file
  158.             if(is_csv_output_possible($form_id) === TRUE)
  159.             {
  160.                 $csv_file =  $temp_directory . $this->config->item('default_filename_csv');
  161.                 save_file($csv_file, format_field_data($data, 'csv'));
  162.                 $this->email->attach($csv_file);
  163.             }
  164.  
  165.             // Attach JSON file
  166.             $json_file =  $temp_directory . $this->config->item('default_filename_json');
  167.             save_file($json_file, format_field_data($data, 'json'));
  168.             $this->email->attach($json_file);
  169.  
  170.             // Attach DataSource file
  171.             $data_source = $this->data_source_model->get_data_source($this->session->userdata('DataSourceId'))->result_array();
  172.             $data_source_children = $this->data_source_model->get_data_source_children($this->session->userdata('DataSourceId'))->result_array();
  173.             $pdf_filename = $temp_directory . $this->config->item('default_filename_pdf');         
  174.             $data_source_file = get_output_pdf($data_source, $data_source_children, $pdf_filename);
  175.             $this->email->attach($data_source_file);
  176.  
  177.             $this->email->send();
  178.             echo $this->email->print_debugger();
  179.            
  180.             // Clean up; delete temporary files and folders
  181.             print_r($csv_file);
  182.             echo('<br/>');
  183.             print_r($json_file);
  184.             echo('<br/>');
  185.             print_r($data_source_file);
  186.             echo('<br/>');                     
  187.            
  188.             unlink($csv_file);
  189.             unlink($json_file);
  190.             unlink($data_source_file);
  191.             rmdir($temp_directory);
  192.         }
  193.  
  194.         protected function _set_page_content()
  195.         {
  196.             // $this->lang->load('xtracta', 'chinese-traditional');
  197.             $this->lang->load('xtracta', 'english');
  198.  
  199.             $this->mysmarty->assign('language_button_next', $this->lang->line('button_next'));
  200.             $this->mysmarty->assign('language_button_passed_quality_assurance', $this->lang->line('button_passed_quality_assurance'));
  201.             $this->mysmarty->assign('language_button_reject', $this->lang->line('button_reject'));
  202.             $this->mysmarty->assign('language_button_save_and_exit', $this->lang->line('button_save_and_exit'));
  203.  
  204.             $this->mysmarty->assign('language_menu_file', $this->lang->line('menu_file'));
  205.             $this->mysmarty->assign('language_menu_file_upload', $this->lang->line('menu_file_upload'));
  206.             $this->mysmarty->assign('language_menu_file_delete', $this->lang->line('menu_file_delete'));
  207.             $this->mysmarty->assign('language_menu_edit', $this->lang->line('menu_edit'));
  208.             $this->mysmarty->assign('language_menu_view', $this->lang->line('menu_view'));
  209.  
  210.             $this->mysmarty->assign('language_field_label_select_client', $this->lang->line('field_label_select_client'));
  211.             $this->mysmarty->assign('language_field_label_select_job', $this->lang->line('field_label_select_job'));
  212.  
  213.             $this->mysmarty->assign('language_header_content', $this->lang->line('header_content'));
  214.             $this->mysmarty->assign('language_header_data_entry', $this->lang->line('header_data_entry'));
  215.             $this->mysmarty->assign('language_header_job_filter', $this->lang->line('header_job_filter'));
  216.             $this->mysmarty->assign('language_header_jobs', $this->lang->line('header_jobs'));
  217.             $this->mysmarty->assign('language_header_quality_assurance', $this->lang->line('header_quality_assurance'));
  218.  
  219.             $this->mysmarty->assign('language_message_success_page_deleted', sprintf($this->lang->line('message_success_page_deleted'), 'Page 1 of 3'));
  220.         }
  221.        
  222.         protected function _set_validation_rules()
  223.         {
  224.        
  225.         }
  226.  
  227.         public function __construct()
  228.         {
  229.             parent::__construct('Document_processing');
  230.             $this->load->model('data_entry_form_model');
  231.             $this->load->model('data_source_model');
  232.  
  233.             $this->load->library('data_entry_form');
  234.             // TODO: this is temporary and most possible should come from login controller or other.
  235.             // remove it in final code
  236.             // setting up  session variables
  237.             $workflow_id = 100000;
  238.             $data_source_id = $this->data_source_model->get_next_data_source_id($workflow_id);
  239.  
  240.             $data = array(
  241.                 'DataSourceId' => $data_source_id,  //DataSourceId is set each time a document is presented to the user
  242.                 'WorkflowId' => $workflow_id        //WorkflowId is set when user picks a workflow to work on
  243.             );
  244.  
  245.             $this->session->set_userdata($data);
  246.         }
  247.        
  248.  
  249.         public function index()
  250.         {
  251.             $form_id = $this->data_entry_form_model->get_form_id($this->session->userdata('WorkflowId'));
  252.  
  253.             $form_fields = $this->data_entry_form_model->get_data_entry_form_array($form_id);
  254.  
  255.             $form_field_markup = $this->data_entry_form->get_data_entry_form($form_fields);
  256.  
  257.             // get data source
  258.             $js_zoom_configuration = $this->_get_js_zoom_configuration($this->session->userdata('DataSourceId'));
  259.            
  260.             $this->mysmarty->assign('form_field_markup', $form_field_markup);
  261.             $this->mysmarty->assign('js_zoom_configuration', $js_zoom_configuration);
  262.             $this->mysmarty->view('main.tpl');
  263.         }
  264.  
  265.         public function submit()
  266.         {
  267.             //TODO: validation, error to be implement
  268.             $form_id = $this->data_entry_form_model->get_form_id($this->session->userdata('WorkflowId'));
  269.  
  270.             $form = $this->data_entry_form_model->get_data_entry_form_array($form_id);
  271.            
  272.             $post_variables = array();
  273.             $post_variables = $_POST;
  274.            
  275.             $_prepped_post_variables = array();
  276.             $_prepped_post_variables = $this->_prep_post_variables($post_variables, $form);
  277.            
  278.             echo "<pre>";
  279.             print_r($_POST);
  280.             print_r($_prepped_post_variables);
  281.             echo "</pre>";
  282.             die();
  283.            
  284.             // insert field data
  285.             $form_data_id = $this->_save_field_data($_prepped_post_variables);
  286.            
  287.             // send email
  288.             // TODO: Aman/KIP, flag the document for sending email instead of doing it here
  289.             $this->_send_email($form_id, $form_data_id);
  290.             echo $this->email->print_debugger();
  291.             // die();
  292.  
  293.             // redirect('document_processing', 'refresh');
  294.         }
  295.        
  296.         /**
  297.          * Return repeating post variable names. For example, "fruits-1", "fruits-2", & "fruits-3" are considered repeating. Return "fruit."
  298.          *
  299.          */
  300.         private function _get_repeating_post_variable_names(array $post_variables, $repeating_field_set_field_names)
  301.         {
  302.             // Get all the post variable names that are part of a repeating field set (this is determine by using $repeating_field_set_field_names)
  303.             // and assign to $repeating_post_variable_names        
  304.             $repeating_post_variable_names = array();
  305.  
  306.             // Loop through each post variable
  307.             foreach($post_variables as $key => $value)
  308.             {
  309.                 $post_variable_name = preg_replace('/-[0-9]+$/', '', $key);
  310.                
  311.                 if (in_array($post_variable_name, $repeating_field_set_field_names) === TRUE)
  312.                 {          
  313.                     if(in_array($post_variable_name, $repeating_post_variable_names) === FALSE)
  314.                     {
  315.                         $repeating_post_variable_names[] = $post_variable_name;
  316.                     }          
  317.                 }
  318.             }
  319.                        
  320.             return $repeating_post_variable_names;
  321.         }
  322.        
  323.         private function _get_post_variables_grouped_by_repeating_post_variable_name($post_variables, $repeating_post_variable_names)
  324.         {
  325.             $post_variables_grouped_by_repeating_post_variable_name = array();
  326.            
  327.             foreach($post_variables as $key => $value)
  328.             {
  329.                 $post_variable_name = preg_replace('/-[0-9]+$/', '', $key);
  330.                            
  331.                 if(in_array($post_variable_name, $repeating_post_variable_names) === TRUE)
  332.                 {
  333.                     $post_variables_grouped_by_repeating_post_variable_name[$post_variable_name][] = $value;
  334.                 }
  335.                 else
  336.                 {
  337.                     $post_variables_grouped_by_repeating_post_variable_name[$key] = $value;
  338.                 }
  339.             }          
  340.            
  341.             return $post_variables_grouped_by_repeating_post_variable_name;
  342.         }
  343.        
  344.         private function _unset_last_repeating_post_variable($post_variables_grouped_by_repeating_post_variable_name, $repeating_field_set_field_names)
  345.         {
  346.             //print_r($post_variables_grouped_by_repeating_post_variable_name);
  347.             //print_r($repeating_field_set_field_names);
  348.            
  349.             // Remove last element of repeating field set
  350.             /* $arbitrary_field_name = $repeating_field_set_field_names[0];
  351.  
  352.             $entry_count = count($post_variables_grouped_by_repeating_post_variable_name[$arbitrary_field_name]);
  353.            
  354.             foreach($post_variables_grouped_by_repeating_post_variable_name as $key => $value)
  355.             {
  356.                 //print_r($value);
  357.                
  358.                 if (in_array($key, $repeating_field_set_field_names))
  359.                 {
  360.                    
  361.                     // print_r($entry_count);
  362.                     unset($post_variables_grouped_by_repeating_post_variable_name[$key][$entry_count - 1]);
  363.                 }
  364.             } */
  365.            
  366.             foreach($post_variables_grouped_by_repeating_post_variable_name as $key => $value)
  367.             {
  368.                 if (is_array($value))
  369.                 {
  370.                     // print_r(count($value)-1);
  371.                     unset($post_variables_grouped_by_repeating_post_variable_name[$key][count($value) - 1]);
  372.                 }
  373.             }
  374.            
  375.             return $post_variables_grouped_by_repeating_post_variable_name;
  376.         }
  377.        
  378.         private function _prep_post_variables(array $post_variables, array $form)
  379.         {
  380.             $repeating_field_set_field_names = array();
  381.             $repeating_field_set_field_names = $this->data_entry_form->get_repeating_fieldset_field_names($form);
  382.            
  383.             $repeating_post_variable_names = array();
  384.             $repeating_post_variable_names = $this->_get_repeating_post_variable_names($post_variables, $repeating_field_set_field_names);
  385.            
  386.             $post_variables_grouped_by_repeating_post_variable_name = array();
  387.             $post_variables_grouped_by_repeating_post_variable_name = $this->_get_post_variables_grouped_by_repeating_post_variable_name($post_variables, $repeating_post_variable_names);
  388.            
  389.            
  390.            
  391.             $post_variables_grouped_by_repeating_post_variable_name = $this->_unset_last_repeating_post_variable($post_variables_grouped_by_repeating_post_variable_name, $repeating_field_set_field_names);
  392.            
  393.             // print_r($post_variables_grouped_by_repeating_post_variable_name);
  394.             // die();
  395.            
  396.             return $post_variables_grouped_by_repeating_post_variable_name;
  397.         }
  398.     }
  399.  
  400.     /* End of file */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement