-
Notifications
You must be signed in to change notification settings - Fork 4
/
InputfieldChosenSelectMultiple.module
141 lines (109 loc) · 4.04 KB
/
InputfieldChosenSelectMultiple.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* InputfieldChosenSelect module
*
* Inputfield for the ProcessWire FieldtypePage. Provides an alternative
* tagging interface.
*
* Copyright (C) 2015 Benjamin Milde
* Licensed under MIT License, see LICENSE.md
*
* http://www.kobrakai.de
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class InputfieldChosenSelectMultiple extends InputfieldSelectMultiple implements InputfieldHasArrayValue{
protected $chosenOptions = array();
protected $supportedAdminThemes = array(
"AdminThemeReno",
"AdminThemeDefault",
"AdminThemeUikit"
);
public static function getModuleInfo() {
return array(
'title' => __('Inputfield Chosen Select Multiple', __FILE__), // Module Title
'summary' => __('Inputfield for the ProcessWire FieldtypePage. Provides an alternative tagging interface.', __FILE__), // Module Summary
'version' => 122,
);
}
public function init() {
parent::init();
wire('modules')->get('JqueryCore'); // required by jQuery UI
wire('modules')->get('JqueryUI');
$this->setChosenOption('width', "100%");
$this->setChosenOption('search_contains', 'true');
$this->setChosenOption('no_results_text_addable', "No results match! Create");
}
public function setChosenOption($key, $value) {
$this->chosenOptions[$key] = $value;
}
/**
* Called before render()
*
* @param Inputfield $parent
* @param bool $renderValueMode
* @return bool
*
*/
public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
$class = $this->className();
$info = self::getModuleInfo();
$ver = $info['version'];
$this->config->scripts->add($this->config->urls->$class . "chosen/chosen.jquery.min.js?v=$ver");
$this->config->styles->add($this->config->urls->$class . "chosen/chosen.min.css?v=$ver");
// Special styles per admin theme
if(in_array($this->adminTheme, $this->supportedAdminThemes)){
$this->config->styles->add($this->config->urls->$class . $this->adminTheme . ".css?v=$ver");
}
if($this->placeholder) $this->setChosenOption('placeholder_text_multiple', $this->placeholder);
if ($this->noresults) $this->setChosenOption('no_results_text', $this->noresults);
$this->config->js($this->id, $this->chosenOptions);
return parent::renderReady($parent, $renderValueMode);
}
public function ___render() {
$selectedOptions = $this->attr('value');
foreach($selectedOptions as $id) {
if(!isset($this->options[$id])) continue;
$label = $this->options[$id];
unset($this->options[$id]);
$this->addOption($id, $label);
}
return parent::___render();
}
public function ___install() {
$data = $this->modules->getModuleConfigData("InputfieldPage");
array_push($data["inputfieldClasses"], $this->className());
$this->modules->saveModuleConfigData("InputfieldPage", $data);
}
public function ___uninstall() {
$fields = wire('fields')->find("type=FieldtypePage");
foreach($fields as $field){
if($field->inputfield !== $this->className()) $fields->remove($field);
}
$this->warning($this->className()." was used in following fields: ".$fields->implode(", ", "name"));
$data = $this->modules->getModuleConfigData("InputfieldPage");
if(in_array($this->className(), $data["inputfieldClasses"])){
$key = array_search($this->className(), $data["inputfieldClasses"]);
unset($data["inputfieldClasses"][$key]);
$this->modules->saveModuleConfigData("InputfieldPage", $data);
}
}
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'placeholder');
$f->attr('value', $this->placeholder);
$f->label = $this->_('Placeholder Text');
$f->description = $this->_('Placeholder-text to be displayed in empty field.');
$inputfields->add($f);
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'noresults');
$f->attr('value', $this->noresults);
$f->label = $this->_('No results');
$f->description = $this->_('Message to display if no result is found.');
$inputfields->add($f);
return $inputfields;
}
}