diff --git a/Admin/Install/SearchCommands.php b/Admin/Install/SearchCommands.php
index 2b574be..a927feb 100644
--- a/Admin/Install/SearchCommands.php
+++ b/Admin/Install/SearchCommands.php
@@ -29,4 +29,14 @@ return [
],
],
],
+ '^:tag (\?.*$|$)' => [
+ [
+ 'dest' => '\Modules\Media\Controller\SearchController:searchTag',
+ 'verb' => RouteVerb::ANY,
+ 'permission' => [
+ 'module' => SearchController::NAME,
+ 'type' => PermissionType::READ,
+ ],
+ ],
+ ],
];
diff --git a/Controller/ApiController.php b/Controller/ApiController.php
index 1c50a16..95e7039 100755
--- a/Controller/ApiController.php
+++ b/Controller/ApiController.php
@@ -32,10 +32,10 @@ use Modules\Media\Models\PathSettings;
use Modules\Media\Models\PermissionCategory;
use Modules\Media\Models\Reference;
use Modules\Media\Models\ReferenceMapper;
-use Modules\Media\Models\Report;
use Modules\Media\Models\UploadFile;
use Modules\Media\Models\UploadStatus;
use Modules\Media\Theme\Backend\Components\Media\ElementView;
+use Modules\Messages\Models\EmailMapper;
use phpOMS\Account\PermissionType;
use phpOMS\Ai\Ocr\Tesseract\TesseractOcr;
use phpOMS\Application\ApplicationAbstract;
@@ -67,6 +67,63 @@ use phpOMS\Views\View;
*/
final class ApiController extends Controller
{
+ /**
+ * Api method to create tag
+ *
+ * @param RequestAbstract $request Request
+ * @param ResponseAbstract $response Response
+ * @param array $data Generic data
+ *
+ * @return void
+ *
+ * @api
+ *
+ * @since 1.0.0
+ */
+ public function apiMediaEmailSend(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
+ {
+ $email = $request->getDataString('email');
+
+ $media = $data['media'] ?? MediaMapper::get()
+ ->where('id', (int) $request->getData('id'))
+ ->execute();
+
+ /** @var \Model\Setting $template */
+ $template = $this->app->appSettings->get(
+ names: (string) $request->getDataString('template')
+ );
+
+ $handler = $this->app->moduleManager->get('Admin', 'Api')->setUpServerMailHandler();
+
+ $mail = EmailMapper::get()
+ ->with('l11n')
+ ->where('id', $template)
+ ->where('l11n/language', $response->header->l11n->language)
+ ->execute();
+
+ $status = false;
+ if ($mail->id !== 0) {
+ $status = $this->app->moduleManager->get('Admin', 'Api')->setupEmailDefaults($mail, $response->header->l11n->language);
+ }
+
+ $mail->addTo($email);
+ $mail->addAttachment($media->getAbsolutePath(), $media->name);
+
+ if ($status) {
+ $status = $handler->send($mail);
+ }
+
+ if (!$status) {
+ \phpOMS\Log\FileLogger::getInstance()->error(
+ \phpOMS\Log\FileLogger::MSG_FULL, [
+ 'message' => 'Couldn\'t send bill media: ' . $media->id,
+ 'line' => __LINE__,
+ 'file' => self::class,
+ ]
+ );
+ }
+ }
+
/**
* Api method to upload media file.
*
@@ -920,34 +977,42 @@ final class ApiController extends Controller
$status = \is_dir($physicalPath) ? true : \mkdir($physicalPath, 0755, true);
}
- $path = \trim($path, '/');
- $paths = \explode('/', $path);
- $tempPaths = $paths;
- $length = \count($paths);
+ $virtualPath = \trim($path, '/');
+ $virtualPaths = \explode('/', $virtualPath);
+ $tempVirtualPaths = $virtualPaths;
+ $length = \count($virtualPaths);
/** @var Collection $parentCollection */
$parentCollection = null;
- $temp = '';
+ $virtual = '';
+ $real = '';
+ $newVirtual = '';
+
for ($i = $length; $i > 0; --$i) {
- $temp = '/' . \implode('/', $tempPaths);
+ $virtual = '/' . \implode('/', $tempVirtualPaths);
/** @var Collection $parentCollection */
- $parentCollection = CollectionMapper::getParentCollection($temp)->execute();
+ $parentCollection = CollectionMapper::getParentCollection($virtual)->execute();
+
if ($parentCollection->id > 0) {
+ $real = $parentCollection->getPath();
+
break;
}
- \array_pop($tempPaths);
+ $newVirtual = \array_pop($tempVirtualPaths) . '/' . $newVirtual;
}
for (; $i < $length; ++$i) {
/* Create collection */
$childCollection = new Collection();
- $childCollection->name = $paths[$i];
+ $childCollection->name = $virtualPaths[$i];
$childCollection->createdBy = new NullAccount($account);
- $childCollection->setVirtualPath('/'. \ltrim($temp, '/'));
- $childCollection->setPath('/Modules/Media/Files' . $temp);
+ $childCollection->setVirtualPath('/'. \ltrim($virtual, '/'));
+
+ // We assume that the new path is real path of the first found parent directory + the new virtual path
+ $childCollection->setPath($real . '/' . \ltrim($newVirtual, '/'));
$this->createModel($account, $childCollection, CollectionMapper::class, 'collection', '127.0.0.1');
$this->createModelRelation(
@@ -961,7 +1026,7 @@ final class ApiController extends Controller
);
$parentCollection = $childCollection;
- $temp .= '/' . $paths[$i];
+ $virtual .= '/' . $virtualPaths[$i];
}
return $parentCollection;
@@ -1077,6 +1142,13 @@ final class ApiController extends Controller
$media->isAbsolute = false;
$media->setVirtualPath(\dirname($path));
$media->setPath('/' . \ltrim($path, '\\/'));
+ } else {
+ $media = MediaMapper::getAll()
+ ->where('virtualPath', $path)
+ ->limit(1)
+ ->execute();
+
+ $filePath = $media->getAbsolutePath();
}
}
@@ -1210,7 +1282,7 @@ final class ApiController extends Controller
$response->endAllOutputBuffering(); // for large files
}
- if (($type = $request->getDataString('type')) === null) {
+ if (\in_array($type = $request->getDataString('type'), [null, 'download', 'raw', 'bin'])) {
$view->setTemplate('/Modules/Media/Theme/Api/render');
} elseif ($type === 'html') {
$head = new Head();
diff --git a/Controller/SearchController.php b/Controller/SearchController.php
index 5c048bd..fd3480f 100644
--- a/Controller/SearchController.php
+++ b/Controller/SearchController.php
@@ -30,6 +30,59 @@ use phpOMS\System\MimeType;
*/
final class SearchController extends Controller
{
+ /**
+ * Api method to search for tags
+ *
+ * @param RequestAbstract $request Request
+ * @param ResponseAbstract $response Response
+ * @param array $data Generic data
+ *
+ * @return void
+ *
+ * @api
+ *
+ * @since 1.0.0
+ */
+ public function searchTag(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
+ {
+ $search = $request->getDataString('search') ?? '';
+
+ $searchIdStartPos = \stripos($search, ':');
+ $patternStartPos = $searchIdStartPos === false
+ ? -1
+ : \stripos($search, ' ', $searchIdStartPos);
+
+ $pattern = \substr($search, $patternStartPos + 1);
+
+ /** @var \Modules\Media\Models\Media[] $media */
+ $media = MediaMapper::getAll()
+ ->with('tags')
+ ->with('tags/title')
+ ->where('tags/title/language', $response->header->l11n->language)
+ ->where('tags/title/content', $pattern)
+ ->sort('createdAt', OrderType::DESC)
+ ->limit(8)
+ ->execute();
+
+ $results = [];
+ foreach ($media as $file) {
+ $results[] = [
+ 'title' => $file->name . ' (' . $file->extension . ')',
+ 'summary' => '',
+ 'link' => '{/base}/media/view?id=' . $file->id,
+ 'account' => '',
+ 'createdAt' => $file->createdAt,
+ 'image' => '',
+ 'tags' => $file->tags,
+ 'type' => 'list_links',
+ 'module' => 'Media',
+ ];
+ }
+
+ $response->header->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true);
+ $response->add($request->uri->__toString(), $results);
+ }
+
/**
* Api method to search for tags
*
diff --git a/Theme/Api/render.tpl.php b/Theme/Api/render.tpl.php
index 66d62ae..ffd582f 100755
--- a/Theme/Api/render.tpl.php
+++ b/Theme/Api/render.tpl.php
@@ -17,6 +17,23 @@ use Modules\Media\Models\NullMedia;
/** @var \Modules\Media\Models\Media $media */
$media = $this->media ?? new NullMedia();
-$fp = \fopen(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath(), 'r');
-\fpassthru($fp);
-\fclose($fp);
+if (\is_file($media->getAbsolutePath())) {
+ $fp = \fopen($media->getAbsolutePath(), 'r');
+ if ($fp !== false) {
+ \fpassthru($fp);
+ \fclose($fp);
+ }
+} elseif (\is_dir($media->getAbsolutePath())) {
+ \phpOMS\Utils\IO\Zip\Zip::pack(
+ $media->getAbsolutePath(),
+ $tmp = \tempnam(\sys_get_temp_dir(), 'oms_tmp_')
+ );
+
+ $fp = \fopen($tmp, 'r');
+ if ($fp !== false) {
+ \fpassthru($fp);
+ \fclose($fp);
+
+ \unlink($tmp);
+ }
+}
diff --git a/Theme/Backend/media-collection-create.tpl.php b/Theme/Backend/media-collection-create.tpl.php
index 343450a..41fac17 100755
--- a/Theme/Backend/media-collection-create.tpl.php
+++ b/Theme/Backend/media-collection-create.tpl.php
@@ -33,21 +33,29 @@ use phpOMS\Uri\UriFactory;
= $this->getHtml('CreateCollection'); ?>