elitedivision/amos-m2m

There is no license information available for the latest version (1.0.0) of this package.

AMOS M2M

1.0.0 2021-05-14 06:58 UTC

README

Extension to easily create M2M widgets within the Amos framework.

How do I get set up?

The preferred way to install this extension is through composer.

Either run:

composer require elitedivision/amos-m2m

or add this row:

"elitedivision/amos-m2m": "dev-master"

to the require section of your composer.json file.

Add module configuration

Add the module to your modules-amos config like this (customize the class parameter if you extend the base class from AmosM2m). Note: a secretKey is required.

'modules' => [
    'm2m' => [
        'class' => 'elitedivision\amos\m2m\AmosM2m',
        'secretKey' => '230fbEl3i7tcGy5iw68F'
    ],
],

Add behavior to the model

public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'm2mBehavior' => [
                'class' => elitedivision\amos\m2m\behaviors\M2mBehavior::className()
            ]
        ]);
    }

Configure one or more m2mWidgets in your view

For the widget to work, you will need to pass the following parameters as the widget configuration:

Parameter Type/Schema Required Description
Generic
idWidget string No Required if you want two different widgets in the same page that refer to the same table_mm.
timeoutPjax integer No Timeout for Pjax requests in milliseconds. Defaults to 900000 ms.
enableLiveSave boolean No Whether inline save is enabled or not. This allows users with the correct permissions to edit association attributes without needing to open another form, but just by editing the content of table cells. Defaults to false.
Cosmetic
classTable string No CSS class to use for the tables. Defaults to grid-view.
enableLoader boolean No Whether a full-page spinner is shown during Pjax or Ajax requests. Defaults to true.
Data
model elitedivision\amos\core\record\Record Yes Base model for the M2M Widget (also called Model From).
modelTo string Yes Class name for the target model (also called Model To).
modelMm string Yes Class name for the M2M Association model.
fromId [string => string] No Model From/M2M Association keys. Each associative item represents a set of Model From => M2M Association primary/foreign keys. If not set, the widget will try to retrieve them automatically by using the first foreign key in the M2M Association linked to the Model From table.
toId [string => string] No Model To/M2M Association keys. Each associative item represents a set of Model To => M2M Association primary/foreign keys. If not set, the widget will try to retrieve them automatically by using the first foreign key in the M2M Association linked to the Model To table.
queryBuilderMm function No This closure will receive one parameter, which is the ActiveQuery built using the Model From and M2M Association. It should return the enhanced ActiveQuery. Use this attribute to extend the original query to include additional filtering or associations.
queryBuilderTo function No This closure will receive one parameter, which is the ActiveQuery built using the Model To and M2M Association. It should return the enhanced ActiveQuery. Use this attribute to extend the original query to include additional filtering or associations.
associationTemplate [string => string] No A default template to use when creating a new M2M Association. The data detailed here will be automatically injected into each new association, unless overwritten from the user.
Permissions
permissionDelete string No RBAC permission name to check whether the current user is authorized to delete associations. If not set, it is generated automatically from the M2M Association class name by appending _DELETE to it. If the user has this permission in the RBAC, a "Delete" button will be shown on each association row.
permissionAssociate string No RBAC permission name to check whether the current user is authorized to create associations. If not set, it is generated automatically from the M2M Association class name by appending _CREATE to it. If the user has this permission in the RBAC, an "Associate" button will be shown on top of the widget, letting the user pick new Model To objects to associate.
permissionModifyAttributes string No RBAC permission name to check whether the current user is authorized to edit association attributes. If not set, permission is always granted.
Search
enableSearchGridMm boolean No Whether search fields in the main association table are enabled or not. Defaults to true.
attributeSearchGridMm [string] or [string => string] No Custom search fields in the search grid related to the M2M Association and its attributes. Can be either an array of strings (where each element will be the field to search against) or an associative array (where key is the label, value is the field to search against).
enableSearchGridTo boolean No Whether search fields in the "Associate" top table are enabled or not. Defaults to true.
attributeSearchGridTo [string] or [string => string] No Custom search fields in the search grid related to the Model To model when creating a new association, after clicking on the "Associate" button. Can be either an array of strings (where each element will be the field to search against) or an associative array (where key is the label, value is the field to search against).
Data Display
defaultInputType [mixed] No Override of data display widgets/HTML elements depending on data type. See the example below and the next section to see the correct format for the array.
blackListAttributes [string] No List of attributes whose columns are hidden in all tables. Fields hidden by default are id, created_by, updated_by, deleted_by, created_at, updated_at, deleted_at. Setting this property will override (not merge) the default.
columnsModelMm [mixed] Unknown List of M2M Association columns to show in the M2M widget. Please refer to the next section to see the correct format for the array.
columnsModelTo [mixed] Unknown List of Model To columns to show in the M2M widget when creating a new association, after clicking on the "Associate" button. Please refer to the next section to see the correct format for the array.

Columns Setup

amos-m2m offers great flexibility and power due to its very customizable colum setup. The Array passed to columnsModelTo and columnsModelMm can be set up as follows:

Example:

\elitedivision\amos\m2m\widgets\base\M2MWidget::widget([
    'idWidget' => 'my-id-widget-m2m',
    'model' => $model,
    'modelTo' => \elitedivision\amos\core\user\User::className(),
    'modelMm' => \elitedivision\amos\projectmanagement\models\ProjectsUserMm::className(),
    'permissionDelete' => 'PROJECTS_DELETE',
    'permissionAssociate' => 'PROJECTS_UPDATE',
    'permissionModifyAttributes' => 'PROJECTS_UPDATE',
    'dataProviderModelTo' => $dataProvider,
    'dataProviderModelMm' => $dataProviderMm,
    'attributeSearchGridTo' => ['Nome' => 'userProfile.nome', 'Cognome' => 'userProfile.cognome'],
    'attributeSearchGridMm' => ['status', 'name'],
    'from_id' => [
        'id' => ['projects_id'],
    ],
    'to_id' => [
        'id' => ['user_id'],
    ],
    'columnsModelTo' => [
        'userProfile.nomeCognome',
        'email',
    ],
    'columnsModelMm' => [
        'user.userProfile.nome',
        'user.userProfile.cognome',
        [
            'attribute' => 'projects_role_id',
            'type' => 'select2',
            'data' => [
                'data' => \yii\helpers\ArrayHelper::map(\elitedivision\amos\projectmanagement\models\ProjectsRoles::find()->all(), 'id', 'name'),
                'options' => ['placeholder' => 'Select ...']
            ],
        ],
       [
            'label' =>  'Funzione 1+1',
            'value' => function(){
                return (1+1);
            }
        ],
        [
            'attribute' => 'organization_id',
            'type' => 'select2',
            'data' => [
                'data' => \yii\helpers\ArrayHelper::map(\openinnovation\organizations\models\Organizations::find()->all(), 'id', 'name'),
                'options' => ['placeholder' => 'Select ...']
            ],
        ],
        [
            'attribute' => 'status',
              'data' => [
                  'readonly' => true
        ]
        ],
        [
            'class' => \elitedivision\amos\core\views\grid\ActionColumn::className(),
            'template' => '{update}{delete}',
            'buttons' => [
                'update' => function($url, $model){
                    if(\Yii::$app->user->can('PROJECTS_UPDATE')) {
                        return Html::a(\elitedivision\amos\core\icons\AmosIcons::show('edit'), ['modify-user-roles', 'id' => $model->id], [
                            'class' => 'btn btn-tools-secondary',
                        ]);
                    }
                },
            ]
        ]
    ],
]);