VatNumberCheckHelper::_addJs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 1
eloc 14
c 4
b 1
f 0
nc 1
nop 0
dl 0
loc 30
rs 9.7998
1
<?php
2
App::uses('AppHelper', 'View/Helper');
3
App::uses('HtmlHelper', 'View/Helper');
4
App::uses('FormHelper', 'View/Helper');
5
6
/**
7
 * VatNumberCheck Helper
8
 *
9
 * @property HtmlHelper $Html
10
 * @property FormHelper $Form
11
 */
12
class VatNumberCheckHelper extends AppHelper {
0 ignored issues
show
Bug introduced by
The type AppHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * An array of names of helpers to load.
16
 *
17
 * @var array
18
 */
19
	public $helpers = ['Html', 'Form'];
20
21
/**
22
 * The number of times this helper is called
23
 *
24
 * @var int
25
 */
26
	protected $_helperCount = 0;
27
28
/**
29
 * The css class name to trigger `check` logic.
30
 *
31
 * @var string
32
 */
33
	protected $_inputClass = 'vat-number-check';
34
35
/**
36
 * Generates a vat number check form field.
37
 *
38
 *  See `FormHelper::input`.
39
 *
40
 * @param string $fieldName This should be "Modelname.fieldname"
41
 * @param array $options Each type of input takes different options.
42
 * @return string Html output for a form field
43
 */
44
	public function input($fieldName, $options = []) {
45
		$this->_helperCount += 1;
46
		if ($this->_helperCount === 1) {
47
			$this->_addJs();
48
		}
49
50
		$options = array_merge($options, ['type' => 'text']);
51
52
		$class = $this->_inputClass;
53
		if (empty($options['class'])) {
54
			$options['class'] = $class;
55
		} else {
56
			$options['class'] = sprintf('%s %s', $options['class'], $class);
57
		}
58
59
		return $this->Form->input($fieldName, $options);
60
	}
61
62
/**
63
 * Adds the needed javascript to the DOM (once).
64
 *
65
 * @return void
66
 */
67
	protected function _addJs() {
68
		$checkUrl = $this->Html->url([
69
			'plugin' => 'vat_number_check', 'controller' => 'vat_number_checks', 'action' => 'check', 'ext' => 'json'
70
		]);
71
		$checkImages = [
72
			'ok' => $this->Html->url('/vat_number_check/img/ok.png'),
73
			'failure' => $this->Html->url('/vat_number_check/img/failure.png'),
74
			'serviceUnavailable' => $this->Html->url('/vat_number_check/img/service-unavailable.png'),
75
		];
76
77
		$script = "
78
			/* jshint jquery:true */
79
80
			jQuery.noConflict();
81
			(function($) {
82
				$(function () {
83
					var options = {
84
						elementSelector: '" . sprintf('input.%s', $this->_inputClass) . "',
85
						checkUrl: '" . $checkUrl . "',
86
						checkImages: " . json_encode($checkImages) . ",
87
					};
88
					var vatNumberCheck = new VatNumberCheck(options);
89
				});
90
			})(jQuery);
91
		";
92
93
		$this->Html->script([
94
			'VatNumberCheck.jquery.min', 'VatNumberCheck.klass.min', 'VatNumberCheck.vat_number_check'
95
		], ['inline' => false, 'once' => true]);
96
		$this->Html->scriptBlock($script, ['inline' => false]);
97
	}
98
99
}
100