<?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' );

class News_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 ) {
		if (defined("_BLOCK_LANG")) $lang_filter="AND language='"._BLOCK_LANG."'"; else $lang_filter="";
		$query = "SELECT idNews, publish_date, title, short_desc, important, publish "
			." FROM ".$this->prefix."_news"
			." WHERE idFolder='". (int)$this->idFolder . "' AND publish='1' $lang_filter";
		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;

		$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=news&amp;item_id=".$arrData['idNews'];
		$arrData['attach'] ="<a href=\"$url\">";
		$arrData['attach'].="<img src=\"".getPathImage()."standard/attach.gif\" alt=\""._ATTACH."\" title=\""._ATTACH."\" /></a>";


		$arrData['publish_date']=conv_datetime($arrData['publish_date'], 0, _TIMEOFFSET);

		return $arrData;
	}
}

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

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

	function News_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( _TITLE,'','','title',true, false );
		$colInfos[] = $this->_createColInfo( _SHORTDESC,'','','short_desc',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 newsDb extends TreeDb {

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

}

class News_TreeView extends TreeView {

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

	function getNewsSelected() { return $this->idNews; }


   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 newsDb();
		$treeView = new News_TreeView( $dirDb, 'news' );
		if ($sel_path != NULL) $treeView->expandPath($sel_path); // espande l'albero in base al path selezionato
		$treeView->parsePositionData( $_POST, $_POST, $_POST );

			$dataRetriever = new News_DataRetriever( NULL, $prefixCms );
			$typeOneRenderer = new typeOne(20);
			$listView = new News_ListView( '', $dataRetriever, $typeOneRenderer, 'idNews');

			$listView->multiSelect = $multiSelect;

			$listView->parsePositionData( $_POST );

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

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

			$treeView->setlistView( $listView );

		return $treeView;
}

?>