You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

78 lines
2.1 KiB

<?php
namespace common\modules\forms\services;
use common\modules\forms\entities\Form;
use common\modules\forms\forms\FormForm;
use common\modules\forms\repositories\FormRepository;
use common\modules\forms\widgets\FormGenerator;
use yii\helpers\Json;
class FormManageService
{
private $repository;
public function __construct(FormRepository $repository)
{
$this->repository = $repository;
}
public function clearJsonLabels($json)
{
$new_data = [];
$data = Json::decode($json, true);
foreach ($data as $item) {
if (isset($item['label'])) {
$item['label'] = str_replace( '<br>', '', $item['label'] );
}
$new_data[] = $item;
}
return Json::encode($new_data, JSON_UNESCAPED_UNICODE);
}
public function create(FormForm $form): Form
{
$form = Form::create(
$form->name,
$this->clearJsonLabels($form->data),
$form->subject,
$form->from,
$form->reply,
$form->return,
$form->complete_text,
$form->complete_page_id,
$form->status,
$form->captcha
);
$this->repository->save($form);
FormGenerator::generateFormClass($form->id, Json::decode($form->data, true));
FormGenerator::generateFormView($form->id, Json::decode($form->data, true));
return $form;
}
public function edit($id, FormForm $form): void
{
$form_entity = $this->repository->get($id);
$form_entity->edit(
$form->name,
$this->clearJsonLabels($form->data),
$form->subject,
$form->from,
$form->reply,
$form->return,
$form->complete_text,
$form->complete_page_id,
$form->status,
$form->captcha
);
$this->repository->save($form_entity);
FormGenerator::generateFormClass($id, Json::decode($form->data, true));
FormGenerator::generateFormView($id, Json::decode($form->data, true));
}
public function remove($id): void
{
$form = $this->repository->get($id);
$this->repository->remove($form);
}
}