Page.php 3.17 KB
<?php

namespace common\models;

use Yii;
use common\models\PageLang;

/**
 * This is the model class for table "page".
 *
 * @property integer $page_id
 * @property string $date_add
 * @property integer $template_id
 * @property integer $image_id
 * @property integer $show
 */
class Page extends \yii\db\ActiveRecord
{
    var $data;

    //public $title;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'page';
    }
    
    // ==== DATA PAGE LANG  ====
    
    public function getData()
    {
        $this->data = PageLang::find()->where(['page_id' => $this->page_id, 'lang_id' => yii::$app->lang_id])->one();
    }
    
    public function getDataByKey($key)
    {
        if (! $this->data)
        {
            $this->getData();
        }

        return isset ($this->data[$key]) ? $this->data[$key] : '';
    }
    
    // ==== DATA PAGE LANG FIELD ====
 
    public function getTitle()
    {
        return $this->getDataByKey('title');
    }

    public function getMeta_title()
    { 
        return $this->getDataByKey('meta_title');
    } 

    public function getMeta_description()
    {
        return $this->getDataByKey('meta_description');
    }

    public function getText()
    {
        return $this->getDataByKey('text');
    }

    public function getPage_alias()
    {
        return $this->getDataByKey('page_alias');
    }

    // ==== PAGE LANG FILTER FIELD ====
    
    public function findPageLangField($post)
    {
        if (! $this->data)
        {
            $this->getData();
        }
        
        if (empty ($this->data) || empty ($post))
        {
            return false;
        }
        
        $result = array ();
        
        foreach ($post as $key1 => $row1)
        {
            foreach ($this->data as $key2 => $row2)
            {
                if ($key1 == $key2)
                {
                    $result[$key1] = $row1;
                }
            } 
        }
        
        return $result;
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['date_add', 'template_id', 'image_id', 'show'], 'required'],
            [['date_add'], 'safe'],
            [['template_id', 'image_id', 'show'], 'integer']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'page_id'       => Yii::t('field', 'page'),
            'date_add'      => Yii::t('field', 'date_add'),
            'template_id'   => Yii::t('field', 'template'),
            'image_id'      => Yii::t('field', 'image'),
            'show'          => Yii::t('field', 'show'),
                 
            'title'             => Yii::t('field', 'title'),
            'meta_title'        => Yii::t('field', 'meta_title'),
            'meta_description'  => Yii::t('field', 'meta_description'),
            'text'              => Yii::t('field', 'text'),
            'page_alias'        => Yii::t('field', 'page_alias'),
            'lang_id'           => Yii::t('field', 'lang_id'),
        ];
    }
/*    
    public function getPage()
    {
        return $this->hasMany(PageLang::className(), ['page_id' => 'page_id']);
    }
*/
}