1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Various minor updates

This commit is contained in:
Ryan Cramer
2019-09-03 11:48:48 -04:00
parent 51f554176d
commit 38d66af6f1
6 changed files with 88 additions and 28 deletions

View File

@@ -1275,15 +1275,17 @@ class PageFinder extends Wire {
$valueArray = $selector->values(true); $valueArray = $selector->values(true);
$fieldtype = $field->type; $fieldtype = $field->type;
$operator = $selector->operator; $operator = $selector->operator;
if($operator === '<>') $operator = '!=';
foreach($valueArray as $value) { foreach($valueArray as $value) {
// shortcut for blank value condition: this ensures that NULL/non-existence is considered blank // shortcut for blank value condition: this ensures that NULL/non-existence is considered blank
// without this section the query would still work, but a blank value must actually be present in the field // without this section the query would still work, but a blank value must actually be present in the field
$useEmpty = empty($value) || ($value && $operator[0] == '<') || ($value < 0 && $operator[0] == '>'); $isEmptyValue = $fieldtype->isEmptyValue($field, $value);
$useEmpty = $isEmptyValue || $operator[0] === '<' || ((int) $value < 0 && $operator[0] === '>');
if($subfield == 'data' && $useEmpty && $fieldtype) { // && !$fieldtype instanceof FieldtypeMulti) { if($subfield == 'data' && $useEmpty && $fieldtype) { // && !$fieldtype instanceof FieldtypeMulti) {
if(empty($value)) $numEmptyValues++; if($isEmptyValue) $numEmptyValues++;
if(in_array($operator, array('=', '!=', '<>', '<', '<=', '>', '>='))) { if(in_array($operator, array('=', '!=', '<', '<=', '>', '>='))) {
// we only accommodate this optimization for single-value selectors... // we only accommodate this optimization for single-value selectors...
if($this->whereEmptyValuePossible($field, $selector, $query, $value, $whereFields)) { if($this->whereEmptyValuePossible($field, $selector, $query, $value, $whereFields)) {
if(count($valueArray) > 1 && $operator == '=') $whereFieldsType = 'OR'; if(count($valueArray) > 1 && $operator == '=') $whereFieldsType = 'OR';

View File

@@ -58,6 +58,17 @@ class FieldtypeCheckbox extends Fieldtype {
return $schema; return $schema;
} }
/**
* @param DatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param mixed $value
*
* @return DatabaseQuery|DatabaseQuerySelect
* @throws WireException
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) { public function getMatchQuery($query, $table, $subfield, $operator, $value) {
if(!empty($value) && ($operator == '!=' || $operator == '<>')) { if(!empty($value) && ($operator == '!=' || $operator == '<>')) {
// allow for matching test_checkbox!=1 since non-checked rows don't appear in database // allow for matching test_checkbox!=1 since non-checked rows don't appear in database
@@ -85,5 +96,31 @@ class FieldtypeCheckbox extends Fieldtype {
return $info; return $info;
} }
/**
* Get an array of Fieldtypes that are compatible with this one
*
* This represents the list of Fieldtype modules that the user is allowed to change to from this one.
*
* @param Field $field
* @return Fieldtypes|null
*
*/
public function ___getCompatibleFieldtypes(Field $field) {
if($field) {}
$fieldtypes = $this->wire(new Fieldtypes());
$toggleClass = 'FieldtypeToggle';
$allowToggle = $this->wire('modules')->isInstalled($toggleClass);
foreach($this->wire('fieldtypes') as $fieldtype) {
if($fieldtype instanceof FieldtypeCheckbox) {
$fieldtypes->add($fieldtype);
} else if($allowToggle && wireInstanceOf($fieldtype, $toggleClass)) {
$fieldtypes->add($fieldtype);
}
}
return $fieldtypes;
}
} }

View File

@@ -596,12 +596,6 @@ class FieldtypeComments extends FieldtypeMulti {
foreach($itemsRemoved as $item) { foreach($itemsRemoved as $item) {
if(!$item->id) continue; if(!$item->id) continue;
$this->deleteComment($page, $field, $item, 'deleted from savePageField()'); $this->deleteComment($page, $field, $item, 'deleted from savePageField()');
/*
$query = $database->prepare("DELETE FROM `$table` WHERE id=:item_id AND pages_id=:pages_id");
$query->bindValue(":item_id", $item->id, \PDO::PARAM_INT);
$query->bindValue(":pages_id", $page->id, \PDO::PARAM_INT);
$query->execute();
*/
} }
} }
@@ -652,21 +646,27 @@ class FieldtypeComments extends FieldtypeMulti {
$sql .= "sort=:sort"; $sql .= "sort=:sort";
$binds['sort'] = ++$maxSort; $binds['sort'] = ++$maxSort;
} }
$query = $database->prepare($sql);
foreach($binds as $k => $v) $query->bindValue(":$k", $v);
try { try {
$query = $database->prepare($sql);
foreach($binds as $k => $v) {
$query->bindValue(":$k", $v);
}
$result = $query->execute(); $result = $query->execute();
if(!$value['id']) { if($result && !$value['id']) {
// populate newly added comment ID to Comment object // populate newly added comment ID to Comment object
$value['id'] = $database->lastInsertId(); $value['id'] = $database->lastInsertId();
foreach($allItems as $item) { foreach($allItems as $item) {
if(!$item->id && $item->code === $value['code']) $item->id = $value['id']; if(!$item->id && $item->code === $value['code']) $item->id = $value['id'];
} }
} }
$error = '';
} catch(\Exception $e) { } catch(\Exception $e) {
$result = false; $result = false;
$error = $e->getMessage();
}
if(!$result) {
$this->error("Error saving comment id $value[id]: $error", Notice::log);
} }
if(!$result) $this->error("Error saving item $value[id] in savePageField", Notice::log);
} }
return true; return true;

View File

@@ -28,6 +28,9 @@ class InputfieldRadios extends InputfieldSelect {
public function ___render() { public function ___render() {
/** @var Sanitizer $sanitizer */
$sanitizer = $this->wire('sanitizer');
$defaults = array( $defaults = array(
'wbr' => true, 'wbr' => true,
'noSelectLabels' => true, 'noSelectLabels' => true,
@@ -58,31 +61,41 @@ class InputfieldRadios extends InputfieldSelect {
} }
foreach($options as $key => $value) { foreach($options as $key => $value) {
$checked = ''; $checked = '';
$id = $this->id . "_" . $sanitizer->name($key);
$id = $this->id . "_" . $this->wire('sanitizer')->name($key);
$attrs = $this->getOptionAttributes($key); $attrs = $this->getOptionAttributes($key);
$inputClass = trim($this->attr('class'));
if(isset($attrs['input.class'])) $inputClass .= ' ' . $attrs['input.class'];
if($this->isOptionSelected($key)) $checked = " checked='checked'"; if($this->isOptionSelected($key)) $checked = " checked='checked'";
$disabled = empty($attrs['disabled']) ? "" : " disabled='disabled'"; $disabled = empty($attrs['disabled']) ? "" : " disabled='disabled'";
unset($attrs['selected'], $attrs['checked'], $attrs['disabled']);
unset($attrs['selected'], $attrs['checked'], $attrs['disabled'], $attrs['input.class']);
$textClass = $settings['noSelectLabels'] ? 'pw-no-select' : ''; $textClass = $settings['noSelectLabels'] ? 'pw-no-select' : '';
if($disabled) $textClass .= ' ui-state-disabled'; if($disabled) $textClass .= ' ui-state-disabled';
$attrs = $this->getOptionAttributesString($attrs); $attrs = $this->getOptionAttributesString($attrs);
if($attrs) $attrs = ' ' . $attrs; if($attrs) $attrs = ' ' . $attrs;
$label = $settings['wbr'] ? str_replace(' ', ' !wbr!', $value) : $value; $label = $settings['wbr'] ? str_replace(' ', ' !wbr!', $value) : $value;
$label = $this->entityEncode($label, Inputfield::textFormatBasic); $label = $this->entityEncode($label, Inputfield::textFormatBasic);
if($settings['wbr']) $label = str_replace('!wbr!', '<wbr>', $label); if($settings['wbr']) $label = str_replace('!wbr!', '<wbr>', $label);
$class = trim($this->wire('sanitizer')->entities($this->attr('class')));
$inputName = $sanitizer->entities($this->attr('name'));
$inputValue = $sanitizer->entities($key);
$inputClass = trim($sanitizer->entities($inputClass));
$out .= $out .=
"<li$liAttr><label$attrs>" . "<li$liAttr><label$attrs>" .
"<input$checked$disabled " . "<input$checked$disabled " .
"type='radio' " . "type='radio' " .
"name='{$this->name}' " . "name='$inputName' " .
"id='$id' " . "id='$id' " .
"class='$class' " . "class='$inputClass' " .
"value='" . htmlspecialchars($key, ENT_QUOTES) . "' />" . "value='$inputValue' />" .
"<span class='$textClass'>$label</span>" . "<span class='$textClass'>$label</span>" .
"</label></li>"; "</label></li>";
} }

View File

@@ -1351,15 +1351,21 @@ class InputfieldSelector extends Inputfield implements ConfigurableModule {
if($type != 'checkbox' && !isset($this->systemFields[$fieldName]) && !isset($this->modifierFields[$fieldName])) { if($type != 'checkbox' && !isset($this->systemFields[$fieldName]) && !isset($this->modifierFields[$fieldName])) {
// allow for a "None" option to find pages that have no selections for the field // allow for a "None" option to find pages that have no selections for the field
$none = '0'; $none = '0';
$hasNone = false;
foreach($options as $value => $label) { foreach($options as $value => $label) {
// 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 = '""';
} else if($value === '""' || $value === "''") {
// if it already literal quoted blank string then we should not show a none value at all
$hasNone = true;
break; break;
} }
} }
$selected = $selectedValue == $none || $selectedValue == '""' ? ' selected' : ''; if(!$hasNone) {
$out .= "<option$selected value='$none'>" . $this->_('None') . "</option>"; $selected = $selectedValue == $none || $selectedValue == '""' ? ' selected' : '';
$out .= "<option$selected value='$none'>" . $this->_('None') . "</option>";
}
} }
// render each option // render each option

View File

@@ -1380,6 +1380,9 @@ class ProcessField extends Process implements ConfigurableModule {
*/ */
protected function ___buildEditFormBasics() { protected function ___buildEditFormBasics() {
/** @var Fields $fields */
$fields = $this->wire('fields');
/** @var InputfieldWrapper $form */ /** @var InputfieldWrapper $form */
$form = $this->wire(new InputfieldWrapper()); $form = $this->wire(new InputfieldWrapper());
$form->attr('id', 'basics'); $form->attr('id', 'basics');
@@ -1406,10 +1409,9 @@ class ProcessField extends Process implements ConfigurableModule {
if(!$this->field->id) $field->description = $this->_("After selecting your field type and saving, you may be presented with additional configuration options specific to the field type you selected."); // Note that appears when adding new field if(!$this->field->id) $field->description = $this->_("After selecting your field type and saving, you may be presented with additional configuration options specific to the field type you selected."); // Note that appears when adding new field
if($this->field->type) $fieldtypes = $this->field->type->getCompatibleFieldtypes($this->field); $fieldtypes = $fields->getCompatibleFieldtypes($this->field);
else $fieldtypes = $this->fieldtypes;
if($fieldtypes && count($fieldtypes)) { if(count($fieldtypes)) {
$fieldtypeLabels = array(); $fieldtypeLabels = array();
foreach($fieldtypes as $fieldtype) { foreach($fieldtypes as $fieldtype) {
/** @var Fieldtype $fieldtype */ /** @var Fieldtype $fieldtype */
@@ -1418,7 +1420,7 @@ class ProcessField extends Process implements ConfigurableModule {
$fieldtypeLabels[$label] = $fieldtype; $fieldtypeLabels[$label] = $fieldtype;
} }
ksort($fieldtypeLabels); ksort($fieldtypeLabels);
$advanced = $this->config->advanced; $advanced = $this->wire('config')->advanced;
foreach($fieldtypeLabels as $label => $fieldtype) { foreach($fieldtypeLabels as $label => $fieldtype) {
if(!$advanced && $fieldtype->isAdvanced() && $this->field->name != 'title' if(!$advanced && $fieldtype->isAdvanced() && $this->field->name != 'title'
&& $field->value != $fieldtype->className()) continue; && $field->value != $fieldtype->className()) continue;
@@ -1430,7 +1432,7 @@ class ProcessField extends Process implements ConfigurableModule {
if(!$this->field->id) { if(!$this->field->id) {
$names = array(); $names = array();
foreach($this->wire('fields') as $f) { foreach($fields as $f) {
if(($f->flags & Field::flagSystem) && !$this->wire('config')->advanced) continue; if(($f->flags & Field::flagSystem) && !$this->wire('config')->advanced) continue;
if(strpos($f->name, '_END')) continue; if(strpos($f->name, '_END')) continue;
$names['_' . $f->name] = $f->name; $names['_' . $f->name] = $f->name;