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-30 15:56:06 +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 ]);
2024-12-29 23:16:21 +03:00
/* if ( $access_key ) {
2024-12-27 17:35:38 +03:00
$doc -> where ( " access_key " , $access_key );
2024-12-29 23:16:21 +03:00
} */
2024-12-27 17:35:38 +03:00
$doc = $doc -> fetch ();
2024-12-29 23:16:21 +03:00
if ( is_null ( $doc ))
2024-12-27 17:35:38 +03:00
return NULL ;
2024-12-29 23:16:21 +03:00
$n_doc = new Document ( $doc );
if ( ! $n_doc -> checkAccessKey ( $access_key ))
return NULL ;
return $n_doc ;
2024-12-27 17:35:38 +03:00
}
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
{
2024-12-30 15:56:06 +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 );
2024-12-28 13:35:55 +03:00
$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 ,
];
}
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 " :
2024-12-29 15:58:31 +03:00
if ( $paramValue < 1 || $paramValue > 8 ) continue ;
2024-12-28 13:35:55 +03:00
$result -> where ( " type " , $paramValue );
break ;
case " tags " :
2024-12-29 15:58:31 +03:00
$result -> where ( " tags LIKE ? " , " % $paramValue % " );
2024-12-28 13:35:55 +03:00
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 );
}
}