public function edit($id = 0, $method = "echo")
 {
     ensure_user_access(TRUE, array('admin'));
     $output = array('message' => "", 'status' => "");
     $details = $this->company_m->details($id);
     $this->form_validation->set_rules('name', 'Company Name', 'required|callback__check_name[' . $id . ']|xss_clean');
     $this->form_validation->set_rules('address', 'Address', 'required|xss_clean');
     $this->form_validation->set_rules('phone', 'Phone', 'required|xss_clean');
     $this->form_validation->set_rules('default_contact_name', 'Default Contact Name', 'required|xss_clean');
     $this->form_validation->set_rules('default_contact_email', 'Default Contact Email', 'required|xss_clean|valid_email');
     $this->form_validation->set_rules('gmt_offset', 'Timezone', 'required|xss_clean');
     if ($this->form_validation->run() == TRUE) {
         $input = array("name" => $this->input->post("name"), "address" => $this->input->post("address"), "phone" => $this->input->post("phone"), "default_contact_name" => $this->input->post("default_contact_name"), "default_contact_email" => $this->input->post("default_contact_email"), 'gmt_offset' => $this->input->post("gmt_offset"));
         $company_logo = $this->input->post('company_logo_img');
         if ($company_logo) {
             $input['logo'] = $company_logo;
         } else {
             $company_logo = $this->upload_logo();
             if (empty($company_logo['error'])) {
                 $input['logo'] = $company_logo['file_name'];
             }
         }
         $is_record_updated = $this->company_m->update($input, $id);
         if ($is_record_updated) {
             $output['message'] = sprintf('The company "%s" was updated.', $details->name);
             $output['status'] = SUCCESS_MESSAGE;
             $output['company_id'] = $id;
             trigger_trip("company_updated", 0, array('company_id' => $id, 'updated_by' => $this->current_user->user_id));
         } else {
             $output['message'] = 'An error occured.';
             $output['status'] = ERROR_MESSAGE;
         }
         $this->_output_request($output, 'companies');
     } else {
         if (validation_errors()) {
             $output['message'] = strip_tags(validation_errors());
             $output['status'] = ERROR_MESSAGE;
         }
     }
     $data = array('form_action' => site_url('companies/edit/' . $id), 'cancel_url' => site_url('companies'), 'page' => 'companies/form', 'title' => 'Edit Company', 'submit_btn_text' => 'Save Changes', 'name' => $this->input->post('name', TRUE) ? $this->input->post('name', TRUE) : $details->name, 'address' => $this->input->post('address', TRUE) ? $this->input->post('address', TRUE) : $details->address, 'phone' => $this->input->post('phone', TRUE) ? $this->input->post('phone', TRUE) : $details->phone, 'default_contact_name' => $this->input->post('default_contact_name', TRUE) ? $this->input->post('default_contact_name', TRUE) : $details->default_contact_name, 'default_contact_email' => $this->input->post('default_contact_email', TRUE) ? $this->input->post('default_contact_email', TRUE) : $details->default_contact_email, 'gmt_offset' => $this->input->post('gmt_offset', TRUE) ? $this->input->post('gmt_offset', TRUE) : $details->gmt_offset, 'logo' => $details->logo, 'js_files' => array('companies/form.js'));
     if ($this->input->is_ajax_request()) {
         $html = $this->template->raw_view('pages/companies/form_modal', $data, TRUE);
         if ($method == "ajax") {
             $output['html'] = $html;
             $this->_output_request($output, 'companies');
         } else {
             echo $html;
         }
     } else {
         $this->template->load('default', $data);
     }
 }
 public function upload_document()
 {
     $status = "";
     $msg = "";
     $file_element_name = 'userfile';
     $document_type_id = $this->input->post('document_type_id') ? $this->input->post('document_type_id') : 1;
     if (empty($_POST['ref_id'])) {
         $status = "error";
         $msg = "No ref_id found";
     }
     $file_id = false;
     if ($status != "error") {
         $this->load->library('upload');
         if (!$this->upload->do_upload($file_element_name)) {
             $status = 'error';
             $msg = $this->upload->display_errors('', '');
             $this->session->set_flashdata('message', $msg);
         } else {
             $data = $this->upload->data();
             $thumbnail_name = '';
             //create a thumbnail if it's an image
             if ($data['is_image']) {
                 $config['image_library'] = 'gd2';
                 $config['source_image'] = $data['full_path'];
                 $config['create_thumb'] = TRUE;
                 $config['maintain_ratio'] = TRUE;
                 $config['width'] = 112;
                 $config['height'] = 87;
                 $this->load->library('image_lib', $config);
                 if (!$this->image_lib->resize()) {
                     $this->session->set_flashdata('message', $this->image_lib->display_errors('<p class="error">', '</p>'));
                 }
                 $thumbnail_name = $data['raw_name'] . $this->image_lib->thumb_marker . $data['file_ext'];
             } else {
                 //not an image so set the thumbnail to be standard
                 switch ($data['file_ext']) {
                     case '.avi':
                         $thumbnail_name = 'avi.png';
                         break;
                     case '.doc':
                         $thumbnail_name = 'doc.png';
                         break;
                     case '.docx':
                         $thumbnail_name = 'docx.png';
                         break;
                     case '.pdf':
                         $thumbnail_name = 'pdf.png';
                         break;
                     case '.ppt':
                         $thumbnail_name = 'ppt.png';
                         break;
                     case '.pptx':
                         $thumbnail_name = 'pptx.png';
                         break;
                     case '.psd':
                         $thumbnail_name = 'psd.png';
                         break;
                     case '.txt':
                         $thumbnail_name = 'txt.png';
                         break;
                     case '.xls':
                         $thumbnail_name = 'xls.png';
                         break;
                     case '.xlsx':
                         $thumbnail_name = 'xlsx.png';
                         break;
                     case '.dwg':
                         $thumbnail_name = 'dwg.png';
                         break;
                     default:
                         $thumbnail_name = 'file.png';
                         break;
                 }
             }
             $file_data = array('document_type_id' => $document_type_id, 'ref_id' => $this->input->post('ref_id'), 'document_name' => $data['file_name'], 'original_name' => $data['orig_name'], 'uploaded_by_user_id' => $this->current_user->user_id, 'mime_type' => $data['file_type'], 'is_image' => $data['is_image'], 'thumbnail_name' => $thumbnail_name);
             $file_id = $this->document_model->insert_file($file_data);
             if ($file_id) {
                 $status = "success";
                 $msg = "File successfully uploaded";
                 if (validate_integer($document_type_id) && (int) $document_type_id == 1) {
                     $job_id = $this->input->post('ref_id');
                     if (validate_integer($job_id) && (int) $job_id == 1) {
                         $this->load->model('job_model', 'job_m');
                         $job = $this->job_m->get_job_by_id($job_id);
                         trigger_trip("new_job_document", $job['company_id'], array('document_id' => $file_id, 'job_id' => $job_id, 'created_by' => $this->current_user->user_id));
                     }
                 }
                 if (ENVIRONMENT == "production" || ENVIRONMENT == 'testing') {
                     try {
                         //move the file
                         //upload it to s3 also...
                         $this->load->library('s3');
                         //$input_file = $this->s3->inputFile($file_element_name);
                         $bucket = 'workdeskpro';
                         $input = $this->s3->inputResource(fopen($data['full_path'], "rb"), filesize($data['full_path']));
                         if ($this->s3->putObject($input, $bucket, DOCUMENT_FOLDER . $data['file_name'], S3::ACL_PUBLIC_READ)) {
                             //remove the locally uploaded file
                             @unlink($data['full_path']);
                             if ($data['is_image']) {
                                 //do same with thumbnail...
                                 $input = $this->s3->inputResource(fopen($data['file_path'] . $thumbnail_name, "rb"), filesize($data['file_path'] . $thumbnail_name));
                                 $this->s3->putObject($input, $bucket, DOCUMENT_FOLDER . $thumbnail_name, S3::ACL_PUBLIC_READ);
                                 @unlink($data['file_path'] . $thumbnail_name);
                             }
                         } else {
                             $status = "error";
                             $msg = "File successfully uploaded, but transfer to CDN failed";
                         }
                     } catch (Exception $e) {
                         $status = "error";
                         $msg = "File successfully uploaded, but transfer to CDN failed";
                     }
                 }
             } else {
                 unlink($data['full_path']);
                 $status = "error";
                 $msg = "Something went wrong when saving the file, please try again.";
             }
         }
         @unlink($_FILES[$file_element_name]);
     }
     echo json_encode(array('status' => $status, 'msg' => $msg, 'file_id' => $file_id));
 }