2025-01-31 18:20:13 +03:00
< ? php
declare ( strict_types = 1 );
2025-01-22 17:05:28 +03:00
namespace openvk\Web\Models\Repositories ;
2025-01-31 18:20:13 +03:00
2025-01-22 17:05:28 +03:00
use openvk\Web\Models\Entities\Document ;
use Nette\Database\Table\ActiveRow ;
use Chandler\Database\DatabaseConnection ;
use openvk\Web\Models\Repositories\Util\EntityStream ;
class Documents
{
private $context ;
private $documents ;
2025-01-31 18:20:13 +03:00
public function __construct ()
2025-01-22 17:05:28 +03:00
{
$this -> context = DatabaseConnection :: i () -> getContext ();
$this -> documents = $this -> context -> table ( " documents " );
}
2025-01-31 18:20:13 +03:00
2025-01-22 17:05:28 +03:00
private function toDocument ( ? ActiveRow $ar ) : ? Document
{
2025-01-31 18:20:13 +03:00
return is_null ( $ar ) ? null : new Document ( $ar );
2025-01-22 17:05:28 +03:00
}
2025-01-31 18:20:13 +03:00
public function get ( int $id ) : ? Document
2025-01-22 17:05:28 +03:00
{
return $this -> toDocument ( $this -> documents -> get ( $id ));
}
2025-01-31 18:20:13 +03:00
2025-01-22 17:05:28 +03:00
# By "Virtual ID" and "Absolute ID" (to not leak owner's id).
2025-01-31 18:20:13 +03:00
public function getDocumentById ( int $virtual_id , int $real_id , string $access_key = null ) : ? Document
2025-01-22 17:05:28 +03:00
{
$doc = $this -> documents -> where ([ 'virtual_id' => $virtual_id , 'id' => $real_id ]);
/* if ( $access_key ) {
$doc -> where ( " access_key " , $access_key );
} */
$doc = $doc -> fetch ();
2025-01-31 18:20:13 +03:00
if ( is_null ( $doc )) {
return null ;
}
2025-01-22 17:05:28 +03:00
$n_doc = new Document ( $doc );
2025-01-31 18:20:13 +03:00
if ( ! $n_doc -> checkAccessKey ( $access_key )) {
return null ;
}
2025-01-22 17:05:28 +03:00
return $n_doc ;
}
2025-01-31 18:20:13 +03:00
public function getDocumentByIdUnsafe ( int $virtual_id , int $real_id ) : ? Document
2025-01-22 17:05:28 +03:00
{
$doc = $this -> documents -> where ([ 'virtual_id' => $virtual_id , 'id' => $real_id ]);
$doc = $doc -> fetch ();
2025-01-31 18:20:13 +03:00
if ( is_null ( $doc )) {
return null ;
}
2025-01-22 17:05:28 +03:00
$n_doc = new Document ( $doc );
return $n_doc ;
}
2025-01-31 18:20:13 +03:00
public function getDocumentsByOwner ( int $owner , int $order = 0 , int $type = - 1 ) : EntityStream
2025-01-22 17:05:28 +03:00
{
$search = $this -> documents -> where ([
" owner " => $owner ,
" unlisted " => 0 ,
" deleted " => 0 ,
]);
2025-01-31 18:20:13 +03:00
if ( in_array ( $type , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ])) {
2025-01-22 17:05:28 +03:00
$search -> where ( " type " , $type );
}
2025-01-31 18:20:13 +03:00
switch ( $order ) {
2025-01-22 17:05:28 +03:00
case 0 :
$search -> order ( " id DESC " );
break ;
case 1 :
$search -> order ( " name DESC " );
break ;
case 2 :
$search -> order ( " filesize DESC " );
break ;
}
return new EntityStream ( " Document " , $search );
}
2025-01-31 18:20:13 +03:00
public function getTypes ( int $owner_id ) : array
2025-01-22 17:05:28 +03:00
{
$result = DatabaseConnection :: i () -> getConnection () -> query ( " SELECT `type`, COUNT(*) AS `count` FROM `documents` WHERE `owner` = ? AND `deleted` = 0 AND `unlisted` = 0 GROUP BY `type` ORDER BY `type` " , $owner_id );
$response = [];
2025-01-31 18:20:13 +03:00
foreach ( $result as $res ) {
if ( $res -> count < 1 || $res -> type == 0 ) {
continue ;
}
2025-01-22 17:05:28 +03:00
2025-01-31 18:20:13 +03:00
$name = tr ( " document_type_ " . $res -> type );
2025-01-22 17:05:28 +03:00
$response [] = [
" count " => $res -> count ,
" type " => $res -> type ,
" name " => $name ,
];
}
return $response ;
}
2025-01-31 18:20:13 +03:00
public function getTags ( int $owner_id , ? int $type = 0 ) : array
2025-01-22 17:05:28 +03:00
{
$query = " SELECT `tags` FROM `documents` WHERE `owner` = ? AND `deleted` = 0 AND `unlisted` = 0 " ;
2025-01-31 18:20:13 +03:00
if ( $type > 0 && $type < 9 ) {
2025-01-22 17:05:28 +03:00
$query .= " AND `type` = $type " ;
}
$query .= " AND `tags` IS NOT NULL ORDER BY `id` " ;
$result = DatabaseConnection :: i () -> getConnection () -> query ( $query , $owner_id );
$tags = [];
2025-01-31 18:20:13 +03:00
foreach ( $result as $res ) {
2025-01-22 17:05:28 +03:00
$tags [] = $res -> tags ;
}
$imploded_tags = implode ( " , " , $tags );
$exploded_tags = array_values ( array_unique ( explode ( " , " , $imploded_tags )));
2025-01-31 18:20:13 +03:00
if ( $exploded_tags [ 0 ] == " " ) {
2025-01-22 17:05:28 +03:00
return [];
2025-01-31 18:20:13 +03:00
}
2025-01-22 17:05:28 +03:00
return array_slice ( $exploded_tags , 0 , 50 );
}
2025-01-31 18:20:13 +03:00
public function find ( string $query , array $params = [], array $order = [ 'type' => 'id' , 'invert' => false ]) : Util\EntityStream
2025-01-22 17:05:28 +03:00
{
$result = $this -> documents -> where ( " name LIKE ? " , " % $query % " ) -> where ([
" deleted " => 0 ,
2025-01-31 18:20:13 +03:00
" folder_id != " => 0 ,
2025-01-22 17:05:28 +03:00
]);
$order_str = 'id' ;
2025-01-31 18:20:13 +03:00
switch ( $order [ 'type' ]) {
2025-01-22 17:05:28 +03:00
case 'id' :
$order_str = 'created ' . ( $order [ 'invert' ] ? 'ASC' : 'DESC' );
break ;
}
2025-01-31 18:20:13 +03:00
foreach ( $params as $paramName => $paramValue ) {
switch ( $paramName ) {
2025-01-22 17:05:28 +03:00
case " type " :
2025-01-31 18:20:13 +03:00
if ( $paramValue < 1 || $paramValue > 8 ) {
continue ;
}
2025-01-22 17:05:28 +03:00
$result -> where ( " type " , $paramValue );
break ;
case " tags " :
$result -> where ( " tags LIKE ? " , " % $paramValue % " );
break ;
case " from_me " :
$result -> where ( " owner " , $paramValue );
break ;
}
}
2025-01-31 18:20:13 +03:00
if ( $order_str ) {
2025-01-22 17:05:28 +03:00
$result -> order ( $order_str );
2025-01-31 18:20:13 +03:00
}
2025-01-22 17:05:28 +03:00
return new Util\EntityStream ( " Document " , $result );
}
}