<?php

/*************************************************************************/
/* DOCEBO - Content Management System                                    */
/* ============================================                          */
/*                                                                       */
/* Copyright (c) 2004 by Giovanni Derks                                  */
/* http://www.spaghettilearning.com                                      */
/*                                                                       */
/* This program is free software. You can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by  */
/* the Free Software Foundation; either version 2 of the License.        */
/*************************************************************************/

require_once( 'core/class/class.listview.php' );
require_once( 'core/class/class.treedb.php' );
require_once( 'core/class/class.treeview.php' );

define(_FPATH, "files/docs/");

class Docs_DataRetriever extends DataRetriever {
	var $idFolder = 0;

	// set the folder
	function setFolder( $idFolder ) { $this->idFolder = $idFolder; }

	// getRows: overload of method of the DataRetriever class
	// execute query for data retrieving
	// tipically called from listView
	function getRows( $startRow, $numRows ) {

		$t1=$this->prefix."_docs";
		$t2=$this->prefix."_docs_info";

		if (!defined("_BLOCK_LANG")) $lang=get_lang(); else $lang=_BLOCK_LANG;
		$query = "SELECT idDocs, title, fname, real_fname, publish, sdesc "
			."FROM $t1 "
			."LEFT JOIN $t2 ON ($t2.idd=$t1.idDocs AND $t2.lang='$lang') "
			."WHERE idFolder='". (int)$this->idFolder . "' AND publish='1'";

		//echo ($query);
		return $this->_getData( $query, $startRow, $numRows );
	}

	// fetchRecord: overload of method of the DataRetriever class
	function fetchRecord() {
		// fetch a record from record set
		include_once("core/manDateTime.php");

		$arrData = parent::fetchRecord();
		if( $arrData === FALSE )
			return FALSE;

		include_once("core/manMimeType.php");


		// Tipo:
		$fn=$arrData["real_fname"];
		$expFileName = explode('.', $fn);
		$totPart = count($expFileName) - 1;
		$mime=mimetype($expFileName[$totPart]);
		$img="<img src=\"".getPathImage().mimeDetect($fn)."\" alt=\"$mime\" title=\"$mime\" />\n";
		$arrData["type"]=$img;

		if(!isset($arrData["sdesc"])) $arrData["sdesc"]="&nbsp;";

		$url="";
		$url.="admin.php?modulename=manpage&op=modblock&write=1&amp;idBlock=".$_GET["idBlock"]."&amp;idSubdivision=".$_GET["idSubdivision"];
		$url.="&amp;act_op=additem&amp;type=docs&amp;item_id=".$arrData['idDocs'];
		$arrData['attach'] ="<a href=\"$url\">";
		$arrData['attach'].="<img src=\"".getPathImage()."standard/attach.gif\" alt=\""._ATTACH."\" title=\""._ATTACH."\" /></a>";


		return $arrData;
	}
}

/**
 * Customizaton of ListView class
 **/
class Docs_ListView extends ListView {

	var $new_perm;
	var $mod_perm;
	var $rem_perm;

	function Docs_ListView( $title, &$data, &$rend, $id ) {
		parent::ListView( $title, $data, $rend, $id );
		$this->new_perm = funAdminAccess('NEW', false);
		$this->mod_perm = funAdminAccess('MOD', false);
		$this->rem_perm = funAdminAccess('REM', false);

	}

	// utility function
	function _createColInfo( $label, $hClass, $fieldClass, $data, $toDisplay, $sortable ) {
		return array( 	'hLabel' => $label,
						'hClass' => $hClass,
						'fieldClass' => $fieldClass,
						'data' => $data,
						'toDisplay' => $toDisplay,
						'sortable' => $sortable );
	}

	// overload
	function _getCols() {
		$colInfos = array();
//		$colInfos[] = $this->_createColInfo( _PUBDATE,'','','publish_date',true, false );
		$colInfos[] = $this->_createColInfo( _TYPE,'','align_center','type',true, false );
		$colInfos[] = $this->_createColInfo( _FILENAME,'','','fname',true, false );
		$colInfos[] = $this->_createColInfo( _TITLE,'','','title',true, false );
		$colInfos[] = $this->_createColInfo( _SHORTDESC,'','','sdesc',true, false );


		$colInfos[] = $this->_createColInfo(
		'<img src="'.getPathImage().'standard/attach.gif" alt="'._ATTACH.'" title="'._ATTACH.'" />' ,
		'image','image','attach', $this->mod_perm , false );

		return $colInfos;
	}

}

// customization of TreeDb for homerepo_dir
class docsDb extends TreeDb {

	function docsDb() {
		global $prefixCms;
		$this->table = $prefixCms . '_docs_dir';
		$this->fields = array( 'id' => 'id', 'idParent' => 'idParent', 'path' => 'path', 'lev' => 'lev' );
	}

}

class Docs_TreeView extends TreeView {

	var $idDocs;
	var $showbtn=0;
	var $show_icons=1;

	function getDocsSelected() { return $this->idDocs; }


   function expandPath( $path ) {
       $arrId = array();
       $splitPath = split('/', $path);
       unset( $splitPath[0] );
       $path = '';
       foreach( $splitPath as $tok ) {
           $path .= '/' . $tok;
           //echo "\n\n<!-- ".$path." -->";

           $folder = $this->tdb->getFolderByPath( $path );
           $arrId[] = $folder->id;
       }
       $this->selectedFolder=end($arrId);
       unset($arrId[count($arrId)-1]);
       $this->pathToExpand = array_flip($arrId);
   }


	function extendedParsing( $arrayState, $arrayExpand, $arrayCompress ) {
		global $prefixCms;

	   if( $this->pathToExpand != NULL ) {
	       if( is_array($this->expandList) ) {
	           $this->expandList = $this->expandList + $this->pathToExpand;
           }
	       else {
	           $this->expandList = $this->pathToExpand;
           }
	   }

		parent::extendedParsing( $arrayState, $arrayExpand, $arrayCompress );
	}

}


function &createTreeView( $withContents = TRUE, $multiSelect = FALSE, $withActions = FALSE, $sel_path="" ) {
		global $prefixCms;
		$dirDb = new docsDb();
		$treeView = new Docs_TreeView( $dirDb, 'docs' );
		if ($sel_path != NULL) $treeView->expandPath($sel_path); // espande l'albero in base al path selezionato
		$treeView->parsePositionData( $_POST, $_POST, $_POST );

			$dataRetriever = new Docs_DataRetriever( NULL, $prefixCms );
			$typeOneRenderer = new typeOne(20);
			$listView = new Docs_ListView( '', $dataRetriever, $typeOneRenderer, 'idDocs');

			$listView->multiSelect = $multiSelect;

			$listView->parsePositionData( $_POST );

			$dataRetriever->setFolder( $treeView->selectedFolder );

			$listView->addurl = $treeView->_getOpNewFolderId();

			$treeView->setlistView( $listView );

		return $treeView;
}

?>