Initial import
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
|
||||
/**
|
||||
* Class Articles
|
||||
* @package app\models
|
||||
* @property $id
|
||||
* @property $key
|
||||
* @property $art_table
|
||||
* @property News $news;
|
||||
* @property Events $events;
|
||||
* @property Campaigns $campaigns;
|
||||
* @property $title
|
||||
* @property $title_en
|
||||
* @property $is_active
|
||||
* @property $publish_date
|
||||
*/
|
||||
class Articles extends _Base
|
||||
{
|
||||
public function getNews()
|
||||
{
|
||||
return $this->hasOne(News::class, ['article_key' => 'key']);
|
||||
}
|
||||
|
||||
public function getEvents()
|
||||
{
|
||||
return $this->hasOne(Events::class, ['article_key' => 'key']);
|
||||
}
|
||||
|
||||
public function getCampaigns()
|
||||
{
|
||||
return $this->hasOne(Campaigns::class, ['article_key' => 'key']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param News | Events | Campaigns | \yii\db\ActiveRecord $model
|
||||
*/
|
||||
public static function updateArticle($model)
|
||||
{
|
||||
$articleUpdate = [
|
||||
'title' => 'title',
|
||||
'title_en' => 'ts_en_title',
|
||||
'is_active' => 'is_active',
|
||||
'publish_date' => 'publish_date',
|
||||
'partner_id' => 'partner_id',
|
||||
'on_partner_page' => 'on_partner_page',
|
||||
'partner_page_selected' => 'partner_page_selected'
|
||||
];
|
||||
|
||||
|
||||
if (empty($model->article_key)) {
|
||||
$article = new Articles();
|
||||
switch (true) {
|
||||
case $model instanceof News:
|
||||
$article->key = "news_$model->id";
|
||||
$article->art_table = "news";
|
||||
break;
|
||||
case $model instanceof Events:
|
||||
$article->key = "events_$model->id";
|
||||
$article->art_table = "events";
|
||||
break;
|
||||
case $model instanceof Campaigns:
|
||||
$article->key = "campaigns_$model->id";
|
||||
$article->art_table = "campaigns";
|
||||
break;
|
||||
}
|
||||
foreach ($articleUpdate as $a => $m) $article->{$a} = $model->{$m};
|
||||
$article->save();
|
||||
$model->article_key = $article->key;
|
||||
$model->save();
|
||||
} else {
|
||||
$article = Articles::find()->where(['key' => $model->article_key])->one();
|
||||
if ($article) {
|
||||
foreach ($articleUpdate as $a => $m) $article->{$a} = $model->{$m};
|
||||
$article->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user