198 lines
7.1 KiB
PHP
198 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use Log;
|
|
|
|
class ContactexportController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'contact';
|
|
$this->_module = 'contact_exports';
|
|
$this->_viewname = 'contactexports';
|
|
$this->_title = 'Contact Exports';
|
|
$this->_base_url = '/contactexport';
|
|
}
|
|
|
|
protected function lambdaResponseData($response, string $label = 'API')
|
|
{
|
|
if (!is_object($response)) {
|
|
throw new \RuntimeException($label . ' returned an invalid response.');
|
|
}
|
|
if (isset($response->body) && is_string($response->body)) {
|
|
$decoded = json_decode($response->body);
|
|
if (is_object($decoded)) {
|
|
$response = $decoded;
|
|
}
|
|
}
|
|
if (isset($response->status) && $response->status !== 'success') {
|
|
$message = !empty($response->message) && is_string($response->message)
|
|
? $response->message
|
|
: ($label . ' request failed.');
|
|
throw new \RuntimeException($message);
|
|
}
|
|
if (!isset($response->data)) {
|
|
throw new \RuntimeException($label . ' response is missing data.');
|
|
}
|
|
return $response->data;
|
|
}
|
|
|
|
public function list(Request $request)
|
|
{
|
|
try {
|
|
$response = LambdaRequest::signedrequest('get', $this->_service, $this->_module . '/get_all', []);
|
|
$data = $this->lambdaResponseData($response, 'Contact exports');
|
|
$items = $data->items ?? [];
|
|
if (is_object($items)) {
|
|
$items = array_values((array) $items);
|
|
}
|
|
if (!is_array($items)) {
|
|
$items = [];
|
|
}
|
|
|
|
foreach ($items as $item) {
|
|
if (isset($item->position_code)) {
|
|
$codes = $item->position_code;
|
|
if (is_array($codes)) {
|
|
$item->position_codes = implode(', ', $codes);
|
|
} elseif (is_object($codes)) {
|
|
$item->position_codes = implode(', ', array_values((array) $codes));
|
|
} else {
|
|
$item->position_codes = (string) $codes;
|
|
}
|
|
} else {
|
|
$item->position_codes = '';
|
|
}
|
|
}
|
|
|
|
$buttons = $this->get_list_buttons();
|
|
$buttons[] = ['title' => 'Refresh', 'link' => $this->_base_url . 's'];
|
|
|
|
return $this->view('list')->with([
|
|
'title' => $this->_title,
|
|
'fields' => $this->get_list_fields(),
|
|
'base_url' => $this->_base_url,
|
|
'items' => $items,
|
|
'buttons' => $buttons,
|
|
]);
|
|
} catch (\RuntimeException $e) {
|
|
Log::error('Contact export list failed: ' . $e->getMessage());
|
|
return redirect('/contacts')->withError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
protected function get_list_buttons()
|
|
{
|
|
return [['title' => 'Add New', 'link' => $this->_base_url . '/addnew']];
|
|
}
|
|
|
|
protected function get_list_fields()
|
|
{
|
|
return [
|
|
'actions' => [
|
|
'title' => 'Actions',
|
|
'buttons' => [
|
|
'delete',
|
|
'download' => ['icon' => 'cloud-download', 'href' => 'download_url'],
|
|
],
|
|
],
|
|
'position_codes' => ['title' => 'Position(s)', 'type' => 'text'],
|
|
'export_status' => ['title' => 'Contact Status', 'type' => 'text'],
|
|
'file_status' => ['title' => 'File Status', 'type' => 'text'],
|
|
'createdAt' => ['title' => 'Created Date', 'type' => 'datetime'],
|
|
];
|
|
}
|
|
|
|
protected function get_form_fields($type, $values)
|
|
{
|
|
$hierar = $this->getlambda('get', 'ums', 'hierarchy', 'dropdown');
|
|
$position_dd = ['all' => 'All positions'];
|
|
if (isset($hierar->data->items)) {
|
|
foreach ($hierar->data->items as $hier) {
|
|
$position_dd[$hier->code] = $hier->name . ' (' . $hier->code . ')';
|
|
}
|
|
}
|
|
|
|
return [
|
|
'position_code' => [
|
|
'title' => 'Position(s)',
|
|
'type' => 'multiselect',
|
|
'required' => true,
|
|
'values' => $position_dd,
|
|
'fullwidth' => true,
|
|
],
|
|
'export_status' => [
|
|
'title' => 'Contact Status',
|
|
'type' => 'select',
|
|
'required' => true,
|
|
'values' => ['active' => 'Active', 'disabled' => 'Disabled'],
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function _beforesave($type, $request)
|
|
{
|
|
$data = $request->except('_token');
|
|
$positionCodes = $data['position_code'] ?? [];
|
|
if (!is_array($positionCodes)) {
|
|
$positionCodes = [$positionCodes];
|
|
}
|
|
$positionCodes = array_values(array_filter($positionCodes, function ($code) {
|
|
return $code !== null && $code !== '';
|
|
}));
|
|
if (in_array('all', $positionCodes, true)) {
|
|
$positionCodes = ['all'];
|
|
}
|
|
$data['position_code'] = $positionCodes;
|
|
return $data;
|
|
}
|
|
|
|
public function save($type, $request)
|
|
{
|
|
if ($type == 'edit') {
|
|
$url = '/update';
|
|
} else {
|
|
$url = '/create';
|
|
}
|
|
$data = $this->_beforesave($type, $request);
|
|
try {
|
|
$response = LambdaRequest::signedrequest('post', $this->_service, $this->_module . $url, $data);
|
|
if ($response->statusCode == 200 && $response->status == 'success') {
|
|
return redirect($this->_base_url . 's')->withMessage(
|
|
'Export request created. Refresh the list in a few moments to download the file when ready.'
|
|
);
|
|
}
|
|
$message = isset($response->message) ? $response->message : 'Some error occurred!';
|
|
return redirect($this->_base_url . 's')->withError('Some Error occured! ' . $message);
|
|
} catch (\Exception $e) {
|
|
Log::error('Contact export save failed: ' . $e->getMessage());
|
|
return redirect($this->_base_url . '/addnew')->withError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
try {
|
|
$response = LambdaRequest::signedrequest(
|
|
'post',
|
|
$this->_service,
|
|
$this->_module . '/delete?id=' . urlencode($id),
|
|
[]
|
|
);
|
|
if ($response->statusCode == 200 && $response->status == 'success') {
|
|
return redirect($this->_base_url . 's')->withMessage('Successfully Marked Deleted !!!');
|
|
}
|
|
$message = isset($response->message) ? $response->message : 'Some error occurred!';
|
|
return redirect($this->_base_url . 's')->withError('Some Error occured! ' . $message);
|
|
} catch (\Exception $e) {
|
|
Log::error('Contact export delete failed: ' . $e->getMessage());
|
|
return redirect($this->_base_url . 's')->withError($e->getMessage());
|
|
}
|
|
}
|
|
|
|
}
|