<commit_before><commit_msg>New List Model class; work in progress<commit_after><?php
namespace phpList\Model;

use phpList\Entity\SubscriberEntity;
use phpList\helper\String;

class ListModel {

    protected $db;

    public function __construct( \phplist\Config $config, \phplist\helper\Database $db )
    {
        $this->config = $config;
        $this->db = $db;
    }

    /**
    * Add subscriber to given list
    * @param $subscriber_id
    * @param $list_id
    */
    public function addSubscriber( $subscriberId, $listId )
    {
        $this->db->query(
            sprintf(
                'INSERT INTO
                    %s
                        (userid, listid)
                VALUES
                    (%d, %d)'
                , $this->config->getTableName( 'listuser' )
                , $subscriberId
                , $listId
            )
        );
    }

    /**
    * Subscribe an array of subscriber ids to given list
    * @param $subscriber_ids
    * @param $list_id
    */
    public function addSubscribers( $subscriberIdArray, $listId )
    {
        if ( !empty( $subscriberIdArray ) ) {
            $query = sprintf( '
                INSERT INTO
                    %s
                        (userid, listid, entered, modified)
                VALUES'
                , $this->config->getTableName( 'listuser' )
            );

            foreach ( $subscriberIdArray as $uid ) {
                $query .= sprintf( '
                    (%d, %d, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),'
                    , $uid
                    , $listId
                );
            }
            $query = rtrim( $query, ',' ) . ';';
            $this->db->query( $query );
        }
    }

    /**
    * Get an array of all lists in the database
    * @return array
    */
    public function getAllLists()
    {
        //TODO: probably best to replace the subselect with a function parameter
        $result = $this->db->query(
        sprintf( '
            SELECT
                *
            FROM
                %s %s'
            , $this->config->getTableName( 'list' )
            , $this->config->get( 'subselect', '' ) )
        );
        return $this->makeLists( $result );
    }

    /**
    * Get all lists of a given owner, can also be used to check if the user is the owner of given list
    * @param $owner_id
    * @param int $id
    * @return array
    */
    public function getListsByOwner( $owner_id, $id = 0 )
    {
        $result = $this->db->query(
            sprintf( '
                SELECT
                    *
                FROM
                    %s
                WHERE
                    owner = %d %s'
                , $this->config->getTableName( 'list' )
                , $owner_id
                , ( ($id == 0) ? '' : " AND id = $id" )
            )
        );
        return $this->makeLists($result);
    }

    /**
    * Get list with given id
    * @param $id
    * @return MailingListEntity
    */
    public function getListById( $id )
    {
        $result = $this->db->query(
            sprintf( '
                SELECT
                    *
                FROM
                    %s
                WHERE
                    id = %d'
                , $this->config->getTableName( 'list' )
                , $id
            )
        );
        return $this->listFromArray($result->fetch(\PDO::FETCH_ASSOC));
    }

    /**
    * Get lists a given user is subscribed to
    * @param $subscriber_id
    * @return array
    */
    public function getListsForSubscriber( $subscriber_id )
    {
        $result = $this->db->query(
            sprintf( '
                SELECT
                    l.*
                FROM
                    %s AS lu
                INNER JOIN
                    %s AS l
                ON
                    lu.listid = l.id
                WHERE
                    lu.userid = %d'
                , $this->config->getTableName( 'listuser' )
                , $this->config->getTableName( 'list' )
                , $subscriber_id
            )
        );
        return $this->makeLists( $result );
    }

    /**
    * Get subscribers subscribed to a given list
    * @param $list
    * @return array
    */
    public function getListSubscribers( $list )
    {
        if( is_array( $list ) ) {
            $where = ' WHERE listid IN (' . join( ',', $list ) .')';
        } else {
            $where = sprintf( ' WHERE listid = %d', $list );
        }
        $result = $this->db->query(
            sprintf( '
                SELECT
                    userid
                FROM
                    %s %s'
                , $this->config->getTableName( 'listuser' )
                , $where
            )
        );

        return $result->fetchAll( \PDO::FETCH_ASSOC );
    }
}
{'fraction_non_alphanumeric': 0.11570419609472372, 'fraction_numerical': 0.0004154549231408392, 'mean_word_length': 1.1174142480211082, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7024017', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1319, 'n_words': 316}