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