| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\Api\Helper; |
| 7 |
use FluentCart\Api\Confirmation; |
| 8 |
use FluentCart\Api\StoreSettings; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
use FluentCart\Framework\Http\Request\Request; |
| 11 |
use FluentCart\App\Helpers\EditorShortCodeHelper; |
| 12 |
use FluentCart\App\Http\Requests\FluentMetaRequest; |
| 13 |
|
| 14 |
class SettingsController extends Controller |
| 15 |
{ |
| 16 |
public function getStore(Request $request, StoreSettings $storeSettings) |
| 17 |
{ |
| 18 |
$data = $storeSettings->get(); |
| 19 |
|
| 20 |
if (empty($data['company_name'])) { |
| 21 |
$data['company_name'] = Arr::get($data, 'store_name', ''); |
| 22 |
} |
| 23 |
|
| 24 |
$tab = $request->get('settings_name'); |
| 25 |
$currenttabFields = Arr::get($storeSettings->fields(), 'setting_tabs.schema.' . $tab); |
| 26 |
|
| 27 |
if (!App::isProActive()) { |
| 28 |
$data['show_email_footer'] = 'yes'; |
| 29 |
} |
| 30 |
|
| 31 |
return [ |
| 32 |
'settings' => $data, |
| 33 |
'fields' => [ |
| 34 |
$tab => $currenttabFields |
| 35 |
] |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
public function saveStore(FluentMetaRequest $request, StoreSettings $storeSettings) |
| 40 |
{ |
| 41 |
$storeLogo = $request->get('store_logo'); |
| 42 |
if($storeLogo){ |
| 43 |
$storeLogo = [ |
| 44 |
'id' => sanitize_text_field(Arr::get($storeLogo, 'id')), |
| 45 |
'url' => sanitize_url(Arr::get($storeLogo, 'url')), |
| 46 |
'title' => sanitize_text_field(Arr::get($storeLogo, 'title')), |
| 47 |
]; |
| 48 |
}else{ |
| 49 |
$storeLogo = ''; |
| 50 |
} |
| 51 |
try { |
| 52 |
// $data = $request->getSafe($request->sanitize()); |
| 53 |
// $data['store_logo'] = $storeLogo; |
| 54 |
// $data = array_merge( |
| 55 |
// $storeSettings->get(), |
| 56 |
// $data |
| 57 |
// ); |
| 58 |
|
| 59 |
$sanitizeMap = $request->sanitize(); |
| 60 |
$data = $request->all(); |
| 61 |
$sanitizedData = []; |
| 62 |
|
| 63 |
|
| 64 |
foreach ($data as $key => $value) { |
| 65 |
$sanitizer = Arr::get($sanitizeMap, $key); |
| 66 |
if (!$sanitizer) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
$sanitizedData[$key] = call_user_func($sanitizer, $value); |
| 70 |
} |
| 71 |
|
| 72 |
$data = array_merge( |
| 73 |
$storeSettings->get(), |
| 74 |
$sanitizedData |
| 75 |
); |
| 76 |
|
| 77 |
// $data = Arr::get($data, 'settings', []); |
| 78 |
// $frontendTheme = $request->get('settings.frontend_theme'); |
| 79 |
$frontendTheme = $request->get('frontend_theme'); |
| 80 |
|
| 81 |
// Check if frontendTheme is an array before using it in foreach |
| 82 |
if (is_array($frontendTheme)) { |
| 83 |
foreach ($frontendTheme as $key => $value) { |
| 84 |
$data['frontend_theme'][$key] = sanitize_hex_color($value); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
$data['require_logged_in'] = 'no'; |
| 89 |
|
| 90 |
|
| 91 |
$saved = $storeSettings->save($data); |
| 92 |
|
| 93 |
|
| 94 |
return $this->sendSuccess([ |
| 95 |
'data' => $saved |
| 96 |
], 423); |
| 97 |
} catch (\Exception $e) { |
| 98 |
$this->sendError([ |
| 99 |
'message' => $e->getMessage() |
| 100 |
], 423); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function getConfirmation() |
| 105 |
{ |
| 106 |
$confirmations = new Confirmation(); |
| 107 |
return array( |
| 108 |
'settings' => $confirmations->get(null, [ |
| 109 |
'confirmation_page_id' => (new StoreSettings())->getReceiptPageId() |
| 110 |
]), |
| 111 |
'fields' => $confirmations->fields() |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
public function saveConfirmation(Request $request) |
| 116 |
{ |
| 117 |
try { |
| 118 |
return (new Confirmation())->save($request->settings); |
| 119 |
} catch (\Exception $e) { |
| 120 |
$this->sendError([ |
| 121 |
'message' => $e->getMessage() |
| 122 |
], 423); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public function getShortcode(): array |
| 127 |
{ |
| 128 |
return [ |
| 129 |
'data' => EditorShortCodeHelper::getEmailNotificationShortcodes() |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
public function getPermissions() |
| 134 |
{ |
| 135 |
try { |
| 136 |
$roles = Helper::getPermittedRoles(); |
| 137 |
} catch (\Exception $e) { |
| 138 |
return $this->sendError([ |
| 139 |
'message' => $e->getMessage() |
| 140 |
], 423); |
| 141 |
} |
| 142 |
|
| 143 |
return array('roles' => $roles); |
| 144 |
} |
| 145 |
|
| 146 |
public function savePermissions(Request $request) |
| 147 |
{ |
| 148 |
try { |
| 149 |
return Helper::savePermissions($request->capability); |
| 150 |
} catch (\Exception $e) { |
| 151 |
$this->sendError([ |
| 152 |
'message' => $e->getMessage() |
| 153 |
], 423); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|