Skip to content
Permalink
Browse files

upload plugin

  • Loading branch information...
jiangmuzi committed Oct 16, 2015
1 parent 88fd1ba commit 60a6898434be6b2c68c5ef6ac3fc2dd3915ada70
Showing with 165 additions and 0 deletions.
  1. +35 −0 Action.php
  2. +130 −0 Plugin.php
@@ -0,0 +1,35 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
class TeStat_Action extends Typecho_Widget implements Widget_Interface_Do
{
private $db;
private $options;
private $prefix;
public function action()
{
$this->db = Typecho_Db::get();
$this->prefix = $this->db->getPrefix();
$this->options = Typecho_Widget::widget('Widget_Options');
$cid = $this->request->cid;
if(!$cid)
$this->response->throwJson(array('status'=>0,'msg'=>'请选择喜欢的文章!'));
$likes = Typecho_Cookie::get('__post_likes');
if(empty($likes)){
$likes = array();
}else{
$likes = explode(',', $likes);
}
if(!in_array($cid,$likes)){
$row = $this->db->fetchRow($this->db->select('likesNum')->from('table.contents')->where('cid = ?', $cid)->limit(1));
$this->db->query($this->db->update('table.contents')->rows(array('likesNum' => (int)$row['likesNum']+1))->where('cid = ?', $cid));
array_push($likes, $cid);
$likes = implode(',', $likes);
Typecho_Cookie::set('__post_likes', $likes); //记录查看cookie
$this->response->throwJson(array('status'=>1,'msg'=>'成功点赞!'));
}
$this->response->throwJson(array('status'=>0,'msg'=>'你已经点赞过了!'));
}
}
@@ -0,0 +1,130 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 浏览数、喜欢数统计插件
*
* @package TeStat
* @author 绛木子
* @version 1.0.0
* @link http://lixianhua.com
*/
class TeStat_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
// contents 表中若无 viewsNum 字段则添加
if (!array_key_exists('viewsNum', $db->fetchRow($db->select()->from('table.contents'))))
$db->query('ALTER TABLE `'. $prefix .'contents` ADD `viewsNum` INT(10) DEFAULT 0;');
// contents 表中若无 likesNum 字段则添加
if (!array_key_exists('likesNum', $db->fetchRow($db->select()->from('table.contents'))))
$db->query('ALTER TABLE `'. $prefix .'contents` ADD `likesNum` INT(10) DEFAULT 0;');
//增加浏览数
Typecho_Plugin::factory('Widget_Archive')->singleHandle = array('TeStat_Plugin', 'viewCounter');
//把新增的字段添加到查询中
Typecho_Plugin::factory('Widget_Archive')->select = array('TeStat_Plugin', 'selectHandle');
//添加动作
Helper::addAction('likes', 'TeStat_Action');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeAction('likes');
$delFields = Typecho_Widget::widget('Widget_Options')->plugin('TeStat')->delFields;
if($delFields){
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$db->query('ALTER TABLE `'. $prefix .'contents` DROP `viewsNum`;');
$db->query('ALTER TABLE `'. $prefix .'contents` DROP `likesNum`;');
}
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$delFields = new Typecho_Widget_Helper_Form_Element_Radio('delFields',
array(0=>_t('保留数据'),1=>_t('删除数据'),), '0', _t('卸载设置'),_t('卸载插件后数据是否保留'));
$form->addInput($delFields);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 增加浏览量
* @params Widget_Archive $archive
* @return void
*/
public static function viewCounter($archive){
if($archive->is('single')){
$cid = $archive->cid;
$views = Typecho_Cookie::get('__post_views');
if(empty($views)){
$views = array();
}else{
$views = explode(',', $views);
}
if(!in_array($cid,$views)){
$db = Typecho_Db::get();
$db->query($db->update('table.contents')->rows(array('viewsNum' => (int)$archive->viewsNum+1))->where('cid = ?', $cid));
array_push($views, $cid);
$views = implode(',', $views);
Typecho_Cookie::set('__post_views', $views); //记录查看cookie
}
}
}
//cleanAttribute('fields')清除查询字段,select *
public static function selectHandle($archive){
$user = Typecho_Widget::widget('Widget_User');
if ('post' == $archive->parameter->type || 'page' == $archive->parameter->type) {
if ($user->hasLogin()) {
$select = $archive->select()->where('table.contents.status = ? OR table.contents.status = ? OR
(table.contents.status = ? AND table.contents.authorId = ?)',
'publish', 'hidden', 'private', $user->uid);
} else {
$select = $archive->select()->where('table.contents.status = ? OR table.contents.status = ?',
'publish', 'hidden');
}
} else {
if ($user->hasLogin()) {
$select = $archive->select()->where('table.contents.status = ? OR
(table.contents.status = ? AND table.contents.authorId = ?)', 'publish', 'private', $user->uid);
} else {
$select = $archive->select()->where('table.contents.status = ?', 'publish');
}
}
$select->where('table.contents.created < ?', Typecho_Date::gmtTime());
$select->cleanAttribute('fields');
return $select;
}
}

0 comments on commit 60a6898

Please sign in to comment.
You can’t perform that action at this time.