Completed
Push — feature/code-analysis ( 5519b1...bdc52b )
by Jonathan
02:58
created

CertificatesModule::validatePrerequisites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * webtrees-lib: MyArtJaub library for webtrees
4
 *
5
 * @package MyArtJaub\Webtrees
6
 * @subpackage Certificates
7
 * @author Jonathan Jaubart <[email protected]>
8
 * @copyright Copyright (c) 2009-2016, Jonathan Jaubart
9
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
10
 */
11
namespace MyArtJaub\Webtrees\Module;
12
13
use Fisharebest\Webtrees\Auth;
14
use Fisharebest\Webtrees\Functions\FunctionsEdit;
15
use Fisharebest\Webtrees\I18N;
16
use Fisharebest\Webtrees\Menu;
17
use Fisharebest\Webtrees\Module\AbstractModule;
18
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
19
use Fisharebest\Webtrees\Tree;
20
use MyArtJaub\Webtrees\Functions\Functions;
21
use MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface;
22
use MyArtJaub\Webtrees\Hook\HookInterfaces\FactSourceTextExtenderInterface;
23
use MyArtJaub\Webtrees\Hook\HookSubscriberInterface;
24
use MyArtJaub\Webtrees\Module\Certificates\Model\Certificate;
25
use MyArtJaub\Webtrees\Module\Certificates\Model\CertificateFileProvider;
26
use MyArtJaub\Webtrees\Module\Certificates\Model\CertificateProviderInterface;
27
use Rhumsaa\Uuid\Uuid;
28
29
/**
30
 * Certificates Module.
31
 */
32
class CertificatesModule 
33
    extends AbstractModule 
34
    implements HookSubscriberInterface, ModuleConfigInterface, ModuleMenuItemInterface, FactSourceTextExtenderInterface, CustomSimpleTagManagerInterface, DependentInterface
35
{
36
    /** @var string For custom modules - link for support, upgrades, etc. */
37
    const CUSTOM_WEBSITE = 'https://github.com/jon48/webtrees-lib';
38
        
39
    /**
40
     * Provider for Certificates
41
     * @var CertificateProviderInterface $provider
42
     */
43
    protected $provider;
44
    
45
    /**
46
     * {@inhericDoc}
47
     */
48
    public function getTitle() {
49
        return /* I18N: Name of the “Certificates” module */ I18N::translate('Certificates');
50
    }
51
    
52
    /**
53
     * {@inhericDoc}
54
     */
55
    public function getDescription() {
56
        return /* I18N: Description of the “Certificates” module */ I18N::translate('Display and edition of certificates linked to sources.');
57
    }
58
    
59
    /**
60
     * {@inhericDoc}
61
     */
62
    public function modAction($mod_action) {
63
        \MyArtJaub\Webtrees\Mvc\Dispatcher::getInstance()->handle($this, $mod_action);
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     * @see \MyArtJaub\Webtrees\Module\DependentInterface::validatePrerequisites()
69
     */
70
    public function validatePrerequisites() {
71
        return Functions::isEncryptionCompatible();    
72
    }
73
    
74
    /**
75
     * {@inhericDoc}
76
     * @see \Fisharebest\Webtrees\Module\ModuleConfigInterface::getConfigLink()
77
     */
78
    public function getConfigLink() {
79
        return 'module.php?mod=' . $this->getName() . '&amp;mod_action=AdminConfig';
80
    }
81
    
82
    /**
83
     * {@inhericDoc}
84
     * @see \MyArtJaub\Webtrees\Hook\HookSubscriberInterface::getSubscribedHooks()
85
     */
86
    public function getSubscribedHooks() {
87
        return array(
88
            'hFactSourcePrepend' => 50,
89
            'hGetExpectedTags' => 50,
90
            'hHtmlSimpleTagDisplay#_ACT' => 50,
91
            'hHtmlSimpleTagEditor#_ACT'	=> 50,
92
            'hAddSimpleTag#SOUR'	=> 50,
93
            'hHasHelpTextTag#_ACT'	=> 50,
94
            'hGetHelpTextTag#_ACT'	=> 50
95
        );
96
    }
97
    
98
    /**
99
     * {@inhericDoc}
100
     * @see \MyArtJaub\Webtrees\Module\ModuleMenuItemInterface::getMenu()
101
     */
102
    public function getMenu(Tree $tree, $reference = null) {
103
        $tree_url = $tree ? $tree->getNameUrl() : '';
104
        return new Menu($this->getTitle(), 'module.php?mod=' . $this->getName() . '&mod_action=Certificate@listAll&ged=' . $tree_url, 'menu-maj-list-certificate', array('rel' => 'nofollow'));
105
    }
106
    
107
    /**
108
     * {@inhericDoc}
109
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\FactSourceTextExtenderInterface::hFactSourcePrepend()
110
     */
111
    public function hFactSourcePrepend($srec) {
112
        global $WT_TREE;
113
        
114
        $html='';
115
        $sid=null;
116
        
117
        if($this->getSetting('MAJ_SHOW_CERT', Auth::PRIV_HIDE) >= Auth::accessLevel($WT_TREE)){
118
            if (!$srec || strlen($srec) == 0) return $html;
119
            	
120
            $certificate = null;
121
            $subrecords = explode("\n", $srec);
122
            $levelSOUR = substr($subrecords[0], 0, 1);
123
            $match = null;
124
            if (preg_match('~^'.$levelSOUR.' SOUR @('.WT_REGEX_XREF.')@$~', $subrecords[0], $match)) {
125
                $sid=$match[1];
126
            };
127
            $nb_subrecords = count($subrecords);
128
            for ($i=0; $i < $nb_subrecords; $i++) {
129
                $subrecords[$i] = trim($subrecords[$i]);
130
                $tag = substr($subrecords[$i], 2, 4);
131
                $text = substr($subrecords[$i], 7);
132
                if($tag == '_ACT') $certificate= new Certificate($text, $WT_TREE, $this->getProvider());
133
            }
134
            	
135
            if($certificate && $certificate->canShow())
136
                $html = $this->getDisplay_ACT($certificate, $sid);
137
                	
138
        }
139
        return $html;
140
    }
141
   
142
    /**
143
     * {@inhericDoc}
144
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\FactSourceTextExtenderInterface::hFactSourceAppend()
145
     */
146
    public function hFactSourceAppend($srec) { }
147
    
148
    /**
149
     * {@inhericDoc}
150
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hGetExpectedTags()
151
     */
152
    public function hGetExpectedTags() {
153
        return array('SOUR' => '_ACT');
154
    }
155
    
156
    /**
157
     * {@inhericDoc}
158
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hHtmlSimpleTagDisplay()
159
     */
160
    public function hHtmlSimpleTagDisplay($tag, $value, $context = null, $contextid = null) {
161
        $html = '';
162
        switch($tag){
163
            case '_ACT':
164
                if($context == 'SOUR') $html = $this->getDisplay_ACT($value, $contextid);
0 ignored issues
show
Documentation introduced by
$value is of type string, but the function expects a object<MyArtJaub\Webtree...ates\Model\Certificate>.

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...
165
                break;
166
        }
167
        return $html;
168
    }
169
    
170
    /**
171
     * {@inhericDoc}
172
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hHtmlSimpleTagEditor()
173
     */
174
    public function hHtmlSimpleTagEditor($tag, $value = null, $element_id = '', $element_name = '', $context = null, $contextid = null) {
175
        global $controller, $WT_TREE;
176
        
177
        $html = '';
178
		
179
		switch($tag){
180
			case '_ACT':
181
				$element_id = Uuid::uuid4();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $element_id. This often makes code more readable.
Loading history...
182
				$controller
183
					->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
184
					->addExternalJavascript(WT_STATIC_URL.WT_MODULES_DIR.$this->getName().'/js/autocomplete.js')
185
					->addExternalJavascript(WT_STATIC_URL.WT_MODULES_DIR.$this->getName().'/js/updatecertificatevalues.js');
186
				$certificate = null;
187
				if($value){
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
188
					$certificate = new Certificate($value, $WT_TREE, $this->getProvider());
189
				}
190
				$tabCities = $this->getProvider()->getCitiesList();
191
				$html .= '<select id="certifCity'.$element_id.'" class="_CITY">';
192
				foreach ($tabCities as $cities){
193
					$selectedCity='';
194
					if($certificate && $cities== $certificate->getCity()) $selectedCity='selected="true"';
195
					$html .= '<option value="'.$cities.'" '.$selectedCity.' />'.$cities.'</option>';
196
				}
197
				$html .= '</select>';
198
				$html .= '<input id="certifFile'.$element_id.'" autocomplete="off" class="_ACT" value="'.
199
					($certificate ? basename($certificate->getFilename()) : '').
200
					'" size="35" />';
201
				$html .= '<input type="hidden" id="'.$element_id.'" name = "'.$element_name.'" value="'.$value.'" size="35"/>';
202
		}
203
		
204
		return $html;
205
    }
206
    
207
    /**
208
     * {@inhericDoc}
209
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hAddSimpleTag()
210
     */
211
    public function hAddSimpleTag($context, $level) {
212
        switch($context){
213
            case 'SOUR':
214
                FunctionsEdit::addSimpleTag($level.' _ACT');
215
                break;
216
        }
217
    }
218
    
219
    /**
220
     * {@inhericDoc}
221
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hHasHelpTextTag()
222
     */
223
    public function hHasHelpTextTag($tag) {
224
        switch($tag){
225
			case '_ACT':
226
				return true;
227
		}
228
		return false;
229
    }
230
    
231
    /**
232
     * {@inhericDoc}
233
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hGetHelpTextTag()
234
     */
235
    public function hGetHelpTextTag($tag) {
236
        switch($tag){
237
            case '_ACT':
238
                return array(
239
                I18N::translate('Certificate'),
240
                '<p>'.I18N::translate('Path to a certificate linked to a source reference.').'</p>');
241
            default:
242
                return null;
243
        }
244
    }
245
246
    /**
247
     * Returns the default Certificate File Provider, as configured in the module
248
     *
249
     * @return \MyArtJaub\Webtrees\Module\Certificates\Model\CertificateProviderInterface
250
     */
251
    public function getProvider() {
252
        global $WT_TREE;
253
    
254
        if(!$this->provider) {
255
            $root_path = $this->getSetting('MAJ_CERT_ROOTDIR', 'certificates/');
256
            $this->provider = new CertificateFileProvider($root_path, $WT_TREE);
257
        }
258
        return $this->provider;
259
    }
260
    
261
    
262
    /**
263
     * Return the HTML code for custom simple tag _ACT
264
     *
265
     * @param Certificate $certificatePath Certificate (as per the GEDCOM)
0 ignored issues
show
Documentation introduced by
There is no parameter named $certificatePath. Did you maybe mean $certificate?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
266
     * @param string|null $sid Linked Source ID, if it exists
267
     */
268
    protected function getDisplay_ACT(Certificate $certificate, $sid = null){    
269
        $html = '';
270
        if($certificate){
271
            $certificate->setSource($sid);
272
            $html = $certificate->displayImage('icon');
273
        }
274
        return $html;
275
    }
276
277
278
}
279