<?php

namespace console\models;

use Yii;

/**
 * This is the model class for table "cinema".
 *
 * @property integer $id
 * @property string $codice
 * @property string $ctel
 * @property string $nome
 * @property string $visione
 * @property string $quartiere
 * @property string $indirizzo
 * @property string $regione
 * @property string $provincia
 * @property string $citta
 * @property string $telefono
 * @property integer $bar
 * @property string $serv_dis
 * @property string $url
 * @property string $pren_tel
 * @property string $latitudine
 * @property string $longitudine
 *
 * @property Sala[] $salas
 */
class Cinema extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'cinema';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['codice', 'ctel', 'nome'], 'required'],
            [['bar'], 'string', 'max' => 8],
            [['codice', 'ctel', 'visione', 'telefono', 'serv_dis', 'pren_tel', 'latitudine', 'longitudine'], 'string', 'max' => 32],
            [['nome', 'quartiere', 'regione', 'provincia'], 'string', 'max' => 128],
            [['indirizzo', 'citta', 'url'], 'string', 'max' => 256]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'codice' => 'Codice',
            'ctel' => 'Ctel',
            'nome' => 'Nome',
            'visione' => 'Visione',
            'quartiere' => 'Quartiere',
            'indirizzo' => 'Indirizzo',
            'regione' => 'Regione',
            'provincia' => 'Provincia',
            'citta' => 'Citta',
            'telefono' => 'Telefono',
            'bar' => 'Bar',
            'serv_dis' => 'Serv Dis',
            'url' => 'Url',
            'pren_tel' => 'Pren Tel',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSalas() {
        return $this->hasMany(Sala::className(), ['id_cinema' => 'id']);
    }

    public static function searchForCity($citta) {
        // create ActiveQuery
        $query = self::find();

        $query->where(['citta' => $citta]);

        $dataProvider = new \yii\data\ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10
            ]
        ]);

        return $dataProvider;
    }

    public function getCinemaUrl() {
        if (strstr($this->url, "http://") !== FALSE || strstr($this->url, "https://") !== FALSE) {
            return $this->url;
        } else {
            return "http://" . $this->url;
        }
    }

}
