/** * 初始化当前输入项 * * @access public * @param string $name 表单元素名称 * @param array $options 选择项 * @return Typecho_Widget_Helper_Layout */ public function input($name = NULL, array $options = NULL) { $input = new Typecho_Widget_Helper_Layout('textarea', array('id' => $name . '-0-' . self::$uniqueId, 'name' => $name)); $this->label->setAttribute('for', $name . '-0-' . self::$uniqueId); $this->container($input->setClose(false)); return $input; }
/** * 初始化当前输入项 * * @access public * @param string $name 表单元素名称 * @param array $options 选择项 * @return Typecho_Widget_Helper_Layout */ public function input($name = NULL, array $options = NULL) { $input = new Typecho_Widget_Helper_Layout('select'); $this->container($input->setAttribute('name', $name)->setAttribute('id', $name . '-0-' . self::$uniqueId)); $this->label->setAttribute('for', $name . '-0-' . self::$uniqueId); foreach ($options as $value => $label) { $this->_options[$value] = new Typecho_Widget_Helper_Layout('option'); $input->addItem($this->_options[$value]->setAttribute('value', $value)->html($label)); } return $input; }
/** * 初始化当前输入项 * * @access public * @param string $name 表单元素名称 * @param array $options 选择项 * @return Typecho_Widget_Helper_Layout */ public function input($name = NULL, array $options = NULL) { foreach ($options as $value => $label) { $this->_options[$value] = new Typecho_Widget_Helper_Layout('input'); $item = $this->multiline(); $item->addItem($this->_options[$value]->setAttribute('name', $this->name . '[]')->setAttribute('type', 'checkbox')->setAttribute('value', $value)->setAttribute('id', $this->name . '-' . $value)); $labelItem = new Typecho_Widget_Helper_Layout('label'); $item->addItem($labelItem->setAttribute('for', $this->name . '-' . $value)->html($label)); $this->container($item); } return current($this->_options); }
/** * 获取插件配置面板 * * @access public * @param Typecho_Widget_Helper_Form $form 配置面板 * @return void */ public static function config(Typecho_Widget_Helper_Form $form) { $introduce = new Typecho_Widget_Helper_Layout("div"); $introduce->html(<<<EOI <p>在 Typecho 正式显示 404 之前,此插件会遍历列表并作出跳转。</p> <p>规则列表一行一个,格式为: Pattern Rewrite结果 [flags]</p> <p>比如【 <code class="success">^\\/(\\d+)\\/?\$ /archives/\$1 T</code> 】可以把 /12 以302重定向跳转到 /archivers/12 </p> <p><a href="https://github.com/laobubu/Typecho_rewriterule">详细说明看这里</a></p> EOI ); $rules = new Typecho_Widget_Helper_Form_Element_Textarea('rules', NULL, '不符合书写规范的行可以当注释哦', _t('规则列表')); $form->addInput($rules); $form->addItem($introduce); }
public static function config(Typecho_Widget_Helper_Form $form) { $help = new Typecho_Widget_Helper_Layout('div', array('style' => 'color: #999; background-color: #eee; border-radius: 10px; padding: 10px;')); $help->html("<strong>使用方法</strong><pre><small>```c#\nConsole.Write(\"Hello World!\");\n```\n或行内代码\n`js console.log(\"Hi!\");`</small></pre>"); $form->addItem($help); $themes = array_map('basename', glob(dirname(__FILE__) . '/themes/*.css')); $themes = array_combine($themes, $themes); $theme = new Typecho_Widget_Helper_Form_Element_Select('theme', $themes, 'prism-coy.css', _t('代码样式')); $form->addInput($theme->addRule('enum', _t('必须选择样式'), $themes)); $showLineNumber = new Typecho_Widget_Helper_Form_Element_Checkbox('showln', array('showln' => _t('显示行号')), array('showln'), _t('是否在大段代码左侧显示行号')); $form->addInput($showLineNumber); $forceWrap = new Typecho_Widget_Helper_Form_Element_Checkbox('forceWrap', array('forceWrap' => _t('强制换行')), array('forceWrap'), _t('是否强制换行')); $form->addInput($forceWrap); $showLang = new Typecho_Widget_Helper_Form_Element_Checkbox('showlang', array('showlang' => _t('显示语言标签')), array('showlang'), _t('是否在大段代码右上角显示语言')); $form->addInput($showLang); }
/** * 在容器里增加元素 * * @access public * @param Typecho_Widget_Helper_Layout $item 表单元素 * @return $this */ public function container(Typecho_Widget_Helper_Layout $item) { /** 创建表单容器 */ if (empty($this->container)) { $this->container = new Typecho_Widget_Helper_Layout('li'); $this->addItem($this->container); } $this->container->addItem($item); return $this; }
/** * 显示表单 * * @access public * @return void */ public function render() { $id = md5(implode('"', array_keys($this->_inputs))); /** 恢复表单值 */ if (isset($_SESSION['__typecho_form_record_' . $id])) { $record = $_SESSION['__typecho_form_record_' . $id]; $message = $_SESSION['__typecho_form_message_' . $id]; foreach ($this->_inputs as $name => $input) { $input->value(isset($record[$name]) ? $record[$name] : $input->value); /** 显示错误消息 */ if (isset($message[$name])) { $input->message($message[$name]); } } unset($_SESSION['__typecho_form_record_' . $id]); } parent::render(); unset($_SESSION['__typecho_form_message_' . $id]); }
/** * 生成表单 * * @access public * @param string $action 表单动作 * @return Typecho_Widget_Helper_Form_Element */ public function form($action = NULL) { /** 构建表格 */ $form = new Typecho_Widget_Helper_Form(Typecho_Common::url('/action/contents-attachment-edit', $this->options->index), Typecho_Widget_Helper_Form::POST_METHOD); /** 文件名称 */ $name = new Typecho_Widget_Helper_Form_Element_Text('name', NULL, $this->title, _t('标题 *')); $form->addInput($name); /** 文件缩略名 */ $slug = new Typecho_Widget_Helper_Form_Element_Text('slug', NULL, $this->slug, _t('缩略名'), _t('文件缩略名用于创建友好的链接形式,建议使用字母,数字,下划线和横杠.')); $form->addInput($slug); /** 文件描述 */ $description = new Typecho_Widget_Helper_Form_Element_Textarea('description', NULL, $this->attachment->description, _t('描述'), _t('此文字用于描述文件,在有的主题中它会被显示.')); $form->addInput($description); /** 分类动作 */ $do = new Typecho_Widget_Helper_Form_Element_Hidden('do', NULL, 'update'); $form->addInput($do); /** 分类主键 */ $cid = new Typecho_Widget_Helper_Form_Element_Hidden('cid', NULL, $this->cid); $form->addInput($cid); /** 提交按钮 */ $submit = new Typecho_Widget_Helper_Form_Element_Submit(NULL, NULL, _t('提交修改')); $submit->input->setAttribute('class', 'primary'); $delete = new Typecho_Widget_Helper_Layout('a', array('href' => Typecho_Common::url('/action/contents-attachment-edit?do=delete&cid=' . $this->cid, $this->options->index), 'class' => 'operate-delete', 'lang' => _t('你确认删除文件 %s 吗?', $this->attachment->name))); $submit->container($delete->html(_t('删除文件'))); $form->addItem($submit); $name->addRule('required', _t('必须填写文件标题')); $name->addRule(array($this, 'nameToSlug'), _t('文件标题无法被转换为缩略名')); $slug->addRule(array($this, 'slugExists'), _t('缩略名已经存在')); return $form; }
/** * getDefaultFieldItems * * @access public * @return array */ public function getDefaultFieldItems() { $defaultFields = array(); $configFile = $this->options->themeFile($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); } if (function_exists($this->themeCustomFieldsHook)) { call_user_func($this->themeCustomFieldsHook, $layout); } } $items = $layout->getItems(); foreach ($items as $item) { if ($item instanceof Typecho_Widget_Helper_Form_Element) { $name = $item->input->getAttribute('name'); $isFieldReadOnly = $this->pluginHandle('Widget_Abstract_Contents')->trigger($plugged)->isFieldReadOnly($name); if ($plugged && $isFieldReadOnly) { continue; } if (preg_match("/^fields\\[(.+)\\]\$/", $name, $matches)) { $name = $matches[1]; } else { foreach ($item->inputs as $input) { $input->setAttribute('name', 'fields[' . $name . ']'); } } $item->value($fields->{$name}); $elements = $item->container->getItems(); array_shift($elements); $div = new Typecho_Widget_Helper_Layout('div'); foreach ($elements as $el) { $div->addItem($el); } $defaultFields[$name] = array($item->label, $div); } } return $defaultFields; }
/** * 增加到某布局元素集合中 * * @access public * @param Typecho_Widget_Helper_Layout $parent 布局对象 * @return Typecho_Widget_Helper_Layout */ public function appendTo(Typecho_Widget_Helper_Layout $parent) { $parent->addItem($this); return $this; }
public static function form($action = NULL) { /** 构建表格 */ $options = Typecho_Widget::widget('Widget_Options'); $form = new Typecho_Widget_Helper_Form(Typecho_Common::url('/action/huifeng-members-edit', $options->index), Typecho_Widget_Helper_Form::POST_METHOD); $form->setAttribute('enctype', 'multipart/form-data'); // 表示可以上传数据 /* 添加隐藏域限制上传大小 */ $maxfilesize = new Typecho_Widget_Helper_Form_Element_Hidden('MAX_FILE_SIZE', NULL, '1000000', NULL, NULL); $form->addInput($maxfilesize); // 限制大小的隐藏域应在上传标签之前 /** 头像 */ $image_upload = new Typecho_Widget_Helper_Form_Element_Text('image_upload', NULL, NULL, _t('上传头像'), NULL); // 用于头像图片上传 $image_upload->input->setAttribute('type', 'file'); $image = new Typecho_Widget_Helper_Form_Element_Text('image', NULL, NULL, _t('头像'), NULL); // 获取数据库里的头像地址 $img_show = new Typecho_Widget_Helper_Layout('img', NULL); $img_src = '/usr/plugins/HuifengMembers/nopic.jpg'; if ($image->getAttribute('src' != '')) { $img_src = $image->getAttribute('src'); } $img_show->setAttribute('src', $img_src); $img_show->setAttribute('height', '180'); $img_show->setAttribute('style', 'margin-top: 4em;max-width: 180px;max-height: 180px;'); $form->addItem($img_show); $form->addInput($image_upload); $form->addInput($image); /** 会员名称 */ // Typecho_Widget_Helper_Form_Element_Text ($name=NULL, array $options=NULL, $value=NULL, $label=NULL, $categories=NULL) $name = new Typecho_Widget_Helper_Form_Element_Text('name', NULL, NULL, _t('会员名称*'), _t('请填写真实姓名')); $form->addInput($name); /** 所在部门 */ $position = new Typecho_Widget_Helper_Form_Element_Text('position', NULL, NULL, _t('所在部门*'), _t('请正确填写所在部门科室')); $form->addInput($position); /** 联系电话 */ $tel = new Typecho_Widget_Helper_Form_Element_Text('tel', NULL, NULL, _t('联系电话'), NULL); $form->addInput($tel); /** 分类 */ $categories = new Typecho_Widget_Helper_Form_Element_Text('categories', NULL, NULL, _t('分类')); $form->addInput($categories); /** 是否值班 */ $is_onduty = new Typecho_Widget_Helper_Form_Element_Radio('is_onduty', array('否' => _t('否'), '是' => _t('是')), '否', _t('是否值班')); $form->addInput($is_onduty); /** 自定义数据 */ $field = new Typecho_Widget_Helper_Form_Element_Text('field', NULL, NULL, _t('自定义数据'), _t('该项用于用户自定义数据扩展')); $form->addInput($field); /** 会员动作 */ $do = new Typecho_Widget_Helper_Form_Element_Hidden('do'); $form->addInput($do); /** 会员主键 */ $mid = new Typecho_Widget_Helper_Form_Element_Hidden('mid'); $form->addInput($mid); /** 提交按钮 */ $submit = new Typecho_Widget_Helper_Form_Element_Submit(); $submit->input->setAttribute('class', 'btn primary'); $form->addItem($submit); $request = Typecho_Request::getInstance(); if (isset($request->mid) && 'insert' != $action) { /** 更新模式 */ $db = Typecho_Db::get(); $prefix = $db->getPrefix(); $member = $db->fetchRow($db->select()->from($prefix . 'hf_members')->where('mid = ?', $request->mid)); if (!$member) { throw new Typecho_Widget_Exception(_t('会员不存在'), 404); } $name->value($member['name']); $position->value($member['position']); $tel->value($member['tel']); $image->value($member['image']); $categories->value($member['categories']); $is_onduty->value($member['is_onduty']); $field->value($member['field']); $do->value('update'); $mid->value($member['mid']); $submit->value(_t('编辑会员')); $_action = 'update'; } else { $do->value('insert'); $submit->value(_t('增加会员')); $_action = 'insert'; } if (empty($action)) { $action = $_action; } /** 给表单增加规则 */ if ('insert' == $action || 'update' == $action) { $name->addRule('required', _t('必须填写会员名称')); $name->addRule('xssCheck', _t('请勿在会员名称栏输入特殊字符')); $position->addRule('required', _t('必须填写所在部门')); $position->addRule('xssCheck', _t('请勿在所在部门栏输入特殊字符')); // $position->addRule('url', _t('不是一个合法的url')); // $image->addRule('image', _t('不是一个合法的图片地址')); $tel->addRule('required', _t('必须填写联系电话')); $tel->addRule('isInteger', _t('电话号码必须是无符号整数')); $tel->addRule('minLength', _t('电话号码不得小于6位'), 6); $tel->addRule('maxLength', _t('电话号码不得大于11位'), 12); // $image->addRule('required', _t('必须上传头像图片')); } if ('update' == $action) { $mid->addRule('required', _t('会员主键不存在')); $mid->addRule(array(new HuifengMembers_Plugin(), 'HuifengMembersExists'), _t('会员不存在')); } return $form; }
/** * 显示表单 * * @access public * @return void */ public function render() { $id = md5(implode('"', array_keys($this->_inputs))); /** 恢复表单值 */ if ($record = Typecho_Cookie::get('__typecho_form_record_' . $id)) { $message = Typecho_Cookie::get('__typecho_form_message_' . $id); foreach ($this->_inputs as $name => $input) { $input->value(isset($record[$name]) ? $record[$name] : $input->value); /** 显示错误消息 */ if (isset($message[$name])) { $input->message($message[$name]); } } Typecho_Cookie::delete('__typecho_form_record_' . $id); } parent::render(); Typecho_Cookie::delete('__typecho_form_message_' . $id); }
public function input($name = NULL, array $options = NULL) { $input = new Typecho_Widget_Helper_Layout('select', array('onchange' => 'select_theme_change(this);')); $this->container($input->setAttribute('name', $name)->setAttribute('id', $name . '-0-' . self::$uniqueId)); $this->label->setAttribute('for', $name . '-0-' . self::$uniqueId); $this->inputs[] = $input; foreach ($options as $value => $label) { $this->_options[$value] = new Typecho_Widget_Helper_Layout('option'); $input->addItem($this->_options[$value]->setAttribute('value', $value)->html($label)); if (DomainTheme_Plugin::$TEMP_THEME_NAME == $value) { $this->_options[$value]->setAttribute('selected', 'selected'); } } return $input; }
/** * 双因素认证表单 * * @access public * @return Typecho_Widget_Helper_Form */ public function twoFactorAuthForm() { /** 构建表格 */ $form = new Typecho_Widget_Helper_Form($this->security->getIndex('/action/users-profile'), Typecho_Widget_Helper_Form::POST_METHOD); if ($this->user->twoFactorAuthKey) { /** 验证码确认 */ $confirm = new Typecho_Widget_Helper_Form_Element_Text('confirm', NULL, NULL, _t('验证码'), _t('请输入验证码以停用双因素认证.')); $confirm->input->setAttribute('class', 'w-60'); $confirm->addRule('required', _t('请输入验证码')); $form->addInput($confirm); $form->addItem(new Typecho_Widget_Helper_Form_Element_Hidden('do', NULL, 'twoFactorAuth')); $submit = new Typecho_Widget_Helper_Form_Element_Submit('submit', NULL, _t('停用双因素认证')); $submit->input->setAttribute('class', 'btn primary'); $form->addItem($submit); } else { /** 生成密钥 */ $secret = $this->widget('Widget_GoogleAuthenticator')->createSecret(); $qrlink = $this->widget('Widget_GoogleAuthenticator')->getQRCodeGoogleUrl('Typecho Blog', $secret); $text = new Typecho_Widget_Helper_Layout('p', array()); $text->html(_t('请在手机上添加密钥 %s,并在下方输入手机上显示的验证码.<br><a href="%s" target="_blank">扫描二维码</a>', $secret, $qrlink)); $form->addItem($text); /** 确认 */ $confirm = new Typecho_Widget_Helper_Form_Element_Text('confirm', NULL, NULL, _t('验证码'), NULL); $confirm->input->setAttribute('class', 'w-60'); $confirm->addRule('required', _t('请输入验证码以确保双因素认证正确配置')); $form->addInput($confirm); $form->addItem(new Typecho_Widget_Helper_Form_Element_Hidden('secret', NULL, $secret)); $form->addItem(new Typecho_Widget_Helper_Form_Element_Hidden('do', NULL, 'twoFactorAuth')); $submit = new Typecho_Widget_Helper_Form_Element_Submit('submit', NULL, _t('启用双因素认证')); $submit->input->setAttribute('class', 'btn primary'); $form->addItem($submit); } return $form; }