Completed
Push — master ( 3603f5...72e2c3 )
by Jonathan
02:00
created

CertificatesModule::validatePrerequisites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
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\Globals;
22
use MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface;
23
use MyArtJaub\Webtrees\Hook\HookInterfaces\FactSourceTextExtenderInterface;
24
use MyArtJaub\Webtrees\Hook\HookSubscriberInterface;
25
use MyArtJaub\Webtrees\Module\Certificates\Model\Certificate;
26
use MyArtJaub\Webtrees\Module\Certificates\Model\CertificateFileProvider;
27
use MyArtJaub\Webtrees\Module\Certificates\Model\CertificateProviderInterface;
28
use Rhumsaa\Uuid\Uuid;
29
30
/**
31
 * Certificates Module.
32
 */
33
class CertificatesModule 
34
    extends AbstractModule 
35
    implements HookSubscriberInterface, ModuleConfigInterface, ModuleMenuItemInterface, FactSourceTextExtenderInterface, CustomSimpleTagManagerInterface
36
{
37
    /** @var string For custom modules - link for support, upgrades, etc. */
38
    const CUSTOM_WEBSITE = 'https://github.com/jon48/webtrees-lib';
39
        
40
    /**
41
     * Provider for Certificates
42
     * @var CertificateProviderInterface $provider
43
     */
44
    protected $provider;
45
    
46
    /**
47
     * {@inhericDoc}
48
     */
49
    public function getTitle() {
50
        return /* I18N: Name of the “Certificates” module */ I18N::translate('Certificates');
51
    }
52
    
53
    /**
54
     * {@inhericDoc}
55
     */
56
    public function getDescription() {
57
        return /* I18N: Description of the “Certificates” module */ I18N::translate('Display and edition of certificates linked to sources.');
58
    }
59
    
60
    /**
61
     * {@inhericDoc}
62
     */
63
    public function modAction($mod_action) {
64
        \MyArtJaub\Webtrees\Mvc\Dispatcher::getInstance()->handle($this, $mod_action);
65
    }
66
    
67
    /**
68
     * {@inhericDoc}
69
     * @see \Fisharebest\Webtrees\Module\ModuleConfigInterface::getConfigLink()
70
     */
71
    public function getConfigLink() {
72
        return 'module.php?mod=' . $this->getName() . '&amp;mod_action=AdminConfig';
73
    }
74
    
75
    /**
76
     * {@inhericDoc}
77
     * @see \MyArtJaub\Webtrees\Hook\HookSubscriberInterface::getSubscribedHooks()
78
     */
79
    public function getSubscribedHooks() {
80
        return array(
81
            'hFactSourcePrepend' => 50,
82
            'hGetExpectedTags' => 50,
83
            'hHtmlSimpleTagDisplay#_ACT' => 50,
84
            'hHtmlSimpleTagEditor#_ACT'	=> 50,
85
            'hAddSimpleTag#SOUR'	=> 50,
86
            'hHasHelpTextTag#_ACT'	=> 50,
87
            'hGetHelpTextTag#_ACT'	=> 50
88
        );
89
    }
90
    
91
    /**
92
     * {@inhericDoc}
93
     * @see \MyArtJaub\Webtrees\Module\ModuleMenuItemInterface::getMenu()
94
     */
95
    public function getMenu(Tree $tree, $reference = null) {
96
        $tree_url = $tree ? $tree->getNameUrl() : '';
97
        return new Menu($this->getTitle(), 'module.php?mod=' . $this->getName() . '&mod_action=Certificate@listAll&ged=' . $tree_url, 'menu-maj-list-certificate', array('rel' => 'nofollow'));
98
    }
99
    
100
    /**
101
     * {@inhericDoc}
102
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\FactSourceTextExtenderInterface::hFactSourcePrepend()
103
     */
104
    public function hFactSourcePrepend($srec) {
105
        $wt_tree = Globals::getTree();
106
        $html='';
107
        $sid=null;
108
        
109
        if($this->getSetting('MAJ_SHOW_CERT', Auth::PRIV_HIDE) >= Auth::accessLevel($wt_tree)){
110
            if (!$srec || strlen($srec) == 0) return $html;
111
            	
112
            $certificate = null;
113
            $subrecords = explode("\n", $srec);
114
            $levelSOUR = substr($subrecords[0], 0, 1);
115
            $match = null;
116
            if (preg_match('~^'.$levelSOUR.' SOUR @('.WT_REGEX_XREF.')@$~', $subrecords[0], $match)) {
117
                $sid=$match[1];
118
            };
119
            $nb_subrecords = count($subrecords);
120
            for ($i=0; $i < $nb_subrecords; $i++) {
121
                $subrecords[$i] = trim($subrecords[$i]);
122
                $tag = substr($subrecords[$i], 2, 4);
123
                $text = substr($subrecords[$i], 7);
124
                if($tag == '_ACT') $certificate= new Certificate($text, $wt_tree, $this->getProvider());
125
            }
126
            	
127
            if($certificate && $certificate->canShow())
128
                $html = $this->getDisplay_ACT($certificate, $sid);
129
                	
130
        }
131
        return $html;
132
    }
133
   
134
    /**
135
     * {@inhericDoc}
136
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\FactSourceTextExtenderInterface::hFactSourceAppend()
137
     */
138
    public function hFactSourceAppend($srec) { }
139
    
140
    /**
141
     * {@inhericDoc}
142
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hGetExpectedTags()
143
     */
144
    public function hGetExpectedTags() {
145
        return array('SOUR' => '_ACT');
146
    }
147
    
148
    /**
149
     * {@inhericDoc}
150
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hHtmlSimpleTagDisplay()
151
     */
152
    public function hHtmlSimpleTagDisplay($tag, $value, $context = null, $contextid = null) {
153
        $html = '';
154
        switch($tag){
155
            case '_ACT':
156
                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...
157
                break;
158
        }
159
        return $html;
160
    }
161
    
162
    /**
163
     * {@inhericDoc}
164
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hHtmlSimpleTagEditor()
165
     */
166
    public function hHtmlSimpleTagEditor($tag, $value = null, $element_id = '', $element_name = '', $context = null, $contextid = null) {        
167
        $html = '';
168
		
169
		switch($tag){
170
			case '_ACT':
171
				$element_id = Uuid::uuid4();
172
				Globals::getController()
173
					->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
174
					->addExternalJavascript(WT_STATIC_URL.WT_MODULES_DIR.$this->getName().'/js/autocomplete.js')
175
					->addExternalJavascript(WT_STATIC_URL.WT_MODULES_DIR.$this->getName().'/js/updatecertificatevalues.js');
176
				$certificate = null;
177
				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...
178
					$certificate = new Certificate($value, Globals::getTree(), $this->getProvider());
179
				}
180
				$tabCities = $this->getProvider()->getCitiesList();
181
				$html .= '<select id="certifCity'.$element_id.'" class="_CITY">';
182
				foreach ($tabCities as $cities){
183
					$selectedCity='';
184
					if($certificate && $cities== $certificate->getCity()) $selectedCity='selected="true"';
185
					$html .= '<option value="'.$cities.'" '.$selectedCity.' />'.$cities.'</option>';
186
				}
187
				$html .= '</select>';
188
				$html .= '<input id="certifFile'.$element_id.'" autocomplete="off" class="_ACT" value="'.
189
					($certificate ? basename($certificate->getFilename()) : '').
190
					'" size="35" />';
191
				$html .= '<input type="hidden" id="'.$element_id.'" name = "'.$element_name.'" value="'.$value.'" size="35"/>';
192
		}
193
		
194
		return $html;
195
    }
196
    
197
    /**
198
     * {@inhericDoc}
199
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hAddSimpleTag()
200
     */
201
    public function hAddSimpleTag($context, $level) {
202
        switch($context){
203
            case 'SOUR':
204
                FunctionsEdit::addSimpleTag($level.' _ACT');
205
                break;
206
        }
207
    }
208
    
209
    /**
210
     * {@inhericDoc}
211
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hHasHelpTextTag()
212
     */
213
    public function hHasHelpTextTag($tag) {
214
        switch($tag){
215
			case '_ACT':
216
				return true;
217
		}
218
		return false;
219
    }
220
    
221
    /**
222
     * {@inhericDoc}
223
     * @see \MyArtJaub\Webtrees\Hook\HookInterfaces\CustomSimpleTagManagerInterface::hGetHelpTextTag()
224
     */
225
    public function hGetHelpTextTag($tag) {
226
        switch($tag){
227
            case '_ACT':
228
                return array(
229
                I18N::translate('Certificate'),
230
                '<p>'.I18N::translate('Path to a certificate linked to a source reference.').'</p>');
231
            default:
232
                return null;
233
        }
234
    }
235
236
    /**
237
     * Returns the default Certificate File Provider, as configured in the module
238
     *
239
     * @return \MyArtJaub\Webtrees\Module\Certificates\Model\CertificateProviderInterface
240
     */
241
    public function getProvider() {
242
        if(!$this->provider) {
243
            $root_path = $this->getSetting('MAJ_CERT_ROOTDIR', 'certificates/');
244
            $this->provider = new CertificateFileProvider($root_path, Globals::getTree());
245
        }
246
        return $this->provider;
247
    }
248
    
249
    
250
    /**
251
     * Return the HTML code for custom simple tag _ACT
252
     *
253
     * @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...
254
     * @param string|null $sid Linked Source ID, if it exists
255
     */
256
    protected function getDisplay_ACT(Certificate $certificate, $sid = null){    
257
        $html = '';
258
        if($certificate){
259
            $certificate->setSource($sid);
260
            $html = $certificate->displayImage('icon');
261
        }
262
        return $html;
263
    }
264
265
266
}
267