yii2-test-1/common/services/UserService.php
2023-05-06 20:40:02 +03:00

76 lines
1.9 KiB
PHP

<?php
namespace common\services;
use common\models\Text;
use common\models\User;
use yii\web\UploadedFile;
class UserService
{
public function saveTexts($post, $profile_id, $texts)
{
foreach ($texts as $text) {
$text->text = $post['text'][$text->language];
$text->title = $post['title'][$text->language];
$text->profile_id = $profile_id;
$text->save();
}
}
public function saveProfile($post, $model)
{
$model->load($post);
$model->created_at = date('Y-d-m h:i:s');
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file) {
$model->image = $model->file->baseName . '.' . $model->file->extension;
$model->file->saveAs('@frontend/web/uploads/' . $model->file->baseName . '.' . $model->file->extension);
$model->file = null;
}
if ($model->save()) {
return true;
} else
return false;
}
public function getAllUsers()
{
return $users = User::find()->all();
}
public function newTexts()
{
$texts = [];
foreach (Text::getLanguages() as $lang) {
$text = new Text();
$text->language = $lang;
$texts[] = $text;
}
return $texts;
}
public function getTexts($id)
{
$textModels = Text::find()->where(['profile_id' => $id])->all();
// foreach ($textModels as $text)
// {
// $text->title[$text->language] = $text->title;
// $text->text[$text->language] = $text->text;
// }
return $textModels;
}
public function getTextByLanguage($id, $slug)
{
return Text::find()->where(['language' => $slug, 'profile_id' => $id])->all();
}
public static function run()
{
return new self();
}
}