1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-10 16:54:44 +02:00

Minor code updates in InputfieldSelector

This commit is contained in:
Ryan Cramer
2023-06-03 11:01:14 -04:00
parent 3b5bad2c58
commit 50db3684fb

View File

@@ -7,7 +7,7 @@
* Code by Ryan Cramer * Code by Ryan Cramer
* Sponsored by Avoine * Sponsored by Avoine
* *
* ProcessWire 3.x, Copyright 2019 by Ryan Cramer * ProcessWire 3.x, Copyright 2023 by Ryan Cramer
* https://processwire.com * https://processwire.com
* *
* @todo add support for "custom: OR-group" option (https://processwire.com/talk/topic/13116-or-selecters-for-different-fields/) * @todo add support for "custom: OR-group" option (https://processwire.com/talk/topic/13116-or-selecters-for-different-fields/)
@@ -166,7 +166,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'previewColumns', 'previewColumns',
'limitFields', 'limitFields',
'maxUsers', 'maxUsers',
); );
/** /**
* Default values of each setting, for outside configuration * Default values of each setting, for outside configuration
@@ -275,25 +275,32 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'inputClass' => '', 'inputClass' => '',
'checkboxClass' => '', 'checkboxClass' => '',
); );
$configSettings = $this->wire('config')->InputfieldSelector;
$config = $this->wire()->config;
$configSettings = $config->InputfieldSelector;
$configSettings = is_array($configSettings) ? array_merge($configDefaults, $configSettings) : $configDefaults; $configSettings = is_array($configSettings) ? array_merge($configDefaults, $configSettings) : $configDefaults;
foreach($configSettings as $key => $value) { foreach($configSettings as $key => $value) {
$this->setting($key, $value); $this->setting($key, $value);
} }
$input = $this->wire('input'); if(!self::debug && !$this->wire()->config->ajax) return;
$input = $this->wire()->input;
$name = $input->get('name'); $name = $input->get('name');
$action = $input->get($this->className()); $action = $input->get($this->className());
if(!$action || !$name) return; if(!$action || !$name) return;
if(!self::debug && !$this->wire('config')->ajax) return;
$this->attr('name', $this->wire('sanitizer')->fieldName($name)); // for session validity $sanitizer = $this->wire()->sanitizer;
$this->attr('name', $sanitizer->fieldName($name)); // for session validity
if(!$this->sessionGet('valid')) return; if(!$this->sessionGet('valid')) return;
if(!$this->wire('user')->isLoggedin()) return; if(!$this->wire()->user->isLoggedin()) return;
$this->set('initValue', $this->sessionGet('initValue')); $this->set('initValue', $this->sessionGet('initValue'));
$sanitizer = $this->wire('sanitizer');
foreach($this->sessionVarNames as $key) { foreach($this->sessionVarNames as $key) {
$this->set($key, $this->sessionGet($key)); $this->set($key, $this->sessionGet($key));
@@ -401,7 +408,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'status' => array('@=', '@!=', '<', '<=', '>', '>='), 'status' => array('@=', '@!=', '<', '<=', '>', '>='),
//'selector' => array('#=', '#!='), //'selector' => array('#=', '#!='),
'selector' => array('=', '!=', '<', '>', '<=', '>='), 'selector' => array('=', '!=', '<', '>', '<=', '>='),
); );
// chars that are trimmed off operators before being used // chars that are trimmed off operators before being used
// enables different contexts for the same operator // enables different contexts for the same operator
@@ -425,12 +432,12 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
// make users selectable if there are under $maxUsers of them // make users selectable if there are under $maxUsers of them
// otherwise utilize the user ID property // otherwise utilize the user ID property
$users = array(); $users = array();
$userTemplates = implode('|', $this->wire('config')->userTemplateIDs); $userTemplates = implode('|', $this->wire()->config->userTemplateIDs);
$numUsers = $this->wire('pages')->count("template=$userTemplates, include=all"); $numUsers = $this->wire()->pages->count("template=$userTemplates, include=all");
if($numUsers < $this->maxUsers) { if($numUsers < $this->maxUsers) {
$usersInput = 'select'; $usersInput = 'select';
$usersOperators = array('=', '!='); $usersOperators = array('=', '!=');
foreach($this->wire('users') as $user) { foreach($this->wire()->users as $user) {
$users[$user->id] = $user->name; $users[$user->id] = $user->name;
} }
} else { } else {
@@ -438,7 +445,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$usersOperators = array('=', '%=', '!='); $usersOperators = array('=', '%=', '!=');
} }
$titleField = $this->wire('fields')->get('title'); $titleField = $this->wire()->fields->get('title');
// system fields definitions // system fields definitions
$this->systemFields = array( $this->systemFields = array(
@@ -553,7 +560,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
foreach(Page::getStatuses() as $name => $status) { foreach(Page::getStatuses() as $name => $status) {
if($status > Page::statusTrash) continue; if($status > Page::statusTrash) continue;
if($status === Page::statusDraft && !$this->wire('modules')->isInstalled('ProDrafts')) continue; if($status === Page::statusDraft && !$this->wire()->modules->isInstalled('ProDrafts')) continue;
if(in_array($status, $ignoreStatuses)) continue; if(in_array($status, $ignoreStatuses)) continue;
if(isset($this->systemFields['status']['options'][$name])) continue; // use existing label if(isset($this->systemFields['status']['options'][$name])) continue; // use existing label
$this->systemFields['status']['options'][$name] = ucfirst($name); $this->systemFields['status']['options'][$name] = ucfirst($name);
@@ -605,7 +612,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
foreach($this->systemFields as $name => $f) { foreach($this->systemFields as $name => $f) {
$options[$name] = $f['label']; $options[$name] = $f['label'];
} }
foreach($this->wire('fields') as $f) { foreach($this->wire()->fields as $f) {
if(strpos($f->type, 'FieldtypeFieldset') === 0) continue; if(strpos($f->type, 'FieldtypeFieldset') === 0) continue;
$options[$f->name] = $f->name; $options[$f->name] = $f->name;
} }
@@ -622,13 +629,15 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
* *
*/ */
protected function sessionSet($key, $value) { protected function sessionSet($key, $value) {
$s = $this->wire('session')->get($this->className()); $session = $this->wire()->session;
if(!$session) return $this;
$s = $session->get($this->className());
if(!is_array($s)) $s = array(); if(!is_array($s)) $s = array();
if(count($s) > 100) $s = array_slice($s, -100); // prevent from growing too large if(count($s) > 100) $s = array_slice($s, -100); // prevent from growing too large
$id = 'id' . $this->wire('page')->id . "_" . $this->wire('sanitizer')->fieldName($this->attr('name')); $id = 'id' . $this->wire('page')->id . "_" . $this->wire()->sanitizer->fieldName($this->attr('name'));
if(!isset($s[$id])) $s[$id] = array(); if(!isset($s[$id])) $s[$id] = array();
$s[$id][$key] = $value; $s[$id][$key] = $value;
$this->wire('session')->set($this->className(), $s); $session->set($this->className(), $s);
return $this; return $this;
} }
@@ -640,9 +649,9 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
* *
*/ */
protected function sessionGet($key) { protected function sessionGet($key) {
$s = $this->wire('session')->get($this->className()); $s = $this->wire()->session->get($this->className());
if(!$s) return null; if(!$s) return null;
$id = 'id' . $this->wire('page')->id . "_" . $this->wire('sanitizer')->fieldName($this->attr('name')); $id = 'id' . $this->wire()->page->id . "_" . $this->wire()->sanitizer->fieldName($this->attr('name'));
if(empty($s[$id])) return null; if(empty($s[$id])) return null;
if(empty($s[$id][$key])) return null; if(empty($s[$id][$key])) return null;
return $s[$id][$key]; return $s[$id][$key];
@@ -658,6 +667,9 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
* *
*/ */
public function getSelectorInfo($field) { public function getSelectorInfo($field) {
$fields = $this->wire()->fields;
$languages = $this->wire()->languages;
if(is_string($field)) { if(is_string($field)) {
if(isset($this->systemFields[$field])) { if(isset($this->systemFields[$field])) {
return $this->systemFields[$field]; return $this->systemFields[$field];
@@ -665,9 +677,9 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if(isset($this->modifierFields[$field])) { if(isset($this->modifierFields[$field])) {
return $this->modifierFields[$field]; return $this->modifierFields[$field];
} }
$field = $this->wire('fields')->get($field); $field = $fields->get($field);
} }
if(!$field || !$field instanceof Field || !$field->type) return array(); if(!$field instanceof Field || !$field->type) return array();
$info = $field->type->getSelectorInfo($field); $info = $field->type->getSelectorInfo($field);
if($info['input'] == 'page') { if($info['input'] == 'page') {
$info['subfields'] = array_merge($info['subfields'], $this->systemPageFields); $info['subfields'] = array_merge($info['subfields'], $this->systemPageFields);
@@ -678,11 +690,11 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
foreach($info['subfields'] as $name => $subfield) { foreach($info['subfields'] as $name => $subfield) {
// consider multi-language // consider multi-language
if(strpos($name, 'data') === 0 && $this->wire('languages')) { if(strpos($name, 'data') === 0 && $languages) {
list($unused, $languageID) = explode('data', "x$name"); list($unused, $languageID) = explode('data', "x$name");
if($unused) {} if($unused) {}
if(ctype_digit($languageID)) { if(ctype_digit($languageID)) {
$language = $this->wire('languages')->get((int) $languageID); $language = $languages->get((int) $languageID);
if($language && $language->id) { if($language && $language->id) {
$subfield['label'] = $field->getLabel() . " (" . $language->get('title|name') . ")"; $subfield['label'] = $field->getLabel() . " (" . $language->get('title|name') . ")";
} }
@@ -693,10 +705,10 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$label = isset($this->systemFields[$name]['label']) ? $this->systemFields[$name]['label'] : $name; $label = isset($this->systemFields[$name]['label']) ? $this->systemFields[$name]['label'] : $name;
} else if(!empty($subfield['label'])) { } else if(!empty($subfield['label'])) {
$label = $subfield['label']; $label = $subfield['label'];
} else if(strpos($name, 'data') === 0 && ctype_digit(substr($name, 4)) && $this->wire('languages')) { } else if(strpos($name, 'data') === 0 && ctype_digit(substr($name, 4)) && $languages) {
$label = $this->wire('languages')->get((int) substr($name, 4))->get('title|name'); $label = $languages->get((int) substr($name, 4))->get('title|name');
} else { } else {
$f = $this->wire('fields')->get($name); $f = $fields->get($name);
$label = $f ? $f->getLabel() : $name; $label = $f ? $f->getLabel() : $name;
} }
$subfield['label'] = $label; $subfield['label'] = $label;
@@ -767,7 +779,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if(is_array($previewColumns) && count($previewColumns)) { if(is_array($previewColumns) && count($previewColumns)) {
$a = array(); $a = array();
$sanitizer = $this->wire()->sanitizer; $sanitizer = $this->wire()->sanitizer;
foreach($previewColumns as $key => $col) { foreach($previewColumns as $col) {
$col = empty($col) ? '' : $sanitizer->name($col); $col = empty($col) ? '' : $sanitizer->name($col);
if(strlen("$col")) $a[] = $col; if(strlen("$col")) $a[] = $col;
} }
@@ -814,7 +826,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'type' => '', 'type' => '',
'prepend' => '<option></option>', 'prepend' => '<option></option>',
'append' => '', 'append' => '',
); );
$settings = array_merge($defaults, $settings); $settings = array_merge($defaults, $settings);
if($this->exclude) { if($this->exclude) {
@@ -855,7 +867,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'group' => '', 'group' => '',
'modifier' => '', 'modifier' => '',
'adjust' => '', 'adjust' => '',
); );
$outLabels = array( $outLabels = array(
'system' => $this->_x('System fields', 'optgroup-label'), 'system' => $this->_x('System fields', 'optgroup-label'),
@@ -864,7 +876,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'group' => $this->_x('Fields matching same (1) item', 'optgroup-label'), 'group' => $this->_x('Fields matching same (1) item', 'optgroup-label'),
'modifier' => $this->_x('Modifiers', 'optgroup-label'), 'modifier' => $this->_x('Modifiers', 'optgroup-label'),
'adjust' => $this->_x('Adjustments', 'optgroup-label'), 'adjust' => $this->_x('Adjustments', 'optgroup-label'),
); );
$optgroups = array(); $optgroups = array();
if($settings['showSystem']) { if($settings['showSystem']) {
@@ -925,6 +937,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
} else { } else {
$customFields = $settings['customFields']; $customFields = $settings['customFields'];
} }
$templates = $this->wire()->templates;
// build custom fields optgroup // build custom fields optgroup
foreach($customFields as $field) { foreach($customFields as $field) {
@@ -936,7 +950,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if(count($settings['templates'])) { if(count($settings['templates'])) {
$allow = 0; $allow = 0;
foreach($settings['templates'] as $templateName) { foreach($settings['templates'] as $templateName) {
$template = $this->wire('templates')->get($templateName); $template = $templates->get($templateName);
if($template && $template->fieldgroup->hasField($field)) { if($template && $template->fieldgroup->hasField($field)) {
$_field = $template->fieldgroup->getFieldContext($field); $_field = $template->fieldgroup->getFieldContext($field);
if($_field) $field = $_field; if($_field) $field = $_field;
@@ -1005,7 +1019,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
static $fieldsToTemplates = array(); static $fieldsToTemplates = array();
if(isset($fieldsToTemplates[$field->name])) return $fieldsToTemplates[$field->name]; if(isset($fieldsToTemplates[$field->name])) return $fieldsToTemplates[$field->name];
$templateIDs = array(); $templateIDs = array();
foreach($this->wire('templates') as $template) { foreach($this->wire()->templates as $template) {
if($template->fieldgroup->hasField($field)) $templateIDs[] = $template->id; if($template->fieldgroup->hasField($field)) $templateIDs[] = $template->id;
} }
$idStr = implode('|', $templateIDs); $idStr = implode('|', $templateIDs);
@@ -1037,7 +1051,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'all' => array() 'all' => array()
); );
$label = $this->wire('sanitizer')->entities($field->getLabel()); $label = $this->wire()->sanitizer->entities($field->getLabel());
if($settings['showFieldLabels'] == 2) $label .= " [$field->name]"; if($settings['showFieldLabels'] == 2) $label .= " [$field->name]";
$text = $settings['showFieldLabels'] ? $label : $field->name; $text = $settings['showFieldLabels'] ? $label : $field->name;
@@ -1108,6 +1122,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
*/ */
protected function renderSelectOperator($name, $type = '', $selectedValue = '', $operators = array()) { protected function renderSelectOperator($name, $type = '', $selectedValue = '', $operators = array()) {
$sanitizer = $this->wire()->sanitizer;
$inputName = $this->attr('name') . "__operator[]"; $inputName = $this->attr('name') . "__operator[]";
if(!count($operators)) { if(!count($operators)) {
@@ -1143,7 +1159,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$operator = ltrim($operator, $this->operatorTrimChars); $operator = ltrim($operator, $this->operatorTrimChars);
//$label .= " ($operator)"; //$label .= " ($operator)";
$selected = $operator === $selectedValue ? ' selected' : ''; $selected = $operator === $selectedValue ? ' selected' : '';
$out .= "<option$selected value='" . $this->wire('sanitizer')->entities($operator) . "'>$label</option>"; $out .= "<option$selected value='" . $sanitizer->entities($operator) . "'>$label</option>";
} }
$out .= "</select>"; $out .= "</select>";
@@ -1173,13 +1189,11 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$this->message("orChecked: $orChecked"); $this->message("orChecked: $orChecked");
*/ */
/** @var Templates $templates */ $templates = $this->wire()->templates;
$templates = $this->wire('templates'); $sanitizer = $this->wire()->sanitizer;
$fields = $this->wire()->fields;
$pages = $this->wire()->pages;
/** @var Sanitizer $sanitizer */
$sanitizer = $this->wire('sanitizer');
if($selector) {}
$field = null; $field = null;
$inputName = $this->attr('name') . "__value[]"; $inputName = $this->attr('name') . "__value[]";
$options = array(); $options = array();
@@ -1189,7 +1203,6 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$subfield = ''; $subfield = '';
$placeholder = ''; $placeholder = '';
$_type = ''; // previous type, if changed $_type = ''; // previous type, if changed
$inputfield = null;
$lastTemplates = $this->initTemplate ? array($this->initTemplate->id) : $this->sessionGet('lastTemplates'); $lastTemplates = $this->initTemplate ? array($this->initTemplate->id) : $this->sessionGet('lastTemplates');
if(strpos($fieldName, '.') !== false) list($fieldName, $subfield) = explode('.', $fieldName, 2); if(strpos($fieldName, '.') !== false) list($fieldName, $subfield) = explode('.', $fieldName, 2);
@@ -1236,7 +1249,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
} else if($type == 'selector') { } else if($type == 'selector') {
// selector field: do nothing here but skip it, and respond to it further down // selector field: do nothing here but skip it, and respond to it further down
} else if(($fieldName == 'parent' && $subfield) || $field = $this->wire('fields')->get($fieldName)) { } else if(($fieldName == 'parent' && $subfield) || $field = $fields->get($fieldName)) {
/** @var Field $field */ /** @var Field $field */
// custom field or parent with subfield // custom field or parent with subfield
$selectorInfo = $this->getSelectorInfo($field); $selectorInfo = $this->getSelectorInfo($field);
@@ -1246,7 +1259,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if($fieldName == 'parent') { if($fieldName == 'parent') {
// if parent, there is a subfield, and $field becomes the subfield // if parent, there is a subfield, and $field becomes the subfield
$selectorInfo = $this->getSelectorInfo($subfield); $selectorInfo = $this->getSelectorInfo($subfield);
$field = $this->wire('fields')->get($subfield); $field = $fields->get($subfield);
} else if($subfield && isset($selectorInfo['subfields'][$subfield])) { } else if($subfield && isset($selectorInfo['subfields'][$subfield])) {
$selectorInfo = $selectorInfo['subfields'][$subfield]; $selectorInfo = $selectorInfo['subfields'][$subfield];
} }
@@ -1257,7 +1270,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if($subfield) { if($subfield) {
// if there is a subfield, focus on that instead // if there is a subfield, focus on that instead
$field = $this->wire('fields')->get($subfield); $field = $fields->get($subfield);
} }
if(strlen($selectedValue) && !ctype_digit("$selectedValue") && Selectors::stringHasOperator($selectedValue)) { if(strlen($selectedValue) && !ctype_digit("$selectedValue") && Selectors::stringHasOperator($selectedValue)) {
@@ -1276,7 +1289,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
} else { } else {
// page field, non-autocomplete, no options specified, and no existing selector string exists // page field, non-autocomplete, no options specified, and no existing selector string exists
$page = $this->wire('pages')->newNullPage(); $page = $pages->newNullPage();
$inputfield = $field->getInputfield($page, $field); $inputfield = $field->getInputfield($page, $field);
if($inputfield instanceof InputfieldPage) { if($inputfield instanceof InputfieldPage) {
@@ -1305,7 +1318,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
} else if($field->get('template_id')) { } else if($field->get('template_id')) {
$findSelector[] = "templates_id=" . (int) $field->get('template_id'); $findSelector[] = "templates_id=" . (int) $field->get('template_id');
} }
foreach($this->wire('pages')->find(implode(', ', $findSelector)) as $item) { foreach($pages->find(implode(', ', $findSelector)) as $item) {
$options[$item->id] = $inputfield->getPageLabel($item); // $item->get('title|name'); $options[$item->id] = $inputfield->getPageLabel($item); // $item->get('title|name');
} }
} }
@@ -1373,6 +1386,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$none = '0'; $none = '0';
$hasNone = false; $hasNone = false;
foreach($options as $value => $label) { foreach($options as $value => $label) {
/** @var string|int $value */
// if select is using "0" as a literal selectable value, don't consider it as "None" // if select is using "0" as a literal selectable value, don't consider it as "None"
if($value === 0 || $value === '0') { if($value === 0 || $value === '0') {
$none = '""'; $none = '""';
@@ -1401,13 +1415,12 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$placeholder = $this->_('Start typing...'); $placeholder = $this->_('Start typing...');
$selectedValueTitle = ''; $selectedValueTitle = '';
if($selectedValueEntities) { if($selectedValueEntities) {
$selectedValuePage = $this->wire('pages')->get((int) $selectedValueEntities); $selectedValuePage = $pages->get((int) $selectedValueEntities);
if($selectedValuePage->id) { if($selectedValuePage->id) {
$selectedValueTitle = $selectedValuePage->get('title|name'); $selectedValueTitle = $selectedValuePage->get('title|name');
if($_type == 'page' && $field) { if($_type == 'page' && $field) {
$inputfield = $field->getInputfield($this->wire('pages')->newNullPage(), $field); $inputfield = $field->getInputfield($pages->newNullPage(), $field);
if($inputfield instanceof InputfieldPage) { if($inputfield instanceof InputfieldPage) {
/** @var InputfieldPage $inputfield */
$selectedValueTitle = $sanitizer->entities1($inputfield->getPageLabel($selectedValuePage)); $selectedValueTitle = $sanitizer->entities1($inputfield->getPageLabel($selectedValuePage));
} }
} }
@@ -1452,11 +1465,12 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
* @param $name * @param $name
* @param $value * @param $value
* @param bool $useTime * @param bool $useTime
* @return mixed * @return string
* *
*/ */
protected function renderDateInput($name, $value, $useTime = false) { protected function renderDateInput($name, $value, $useTime = false) {
$inputfield = $this->wire('modules')->get('InputfieldDatetime'); /** @var InputfieldDatetime $inputfield */
$inputfield = $this->wire()->modules->get('InputfieldDatetime');
$inputfield->attr('name', $name); $inputfield->attr('name', $name);
$inputfield->datepicker = InputfieldDatetime::datepickerFocus; $inputfield->datepicker = InputfieldDatetime::datepickerFocus;
$inputfield->placeholder = $this->datePlaceholder; $inputfield->placeholder = $this->datePlaceholder;
@@ -1482,6 +1496,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
*/ */
protected function renderSelectSubfield($fieldName, $selectedValue = '', Selector $selector = null) { protected function renderSelectSubfield($fieldName, $selectedValue = '', Selector $selector = null) {
$sanitizer = $this->wire()->sanitizer;
if(strpos($fieldName, '.')) list($fieldName,) = explode('.', $fieldName, 2); if(strpos($fieldName, '.')) list($fieldName,) = explode('.', $fieldName, 2);
$valueLabel = $this->_('Value'); $valueLabel = $this->_('Value');
@@ -1513,7 +1529,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
} }
// check if this is a custom field // check if this is a custom field
$field = $this->wire('fields')->get($fieldName); $field = $this->wire()->fields->get($fieldName);
if(!$field) return "Unknown Field: $fieldName"; // shouldn't happen, but for debugging if(!$field) return "Unknown Field: $fieldName"; // shouldn't happen, but for debugging
// if we've reached this point we're dealing with a custom field // if we've reached this point we're dealing with a custom field
@@ -1539,19 +1555,22 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$valueHasSelectorString = strlen($selectorValue) > 0 && Selectors::stringHasSelector($selectorValue); $valueHasSelectorString = strlen($selectorValue) > 0 && Selectors::stringHasSelector($selectorValue);
$limitSubfields = array(); $limitSubfields = array();
if(is_array($this->limitFields)) foreach($this->limitFields as $limitField) { if(is_array($this->limitFields)) {
if(strpos($limitField, '.') === false) continue; foreach($this->limitFields as $limitField) {
if(strpos($limitField, $fieldName) !== 0) continue; if(strpos($limitField, '.') === false) continue;
list($limitField, $limitSubfield) = explode('.', $limitField); if(strpos($limitField, $fieldName) !== 0) continue;
if($limitField) {} // ignore list($limitField, $limitSubfield) = explode('.', $limitField);
if($limitSubfield) $limitSubfields[$limitSubfield] = $limitSubfield; if($limitField) {
} // ignore
if($limitSubfield) $limitSubfields[$limitSubfield] = $limitSubfield;
}
} }
// render all the subfield options // render all the subfield options
foreach($selectorInfo['subfields'] as $name => $info) { foreach($selectorInfo['subfields'] as $name => $info) {
if(count($limitSubfields) && !isset($limitSubfields[$name])) continue; if(count($limitSubfields) && !isset($limitSubfields[$name])) continue;
$label = $this->wire('sanitizer')->entities($info['label']); $label = $sanitizer->entities($info['label']);
// render primary subfield selection (unless selector info says not to) // render primary subfield selection (unless selector info says not to)
if(isset($info['input']) && $info['input'] != 'none') { if(isset($info['input']) && $info['input'] != 'none') {
$selected = $selectedValue == $name && (!$valueHasSelectorString || empty($info['subfields'])) ? ' selected' : ''; $selected = $selectedValue == $name && (!$valueHasSelectorString || empty($info['subfields'])) ? ' selected' : '';
@@ -1646,7 +1665,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if(!$checkQuantity) return $selector; if(!$checkQuantity) return $selector;
if($hasPageListSelect) return $selector; if($hasPageListSelect) return $selector;
$quantity = $this->wire('pages')->count($selector); $quantity = $this->wire()->pages->count($selector);
return $quantity > $threshold ? $selector : ''; return $quantity > $threshold ? $selector : '';
} }
@@ -1662,6 +1681,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
protected function renderAutocompleteJSON($fieldName, $q) { protected function renderAutocompleteJSON($fieldName, $q) {
header("Content-Type: application/json"); header("Content-Type: application/json");
$fields = $this->wire()->fields;
// format for our returned JSON // format for our returned JSON
$data = array( $data = array(
@@ -1669,7 +1690,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'status' => 0, // 0=error, 1=success 'status' => 0, // 0=error, 1=success
'selector' => '', 'selector' => '',
'items' => array() 'items' => array()
); );
$selector = ''; $selector = '';
$subfield = ''; $subfield = '';
@@ -1677,7 +1699,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
list($fieldName, $subfield) = explode('.', $fieldName, 2); list($fieldName, $subfield) = explode('.', $fieldName, 2);
} }
$field = $this->wire('fields')->get($fieldName); $field = $fields->get($fieldName);
$selectorInfo = $this->getSelectorInfo($field); $selectorInfo = $this->getSelectorInfo($field);
if($subfield && isset($selectorInfo['subfields'][$subfield])) { if($subfield && isset($selectorInfo['subfields'][$subfield])) {
$selectorInfo = $selectorInfo['subfields'][$subfield]; $selectorInfo = $selectorInfo['subfields'][$subfield];
@@ -1688,7 +1710,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if(!$selector && $subfield) { if(!$selector && $subfield) {
$fieldName = $subfield; $fieldName = $subfield;
$field = $this->wire('fields')->get($fieldName); $field = $fields->get($fieldName);
} }
if(!$field) { if(!$field) {
@@ -1703,6 +1725,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
return json_encode($data); return json_encode($data);
} }
/** @var Field|InputfieldPageAutocomplete $field */
$searchFields = $field->searchFields; // used by InputfieldPageAutocomplete $searchFields = $field->searchFields; // used by InputfieldPageAutocomplete
$labelFieldName = $field->labelFieldName; $labelFieldName = $field->labelFieldName;
$labelFieldFormat = $field->labelFieldFormat; $labelFieldFormat = $field->labelFieldFormat;
@@ -1713,9 +1736,9 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$labelFieldName = $selectorInfo['labelFieldName']; $labelFieldName = $selectorInfo['labelFieldName'];
} }
$labelField = $labelFieldName ? $this->wire('fields')->get($labelFieldName) : null; $labelField = $labelFieldName ? $fields->get($labelFieldName) : null;
$template_id = (int) (is_array($field->template_id) ? reset($field->template_id) : $field->template_id); $template_id = (int) (is_array($field->template_id) ? reset($field->template_id) : $field->template_id);
$template = $template_id ? $this->wire('templates')->get($template_id) : null; $template = $template_id ? $this->wire()->templates->get($template_id) : null;
if($labelFieldFormat) { if($labelFieldFormat) {
/** @var InputfieldPage $inputfield */ /** @var InputfieldPage $inputfield */
@@ -1739,10 +1762,10 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$labelFieldName = 'name'; $labelFieldName = 'name';
} }
$selector .= ", $searchFields%=" . $this->wire('sanitizer')->selectorValue($q); $selector .= ", $searchFields%=" . $this->wire()->sanitizer->selectorValue($q);
$selector .= ", limit=50, include=hidden"; $selector .= ", limit=50, include=hidden";
foreach($this->wire('pages')->find($selector) as $item) { foreach($this->wire()->pages->find($selector) as $item) {
$label = $inputfield ? $inputfield->getPageLabel($item) : $item->get("$labelFieldName|name"); $label = $inputfield ? $inputfield->getPageLabel($item) : $item->get("$labelFieldName|name");
$data['items'][] = array( $data['items'][] = array(
'value' => $item->id, 'value' => $item->id,
@@ -1768,19 +1791,20 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
public function renderRow($select, $subfield, $opval, $class = '') { public function renderRow($select, $subfield, $opval, $class = '') {
if($this->allowAddRemove) { if($this->allowAddRemove) {
$delete = "<a href='#' class='delete-row'><i class='fa fa-trash-o'></i></a>"; $icon = wireIconMarkup('trash-o');
$delete = "<a href='#' class='delete-row'>$icon</a>";
} else { } else {
$delete = ''; $delete = '';
} }
$out = "<li class='selector-row ui-helper-clearfix $class'> return "
$select <li class='selector-row ui-helper-clearfix $class'>
<span class='subfield'>$subfield</span> $select
<span class='opval'>$opval</span> &nbsp; <span class='subfield'>$subfield</span>
$delete <span class='opval'>$opval</span> &nbsp;
$delete
</li> </li>
"; ";
return $out;
} }
/** /**
@@ -1809,8 +1833,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
foreach(new Selectors($this->lastSelector) as $item) { foreach(new Selectors($this->lastSelector) as $item) {
if($item->field != 'template') continue; if($item->field != 'template') continue;
foreach($item->values as $templateValue) { foreach($item->values as $templateValue) {
$template = $this->wire('templates')->get($templateValue); $template = $this->wire()->templates->get($templateValue);
if($template && $template instanceof Template) $lastTemplates[] = $template->id; if($template instanceof Template) $lastTemplates[] = $template->id;
} }
} }
} else { } else {
@@ -1833,7 +1857,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
public function set($key, $value) { public function set($key, $value) {
if($key === 'initTemplate') { if($key === 'initTemplate') {
if($value && !$value instanceof Template) { if($value && !$value instanceof Template) {
$value = $this->wire('templates')->get($value); $value = $this->wire()->templates->get($value);
} }
if($value === null || $value instanceof Template) { if($value === null || $value instanceof Template) {
$this->initTemplate = $value; $this->initTemplate = $value;
@@ -1887,8 +1911,10 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
*/ */
public function ___render() { public function ___render() {
$this->ready(); $this->ready();
// tell jQuery UI we want it to load the modal component which makes a.modal open modal windows // tell jQuery UI we want it to load the modal component which makes a.modal open modal windows
$this->wire('modules')->get('JqueryUI')->use('modal'); $jQueryUI = $this->wire()->modules->getModule('JqueryUI'); /** @var JqueryUI $jQueryUI */
$jQueryUI->use('modal');
if(self::debug) $this->counter = true; if(self::debug) $this->counter = true;
@@ -1924,15 +1950,17 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
// determine if there are any initValue templates in play, so that we can pre-limit what fields are available // determine if there are any initValue templates in play, so that we can pre-limit what fields are available
$templates = array(); $templates = array();
$renderSelectOptions = array(); $renderSelectOptions = array();
if($this->initValue) foreach(new Selectors($this->initValue) as $selector) { if($this->initValue) {
if($selector->field == 'template') { foreach(new Selectors($this->initValue) as $selector) {
$templateValue = $selector->value; if($selector->field == 'template') {
if(!is_array($templateValue)) $templateValue = array($templateValue); $templateValue = $selector->value;
foreach($templateValue as $t) { if(!is_array($templateValue)) $templateValue = array($templateValue);
$template = $this->wire('templates')->get($t); foreach($templateValue as $t) {
if($template) { $template = $this->wire()->templates->get($t);
$templates[] = $template->name; if($template) {
if(count($templateValue) == 1) $this->initTemplate = $template; $templates[] = $template->name;
if(count($templateValue) == 1) $this->initTemplate = $template;
}
} }
} }
} }
@@ -2016,7 +2044,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'value' => $this->attr('value'), 'value' => $this->attr('value'),
'class' => 'selector-value', 'class' => 'selector-value',
'data-template-ids' => implode(',', $this->getTemplatesFromInitValue($this->initValue)), 'data-template-ids' => implode(',', $this->getTemplatesFromInitValue($this->initValue)),
); );
if($this->allowBlankValues) $attr['class'] .= ' allow-blank'; if($this->allowBlankValues) $attr['class'] .= ' allow-blank';
$attrStr = $this->getAttributesString($attr); $attrStr = $this->getAttributesString($attr);
@@ -2026,23 +2054,22 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
// starting output // starting output
$out = " $out = "
<ul class='selector-list'> <ul class='selector-list'>
$rows $rows
</ul> </ul>
<div class='ui-helper-clearfix'> <div class='ui-helper-clearfix'>
"; ";
if($this->allowAddRemove) $out .= " if($this->allowAddRemove) $out .= "
<a class='selector-add' href='#'><i class='fa fa-$this->addIcon'></i> $this->addLabel</a> <a class='selector-add' href='#'><i class='fa fa-$this->addIcon'></i> $this->addLabel</a>
"; ";
$out .= " $out .= "
<span class='selector-counter$counterClass detail'></span> <span class='selector-counter$counterClass detail'></span>
<p class='selector-preview$previewClass' data-init-value='$initValue'>$attr[value]</p> <p class='selector-preview$previewClass' data-init-value='$initValue'>$attr[value]</p>
<input $attrStr /> <input $attrStr />
<p class='detail or-notes'>$notes</p> <p class='detail or-notes'>$notes</p>
</div> </div>
";
";
return $this->prep($out); return $this->prep($out);
} }
@@ -2060,9 +2087,9 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
$sanitizer = $this->wire()->sanitizer; $sanitizer = $this->wire()->sanitizer;
$users = $this->wire()->users; $users = $this->wire()->users;
$initSelectors = $this->wire(new Selectors()); $initSelectors = $this->wire(new Selectors()); /** @var Selectors $initSelectors */
$userSelectors = $this->wire(new Selectors()); $userSelectors = $this->wire(new Selectors()); /** @var Selectors $userSelectors */
if(!$parseVars) { if(!$parseVars) {
$initSelectors->setParseVars(false); $initSelectors->setParseVars(false);
@@ -2082,7 +2109,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if(in_array($s->field, array('modified_users_id', 'created_users_id')) && !ctype_digit("$s->value")) { if(in_array($s->field, array('modified_users_id', 'created_users_id')) && !ctype_digit("$s->value")) {
$ids = array(); $ids = array();
foreach($s->values as $key => $name) { foreach($s->values as $name) {
$property = ctype_digit("$name") ? 'id' : 'name'; $property = ctype_digit("$name") ? 'id' : 'name';
$operator = $s->operator === '!=' ? '=' : $s->operator; $operator = $s->operator === '!=' ? '=' : $s->operator;
if($property === 'name') { if($property === 'name') {
@@ -2103,11 +2130,7 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
} }
} }
$selector = (string) $initSelectors . ", "; return trim("$initSelectors, $userSelectors", ", ");
$selector .= (string) $userSelectors;
$selector = trim($selector, ", ");
return $selector;
} }
@@ -2120,18 +2143,18 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
*/ */
protected function getTemplatesFromInitValue($initValue) { protected function getTemplatesFromInitValue($initValue) {
// determine if a template is enforced and populate allowedTemplates // determine if a template is enforced and populate allowedTemplates
$templates = array(); $templates = $this->wire()->templates;
$templateIds = array();
if(!$initValue || strpos($initValue, 'template=') === false) return array(); if(!$initValue || strpos($initValue, 'template=') === false) return array();
foreach(new Selectors($initValue) as $selector) { foreach(new Selectors($initValue) as $selector) {
if($selector->field == 'template') { if($selector->field != 'template') continue;
$value = is_array($selector->value) ? $selector->value : array($selector->value); $value = is_array($selector->value) ? $selector->value : array($selector->value);
foreach($value as $name) { foreach($value as $name) {
$t = $this->wire('templates')->get($name); $t = $templates->get($name);
if($t) $templates[] = $t->id; if($t) $templateIds[] = $t->id;
}
} }
} }
return $templates; return $templateIds;
} }
/** /**
@@ -2158,7 +2181,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
public function getModuleConfigInputfields(array $data) { public function getModuleConfigInputfields(array $data) {
if($data) {} // ignore if($data) {} // ignore
$form = $this->wire(new InputfieldWrapper()); $form = $this->wire(new InputfieldWrapper());
$f = $this->wire('modules')->get('InputfieldSelector'); /** @var InputfieldSelector $f */
$f = $this->wire()->modules->get('InputfieldSelector');
$f->name = 'test'; $f->name = 'test';
$f->label = $this->_('Selector Sandbox'); $f->label = $this->_('Selector Sandbox');
$f->description = $this->_('This is here just in case you want to test out the functionality of this Inputfield.'); $f->description = $this->_('This is here just in case you want to test out the functionality of this Inputfield.');
@@ -2176,8 +2200,9 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
*/ */
public function getParentPages(Template $template) { public function getParentPages(Template $template) {
$templates = $this->wire('templates'); $templates = $this->wire()->templates;
$parentPages = $templates->getParentPages($template, true, Page::statusUnpublished); $parentPages = $templates->getParentPages($template, true, Page::statusUnpublished);
if($parentPages->count()) return $parentPages; if($parentPages->count()) return $parentPages;
$parentIDs = array(); $parentIDs = array();
@@ -2186,7 +2211,8 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
'WHERE pages.templates_id=:templates_id ' . 'WHERE pages.templates_id=:templates_id ' .
'AND status<' . Page::statusTrash . ' ' . 'AND status<' . Page::statusTrash . ' ' .
'GROUP BY parent_id LIMIT 500'; 'GROUP BY parent_id LIMIT 500';
$query = $this->wire('database')->prepare($sql);
$query = $this->wire()->database->prepare($sql);
$query->bindValue('templates_id', $template->id, \PDO::PARAM_INT); $query->bindValue('templates_id', $template->id, \PDO::PARAM_INT);
$query->execute(); $query->execute();
@@ -2196,10 +2222,12 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
} }
$query->closeCursor(); $query->closeCursor();
$parentPages = count($parentIDs) ? $this->wire('pages')->getById($parentIDs) : new PageArray(); $parentPages = count($parentIDs) ? $this->wire()->pages->getById($parentIDs) : new PageArray();
foreach($parentPages as $parentPage) { foreach($parentPages as $parentPage) {
if(!$parentPage->viewable(false)) $parentPages->remove($parentPage); if(!$parentPage->viewable(false)) $parentPages->remove($parentPage);
} }
return $parentPages; return $parentPages;
} }