118 lines
3.3 KiB
PHP
118 lines
3.3 KiB
PHP
<?php
|
|
|
|
|
|
namespace app\models;
|
|
|
|
/**
|
|
* Class UserPublic
|
|
* @package app\models
|
|
* @property $id
|
|
* @property $full_name
|
|
* @property $email
|
|
* @property $password
|
|
* @property $password_hash
|
|
* @property $club_card
|
|
* @property $has_double_auth
|
|
* @property $has_newsletter
|
|
* @property $open_id
|
|
* @property $is_active
|
|
* @property $is_mobile_user
|
|
* @property $user_agent
|
|
* @property $device_id
|
|
* @property $date_registered
|
|
* @property $favorites
|
|
* @property Order[] $orders;
|
|
* @property \app\models\QrValidators[] $qrValidators
|
|
*/
|
|
class UserPublic extends _Base
|
|
{
|
|
public function setPasswordHash($password)
|
|
{
|
|
$this->password_hash = password_hash($password, PASSWORD_DEFAULT);
|
|
}
|
|
|
|
public function getExplorerObjectsSumPoints()
|
|
{
|
|
return $this->hasMany(UserExplorerObjects::class, ['user_id' => 'id'])
|
|
->joinWith('explorerObject')->sum('points');
|
|
}
|
|
|
|
public function getExplorerObjectsIds()
|
|
{
|
|
$visited = UserExplorerObjects::find()->where(['user_id' => $this->id])->select('explorer_object_id')->all();
|
|
$visitedIds = [];
|
|
foreach ($visited as $v) {
|
|
$visitedIds[] = $v->explorer_object_id;
|
|
}
|
|
return $visitedIds;
|
|
}
|
|
|
|
public function profileData()
|
|
{
|
|
$lg = \Yii::$app->language;
|
|
|
|
$userData = [
|
|
'id' => $this->id,
|
|
'email' => $this->email,
|
|
'club_card' => $this->club_card,
|
|
'full_name' => $this->full_name,
|
|
'points' => $this->getExplorerObjectsSumPoints()
|
|
];
|
|
|
|
|
|
if (sizeof($this->qrValidators) > 0) {
|
|
$partners = [];
|
|
foreach ($this->qrValidators as $qrValidator) {
|
|
if ($qrValidator->partner) {
|
|
$partners[] = [
|
|
'id' => $qrValidator->partner_id,
|
|
'name' => $qrValidator->partner->{'name' . ($lg == 'en' ? '_en' : '')}
|
|
|
|
];
|
|
}
|
|
}
|
|
$userData['associate_partners'] = $partners;
|
|
}
|
|
return (object)$userData;
|
|
}
|
|
|
|
public function getOrders()
|
|
{
|
|
return $this->hasMany(Order::class, ['user_id' => 'id'])->orderBy(['order_date' => SORT_DESC])->all();
|
|
}
|
|
|
|
public function getQrValidators()
|
|
{
|
|
return $this->hasMany(QrValidators::class, ['user_id' => 'id']);
|
|
}
|
|
|
|
|
|
public function groupOrderItems() {
|
|
$order_bookings = [];
|
|
$order_publications = [];
|
|
$order_subscriptions = [];
|
|
$order_collections = [];
|
|
foreach ($this->orders as $order) {
|
|
foreach ($order->booking as $b) {
|
|
$order_bookings[$order->id][] = $b;
|
|
}
|
|
foreach ($order->subscriptions as $subscription) {
|
|
$order_subscriptions[$order->id][] = $subscription;
|
|
}
|
|
foreach ($order->collections as $collection) {
|
|
$order_collections[$order->id][] = $collection;
|
|
}
|
|
foreach ($order->publications as $publication) {
|
|
$order_publications[$order->id][] = $publication;
|
|
}
|
|
}
|
|
|
|
return [
|
|
'bookings' => $order_bookings,
|
|
'subscriptions' => $order_subscriptions,
|
|
'collections' => $order_collections,
|
|
'publications' => $order_publications
|
|
];
|
|
}
|
|
}
|