Commit c445fe54 authored by andre's avatar andre

#1302

parent cac0c014
<?php
namespace common\components\activeRecordBehaviors;
use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
use \common\models\MetaTags;
use common\modules\languages\models\Languages;
class CaseMetaTagBehavior extends Behavior
{
public $meta;
public $actions = [];
public function events()
{
return [
ActiveRecord::EVENT_INIT => 'eventInit',
ActiveRecord::EVENT_AFTER_FIND => 'eventFind',
ActiveRecord::EVENT_AFTER_UPDATE => 'eventSave',
ActiveRecord::EVENT_AFTER_INSERT => 'eventInsert',
ActiveRecord::EVENT_BEFORE_DELETE => 'eventDelete',
];
}
public function eventInit($event)
{
if(Yii::$app->controller && in_array(Yii::$app->controller->action->id, $this->actions))
{
$langs = Languages::find()->all();
foreach ($langs as $lang)
{
$mt = new MetaTags;
$mt->lang_id = $lang->id;
$this->meta[$lang->id] = $mt;
}
}
}
public function eventFind($event)
{
if(Yii::$app->controller && in_array(Yii::$app->controller->action->id, $this->actions))
{
$langs = Languages::find()->all();
foreach ($langs as $lang)
{
$mt = $this->owner->getMetaTags($lang->id)->one();
if(!$mt)
{
$mt = new MetaTags;
$mt->lang_id = $lang->id;
}
$this->meta[$lang->id] = $mt;
}
}
}
public function eventSave($event)
{
$meta = \Yii::$app->request->post('MetaTags');
if ($meta)
{
foreach ($meta as $lang_id => $attributes)
{
$meta_tag = MetaTags::find()->where([
'object_id' => $this->owner->id,
'model_id' => $this->owner->parentClass,
'lang_id' => $lang_id,
])->one();
if (!$meta_tag)
{
$meta_tag = new MetaTags;
}
$attributes['object_id'] = $this->owner->id;
$attributes['model_id'] = $this->owner->parentClass;
$attributes['lang_id'] = $lang_id;
$meta_tag->setAttributes( $attributes );
if(!$meta_tag->save()) die(print_r($meta_tag->errors));
}
}
return true;
}
public function eventInsert($event)
{
$meta = \Yii::$app->request->post('MetaTags');
if ($meta)
{
foreach ($meta as $lang_id => $attributes)
{
$meta_tag = new MetaTags;
$attributes['object_id'] = $this->owner->id;
$attributes['model_id'] = $this->owner->parentClass;
$attributes['lang_id'] = $lang_id;
$meta_tag->setAttributes($attributes);
$meta_tag->save(false);
}
}
return true;
}
public function eventDelete($event)
{
if($this->owner->metaTags)
{
foreach ($this->owner->metaTags as $meta)
{
$meta->delete();
}
}
return true;
}
}
......@@ -25,6 +25,50 @@ class CaseContent extends CoContent
{
public $parentClass = 'common\modules\content\models\CoContent';
public function behaviors()
{
return [
'meta' => [
'class' => 'common\components\activeRecordBehaviors\CaseMetaTagBehavior',
'actions' => ['create', 'update', 'copy', 'createcase', 'createproject']
],
'langs' => [
'class' => 'common\modules\languages\components\LanguageHelperBehavior',
'field' => 'content_id',
'langClass' => 'common\modules\content\models\CoContentLang',
'actions' => ['create', 'update', 'copy', 'createcase', 'createproject']
],
'file' => [
'class' => 'common\components\activeRecordBehaviors\FileUploadBehavior',
'path' => '@frontend/web',
'folder' => '/uploads/content/',
'field' => 'preview'
],
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => time(),
],
'sitemap' => [
'class' => SitemapBehavior::className(),
'scope' => function ($model) {
/** @var \yii\db\ActiveQuery $model */
$model->select(['url', 'updated_at', 'priority']);
$model->andWhere(['active' => 1]);
},
'dataClosure' => function ($model) {
return [
'loc' => Url::to($model->url),
'lastmod' => date('c', $model->updated_at),
'changefreq' => SitemapBehavior::CHANGEFREQ_DAILY,
'priority' => $model->priority
];
}
],
];
}
public function getPreviewType()
{
return $this->hasOne(CasesPreviewType::className(), ['content_id' => 'id'])->one();
......@@ -71,15 +115,6 @@ class CaseContent extends CoContent
$previewType->attributes = Yii::$app->request->post('CasesPreviewType');
$previewType->content_id = $this->id;
$previewType->save();
$metatags = MetaTags::find()->where(['model_id' => get_class($this)])->all();
foreach ($metatags as $metatag)
{
$metatag->model_id = $this->parentClass;
$metatag->save();
}
}
// public function __construct(array $config)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment