<commit_before><?php
/**
 * Tatoeba Project, free collaborative creation of multilingual corpuses project
 * Copyright (C) 2010 SIMON   Allan   <<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   SIMON   Allan   <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */

/**
 * Model for join table Tags => sentences.
 *
 * @category Tags
 * @package  Models
 * @author   SIMON   Allan   <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */
class TagsSentences extends AppModel
{
    public $name = 'TagSentences';
    public $useTable = "tags_sentences";
    public $actsAs = array('Containable');


    public $belongsTo = array(
        'User',
        'Sentence',
        'Tag',
        );


    public function beforeSave()
    {
        $tagId = $this->data['TagsSentences']['tag_id'];
        $sentenceId = $this->data['TagsSentences']['sentence_id'];

        $result = $this->find(
            'first',
            array(
                'fields' => 'tag_id',
                'conditions' => array(
                    "tag_id" => $tagId,
                    "sentence_id" => $sentenceId),
                'contain' => array()
            )
        );
        return empty($result);
    }

    public function beforeDelete() {
        $tagId = $this->data['TagsSentences']['tag_id'];
        $sentenceId = $this->data['TagsSentences']['sentence_id'];

        $result = $this->find(
            'first',
            array(
                'fields' => 'user_id',
                'conditions' => array(
                    'sentence_id' => $sentenceId,
                    'tag_id' => $tagId,
                ),
                'contain' => array()
            )
        );
        if (empty($result)) {
            return false;
        }
        $taggerId = $result['TagsSentences']['user_id'];
        if (CurrentUser::canRemoveTagFromSentence($taggerId)) {
            return true;
        }
        return false;
    }


    public function tagSentence($sentenceId, $tagId, $userId)
    {
        $data = array(
            "TagsSentences" => array(
                "user_id" => $userId,
                "tag_id" => $tagId,
                "sentence_id" => $sentenceId,
                "added_time" => date("Y-m-d H:i:s")
            )
        );

        return $this->save($data);

    }


    public function getAllTagsOnSentence($sentenceId)
    {
        return $this->find(
            'all',
            array(
                'fields' => array(
                    'Tag.name',
                    'TagsSentences.user_id',
                    'TagsSentences.tag_id',
                    'TagsSentences.added_time'
                ),
                'conditions' => array(
                    'TagsSentences.sentence_id' => $sentenceId
                ),
                'contain' => array(
                    'Tag'
                )
            )
        );
    }

    public function removeTagFromSentence($tagId,$sentenceId) {
        $this->unBindModel(
            array(
                'belongsTo' => array('User', 'Tag', 'Sentence')
            )
        );
        $this->deleteAll(
            array(
                'tag_id' => $tagId,
                'sentence_id' => $sentenceId
            ),
            // we don't want record to be deleted in cascade, as we only want
            // the relation to be broken
            false 
        );
    }


    /**
     * Get sentences with tag that were tagged more than 2 weeks ago.
     *
     * @param int    $tagId Id of the tag.
     * @param string $lang  Language of the sentences.
     *
     * @return array
     */
    public function getSentencesWithNonNewTag($tagId, $lang)
    {
        $date = date('Y-m-d', strtotime("-2 weeks"));

        $sentenceConditions = array();

        if (!empty($lang)) {
            $sentenceConditions = array('lang' => $lang);
        }

        return $this->find(
            'all',
            array(
                'fields' => array('sentence_id'),
                'conditions' => array(
                    'tag_id' => $tagId,
                    'added_time <' => $date,
                    'text !=' => null
                ),
                'contain' => array(
                    'Sentence' => array(
                        'fields' => array(
							'id', 'text', 'lang', 'correctness'
						),
                        'conditions' => $sentenceConditions
                    )
                ),
                'limit' => 100
            )
        );
    }

}<commit_msg>#385 Moved beforeSave() code into tagSentence()<commit_after><?php
/**
 * Tatoeba Project, free collaborative creation of multilingual corpuses project
 * Copyright (C) 2010 SIMON   Allan   <<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   SIMON   Allan   <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */

/**
 * Model for join table Tags => sentences.
 *
 * @category Tags
 * @package  Models
 * @author   SIMON   Allan   <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
 */
class TagsSentences extends AppModel
{
    public $name = 'TagSentences';
    public $useTable = "tags_sentences";
    public $actsAs = array('Containable');


    public $belongsTo = array(
        'User',
        'Sentence',
        'Tag',
        );


    public function beforeDelete() {
        $tagId = $this->data['TagsSentences']['tag_id'];
        $sentenceId = $this->data['TagsSentences']['sentence_id'];

        $result = $this->find(
            'first',
            array(
                'fields' => 'user_id',
                'conditions' => array(
                    'sentence_id' => $sentenceId,
                    'tag_id' => $tagId,
                ),
                'contain' => array()
            )
        );
        if (empty($result)) {
            return false;
        }
        $taggerId = $result['TagsSentences']['user_id'];
        if (CurrentUser::canRemoveTagFromSentence($taggerId)) {
            return true;
        }
        return false;
    }


    public function tagSentence($sentenceId, $tagId, $userId)
    {
        $isTagged = $this->isSentenceTagged($sentenceId, $tagId);

        if (!$isTagged) {
            $data = array(
                "TagsSentences" => array(
                    "user_id" => $userId,
                    "tag_id" => $tagId,
                    "sentence_id" => $sentenceId,
                    "added_time" => date("Y-m-d H:i:s")
                )
            );

            $this->save($data);

            return true;
        }

        return false;
    }


    public function getAllTagsOnSentence($sentenceId)
    {
        return $this->find(
            'all',
            array(
                'fields' => array(
                    'Tag.name',
                    'TagsSentences.user_id',
                    'TagsSentences.tag_id',
                    'TagsSentences.added_time'
                ),
                'conditions' => array(
                    'TagsSentences.sentence_id' => $sentenceId
                ),
                'contain' => array(
                    'Tag'
                )
            )
        );
    }

    public function removeTagFromSentence($tagId,$sentenceId) {
        $this->unBindModel(
            array(
                'belongsTo' => array('User', 'Tag', 'Sentence')
            )
        );
        $this->deleteAll(
            array(
                'tag_id' => $tagId,
                'sentence_id' => $sentenceId
            ),
            // we don't want record to be deleted in cascade, as we only want
            // the relation to be broken
            false 
        );
    }


    /**
     * Get sentences with tag that were tagged more than 2 weeks ago.
     *
     * @param int    $tagId Id of the tag.
     * @param string $lang  Language of the sentences.
     *
     * @return array
     */
    public function getSentencesWithNonNewTag($tagId, $lang)
    {
        $date = date('Y-m-d', strtotime("-2 weeks"));

        $sentenceConditions = array();

        if (!empty($lang)) {
            $sentenceConditions = array('lang' => $lang);
        }

        return $this->find(
            'all',
            array(
                'fields' => array('sentence_id'),
                'conditions' => array(
                    'tag_id' => $tagId,
                    'added_time <' => $date,
                    'text !=' => null
                ),
                'contain' => array(
                    'Sentence' => array(
                        'fields' => array(
							'id', 'text', 'lang', 'correctness'
						),
                        'conditions' => $sentenceConditions
                    )
                ),
                'limit' => 100
            )
        );
    }


    /**
     * Returns true if a sentence is tagged with given tagId
     *
     * @param int $sentenceId Id of the sentence.
     * @param int $tagId      Id of the tag.
     *
     * @return boolean
     */
    public function isSentenceTagged($sentenceId, $tagId)
    {
        $result = $this->find(
            'first',
            array(
                'fields' => 'tag_id',
                'conditions' => array(
                    "tag_id" => $tagId,
                    "sentence_id" => $sentenceId),
                'contain' => array()
            )
        );

        return !empty($result);
    }
}
{'fraction_non_alphanumeric': 0.12041547936391213, 'fraction_numerical': 0.002298005331372369, 'mean_word_length': 1.4834512668340563, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 106, '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': '2986590', 'n_tokens_mistral': 3153, 'n_tokens_neox': 2913, 'n_words': 790}