<commit_before><?php
/********************************************************************************
*                                                                               *
*   Copyright 2012 <NAME> (<EMAIL>)                      *
*   http://www.elabftw.net/                                                     *
*                                                                               *
********************************************************************************/

/********************************************************************************
*  This file is part of eLabFTW.                                                *
*                                                                               *
*    eLabFTW 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.                        *
*                                                                               *
*    eLabFTW 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 eLabFTW.  If not, see <http://www.gnu.org/licenses/>.   *
*                                                                               *
********************************************************************************/
namespace elabftw\elabftw;

class MakePdf {
    private $id, $type, $data;

    public $author;
    public $title;
    public $cleanTitle;
    public $tags;
    public $content;


    public function __construct($id, $type)
    {
        $this->id = $id;
        $this->type = $type;

        $this->initData();
        $this->setAuthor();
        $this->setCleanTitle();
        $this->setTags();
        $this->buildContent();
    }

    public function getPath()
    {
        return ELAB_ROOT . 'uploads/' . $this->cleanTitle . '.pdf';
    }

    public function getFileName()
    {
        return $this->cleanTitle . '.pdf';
    }

    private function initData()
    {
        global $pdo;

        // title, date, body, elabid, userid, lock
        $sql = "SELECT * FROM " . $this->type . " WHERE id = " . $this->id;
        $req = $pdo->prepare($sql);
        $req->execute();
        $this->data = $req->fetch();
    }

    private function setAuthor()
    {
        global $pdo;

        // SQL to get firstname + lastname
        $sql = "SELECT firstname,lastname FROM users WHERE userid = :userid";
        $req = $pdo->prepare($sql);
        $req->execute(array(
            'userid' => $this->data['userid']
        ));
        $data = $req->fetch();

        $this->author = $data['firstname'] . ' ' . $data['lastname'];
    }

    private function setCleanTitle()
    {
        $this->title = stripslashes($this->data['title']);
        $this->cleanTitle = $this->data['date'] . "-" . preg_replace('/[^A-Za-z0-9]/', '_', stripslashes($this->data['title']));
    }

    private function setTags()
    {

        global $pdo;
        // SQL to get tags
        $sql = "SELECT tag FROM " . $this->type . "_tags WHERE item_id = " . $this->id;
        $req = $pdo->prepare($sql);
        $req->execute();
        $this->tags = null;
        while ($data = $req->fetch()) {
            $this->tags .= $data['tag'] . ' ';
        }
        $req->closeCursor();
    }

    private function addElabid()
    {

        // ELABID
        if ($this->type === 'experiments') {
            $this->content .= "<p class='elabid'>elabid : " . $this->data['elabid'] . "</p>";
        }
    }

    private function addLockinfo()
    {

        // LOCK BLOCK
        if ($this->data['locked'] == '1' && $this->type == 'experiments') {
            global $pdo;
            // get info about the locker
            $sql = "SELECT firstname,lastname FROM users WHERE userid = :userid LIMIT 1";
            $reqlock = $pdo->prepare($sql);
            $reqlock->execute(array(
                'userid' => $this->data['lockedby']
            ));
            $lockuser = $reqlock->fetch();

            // separate date and time
            if (isset($this->data['lockedwhen'])) {
                $lockdate = explode(' ', $this->data['lockedwhen']);
                $this->content .= "<p class='elabid'>locked by " . $lockuser['firstname'] . " " . $lockuser['lastname'] . " on " . $lockdate[0] . " at " . $lockdate[1] . ".</p>";
            }
        }
    }

    private function addComments()
    {
        global $pdo;

        // SQL to get comments
        // check if there is something to display first
        // get all comments, and infos on the commenter associated with this experiment
        $sql = "SELECT * FROM experiments_comments
            LEFT JOIN users ON (experiments_comments.userid = users.userid)
            WHERE exp_id = :id
            ORDER BY experiments_comments.datetime DESC";
        $req = $pdo->prepare($sql);
        $req->execute(array(
            'id' => $this->id
        ));
        // if we have comments
        if ($req->rowCount() > 0) {
            $this->content.= "<section>";
            if ($req->rowCount() === 1) {
                $this->content .= "<h3>Comment :</h3>";
            } else {
                $this->content .= "<h3>Comments :</h3>";
            }
            // there is comments to display
            while ($comments = $req->fetch()) {
                if (empty($comments['firstname'])) {
                    $comments['firstname'] = '[deleted]';
                }
                $this->content .= "<p>On " . $comments['datetime'] . " " . $comments['firstname'] . " " . $comments['lastname'] . " wrote :<br />";
                $this->content .= "<p>" . $comments['comment'] . "</p>";<commit_msg>cleanTitle is private, fix spacing, fix multiple declaration<commit_after><?php
/********************************************************************************
*                                                                               *
*   Copyright 2012 <NAME> (<EMAIL>)                      *
*   http://www.elabftw.net/                                                     *
*                                                                               *
********************************************************************************/

/********************************************************************************
*  This file is part of eLabFTW.                                                *
*                                                                               *
*    eLabFTW 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.                        *
*                                                                               *
*    eLabFTW 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 eLabFTW.  If not, see <http://www.gnu.org/licenses/>.   *
*                                                                               *
********************************************************************************/
namespace elabftw\elabftw;

class MakePdf {

    private $id;
    private $type;
    private $data;
    private $cleanTitle;

    public $author;
    public $title;
    public $tags;
    public $content;


    public function __construct($id, $type)
    {
        $this->id = $id;
        $this->type = $type;

        $this->initData();
        $this->setAuthor();
        $this->setCleanTitle();
        $this->setTags();
        $this->buildContent();
    }

    public function getPath()
    {
        return ELAB_ROOT . 'uploads/' . $this->cleanTitle . '.pdf';
    }

    public function getFileName()
    {
        return $this->cleanTitle . '.pdf';
    }

    private function initData()
    {
        global $pdo;

        // title, date, body, elabid, userid, lock
        $sql = "SELECT * FROM " . $this->type . " WHERE id = " . $this->id;
        $req = $pdo->prepare($sql);
        $req->execute();
        $this->data = $req->fetch();
    }

    private function setAuthor()
    {
        global $pdo;

        // SQL to get firstname + lastname
        $sql = "SELECT firstname,lastname FROM users WHERE userid = :userid";
        $req = $pdo->prepare($sql);
        $req->execute(array(
            'userid' => $this->data['userid']
        ));
        $data = $req->fetch();

        $this->author = $data['firstname'] . ' ' . $data['lastname'];
    }

    private function setCleanTitle()
    {
        $this->title = stripslashes($this->data['title']);
        $this->cleanTitle = $this->data['date'] . "-" . preg_replace('/[^A-Za-z0-9]/', '_', stripslashes($this->data['title']));
    }

    private function setTags()
    {

        global $pdo;
        // SQL to get tags
        $sql = "SELECT tag FROM " . $this->type . "_tags WHERE item_id = " . $this->id;
        $req = $pdo->prepare($sql);
        $req->execute();
        $this->tags = null;
        while ($data = $req->fetch()) {
            $this->tags .= $data['tag'] . ' ';
        }
        $req->closeCursor();
    }

    private function addElabid()
    {

        // ELABID
        if ($this->type === 'experiments') {
            $this->content .= "<p class='elabid'>elabid : " . $this->data['elabid'] . "</p>";
        }
    }

    private function addLockinfo()
    {

        // LOCK BLOCK
        if ($this->data['locked'] == '1' && $this->type == 'experiments') {
            global $pdo;
            // get info about the locker
            $sql = "SELECT firstname,lastname FROM users WHERE userid = :userid LIMIT 1";
            $reqlock = $pdo->prepare($sql);
            $reqlock->execute(array(
                'userid' => $this->data['lockedby']
            ));
            $lockuser = $reqlock->fetch();

            // separate date and time
            if (isset($this->data['lockedwhen'])) {
                $lockdate = explode(' ', $this->data['lockedwhen']);
                $this->content .= "<p class='elabid'>locked by " . $lockuser['firstname'] . " " . $lockuser['lastname'] . " on " . $lockdate[0] . " at " . $lockdate[1] . ".</p>";
            }
        }
    }

    private function addComments()
    {
        global $pdo;

        // SQL to get comments
        // check if there is something to display first
        // get all comments, and infos on the commenter associated with this experiment
        $sql = "SELECT * FROM experiments_comments
            LEFT JOIN users ON (experiments_comments.userid = users.userid)
            WHERE exp_id = :id
            ORDER BY experiments_comments.datetime DESC";
        $req = $pdo->prepare($sql);
        $req->execute(array(
            'id' => $this->id
        ));
        // if we have comments
        if ($req->rowCount() > 0) {
            $this->content .= "<section>";
            if ($req->rowCount() === 1) {
                $this->content .= "<h3>Comment :</h3>";
            } else {
                $this->content .= "<h3>Comments :</h3>";
            }
            // there is comments to display
            while ($comments = $req->fetch()) {
                if (empty($comments['firstname'])) {
                    $comments['firstname'] = '[deleted]';
                }
                $this->content .= "<p>On " . $comments['datetime'] . " " . $comments['firstname'] . " " . $comments['lastname'] . " wrote :<br />";
                $this->content .= "<p>" . $comments['comment'] . "</p>";
{'fraction_non_alphanumeric': 0.18746021642266072, 'fraction_numerical': 0.0027052832590706557, 'mean_word_length': 1.4742125984251968, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 155, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7194636', 'n_tokens_mistral': 3278, 'n_tokens_neox': 3142, 'n_words': 816}