Google Map V3 Cakephp helper and multiple markers - google-maps

I am using the Cakephp Google Map V3 Helper. I can get the google map to show up but the markers do not. Here is my view code:
<?php
echo $this->GoogleMapV3->map();
foreach ($allcondos as $condo) {
$options = array(
'lat' => $condo['Unit']['lat'],
'lng' => $condo['Unit']['lon']
);
$this->GoogleMapV3->addMarker($options);
}
?>
I know that if I just tell the app to echo out my $condo['Unit']['lat'] or ['lon'] it will do so in the foreach loop (so it is pulling my data). What I don't know how to do is how to write the code for the $options array. I have also tried this:
foreach ($allcondos as $condo) {
$lat=$condo['Unit']['lat'];
$lon=$condo['Unit']['lon'];
$options = array(
'lat' => $lat,
'lng' => $lon
);
$this->GoogleMapV3->addMarker($options);
}
How do I write this correctly?

A couple easy steps to get this to work:
Download
Download from https://github.com/dereuromark/cakephp-google-map-v3-helper and place the GoogleMapV3Helper.php file in /app/view/helper/GoogleMapV3Helper.php.
Load Helper
Either modify your appcontroller so that the top of it reads like the following:
<?php
class AppController extends Contoller{
public $helpers = array('Html','Javascript','GoogleMapV3');
}
?>
Or load it in a single controller by adding it to the helpers array as such:
<?php
class DemoController extends AppContoller{
function map() {
$this->helpers[] = 'GoogleMapV3';
# rest of your code
}
}
?
Include Scripts
Include Jquery in your header. Include the following as well:
<?php
echo '<script type="text/javascript" src="'.$this->GoogleMapV3->apiUrl().'"></script>';
?>
Create Map Container
Put this in your view where you want your map to appear. Feel free to modify the properties of the div.
<?php echo $this->GoogleMapV3->map(array('div'=>array('id'=>'my_map', 'height'=>'400', 'width'=>'100%'))); ?>
Note: you can change the default position of the map by including more options than just 'div':
<?php echo $this->GoogleMapV3->map(array('map'=>array(
'defaultLat' => 40, # only last fallback, use Configure::write('Google.lat', ...); to define own one
'defaultLng' => -74, # only last fallback, use Configure::write('Google.lng', ...); to define own one
'defaultZoom' => 5,
),'div'=>array('id'=>'my_map', 'height'=>'400', 'width'=>'100%'))); ?>
Add markers
Can be in a loop or whatever, but this is done in the view after your container is created.
<?php
$options = array(
'lat'=>40.770272,
'lng'=>-73.974037,
'title' => 'Some title', # optional
'content' => '<b>HTML</b> Content for the Bubble/InfoWindow' # optional
);
$this->GoogleMapV3->addMarker($options);
?>
note: only set the 'icon' key in the array if you want to use a custom image. Otherwise, they will not show up.
Include the script for the markers
<?php echo $this->GoogleMapV3->script() ?>
All done!
Alternately, you can use finalize() instead of script() if you do not want to echo the javascript right away, but write it to the buffer for later output in your layout:
<?php $this->GoogleMapV3->finalize(); ?>
See http://www.dereuromark.de/2010/12/21/googlemapsv3-cakephp-helper/ for details.

Related

Get ACF gutenberg block data from custom post type

I running into some problems with ACF Gutenberg blocks.
I have registered a Gutenberg block to be used in a custom post type called "Portfolio"
Through a normal wp_query i can display the custom post type on the homepage. But i can not get the ACF Gutenberg block data to show?
Below is the code im currently using.
<?php
// WP_Query arguments
$args = array(
'post_type' => array( 'portfolio' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '5',
);
// The Query
$query_portfolio = new WP_Query( $args );
// The Loop
if ( $query_portfolio->have_posts() ) {
while ( $query_portfolio->have_posts() ) {
$query_portfolio->the_post();
}
// ACF group
$content = get_field('content');
?>
<!-- // ACF field from within group NOT SHOWING -->
<h1><?php echo $content['title']; ?></h1>
<h2><?php the_title(); ?></h2>
<?php
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
Im stuck at this point. Somehow i can not get the Gutenberg Block fields to show within the query on the homepage.
All help is appreciated.
Thanks

Wordpress custom page template returns the theme headers

I want to return a JSON array from a Wordpress website. I know this can be done using plugins like WP REST API and JSON API, but I am dealing with custom post types so I want to avoid complexity and write the queries myself.
I have overridden one of my pages by writing a new php file in my theme directory called page-345.php.
Inside my PHP file I have the following code
$args = array(
'post_type' => 'partner',
'post_status' => 'publish',
'posts_per_page' => -1 // all
);
$query = new WP_Query( $args );
$cars = array();
while( $query->have_posts() ) : $query->the_post();
// Add a car entry
$cars[] = array(
'name' => get_the_title()
);
endwhile;
wp_reset_query();
wp_send_json( $cars );
I code works and I get output in JSON. However, JSON in returned alongside the header of the template. (See screenshot)
I also tried entering the code
header( 'Content-type: application/json' );
at the top of my page, but then Wordpress returns an error saying that headers have already been set.

Cakephp Pagination is not working?

I am new to cakephp, My pagination is not working..
My controller code:
$this->paginate = array('Favourite' =>array('limit' => '1','conditions'=>array('user_id'=>$id)));
$cat_data = $this->paginate('Favourite');
$this->set('fav_data',$cat_data);
My view code:
<!---Code for pagination -->
<div>
<?php echo $this->Paginator->prev('', null, null, array('class' => 'pdisable','escape' => false)); ?>
<div class="numbers"><?php echo $this->Paginator->numbers(); ?> Pages: <?php echo $this->Paginator->counter(); ?></div>
<?php echo $this->Paginator->next('', null, null, array('class' => 'ndisable','escape' => false)); ?>
</div>
It is not displaying one result based on the limit i have given.. It displays all the records.
When you are trying to get the paginated recordset using this line in your code:
$cat_data = $this->paginate('Favourite');
You are actually redeclaring the paginate settings a second time, only without the limit or condition specified anymore. You need to use the Paginator Component class with the Paginate settings you declare beforehand.
In your code:
//here your are declaring the paginate settings one time.
$this->paginate = array('Favourite' =>array('limit'=>'1','conditions'=>array('user_id'=>$id)));
//Here you are re-declaring them again, only not with a limit or condition specified. This effectively overwrites the first settings declaration you made.
$cat_data = $this->paginate('Favourite');
$this->set('fav_data',$cat_data);
Change:
$cat_data = $this->paginate('Favourite');
To:
$cat_data = $this->Paginator->paginate('Favourite');
So your controller code should look like:
//declare settings without conditions.
$this->paginate = array(
'limit' => 1
);
//use above settings to pull the paginated results.
//be sure to add the conditions you had in the settings before to the 2nd parameter of the `paginate()` function.
$this->set('fav_data', $this->Paginator->paginate('Favourite', array('user_id' => $id)));

Upload files and store titles into database

Controller :
class gallery extends CI_Controller {
function index()
{
$this->load->model('gallery_model');
if($this->input->post('upload'))
{
$this->gallery_model->do_upload();
}
$data['images'] = $this->gallery_model->get_images();
$this->load->view('gallery',$data);
}
}
Model :
<?php
class gallery_model extends CI_Model {
var $gallery_path;
var $gallery_path_url;
function gallery_model()
{
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url().'images/';
}
function do_upload()
{
$config = array(
'allowed_types' => 'jpeg|jpg|gif|png',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path. '/thumbs',
'maintain_ration' => TRUE,
'width' => 150,
'height' =>150
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
View:
<!DOCTYPE html>
<html>
<head>
<title>My gallery</title>
<meta charset="UTF-8"/>
</head>
<body>
<div id="upload">
<?php
echo form_open_multipart('gallery');
echo form_upload('userfile');
echo form_submit('upload','Upload');
echo form_close();
?>
</div>
</body>
</html>
I want to do something like an photo album. In the upload form i upload photos in images folder and the thumb into thumb folder. What i want to do is to store the titles of pictures into the database and retrieve from there and i don't know how to store the pictures titles.
I would add a text field to your form for Image Title, pass it to your do_upload method from the gallery model, or separately into a new image database model (to connect and query your database).
I'm writing this on the fly and is untested. Some advice to the process, make sure you are cleaning the inputs. Codeigniter does a great job at this, but it's good coding to share that advice. Also, you should like an image with its title. I would maybe also pass to the database, the dir path of the image and/or its thumb path. It also makes calling the image from gallery easier as you are already querying the Database for meta data. Something like a foreach($img in$result){ echo $img['title']." - ".echo $img['path'];} to produce "My title - http://example.com/images/thumb/1234.jpg"
View (input)
..
echo form_input('imgTitle', 'Image Title');
..
Controller
..
if($this->input->post('upload'))
{
$data['imgTitle'] = $this->input->post('imgTitle'); //Assigns the post value to the data array so you can pass it via $data to your views
$this->gallery_model->do_upload($data['imgTitle']);
}
..
Model
..
function do_upload($imgTitle=null) //passes a variable as NULL unless specified
{
..
$DB1 = $this->load->database(); //specify db connection/name
$data = array(
'title' => $imgTitle, //passed from controller
'name' => 'My Name');
$this->db->insert('mytable', $data);
..
}

cakephp Image escape false not working

I am making a Image link to user profile, but it is not working as it should be
this is my code.with this i want to add contoroller,function and id. How i can do it.
<?php
$pic = $User['User']['url'];
if(!$pic){
echo $this->Html->link($this->Html->image('pro.jpg'), array('alt'=>$User['User']['handle'],'title' => $User['User']['handle']),array('escape'=>false) ,array('class'=>'inner_image'));
}else{
echo $this->Html->link($this->Html->image($User['User']['url']),array('alt' => $User['User']['handle']),array('escape'=>false),array('class'=>'inner_image'));
}
?>
This code is making image a link but i can't define a link and it is not accepting the class
.I want to pass this url
$this->Html->link('', array('controller'=>'User','action'=>'view','id'=>$User['User']['id']));
This is how I did it
echo $this->Html->link($this->Html->image($pic, array('class'=>'inner_image')), $url_array, array('alt' => $User['User']['handle'], 'escape'=>false));
What cake version are you using? You don't seem to be following the documentation for Html::link.
HtmlHelper::link(string $title, mixed $url = null, array $options =
array(), string $confirmMessage = false)
alt, escape and class should be indexes in the options array, but you're not defininf the url parameter anywhere.
It should be something like this
if(!$pic){
echo $this->Html->link($this->Html->image('pro.jpg'), $url_array, array('alt'=>$User['User']['handle'],'title' => $User['User']['handle'],'escape'=>false,'class'=>'inner_image'));
} else {
echo $this->Html->link($this->Html->image($User['User']['url']), $url_array, array('alt' => $User['User']['handle'], 'escape'=>false, 'class'=>'inner_image'));
(don't know what url you want to point this at, so replace $url_array to your convenience.