FormInputsSetup::initialize()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 17
cp 0
rs 9.1288
c 0
b 0
f 0
cc 5
nc 8
nop 0
crap 30
1
<?php
2
3
namespace SemanticMaps;
4
5
use MapsMappingServices;
6
use SFFormPrinter;
7
8
/**
9
 * Initialization file for form input functionality in the Maps extension
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class FormInputsSetup {
15
16
	public static function run( SFFormPrinter $printer ) {
17
		$setup = new FormInputsSetup( $printer );
18
		$setup->initialize();
19
		return true;
20
	}
21
22
	private $printer;
23
24
	public function __construct( SFFormPrinter $printer ) {
25
		$this->printer = $printer;
26
		$this->initialize();
27
	}
28
29
	private function initialize() {
30
		$hasFormInputs = false;
31
32
		foreach ( MapsMappingServices::getServiceIdentifiers() as $serviceIdentifier ) {
33
			$service = MapsMappingServices::getServiceInstance( $serviceIdentifier );
34
			
35
			// Check if the service has a form input.
36
			$FIClass = $service->getFeature( 'fi' );
37
			
38
			// If the service has no FI, skip it and continue with the next one.
39
			if ( $FIClass === false ) continue;
40
			
41
			// At least one form input will be enabled when this point is reached.
42
			$hasFormInputs = true;
43
44
			// Add the result form input type for the service name.
45
			$this->initFormHook( $service->getName(), $service->getName() );
46
			
47
			// Loop through the service alliases, and add them as form input types.
48
			foreach ( $service->getAliases() as $alias ) {
49
				$this->initFormHook( $alias, $service->getName() );
50
			}
51
		}
52
		
53
		// Add the 'map' form input type if there are mapping services that have FI's loaded.
54
		if ( $hasFormInputs ) {
55
			$this->initFormHook( 'map' );
56
		}
57
		$this->initFormHook( 'googlemapsEditor' );
58
	}
59
	
60
	/**
61
	 * Adds a mapping service's form hook.
62
	 *
63
	 * @param string $inputName The name of the form input.
64
	 * @param string $mainName
65
	 */
66
	private function initFormHook( $inputName, $mainName = '' ) {
67
		// Add the form input hook for the service.
68
		$field_args = [];
69
		
70
		if ( $mainName !== '' ) {
71
			$field_args['service_name'] = $mainName;
72
		}
73
74
		if( $inputName == 'googlemapsEditor' ) {
75
			$this->printer->setInputTypeHook( $inputName, 'SemanticMaps\FormInputsSetup::smfSelectEditorFormInputHTML', $field_args );
76
		} else {
77
			$this->printer->setInputTypeHook( $inputName, 'SemanticMaps\FormInputsSetup::smfSelectFormInputHTML', $field_args );
78
		}
79
	}
80
81
	/**
82
	 * Calls the relevant form input class depending on the provided service.
83
	 *
84
	 * @param string $coordinates
85
	 * @param string $input_name
86
	 * @param boolean $is_mandatory
87
	 * @param boolean $is_disabled
88
	 * @param array $field_args
89
	 *
90
	 * @return array
91
	 */
92
	public static function smfSelectFormInputHTML( $coordinates, $input_name, $is_mandatory, $is_disabled, array $field_args ) {
93
		// Get the service name from the field_args, and set it to null if it doesn't exist.
94
		$serviceName = array_key_exists( 'service_name', $field_args ) ? $field_args['service_name'] : null;
95
96
		// Get the instance of the service class.
97
		$service = MapsMappingServices::getValidServiceInstance( $serviceName, 'fi' );
98
99
		// Get an instance of the class handling the current form input and service.
100
		$formInput = $service->getFeatureInstance( 'fi' );
101
102
		// Get and return the form input HTML from the hook corresponding with the provided service.
103
		return $formInput->getInputOutput( $coordinates, $input_name, $is_mandatory, $is_disabled, $field_args );
104
	}
105
106
	/**
107
	 * Calls the relevant form Editor input class depending on the provided service.
108
	 * NOTE: Currently only GoogleMaps is supported
109
	 *
110
	 * @since 2.0
111
	 *
112
	 * @param string $coordinates
113
	 * @param string $input_name
114
	 * @param boolean $is_mandatory
115
	 * @param boolean $is_disabled
116
	 * @param array $field_args
117
	 *
118
	 * @return array
119
	 */
120
	public static function smfSelectEditorFormInputHTML( $coordinates, $input_name, $is_mandatory, $is_disabled, array $field_args ) {
121
		// Get the service name from the field_args, and set it to null if it doesn't exist.
122
		$serviceName = array_key_exists( 'service_name', $field_args ) ? $field_args['service_name'] : null;
123
		// Get the instance of the service class.
124
		$service = MapsMappingServices::getValidServiceInstance( $serviceName, 'fi' );
125
126
		// Get an instance of the class handling the current form input and service.
127
		$formInput = $service->getFeatureInstance( 'fi' );
128
		// Get and return the form input HTML from the hook corresponding with the provided service.
129
		return $formInput->getEditorInputOutput( $coordinates, $input_name, $is_mandatory, $is_disabled, $field_args );
130
	}
131
132
133
}
134