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\Functions\FunctionsEdit; 24use Fisharebest\Webtrees\GedcomRecord; 25use Fisharebest\Webtrees\Html; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\View; 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 $end_jd = $show_future ? 99999999 : WT_CLIENT_JD; 71 72 $xrefs = Database::prepare( 73 "SELECT DISTINCT d_gid FROM `##dates`" . 74 " WHERE d_file = :tree_id AND d_fact = '_TODO' AND d_julianday1 < :jd" 75 )->execute([ 76 'tree_id' => $WT_TREE->getTreeId(), 77 'jd' => $end_jd, 78 ])->fetchOneColumn(); 79 80 $content = ''; 81 $content .= '<table ' . Datatables::researchTaskTableAttributes() . '>'; 82 $content .= '<thead><tr>'; 83 $content .= '<th>' . I18N::translate('Date') . '</th>'; 84 $content .= '<th>' . I18N::translate('Record') . '</th>'; 85 $content .= '<th>' . I18N::translate('Username') . '</th>'; 86 $content .= '<th>' . I18N::translate('Research task') . '</th>'; 87 $content .= '</tr></thead><tbody>'; 88 89 foreach ($xrefs as $xref) { 90 $record = GedcomRecord::getInstance($xref, $WT_TREE); 91 if ($record->canShow()) { 92 foreach ($record->getFacts('_TODO') as $fact) { 93 $user_name = $fact->getAttribute('_WT_USER'); 94 if ($user_name === Auth::user()->getUserName() || !$user_name && $show_unassigned || $user_name && $show_other) { 95 $content .= '<tr>'; 96 $content .= '<td data-sort="' . $fact->getDate()->julianDay() . '">' . $fact->getDate()->display() . '</td>'; 97 $content .= '<td data-sort="' . Html::escape($record->getSortName()) . '"><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></td>'; 98 $content .= '<td>' . $user_name . '</td>'; 99 $content .= '<td dir="auto">' . $fact->getValue() . '</td>'; 100 $content .= '</tr>'; 101 } 102 } 103 } 104 } 105 106 $content .= '</tbody></table>'; 107 if (empty($xrefs)) { 108 $content .= '<p>' . I18N::translate('There are no research tasks in this family tree.') . '</p>'; 109 } 110 111 if ($template) { 112 if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) { 113 $config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]); 114 } else { 115 $config_url = ''; 116 } 117 118 return View::make('blocks/template', [ 119 'block' => str_replace('_', '-', $this->getName()), 120 'id' => $block_id, 121 'config_url' => $config_url, 122 'title' => $this->getTitle(), 123 'content' => $content, 124 ]); 125 } else { 126 return $content; 127 } 128 } 129 130 /** {@inheritdoc} */ 131 public function loadAjax() { 132 return false; 133 } 134 135 /** {@inheritdoc} */ 136 public function isUserBlock() { 137 return true; 138 } 139 140 /** {@inheritdoc} */ 141 public function isGedcomBlock() { 142 return true; 143 } 144 145 /** 146 * An HTML form to edit block settings 147 * 148 * @param int $block_id 149 */ 150 public function configureBlock($block_id) { 151 if (Filter::postBool('save') && Filter::checkCsrf()) { 152 $this->setBlockSetting($block_id, 'show_other', Filter::postBool('show_other')); 153 $this->setBlockSetting($block_id, 'show_unassigned', Filter::postBool('show_unassigned')); 154 $this->setBlockSetting($block_id, 'show_future', Filter::postBool('show_future')); 155 } 156 157 $show_other = $this->getBlockSetting($block_id, 'show_other', self::DEFAULT_SHOW_OTHER); 158 $show_unassigned = $this->getBlockSetting($block_id, 'show_unassigned', self::DEFAULT_SHOW_UNASSIGNED); 159 $show_future = $this->getBlockSetting($block_id, 'show_future', self::DEFAULT_SHOW_FUTURE); 160 161 ?> 162 <p> 163 <?= 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.') ?> 164 <?= 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.') ?> 165 <?= I18N::translate('Research tasks are stored using the custom GEDCOM tag “_TODO”. Other genealogy applications may not recognize this tag.') ?> 166 </p> 167 <?php 168 169 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_other">'; 170 echo I18N::translate('Show research tasks that are assigned to other users'); 171 echo '</div><div class="col-sm-9">'; 172 echo Bootstrap4::radioButtons('show_other', FunctionsEdit::optionsNoYes(), $show_other, true); 173 echo '</div></div>'; 174 175 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_unassigned">'; 176 echo I18N::translate('Show research tasks that are not assigned to any user'); 177 echo '</div><div class="col-sm-9">'; 178 echo Bootstrap4::radioButtons('show_unassigned', FunctionsEdit::optionsNoYes(), $show_unassigned, true); 179 echo '</div></div>'; 180 181 echo '<div class="form-group row"><label class="col-sm-3 col-form-label" for="show_future">'; 182 echo I18N::translate('Show research tasks that have a date in the future'); 183 echo '</div><div class="col-sm-9">'; 184 echo Bootstrap4::radioButtons('show_future', FunctionsEdit::optionsNoYes(), $show_future, true); 185 echo '</div></div>'; 186 } 187} 188