<commit_before><?php
/**
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see https://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
declare(strict_types=1);

namespace Elabftw\Models;

use Elabftw\Elabftw\Db;
use PDO;

/**
 * For dealing with pinned items
 */
class Pins
{
    /** @var Db $Db */
    private $Db;

    /** @var AbstractEntity $Entity */
    private $Entity;

    public function __construct(AbstractEntity $entity)
    {
        $this->Db = Db::getConnection();
        $this->Entity = $entity;
    }

    /**
     * Check if the current entity is pin of current user
     *
     * @return bool
     */
    public function isPinned(): bool
    {
        $sql = 'SELECT DISTINCT id FROM pin2users WHERE entity_id = :entity_id AND type = :type AND users_id = :users_id';
        $req = $this->Db->prepare($sql);
        $req->bindParam(':users_id', $this->Entity->Users->userData['userid']);
        $req->bindParam(':entity_id', $this->Entity->id, PDO::PARAM_INT);
        $req->bindParam(':type', $this->Entity->type);

        $this->Db->execute($req);
        return $req->rowCount() > 0;
    }

    /**
     * Add/remove current entity as pinned for current user
     *
     * @return void
     */
    public function togglePin(): void
    {
        $this->isPinned() ? $this->rmFromPinned() : $this->addToPinned();
    }

    /**
     * Get the items pinned by current users to display in show mode
     *
     * @return array
     */
    public function getPinned(): array
    {
        $sql = 'SELECT DISTINCT entity_id FROM pin2users WHERE users_id = :users_id AND type = :type';
        $req = $this->Db->prepare($sql);
        $req->bindParam(':users_id', $this->Entity->Users->userData['userid']);
        $req->bindParam(':type', $this->Entity->type);

        $this->Db->execute($req);

        $pinArr = array();
        $ids = $req->fetchAll();
        if ($ids === false) {
            return $pinArr;
        }
        $entity = clone $this->Entity;
        foreach ($ids as $id) {
            $entity->setId((int) $id['entity_id']);
            $pinArr[] = $entity->read();
        }
        return $pinArr;
    }

    /**
     * Remove current entity from pinned of current user
     * The method is public because it is called on entity destroy
     *
     * @return void
     */
    public function rmFromPinned(): void
    {
        $this->Entity->canOrExplode('read');

        $sql = 'DELETE FROM pin2users WHERE entity_id = :entity_id AND users_id = :users_id AND type = :type';
        $req = $this->Db->prepare($sql);
        $req->bindParam(':users_id', $this->Entity->Users->userData['userid']);
        $req->bindParam(':entity_id', $this->Entity->id, PDO::PARAM_INT);
        $req->bindParam(':type', $this->Entity->type);

        $this->Db->execute($req);
    }

    /**
     * Add current entity to pinned of current user
     *
     * @return void
     */<commit_msg>fix issue with leftover pinned items if user deleting is not the one<commit_after><?php
/**
 * @author <NAME> <<EMAIL>>
 * @copyright 2012 <NAME>
 * @see https://www.elabftw.net Official website
 * @license AGPL-3.0
 * @package elabftw
 */
declare(strict_types=1);

namespace Elabftw\Models;

use Elabftw\Elabftw\Db;
use PDO;

/**
 * For dealing with pinned items
 */
class Pins
{
    /** @var Db $Db */
    private $Db;

    /** @var AbstractEntity $Entity */
    private $Entity;

    public function __construct(AbstractEntity $entity)
    {
        $this->Db = Db::getConnection();
        $this->Entity = $entity;
    }

    /**
     * Check if the current entity is pin of current user
     *
     * @return bool
     */
    public function isPinned(): bool
    {
        $sql = 'SELECT DISTINCT id FROM pin2users WHERE entity_id = :entity_id AND type = :type AND users_id = :users_id';
        $req = $this->Db->prepare($sql);
        $req->bindParam(':users_id', $this->Entity->Users->userData['userid']);
        $req->bindParam(':entity_id', $this->Entity->id, PDO::PARAM_INT);
        $req->bindParam(':type', $this->Entity->type);

        $this->Db->execute($req);
        return $req->rowCount() > 0;
    }

    /**
     * Add/remove current entity as pinned for current user
     *
     * @return void
     */
    public function togglePin(): void
    {
        $this->isPinned() ? $this->rmFromPinned() : $this->addToPinned();
    }

    /**
     * Get the items pinned by current users to display in show mode
     *
     * @return array
     */
    public function getPinned(): array
    {
        $sql = 'SELECT DISTINCT entity_id FROM pin2users WHERE users_id = :users_id AND type = :type';
        $req = $this->Db->prepare($sql);
        $req->bindParam(':users_id', $this->Entity->Users->userData['userid']);
        $req->bindParam(':type', $this->Entity->type);

        $this->Db->execute($req);

        $pinArr = array();
        $ids = $req->fetchAll();
        if ($ids === false) {
            return $pinArr;
        }
        $entity = clone $this->Entity;
        foreach ($ids as $id) {
            $entity->setId((int) $id['entity_id']);
            $pinArr[] = $entity->read();
        }
        return $pinArr;
    }

    /**
     * Remove current entity from pinned of current user
     * The method is public because it is called on entity destroy
     *
     * @return void
     */
    public function rmFromPinned(): void
    {
        $this->Entity->canOrExplode('read');

        $sql = 'DELETE FROM pin2users WHERE entity_id = :entity_id AND type = :type';
        $req = $this->Db->prepare($sql);
        $req->bindParam(':entity_id', $this->Entity->id, PDO::PARAM_INT);
        $req->bindParam(':type', $this->Entity->type);

        $this->Db->execute($req);
    }

    /**
     * Add current entity to pinned of current user
     *
     * @return void
     */
{'fraction_non_alphanumeric': 0.16212069553358335, 'fraction_numerical': 0.0037504261847937266, 'mean_word_length': 2.3316297558205563, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 111, 'https://': 2, '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': '1314282', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1828, 'n_words': 459}