Completed
Push — master ( 0f93c5...bdc52b )
by Jonathan
04:53
created

CertificateController::index()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 59
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 59
rs 7.5346
cc 7
eloc 37
nc 10
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Certificates;
12
13
use Fisharebest\Webtrees\Auth;
14
use Fisharebest\Webtrees\Controller\PageController;
15
use Fisharebest\Webtrees\Filter;
16
use Fisharebest\Webtrees\I18N;
17
use Fisharebest\Webtrees\Module;
18
use Fisharebest\Webtrees\Module\AbstractModule;
19
use MyArtJaub\Webtrees\Constants;
20
use MyArtJaub\Webtrees\Controller\JsonController;
21
use MyArtJaub\Webtrees\Functions\Functions;
22
use MyArtJaub\Webtrees\ImageBuilder;
23
use MyArtJaub\Webtrees\Module\Certificates\Model\Certificate;
24
use MyArtJaub\Webtrees\Module\Certificates\Model\CertificateProviderInterface;
25
use MyArtJaub\Webtrees\Mvc\Controller\MvcController;
26
use MyArtJaub\Webtrees\Mvc\View\ViewBag;
27
use MyArtJaub\Webtrees\Mvc\View\ViewFactory;
28
use Rhumsaa\Uuid\Uuid;
29
30
/**
31
 * Controller for Certificate
32
 */
33
class CertificateController extends MvcController
34
{
35
    /**
36
     * Certificate Provider
37
     * @var CertificateProviderInterface $provider
38
     */
39
    protected $provider;
40
    
41
    /**
42
     * Constructor for Certificate controller
43
     * @param AbstractModule $module
44
     */
45
    public function __construct(AbstractModule $module) {
46
        parent::__construct($module);
47
        
48
        $this->provider = $this->module->getProvider();
49
    }
50
    
51
    
52
    /**
53
     * Pages
54
     */
55
        
56
    /**
57
     * Certificate@index
58
     */
59
    public function index() {
60
        global $WT_TREE;
61
        
62
        $controller = new PageController();
63
        $controller
64
        ->setPageTitle(I18N::translate('Certificate'))
65
        ->restrictAccess(
66
            $this->module->getSetting('MAJ_SHOW_CERT', Auth::PRIV_HIDE) >= Auth::accessLevel($WT_TREE)
67
        );
68
        
69
        $cid = Filter::get('cid');
70
        
71
        $certificate = null;
72
        if(!empty($cid) && strlen($cid) > 22){
73
            $certificate = Certificate::getInstance($cid, $WT_TREE, null, $this->provider);
74
        }
75
        
76
        $data = new ViewBag();
77
        $data->set('title', $controller->getPageTitle());
78
        
79
        $data->set('has_certif', false);
80
        if($certificate) {
81
            $controller->restrictAccess($certificate->canShow());
82
            $data->set('title', $certificate->getTitle());
83
            $data->set('has_certif', true);
84
            $data->set('certificate', $certificate);
85
            
86
            $data->set(
87
                'url_certif_city', 
88
                'module.php?mod=' . Constants::MODULE_MAJ_CERTIF_NAME . 
89
                    '&mod_action=Certificate@listAll' .
90
                    '&ged=' . $WT_TREE->getNameUrl() .
91
                    '&city=' . Functions::encryptToSafeBase64($certificate->getCity())
92
            );
93
            
94
            $controller->addInlineJavascript('
95
                jQuery("#certificate-tabs").tabs();
96
			    jQuery("#certificate-tabs").css("visibility", "visible");    
97
            ');
98
            
99
            $data->set('has_linked_indis', false);
100
            $data->set('has_linked_fams', false);
101
            
102
            $linked_indis = $certificate->linkedIndividuals();
103
            $linked_fams = $certificate->linkedFamilies();
104
                        
105
            if($linked_indis && count($linked_indis) > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $linked_indis of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
106
                $data->set('has_linked_indis', true);
107
                $data->set('linked_indis', $linked_indis);
108
            }
109
            
110
            if(!empty($linked_fams)) {
111
                $data->set('has_linked_fams', true);
112
                $data->set('linked_fams', $linked_fams);
113
            }
114
        }
115
        
116
        ViewFactory::make('Certificate', $this, $controller, $data)->render();
117
    }
118
    
119
    /**
120
     * Certificate@image
121
     */
122
    public function image() {      
123
        global $WT_TREE;
124
        
125
        $cid   = Filter::get('cid');
126
        $certificate = null;
127
        if(!empty($cid)) $certificate =  Certificate::getInstance($cid, $WT_TREE, null, $this->provider);
128
        
129
        $imageBuilder = new ImageBuilder($certificate);
130
        
131
        if (!empty(Filter::get('cb'))) {
132
            $imageBuilder->setExpireOffset($imageBuilder->getExpireOffset() * 7);
133
        }
134
        
135
        $imageBuilder
136
            ->setShowWatermark(Auth::accessLevel($WT_TREE) >= $this->module->getSetting('MAJ_SHOW_NO_WATERMARK', Auth::PRIV_HIDE))
137
            ->setFontMaxSize($this->module->getSetting('MAJ_WM_FONT_MAXSIZE', 18))
138
            ->setFontColor($this->module->getSetting('MAJ_WM_FONT_COLOR', '#4D6DF3'))
139
        ;
140
        
141
        $imageBuilder->render();
142
        
143
    }
144
    
145
    /**
146
     * Certificate@listAll
147
     */
148
    public function listAll() {
149
        global $WT_TREE;
150
        
151
        $controller = new PageController();
152
        $controller
153
            ->setPageTitle(I18N::translate('Certificates'))
154
            ->restrictAccess(
155
                $this->module->getSetting('MAJ_SHOW_CERT', Auth::PRIV_HIDE) >= Auth::accessLevel($WT_TREE)
156
            );
157
        
158
        $city = Filter::get('city');
159
        
160
        if(!empty($city) && strlen($city) > 22){
161
            $city = Functions::decryptFromSafeBase64($city);
162
            $controller->setPageTitle(I18N::translate('Certificates for %s', $city));
163
        }
164
        
165
        $data = new ViewBag();
166
        $data->set('title', $controller->getPageTitle());
167
        $data->set('url_module', $this->module->getName());
168
        $data->set('url_action', 'Certificate@listAll');
169
        $data->set('url_ged', $WT_TREE->getNameUrl());
170
        
171
        $data->set('cities', $this->provider->getCitiesList());
172
        $data->set('selected_city', $city);
173
        
174
        $data->set('has_list', false);        
175
        if(!empty($city)) {            
176
            $table_id = 'table-certiflist-' . Uuid::uuid4();
177
            
178
            $certif_list = $this->provider->getCertificatesList($city);            
179
            if(!empty($certif_list)) {                
180
                $data->set('has_list', true);
181
                $data->set('table_id', $table_id);
182
                $data->set('certificate_list', $certif_list);
183
                
184
                $controller
185
                    ->addExternalJavascript(WT_JQUERY_DATATABLES_JS_URL)
186
                    ->addInlineJavascript('
187
                        /* Initialise datatables */
188
        				jQuery.fn.dataTableExt.oSort["unicode-asc"  ]=function(a,b) {return a.replace(/<[^<]*>/, "").localeCompare(b.replace(/<[^<]*>/, ""))};
189
        				jQuery.fn.dataTableExt.oSort["unicode-desc" ]=function(a,b) {return b.replace(/<[^<]*>/, "").localeCompare(a.replace(/<[^<]*>/, ""))};
190
        				jQuery.fn.dataTableExt.oSort["num-html-asc" ]=function(a,b) {a=parseFloat(a.replace(/<[^<]*>/, "")); b=parseFloat(b.replace(/<[^<]*>/, "")); return (a<b) ? -1 : (a>b ? 1 : 0);};
191
        				jQuery.fn.dataTableExt.oSort["num-html-desc"]=function(a,b) {a=parseFloat(a.replace(/<[^<]*>/, "")); b=parseFloat(b.replace(/<[^<]*>/, "")); return (a>b) ? -1 : (a<b ? 1 : 0);};
192
                        
193
                        jQuery("#'.$table_id.'").dataTable( {
194
        					dom: \'<"H"<"filtersH_' . $table_id . '">T<"dt-clear">pf<"dt-clear">irl>t<"F"pl<"dt-clear"><"filtersF_' . $table_id . '">>\',
195
    					    '.I18N::datatablesI18N().',
196
    					    jQueryUI: true,
197
        					autoWidth: false,
198
        					processing: true,
199
        					columns: [
200
        		                    /* 0-Date */  			{ dataSort: 1, width: "15%", class: "center" },
201
        							/* 1-DateSort */		{ type: "unicode", visible : false },
202
        		                    /* 2-Type */ 			{ width: "5%", searchable: false, class: "center"},
203
        		                    /* 3-CertificateSort */ { type: "unicode", visible : false },
204
        		                    /* 4-Certificate */     { dataSort: 3, class: "left" }
205
        		                ],
206
        		            sorting: [[0,"asc"], [2,"asc"]],
207
        					displayLength: 20,
208
        					pagingType: "full_numbers"
209
        			   });
210
        				jQuery(".certificate-list").css("visibility", "visible");
211
        				jQuery(".loading-image").css("display", "none");
212
                    ');
213
            }
214
        }
215
        
216
        ViewFactory::make('CertificatesList', $this, $controller, $data)->render();
217
        
218
    }
219
    
220
    /**
221
     * Certificate@autocomplete
222
     */
223
    public function autocomplete() {
224
        global $WT_TREE;
225
        
226
        $controller = new JsonController();
227
        
228
        $city = Filter::get('city');
229
        $contains = Filter::get('term');        
230
231
        $controller
232
            ->restrictAccess(Auth::isEditor($WT_TREE) && !empty($city) && !empty($contains))
233
            ->pageHeader();
234
        
235
        $listCert = $this->provider->getCertificatesListBeginWith($city, $contains); 
0 ignored issues
show
Bug introduced by
The call to getCertificatesListBeginWith() misses a required argument $limit.

This check looks for function calls that miss required arguments.

Loading history...
236
        echo \Zend_Json::encode($listCert);
237
    }
238
}