125 lines
5.1 KiB
PHP
125 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use DB;
|
|
|
|
class FormpersonalizationController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'form';
|
|
$this->_module = 'personalization';
|
|
$this->_viewname = 'formpersonalization';
|
|
$this->_title = 'Form Personalization';
|
|
$this->_base_url = '/formpersonalization';
|
|
}
|
|
|
|
public function list(Request $request) {
|
|
$entity_id = $request->input('form', $request->session()->get('entity_id'));
|
|
if($entity_id === 0) {
|
|
return redirect('/formentitys')->withError('Form not found!');
|
|
}
|
|
$request->session()->put('entity_id', $entity_id);
|
|
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module.'/get_all', ['entity_id' => $entity_id]);
|
|
//echo '<pre>'; print_r($response); exit;
|
|
$fields = $this->get_list_fields();
|
|
return $this->view('list')->with([
|
|
'title' => $this->_title,
|
|
'fields' => $fields,
|
|
'base_url' => $this->_base_url,
|
|
'items' => $response->data->items,
|
|
'buttons' => $this->get_list_buttons()
|
|
]);
|
|
}
|
|
|
|
protected function get_list_buttons() {
|
|
return [ ['title' => 'Add New', 'link' => $this->_base_url.'/addnew' ], ['title' => 'Back to Forms', 'link' => '/formentitys'] ];
|
|
}
|
|
|
|
protected function get_list_fields(){
|
|
return [
|
|
'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete' ] ],
|
|
'icon' => ['title' => 'Icon', 'type' => 'image'],
|
|
'title' => ['title' => 'Question', 'type' => 'text'],
|
|
'status' => ['title' => 'Status', 'type' => 'select'],
|
|
];
|
|
}
|
|
|
|
protected function get_form_fields($type, $values){
|
|
$forms = $this->getlambda('get', 'form', 'formquestion', 'get_all', ['entity_id' => $this->_request->session()->get('entity_id') ]);
|
|
//echo '<pre>'; print_r($forms); exit;
|
|
$forms_dd = [];
|
|
$questions_dd = [];
|
|
foreach($forms->data->items as $form){
|
|
$forms_dd[$form->id] = $form->question;
|
|
$questions_dd[$form->code] = $form->question;
|
|
}
|
|
//echo '<pre>'; print_r($questions_dd); exit;
|
|
return [
|
|
'title' => ['title' => 'Title', 'type' => 'text', 'required' => true, 'fullwidth' => true],
|
|
'questions' => ['title' => 'Select 1 or Multiple questions (to be included in this personalization)', 'type' => 'checkbox', 'values' => $forms_dd, 'required' => true],
|
|
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
|
|
|
|
'icon' => ['title' => 'Icon', 'type' => 'image', 'required' => true ],
|
|
//'template' => ['title' => 'Template File', 'type' => 'image', 'required' => true ],
|
|
//'positions' => ['title' => 'Positions', 'type' => 'textarea', 'required' => true, 'fullwidth' => true],
|
|
'mappings' => ['title' => 'Template and Positions', 'type' => 'mapping', 'fullwidth' => true, 'values' => $questions_dd],
|
|
];
|
|
}
|
|
|
|
protected function _beforesave($type, $request) {
|
|
$data = $request->except('_token');
|
|
if( $request->file('template') ) {
|
|
$data['template'] = $this->_uploadMedia($request, 'template', 'personalization');
|
|
}
|
|
if ($request->file('icon')) {
|
|
$data['icon'] = $this->_uploadMedia($request, 'icon', 'personalization');
|
|
}
|
|
$data['entity_id'] = $request->session()->get('entity_id');
|
|
$templates = [];
|
|
if ($request->file('templates')) {
|
|
foreach ($request->file('templates') as $k => $file) {
|
|
$templates[$k] = $this->_uploadUploadedFile($file, 'personalization');
|
|
}
|
|
}
|
|
$data['templates'] = $templates;
|
|
foreach($data['positions'] as $k => $pos) {
|
|
if(!isset($templates[$k])) {
|
|
$data['templates'][$k] = $data['templates_existing'][$k];
|
|
}
|
|
}
|
|
ksort($data['templates']);
|
|
ksort($data['positions']);
|
|
$data['templates'] = array_values($data['templates']);
|
|
$data['positions'] = array_values($data['positions']);
|
|
|
|
unset($data['templates_existing']);
|
|
//echo '<pre>'; print_r($data); exit;
|
|
return $data;
|
|
}
|
|
|
|
protected function _beforeform($type, $values) {
|
|
if($type == 'edit') {
|
|
$values->templates = isset($values->templates) ? (array)$values->templates : [];
|
|
$values->positions = (array)$values->positions;
|
|
}
|
|
//echo '<pre>'; print_r($values); exit;
|
|
return $values;
|
|
}
|
|
|
|
protected function _uploadUploadedFile($file, $folder = 'personalization')
|
|
{
|
|
$path = '';
|
|
try {
|
|
$storedPath = $file->store($folder, 's3');
|
|
$path = rtrim(env('AWS_URL', 'https://did94hhhcgq1d.cloudfront.net'), '/') . '/' . $storedPath;
|
|
} catch (\Exception $e) {
|
|
\Log::debug($e->getMessage());
|
|
}
|
|
return $path;
|
|
}
|
|
}
|