Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| CorpUploadController | |
0.00% |
0 / 61 |
|
0.00% |
0 / 3 |
306 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| upload | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
240 | |||
| delete | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\DigitalSign\Controllers; |
| 4 | |
| 5 | use BuyerKiosk\DigitalSign\UploadHandler; |
| 6 | use BuyerKiosk\DigitalSign\CorpSlide; |
| 7 | use BuyerKiosk\DigitalSign\Services\SlideTagService; |
| 8 | |
| 9 | class CorpUploadController extends \BuyerKiosk\Core\Controllers\BaseController { |
| 10 | private $log; |
| 11 | private $uploadDir; |
| 12 | public function __construct($app) |
| 13 | { |
| 14 | parent::__construct($app); |
| 15 | $this->log = new \KLogger($_ENV['LOG_DIR']."/upload_log.txt", \KLogger::DEBUG); |
| 16 | $this->uploadDir = "upload/digitalSign/corp/"; |
| 17 | $this->_app = $app; |
| 18 | } |
| 19 | |
| 20 | public function upload() { |
| 21 | $options = array( |
| 22 | "upload_dir" => $this->uploadDir, |
| 23 | "upload_url" => "/".$this->uploadDir, |
| 24 | "script_url" => "/api/upload-media/corp", |
| 25 | "accept_file_types" => "/\.(gif|jpe?g|png|mp4|webm)$/i", |
| 26 | "image_versions" => array( |
| 27 | "thumbs" => array( |
| 28 | "max_width" => 300, |
| 29 | "max_height" => 300, |
| 30 | ) |
| 31 | ) |
| 32 | ); |
| 33 | $handler = new UploadHandler($options); |
| 34 | $fileName = $handler->response['files'][0]->name; |
| 35 | $slideName = $this->_app->request->post('slideName')[0]; |
| 36 | $storeType = $this->_app->request->post('storeType')[0]; |
| 37 | $slide = new CorpSlide(); |
| 38 | $slide->slideName = $slideName; |
| 39 | $slide->fileName = $fileName; |
| 40 | $slide->storeType = $storeType; |
| 41 | $this->log->LogDebug("Store Type: ".$storeType); |
| 42 | $this->log->LogDebug("Slide: " . print_r($slide, true)); |
| 43 | if($handler->response['files'][0]->type == "image/jpeg" || $handler->response['files'][0]->type == "image/png" || $handler->response['files'][0]->type == "image/gif") { |
| 44 | $slide->type = 0; |
| 45 | $this->log->LogDebug("Slide type: " . $slide->type); |
| 46 | } else if($handler->response['files'][0]->type == "video/mp4" || $handler->response['files'][0]->type == "video/webm") { |
| 47 | $slide->type = 1; |
| 48 | $this->log->LogDebug("Slide type: " . $slide->type); |
| 49 | $slide->videoDuration = $handler->response['files'][0]->duration; |
| 50 | } |
| 51 | $this->log->LogDebug("Slide: " . print_r($slide, true)); |
| 52 | $saved = $slide->save(); |
| 53 | $slideId = $slide->id; // Get ID from object after save |
| 54 | |
| 55 | // Handle tags if provided (supports both Select2 array format and comma-separated string) |
| 56 | $slideTags = $this->_app->request->post('slideTags'); |
| 57 | $this->log->LogDebug("Raw slideTags input: " . print_r($slideTags, true)); |
| 58 | |
| 59 | if ($saved && $slideId && !empty($slideTags)) { |
| 60 | $tags = []; |
| 61 | |
| 62 | // Handle Select2 format: slideTags[] sends array of arrays with first element being array of selected values |
| 63 | if (is_array($slideTags)) { |
| 64 | // First element contains the tags for the first file |
| 65 | $firstFileTags = $slideTags[0] ?? []; |
| 66 | if (is_array($firstFileTags)) { |
| 67 | // Select2 multi-select sends array of values |
| 68 | $tags = array_filter(array_map('trim', $firstFileTags)); |
| 69 | } else if (is_string($firstFileTags) && !empty($firstFileTags)) { |
| 70 | // Fallback: comma-separated string |
| 71 | $tags = array_filter(array_map('trim', explode(',', $firstFileTags))); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (!empty($tags)) { |
| 76 | try { |
| 77 | global $db_name; |
| 78 | $db = dbConnectByName($db_name); |
| 79 | $tagService = new SlideTagService($db); |
| 80 | $tagService->addTags($slideId, $tags, 'corp'); |
| 81 | $this->log->LogDebug("Added tags to corp slide $slideId: " . implode(', ', $tags)); |
| 82 | } catch (\Exception $e) { |
| 83 | $this->log->LogError("Failed to add tags: " . $e->getMessage()); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | $thumbFileName = $handler->response['files'][0]->name; |
| 89 | $handler->response['files'][0]->thumbnailUrl = $this->uploadDir."thumbs/".urlencode($thumbFileName); |
| 90 | $handler->response['files'][0]->slideId = $slideId; |
| 91 | return $handler; |
| 92 | |
| 93 | } |
| 94 | public function delete() { |
| 95 | $options = array( |
| 96 | |
| 97 | ); |
| 98 | return $handler = new UploadHandler($options); |
| 99 | } |
| 100 | } |