d3yii2/d3labels

Yii2 标签

dev-master 2024-04-19 12:14 UTC

This package is auto-updated.

Last update: 2024-09-19 13:05:44 UTC


README

为模型分配多个标签

安装

安装此扩展的首选方法是使用composer

运行以下命令之一

php composer.phar require --prefer-dist d3yii2/d3labels "*"

"d3yii2/d3labels": "*"

将以下内容添加到您的composer.json文件的require部分。

数据库

DB strukture

配置

将d3labels添加到迁移路径并运行迁移

  'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                '@vendor/d3yii2/d3labels/migrations',
            ]
  ]              

将d3labels添加到模块中

    'modules' => [
        'd3labels' => [
            'class' => 'd3yii2\d3labels\Module',
        ],
    ]

通过迁移定义标签

添加新标签的迁移示例

use yii\db\Migration;
use \d3yii2\d3labels\logic\D3Definition;
use d3modules\lietvediba\models\RkInvoice;
use d3system\widgets\ThBadge;
class m190329_095047_invoice_labels extends Migration
{
    /**
    * {@inheritdoc}
    */
    public function safeUp()
     {
         $def = new D3Definition(RkInvoice::class);
         $def->setLabel('Warning label');
         $def->setColor(ThBadge::TYPE_INVERSE);
         $def->setCode('WarningLabel');
         //$def->setCompanyId(14);
         $def->save();
    }
    public function safeDown()
    {
        echo "m190329_095047_invoice_labels cannot be reverted.\n";
        return false;
    }

}     

删除标签的迁移示例

use d3modules\d3accexport\logic\ExportRkInvoiceFormExtensions;
use d3modules\lietvediba\models\RkInvoice;
use d3yii2\d3labels\logic\D3LabelMaintenance;
use yii\db\Migration;

class m210426_100707_label_new_remove  extends Migration {

    public function safeUp() {
        $removedLabelsFromModelRecords = D3LabelMaintenance::removeLabel(ExportRkInvoiceFormExtensions::NEW,RkInvoice::class);
        echo 'Removed Labels FromModel Records: ' . $removedLabelsFromModelRecords .PHP_EOL;

    }

    public function safeDown() {
        echo "m210426_100707_label_new_remove cannot be reverted.\n";
        return false;
    }
}

用于创建标签的部件

模型控制器

添加d3labelsattach和d3labelsremove动作

访问规则

                  [
                        'allow' => true,
                        'actions' => [
                            'd3labelsattach',
                            'd3labelsremove',
                        ],
                        'roles' => [
                            'ModuleAdminRoleName',
                        ],
                    ],

动作

    public function actions(): array
    {
        return [
            'd3labelsattach' => [
                'class' => AttachAction::class,
                'modelName' => D3pPerson::class,
            ],
            'd3labelsremove' => [
                'class' => DeleteAction::class,
                'modelName' => D3pPerson::class,
            ],
        ];
    }

标签管理控制器

namespace cewood\cwstore\controllers;

use cewood\cwstore\models\CwbrProduct;
use ea\app\controllers\LayoutController;
use yii\filters\AccessControl;
use Yii;
use d3yii2\d3labels\components\CreateAction;
use d3yii2\d3labels\components\AttachAction;
use d3yii2\d3labels\components\DeleteAction;
use d3yii2\d3labels\components\DefinitionDeleteAction;

/**
 * Class RkInvoiceSettingsController
 * @package d3modules\lietvediba\controllers
 */
class SettingsController extends LayoutController
{
    /**
     * @inheritdoc
     */
    public function behaviors(): array
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => [
                            'labels',
                            'd3labelscreate',
                            'd3labelsdefinitionremove',
                        ],
                        'roles' => [
                            'ModuleAdminRoleName'
                        ]
                    ],
                ]
            ],
        ];

    }

    /**
     * @return array
     */
    public function actions()
    {
        return [
            'd3labelscreate' => [
                'class' => CreateAction::class,
                'modelName' => CwbrProduct::class,
                'sysCompanyId' => static function(){
                    return Yii::$app->SysCmp->getActiveCompanyId();
                }
            ],
            
            'd3labelsdefinitionremove' => [
                'class' => DefinitionDeleteAction::class,
                'modelName' => CwbrProduct::class,
                //'sysLabelsIdList' => [2]
            ],
        ];
    }

    /**
     * @return string
     */
    public function actionLabels(): string
    {
        return $this->render('labels', [
            'active' => true,
        ]);

    }

}

标签管理视图

<?php

use d3yii2\d3labels\widgets\D3LabelCreate;
use eaBlankonThema\assetbundles\layout\LayoutAsset;
use yii2d3\d3persons\models\D3pPerson;

LayoutAsset::register($this);

/**
 * @var \d3system\yii2\web\D3SystemView $this
 */

$title = Yii::t('d3persons', 'Labels settings');
$this->title = $title;
$this->setPageHeader($title);
$this->setPageIcon('tags');
$this->setPageWiki('drpersons-conf-person-labels');
$this->addPageButtons(\eaBlankonThema\widget\ThReturnButton::widget([
        'link' => ['d3p-person/my-company-index']
]))

/**
 * @var yii\web\View $this
 *
 * */
?>
<div class="panel panel-tab panel-tab-double shadow">
    <div class="panel-body">
        <div class="tab-content">
            <div class="tab-pane fade in active">
                <?= D3LabelCreate::widget([
                    'modelClass' => D3pPerson::class,
                    'sysCompanyId' => Yii::$app->SysCmp->getActiveCompanyId()
                ])?>
            </div>
        </div>
    </div>
</div>

用于显示、添加和删除标签的面板。紧凑型变体

 use cewood\cwstore\models\CwbrProduct;
use d3yii2\d3labels\widgets\D3LabelCreate;

?>
<?= D3LabelCreate::widget([
    'modelClass' => CwbrProduct::class,
    'sysCompanyId' => Yii::$app->SysCmp->getActiveCompanyId()
]) ?>

在DetailView中的简单输出

        echo ThDetailView::widget([
            'model' => $model,
            'attributes' => [
                [
                    'label' => 'Labels',
                    'format' => 'raw',
                    'value' => D3LabelView::widget([
                        'model' => $model,
                        'sysCompanyId' => 1
                    ])
                ]            
            ]       
            ,
        ]);

用于显示、附加和从记录中删除标签的模型部件

<?=\d3yii2\d3labels\widgets\D3LabelList::widget([
        //'title' => Yii::t('d3labels', 'Labels'), //Optional
        'sysCompanyId' => 1,
        'model' => $model,
        'readOnly' => true,
])?>

为D3LabelList部件的控制器动作

    public function actions()
    {
        return [

            'd3labelsattach' => [
                'class' => AttachAction::class,
                'modelName' => CwbrProduct::class,
            ],
            'd3labelsremove' => [
                'class' => DeleteAction::class,
                'modelName' => CwbrProduct::class,
            ],
        ];
    }

用于GridView的D3LabelColumn

用于网格的列

<?php
$columns[] = [
    'class' => d3yii2\d3labels\components\D3LabelColumn::class,                    
    'model' => $searchModel,
    'modelClass' => \cewood\cwstore\models\CwbrProduct::class,
    'attribute'=>'label_type',
    'format'=>'raw',
    'filterNotAssignedLabel' => true,
    'label' => Yii::t('d3labels', 'Labels'),
    'sysCompanyId' => $sysCompanyId
    //'badgeRenderOptions' => ['iconsWithText' => true],
];

在网格视图中进行批量操作

控制器

use d3yii2\d3labels\logic\D3LabelBulk;

    /**
     * @return \d3yii2\d3labels\logic\D3LabelBulk
     * @throws \yii\db\Exception
     */
    private function createLabelBulk(): D3LabelBulk
    {
        return new D3LabelBulk([
            'modelClassName' => MyClASS::class,
            'sysCompanyId' => Yii::$app->SysCmp->getActiveCompanyId(),
            'userId' => Yii::$app->user->id,
            'ignoreLabelsByCode' => [RkInvoice::LABEL_CODE_CLOSED]
        ]);
    }

    public function actionIndex()
    {
            $labelBulk = $this->createLabelBulk();
            return $this->render('index', [
                'dataProvider' => $searchModel->search(),
                'searchModel' => $searchModel,
                'bulkActions' => $labelBulk->list()
            ]);        
    }

    public function actionBulk()
    {
        $request = Yii::$app->request;
        $action = $request->post('action');
        /** @var int[] $selection */
        if (!$selection = $request->post('selection')) {
            return $this->redirect(['index']);
        }

        /**
         * check access for checked rows
         */
        foreach ($selection as $id) {
            $this->findModel($id);
        }
    
        /** Labels */
        $labelBulk = $this->createLabelBulk();
        if ($labelBulk->isBulkAction($action)) {
            if ($cnt = $labelBulk->processBulkAction($action,$selection)) {
                FlashHelper::addSuccess($labelBulk->successMessage($action, $cnt));
            } else {
                FlashHelper::addSuccess($labelBulk->nothingAddedMessage($action));
            }
            return $this->redirect(['index']);
        }
    }

搜索模型

将属性label_type添加到搜索模型

    use d3yii2\d3labels\components\QuerySearch;
    
    ...
    
    public $label_type;
    
    ...
    
    public function rules() {
        return [
            ['label_type','safe']
        ];
    }    
    
    ....
    
    QuerySearch::addFilter($query, $this->label_type, '`inv_invoice`.`id`');

导出到Excel

// create widget object
$labelWidget = new  D3LabelColumn([
    'model' => $searchModel,
    'modelClass' => \cewood\cwstore\models\CwbrProduct::class,
    'attribute'=>'label_type',
    'format'=>'raw',
    'label' => Yii::t('d3labels', 'Labels'),
    'sysCompanyId' => Yii::$app->SysCmp->getActiveCompanyId(),
    'dataProvider' => $dataProvider
]);

// columns
    [
        'header' => 'Labels',
        'value' => static function($model) use ($labelWidget){
            return $labelWidget->renderForExcel($model);
        }
    ]

附加到模型记录

use d3yii2\d3labels\logic\D3Label;

// by record id and labelDefId
$labelDefId = D3lDefinitionDictionary::findByCodeModel($labelCode,InvInvoice::class);
D3Label::attach($model->id,$labelDefId);

// by model and label code
D3Label::attachByModelCode($model,$labelCode);

从模型记录中分离

use d3yii2\d3labels\logic\D3Label;

// by record id and labelDefId
$labelDefId = D3lDefinitionDictionary::findByCodeModel($labelCode, InvInvoice::class);
D3Label::detach($model->id, $labelDefId);

// by model and label code
D3Label::detachByModelCode($model->id, $labelCode);
 echo D3LabelCreate::widget([
                    'modelClass' => RkInvoice::class,
                    'sysCompanyId' => Yii::$app->SysCmp->getActiveCompanyId()
                ]);

获取模型所有标签的列表(id => label)

use d3yii2\d3labels\dictionaries\D3lDefinitionDictionary;

$list = D3lDefinitionDictionary::getList($sysCompanyId, CwStorePack::class)

将注释附加到模型

use d3yii2\d3labels\logic\D3Note;

D3Note::attach($model, $noteContent);

将用户注释附加到模型

use d3yii2\d3labels\logic\D3Note;

D3Note::attach($model, $noteContent, $userId);

从模型中删除注释

use d3yii2\d3labels\logic\D3Note;

D3Note::detach($model);

从模型中删除用户注释

use d3yii2\d3labels\logic\D3Note;

D3Note::detach($model, $userId);

在网格视图列中显示注释

use d3yii2\d3labels\widgets\D3NoteColumn;

$columns[] = [
    'attribute' => 'notes',
    'header' => 'Piezīmes',
    'class' => D3NoteColumn::class,
];

使用自定义类在网格视图列中显示注释

use d3yii2\d3labels\widgets\D3NoteColumn;

$columns[] = [
    'attribute' => 'notes',
    'header' => 'Piezīmes',
    'class' => D3NoteColumn::class,
    'modelClasss' => ExampleModel::class,
];

在部件中显示注释

use d3yii2\d3labels\widgets\D3NoteView

<?= D3NoteView::widget([
        'model' => $model,
        // Optional
        'userId' => $userId,
]) ?>