This repository has been archived by the owner on Oct 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FieldtypeContinentsAndCountries.module
112 lines (98 loc) · 3.06 KB
/
FieldtypeContinentsAndCountries.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php namespace ProcessWire;
/**
* Fieldtype ContinentsAndCountries (Select)
*
* See README.md for usage instructions.
*
* ProcessWire 3.x
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* @author Tabea David <info@justonestep.de>
* @version 1.0.1
* @see https://github.com/justonestep/processwire-countries
* @see http://www.processwire.com
*/
/**
* Class FieldtypeContinentsAndCountries
*/
class FieldtypeContinentsAndCountries extends Fieldtype {
/**
* Retrieves module meta data
* Implementation of the Module interface
*
* @return array
* @see http://processwire.com/apigen/class-Module.html
*/
public static function getModuleInfo() {
return array(
'title' => 'Select Options Countries',
'summary' => 'Fieldtype to store a list of continents from which to choose from via select.',
'version' => 101,
'href' => 'https://github.com/justonestep/processwire-countries',
'requires' => 'ContinentsAndCountries'
);
}
/**
* Get Inputfield
*
* @param $page
* @param Field $fields
* return InputfieldSelect
*/
public function getInputfield(Page $page, Field $fields) {
$inputfield = $this->modules->get('InputfieldSelect');
// get countries and add them as options to the input field
if ($fields->select_options === 'all') {
$countries = $this->modules->get('ContinentsAndCountries')->getCountries();
} else {
$countries = $this->modules->get('ContinentsAndCountries')->findByContinent($fields->select_options);
}
foreach ($countries as $c) $inputfield->addOption($c->code, $c->name);
return $inputfield;
}
/**
* Get Database Schema
*
* @param Field $field
* @return array
*/
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['data'] = 'text NOT NULL';
$schema['keys']['data_exact'] = 'KEY `data_exact` (`data`(255))';
$schema['keys']['data'] = 'FULLTEXT KEY `data` (`data`)';
return $schema;
}
/**
* Sanitize Value
*
* @param Page $page
* @param Field $field
* @param string $value
* @return string
*/
public function sanitizeValue(Page $page, Field $field, $value) {
return $this->sanitizer->text($value);
}
/**
* Get Config Input Field
* For PW Admin, Tab Details: List Continents
*
* @param Field $fields
* @return InputfieldWrapper
*/
public function ___getConfigInputfields(Field $fields) {
$inputfields = parent::___getConfigInputfields($fields);
// get continents
$continents = $this->modules->get('ContinentsAndCountries')->getContinents();
// list of options
$select = $this->modules->get('InputfieldSelect');
$select->label = $this->_('Choose continent to get countries from');
$select->name = 'select_options';
$select->attr('value', count($fields->select_options) > 0 ? $fields->select_options : '');
$select->addOption('all', $this->_('– All –'));
foreach ($continents as $c) $select->addOption($c->code, $c->name);
$inputfields->add($select);
return $inputfields;
}
}