1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 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\Bootstrap4; 20use Fisharebest\Webtrees\Database; 21use Fisharebest\Webtrees\Datatables; 22use Fisharebest\Webtrees\Filter; 23use Fisharebest\Webtrees\FontAwesome; 24use Fisharebest\Webtrees\Functions\FunctionsEdit; 25use Fisharebest\Webtrees\GedcomRecord; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Theme; 28 29/** 30 * Class ResearchTaskModule 31 */ 32class ResearchTaskModule extends AbstractModule implements ModuleBlockInterface { 33 const DEFAULT_SHOW_OTHER = '1'; 34 const DEFAULT_SHOW_UNASSIGNED = '1'; 35 const DEFAULT_SHOW_FUTURE = '1'; 36 const DEFAULT_BLOCK = '1'; 37 38 /** {@inheritdoc} */ 39 public function getTitle() { 40 return /* I18N: Name of a module. Tasks that need further research. */ I18N::translate('Research tasks'); 41 } 42 43 /** {@inheritdoc} */ 44 public function getDescription() { 45 return /* I18N: Description of “Research tasks” module */ I18N::translate('A list of tasks and activities that are linked to the family tree.'); 46 } 47 48 /** 49 * Generate the HTML content of this block. 50 * 51 * @param int $block_id 52 * @param bool $template 53 * @param string[] $cfg 54 * 55 * @return string 56 */ 57 public function getBlock($block_id, $template = true, $cfg = []) { 58 global $ctype, $WT_TREE; 59 60 $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 61 $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 62 $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 63 64 foreach (['show_unassigned', 'show_other', 'show_future'] as $name) { 65 if (array_key_exists($name, $cfg)) { 66 $$name = $cfg[$name]; 67 } 68 } 69 70 $id = $this->getName() . $block_id; 71 $class = $this->getName() . '_block'; 72 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 73 $title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' '; 74 } else { 75 $title = ''; 76 } 77 $title .= $this->getTitle(); 78 79 $end_jd = $show_future ? 99999999 : WT_CLIENT_JD; 80 81 $xrefs = Database::prepare( 82 "SELECT DISTINCT d_gid FROM `##dates`" . 83 " WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd" 84 )->execute([ 85 'tree_id' => $WT_TREE->getTreeId(), 86 'jd' => $end_jd, 87 ])->fetchOneColumn(); 88 89 $content = ''; 90 $content .= '<table ' . Datatables::researchTaskTableAttributes() . '>'; 91 $content .= '<thead><tr>'; 92 $content .= '<th>' . I18N::translate('Date') . '</th>'; 93 $content .= '<th>' . I18N::translate('Record') . '</th>'; 94 $content .= '<th>' . I18N::translate('Username') . '</th>'; 95 $content .= '<th>' . I18N::translate('Research task') . '</th>'; 96 $content .= '</tr></thead><tbody>'; 97 98 foreach ($xrefs as $xref) { 99 $record = GedcomRecord::getInstance($xref, $WT_TREE); 100 if ($record->canShow()) { 101 foreach ($record->getFacts('_TODO') as $fact) { 102 $user_name = $fact->getAttribute('_WT_USER'); 103 if ($user_name === Auth::user()->getUserName() || !$user_name && $show_unassigned || $user_name && $show_other) { 104 $content .= '<tr>'; 105 $content .= '<td data-sort="' . $fact->getDate()->julianDay() . '">' . $fact->getDate()->display() . '</td>'; 106 $content .= '<td data-sort="' . Filter::escapeHtml($record->getSortName()) . '"><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>'; 107 $content .= '<td>' . $user_name . '</td>'; 108 $content .= '<td dir="auto">' . $fact->getValue() . '</td>'; 109 $content .= '</tr>'; 110 } 111 } 112 } 113 } 114 115 $content .= '</tbody></table>'; 116 if (empty($xrefs)) { 117 $content .= '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 118 } 119 120 if ($template) { 121 return Theme::theme()->formatBlock($id, $title, $class, $content); 122 } else { 123 return $content; 124 } 125 } 126 127 /** {@inheritdoc} */ 128 public function loadAjax() { 129 return false; 130 } 131 132 /** {@inheritdoc} */ 133 public function isUserBlock() { 134 return true; 135 } 136 137 /** {@inheritdoc} */ 138 public function isGedcomBlock() { 139 return true; 140 } 141 142 /** 143 * An HTML form to edit block settings 144 * 145 * @param int $block_id 146 */ 147 public function configureBlock($block_id) { 148 if (Filter::postBool('save') && Filter::checkCsrf()) { 149 $this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other')); 150 $this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned')); 151 $this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future')); 152 } 153 154 $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 155 $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 156 $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 157 158 ?> 159 <p> 160 <?= 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.') ?> 161 <?= 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.') ?> 162 <?= I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”. Other genealogy applications may not recognize this tag.') ?> 163 </p> 164 <?php 165 166 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_other">'; 167 echo I18N::translate('Show research tasks that are assigned to other users'); 168 echo '</div><div class="col-sm-9">'; 169 echo Bootstrap4::radioButtons('show_other', FunctionsEdit::optionsNoYes(), $show_other, true); 170 echo '</div></div>'; 171 172 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_unassigned">'; 173 echo I18N::translate('Show research tasks that are not assigned to any user'); 174 echo '</div><div class="col-sm-9">'; 175 echo Bootstrap4::radioButtons('show_unassigned', FunctionsEdit::optionsNoYes(), $show_unassigned, true); 176 echo '</div></div>'; 177 178 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_future">'; 179 echo I18N::translate('Show research tasks that have a date in the future'); 180 echo '</div><div class="col-sm-9">'; 181 echo Bootstrap4::radioButtons('show_future', FunctionsEdit::optionsNoYes(), $show_future, true); 182 echo '</div></div>'; 183 } 184} 185