2024-12-27 17:35:38 +03:00
< ? php declare ( strict_types = 1 );
namespace openvk\Web\Models\Repositories ;
use openvk\Web\Models\Entities\Document ;
use Nette\Database\Table\ActiveRow ;
use Chandler\Database\DatabaseConnection ;
2024-12-28 13:35:55 +03:00
use openvk\Web\Models\Repositories\Util\EntityStream ;
2024-12-27 17:35:38 +03:00
class Documents
{
private $context ;
private $documents ;
function __construct ()
{
$this -> context = DatabaseConnection :: i () -> getContext ();
$this -> documents = $this -> context -> table ( " documents " );
}
private function toDocument ( ? ActiveRow $ar ) : ? Document
{
return is_null ( $ar ) ? NULL : new Document ( $ar );
}
2024-12-28 18:33:09 +03:00
function get ( int $id ) : ? Document
2024-12-27 17:35:38 +03:00
{
return $this -> toDocument ( $this -> documents -> get ( $id ));
}
# By "Virtual ID" and "Absolute ID" (to not leak owner's id).
2024-12-28 13:35:55 +03:00
function getDocumentById ( int $virtual_id , int $real_id , ? string $access_key = NULL ) : ? Document
2024-12-27 17:35:38 +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 ();
if ( ! is_null ( $doc ))
return new Document ( $doc );
else
return NULL ;
}
2024-12-28 13:35:55 +03:00
function getDocumentsByOwner ( int $owner , int $order = 0 , int $type = - 1 ) : EntityStream
{
$search = $this -> documents -> where ([
" owner " => $owner ,
" unlisted " => 0 ,
" deleted " => 0 ,
]);
if ( in_array ( $type , [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ])) {
$search -> where ( " type " , $type );
}
switch ( $order ) {
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 );
}
function getTypes ( int $owner_id ) : array
{
$result = DatabaseConnection :: i () -> getConnection () -> query ( " SELECT `type`, COUNT(*) AS `count` FROM `documents` WHERE `owner` = $owner_id GROUP BY `type` ORDER BY `type` " );
$response = [];
foreach ( $result as $res ) {
2024-12-28 22:54:32 +03:00
if ( $res -> count < 1 || $res -> type == 0 ) continue ;
2024-12-28 13:35:55 +03:00
$name = tr ( " document_type_ " . $res -> type );
$response [] = [
" count " => $res -> count ,
" type " => $res -> type ,
" name " => $name ,
];
}
2024-12-28 18:33:09 +03:00
if ( sizeof ( $response ) < 1 ) {
return [[
" count " => 0 ,
" type " => 0 ,
" name " => tr ( " document_type_0 " ),
]];
}
2024-12-28 13:35:55 +03:00
return $response ;
}
2024-12-27 17:35:38 +03:00
function find ( string $query , array $params = [], array $order = [ 'type' => 'id' , 'invert' => false ]) : Util\EntityStream
{
2024-12-28 13:35:55 +03:00
$result = $this -> documents -> where ( " name LIKE ? " , " % $query % " ) -> where ([
" deleted " => 0 ,
" folder_id != " => 0 ,
]);
2024-12-27 17:35:38 +03:00
$order_str = 'id' ;
switch ( $order [ 'type' ]) {
case 'id' :
$order_str = 'created ' . ( $order [ 'invert' ] ? 'ASC' : 'DESC' );
break ;
}
foreach ( $params as $paramName => $paramValue ) {
switch ( $paramName ) {
2024-12-28 13:35:55 +03:00
case " type " :
$result -> where ( " type " , $paramValue );
break ;
case " tags " :
$result -> where ( " tags " , $paramValue );
break ;
2024-12-28 22:54:32 +03:00
case " from_me " :
2024-12-28 13:35:55 +03:00
$result -> where ( " owner " , $paramValue );
2024-12-27 17:35:38 +03:00
break ;
}
}
if ( $order_str )
$result -> order ( $order_str );
return new Util\EntityStream ( " Document " , $result );
}
}