Subversion Repository Public Repository

wpbsav

Diff Revisions 19 vs 42 for /SagreraWP/wp-content/themes/bsav/includes/Gallery.php

Diff revisions: vs.
  @@ -3,32 +3,22 @@
3 3 class Gallery
4 4 {
5 5
6 - public $markersSagrera = array(
7 - array("x" => 215, "y" => 170),
8 - array("x" => 559, "y" => 242),
9 - array("x" => 180, "y" => 141),
10 - array("x" => 435, "y" => 182),
11 - array("x" => 377, "y" => 234),
12 - array("x" => 652, "y" => 249)
13 - );
14 -
15 - public $markersSantAndreu = array(
16 - array("x" => 326, "y" => 105),
17 - array("x" => 188, "y" => 147),
18 - array("x" => 440, "y" => 120),
19 - array("x" => 379, "y" => 141)
20 - );
6 + private $posts = array();
21 7
22 8 public function __construct()
23 9 {
24 10 wp_register_script( 'ajax_gallery', get_template_directory_uri().'/js/gallery.js' );
11 + wp_register_script( 'blockui', get_template_directory_uri().'/js/blockui.js' );
25 12 wp_enqueue_script('ajax_gallery');
13 + wp_enqueue_script('blockui');
26 14 wp_localize_script(
27 15 'ajax_gallery',
28 16 'galleryAjax',
29 17 array(
30 18 'url' => admin_url( 'admin-ajax.php' ),
31 - 'nonce' => wp_create_nonce( "9f1391904294aff0c10c744f8b2442753f2b739b42a1aa1d" ),
19 + 'sagrera' => 'actuaciones-sagrera',
20 + 'sandreu' => 'actuaciones-sandreu',
21 + 'videos' => 'videos'
32 22 )
33 23 );
34 24 add_action('init', array(&$this, 'init_gallery'));
  @@ -36,28 +26,396 @@
36 26
37 27 public function init_gallery()
38 28 {
39 - add_action( 'wp_ajax_reload_gallery', array( &$this, 'reload_gallery' ) );
40 - add_action( 'wp_ajax_nopriv_reload_gallery', array( &$this, 'reload_gallery' ) );
41 - add_action( 'wp_ajax_coords', array( &$this, 'coords' ) );
42 - add_action( 'wp_ajax_nopriv_coords', array( &$this, 'coords' ) );
29 + add_action( 'wp_ajax_actuacions', array( &$this, 'search_actuacions' ) );
30 + add_action( 'wp_ajax_nopriv_actuacions', array( &$this, 'search_actuacions' ) );
31 + add_action( 'wp_ajax_load_gallery', array( &$this, 'reload_gallery' ) );
32 + add_action( 'wp_ajax_nopriv_load_gallery', array( &$this, 'reload_gallery' ) );
33 + add_action( 'wp_ajax_load_childrens_gallery', array( &$this, 'load_childrens_gallery' ) );
34 + add_action( 'wp_ajax_nopriv_load_childrens_gallery', array( &$this, 'load_childrens_gallery' ) );
43 35 }
44 36
45 - public function coords()
37 + public function search_actuacions()
46 38 {
47 - echo $_GET["location"];
48 - die();
39 + if( isset( $_GET["type"] ) && !empty( $_GET["type"] ) )
40 + {
41 + $type = explode("-",$_GET["type"]);
42 + $location = $type[0];
43 + $title = $_GET["title"];
44 + $this->get_post_by_title_and_type("actuaciones-".$location, $title);
45 + die();
46 + }
49 47 }
50 48
51 49 public function reload_gallery()
52 50 {
53 - echo 1; die();
54 - check_ajax_referer( 'reload_gallery_nonce', 'nonce' );
51 + $type = sanitize_text_field($_GET["type"]);
52 + $lang = sanitize_text_field($_GET["language"]) === "CA" ? "cat" : "es";
55 53
56 - if( true )
57 - wp_send_json_success( 'Ajax here!' );
54 + if( $type == "videos" )
55 + {
56 + new GalleryVideoFactory( $type, $lang, null );
57 + }
58 58 else
59 - wp_send_json_error( array( 'error' => $custom_error ) );
59 + {
60 + new GalleryFactory( $type, $lang, '_loadGallery', null );
61 + }
60 62 }
63 +
64 + public function load_childrens_gallery()
65 + {
66 + $type = sanitize_text_field($_GET["type"]);
67 + $id = sanitize_text_field($_GET["id"]);
68 + $lang = sanitize_text_field($_GET["language"]) === "CA" ? "cat" : "es";
69 +
70 + if( $type === "videos" )
71 + {
72 + new GalleryVideoFactory( $type, $lang, $id );
73 + }
74 + else
75 + {
76 + new GalleryFactory( $type, $lang, '_loadChildrensGallery', $id );
77 + }
78 +
79 + }
80 + }
81 +
82 + class GalleryVideoFactory
83 + {
84 + public function __construct( $type, $lang, $id )
85 + {
86 +
87 + if( $id !== null )
88 + {
89 + $this->_loadChildrensVideoGallery( $type, $lang, $id );
90 + die();
91 + }
92 +
93 + global $wpdb;
94 +
95 + $querystr = "
96 + SELECT wposts.ID, wposts.post_title, wposts.post_type
97 + FROM $wpdb->posts wposts
98 + WHERE wposts.post_type = '{$type}'
99 + AND wposts.post_status = 'publish'
100 + ";
101 +
102 + //obtenemos todos los resultados del sidebar que pertenecen a este custom-post-type
103 + $parents = $wpdb->get_results($querystr, OBJECT);
104 +
105 + $first = $parents[0]->ID; //obtenemos el primer id padre
106 +
107 + $qry = "
108 + SELECT p.ID, pm.meta_key, pm.meta_value, p.post_date, p.post_type
109 + FROM $wpdb->posts p
110 + JOIN $wpdb->postmeta pm on p.ID = pm.post_id
111 + WHERE p.post_status = 'publish'
112 + AND p.ID = {$first}
113 + AND (
114 + meta_key = 'wpcf-video-{$lang}'
115 + )
116 + ORDER BY p.ID DESC
117 + ";
118 +
119 + $childrens = $wpdb->get_results($qry, OBJECT);
120 +
121 + if( $parents != NULL && $childrens != NULL )
122 + {
123 + $metaChildrens = array();
124 + foreach( $childrens as $children )
125 + {
126 + if( count(explode("<!--:-->", $parents[0]->post_title)) > 1)
127 + {
128 + $title = explode("<!--:-->", $parents[0]->post_title);
129 + if ( $lang === "CA" )
130 + {
131 + $title = str_replace(
132 + array( "<!--:CA-->", "<!--:es-->"),
133 + array( "", ""),
134 + $title[0]
135 + );
136 + }
137 + else
138 + {
139 + $title = str_replace(
140 + array( "<!--:CA-->", "<!--:es-->"),
141 + array( "", ""),
142 + $title[1]
143 + );
144 + }
145 + }
146 + $metaChildrens[] = array( "video" => $children->meta_value, "id" => $children->ID, "title" => $title );
147 + }
148 + wp_send_json_success( array( "parents" => $parents, "childrens" => $metaChildrens ) );
149 + }
150 + else
151 + {
152 + wp_send_json_error();
153 + }
154 + die();
155 + }
156 +
157 + private function _loadChildrensVideoGallery( $type, $lang, $id )
158 + {
159 + global $wpdb;
160 +
161 +
162 + $querystr = "
163 + SELECT wposts.ID, wposts.post_title, wposts.post_type
164 + FROM $wpdb->posts wposts
165 + WHERE wposts.post_type = '{$type}'
166 + AND wposts.ID = {$id}
167 + AND wposts.post_status = 'publish'
168 + ";
169 +
170 + //obtenemos todos los resultados del sidebar que pertenecen a este custom-post-type
171 + $parent = $wpdb->get_results($querystr, OBJECT);
172 +
173 + $qry = "
174 + SELECT p.ID, pm.meta_key, pm.meta_value, p.post_date, p.post_type
175 + FROM $wpdb->posts p
176 + JOIN $wpdb->postmeta pm on p.ID = pm.post_id
177 + WHERE p.post_status = 'publish'
178 + AND p.ID = {$id}
179 + AND (
180 + meta_key = 'wpcf-video-{$lang}'
181 + )
182 + ORDER BY p.ID DESC
183 + ";
184 +
185 + $childrens = $wpdb->get_results($qry, OBJECT);
186 +
187 + if( $parent != NULL && $childrens != NULL )
188 + {
189 + $metaChildrens = array();
190 + foreach( $childrens as $children )
191 + {
192 + if( count(explode("<!--:-->", $parent[0]->post_title)) > 1)
193 + {
194 + $title = explode("<!--:-->", $parent[0]->post_title);
195 + if ( $lang === "cat" )
196 + {
197 + $title = str_replace(
198 + array( "<!--:CA-->", "<!--:es-->"),
199 + array( "", ""),
200 + $title[0]
201 + );
202 + }
203 + else
204 + {
205 + $title = str_replace(
206 + array( "<!--:CA-->", "<!--:es-->"),
207 + array( "", ""),
208 + $title[1]
209 + );
210 + }
211 + }
212 + $metaChildrens[] = array( "video" => $children->meta_value, "id" => $children->ID, "title" => $title );
213 + }
214 + wp_send_json_success( array( "parent" => $parent, "childrens" => $metaChildrens ) );
215 + }
216 + else
217 + {
218 + wp_send_json_error();
219 + }
220 + die();
221 + }
222 + }
223 +
224 + class GalleryFactory
225 + {
226 +
227 + private $posts;
228 +
229 + public function __construct( $type, $lang, $method, $id )
230 + {
231 +
232 + $this->$method( $type, $lang, $id );
233 +
234 + // foreach( $parents as $parent)
235 + // {
236 + // $qry = "
237 + // SELECT p.ID, pm.meta_key, pm.meta_value, p.post_date, p.post_type
238 + // FROM $wpdb->posts p
239 + // JOIN $wpdb->postmeta pm on p.ID = pm.post_id
240 + // WHERE p.post_parent = {$parent->ID}
241 + // AND p.post_status = 'publish'
242 + // AND (
243 + // meta_key = 'wpcf-img-gallery' OR meta_key = 'wpcf-title-{$lang}-gallery' OR meta_key = 'wpcf-desc-{$lang}-gallery' OR meta_key = 'wpcf-videos-gallery'
244 + // )
245 + // ORDER BY p.ID DESC
246 + // ";
247 + //
248 + // $childrens = $wpdb->get_results($qry, OBJECT);
249 + //
250 + // $metaChildrens = array();
251 + // $date = "";
252 + // foreach( $childrens as $children )
253 + // {
254 + // $key = str_replace(
255 + // array( "wpcf-title-{$lang}-gallery", "wpcf-desc-{$lang}-gallery", "wpcf-img-gallery", "wpcf-videos-gallery"),
256 + // array( "title", "desc", "img", "video"),
257 + // $children->meta_key
258 + // );
259 + //
260 + // if( $key == "img" || $key == "video")
261 + // {
262 + // $id = explode(".", end(explode('/',$children->meta_value)))[0];
263 + // $date = $wpdb->get_row( "SELECT date FROM multimedia WHERE ID = $id", OBJECT );
264 + // add_post_meta( $children->ID, 'wpcf-date-gallery', $date->date, false );
265 + // }
266 + //
267 + // //$metaChildrens[$children->ID][$key] = array( "value" => $children->meta_value, "date" => $date );
268 + // }
269 + // }
270 + }
271 +
272 + private function _loadGallery( $type, $lang, $id )
273 + {
274 + global $wpdb;
275 +
276 + $querystr = "
277 + SELECT wposts.ID, wposts.post_title, wposts.post_type
278 + FROM $wpdb->posts wposts
279 + WHERE wposts.post_type = '{$type}'
280 + AND wposts.post_status = 'publish'
281 + ";
282 +
283 + //obtenemos todos los resultados del sidebar que pertenecen a este custom-post-type
284 + $parents = $wpdb->get_results($querystr, OBJECT);
285 +
286 + $first = $parents[0]->ID; //obtenemos el primer id padre
287 +
288 + $qry = "
289 + SELECT p.ID, pm.meta_key, pm.meta_value, p.post_date, p.post_type
290 + FROM $wpdb->posts p
291 + JOIN $wpdb->postmeta pm on p.ID = pm.post_id
292 + WHERE p.post_parent = {$first}
293 + AND p.post_status = 'publish'
294 + AND (
295 + meta_key = 'wpcf-img-gallery'
296 + OR meta_key = 'wpcf-title-{$lang}-gallery'
297 + OR meta_key = 'wpcf-desc-{$lang}-gallery'
298 + OR meta_key = 'wpcf-videos-gallery'
299 + OR meta_key = 'wpcf-date-gallery'
300 + )
301 + ORDER BY p.ID DESC
302 + ";
303 +
304 + $childrens = $wpdb->get_results($qry, OBJECT);
305 +
306 + if( $parents != NULL && $childrens != NULL )
307 + {
308 + $metaChildrens = array();
309 + foreach( $childrens as $children )
310 + {
311 + $key = str_replace(
312 + array( "wpcf-title-{$lang}-gallery", "wpcf-desc-{$lang}-gallery", "wpcf-img-gallery", "wpcf-videos-gallery", "wpcf-date-gallery"),
313 + array( "title", "desc", "img", "video", "date"),
314 + $children->meta_key
315 + );
316 + $metaChildrens[$children->ID][$key] = array( "value" => $children->meta_value );
317 + }
318 + wp_send_json_success( array( "parents" => $parents, "childrens" => $metaChildrens ) );
319 + }
320 + else
321 + {
322 + wp_send_json_error();
323 + }
324 + die();
325 + }
326 +
327 + private function _loadChildrensGallery( $type, $lang, $id )
328 + {
329 + global $wpdb;
330 +
331 + $querystr = "
332 + SELECT wposts.ID, wposts.post_title, wposts.post_type
333 + FROM $wpdb->posts wposts
334 + WHERE wposts.ID = {$id}
335 + AND wposts.post_status = 'publish'
336 + ";
337 +
338 + //obtenemos todos los resultados del sidebar que pertenecen a este custom-post-type
339 + $parent = $wpdb->get_results($querystr, OBJECT);
340 +
341 + $qry = "
342 + SELECT p.ID, pm.meta_key, pm.meta_value, p.post_date, p.post_type
343 + FROM $wpdb->posts p
344 + JOIN $wpdb->postmeta pm on p.ID = pm.post_id
345 + WHERE p.post_parent = {$id}
346 + AND p.post_status = 'publish'
347 + AND (
348 + meta_key = 'wpcf-img-gallery'
349 + OR meta_key = 'wpcf-title-{$lang}-gallery'
350 + OR meta_key = 'wpcf-desc-{$lang}-gallery'
351 + OR meta_key = 'wpcf-videos-gallery'
352 + OR meta_key = 'wpcf-date-gallery'
353 + )
354 + ORDER BY p.ID DESC
355 + ";
356 +
357 + $childrens = $wpdb->get_results($qry, OBJECT);
358 +
359 + if( $parent != NULL && $childrens != NULL )
360 + {
361 + $metaChildrens = array();
362 + foreach( $childrens as $children )
363 + {
364 + $key = str_replace(
365 + array( "wpcf-title-{$lang}-gallery", "wpcf-desc-{$lang}-gallery", "wpcf-img-gallery", "wpcf-videos-gallery", "wpcf-date-gallery"),
366 + array( "title", "desc", "img", "video", "date"),
367 + $children->meta_key
368 + );
369 + $metaChildrens[$children->ID][$key] = array( "value" => $children->meta_value );
370 + }
371 + wp_send_json_success( array( "parent" => $parent, "childrens" => $metaChildrens ) );
372 + }
373 + else
374 + {
375 + wp_send_json_error();
376 + }
377 + die();
378 + }
379 + }
380 +
381 + class GalleryDecorator
382 + {
383 + // foreach( $parents as $parent)
384 + // {
385 + // $qry = "
386 + // SELECT p.ID, pm.meta_key, pm.meta_value, p.post_date, p.post_type
387 + // FROM $wpdb->posts p
388 + // JOIN $wpdb->postmeta pm on p.ID = pm.post_id
389 + // WHERE p.post_parent = {$parent->ID}
390 + // AND p.post_status = 'publish'
391 + // AND (
392 + // meta_key = 'wpcf-img-gallery' OR meta_key = 'wpcf-title-{$lang}-gallery' OR meta_key = 'wpcf-desc-{$lang}-gallery' OR meta_key = 'wpcf-videos-gallery'
393 + // )
394 + // ORDER BY p.ID DESC
395 + // ";
396 + //
397 + // $childrens = $wpdb->get_results($qry, OBJECT);
398 + //
399 + // $metaChildrens = array();
400 + // $date = "";
401 + // foreach( $childrens as $children )
402 + // {
403 + // $key = str_replace(
404 + // array( "wpcf-title-{$lang}-gallery", "wpcf-desc-{$lang}-gallery", "wpcf-img-gallery", "wpcf-videos-gallery"),
405 + // array( "title", "desc", "img", "video"),
406 + // $children->meta_key
407 + // );
408 + //
409 + // if( $key == "img" || $key == "video")
410 + // {
411 + // $id = explode(".", end(explode('/',$children->meta_value)))[0];
412 + // $date = $wpdb->get_row( "SELECT date FROM multimedia WHERE ID = $id", OBJECT );
413 + // add_post_meta( $parent->ID, 'wpcf-date-gallery', $date->date, false );
414 + // }
415 + //
416 + // //$metaChildrens[$children->ID][$key] = array( "value" => $children->meta_value, "date" => $date );
417 + // }
418 + // }
61 419 }
62 420
63 421 new Gallery;