Skip to content

Commit

Permalink
fix #1574 (#1575)
Browse files Browse the repository at this point in the history
* split multiple values

* add a new field type 'json' to handle complex situation

* fix json input display
  • Loading branch information
joyqi committed May 16, 2023
1 parent c725fec commit 6f19a24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
8 changes: 5 additions & 3 deletions admin/custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class="typecho-post-option<?php if (empty($defaultFields) && empty($fields)): ?>
class="i-caret-right"></i> <?php _e('自定义字段'); ?></a></label>
<table class="typecho-list-table mono">
<colgroup>
<col width="25%"/>
<col width="10%"/>
<col width="20%"/>
<col width="15%"/>
<col width="55%"/>
<col width="10%"/>
</colgroup>
Expand All @@ -37,12 +37,14 @@ class="i-caret-right"></i> <?php _e('自定义字段'); ?></a></label>
value="int"<?php if ('int' == $field['type']): ?> selected<?php endif; ?>><?php _e('整数'); ?></option>
<option
value="float"<?php if ('float' == $field['type']): ?> selected<?php endif; ?>><?php _e('小数'); ?></option>
<option
value="json"<?php if ('json' == $field['type']): ?> selected<?php endif; ?>><?php _e('JSON 结构'); ?></option>
</select>
</td>
<td>
<label for="fieldvalue" class="sr-only"><?php _e('字段值'); ?></label>
<textarea name="fieldValues[]" id="fieldvalue" class="text-s w-100"
rows="2"><?php echo htmlspecialchars($field[$field['type'] . '_value']); ?></textarea>
rows="2"><?php echo htmlspecialchars($field[($field['type'] == 'json' ? 'str' : $field['type']) . '_value']); ?></textarea>
</td>
<td>
<button type="button" class="btn btn-xs"><?php _e('删除'); ?></button>
Expand Down
40 changes: 21 additions & 19 deletions var/Widget/Base/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,41 +345,42 @@ public function checkFieldName(string $name): bool
*
* @param string $name
* @param string $type
* @param string $value
* @param mixed $value
* @param integer $cid
* @return integer|bool
* @throws Exception
*/
public function setField(string $name, string $type, string $value, int $cid)
public function setField(string $name, string $type, $value, int $cid)
{
if (
empty($name) || !$this->checkFieldName($name)
|| !in_array($type, ['str', 'int', 'float'])
|| !in_array($type, ['str', 'int', 'float', 'json'])
) {
return false;
}

if ($type === 'json') {
$value = json_encode($value);
}

$exist = $this->db->fetchRow($this->db->select('cid')->from('table.fields')
->where('cid = ? AND name = ?', $cid, $name));

$rows = [
'type' => $type,
'str_value' => 'str' == $type || 'json' == $type ? $value : null,
'int_value' => 'int' == $type ? intval($value) : 0,
'float_value' => 'float' == $type ? floatval($value) : 0
];

if (empty($exist)) {
return $this->db->query($this->db->insert('table.fields')
->rows([
'cid' => $cid,
'name' => $name,
'type' => $type,
'str_value' => 'str' == $type ? $value : null,
'int_value' => 'int' == $type ? intval($value) : 0,
'float_value' => 'float' == $type ? floatval($value) : 0
]));
$rows['cid'] = $cid;
$rows['name'] = $name;

return $this->db->query($this->db->insert('table.fields')->rows($rows));
} else {
return $this->db->query($this->db->update('table.fields')
->rows([
'type' => $type,
'str_value' => 'str' == $type ? $value : null,
'int_value' => 'int' == $type ? intval($value) : 0,
'float_value' => 'float' == $type ? floatval($value) : 0
])
->rows($rows)
->where('cid = ? AND name = ?', $cid, $name));
}
}
Expand Down Expand Up @@ -872,7 +873,8 @@ protected function ___fields(): Config
->where('cid = ?', $this->cid));

foreach ($rows as $row) {
$fields[$row['name']] = $row[$row['type'] . '_value'];
$value = 'json' == $row['type'] ? json_decode($row['str_value'], true) : $row[$row['type'] . '_value'];
$fields[$row['name']] = $value;
}

return new Config($fields);
Expand Down
12 changes: 9 additions & 3 deletions var/Widget/Contents/Post/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,14 @@ public function getDefaultFieldItems(): array
if (preg_match("/^fields\[(.+)\]$/", $name, $matches)) {
$name = $matches[1];
} else {
$inputName = 'fields[' . $name . ']';
if (preg_match("/^(.+)\[\]$/", $name, $matches)) {
$name = $matches[1];
$inputName = 'fields[' . $name . '][]';
}

foreach ($item->inputs as $input) {
$input->setAttribute('name', 'fields[' . $name . ']');
$input->setAttribute('name', $inputName);
}
}

Expand Down Expand Up @@ -662,8 +668,8 @@ protected function getFields(): array
}

$customFields = $this->request->getArray('fields');
if (!empty($customFields)) {
$fields = array_merge($fields, $customFields);
foreach ($customFields as $key => $val) {
$fields[$key] = [is_array($val) ? 'json' : 'str', $val];
}

return $fields;
Expand Down

0 comments on commit 6f19a24

Please sign in to comment.