From d0ec2ea369b2f573edd28dfadefd6610f3f15449 Mon Sep 17 00:00:00 2001 From: manageryzy Date: Wed, 22 Apr 2015 20:00:02 +0800 Subject: [PATCH] init Signed-off-by: manageryzy --- Plugin.php | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 51 ++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 Plugin.php create mode 100644 readme.md diff --git a/Plugin.php b/Plugin.php new file mode 100644 index 0000000..84456c8 --- /dev/null +++ b/Plugin.php @@ -0,0 +1,126 @@ +personalConfigHandle = array('UserSign_Plugin', 'personalConfigHandle'); +// Typecho_Plugin::factory('Widget_Archive')->select = array('UserSign_Plugin', 'selectHandle'); + return _t($info); + } + + //SQL创建 + public static function sqlInstall() + { + $db = Typecho_Db::get(); + $type = explode('_', $db->getAdapterName()); + $type = array_pop($type); + $prefix = $db->getPrefix(); + try { + $select = $db->select('table.users.userSign')->from('table.users'); + $db->query($select); + return '检测到个性签名字段,插件启用成功'; + } catch (Typecho_Db_Exception $e) { + $code = $e->getCode(); + if(('Mysql' == $type && (0 == $code ||1054 == $code || $code == '42S22')) || + ('SQLite' == $type && ('HY000' == $code || 1 == $code))) { + try { + if ('Mysql' == $type) { + $db->query("ALTER TABLE `".$prefix."users` ADD `userSign` VARCHAR( 255 ) DEFAULT '' COMMENT '用户个性签名';"); + } else if ('SQLite' == $type) { + $db->query("ALTER TABLE `".$prefix."users` ADD `userSign` VARCHAR( 10 ) DEFAULT ''"); + } else { + throw new Typecho_Plugin_Exception('不支持的数据库类型:'.$type); + } + return '建立个性签名字段,插件启用成功'; + } catch (Typecho_Db_Exception $e) { + $code = $e->getCode(); + if(('Mysql' == $type && 1060 == $code) ) { + return '个性签名已经存在,插件启用成功'; + } + throw new Typecho_Plugin_Exception('个性签名插件启用失败。错误号:'.$code); + } + } + throw new Typecho_Plugin_Exception('数据表检测失败,个性签名插件启用失败。错误号:'.$code); + } + } + + /** + * 禁用插件方法,如果禁用失败,直接抛出异常 + * + * @static + * @access public + * @return void + * @throws Typecho_Plugin_Exception + */ + public static function deactivate(){} + + /** + * 获取插件配置面板 + * + * @access public + * @param Typecho_Widget_Helper_Form $form 配置面板 + * @return void + */ + public static function config(Typecho_Widget_Helper_Form $form){ + + } + + /** + * 个人用户的配置面板 + * + * @access public + * @param Typecho_Widget_Helper_Form $form + * @return void + */ + public static function personalConfig(Typecho_Widget_Helper_Form $form){ + $db = Typecho_Db::get(); + + $user = Typecho_Widget::widget('Widget_User'); + $user->execute(); + + $res = $db->fetchRow($db->select('table.users.userSign')->from('table.users')->where('uid = ?', $user->uid)); + + $sign = new Typecho_Widget_Helper_Form_Element_Text('userSign', NULL, $res['userSign'], _t('个性签名')); + + $form->addInput($sign); + } + + public static function personalConfigHandle($settings,$isSetup){ + $db = Typecho_Db::get(); + + if($isSetup) + { + Typecho_Widget::widget('Widget_Abstract_Options')->insert(array( + 'name' => '_plugin:UserSign', + 'value' => serialize($settings), + 'user' => 0 + )); + } + + + $user = Typecho_Widget::widget('Widget_User'); + $user->execute(); + + $db->query($db->sql()->where('uid = ?', $user->uid)->update('table.users')->rows(array('userSign'=>Typecho_Common::removeXSS($settings['userSign'])))); + } + +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..66ed521 --- /dev/null +++ b/readme.md @@ -0,0 +1,51 @@ +## typecho 用户个性签名插件 + +### 安装方法: +在插件目录下面建立UserSign文件夹,把Plugin.php复制进去 + +**注意:由于typecho的问题,插件的用户配置在读取的时候会自动读取在配置表里面的插件数据。如果要让用户的设置正常运行请修改 '/var/Widget/Users/Profile.php'。修改下面这个函数。不修改的话用户的个人页面里面显示用户的当前签名** + + /** + * 输出自定义设置选项 + * + * @access public + * @param string $pluginName 插件名称 + * @param string $className 类名称 + * @param string $pluginFileName 插件文件名 + * @param string $group 用户组 + * @return Typecho_Widget_Helper_Form + */ + public function personalForm($pluginName, $className, $pluginFileName, &$group) + { + /** 构建表格 */ + $form = new Typecho_Widget_Helper_Form($this->security->getIndex('/action/users-profile'), + Typecho_Widget_Helper_Form::POST_METHOD); + $form->setAttribute('name', $pluginName); + $form->setAttribute('id', $pluginName); + + require_once $pluginFileName; + $group = call_user_func(array($className, 'personalConfig'), $form); + $group = $group ? $group : 'subscriber'; + + $options = $this->options->personalPlugin($pluginName); + + if (!empty($options)) { + foreach ($options as $key => $val) { + if(!isset($form->getInput($key)->value)) + $form->getInput($key)->value($val); + } + } + + $form->addItem(new Typecho_Widget_Helper_Form_Element_Hidden('do', NULL, 'personal')); + $form->addItem(new Typecho_Widget_Helper_Form_Element_Hidden('plugin', NULL, $pluginName)); + + $submit = new Typecho_Widget_Helper_Form_Element_Submit(NULL, NULL, _t('保存设置')); + $submit->input->setAttribute('class', 'btn primary'); + $form->addItem($submit); + + return $form; + } + +###使用方法 + +由于已经在users表里面插入了userSign这个字段了,这个字段会自动的被系统读取。只需要在需要的地方输出$user->userSign就可以了 \ No newline at end of file