<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\models;

use Yii;

/**
 * 用户组件模型 对应表"{{%user_portlet}}".
 *
 * @property integer $user_id 用户ID
 * @property string $portlet 组件
 * @property string $title 名称
 * @property integer $x 横坐标
 * @property integer $y 纵坐标
 * @property integer $create_time 新增时间
 *
 * @author legendjw http://blog.u-bo.com
 * @since 0.1
 */
class UserPortlet extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_portlet}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['portlet', 'title'], 'required'],
            ['portlet', 'checkPortletUnique'],
            [['user_id', 'x', 'y', 'create_time'], 'integer'],
            [['portlet', 'title'], 'string', 'max' => 64]
        ];
    }

    /**
     * 验证组件是否已经添加过
     */
    public function checkPortletUnique($attribute, $params)
    {
        $portlet = $this->$attribute;
        if (static::find()->where(['portlet' => $portlet, 'user_id' => Yii::$app->user->id])->one()) {
            $this->addError($attribute, '当前组件已经存在，无需重复添加');
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'user_id' => '用户ID',
            'portlet' => '组件',
            'title' => '名称',
            'x' => '横坐标',
            'y' => '纵坐标',
            'create_time' => '新增时间',
        ];
    }

    /**
     * 前置操作
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($insert) {
                $portlets = Portlet::getPortlet();
                $this->user_id = Yii::$app->user->id;
                $this->x = $portlets[$this->portlet]['x'];
                $this->y = $portlets[$this->portlet]['y'];
                $this->create_time = time();
            }

            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取某个用户的组件
     */
    public static function getUserPortlet($userId = null)
    {
        $userId = ($userId === null) ? Yii::$app->user->id : $userId;
        $portlets = static::find()
            ->where(['user_id' => $userId])
            ->asArray()
            ->orderBy(['y' => SORT_ASC, 'x' => SORT_ASC])
            ->all();

        return $portlets;
    }

    /**
     * 新增用户是添加默认系统组件
     *
     * @param Event $event 新增用户后触发的事件对象
     */
    public static function addDefaultPortlet($event)
    {
        $user = $event->sender;
        $portlets = Portlet::getPortlet();
        $insertData = [];

        foreach ($portlets as $portlet) {
            if ($portlet['system'] == Portlet::IS_SYSTEM) {
                $insertData[] = [$user->user_id, $portlet['id'], $portlet['title'], $portlet['x'], $portlet['y'], time()];
            }
        }
        Yii::$app->db->createCommand()->batchInsert(self::tableName(), ['user_id', 'portlet', 'title', 'x', 'y', 'create_time'], $insertData)->execute();
    }

    public static function deleteUserPortlet()
    {
    }

}
