Files
2021-07-10 16:25:16 +02:00

360 lines
11 KiB
PHP

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Description of Documents
*
* @author Theo Weijmer
* @date juni 11, 2013
*
* @purpose
*
*/
class Documents extends MY_Controller {
public $rootpath;
function __construct(){
parent::__construct();
if(!$this->userid) return;
$models = array('ijkpartituur/Documents_model',
'ijkpartituur/Modhulpmiddelen',
'ijkpartituur/Modhelpdesk',
'ijkpartituur/Doctypes_model',
'ijkpartituur/Doctags_model',
'ijkpartituur/Tags_model',
'ijkpartituur/ModReferenties');
$this->load->model($models);
$this->rootpath = str_replace('/public','',$_SERVER['DOCUMENT_ROOT']);
if ( ! $this->check_online_status() ) {
echo json_encode( array('offline' => TRUE) );
die;
};// if
}
function index(){
$doctypes = $this->Doctypes_model->get_details();
echo json_encode(array('doctypes'=>$doctypes));
}
public function get_documents(){
if (!$this->userid) return;
$hulpid = $this->uri->segment(4);
$upload = $this->check_upload($hulpid);
$documents = $this->Documents_model->get_details(array('document_hulpmiddel_id'=>$hulpid));
$documentid = $nontags = $selectedtags = FALSE;
if($documents && count($documents)==1){
$documentid = $documents[0]->id;
$selectedtags = $this->Doctags_model->get_selected_tags($documentid);
$nontags = $this->Doctags_model->get_non_selected_tags($documentid);
}
$data = array( 'documents' => $documents ,
'upload' => $upload ,
'selectedtags' => $selectedtags,
'nontags' => $nontags ,
'documentid' => $documentid );
echo json_encode($data);
}
public function upload_document(){
if (!$this->userid) return;
$hulpid = $this->uri->segment(4);
$doctypeid = $this->uri->segment(5);
$docid = $this->uri->segment(6);
if(!$this->check_upload($docid) && $docid){
$result = array('errors'=>json_encode('<p>u heeft geen rechten om documenten/dit document te uploaden</p>'));
}else{
$documentname = $this->input->post('name-upload-document');
// $config['upload_path'] = $this->rootpath.'/tmp_uploads/';
$config['upload_path'] = $this->partituurUpload;
$config['allowed_types'] = 'doc|docx|jpg|gif|png|pdf|txt|ppt|pptx';
$config['max_size'] = '10000';
$this->load->library('upload', $config);
$uploadresult = $this->upload->do_upload();
if (!$uploadresult && !$docid){
$result = array('errors'=>json_encode($config['upload_path']));
//$result = array('errors'=>json_encode($this->upload->display_errors()));
}else{
$data = array(
'document_name' => $documentname ,
'document_doctype_id' => $doctypeid ,
'document_hulpmiddel_id' => $hulpid );
if(!$uploadresult){
$result = array('errors'=>json_encode('<p>de gegevens zijn opgeslagen</p>'));
}else{
//check if there is already a file (docid!=0) unlink the old file
if($docid){
$file = $this->Documents_model->get_record_by_id($docid);
unlink($this->partituurSchatkist.$file->document_filename.$file->document_extension);
}
$result = array('errors'=>json_encode('<p>uw bestand is succesvol geupload</p>'));
$document = $this->upload->data();
$this->load->helper('string');
$newname = sha1(random_string('alnum', 16));
$data['document_original'] = $document['file_name'];
$data['document_extension'] = $document['file_ext'] ;
$data['document_size'] = $document['file_size'];
$data['document_filename'] = $newname ;
$oldname = $this->partituurUpload.$document['file_name'];
$newname = $this->partituurSchatkist.$newname.$document['file_ext'];
rename($oldname, $newname);
}
$this->Documents_model->save_record($data, $docid);
}
}
$this->load->view('ijkpartituur/popups/uploadresult_view', $result);
}
public function download_document(){
if (!$this->userid) return;
$this->load->helper('download');
$docid = $this->uri->segment(4);
$original='test';
if ($docid) {
$file = $this->Documents_model->get_record_by_id($docid);
$original = $file->document_original;
$data = file_get_contents($this->partituurSchatkist.$file->document_filename.$file->document_extension);
}
else {
$info = $this->Modhelpdesk->get_helpdesk();
$filenaam = $this->rootpath.'/documents/'.$info->handleiding;
$original = "handleiding.pdf";
$data= file_get_contents($filenaam);
};
force_download($original, $data);
}
public function downloadHandleiding(){
$this->load->helper('download');
$info = $this->Modhelpdesk->get_helpdesk();
$filenaam = $this->rootpath.'/'.$info->handleiding;
$data = file_get_contents($filenaam);
force_download("handleidingPartituur", $data);
}
public function delete_document(){
if (!$this->userid) return;
if ($this->Autorisaties_model->check_autorisatie($this->userrole, WRITER) <=0 ) {
return;
}
$docid = $this->uri->segment(4);
$document = $this->Documents_model->get_record_by_id($docid);
if($this->check_upload($document->document_hulpmiddel_id)){
$this->Doctags_model->delete_record_where(array('doctag_document_id'=>$docid));
$this->Documents_model->delete_record($docid);
$filename = $document->document_filename.$document->document_extension;
unlink($this->rootpath.'/schatkist/'.$filename);
}
}
public function set_tags(){
if (!$this->userid) return;
$documentid = $this->uri->segment(4);
$this->send_tags($documentid);
}
public function save_tag(){
$documentid = $this->uri->segment(4);
$tagname = $this->clean_input('tagname');
$tag = $this->Tags_model->get_records_where(array('UCASE(tag_name)'=>strtoupper($tagname)), TRUE);
if(!$tag){
$newtag = $this->Tags_model->insert_record(array('tag_name'=>$tagname));
$tag = $this->Tags_model->get_record_by_id($newtag);
}
$data = array('doctag_tag_id'=>$tag->id, 'doctag_document_id'=>$documentid);
if(!$this->Doctags_model->check_for_record($data)) $this->Doctags_model->insert_record($data);
$this->send_tags($documentid);
}
public function add_tags(){
if ($this->Autorisaties_model->check_autorisatie($this->userrole, WRITER) <=0) return;
$documentid = $this->uri->segment(4);
$newtags = $this->clean_input('newtags');
if($newtags){
foreach($newtags as $newtag){
$data = array('doctag_document_id'=>$documentid, 'doctag_tag_id'=>$newtag);
if(!$this->Doctags_model->check_for_record($data)){
$this->Doctags_model->insert_record($data);
}
}
}
$this->send_tags($documentid);
}
public function remove_tag(){
$documentid = $this->uri->segment(4);
$tags = $this->clean_input('newtags');
if($tags){
foreach($tags as $tag){
$this->Doctags_model->delete_record_where(array('doctag_tag_id'=>$tag));
$this->Tags_model->delete_record($tag);
}
}
$this->send_tags($documentid);
}
public function remove_tags(){
$documentid = $this->uri->segment(3);
$removetags = $this->clean_input('removetags');
if($removetags){
foreach($removetags as $removetag){
$data = array('doctag_document_id'=>$documentid, 'doctag_tag_id'=>$removetag);
$this->Doctags_model->delete_record_where($data);
}
}
$this->send_tags($documentid);
}
private function send_tags($documentid){
$nontags = $this->Doctags_model->get_non_selected_tags($documentid);
$selectedtags = $this->Doctags_model->get_selected_tags($documentid);
echo json_encode(array('nontags'=>$nontags, 'selectedtags'=>$selectedtags));
}
private function check_upload($hulpid){
if($this->super){
return TRUE;
}else{
$orgid = $this->Modhulpmiddelen->get_record_by_id($hulpid, 'bronID'); // was organisatie-ID
if($this->globorgid==$orgid){
return 1;
}else{
return FALSE;
}
}
}
private function check_download($docid){
if($this->super){
return TRUE;
}else{
$hulpid = $this->Documents_model->get_record_by_id($docid, 'document_hulpmiddel_id');
$orgid = $this->Modhulpmiddelen->get_record_by_id($hulpid, 'organisatie_id');
if($orgid==1){
return TRUE;
}elseif($orid==$this->globorgid){
return TRUE;
}else{
return FALSE;
}
}
}
public function LSUpload(){
if (!$this->userid) return;
$refID = $this->uri->segment(4);
$soort = $this->uri->segment(5);
if(!$this->super){
$result = array('errors'=>json_encode('<p>u heeft geen rechten om documenten te uploaden</p>'));
}else{
$toonnaam = $this->input->post('toonnaam');
$config['upload_path'] = $this->partituurUpload;
$config['allowed_types'] = 'doc|docx|jpg|gif|png|pdf|txt';
$config['max_size'] = '10000';
$this->load->library('upload', $config);
$uploadresult = $this->upload->do_upload();
if (!$uploadresult && !$refID){
$result = array('errors'=>json_encode($this->upload->display_errors()));
}else{
if(!$uploadresult){
$result = array('errors'=>json_encode('<p>de gegevens zijn opgeslagen</p>'));
}else{
//check if there is already a file (refID!=0) unlink the old file
if($refID){
$file = $this->ModReferenties->get_record_by_id($refID);
if ($file) {
if ($file->hashnaam) {
echo "file extension ".$file->doc_extension;
if (file_exists($this->rootpath.'/docs/'.$file->hashnaam.$file->doc_extension)) {
unlink($this->rootpath.'/docs/'.$file->hashnaam.$file->doc_extension);
};
}
}
}
$result = array('errors'=>json_encode('<p>Uw bestand is succesvol geupload</p>'));
$document = $this->upload->data();
$this->load->helper('string');
$newname = sha1(random_string('alnum', 16));
$data = array( 'naam' => $toonnaam,
'id' => $refID,
'docnaam' => $document['file_name'],
'soort' => $soort,
'srtlink' => 'file');
$data['doc_extension'] = $document['file_ext'];
$data['filesize'] = $document['file_size'];
$data['hashnaam'] = $newname;
$oldname = $this->rootpath.'/tmp_uploads/'.$document['file_name'];
$newname = $this->rootpath.'/docs/'.$newname.$document['file_ext'];
rename($oldname, $newname);
$refID = $this->ModReferenties->save_record($data, $refID);
}
}
}
$data = array('result' =>$result, 'id'=>$refID );
$this->load->view('ijkpartituur/popups/Toonuploadresult', $data) ;
}
public function LSDownloadDoc(){
if (!$this->userid) return;
$this->load->helper('download');
$refID= $this->uri->segment(4);
$original='test';
if ($refID) {
$file = $this->ModReferenties->get_record_by_id($refID);
$original = $file->docnaam;
$data = file_get_contents($this->rootpath."/docs/".$file->hashnaam.$file->doc_extension);
force_download($original, $data);
};
}
}