<commit_before><?php
/**
    Tatoeba Project, free collaborativ creation of languages 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 Class which represent the wall and messages posted on
 *
 * @category PHP
 * @package  Tatoeba
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
*/

class Wall extends AppModel
{
    public $name = 'Wall';
    public $useTable = 'wall';
    public $actsAs = array('Tree','Containable');

    public $belongsTo = array(
        'User' => array(
            'className'  => 'User',
            'foreignKey' => 'owner'
        )
    );

    public $hasOne = array(
        'WallThread' => array(
            'className' => 'WallThread',
             'dependent' => true,
             'foreignKey' => 'id'
        )
    );

    /**
     * used after a save is made in the database
     *
     * @param bool $created if succeed or not TODO: to check
     * 
     * @return void
     */ 
     
    public function afterSave($created)
    {
        if (isset($this->data['Wall']['content'])) {
            $data['newMessage']['owner'] = $this->data['Wall']['owner'] ;
            $data['newMessage']['date'] = date("Y-m-d H:i:s");
        }

    }

    /**
     * Get all the message which start a thread
     *
     * @return array of all messages which start a thread
     */

    public function getFirstMessages()
    {
        return  $this->find(
            'all',
            array(
                "order" => "Wall.date DESC", 
                "conditions" => array ("Wall.parent_id" => 0),
                "contain"    => array (
                    "User" => array (
                        "fields" => array(
                            "User.image",
                            "User.username",
                            "User.id"
                        ) 
                    )
                ) 
            )
        );
    }

    /**
     * get all Messages
     *
     * @return array of all messages
     */

    public function getMessages()
    {
        return $this->find(
            'all',
            array(
                "order" => "Wall.id", 
                "contain"    => array (
                    "User" => array (
                        "fields" => array("User.image","User.username", "User.id") 
                        )
                    ) 
            )
        );
    }

    /**
     * retrieve all the thread of the given root message
     *
     * @param array $rootMessages Array of array with 'lft' and 'rght' field
     *                            take a look at how to store hierarchical data
     *                            with mysql for more informations
     *
     * @return array A nested array of array of array ... nested in a logical way
     *                children are nested in their parent etc...
     */

    public function getMessagesThreaded($rootMessages)
    {
        if (empty($rootMessages)) {
            return array();
        }
        
        // generate the condition array as it's a bit complicated
        $orArray = array();
        foreach ($rootMessages as $rootMessage) {
            $lftRghtArray = array($rootMessage['lft'], $rootMessage['rght']);
            $betweenArray = array (
                'Wall.lft BETWEEN ? AND ?' => $lftRghtArray
            );

            array_push($orArray, $betweenArray);
        }
        // execute the request
        $result = $this->find(
            'threaded',
            array(
                "order" => "WallThread.last_message_date DESC", 
                "conditions" => array(
                    'OR' => $orArray 
                ),
                "contain" => array (
                    "User" => array (
                        "fields" => array(
                            "User.image",
                            "User.username",
                            "User.id"
                        )
                    ),
                    "WallThread" => array(
                        'fields' => "last_message_date"
                    ) 
                ) 
            )
        );

        return $result;

    }
    /**
     * get the X last messages posted
     *
     * @param int $numberOfLastMessages number of messages wanted
     *
     * @return array of these messages
     */

    public function getLastMessages($numberOfLastMessages)
    {
        return $this->find(
            'all',
            array(
                "order" => "Wall.date DESC",
                "limit" => $numberOfLastMessages,  
                "contain"    => array (
                    "User" => array (
                        "fields" => array(
                            "User.username",
                            "User.id"
                        ) 
                    )
                ) <commit_msg>re #373. Filter out hidden wall messages so they don't show up on the home page<commit_after><?php
/**
    Tatoeba Project, free collaborativ creation of languages 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 Class which represent the wall and messages posted on
 *
 * @category PHP
 * @package  Tatoeba
 * @author   <NAME> <<EMAIL>>
 * @license  Affero General Public License
 * @link     http://tatoeba.org
*/

class Wall extends AppModel
{
    public $name = 'Wall';
    public $useTable = 'wall';
    public $actsAs = array('Tree','Containable');

    public $belongsTo = array(
        'User' => array(
            'className'  => 'User',
            'foreignKey' => 'owner'
        )
    );

    public $hasOne = array(
        'WallThread' => array(
            'className' => 'WallThread',
             'dependent' => true,
             'foreignKey' => 'id'
        )
    );

    /**
     * used after a save is made in the database
     *
     * @param bool $created if succeed or not TODO: to check
     * 
     * @return void
     */ 
     
    public function afterSave($created)
    {
        if (isset($this->data['Wall']['content'])) {
            $data['newMessage']['owner'] = $this->data['Wall']['owner'] ;
            $data['newMessage']['date'] = date("Y-m-d H:i:s");
        }

    }

    /**
     * Get all the message which start a thread
     *
     * @return array of all messages which start a thread
     */

    public function getFirstMessages()
    {
        return  $this->find(
            'all',
            array(
                "order" => "Wall.date DESC", 
                "conditions" => array ("Wall.parent_id" => 0),
                "contain"    => array (
                    "User" => array (
                        "fields" => array(
                            "User.image",
                            "User.username",
                            "User.id"
                        ) 
                    )
                ) 
            )
        );
    }

    /**
     * get all Messages
     *
     * @return array of all messages
     */

    public function getMessages()
    {
        return $this->find(
            'all',
            array(
                "order" => "Wall.id", 
                "contain"    => array (
                    "User" => array (
                        "fields" => array("User.image","User.username", "User.id") 
                        )
                    ) 
            )
        );
    }

    /**
     * retrieve all the thread of the given root message
     *
     * @param array $rootMessages Array of array with 'lft' and 'rght' field
     *                            take a look at how to store hierarchical data
     *                            with mysql for more informations
     *
     * @return array A nested array of array of array ... nested in a logical way
     *                children are nested in their parent etc...
     */

    public function getMessagesThreaded($rootMessages)
    {
        if (empty($rootMessages)) {
            return array();
        }
        
        // generate the condition array as it's a bit complicated
        $orArray = array();
        foreach ($rootMessages as $rootMessage) {
            $lftRghtArray = array($rootMessage['lft'], $rootMessage['rght']);
            $betweenArray = array (
                'Wall.lft BETWEEN ? AND ?' => $lftRghtArray
            );

            array_push($orArray, $betweenArray);
        }
        // execute the request
        $result = $this->find(
            'threaded',
            array(
                "order" => "WallThread.last_message_date DESC", 
                "conditions" => array(
                    'OR' => $orArray 
                ),
                "contain" => array (
                    "User" => array (
                        "fields" => array(
                            "User.image",
                            "User.username",
                            "User.id"
                        )
                    ),
                    "WallThread" => array(
                        'fields' => "last_message_date"
                    ) 
                ) 
            )
        );

        return $result;

    }
    /**
     * get the X last messages posted
     *
     * @param int $numberOfLastMessages number of messages wanted
     *
     * @return array of these messages
     */

    public function getLastMessages($numberOfLastMessages)
    {
        return $this->find(
            'all',
            array(
                "order" => "Wall.date DESC",
                "limit" => $numberOfLastMessages,  
                "conditions" => array("hidden" => 0),
                "contain"    => array (
                    "User" => array (
                        "fields" => array(
                            "User.username",
                            "User.id"
                        ) 
                    )
                ) 
{'fraction_non_alphanumeric': 0.10685169124024284, 'fraction_numerical': 0.0015611448395490026, 'mean_word_length': 1.3071228491396558, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3993874', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2878, 'n_words': 900}