PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Controllers / Ajax / MediaLibraryAjaxController.php

MediaLibraryAjaxController.php in Depicter — Popup & Slider Builder trunk, at app/src/Controllers/Ajax/MediaLibraryAjaxController.php

132 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Controllers\Ajax;
3
4
5 use Depicter\Services\MediaLibraryService;
6 use Depicter\Utility\Sanitize;
7 use Psr\Http\Message\RequestInterface;
8 use Psr\Http\Message\ResponseInterface;
9
10
11 class MediaLibraryAjaxController
12 {
13
14 /**
15 * @var MediaLibraryService
16 */
17 private $mediaLibraryService;
18
19
20 public function __construct()
21 {
22 $this->mediaLibraryService = \Depicter::app()->mediaLibrary();
23 }
24
25 /**
26 * Get search result of images from media library
27 *
28 * @param RequestInterface $request
29 * @param string $view
30 *
31 * @return ResponseInterface
32 */
33 public function images( RequestInterface $request, $view )
34 {
35 return $this->query( $request, $view, 'photos');
36 }
37
38 /**
39 * Get search result of videos from media library
40 *
41 * @param RequestInterface $request
42 * @param string $view
43 *
44 * @return ResponseInterface
45 */
46 public function videos( RequestInterface $request, $view )
47 {
48 return $this->query( $request, $view, 'video');
49 }
50
51 /**
52 * Get search result of audios from media library
53 *
54 * @param RequestInterface $request
55 * @param string $view
56 *
57 * @return ResponseInterface
58 */
59 public function audios( RequestInterface $request, $view )
60 {
61 return $this->query( $request, $view, 'audio');
62 }
63
64 /**
65 * Get search result of vectors from media library
66 *
67 * @param RequestInterface $request
68 * @param string $view
69 *
70 * @return ResponseInterface
71 */
72 public function vectors( RequestInterface $request, $view )
73 {
74 return $this->query( $request, $view, 'vector');
75 }
76
77 /**
78 * Get media results from media library
79 *
80 * @param RequestInterface $request
81 * @param string $view
82 *
83 * @param string $assetType
84 *
85 * @return ResponseInterface
86 */
87 public function query( RequestInterface $request, $view, $assetType = 'all' )
88 {
89 // sanitize incoming params
90 $perPage = ! empty( $request->query( 'perpage' ) ) ? Sanitize::int( $request->query('perpage') ) : 20;
91 $page = ! empty( $request->query( 'page' ) ) ? Sanitize::int( $request->query('page' ) ) : 1;
92 $search = ! empty( $request->query( 's' ) ) ? Sanitize::textfield( $request->query('s' ) ) : '';
93
94 $queryParams = [
95 'post_type' => 'attachment',
96 'post_status' => 'inherit',
97 'posts_per_page' => $perPage,
98 'paged' => $page
99 ];
100
101 // add search term to query params if was defined
102 if( ! empty( $search ) ){
103 $queryParams['s'] = $search;
104 }
105
106 // change "images" to "image" for accurate mime_type
107 $assetType = rtrim( $assetType, 's' );
108
109 if( $mimTypes = $this->mediaLibraryService->getSupportedMimeTypes( $assetType ) ){
110 $queryParams['post_mime_type'] = $mimTypes;
111 }
112
113 $attachments = $this->mediaLibraryService->query( $queryParams );
114 $result = $this->mediaLibraryService->getQueryOutput( $attachments, $assetType );
115 if( empty( $result ) ){
116 return \Depicter::json(['errors' => ['Search not found.'] ])->withStatus(404);
117 }
118
119 $totalPages = $result['totalPages'];
120 unset( $result['totalPages'] );
121
122 $total = $result['total'];
123 unset( $result['total'] );
124
125 return \Depicter::json( $result )
126 ->withHeader('X-Total-Pages', $totalPages )
127 ->withHeader('X-Total', $total )
128 ->withStatus(200 );
129 }
130
131 }
132