1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Search_ContentSource_SheetSource implements Search_ContentSource_Interface
9{
10	private $db;
11
12	function __construct()
13	{
14		$this->db = TikiDb::get();
15	}
16
17	function getDocuments()
18	{
19		return $this->db->table('tiki_sheets')->fetchColumn('sheetId', []);
20	}
21
22	function getDocument($objectId, Search_Type_Factory_Interface $typeFactory)
23	{
24		$sheetlib = TikiLib::lib('sheet');
25
26		$info = $sheetlib->get_sheet_info($objectId);
27
28		if (! $info) {
29			return false;
30		}
31
32		$values = $this->db->table('tiki_sheet_values');
33		$contributors = $values->fetchColumn(
34			$values->expr('DISTINCT `user`'),
35			[
36				'sheetId' => $objectId,
37			]
38		);
39		$lastModif = $values->fetchOne(
40			$values->max('begin'),
41			[
42				'sheetId' => $objectId,
43			]
44		);
45
46		$loader = new TikiSheetDatabaseHandler($objectId);
47		$writer = new TikiSheetCSVHandler('php://output');
48
49		$grid = new TikiSheet;
50		$grid->import($loader);
51
52		$grid->export($writer);
53		$text = $writer->output;
54
55		$data = [
56			'title' => $typeFactory->sortable($info['title']),
57			'description' => $typeFactory->sortable($info['description']),
58			'modification_date' => $typeFactory->timestamp($lastModif),
59			'date' => $typeFactory->timestamp($lastModif),
60			'contributors' => $typeFactory->multivalue($contributors),
61
62			'sheet_content' => $typeFactory->plaintext($text),
63
64			'view_permission' => $typeFactory->identifier('tiki_p_view_sheet'),
65		];
66
67		return $data;
68	}
69
70	function getProvidedFields()
71	{
72		return [
73			'title',
74			'description',
75			'modification_date',
76			'date',
77			'contributors',
78
79			'sheet_content',
80
81			'view_permission',
82		];
83	}
84
85	function getGlobalFields()
86	{
87		return [
88			'title' => true,
89			'description' => true,
90			'date' => true,
91
92			'sheet_content' => false,
93		];
94	}
95}
96