Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Jan 26, 2018
0 parents commit c1e9cdb
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ActionStore for Yii2
====================
ActionStore for Yii2

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yiier/yii2-action-store "*"
```

or add

```
"yiier/yii2-action-store": "*"
```

to the require section of your `composer.json` file.


Migrations
-----------

Run the following command

```shell
$ php yii migrate --migrationPath=@yiier/actionStore/migrations/
```

Usage
-----

Once the extension is installed, simply use it in your code by :

```php
<?= \yiier\actionStore\AutoloadExample::widget(); ?>```
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "yiier/yii2-action-store",
"description": "ActionStore for Yii2",
"type": "yii2-extension",
"keywords": ["yii2","extension","action-store"],
"license": "BSD-4-Clause",
"authors": [
{
"name": "forecho",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": "~2.0.0"
},
"autoload": {
"psr-4": {
"yiier\\actionStore\\": "src"
}
}
}
40 changes: 40 additions & 0 deletions src/actions/ActionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* author : forecho <[email protected]>
* createTime : 2018/1/26 18:14
* description:
*/

namespace yiier\actionStore\actions;

use Yii;
use yii\db\Exception;
use yii\web\Response;
use yiier\actionStore\models\ActionStore;

class ActionAction extends \yii\base\Action
{
public function init()
{
parent::init();
\Yii::$app->controller->enableCsrfValidation = false;
}

public function run()
{
if (Yii::$app->user->isGuest) {
Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl)->send();
} else {
$model = new ActionStore();
$model->load(array_merge(Yii::$app->request->getQueryParams(), ['user_id' => Yii::$app->user->id]), '');
$model->validate();
if (!$model->errors) {
if ($counter = ActionStore::createAction($model)) {
Yii::$app->response->format = Response::FORMAT_JSON;
return $counter;
}
}
throw new Exception(json_encode($model->errors));
}
}
}
44 changes: 44 additions & 0 deletions src/migrations/m171214_101829_create_action_store_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use yii\db\Migration;

/**
* Handles the creation of table `action_store`.
*/
class m171214_101829_create_action_store_table extends Migration
{
/**
* @var string 用户行为表
*/
public $tableName = '{{%action_store}}';

/**
* @inheritdoc
*/
public function up()
{
$this->createTable($this->tableName, [
'id' => $this->primaryKey(),
'type' => $this->string()->notNull(),
'value' => $this->integer()->defaultValue(1),
'user_type' => $this->string()->defaultValue('user'),
'user_id' => $this->integer()->notNull(),
'model' => $this->string()->notNull(),
'model_id' => $this->integer()->notNull(),
'created_at' => $this->integer()->defaultValue(null),
'updated_at' => $this->integer()->defaultValue(null),
]);

$this->addCommentOnTable($this->tableName, '用户行为表');
$this->createIndex('fk_model_id', $this->tableName, ['model', 'model_id', 'type']);
$this->createIndex('fk_user_type_id', $this->tableName, ['user_type', 'user_id', 'type']);
}

/**
* @inheritdoc
*/
public function down()
{
$this->dropTable($this->tableName);
}
}
142 changes: 142 additions & 0 deletions src/models/ActionStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace yiier\actionStore\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;

/**
* This is the model class for table "{{%action_store}}".
*
* @property int $id
* @property string $type
* @property integer $value
* @property string $user_type
* @property int $user_id
* @property string $model
* @property int $model_id
* @property int $created_at
* @property int $updated_at
*/
class ActionStore extends \yii\db\ActiveRecord
{
/**
* @var string
*/
const LIKE_TYPE = 'like';

/**
* @var string
*/
const DISLIKE_TYPE = 'dislike';

/**
* @var string
*/
const CLAP_TYPE = 'clap';

/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%action_store}}';
}

/**
* @inheritdoc
*/
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['type', 'model', 'model_id'], 'required'],
[['user_id', 'model_id', 'value', 'created_at', 'updated_at'], 'integer'],
['user_type', 'default', 'value' => 'user'],
[['type', 'user_type', 'model'], 'string', 'max' => 255],
];
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'type' => Yii::t('app', 'Type'),
'value' => Yii::t('app', 'Value'),
'user_type' => Yii::t('app', 'User Type'),
'user_id' => Yii::t('app', 'User ID'),
'model' => Yii::t('app', 'Model'),
'model_id' => Yii::t('app', 'Model ID'),
'created_at' => Yii::t('app', 'Created At'),
];
}


/**
* @param $model ActionStore
* @return int
* @throws Exception
*/
public static function createAction($model)
{
$conditions = array_filter($model->attributes);
switch ($model->type) {
case self::LIKE_TYPE:
self::deleteAll(array_merge(['type' => self::DISLIKE_TYPE], $conditions));
$data = array_merge(['type' => self::LIKE_TYPE], $conditions);
break;
case self::DISLIKE_TYPE:
self::deleteAll(array_merge(['type' => self::LIKE_TYPE], $conditions));
$data = array_merge(['type' => self::DISLIKE_TYPE], $conditions);
break;

default:
$data = array_merge(['type' => $model->type], $conditions);
break;
}
if ($model->type == self::CLAP_TYPE) {
$value = self::find()->filterWhere($data)->select('value')->scalar();
$model->value = $value + 1;
}
self::deleteAll($data);
$model->load($data, '');
if ($model->save()) {
return $model->resetCounter();
}
throw new Exception(json_encode($model->errors));
}

/**
* @param $model ActionStore
* @return false|int
*/
public static function destroyAction($model)
{
return $model->delete();
}


/**
* 返回计数器
* @return int
*/
public function resetCounter()
{
$data = $this->attributes;
unset($data['id'], $data['created_at'], $data['updated_at'], $data['value']);
return self::find()->filterWhere($data)->select('value')->scalar();
}
}

0 comments on commit c1e9cdb

Please sign in to comment.