Rapport-Laravel/app/Http/Controllers/VideoCustomizationControlle...

314 lines
17 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\LambdaRequest;
use Auth;
class VideoCustomizationController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->_service = 'vgreet';
$this->_module = 'video';
$this->_viewname = 'videocustomization';
$this->_title = 'Video Customization';
$this->_base_url = '/videocustomization';
}
protected function get_list_fields(){
return [
'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete' ] ],
'title' => ['title' => 'Title', 'type' => 'text'],
'customization_type' => ['title' => 'Customization Type', 'type' => 'text'],
'status' => ['title' => 'Status', 'type' => 'select'],
];
}
protected function get_customization_types() {
return [
'video_frame' => [
'fields' => [
'orientation' => ['title' => 'Orientation', 'type' => 'select', 'required' => true, 'values' => ['portrait' => 'Portrait', 'landscape' => 'Landscape', 'both' => 'Both'], 'onchange' => 'showOrientationFields', 'fullwidth'=>'yes'],
'portrait_video_width' => ['title' => 'Portrait Video Width', 'type' => 'number', 'required' => false],
'portrait_video_height' => ['title' => 'Portrait Video Height', 'type' => 'number', 'required' => false],
'landscape_video_width' => ['title' => 'Landscape Video Width', 'type' => 'number', 'required' => false],
'landscape_video_height' => ['title' => 'Landscape Video Height', 'type' => 'number', 'required' => false],
'portrait_frame_image' => ['title' => 'Portrait Frame Image', 'type' => 'image'],
'landscape_frame_image' => ['title' => 'Landscape Frame Image', 'type' => 'image'],
'personalization' => ['title' => 'Personalization (JSON)', 'type' => 'textarea', 'required' => false],
],
'title' => 'Video Frame'
],
// Add more customization types here in the future
// 'Watermark' => [
// 'fields' => [
// 'watermark_image' => ['title' => 'Watermark Image', 'type' => 'image'],
// 'position' => ['title' => 'Position', 'type' => 'select', 'values' => [...]],
// ]
// ],
];
}
protected function get_form_fields($type, $values){
// Extract customization params if editing
$customizationParams = [];
$currentCustomizationType = $values->customization_type ?? '';
if(isset($values->customization_params)) {
if(is_string($values->customization_params)) {
$customizationParams = json_decode($values->customization_params, true);
} elseif(is_object($values->customization_params)) {
// Recursively convert object to array
$customizationParams = json_decode(json_encode($values->customization_params), true);
} elseif(is_array($values->customization_params)) {
$customizationParams = $values->customization_params;
}
// If customization_type is empty but we have a type in params, use it
if(empty($currentCustomizationType) && isset($customizationParams['type'])) {
$currentCustomizationType = $customizationParams['type'];
}
}
// Get available customization types
$customizationTypes = $this->get_customization_types();
$customizationTypeOptions = [];
foreach($customizationTypes as $typeKey => $typeConfig) {
$customizationTypeOptions[$typeKey] = $typeConfig['title'];
}
// Get orientation value for video_frame type
$orientationValue = 'both';
if($currentCustomizationType == 'video_frame' && isset($customizationParams['orientation'])) {
$orientationValue = is_object($customizationParams['orientation']) ? (string)$customizationParams['orientation'] : $customizationParams['orientation'];
}
$fields = [
'title' => ['title' => 'Title', 'type' => 'text', 'required' => true, 'fullwidth'=>true],
'customization_type' => ['title' => 'Customization Type', 'type' => 'select', 'required' => true, 'values' => $customizationTypeOptions, 'value' => $currentCustomizationType, 'onchange' => 'showCustomizationFields'],
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled']],
'orientation' => [
'title' => 'Orientation',
'type' => 'select',
'required' => false,
'values' => ['portrait' => 'Portrait', 'landscape' => 'Landscape', 'both' => 'Both'],
'value' => $orientationValue,
'onchange' => 'showOrientationFields',
'class' => 'customization-field video-frame-field',
'style' => ($currentCustomizationType == 'video_frame') ? '' : 'display:none;'
],
];
// Add fields for each customization type
foreach($customizationTypes as $typeKey => $typeConfig) {
$typeClass = strtolower(str_replace('_', '-', $typeKey)) . '-field';
$isVisible = ($currentCustomizationType == $typeKey);
foreach($typeConfig['fields'] as $fieldKey => $fieldConfig) {
// Skip orientation as it's already added above
if($fieldKey == 'orientation') {
continue;
}
// Get value from customization params
$fieldValue = '';
if($typeKey == 'video_frame') {
switch($fieldKey) {
case 'portrait_video_width':
$fieldValue = '';
if(isset($customizationParams['portrait_video_dimensions'])) {
$dims = $customizationParams['portrait_video_dimensions'];
if(is_array($dims) && isset($dims['width'])) {
$fieldValue = $dims['width'];
} elseif(is_object($dims) && isset($dims->width)) {
$fieldValue = $dims->width;
}
}
break;
case 'portrait_video_height':
$fieldValue = '';
if(isset($customizationParams['portrait_video_dimensions'])) {
$dims = $customizationParams['portrait_video_dimensions'];
if(is_array($dims) && isset($dims['height'])) {
$fieldValue = $dims['height'];
} elseif(is_object($dims) && isset($dims->height)) {
$fieldValue = $dims->height;
}
}
break;
case 'landscape_video_width':
$fieldValue = '';
if(isset($customizationParams['landscape_video_dimensions'])) {
$dims = $customizationParams['landscape_video_dimensions'];
if(is_array($dims) && isset($dims['width'])) {
$fieldValue = $dims['width'];
} elseif(is_object($dims) && isset($dims->width)) {
$fieldValue = $dims->width;
}
}
break;
case 'landscape_video_height':
$fieldValue = '';
if(isset($customizationParams['landscape_video_dimensions'])) {
$dims = $customizationParams['landscape_video_dimensions'];
if(is_array($dims) && isset($dims['height'])) {
$fieldValue = $dims['height'];
} elseif(is_object($dims) && isset($dims->height)) {
$fieldValue = $dims->height;
}
}
break;
case 'portrait_frame_image':
case 'landscape_frame_image':
$fieldValue = '';
if(isset($customizationParams[$fieldKey])) {
$fieldValue = is_object($customizationParams[$fieldKey]) ? (string)$customizationParams[$fieldKey] : $customizationParams[$fieldKey];
}
break;
case 'personalization':
$fieldValue = '';
if(isset($customizationParams['personalization']) && !empty($customizationParams['personalization'])) {
$fieldValue = json_encode($customizationParams['personalization'], JSON_PRETTY_PRINT);
}
break;
default:
$fieldValue = '';
if(isset($customizationParams[$fieldKey])) {
$fieldValue = is_object($customizationParams[$fieldKey]) ? (string)$customizationParams[$fieldKey] : $customizationParams[$fieldKey];
}
}
}
$fields[$fieldKey] = array_merge($fieldConfig, [
'value' => $fieldValue,
'class' => 'customization-field ' . $typeClass,
'style' => $isVisible ? '' : 'display:none;'
]);
}
}
return $fields;
}
protected function _beforesave($type, $request) {
try {
$data = $request->except('_token');
//echo "<pre>"; print_r($data); exit;
// Only process customization params if customization_type is selected
if(!empty($data['customization_type'])) {
$customizationTypes = $this->get_customization_types();
$selectedType = $data['customization_type'];
if(isset($customizationTypes[$selectedType])) {
$typeConfig = $customizationTypes[$selectedType];
$customizationParams = ['type' => $selectedType];
$fieldsToRemove = [];
// Process fields based on customization type
if($selectedType == 'video_frame') {
// Handle image uploads
try {
if ($request->hasFile('portrait_frame_image') && $request->file('portrait_frame_image')->isValid()) {
$uploadedPath = $this->_uploadMedia($request, 'portrait_frame_image', 'vgreet/frames');
if (!empty($uploadedPath)) {
$data['portrait_frame_image'] = $uploadedPath;
} else {
\Log::warning('Portrait frame image upload returned empty path');
// Keep existing value if upload failed
$data['portrait_frame_image'] = $data['portrait_frame_image'] ?? '';
}
} else {
// Keep existing value if not uploading new file
$data['portrait_frame_image'] = $data['portrait_frame_image'] ?? '';
}
} catch (\Exception $e) {
\Log::error('Portrait frame image upload exception: ' . $e->getMessage());
$data['portrait_frame_image'] = $data['portrait_frame_image'] ?? '';
}
try {
if ($request->hasFile('landscape_frame_image') && $request->file('landscape_frame_image')->isValid()) {
$uploadedPath = $this->_uploadMedia($request, 'landscape_frame_image', 'vgreet/frames');
if (!empty($uploadedPath)) {
$data['landscape_frame_image'] = $uploadedPath;
} else {
\Log::warning('Landscape frame image upload returned empty path');
// Keep existing value if upload failed
$data['landscape_frame_image'] = $data['landscape_frame_image'] ?? '';
}
} else {
// Keep existing value if not uploading new file
$data['landscape_frame_image'] = $data['landscape_frame_image'] ?? '';
}
} catch (\Exception $e) {
\Log::error('Landscape frame image upload exception: ' . $e->getMessage());
$data['landscape_frame_image'] = $data['landscape_frame_image'] ?? '';
}
// Store portrait and landscape video dimensions separately
$customizationParams['portrait_video_dimensions'] = [
'width' => (int)($data['portrait_video_width'] ?? 0),
'height' => (int)($data['portrait_video_height'] ?? 0),
];
$customizationParams['landscape_video_dimensions'] = [
'width' => (int)($data['landscape_video_width'] ?? 0),
'height' => (int)($data['landscape_video_height'] ?? 0),
];
$customizationParams['portrait_frame_image'] = $data['portrait_frame_image'] ?? '';
$customizationParams['landscape_frame_image'] = $data['landscape_frame_image'] ?? '';
$customizationParams['orientation'] = $data['orientation'] ?? 'both';
// Handle personalization JSON decoding with error handling
$personalization = [];
if (isset($data['personalization']) && !empty($data['personalization'])) {
$decoded = json_decode($data['personalization'], true);
if (json_last_error() === JSON_ERROR_NONE) {
$personalization = $decoded;
}
}
$customizationParams['personalization'] = $personalization;
// Mark fields for removal
$fieldsToRemove = ['portrait_video_width', 'portrait_video_height', 'landscape_video_width', 'landscape_video_height', 'portrait_frame_image', 'landscape_frame_image', 'orientation', 'personalization'];
}
// Add more customization type processing here in the future
// elseif($selectedType == 'Watermark') {
// // Process watermark fields
// }
$data['customization_params'] = $customizationParams;
// Remove fields that shouldn't be sent to Lambda
foreach($fieldsToRemove as $field) {
unset($data[$field]);
}
}
}
$data['is_active'] = ($data['status'] ?? 'pending') == 'active';
// Debug: uncomment to see final data
return $data;
} catch (\Exception $e) {
// Log the error and return basic data structure
\Log::error('VideoCustomizationController _beforesave error: ' . $e->getMessage());
\Log::error('Stack trace: ' . $e->getTraceAsString());
// Return minimal data to prevent blank page
$data = $request->except('_token');
$data['is_active'] = ($data['status'] ?? 'pending') == 'active';
return $data;
}
}
protected function _beforeform($type, $values) {
//echo "<pre>"; print_r($values); exit;
return $values;
}
}