PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Uldis Nelsons   D3 System   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Read me
Class: D3 System
Framework to edit model data using CRUD interfaces
Author: By
Last change:
Date: 3 years ago
Size: 3,919 bytes
 

Contents

Class file image Download

d3system

Installation

composer require d3yii2/d3system dev-master

Configuration

add translation

$config = [
   'components' => [
        'i18n' => [
            'translations' => [ 
                'd3system*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@d3yii2/d3system/messages',
                    'sourceLanguage' => 'en-US',
                ],
                'crud' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@d3yii2/d3system/messages',
                    'sourceLanguage' => 'en-US',
                ],
            ]
        ]
    ]
];

Components

ModelsList

Configuration:

 'components' => [
        'ModelsList' => [
            'class' => 'd3system\compnents\ModelsList',
            'cacheKey' => 'd3system\modeList',
            'cacheDuration' => 3600
        ]    
        

Usage:

  $modelId = \Yii::$app->ModelsList->getId($model);

Date & Time conversions

Dependency https://github.com/d3yii2/yii2-datetime

Conversion works only for the model attributes suffixed vith "_local" A example, instead

$model->YOUR_ATTRIBUTE

use

$model->YOUR_ATTRIBUTE_local

Add behavior config in model

Add the behavior to your model and list the attributes need to be converted Important: do NOT add the "_local" suffix here!

public function behaviors(): array
{
    return D3DateTimeBehavior::getConfig(['YOUR_ATTRIBUTE']);
}

Or if You need custom options (see the https://github.com/d3yii2/yii2-datetime)

public function behaviors()
{
    return [
        'datetime' => [
            'class' => D3DateTimeBehavior::className(), // Our behavior
            'attributes' => [
                'YOUR_ATTRIBUTE', // List all editable date/time attributes
            ],
            // Date formats or other options
           'originalFormat' => ['datetime', 'yyyy-MM-dd HH:mm:ss'],
           'targetFormat' => 'date',
        ]
    ];
}

Display value in view

<?= $model->YOUR_ATTRIBUTE_local ?>

Assign the value before save

$model->load(Yii::$app->request->post());

or

 $model->YOUR_ATTRIBUTE_local = $value;

or

 $model->setAttribute('YOUR_ATTRIBUTE_local', $value);

By multiple assignment via load() ensure the local attributes have 'safe' rules:

// Virtual params for DateTimeBehavior
public function rules(): array
{   
    return [
        [...],
        [['YOUR_ATTRIBUTE_local'], 'safe'],
    ];
}

D3EditableAction Initial Setup in Controller

editAbleFields: must match real attributes
editAbleFieldsForbbiden: must match real attributes
modelName: pass current controller model Name with full Namespace
/
 * @var array
 */
public $editAbleFields = ['hasEditable', 'status'];

/
 * @var array
 */
public $editAbleFieldsForbbiden = [];

Actions

public function actions()
{
    return [
        'editable'      => [
            'class'                   => D3EditableAction::class,
             'modelName'               => AudAuditor::class,
             'editAbleFields'          => ['status','notes'],
             'editAbleFieldsForbbiden' => $this->editAbleFieldsForbbiden,
             'preProcess' => static function (Inout $model) {
                  if ($model->isAttributeChanged('driver')) {
                     $model->driver = iconv('UTF-8', 'ASCII//TRANSLIT',$model->driver);
                  }   
             },
             'outPreProcess' => static function (ContInout $model, array $output) {
                 if (isset($output['ediBookingId'])) {
                     $output['ediBookingId'] = DepoEdiBookingDictionary::getIdLabel($output['ediBookingId']);
                 }
                 return $output;
             }             
        ],
    ];
}