Passed
Branch master (380e00)
by Greg
20:17
created

ReviewChangesModule   B

Complexity

Total Complexity 30

Size/Duplication

Total Lines 179
Duplicated Lines 11.73 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 16
dl 21
loc 179
rs 8.4614
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A getDescription() 0 3 1
F getBlock() 17 100 22
A loadAjax() 0 3 1
A isUserBlock() 0 3 1
A isGedcomBlock() 0 3 1
B configureBlock() 4 35 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 */
16
namespace Fisharebest\Webtrees\Module;
17
18
use Fisharebest\Webtrees\Auth;
19
use Fisharebest\Webtrees\Bootstrap4;
20
use Fisharebest\Webtrees\Database;
21
use Fisharebest\Webtrees\Filter;
22
use Fisharebest\Webtrees\Functions\FunctionsDate;
23
use Fisharebest\Webtrees\Functions\FunctionsEdit;
24
use Fisharebest\Webtrees\GedcomRecord;
25
use Fisharebest\Webtrees\Html;
26
use Fisharebest\Webtrees\I18N;
27
use Fisharebest\Webtrees\Mail;
28
use Fisharebest\Webtrees\Site;
29
use Fisharebest\Webtrees\Tree;
30
use Fisharebest\Webtrees\User;
31
use Fisharebest\Webtrees\View;
32
33
/**
34
 * Class ReviewChangesModule
35
 */
36
class ReviewChangesModule extends AbstractModule implements ModuleBlockInterface {
37
	/** {@inheritdoc} */
38
	public function getTitle() {
39
		return /* I18N: Name of a module */ I18N::translate('Pending changes');
40
	}
41
42
	/** {@inheritdoc} */
43
	public function getDescription() {
44
		return /* I18N: Description of the “Pending changes” module */ I18N::translate('A list of changes that need to be reviewed by a moderator, and email notifications.');
45
	}
46
47
	/**
48
	 * Generate the HTML content of this block.
49
	 *
50
	 * @param int      $block_id
51
	 * @param bool     $template
52
	 * @param string[] $cfg
53
	 *
54
	 * @return string
55
	 */
56
	public function getBlock($block_id, $template = true, $cfg = []): string {
57
		global $ctype, $WT_TREE;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
58
59
		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
60
		$days     = $this->getBlockSetting($block_id, 'days', '1');
61
62
		foreach (['days', 'sendmail'] as $name) {
63
			if (array_key_exists($name, $cfg)) {
64
				$$name = $cfg[$name];
65
			}
66
		}
67
68
		$changes = Database::prepare(
69
			"SELECT 1" .
70
			" FROM `##change`" .
71
			" WHERE status='pending'" .
72
			" LIMIT 1"
73
		)->fetchOne();
74
75
		if ($changes === '1' && $sendmail === '1') {
76
			// There are pending changes - tell moderators/managers/administrators about them.
77
			if (WT_TIMESTAMP - (int) Site::getPreference('LAST_CHANGE_EMAIL') > (60 * 60 * 24 * $days)) {
78
				// Which users have pending changes?
79
				foreach (User::all() as $user) {
80
					if ($user->getPreference('contactmethod') !== 'none') {
81
						foreach (Tree::getAll() as $tree) {
82
							if ($tree->hasPendingEdit() && Auth::isManager($tree, $user)) {
83
								I18N::init($user->getPreference('language'));
84
85
								$sender = new User(
86
									(object) [
87
										'user_id'   => null,
88
										'user_name' => '',
89
										'real_name' => $WT_TREE->getTitle(),
90
										'email'     => $WT_TREE->getPreference('WEBTREES_EMAIL'),
91
									]
92
								);
93
94
								Mail::send(
95
									$sender,
96
									$user,
97
									$sender,
98
									I18N::translate('Pending changes'),
99
									View::make('emails/pending-changes-text', ['tree' => $tree, 'user' => $user]),
100
									View::make('emails/pending-changes-html', ['tree' => $tree, 'user' => $user])
101
								);
102
								I18N::init(WT_LOCALE);
103
							}
104
						}
105
					}
106
				}
107
				Site::setPreference('LAST_CHANGE_EMAIL', WT_TIMESTAMP);
108
			}
109
		}
110
		if (Auth::isEditor($WT_TREE) && $WT_TREE->hasPendingEdit()) {
111
			$content = '';
112
			if (Auth::isModerator($WT_TREE)) {
113
				$content .= '<a href="edit_changes.php">' . I18N::translate('There are pending changes for you to moderate.') . '</a><br>';
114
			}
115
			if ($sendmail === '1') {
116
				$content .= I18N::translate('Last email reminder was sent ') . FunctionsDate::formatTimestamp(Site::getPreference('LAST_CHANGE_EMAIL')) . '<br>';
117
				$content .= I18N::translate('Next email reminder will be sent after ') . FunctionsDate::formatTimestamp((int) Site::getPreference('LAST_CHANGE_EMAIL') + (60 * 60 * 24 * $days)) . '<br><br>';
118
			}
119
			$content .= '<ul>';
120
			$changes = Database::prepare(
121
				"SELECT xref" .
122
				" FROM  `##change`" .
123
				" WHERE status='pending'" .
124
				" AND   gedcom_id=?" .
125
				" GROUP BY xref"
126
			)->execute([$WT_TREE->getTreeId()])->fetchAll();
127
			foreach ($changes as $change) {
128
				$record = GedcomRecord::getInstance($change->xref, $WT_TREE);
129
				if ($record->canShow()) {
130
					$content .= '<li><a href="' . $record->getHtmlUrl() . '">' . $record->getFullName() . '</a></li>';
131
				}
132
			}
133
			$content .= '</ul>';
134
135 View Code Duplication
			if ($template) {
136
				if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
137
					$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
138
				} else {
139
					$config_url = '';
140
				}
141
142
				return View::make('blocks/template', [
143
					'block'      => str_replace('_', '-', $this->getName()),
144
					'id'         => $block_id,
145
					'config_url' => $config_url,
146
					'title'      => $this->getTitle(),
147
					'content'    => $content,
148
				]);
149
			} else {
150
				return $content;
151
			}
152
		}
153
154
		return '';
155
	}
156
157
	/** {@inheritdoc} */
158
	public function loadAjax(): bool {
159
		return false;
160
	}
161
162
	/** {@inheritdoc} */
163
	public function isUserBlock(): bool {
164
		return true;
165
	}
166
167
	/** {@inheritdoc} */
168
	public function isGedcomBlock(): bool {
169
		return true;
170
	}
171
172
	/**
173
	 * An HTML form to edit block settings
174
	 *
175
	 * @param int $block_id
176
	 *
177
	 * @return void
178
	 */
179
	public function configureBlock($block_id): void {
180 View Code Duplication
		if (Filter::postBool('save') && Filter::checkCsrf()) {
181
			$this->setBlockSetting($block_id, 'days', Filter::postInteger('num', 1, 180, 1));
182
			$this->setBlockSetting($block_id, 'sendmail', Filter::postBool('sendmail'));
0 ignored issues
show
Documentation introduced by
\Fisharebest\Webtrees\Fi...r::postBool('sendmail') is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183
		}
184
185
		$sendmail = $this->getBlockSetting($block_id, 'sendmail', '1');
186
		$days     = $this->getBlockSetting($block_id, 'days', '1');
187
188
	?>
189
	<p>
190
		<?= I18N::translate('This block will show editors a list of records with pending changes that need to be reviewed by a moderator. It also generates daily emails to moderators whenever pending changes exist.') ?>
191
	</p>
192
193
	<fieldset class="form-group">
194
		<div class="row">
195
			<legend class="col-form-legend col-sm-3">
196
				<?= /* I18N: Label for a configuration option */ I18N::translate('Send out reminder emails') ?>
197
			</legend>
198
			<div class="col-sm-9">
199
				<?= Bootstrap4::radioButtons('sendmail', FunctionsEdit::optionsNoYes(), $sendmail, true) ?>
200
			</div>
201
		</div>
202
	</fieldset>
203
204
	<div class="form-group row">
205
		<label class="col-sm-3 col-form-label" for="days">
206
			<?= I18N::translate('Reminder email frequency (days)') ?>
207
		</label>
208
		<div class="col-sm-9">
209
			<input class="form-control" type="text" name="days" id="days" value="<?= $days ?>">
210
		</div>
211
	</div>
212
	<?php
213
	}
214
}
215