2020-06-07 19:04:43 +03:00
< ? php declare ( strict_types = 1 );
namespace openvk\Web\Models\Repositories ;
use openvk\Web\Models\Entities\User ;
2021-12-27 18:36:05 +03:00
use openvk\Web\Models\Repositories\Posts ;
use openvk\Web\Models\Repositories\Comments ;
2020-06-07 19:04:43 +03:00
use Nette\Database\Table\ActiveRow ;
use Chandler\Database\DatabaseConnection ;
use Chandler\Security\User as ChandlerUser ;
class Users
{
private $context ;
private $users ;
function __construct ()
{
$this -> context = DatabaseConnection :: i () -> getContext ();
$this -> users = $this -> context -> table ( " profiles " );
}
private function toUser ( ? ActiveRow $ar ) : ? User
{
return is_null ( $ar ) ? NULL : new User ( $ar );
}
function get ( int $id ) : ? User
{
return $this -> toUser ( $this -> users -> get ( $id ));
}
2021-12-28 18:24:30 +03:00
function getByShortURL ( string $url , bool $handleId = false ) : ? User
2020-06-07 19:04:43 +03:00
{
2021-12-28 18:45:56 +03:00
$user = $this -> toUser (( clone $this -> users ) -> where ( " shortcode " , $url ) -> fetch ());
if ( $user !== null )
2021-12-28 18:25:41 +03:00
return $user ;
2021-12-28 18:24:30 +03:00
else if ( $handleId == true )
{
2021-12-28 18:27:23 +03:00
$id = array ();
2021-12-28 18:24:30 +03:00
preg_match ( " /id([0-9]+)/ " , $url , $id );
2021-12-28 18:45:56 +03:00
$id = intval ( $id [ 1 ]);
return $this -> get ( $id );
2021-12-28 18:24:30 +03:00
}
return null ;
2020-06-07 19:04:43 +03:00
}
function getByChandlerUser ( ChandlerUser $user ) : ? User
{
return $this -> toUser ( $this -> users -> where ( " user " , $user -> getId ()) -> fetch ());
}
2020-11-22 13:29:27 +03:00
function find ( string $query ) : Util\EntityStream
2020-06-07 19:04:43 +03:00
{
2020-11-22 13:29:27 +03:00
$query = " % $query % " ;
2022-01-27 14:46:28 +03:00
$result = $this -> users -> where ( " CONCAT_WS(' ', first_name, last_name) LIKE ? " , $query ) -> where ( " deleted " , 0 );
2020-08-01 17:27:10 +03:00
return new Util\EntityStream ( " User " , $result );
2020-06-07 19:04:43 +03:00
}
function getStatistics () : object
{
return ( object ) [
2021-12-27 18:36:05 +03:00
" all " => sizeof ( clone $this -> users ),
" active " => sizeof (( clone $this -> users ) -> where ( " online > 0 " )),
" online " => sizeof (( clone $this -> users ) -> where ( " online >= ? " , time () - 900 )),
" posts " => ( new Posts ) -> getCountOfAllPosts (),
" comments " => ( new Comments ) -> getCountOfAllComments ()
2020-06-07 19:04:43 +03:00
];
}
2021-12-19 15:34:33 +03:00
function getByAddress ( string $address ) : ? User
{
if ( substr_compare ( $address , " / " , - 1 ) === 0 )
$address = substr ( $address , 0 , iconv_strlen ( $address ) - 1 );
$serverUrl = ovk_scheme ( true ) . $_SERVER [ " SERVER_NAME " ];
if ( strpos ( $address , $serverUrl . " / " ) === 0 )
$address = substr ( $address , iconv_strlen ( $serverUrl ) + 1 );
if ( strpos ( $address , " id " ) === 0 ) {
$user = $this -> get (( int ) substr ( $address , 2 ));
if ( $user ) return $user ;
}
return $this -> getByShortUrl ( $address );
}
2022-01-03 18:31:55 +03:00
/**
* If you need to check if the user is an instance administrator , use `$user->getChandlerUser()->can("access")->model("admin")->whichBelongsTo(NULL)` .
* This method is more suitable for instance administrators lists
*/
function getInstanceAdmins ( bool $excludeHidden = true ) : \Traversable
{
$query = " SELECT DISTINCT(`profiles`.`id`) FROM `ChandlerACLRelations` JOIN `profiles` ON `ChandlerACLRelations`.`user` = `profiles`.`user` COLLATE utf8mb4_unicode_520_ci WHERE `ChandlerACLRelations`.`group` IN (SELECT `group` FROM `ChandlerACLGroupsPermissions` WHERE `model` = \" admin \" AND `permission` = \" access \" ) " ;
if ( $excludeHidden )
$query .= " AND `ChandlerACLRelations`.`user` NOT IN (SELECT `user` FROM `ChandlerACLRelations` WHERE `group` IN (SELECT `group` FROM `ChandlerACLGroupsPermissions` WHERE `model` = \" hidden_admin \" AND `permission` = \" be \" )) " ;
$query .= " ORDER BY `profiles`.`id`; " ;
$result = DatabaseConnection :: i () -> getConnection () -> query ( $query );
foreach ( $result as $entry )
yield $this -> get ( $entry -> id );
}
2020-06-07 19:04:43 +03:00
use \Nette\SmartObject ;
}