diff --git a/README.md b/README.md new file mode 100644 index 0000000..9623200 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +Notification for Yii2 +===================== +Notification for Yii2 + +[![Latest Stable Version](https://poser.pugx.org/yiier/yii2-notification/v/stable)](https://packagist.org/packages/yiier/yii2-notification) +[![Total Downloads](https://poser.pugx.org/yiier/yii2-notification/downloads)](https://packagist.org/packages/yiier/yii2-notification) +[![Latest Unstable Version](https://poser.pugx.org/yiier/yii2-notification/v/unstable)](https://packagist.org/packages/yiier/yii2-notification) +[![License](https://poser.pugx.org/yiier/yii2-notification/license)](https://packagist.org/packages/yiier/yii2-notification) + +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-notification "*" +``` + +or add + +``` +"yiier/yii2-notification": "*" +``` + +to the require section of your `composer.json` file. + + + +Migrations +----------- + +Run the following command + +```shell +$ php yii migrate --migrationPath=@yiier/notification/migrations/ +``` + +Usage +----- + +[Notification](/src/models/Notification.php) \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..994c9b5 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "yiier/yii2-notification", + "description": "Notification for Yii2", + "type": "yii2-extension", + "keywords": ["yii2","extension","notification"], + "license": "BSD-4-Clause", + "authors": [ + { + "name": "forecho", + "email": "caizhenghai@gmail.com" + } + ], + "require": { + "yiisoft/yii2": "~2.0.0" + }, + "autoload": { + "psr-4": { + "yiier\\notification\\": "src" + } + } +} diff --git a/src/migrations/m180131_082652_create_notification_table.php b/src/migrations/m180131_082652_create_notification_table.php new file mode 100644 index 0000000..9ef95cc --- /dev/null +++ b/src/migrations/m180131_082652_create_notification_table.php @@ -0,0 +1,44 @@ +createTable($this->tableName, [ + 'id' => $this->primaryKey(), + 'type' => $this->string(20)->notNull(), + 'from_user_id' => $this->integer()->defaultValue(null), + 'user_id' => $this->integer()->notNull(), + 'title' => $this->string()->defaultValue(null), + 'content' => $this->text()->notNull(), + 'model' => $this->string(20)->defaultValue(null), + 'model_id' => $this->integer()->defaultValue(null), + 'status' => $this->smallInteger(2)->defaultValue(0), + 'created_at' => $this->integer()->defaultValue(null), + ]); + + $this->addCommentOnTable($this->tableName, '通知表'); + $this->createIndex('fk_user_id', $this->tableName, ['user_id']); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropTable($this->tableName); + } +} diff --git a/src/models/Notification.php b/src/models/Notification.php new file mode 100644 index 0000000..8df30d9 --- /dev/null +++ b/src/models/Notification.php @@ -0,0 +1,214 @@ + 20], + [['title'], 'string', 'max' => 255], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'type' => Yii::t('app', 'Type'), + 'from_user_id' => Yii::t('app', 'From User ID'), + 'user_id' => Yii::t('app', 'User ID'), + 'title' => Yii::t('app', 'Title'), + 'content' => Yii::t('app', 'Content'), + 'model' => Yii::t('app', 'Model'), + 'model_id' => Yii::t('app', 'Model ID'), + 'status' => Yii::t('app', 'Status'), + 'created_at' => Yii::t('app', 'Created At'), + ]; + } + + /** + * @param bool $insert + * @return bool + */ + public function beforeSave($insert) + { + if (parent::beforeSave($insert)) { + $this->created_at = time(); + return true; + } else { + return false; + } + } + + + /** + * @return int + */ + public static function readAll() + { + return self::updateAll(['user_id' => Yii::$app->user->id]); + } + + + /** + * @param $id + * @return int + */ + public static function read($id) + { + return self::updateAll(['user_id' => Yii::$app->user->id, 'id' => $id]); + } + + + /** + * @return int + */ + public static function delAll() + { + return self::deleteAll(['user_id' => Yii::$app->user->id]); + } + + + /** + * @param $id + * @return int + */ + public static function del($id) + { + return self::deleteAll(['user_id' => Yii::$app->user->id, 'id' => $id]); + } + + /** + * @return int + */ + public static function unreadCount() + { + return self::find()->where(['user_id' => Yii::$app->user->id, 'status' => self::STATUS_UNREAD])->count('id'); + } + + /** + * @param $type + * @param $toUserId + * @param $content + * @param array $params Optional ['from_user_id', 'title', 'model', 'model_id'] + * @param string $userNotification Optional user Notification field + * @throws Exception + */ + public static function create($type, $toUserId, $content, $params = [], $userNotification = '') + { + $model = new self(); + $model->setAttributes(array_merge([ + 'type' => $type, + 'user_id' => $toUserId, + 'content' => $content, + ], $params)); + if ($model->save()) { + if ($userNotification) { + User::updateAllCounters([$userNotification => 1], ['id' => $toUserId]); + } + } else { + throw new Exception(array_values($model->getFirstErrors())[0]); + } + } + + + /** + * @param $type + * @param $content + * @param array $params Optional ['from_user_id', 'title', 'model', 'model_id'] + * @param string $userNotification Optional user Notification field + * @throws Exception + */ + public static function createToAllUser($type, $content, $params = [], $userNotification = '') + { + $items = User::find()->column(); + $rows = []; + foreach ($items as $key => $item) { + $rows[$key]['user_id'] = $item; + $rows[$key]['type'] = $type; + $rows[$key]['content'] = $content; + $rows[$key] = array_merge($rows[$key], $params); + } + if (!self::saveAll(self::tableName(), $rows)) { + throw new Exception('create to all user notifications errors'); + } + if ($userNotification) { + self::syncUserNotificationCount($userNotification); + } + } + + /** + * @param $userNotification + */ + public static function syncUserNotificationCount($userNotification) + { + $items = User::find()->column(); + foreach ($items as $key => $item) { + $unreadCount = self::find()->where(['user_id' => $item, 'status' => self::STATUS_UNREAD])->count('id'); + User::updateAll([$userNotification => $unreadCount, 'id' => $item]); + } + } + + /** + * @param $tableName + * @param array $rows + * @return bool|int + */ + public static function saveAll($tableName, $rows = []) + { + if ($rows) { + return \Yii::$app->db->createCommand() + ->batchInsert($tableName, array_keys(array_values($rows)[0]), $rows) + ->execute(); + } + return false; + } +}