<?php

/**
 * This is the model class for table "googlemaps_map".
 *
 * The followings are the available columns in table 'googlemaps_map':
 * @property integer $id
 * @property string $background_color
 * @property string $title
 * @property double $center_latitude
 * @property double $center_longitude
 * @property integer $disable_double_click_zoom
 * @property integer $draggable
 * @property integer $maptype_control
 * @property string $maptype_control_ids
 * @property string $maptype_control_style
 * @property string $maptype_control_position
 * @property string $maptype_id
 * @property integer $overview_control
 * @property integer $overview_control_opened
 * @property integer $pan_control
 * @property string $pan_control_position
 * @property integer $rotate_control
 * @property string $rotate_control_position
 * @property integer $scale_control
 * @property string $scale_control_position
 * @property integer $scrollwheel
 * @property integer $zoom
 * @property integer $zoom_control
 * @property string $zoom_control_position
 * @property string $zoom_control_style
 * @property string $width
 * @property string $height
 * @property string $html_id
 * @property string $html_class
 * @property integer $fit_markers_into_view
 *
 * The followings are the available model relations:
 * @property GoogleMapMarker[] $markers
 */
class GoogleMap extends CActiveRecord {

    // type
    const TYPE_HYBRID = 'HYBRID';
    const TYPE_ROADMAP = 'ROADMAP';
    const TYPE_SATELLITE = 'SATELLITE';
    const TYPE_TERRAIN = 'TERRAIN';
    // style
    const STYLE_DEFAULT = 'DEFAULT';
    const STYLE_DROPDOWN_MENU = 'DROPDOWN_MENU';
    const STYLE_HORIZONTAL_BAR = 'HORIZONTAL_BAR';
    // position
    const POSITION_BOTTOM_CENTER = 'BOTTOM_CENTER';
    const POSITION_BOTTOM_LEFT = 'BOTTOM_LEFT';
    const POSITION_BOTTOM_RIGHT = 'BOTTOM_RIGHT';
    const POSITION_LEFT_BOTTOM = 'LEFT_BOTTOM';
    const POSITION_LEFT_CENTER = 'LEFT_CENTER';
    const POSITION_LEFT_TOP = 'LEFT_TOP';
    const POSITION_RIGHT_BOTTOM = 'RIGHT_BOTTOM';
    const POSITION_RIGHT_CENTER = 'RIGHT_CENTER';
    const POSITION_RIGHT_TOP = 'RIGHT_TOP';
    const POSITION_TOP_CENTER = 'TOP_CENTER';
    const POSITION_TOP_LEFT = 'TOP_LEFT';
    const POSITION_TOP_RIGHT = 'TOP_RIGHT';
    // zoom control style
    const ZOOMCONTROL_STYLE_DEFAULT = 'DEFAULT';
    const ZOOMCONTROL_STYLE_LARGE = 'LARGE';
    const ZOOMCONTROL_STYLE_SMALL = 'SMALL';

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'googlemaps_map';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('center_latitude, center_longitude, width, height, fit_markers_into_view', 'required'),
            array('disable_double_click_zoom, draggable, maptype_control, overview_control, overview_control_opened, pan_control, rotate_control, scale_control, scrollwheel, zoom, zoom_control, fit_markers_into_view', 'numerical', 'integerOnly' => true),
            array('center_latitude, center_longitude', 'numerical'),
            array('title', 'length', 'max' => 100),
            array('background_color, zoom_control_style', 'length', 'max' => 7),
            array('maptype_control_ids', 'length', 'max' => 35),
            array('maptype_control_style, maptype_control_position, pan_control_position, rotate_control_position, scale_control_position, zoom_control_position', 'length', 'max' => 15),
            array('maptype_id', 'length', 'max' => 10),
            array('width, height', 'length', 'max' => 20),
            array('html_id, html_class', 'length', 'max' => 45),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, title,background_color, center_latitude, center_longitude, disable_double_click_zoom, draggable, maptype_control, maptype_control_ids, maptype_control_style, maptype_control_position, maptype_id, overview_control, overview_control_opened, pan_control, pan_control_position, rotate_control, rotate_control_position, scale_control, scale_control_position, scrollwheel, zoom, zoom_control, zoom_control_position, zoom_control_style, width, height, html_id, html_class, fit_markers_into_view', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'markers' => array(self::HAS_MANY, 'GoogleMapMarker', 'map_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => Yii::t('admin', 'ID'),
            'title' => Yii::t('admin', 'Title'),
            'background_color' => Yii::t('admin', 'Background'),
            'center_latitude' => Yii::t('admin', 'Latitude'),
            'center_longitude' => Yii::t('admin', 'Longitude'),
            'disable_double_click_zoom' => Yii::t('admin', 'Disable Double Click Zoom'),
            'draggable' => Yii::t('admin', 'Draggable'),
            'maptype_control' => Yii::t('admin', 'Maptype Control'),
            'maptype_control_ids' => Yii::t('admin', 'Types'),
            'maptype_control_style' => Yii::t('admin', 'Style'),
            'maptype_control_position' => Yii::t('admin', 'Position'),
            'maptype_id' => Yii::t('admin', 'Maptype'),
            'overview_control' => Yii::t('admin', 'Overview Control'),
            'overview_control_opened' => Yii::t('admin', 'Opened'),
            'pan_control' => Yii::t('admin', 'Pan Control'),
            'pan_control_position' => Yii::t('admin', 'Position'),
            'rotate_control' => Yii::t('admin', 'Rotate Control'),
            'rotate_control_position' => Yii::t('admin', 'Position'),
            'scale_control' => Yii::t('admin', 'Scale Control'),
            'scale_control_position' => Yii::t('admin', 'Position'),
            'scrollwheel' => Yii::t('admin', 'Scroll Wheel'),
            'zoom' => Yii::t('admin', 'Zoom'),
            'zoom_control' => Yii::t('admin', 'Zoom Control'),
            'zoom_control_position' => Yii::t('admin', 'Position'),
            'zoom_control_style' => Yii::t('admin', 'Style'),
            'width' => Yii::t('admin', 'Width'),
            'height' => Yii::t('admin', 'Height'),
            'html_id' => Yii::t('admin', 'Html ID'),
            'html_class' => Yii::t('admin', 'Html Class'),
            'fit_markers_into_view' => Yii::t('admin', 'Fit Markers Into View'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('background_color', $this->background_color, true);
        $criteria->compare('center_latitude', $this->center_latitude);
        $criteria->compare('center_longitude', $this->center_longitude);
        $criteria->compare('disable_double_click_zoom', $this->disable_double_click_zoom);
        $criteria->compare('draggable', $this->draggable);
        $criteria->compare('maptype_control', $this->maptype_control);
        $criteria->compare('maptype_control_ids', $this->maptype_control_ids, true);
        $criteria->compare('maptype_control_style', $this->maptype_control_style, true);
        $criteria->compare('maptype_control_position', $this->maptype_control_position, true);
        $criteria->compare('maptype_id', $this->maptype_id, true);
        $criteria->compare('overview_control', $this->overview_control);
        $criteria->compare('overview_control_opened', $this->overview_control_opened);
        $criteria->compare('pan_control', $this->pan_control);
        $criteria->compare('pan_control_position', $this->pan_control_position, true);
        $criteria->compare('rotate_control', $this->rotate_control);
        $criteria->compare('rotate_control_position', $this->rotate_control_position, true);
        $criteria->compare('scale_control', $this->scale_control);
        $criteria->compare('scale_control_position', $this->scale_control_position, true);
        $criteria->compare('scrollwheel', $this->scrollwheel);
        $criteria->compare('zoom', $this->zoom);
        $criteria->compare('zoom_control', $this->zoom_control);
        $criteria->compare('zoom_control_position', $this->zoom_control_position, true);
        $criteria->compare('zoom_control_style', $this->zoom_control_style, true);
        $criteria->compare('width', $this->width, true);
        $criteria->compare('height', $this->height, true);
        $criteria->compare('html_id', $this->html_id, true);
        $criteria->compare('html_class', $this->html_class, true);
        $criteria->compare('fit_markers_into_view', $this->fit_markers_into_view);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return GoogleMap the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function getMaptypeControlIds() {
        if (!is_array($this->maptype_control_ids)) {
            $this->maptype_control_ids = explode(',', $this->maptype_control_ids);
        }
        return $this->maptype_control_ids;
    }

    protected function beforeValidate() {
        if (is_array($this->maptype_control_ids)) {
            $this->maptype_control_ids = implode(',', $this->maptype_control_ids);
        }

        return parent::beforeValidate();
    }

    public static function getTypes() {
        return array(
            self::TYPE_HYBRID => Yii::t('admin', 'Hybrid'),
            self::TYPE_ROADMAP => Yii::t('admin', 'Road Map'),
            self::TYPE_SATELLITE => Yii::t('admin', 'Satellite'),
            self::TYPE_TERRAIN => Yii::t('admin', 'Terrain'),
        );
    }

    public static function getStyles() {
        return array(
            self::STYLE_DEFAULT => Yii::t('admin', 'Default'),
            self::STYLE_DROPDOWN_MENU => Yii::t('admin', 'Dropdown Menu'),
            self::STYLE_HORIZONTAL_BAR => Yii::t('admin', 'Horizontal Bar'),
        );
    }

    public static function getPositions() {
        return array(
            self::POSITION_BOTTOM_CENTER => Yii::t('admin', 'Bottom Center'),
            self::POSITION_BOTTOM_LEFT => Yii::t('admin', 'Bottom Left'),
            self::POSITION_BOTTOM_RIGHT => Yii::t('admin', 'Bottom Right'),
            self::POSITION_LEFT_BOTTOM => Yii::t('admin', 'Left Bottom'),
            self::POSITION_LEFT_CENTER => Yii::t('admin', 'Left Center'),
            self::POSITION_LEFT_TOP => Yii::t('admin', 'Left Top'),
            self::POSITION_RIGHT_BOTTOM => Yii::t('admin', 'Right Bottom'),
            self::POSITION_RIGHT_CENTER => Yii::t('admin', 'Right Center'),
            self::POSITION_RIGHT_TOP => Yii::t('admin', 'Right Top'),
            self::POSITION_TOP_CENTER => Yii::t('admin', 'Top Center'),
            self::POSITION_TOP_LEFT => Yii::t('admin', 'Top Left'),
            self::POSITION_TOP_RIGHT => Yii::t('admin', 'Top Right'),
        );
    }

    public static function getZoomControlStyles() {
        return array(
            self::ZOOMCONTROL_STYLE_DEFAULT => Yii::t("admin", 'Default'),
            self::ZOOMCONTROL_STYLE_LARGE => Yii::t("admin", 'Large'),
            self::ZOOMCONTROL_STYLE_SMALL => Yii::t("admin", 'Small'),
        );
    }

    public function getOptions() {
        $options = array(
            'backgroundColor' => $this->background_color,
            'center' => new CJavaScriptExpression("new google.maps.LatLng({$this->center_latitude}, {$this->center_longitude})"),
            'disableDoubleClickZoom' => new CJavaScriptExpression($this->disable_double_click_zoom),
            'draggable' => (boolean) $this->draggable,
            'mapTypeControl' => (boolean) $this->maptype_control,
            'mapTypeControlOptions' => $this->maptype_control ? array(
                'mapTypeIds' => array_map(create_function('$a', 'return new CJavaScriptExpression("google.maps.MapTypeId.{$a}");'), $this->getMaptypeControlIds()),
                'position' => new CJavaScriptExpression("google.maps.ControlPosition.{$this->maptype_control_position}"),
                'style' => new CJavaScriptExpression("google.maps.MapTypeControlStyle.{$this->maptype_control_style}"),
                    ) : array(),
            'mapTypeId' => new CJavaScriptExpression("google.maps.MapTypeId.{$this->maptype_id}"),
            'overviewMapControl' => (boolean) $this->overview_control,
            'overviewMapControlOptions' => $this->overview_control ? array(
                'position' => new CJavaScriptExpression($this->overview_control_opened),
                    ) : array(),
            'panControl' => (boolean) $this->pan_control,
            'panControlOptions' => $this->pan_control ? array(
                'position' => new CJavaScriptExpression("google.maps.ControlPosition.{$this->pan_control_position}"),
                    ) : array(),
            'rotateControl' => (boolean) $this->rotate_control,
            'rotateControlOptions' => $this->rotate_control ? array(
                'position' => new CJavaScriptExpression("google.maps.ControlPosition.{$this->rotate_control_position}"),
                    ) : array(),
            'scaleControl' => (boolean) $this->scale_control,
            'scaleControlOptions' => $this->scale_control ? array(
                'position' => new CJavaScriptExpression("google.maps.ControlPosition.{$this->scale_control_position}"),
                    ) : array(),
            'scrollwheel' => (boolean) $this->scrollwheel,
            'zoom' => new CJavaScriptExpression($this->zoom),
            'zoomControl' => (boolean) $this->zoom_control,
            'zoomControlOptions' => $this->zoom_control ? array(
                'position' => new CJavaScriptExpression("google.maps.ControlPosition.{$this->zoom_control_position}"),
                'style' => new CJavaScriptExpression("google.maps.ZoomControlStyle.{$this->zoom_control_style}"),
                    ) : array(),
        );


        return CJavaScript::encode($options);
    }

}
