<?php

	require('config.php');
	require('./class/dossier.class.php');
	require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
	require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
	dol_include_once('/recouvrement/admin/recouvrementsetuppage.php');

	$ATMdb = new TPDOdb;

	$action		= GETPOST('action','alpha');
	$confirm	= GETPOST('confirm','alpha');
	$id			= GETPOST('id','int');

	$object = new TRecDossier;
	$object->load($ATMdb, $id);
	$object->id = $object->rowid;
	$upload_dir = DOL_DATA_ROOT.'/recouvrement/'.$object->ref;

	if (file_exists(DOL_DOCUMENT_ROOT . '/core/tpl/document_actions_pre_headers.tpl.php')) {
		include_once DOL_DOCUMENT_ROOT . '/core/tpl/document_actions_pre_headers.tpl.php';
	}
	if (file_exists(DOL_DOCUMENT_ROOT . '/core/actions_linkedfiles.inc.php')) {
		include_once DOL_DOCUMENT_ROOT . '/core/actions_linkedfiles.inc.php';
	}

	llxHeader('',$langs->trans("Dossier"),'','','','',$extrajs);

	$head = rec_prepare_head($object);
	dol_fiche_head($head, 'attachedfile', "Fiche du dossier", 0);

	if(!empty($action)) {
		switch($action) {
			case 'default':
				$object = new TRecDossier;
				$object->load($ATMdb, $id);
				_gestionDocument($object, $action);
			case 'delete':

				if(!empty($_REQUEST['urlfile'])){
					$object = new TRecDossier;
					$object->load($ATMdb, $id);
					$upload_dir = $conf->recouvrement->multidir_output[$object->entity].'/';
					
					if(file_exists($upload_dir.$_REQUEST['urlfile']))
						unlink($upload_dir.$_REQUEST['urlfile']);
				}

				$object = new TRecDossier;
				$object->load($ATMdb, $id);

				_gestionDocument($object, $action);

				break;
		}

	} else {

		$object = new TRecDossier;
		$object->load($ATMdb, $id);

		_gestionDocument($object, $action);
	}

	
	
	llxFooter();
	
	
	
	function _gestionDocument($object, $action){
		global $db,$user,$conf,$langs;

		echo '<br><br>';
		
		@mkdir($object->getPath(),0777,true);

		$upload_dir = $object->getPath();

		// Post file
		if (GETPOST('sendit') && ! empty($conf->global->MAIN_UPLOAD_DOC))
		{
			if ($object->getId())
			{
				dol_add_file_process($upload_dir,0,1,'userfile');
			}
		}

		//$formfile=new FormFile($db);
		$form = new Form($db);
		// Show upload form
		//$formfile->form_attach_new_file($_SERVER["PHP_SELF"].'?id='.$object->getId(),'',0,0,$user->rights->recouvrement->dossier->read,50,$object);
		$filearray=dol_dir_list($object->getPath());

		// Pour ne pas afficher que le dossier "thumbs" auto-généré soit affiché dans la liste des fichiers joints

		$i = 0;
		foreach($filearray as $f) {
			/*echo "<pre>";
			echo count(explode(".",$f['name']));
			echo "</pre>";
			exit;*/
			if(!is_file($f['fullname'])) {
				unset($filearray[$i]); // On retire du tableau les éléments qui ne sont pas des fichiers.
			}
			$i++;
		}

		//$_SERVER["PHP_SELF"]
		//function list_of_documents($filearray,$object,$modulepart,$param='',$forcedownload=0,$relativepath='',$permtodelete=1,$useinecm=0,$textifempty='',$maxlength=0,$title='',$url='')
		// List of document
		$modulepart='recouvrement';
		$object->id = $object->rowid;
		$permission = $user->rights->propal->creer;
		$param = '&id=' . $object->id;
		include_once DOL_DOCUMENT_ROOT . '/core/tpl/document_actions_post_headers.tpl.php';
		//$formfile->list_of_documents($filearray,$object,'recouvrement', $param);

	}

