You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					46 lines
				
				1.3 KiB
			
		
		
			
		
	
	
					46 lines
				
				1.3 KiB
			| 
								 
											8 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace core\behaviors;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use core\entities\Meta;
							 | 
						||
| 
								 | 
							
								use yii\base\Behavior;
							 | 
						||
| 
								 | 
							
								use yii\base\Event;
							 | 
						||
| 
								 | 
							
								use yii\db\ActiveRecord;
							 | 
						||
| 
								 | 
							
								use yii\helpers\ArrayHelper;
							 | 
						||
| 
								 | 
							
								use yii\helpers\Json;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class MetaBehavior extends Behavior
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    public $attribute = 'meta';
							 | 
						||
| 
								 | 
							
								    public $jsonAttribute = 'meta_json';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function events(): array
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        return [
							 | 
						||
| 
								 | 
							
								            ActiveRecord::EVENT_AFTER_FIND => 'onAfterFind',
							 | 
						||
| 
								 | 
							
								            ActiveRecord::EVENT_BEFORE_INSERT => 'onBeforeSave',
							 | 
						||
| 
								 | 
							
								            ActiveRecord::EVENT_BEFORE_UPDATE => 'onBeforeSave',
							 | 
						||
| 
								 | 
							
								        ];
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function onAfterFind(Event $event): void
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $model = $event->sender;
							 | 
						||
| 
								 | 
							
								        $meta = Json::decode($model->getAttribute($this->jsonAttribute));
							 | 
						||
| 
								 | 
							
								        $model->{$this->attribute} = new Meta(
							 | 
						||
| 
								 | 
							
								            ArrayHelper::getValue($meta, 'title'),
							 | 
						||
| 
								 | 
							
								            ArrayHelper::getValue($meta, 'description'),
							 | 
						||
| 
								 | 
							
								            ArrayHelper::getValue($meta, 'keywords')
							 | 
						||
| 
								 | 
							
								        );
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public function onBeforeSave(Event $event): void
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $model = $event->sender;
							 | 
						||
| 
								 | 
							
								        $model->setAttribute('meta_json', Json::encode([
							 | 
						||
| 
								 | 
							
								            'title' => $model->{$this->attribute}->title,
							 | 
						||
| 
								 | 
							
								            'description' => $model->{$this->attribute}->description,
							 | 
						||
| 
								 | 
							
								            'keywords' => $model->{$this->attribute}->keywords,
							 | 
						||
| 
								 | 
							
								        ]));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |