PluginProbe
Depicter — Popup & Slider Builder / 1.3.3
Depicter — Popup & Slider Builder v1.3.3
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 1.3.3, at app/src/Controllers/Ajax/MediaLibraryAjaxController.php

148 lines 3.6 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 $mimTypes = [
110 'image' => ['image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon'],
111 'photo' => ['image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon'],
112 'video' => 'video',
113 'audio' => 'audio',
114 'vector' => 'image/svg+xml'
115 ];
116
117 if( in_array( $assetType, array_keys( $mimTypes ) ) ){
118 $queryParams['post_mime_type'] = $mimTypes[ $assetType ];
119 }
120
121
122 $attachments = $this->mediaLibraryService->query( $queryParams );
123 $result = $this->mediaLibraryService->getQueryOutput( $attachments, $assetType );
124 if( empty( $result ) ){
125 return \Depicter::json(['errors' => ['Search not found.'] ])->withStatus(404);
126 }
127
128 $totalPages = $result['totalPages'];
129 unset( $result['totalPages'] );
130
131 $total = $result['total'];
132 unset( $result['total'] );
133
134 return \Depicter::json( $result )
135 ->withHeader('X-Total-Pages', $totalPages )
136 ->withHeader('X-Total', $total )
137 ->withStatus(200 );
138 try {
139 } catch ( \Exception $exception ){
140 return \Depicter::json([
141 'errors' => [ $exception->getMessage() . 'jjjj' ]
142 ]);
143 }
144
145 }
146
147 }
148