/** * Ajax action method * * @param int $lid * @return void */ public function ajax($lid) { $library = new Model\MediaLibrary(); $library->getById($lid); $json = []; $categories = []; if (class_exists('Phire\\Categories\\Model\\Category') && isset($_POST['categories'])) { $categories = explode(',', $_POST['categories']); } if (!isset($library->id)) { $json['error'] = 'That library was not found.'; } else { $settings = $library->getSettings(); $folder = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder; if (!file_exists($folder)) { $library->createFolder($library->folder); } $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); foreach ($_FILES as $key => $file) { if (!empty($file['name'])) { $json['id'] = substr($key, strrpos($key, '_') + 1); if (!$upload->test($file)) { $json['error'] = $upload->getErrorMessage(); } else { $media = new Model\Media(); $media->save($file, ['library_id' => $lid]); if (count($categories) > 0) { foreach ($categories as $category) { $catItem = new \Phire\Categories\Table\CategoryItems(['category_id' => (int) $category, 'content_id' => null, 'media_id' => $media->id, 'order' => 0]); $catItem->save(); } } } } } } $this->response->setBody(json_encode($json, JSON_PRETTY_PRINT)); $this->send(200, ['Content-Type' => 'application/json']); }