* split multiple values

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

* fix json input display
This commit is contained in:
joyqi 2023-05-16 21:45:41 +08:00 committed by GitHub
parent c725fec12e
commit 6f19a24aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 25 deletions

View File

@ -9,8 +9,8 @@ $defaultFields = isset($post) ? $post->getDefaultFieldItems() : $page->getDefaul
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>
@ -37,12 +37,14 @@ $defaultFields = isset($post) ? $post->getDefaultFieldItems() : $page->getDefaul
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>

View File

@ -345,41 +345,42 @@ class Contents extends Base implements QueryInterface
*
* @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));
}
}
@ -872,7 +873,8 @@ class Contents extends Base implements QueryInterface
->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);

View File

@ -229,8 +229,14 @@ class Edit extends Contents implements ActionInterface
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);
}
}
@ -662,8 +668,8 @@ class Edit extends Contents implements ActionInterface
}
$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;