public function create(array $users, int $public = 0): ?string
  {
    if ( $this->check() ){
      $join = '';
      $where = '';
      $values = [$this->user->get_id(), $public];
      foreach ( $users as $i => $u ){
        $join .= "JOIN bbn_chats_users AS u$i ON u$i.id_chat = bbn_chats.id".PHP_EOL;
        $where .= "AND u$i.id_user = ?".PHP_EOL;
        $values[] = $u;
      }
      $sql = <<<SQL
SELECT id
FROM bbn_chats
  $join
WHERE creator = ?
AND public = ? 
$where
SQL;
      if ( ($id_chat = $this->db->get_one($sql, $values)) && (count($users) === $this->db->count('bbn_chat_users', ['id_chat' => $id_chat])) ){
        return $id_chat;
      }
      if ( $this->db->insert('bbn_chats', [
        'creator' => $this->user->get_id(),
        'creation' => date('Y-m-d H:i:s'),
        'public' => $public ? 1 : 0
      ]) ){
        $id_chat = $this->db->last_id();
        $this->db->insert('bbn_chats_users', [
          'id_chat' => $id_chat,
          'id_user' => $this->user->get_id(),
          'entrance' => microtime(true),
          'admin' => 1
        ]);
        foreach ( $users as $user ){
          $this->db->insert_ignore('bbn_chats_users', [
            'id_chat' => $id_chat,
            'id_user' => $user,
            'entrance' => microtime(true),
            'admin' => 0
          ]);
        }
        return $id_chat;
      }
    }
    return null;
  }
public function create(array $users, int $public = 0): ?string
  {
    if ( $this->check() ){
      $join = '';
      $where = '';
      $values = [$this->user->get_id(), $public];
      foreach ( $users as $i => $u ){
        $join .= "JOIN bbn_chats_users AS u$i ON u$i.id_chat = bbn_chats.id".PHP_EOL;
        $where .= "AND u$i.id_user = ?".PHP_EOL;
        $values[] = $u;
      }
      $sql = <<<SQL
SELECT id
FROM bbn_chats
  $join
WHERE creator = ?
AND public = ? 
$where
SQL;
      if ( ($id_chat = $this->db->get_one($sql, $values)) && (count($users) === $this->db->count('bbn_chat_users', ['id_chat' => $id_chat])) ){
        return $id_chat;
      }
      if ( $this->db->insert('bbn_chats', [
        'creator' => $this->user->get_id(),
        'creation' => date('Y-m-d H:i:s'),
        'public' => $public ? 1 : 0
      ]) ){
        $id_chat = $this->db->last_id();
        $this->db->insert('bbn_chats_users', [
          'id_chat' => $id_chat,
          'id_user' => $this->user->get_id(),
          'entrance' => microtime(true),
          'admin' => 1
        ]);
        foreach ( $users as $user ){
          $this->db->insert_ignore('bbn_chats_users', [
            'id_chat' => $id_chat,
            'id_user' => $user,
            'entrance' => microtime(true),
            'admin' => 0
          ]);
        }
        return $id_chat;
      }
    }
    return null;
  }
public function create ( array $ users , int $ public = 0 ) : ? string { if ( $ this -> check ( ) ) { $ join = '' ; $ where = '' ; $ values = [ $ this -> user -> get_id ( ) , $ public ] ; foreach ( $ users as $ i => $ u ) { $ join .= "JOIN bbn_chats_users AS u$i ON u$i.id_chat = bbn_chats.id" . PHP_EOL ; $ where .= "AND u$i.id_user = ?" . PHP_EOL ; $ values [ ] = $ u ; } $ sql =  <<<SQL
SELECT id
FROM bbn_chats
  $join
WHERE creator = ?
AND public = ? 
$where
SQL ; if ( ( $ id_chat = $ this -> db -> get_one ( $ sql , $ values ) ) && ( count ( $ users ) === $ this -> db -> count ( 'bbn_chat_users' , [ 'id_chat' => $ id_chat ] ) ) ) { return $ id_chat ; } if ( $ this -> db -> insert ( 'bbn_chats' , [ 'creator' => $ this -> user -> get_id ( ) , 'creation' => date ( 'Y-m-d H:i:s' ) , 'public' => $ public ? 1 : 0 ] ) ) { $ id_chat = $ this -> db -> last_id ( ) ; $ this -> db -> insert ( 'bbn_chats_users' , [ 'id_chat' => $ id_chat , 'id_user' => $ this -> user -> get_id ( ) , 'entrance' => microtime ( true ) , 'admin' => 1 ] ) ; foreach ( $ users as $ user ) { $ this -> db -> insert_ignore ( 'bbn_chats_users' , [ 'id_chat' => $ id_chat , 'id_user' => $ user , 'entrance' => microtime ( true ) , 'admin' => 0 ] ) ; } return $ id_chat ; } } return null ; }
Creates a new chat with the current userr and another participant.

@param array $users
@param int $public
@return null|string
Creates a new chat with the current userr and another participant .