hannoma/larapolls

在 Laravel 框架中创建、查看和管理投票

安装: 54

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 0

分支: 1

开放问题: 0

语言:HTML

类型:

v1.0.2 2018-04-02 14:13 UTC

This package is not auto-updated.

Last update: 2024-10-02 04:50:51 UTC


README

Laravel 包,用于显示、创建和管理具有权限的投票!

演示

使用中

安装

您可以通过 composer 安装此包

composer require hannoma/larapolls

在 Laravel 5.5 中,服务提供者将自动注册。在框架的旧版本中,只需在 config/app.php 文件中添加服务提供者即可

'providers' => [
    // ...
    Hannoma\Larapolls\LarapollsServiceProvider::class,
];

您可以使用以下命令发布包文件

php artisan vendor:publish --provider="Hannoma\Larapolls\LarapollsServiceProvider"

因为此包使用 spatie/laravel-permission 来处理权限,所以您还应该发布该包的迁移文件

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"

迁移发布后,您可以通过运行迁移来创建投票和权限表

php artisan migrate

配置

所有重要配置选项都可以在 config/larapolls.php 文件中找到

<?php
return [
  //IMPORTANT: THIS PACKAGE USES USER MODEL (app/User.php)!!!

  /*
  |--------------------------------------------------------------------------
  | User name variable name
  |--------------------------------------------------------------------------
  |
  | Here you can specify how the username database field is set for the user
  |
  */
  'username_key' => 'name',

  /*
  |--------------------------------------------------------------------------
  | Basic settings
  |--------------------------------------------------------------------------
  |
  | maxOptionCount: This is the limit for options a poll can have if it is created
  | over the UI way.
  |
  | maxUnallowedPolls: If a user does not have the appertaining permission,
  | a poll created by this user is not allowed. This means that only this user
  | and admins can see the poll. This value sets the maximum amount of polls that
  | are not allowed for a user. If this maximum is reached, the user can not create
  | any polls until an admin allows some polls of the user.
  |
  */
  'maxOptionCount' => 10,
  'maxUnallowedPolls' => 2,

  /*
  |--------------------------------------------------------------------------
  | Bootstrap Version
  |--------------------------------------------------------------------------
  |
  | Based on the Bootstrap Version you are using the views change
  | true = Bootstrap Version 4.x
  | false = Bootstrap Version 3.x
  |
  */
  'bootstrap_v4' => true,

  /*
  |--------------------------------------------------------------------------
  | Do you use fontawesome Version 5?
  |--------------------------------------------------------------------------
  |
  | This package uses icons of fontawesome Version 5.X
  | If you are already using Font Awesome Version 5.X in your project, then set
  | this value to true.
  |
  */
  'fontawesome_v5' => false,

  /*
  |--------------------------------------------------------------------------
  | Date Format
  |--------------------------------------------------------------------------
  |
  | Please enter your date format Here
  | d = DAY
  | m = MONTH
  | Y = YEAR
  |
  */
  'date_format' => 'd.m.Y',

  /*
  |--------------------------------------------------------------------------
  | The master layout file for your site
  |--------------------------------------------------------------------------
  |
  | By default Laravel's master file is the layouts.app file, but if your
  | master layout file is somewhere else, you can specify it below
  |
  */
  'master_file_extend' => 'layouts.app',

  /*
  |--------------------------------------------------------------------------
  | Auth Middleware
  |--------------------------------------------------------------------------
  |
  | By default Laravel's auth middleware is 'auth', but if your
  | auth middleware is called anything else, you can specify it below
  |
  */
  'authMiddleware' => 'auth',

  /*
  |--------------------------------------------------------------------------
  | Poll Routes
  |--------------------------------------------------------------------------
  |
  | Here you can specify the specific routes for larapolls.
  | home: This is the prefix for larapolls
  | profile: enter the route name for your user profile
  | profile_argument: Surely you are using a route parameter for your profile route. Set this setting to your parameter name
  | profile_arg_value: Set this to the database field of your user model which you are using to retrieve the user on the profile page
  | login: Set this to your login route
  |
  */
  'routes' => [
      'home'       => 'polls',
      'profile'    => 'profile',
      'profile_argument' => 'username',
      'profile_arg_value' => 'name',
      'login'      => 'login',
  ],

  /*
  |--------------------------------------------------------------------------
  | Permissions
  |--------------------------------------------------------------------------
  |
  | Here you can specify the permission names
  | !! If you already have run "php artisan larapolls:setup_roles" or added some permissions, do not change this!
  |
  */
  'permissions' => [
    //Prefix for all permissions
    'prefix' => 'larapolls_',

    // "STANDARD" PERMISSIONS

    // The user is allowed to create a new poll
      // BUT the user must have the permission to post the poll in a category
      // If the user has the permission to create new categories, he can choose freely
      // If the user has only one (or more) permissions to create a poll in a specific category (See below 'createPollWithCategory'),
      // he can choose from these categories
      // Therefore if the user has not any of these permissions, he can see the "create a poll" form, but CAN'T create a poll!
    'createPoll' => 'createPoll',
    // The user is allowed to create a sticky poll
    'createPollSticky' => 'createPollSticky',
    //The user is allowed to create a poll where multiple options can be checked
    'createPollMultiple' => 'createPollMultiple',
    // The user is allowed to create a poll where negative votes are allowed
    // REQUIRES multiple Poll !
    'createPollContra' => 'createPollContra',

    // ADMIN PERMISSIONS

    //The user is allowed to unlock/allow other polls
    'allowPoll' => 'allowPoll',
    //the user is allowed to delete any poll
    'deletePoll' => 'deletePoll',
    //The user is allowed to create a poll with any (existing or not) category
    'createNewCategory' => 'createNewCategory',
    //The user can view and vote polls of all categories even protected ones
    'showAllCategories' => 'showAllCategories',

    // THE FOLLOWING PERMISSIONS ARE CATEGORY SPECIFIC
    // These can be set for more than one category!

      // Specific categories
    //The user is able to allow polls of this category
    //Please give the permission: {PREFIX for Larapolls}_allowPollWithCategory_{Your Category goes here}
    'allowPollWithCategory' => 'allowPollWithCategory_',
    //The user is able to delete polls of this category
    //Please give the permission: {PREFIX for Larapolls}_deletePollWithCategory_{Your Category goes here}
    'deletePollWithCategory' => 'deletePollWithCategory_',
    //The user is allowed to create polls in this category
    //Please give the permission: {PREFIX for Larapolls}_createPollWithCategory_{Your Category goes here}
    'createPollWithCategory' => 'createPollWithCategory_',
      // Protected categories
    //The user is allowed to view and vote polls of the protected category
    //Please give the permission: {PREFIX for Larapolls}_showPollWithCategory_{Your Protected Category goes here}
    'showPollWithCategory' => 'showPollWithCategory_',
  ],
  //!! CAUTION: Every user gets these permissions!
    // Maybe you also want to specify a "general" category where ALL User can create Polls
    // Do this be adding the permission 'createPollWithCategory_{Your "general" category name}'
  'standard_permissions' => [
    'createPoll', 'createPollSticky', 'createPollMultiple', 'createPollContra'
  ],
  // The user is allowed to delete his own polls!
  'delete_own_poll' => true,

  /*
  |--------------------------------------------------------------------------
  | Protected Categories
  |--------------------------------------------------------------------------
  |
  | If you have categories that shall be only accessed by admins or users with
  | permission, add your category in this array and give the users that you want to
  | access this category the appertaining permission
  |
  */
  'protectedCategories' => [

  ],
];

注意:当您首次访问 Larapolls 的主页路由时,定义在 standard_permissions 数组中的权限会被添加到 larapolls_standard 角色。如果您稍后想添加或删除此角色的权限,请这样做

use Spatie\Permission\Models\Role;

$role = Role::where('name', 'larapolls_standard')->first();
//Add a permission
$role->givePermissionTo('your permission');
//Remove a permission
$role->revokePermissionTo('your permission');

或者您可以从某些用户中删除该角色

use Spatie\Permission\Models\Role;

$user->removeRole('larapolls_standard');

本地化

发布此包的供应商文件后,您可以在 resources/lang/vendor/larapolls/ 中找到本地化文件。此包支持的语言有

  • 英语
  • 德语

如果您想添加您的本地化,创建 resources/lang/vendor/larapolls/YOUR LOCALE/ 文件夹。然后只需复制另一个本地化的 larapolls.php 本地化文件并将字符串翻译。如果您添加了您的本地化,我将非常感激您创建一个带有您的本地化的拉取请求 ^^

视图

此包是为 Bootstrap 版本 4.x 创建和设计的,但您可以选择使用 Bootstrap 版本 3.x。因此,您需要将 config/larapolls.php 文件中的 bootstrap_v4 的值更改为 false。Bootstrap 版本 4.x 的模板称为 *_v4.blade.php,版本 3.x 的模板称为 *_v3.blade.php。注意:因为是用版本 4.x 创建的,所以版本 3.x 的视图文件与版本 4.x 的视图文件包含相同的代码!因此,您需要更改一些内容才能使其与版本 3.x 一起工作!

个性化

发布包的供应商文件后,您可以在 resources/views/vendor/larapolls/ 中找到 blade 模板。您对这些文件的所有更改都将显示在您的网站上。 注意:不要更改任何 if 条件或其他 blade 命令,否则此包可能不再工作!

使用

首先,将 Spatie\Permission\Traits\HasRoles 特性添加到您的 User 模型中。请使用用户模型 App\User,否则使用 Larapolls 时将出现错误

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    // ...
}

权限设置

此包包含 artisan 命令来设置不同角色和类别的权限。只需运行 php artisan larapolls:setup_roles 并使用这些选项

  • --A--admin 用于管理员角色
  • --M--moderator 用于版主(与特定类别绑定)
  • --D--defaultMember 用于特定类别的成员

显示投票

所有投票都显示在 larapolls.home 路由上。

您可以使用 larapolls.category 路由来显示某一类别的投票,该路由需要传递一个参数 category

您也可以在任何页面上显示具有特定 ID 的投票:

{!! Hannoma\Larapolls\PollDrawer::draw($poll_id) !!}

如果您使用此命令,请确保在您的页面上导入 Font Awesome 版本 5.x。

如果您有一些只能由管理员或具有权限的用户访问的分类,请将您的分类添加到配置文件中的 protectedCategories 数组中,并授予您希望访问此分类的用户相应的权限

$user->givePermissionTo('larapolls_showPollWithCategory_{Your category goes here}');

如果您之前没有创建此权限,您可能需要这样做:

use Spatie\Permission\Models\Permission;

$permission = Permission::create(['name' => 'larapolls_showPollWithCategory_{Your category goes here}']);