<commit_before><?php
/**
 * Tatoeba Project, free collaborative creation of multilingual corpuses project
 * Copyright (C) 2009  <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 *
 * @category PHP
 * @package  Tatoeba
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */

/**
 * Model for sentences list.
 *
 * @category SentencesLists
 * @package  Models
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */
class SentencesList extends AppModel
{
    public $belongsTo = array('User');
    public $actsAs = array('ExtendAssociations', 'Containable');
    public $hasAndBelongsToMany = array('Sentence');
    
    /**
     * Returns the sentences lists that the given user can add sentences to.
     *
     * @param int $userId Id of the user.
     *
     * @return array
     */
    public function getUserChoices($userId)
    {
        return $this->find(
            "all", 
            array(
                "conditions" => 
                    array("OR" => array(
                        "SentencesList.user_id" => $userId,
                        "SentencesList.is_public" => 1
                    )
                ),
                'contain' => array()
            )
        );
    }
    
    /**
     * Returns public lists that do not belong to given user.
     *
     * @param int $userId Id of the user.
     *
     * @return array
     */
    public function getPublicListsNotFromUser($userId)
    {
        return $this->find(
            "all", 
            array(
                "conditions" => array(
                    "SentencesList.user_id !=" => $userId,
                    "SentencesList.is_public" => 1
                ),
                'contain' => array(
                    'User' => array(
                        'fields' => array('username')
                    )
                )
            )
        );
    }
    
    /**
     * Returns all the lists that given user cannot edit.
     *
     * @param int $userId Id of the user
     *
     * @return array
     */
    public function getNonEditableListsForUser($userId)
    {
        return $this->find(
            "all",
            array(
                "conditions" => array(
                    "SentencesList.user_id !=" => $userId,
                    "SentencesList.is_public" => 0
                ),
                'contain' => array(
                    'User' => array(
                        'fields' => array('username')
                    )
                )
            )
        );
    }
    
    /**
     * Returns sentences from a list, along with the translations of the sentences 
     * if language is specified.
     *
     * @param int    $listId           Id of the list.
     * @param string $translationsLang Language of the translations.
     * @param string $romanization     Display or not romanizations.
     *
     * @return array
     */
    public function getSentences(
        $listId, $translationsLang = null, $romanization = null
    ) {
        
        $contain = array("Sentence");
        
        if ($translationsLang != null) {
            $contain = array(
                "Sentence" => array( 
                    "Translation" => array( 
                        "fields" => array("id", "text"),
                        "conditions" => array(
                            "Translation.lang" => $translationsLang
                        )
                    ),
                    "fields" => array("text")
                )
            );
            
        }
        
        $list = $this->find(
            "first",
            array(
                "conditions" => array("SentencesList.id" => $listId),
                "contain" => $contain
            )
        );
        
        if ($romanization != null) {
            $sentences = array();
            foreach ($list['Sentence'] as $sentence) {
                $sentence['romanization'] = $this->Sentence->getRomanization(
                    $sentence['text'], $sentence['lang']
                );
                <commit_msg>Fix for sentences lists. The translations were not displayed in correction print version.<commit_after><?php
/**
 * Tatoeba Project, free collaborative creation of multilingual corpuses project
 * Copyright (C) 2009  <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 *
 * @category PHP
 * @package  Tatoeba
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */

/**
 * Model for sentences list.
 *
 * @category SentencesLists
 * @package  Models
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */
class SentencesList extends AppModel
{
    public $belongsTo = array('User');
    public $actsAs = array('ExtendAssociations', 'Containable');
    public $hasAndBelongsToMany = array('Sentence');
    
    /**
     * Returns the sentences lists that the given user can add sentences to.
     *
     * @param int $userId Id of the user.
     *
     * @return array
     */
    public function getUserChoices($userId)
    {
        return $this->find(
            "all", 
            array(
                "conditions" => 
                    array("OR" => array(
                        "SentencesList.user_id" => $userId,
                        "SentencesList.is_public" => 1
                    )
                ),
                'contain' => array()
            )
        );
    }
    
    /**
     * Returns public lists that do not belong to given user.
     *
     * @param int $userId Id of the user.
     *
     * @return array
     */
    public function getPublicListsNotFromUser($userId)
    {
        return $this->find(
            "all", 
            array(
                "conditions" => array(
                    "SentencesList.user_id !=" => $userId,
                    "SentencesList.is_public" => 1
                ),
                'contain' => array(
                    'User' => array(
                        'fields' => array('username')
                    )
                )
            )
        );
    }
    
    /**
     * Returns all the lists that given user cannot edit.
     *
     * @param int $userId Id of the user
     *
     * @return array
     */
    public function getNonEditableListsForUser($userId)
    {
        return $this->find(
            "all",
            array(
                "conditions" => array(
                    "SentencesList.user_id !=" => $userId,
                    "SentencesList.is_public" => 0
                ),
                'contain' => array(
                    'User' => array(
                        'fields' => array('username')
                    )
                )
            )
        );
    }
    
    /**
     * Returns sentences from a list, along with the translations of the sentences 
     * if language is specified.
     *
     * @param int    $listId           Id of the list.
     * @param string $translationsLang Language of the translations.
     * @param string $romanization     Display or not romanizations.
     *
     * @return array
     */
    public function getSentences(
        $listId, $translationsLang = null, $romanization = null
    ) {
        
        $contain = array("Sentence");
        
        if ($translationsLang != null) {
            $contain = array(
                "Sentence" => array(
                    "fields" => array("id", "lang", "text"),
                    "Translation" => array( 
                        "fields" => array("id", "lang", "text"),
                        "conditions" => array(
                            "lang" => $translationsLang
                        )
                    )
                )
            );
        }
        
        $list = $this->find(
            "first",
            array(
                "conditions" => array("SentencesList.id" => $listId),
                "contain" => $contain
            )
        );
        
        if ($romanization != null) {
            $sentences = array();
            foreach ($list['Sentence'] as $sentence) {
                $sentence['romanization'] = $this->Sentence->getRomanization(
                    $sentence['text'], $sentence['lang']
                );
                
{'fraction_non_alphanumeric': 0.10706903679206284, 'fraction_numerical': 0.0018602728400165358, 'mean_word_length': 1.45297845373891, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4712563', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2456, 'n_words': 751}