107 lines
2.6 KiB
PHP
107 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace common\services;
|
|
|
|
use common\models\Text;
|
|
use common\models\User;
|
|
use yii\web\UploadedFile;
|
|
|
|
class UserService
|
|
{
|
|
/**
|
|
* Метод для сохранения текстов профиля
|
|
* @param $post
|
|
* @param $profile_id
|
|
* @param $texts
|
|
* @return void
|
|
*/
|
|
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();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Метод для сохранения профиля
|
|
* @param $post
|
|
* @param $model
|
|
* @return bool
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Метод для получения всех юзеров
|
|
* @return array|\yii\db\ActiveRecord[]
|
|
*/
|
|
public function getAllUsers()
|
|
{
|
|
return $users = User::find()->all();
|
|
}
|
|
|
|
/**
|
|
* Метод создает 5 моделей текста, для actionCreate
|
|
* @return array
|
|
*/
|
|
public function newTexts()
|
|
{
|
|
$texts = [];
|
|
foreach (Text::getLanguages() as $lang) {
|
|
$text = new Text();
|
|
$text->language = $lang;
|
|
$texts[] = $text;
|
|
}
|
|
|
|
return $texts;
|
|
}
|
|
|
|
/**
|
|
* Метод для получения текстов по профилю
|
|
* @param $id
|
|
* @return array|\yii\db\ActiveRecord[]
|
|
*/
|
|
public function getTexts($id)
|
|
{
|
|
$textModels = Text::find()->where(['profile_id' => $id])->all();
|
|
|
|
return $textModels;
|
|
}
|
|
|
|
/**
|
|
* Метод для получения текста по профилю и языку
|
|
* @param $id
|
|
* @param $slug
|
|
* @return array|\yii\db\ActiveRecord[]
|
|
*/
|
|
public function getTextByLanguage($id, $slug)
|
|
{
|
|
return Text::find()->where(['language' => $slug, 'profile_id' => $id])->all();
|
|
}
|
|
|
|
/**
|
|
* @return self
|
|
*/
|
|
public static function run()
|
|
{
|
|
return new self();
|
|
}
|
|
} |