<?php

/*
 * LimeSurvey
 * Copyright (C) 2007-2026 The LimeSurvey Project Team
 * All rights reserved.
 * License: GNU/GPL License v2 or later, see LICENSE.php
 * LimeSurvey is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 *
 */

/**
 * Class QuestionL10n
 *
 * Localization model for the {{question_l10ns}} table.
 * Stores per-language translations of question text, help text, and scripts.
 *
 * @property int    $id       Primary key (auto-increment)
 * @property int    $qid      Foreign key to the questions table
 * @property string $language Language code (e.g. 'en'). Unique together with $qid
 * @property string $question Question display text (the actual question shown to participants)
 * @property string $help     Question help-text displayed to participants
 * @property string $script   JavaScript to be executed at runtime for this question
 *
 * @see Question
 */
class QuestionL10n extends LSActiveRecord
{
    /** @inheritdoc */
    public function tableName()
    {
        return '{{question_l10ns}}';
    }

    /** @inheritdoc */
    public function primaryKey()
    {
        return 'id';
    }

    /**
     * @inheritdoc
     * @return QuestionL10n
     */
    public static function model($className = __CLASS__)
    {
        /** @var self $model */
        $model = parent::model($className);
        return $model;
    }

    /** @inheritdoc */
    public function relations()
    {
        return array(
            // FIXME this conflicts with the attribute "question"
            //'question' => array(self::BELONGS_TO, 'Question', 'qid'),
        );
    }

    /**
     * This defaultScope indexes the ActiveRecords given back by language
     * Important: This does not work if you want to retrieve records for more than one question at a time.
     * in that case reset disable the defaultScope by using MyModel::model()->resetScope()->findAll();
     * @return array Scope that indexes the records by their language
     */
    public function defaultScope()
    {
        return array('index' => 'language');
    }

    /**
     * @inheritdoc
     * @return array Validation rules for model attributes
     */
    public function rules()
    {
        $rules = array(
            ['qid,language', 'required'],
            ['qid', 'numerical', 'integerOnly' => true],
            array('question', 'LSYii_Validators'),
            array('help', 'LSYii_Validators'),
            array('script', 'safe'),
            array('language', 'length', 'min' => 2, 'max' => 20), // in array languages ?
            /* Add rules for existing unique index : idx1_question_ls ['qid', 'language'] */
            array('qid', 'unique', 'criteria' => array(
                    'condition' => 'language=:language',
                    'params' => array(':language' => $this->language)
                ),
                'message' => sprintf(
                    // Usage of {attribute} need attributeLabels, {value} never exist in message
                    gT("Question ID '%s' is already in use for language '%s'."),
                    $this->qid,
                    $this->language
                ),
            ),
        );
        if (!Yii::app()->user->isScriptUpdateAllowed()) {
            $rules[] = array('script', 'LSYii_NoUpdateValidator');
        }
        return $rules;
    }
}
