Advertisement
Guest User

Untitled

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