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