mirror of
https://github.com/typecho/typecho.git
synced 2025-04-21 18:21:53 +02:00
今天就搞到这里,要去陪睡了
This commit is contained in:
parent
5545ce286a
commit
e6bf9e460d
59
admin/custom-fields.php
Normal file
59
admin/custom-fields.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php if(!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
|
||||
<?php
|
||||
$fields = isset($post) ? $post->getFieldItems() : $page->getFieldItems();
|
||||
$defaultFields = isset($post) ? $post->getDefaultFieldItems() : $post->getDefaultFieldItems();
|
||||
?>
|
||||
<section id="custom-field" class="typecho-post-option fold">
|
||||
<label class="typecho-label"><?php _e('自定义字段'); ?></label>
|
||||
<table class="typecho-list-table mono">
|
||||
<colgroup>
|
||||
<col width="25%"/>
|
||||
<col width="10%"/>
|
||||
<col width=""/>
|
||||
<col width="10%"/>
|
||||
</colgroup>
|
||||
<?php foreach ($defaultFields as $field): ?>
|
||||
<?php list ($label, $input) = $field; ?>
|
||||
<tr>
|
||||
<td><?php $label->render(); ?></td>
|
||||
<td colspan="3"><?php $input->render(); ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php foreach ($fields as $field): ?>
|
||||
<tr>
|
||||
<td><input type="text" name="fieldNames[]" value="<?php echo htmlspecialchars($field['name']); ?>" class="text-s w-100"></td>
|
||||
<td>
|
||||
<select name="fieldTypes[]" id="">
|
||||
<option value="str"<?php if ('str' == $field['type']): ?> selected<?php endif; ?>><?php _e('字符'); ?></option>
|
||||
<option 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>
|
||||
</select>
|
||||
</td>
|
||||
<td><textarea name="fieldValues[]" class="text-s w-100" rows="2"><?php echo htmlspecialchars($field['value']); ?></textarea></td>
|
||||
<td>
|
||||
<button type="button" class="btn-xs"><?php _e('删除'); ?></button>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php if (empty($defaultFields) && empty($fields)): ?>
|
||||
<tr>
|
||||
<td><input type="text" name="fieldNames[]" placeholder="<?php _e('字段名称'); ?>" class="text-s w-100"></td>
|
||||
<td>
|
||||
<select name="fieldTypes[]" id="">
|
||||
<option value="str"><?php _e('字符'); ?></option>
|
||||
<option value="int"><?php _e('整数'); ?></option>
|
||||
<option value="float"><?php _e('小数'); ?></option>
|
||||
</select>
|
||||
</td>
|
||||
<td><textarea name="fieldValues[]" placeholder="<?php _e('字段值'); ?>" class="text-s w-100" rows="2"></textarea></td>
|
||||
<td>
|
||||
<button type="button" class="btn-xs"><?php _e('删除'); ?></button>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</table>
|
||||
<p class="description">
|
||||
<button type="button" class="btn-xs"><?php _e('+添加字段'); ?></button>
|
||||
<?php _e('自定义字段可以扩展你的模板功能, 使用方法参见 <a href="">帮助文档</a>'); ?>
|
||||
</p>
|
||||
</section>
|
@ -1,3 +1,4 @@
|
||||
<?php if(!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
|
||||
<?php $content = !empty($post) ? $post : $page; if ($options->markdown && (!$content->have() || $content->isMarkdown)): ?>
|
||||
<script src="<?php $options->adminUrl('js/marked.js?v=' . $suffixVersion); ?>"></script>
|
||||
<script src="<?php $options->adminUrl('js/pagedown.js?v=' . $suffixVersion); ?>"></script>
|
||||
|
@ -1,3 +1,4 @@
|
||||
<?php if(!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
|
||||
<script>
|
||||
(function () {
|
||||
$(document).ready(function () {
|
||||
|
@ -1,3 +1,4 @@
|
||||
<?php if(!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
|
||||
<script>
|
||||
(function () {
|
||||
$(document).ready(function () {
|
||||
|
@ -1,3 +1,4 @@
|
||||
<?php if(!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
|
||||
<?php Typecho_Plugin::factory('admin/write-js.php')->write(); ?>
|
||||
<?php Typecho_Widget::widget('Widget_Metas_Tag_Cloud', 'sort=count&desc=1&limit=200')->to($tags); ?>
|
||||
|
||||
|
@ -48,36 +48,7 @@ Typecho_Widget::widget('Widget_Contents_Post_Edit')->to($post);
|
||||
</div> -->
|
||||
|
||||
<?php include 'file-upload.php'; ?>
|
||||
|
||||
<section id="custom-field" class="typecho-post-option fold">
|
||||
<label class="typecho-label"><?php _e('自定义字段'); ?></label>
|
||||
<table class="mono">
|
||||
<tr>
|
||||
<th width="25%"><?php _e('KEY'); ?></th>
|
||||
<th width="10%"><?php _e('TYPE'); ?></th>
|
||||
<th><?php _e('VALUE'); ?></th>
|
||||
<th width="10%"></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="text" class="text-s w-100"></td>
|
||||
<td>
|
||||
<select name="" id="">
|
||||
<option value=""><?php _e('varchar'); ?></option>
|
||||
<option value=""><?php _e('int'); ?></option>
|
||||
<option value=""><?php _e('float'); ?></option>
|
||||
</select>
|
||||
</td>
|
||||
<td><textarea class="text-s w-100" rows="2"></textarea></td>
|
||||
<td>
|
||||
<button type="button" class="btn-xs"><?php _e('删除'); ?></button>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p class="description">
|
||||
<button type="button" class="btn-xs"><?php _e('+添加字段'); ?></button>
|
||||
<?php _e('自定义字段可以扩展你的模板功能, 使用方法参见 <a href="">帮助文档</a>'); ?>
|
||||
</p>
|
||||
</section>
|
||||
<?php include 'custom-fields.php'; ?>
|
||||
|
||||
<?php Typecho_Plugin::factory('admin/write-post.php')->content($post); ?>
|
||||
<p class="submit clearfix">
|
||||
|
@ -14,3 +14,10 @@ function themeConfig($form) {
|
||||
|
||||
$form->addInput($sidebarBlock->multiMode());
|
||||
}
|
||||
|
||||
/*
|
||||
function themeFields($layout) {
|
||||
$logoUrl = new Typecho_Widget_Helper_Form_Element_Text('logoUrl', NULL, NULL, _t('站点LOGO地址'), _t('在这里填入一个图片URL地址, 以在网站标题前加上一个LOGO'));
|
||||
$layout->addItem($logoUrl);
|
||||
}
|
||||
*/
|
||||
|
@ -22,14 +22,6 @@ require_once 'Typecho/Widget/Helper/Layout.php';
|
||||
*/
|
||||
abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_Layout
|
||||
{
|
||||
/**
|
||||
* 表单标题
|
||||
*
|
||||
* @access protected
|
||||
* @var string
|
||||
*/
|
||||
protected $label;
|
||||
|
||||
/**
|
||||
* 表单描述
|
||||
*
|
||||
@ -78,6 +70,14 @@ abstract class Typecho_Widget_Helper_Form_Element extends Typecho_Widget_Helper_
|
||||
*/
|
||||
public $input;
|
||||
|
||||
/**
|
||||
* 表单标题
|
||||
*
|
||||
* @access public
|
||||
* @var Typecho_Widget_Helper_Layout
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* 表单验证器
|
||||
*
|
||||
|
@ -121,6 +121,17 @@ class Typecho_Widget_Helper_Layout
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getItems
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
return $this->_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置内部数据
|
||||
*
|
||||
|
@ -75,6 +75,25 @@ class Widget_Abstract_Contents extends Widget_Abstract
|
||||
return Typecho_Common::subStr($plainTxt, 0, 100, '...');
|
||||
}
|
||||
|
||||
/**
|
||||
* __fields
|
||||
*
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
protected function __fields()
|
||||
{
|
||||
$fields = array();
|
||||
$rows = $this->db->fetchAll($this->db->select()->from('table.fields')
|
||||
->where('cid = ?', $this->cid));
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$fields[$row['name']] = $fields[$fields['type'] . '_value'];
|
||||
}
|
||||
|
||||
return new Typecho_Config($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章内容摘要
|
||||
*
|
||||
@ -383,6 +402,133 @@ class Widget_Abstract_Contents extends Widget_Abstract
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除自定义字段
|
||||
*
|
||||
* @param integer $cid
|
||||
* @access public
|
||||
* @return integer
|
||||
*/
|
||||
public function deleteFields($cid)
|
||||
{
|
||||
return $this->db->query($this->db->delete('table.contents')
|
||||
->where('cid = ?', $cid));
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存自定义字段
|
||||
*
|
||||
* @param array $fields
|
||||
* @param mixed $cid
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function applyFields(array $fields, $cid)
|
||||
{
|
||||
$exists = array_flip(Typecho_Common::arrayFlatten($this->db->fetchAll($this->db->select('name')
|
||||
->from('table.contents')->where('cid = ?', $cid)), 'name'));
|
||||
|
||||
foreach ($fields as $name => $value) {
|
||||
$type = 'str';
|
||||
|
||||
if (is_array($value) && 2 == count($value)) {
|
||||
list($type, $value) = $value;
|
||||
} else if (strpos($name, ':') > 0) {
|
||||
list ($type, $name) = explode(':', $name, 2);
|
||||
}
|
||||
|
||||
if (isset($exists[$name])) {
|
||||
unset($exists[$name]);
|
||||
}
|
||||
|
||||
$this->setField($name, $type, $value, $cid);
|
||||
}
|
||||
|
||||
foreach ($exists as $name => $value) {
|
||||
$this->db->query($this->db->delete('table.contents')
|
||||
->where('cid = ? AND name = ?', $cid, $name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单个字段
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $type
|
||||
* @param string $value
|
||||
* @param integer $cid
|
||||
* @access public
|
||||
* @return integer
|
||||
*/
|
||||
public function setField($name, $type, $value, $cid)
|
||||
{
|
||||
$exist = $this->db->fetchRow($this->db->select('cid')->from('table.contents')
|
||||
->where('cid = ? AND name = ?', $cid, $name));
|
||||
|
||||
if (empty($exist)) {
|
||||
return $this->db->query($this->db->insert('table.contents')
|
||||
->rows(array(
|
||||
'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
|
||||
)));
|
||||
} else {
|
||||
return $this->db->query($this->db->update('table.contents')
|
||||
->rows(array(
|
||||
'type' => $type,
|
||||
'str_value' => 'str' == $type ? $value : NULL,
|
||||
'int_value' => 'int' == $type ? intval($value) : 0,
|
||||
'float_value' => 'float' == $type ? floatval($value) : 0
|
||||
))
|
||||
->where('cid = ? AND name = ?', $cid, $name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增一个整形字段
|
||||
*
|
||||
* @param string $name
|
||||
* @param integer $value
|
||||
* @param integer $cid
|
||||
* @access public
|
||||
* @return integer
|
||||
*/
|
||||
public function incrIntField($name, $value, $cid)
|
||||
{
|
||||
$exist = $this->db->fetchRow($this->db->select('type')->from('table.contents')
|
||||
->where('cid = ? AND name = ?', $cid, $name));
|
||||
$value = intval($value);
|
||||
|
||||
if (empty($exist)) {
|
||||
return $this->db->query($this->db->insert('table.contents')
|
||||
->rows(array(
|
||||
'cid' => $cid,
|
||||
'name' => $name,
|
||||
'type' => 'int',
|
||||
'str_value' => NULL,
|
||||
'int_value' => $value,
|
||||
'float_value' => 0
|
||||
)));
|
||||
} else {
|
||||
$struct = array(
|
||||
'str_value' => NULL,
|
||||
'float_value' => NULL
|
||||
);
|
||||
|
||||
if ('int' != $exist['type']) {
|
||||
$struct['type'] = 'int';
|
||||
}
|
||||
|
||||
return $this->db->query($this->db->update('table.contents')
|
||||
->rows($struct)
|
||||
->expression('int_value', 'int_value ' . ($value >= 0 ? '+' : '') . $value)
|
||||
->where('cid = ? AND name = ?', $cid, $name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 内容是否可以被修改
|
||||
*
|
||||
|
@ -147,8 +147,12 @@ class Widget_Contents_Page_Edit extends Widget_Contents_Post_Edit implements Wid
|
||||
$page, 'page_draft')
|
||||
->limit(1));
|
||||
|
||||
/** 删除自定义字段 */
|
||||
$this->deleteFields($page);
|
||||
|
||||
if ($draft) {
|
||||
$this->deleteDraft($draft['cid']);
|
||||
$this->deleteFields($draft['cid']);
|
||||
}
|
||||
|
||||
$deleteCount ++;
|
||||
@ -189,6 +193,7 @@ class Widget_Contents_Page_Edit extends Widget_Contents_Post_Edit implements Wid
|
||||
|
||||
if ($draft) {
|
||||
$this->deleteDraft($draft['cid']);
|
||||
$this->deleteFields($draft['cid']);
|
||||
$deleteCount ++;
|
||||
}
|
||||
}
|
||||
|
@ -72,6 +72,34 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFields
|
||||
*
|
||||
* @access protected
|
||||
* @return void
|
||||
*/
|
||||
protected function getFields()
|
||||
{
|
||||
$fields = array();
|
||||
|
||||
if (!empty($this->request->fieldNames)) {
|
||||
$data = $this->request->from('fieldNames', 'fieldTypes', 'fieldValues');
|
||||
foreach ($data['fieldNames'] as $key => $val) {
|
||||
if (empty($val)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fields[$val] = array($data['fieldTypes'][$key], $data['fieldValues'][$key]);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->request->fields)) {
|
||||
$fields = array_merge($fields, $this->request->fields);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据提交值获取created字段值
|
||||
*
|
||||
@ -209,6 +237,7 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
/** 如果它本身不是草稿, 需要删除其草稿 */
|
||||
if (!$isDraftToPublish && $this->draft) {
|
||||
$this->deleteDraft($this->draft['cid']);
|
||||
$this->deleteFields($this->draft['cid']);
|
||||
}
|
||||
|
||||
/** 直接将草稿状态更改 */
|
||||
@ -235,6 +264,9 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
|
||||
/** 同步附件 */
|
||||
$this->attach($realId);
|
||||
|
||||
/** 保存自定义字段 */
|
||||
$this->applyFields($this->getFields(), $realId);
|
||||
|
||||
$this->db->fetchRow($this->select()->where('table.contents.cid = ?', $realId)->limit(1), array($this, 'push'));
|
||||
}
|
||||
@ -307,6 +339,9 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
|
||||
/** 同步附件 */
|
||||
$this->attach($this->cid);
|
||||
|
||||
/** 保存自定义字段 */
|
||||
$this->applyFields($this->getFields(), $realId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -429,6 +464,77 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
return _t('编辑 %s', $this->title);
|
||||
}
|
||||
|
||||
/**
|
||||
* getDefaultFieldItems
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getDefaultFieldItems()
|
||||
{
|
||||
$defaultFields = array();
|
||||
$configFile = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . '/' . $this->options->theme . '/functions.php';
|
||||
$layout = new Typecho_Widget_Helper_Layout();
|
||||
$fields = new Typecho_Config();
|
||||
|
||||
if ($this->have()) {
|
||||
$fields = $this->fields;
|
||||
}
|
||||
|
||||
$this->pluginHandle()->getDefaultFieldItems($layout);
|
||||
|
||||
if (file_exists($configFile)) {
|
||||
require_once $configFile;
|
||||
|
||||
if (function_exists('themeFields')) {
|
||||
themeFields($layout);
|
||||
}
|
||||
}
|
||||
|
||||
$items = $layout->getItems();
|
||||
foreach ($items as $item) {
|
||||
if ($item instanceof Typecho_Widget_Helper_Form_Element) {
|
||||
$name = $item->input->getAttribute('name');
|
||||
|
||||
if (preg_match("/^fields\[(.+)\]$/", $name, $matches)) {
|
||||
$name = $matches[1];
|
||||
} else {
|
||||
$item->input->setAttribute('name', 'fields[' . $name . ']');
|
||||
}
|
||||
|
||||
$item->value($fields->{$name});
|
||||
$defaultFields[$name] = array($item->label, $item->input);
|
||||
}
|
||||
}
|
||||
|
||||
return $defaultFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFieldItems
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function getFieldItems()
|
||||
{
|
||||
$fields = array();
|
||||
|
||||
if ($this->have()) {
|
||||
$defaultFields = $this->getDefaultFieldItems();
|
||||
$rows = $this->db->fetchAll($this->db->select()->from('table.fields')
|
||||
->where('cid = ?', $this->cid));
|
||||
|
||||
foreach ($rows as $row) {
|
||||
if (!isset($defaultFields[$row['name']])) {
|
||||
$fields[$row['name']] = $fields[$fields['type'] . '_value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置内容标签
|
||||
*
|
||||
@ -558,7 +664,7 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
public function writePost()
|
||||
{
|
||||
$contents = $this->request->from('password', 'allowComment',
|
||||
'allowPing', 'allowFeed', 'slug', 'category', 'tags', 'text', 'visibility');
|
||||
'allowPing', 'allowFeed', 'slug', 'category', 'tags', 'text', 'visibility');
|
||||
|
||||
$contents['title'] = $this->request->get('title', _t('未命名文档'));
|
||||
$contents['created'] = $this->getCreated();
|
||||
@ -659,8 +765,12 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
$post, 'post_draft')
|
||||
->limit(1));
|
||||
|
||||
/** 删除自定义字段 */
|
||||
$this->deleteFields($post);
|
||||
|
||||
if ($draft) {
|
||||
$this->deleteDraft($draft['cid']);
|
||||
$this->deleteFields($draft['cid']);
|
||||
}
|
||||
|
||||
$deleteCount ++;
|
||||
@ -703,6 +813,7 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
|
||||
|
||||
if ($draft) {
|
||||
$this->deleteDraft($draft['cid']);
|
||||
$this->deleteFields($draft['cid']);
|
||||
$deleteCount ++;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user