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
8/**
9 * Handler class for File
10 *
11 * Letter key: ~A~
12 *
13 */
14class Tracker_Field_File extends Tracker_Field_Abstract
15{
16	public static function getTypes()
17	{
18		return [
19			'A' => [
20				'name' => tr('Attachment'),
21				'description' => tr('Deprecated in favor of the Files field.'),
22				'help' => 'Attachment Field',
23				'prefs' => ['trackerfield_file'],
24				'tags' => ['deprecated'],
25				'warning' => tra('Deprecated in favor of the Files field.'),
26				'default' => 'n',
27				'params' => [
28					'listview' => [
29						'name' => tr('List View'),
30						'description' => tr('Defines how attachments will be displayed within the field.'),
31						'options' => [
32							'n' => tr('name'),
33							't' => tr('type'),
34							'ns' => tr('name, size'),
35							'nts' => tr('name, type, size'),
36							'u' => tr('uploader'),
37							'm' => tr('mediaplayer'),
38						],
39						'filter' => 'alpha',
40						'legacy_index' => 0,
41					],
42				],
43			],
44		];
45	}
46
47	function getFieldData(array $requestData = [])
48	{
49		$ins_id = $this->getInsertId();
50
51		if (! empty($requestData) && isset($_FILES[$ins_id]) && is_uploaded_file($_FILES[$ins_id]['tmp_name'])) {
52			$data['old_value'] = $this->getValue();
53			$data['value'] = file_get_contents($_FILES[$ins_id]['tmp_name']);
54			$data['file_type'] = $_FILES[$ins_id]['type'];
55			$data['file_size'] = $_FILES[$ins_id]['size'];
56			$data['file_name'] = $_FILES[$ins_id]['name'];
57		} else {
58			$data = ['value' => $this->getValue()];
59			if (! empty($data['value']) && (int) $data['value'] > 0) {
60				$attachment = TikiLib::lib('trk')->get_item_attachment($data['value']);
61				$data['filename'] = $attachment['filename'];
62			}
63		}
64		return $data;
65	}
66
67	function renderInput($context = [])
68	{
69		return $this->renderTemplate('trackerinput/file.tpl', $context);
70	}
71
72	function renderInnerOutput($context = [])
73	{
74		$att_id = $this->getValue();
75
76		if (empty($att_id)) {
77			return '';
78		}
79
80		if ($context['list_mode'] === 'csv') {
81			global $base_url;
82			return $base_url . 'tiki-download_item_attachment.php?attId=' . $att_id;	// should something to do with export_attachment() happen here?
83		}
84
85		$attachment = TikiLib::lib('trk')->get_item_attachment($att_id);
86
87		$smarty = TikiLib::lib('smarty');
88		$smarty->loadPlugin('smarty_block_self_link');
89		$smarty->loadPlugin('smarty_function_icon');
90
91		$link = smarty_block_self_link(
92			[
93				'_script' => 'tiki-download_item_attachment.php',
94				'attId' => $att_id,
95			],
96			smarty_function_icon(['_id' => 'disk', 'alt' => tra('Download')], $smarty->getEmptyInternalTemplate()) . ' ' .
97			$attachment['filename'],
98			$smarty
99		);
100		return $link;
101	}
102
103	function handleSave($value, $oldValue)
104	{
105		global $prefs, $user;
106		$tikilib = TikiLib::lib('tiki');
107
108		$trackerId = $this->getConfiguration('trackerId');
109		$file_name = $this->getConfiguration('file_name');
110		$file_size = $this->getConfiguration('file_size');
111		$file_type = $this->getConfiguration('file_type');
112
113		$perms = Perms::get('tracker', $trackerId);
114		if ($perms->attach_trackers && $file_name) {
115			if ($prefs['t_use_db'] == 'n') {
116				$fhash = md5($file_name . $tikilib->now);
117				if (file_put_contents($prefs['t_use_dir'] . $fhash, $value) === false) {
118					$smarty = TikiLib::lib('smarty');
119					$smarty->assign('msg', tra('Cannot write to this file:') . $fhash);
120					$smarty->display("error.tpl");
121					die;
122				}
123				$value = '';
124			} else {
125				$fhash = 0;
126			}
127
128			$trklib = TikiLib::lib('trk');
129			$value = $trklib->replace_item_attachment($oldValue, $file_name, $file_type, $file_size, $value, '', $user, $fhash, '', '', $trackerId, $this->getItemId(), '', false);
130		}
131
132		return [
133			'value' => $value,
134		];
135	}
136
137	function getDocumentPart(Search_Type_Factory_Interface $typeFactory)
138	{
139		return [
140		];
141	}
142
143	function getProvidedFields()
144	{
145		return [];
146	}
147
148	function getGlobalFields()
149	{
150		return [];
151	}
152}
153