xref: /webtrees/app/Module/ResearchTaskModule.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Filter;
20use Fisharebest\Webtrees\Functions\FunctionsDb;
21use Fisharebest\Webtrees\Functions\FunctionsEdit;
22use Fisharebest\Webtrees\GedcomTag;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Theme;
25use Rhumsaa\Uuid\Uuid;
26
27/**
28 * Class ResearchTaskModule
29 */
30class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface {
31	/** {@inheritdoc} */
32	public function getTitle() {
33		return /* I18N: Name of a module.  Tasks that need further research.  */ I18N::translate('Research tasks');
34	}
35
36	/** {@inheritdoc} */
37	public function getDescription() {
38		return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.');
39	}
40
41	/**
42	 * Generate the HTML content of this block.
43	 *
44	 * @param int   $block_id
45	 * @param bool  $template
46	 * @param array $cfg
47	 *
48	 * @return string
49	 */
50	public function getBlock($block_id, $template = true, $cfg = array()) {
51		global $ctype, $controller, $WT_TREE;
52
53		$show_other      = $this->getBlockSetting($block_id, 'show_other', '1');
54		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', '1');
55		$show_future     = $this->getBlockSetting($block_id, 'show_future', '1');
56		$block           = $this->getBlockSetting($block_id, 'block', '1');
57
58		foreach (array('show_unassigned', 'show_other', 'show_future', 'block') as $name) {
59			if (array_key_exists($name, $cfg)) {
60				$$name = $cfg[$name];
61			}
62		}
63
64		$id    = $this->getName() . $block_id;
65		$class = $this->getName() . '_block';
66		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
67			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
68		} else {
69			$title = '';
70		}
71		$title .= $this->getTitle();
72
73		$table_id = Uuid::uuid4(); // create a unique ID
74
75		$controller
76			->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
77			->addInlineJavascript('
78			jQuery("#' . $table_id . '").dataTable({
79				dom: \'t\',
80				' . I18N::datatablesI18N() . ',
81				autoWidth: false,
82				paginate: false,
83				lengthChange: false,
84				filter: false,
85				info: true,
86				jQueryUI: true,
87				columns: [
88					/* 0-DATE */     { visible: false },
89					/* 1-Date */     { dataSort: 0 },
90					/* 1-Record */   null,
91					/* 2-Username */ null,
92					/* 3-Text */     null
93				]
94			});
95			jQuery("#' . $table_id . '").css("visibility", "visible");
96			jQuery(".loading-image").css("display", "none");
97		');
98
99		$content = '';
100		$content .= '<div class="loading-image">&nbsp;</div>';
101		$content .= '<table id="' . $table_id . '" style="visibility:hidden;">';
102		$content .= '<thead><tr>';
103		$content .= '<th>DATE</th>'; //hidden by datables code
104		$content .= '<th>' . GedcomTag::getLabel('DATE') . '</th>';
105		$content .= '<th>' . I18N::translate('Record') . '</th>';
106		if ($show_unassigned || $show_other) {
107			$content .= '<th>' . I18N::translate('Username') . '</th>';
108		}
109		$content .= '<th>' . GedcomTag::getLabel('TEXT') . '</th>';
110		$content .= '</tr></thead><tbody>';
111
112		$found  = false;
113		$end_jd = $show_future ? 99999999 : WT_CLIENT_JD;
114		foreach (FunctionsDb::getCalendarEvents(0, $end_jd, '_TODO', $WT_TREE) as $fact) {
115			$record    = $fact->getParent();
116			$user_name = $fact->getAttribute('_WT_USER');
117			if ($user_name === Auth::user()->getUserName() || !$user_name && $show_unassigned || $user_name && $show_other) {
118				$content .= '<tr>';
119				//-- Event date (sortable)
120				$content .= '<td>'; //hidden by datables code
121				$content .= $fact->getDate()->julianDay();
122				$content .= '</td>';
123				$content .= '<td class="wrap">' . $fact->getDate()->display() . '</td>';
124				$content .= '<td class="wrap"><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>';
125				if ($show_unassigned || $show_other) {
126					$content .= '<td class="wrap">' . $user_name . '</td>';
127				}
128				$text = $fact->getValue();
129				$content .= '<td class="wrap">' . $text . '</td>';
130				$content .= '</tr>';
131				$found = true;
132			}
133		}
134
135		$content .= '</tbody></table>';
136		if (!$found) {
137			$content .= '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>';
138		}
139
140		if ($template) {
141			if ($block) {
142				$class .= ' small_inner_block';
143			}
144
145			return Theme::theme()->formatBlock($id, $title, $class, $content);
146		} else {
147			return $content;
148		}
149	}
150
151	/** {@inheritdoc} */
152	public function loadAjax() {
153		return false;
154	}
155
156	/** {@inheritdoc} */
157	public function isUserBlock() {
158		return true;
159	}
160
161	/** {@inheritdoc} */
162	public function isGedcomBlock() {
163		return true;
164	}
165
166	/**
167	 * An HTML form to edit block settings
168	 *
169	 * @param int $block_id
170	 */
171	public function configureBlock($block_id) {
172		if (Filter::postBool('save') && Filter::checkCsrf()) {
173			$this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other'));
174			$this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned'));
175			$this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future'));
176			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
177		}
178
179		$show_other      = $this->getBlockSetting($block_id, 'show_other', '1');
180		$show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', '1');
181		$show_future     = $this->getBlockSetting($block_id, 'show_future', '1');
182		$block           = $this->getBlockSetting($block_id, 'block', '1');
183
184		?>
185		<tr>
186			<td colspan="2">
187				<?php echo I18N::translate('Research tasks are special events, added to individuals in your family tree, which identify the need for further research.  You can use them as a reminder to check facts against more reliable sources, to obtain documents or photographs, to resolve conflicting information, etc.'); ?>
188				<?php echo I18N::translate('To create new research tasks, you must first add “research task” to the list of facts and events in the family tree’s preferences.'); ?>
189				<?php echo I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”.  Other genealogy applications may not recognize this tag.'); ?>
190			</td>
191		</tr>
192		<?php
193
194		echo '<tr><td class="descriptionbox wrap width33">';
195		echo I18N::translate('Show research tasks that are assigned to other users');
196		echo '</td><td class="optionbox">';
197		echo FunctionsEdit::editFieldYesNo('show_other', $show_other);
198		echo '</td></tr>';
199
200		echo '<tr><td class="descriptionbox wrap width33">';
201		echo I18N::translate('Show research tasks that are not assigned to any user');
202		echo '</td><td class="optionbox">';
203		echo FunctionsEdit::editFieldYesNo('show_unassigned', $show_unassigned);
204		echo '</td></tr>';
205
206		echo '<tr><td class="descriptionbox wrap width33">';
207		echo I18N::translate('Show research tasks that have a date in the future');
208		echo '</td><td class="optionbox">';
209		echo FunctionsEdit::editFieldYesNo('show_future', $show_future);
210		echo '</td></tr>';
211
212		echo '<tr><td class="descriptionbox wrap width33">';
213		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
214		echo '</td><td class="optionbox">';
215		echo FunctionsEdit::editFieldYesNo('block', $block);
216		echo '</td></tr>';
217	}
218}
219