<?php
namespace Andy\ShowMap\Pub\Controller;
use XF\Pub\Controller\AbstractController;
class ShowMap extends AbstractController
{
public function actionIndex()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'view'))
{
return $this->noPermission();
}
// get location
$location = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
// get id
$id = $this->filter('id', 'str');
// check condition
if (empty($id))
{
// show error message
return $this->error(\XF::phrase('showmap_no_results_found_for_search_criteria'));
}
// get db
$db = \XF::db();
// run query
$coordinates = $db->fetchOne("
SELECT coordinates
FROM xf_andy_show_map
WHERE category_id = ?
", $id);
// get coordinatesArray
$coordinatesArray = explode(",", $coordinates);
// check if not numeric
if ($coordinatesArray[0] == '' OR $coordinatesArray[1] == '')
{
// show error message
return $this->error(\XF::phrase('showmap_no_results_found_for_search_criteria'));
}
// get initialLat
$initialLat = $coordinatesArray[0];
// get initialLng
$initialLng = $coordinatesArray[1];
// run query
$description = $db->fetchOne("
SELECT description
FROM xf_andy_show_map
WHERE category_id = ?
", $id);
// run query
$zoom = $db->fetchOne("
SELECT zoom
FROM xf_andy_show_map
WHERE category_id = ?
", $id);
// run query
$results = $db->fetchAll("
SELECT xf_andy_show_map_data.thread_id,
xf_andy_show_map_data.coordinates,
xf_thread.thread_id,
xf_thread.title,
xf_node.node_id,
xf_node.title AS forumTitle
FROM xf_andy_show_map_data
INNER JOIN xf_thread ON xf_thread.thread_id = xf_andy_show_map_data.thread_id
INNER JOIN xf_node ON xf_node.node_id = xf_thread.node_id
WHERE xf_andy_show_map_data.category_id = ?
ORDER BY xf_thread.title ASC
", $id);
// check if not numeric
if (empty($results))
{
// show error message
return $this->error(\XF::phrase('showmap_no_results_found_for_search_criteria'));
}
// declare variable
$locations_array = array();
// update multidimensional array
foreach ($results as $k => $v)
{
// get coordinatesArray
$coordinatesArray = explode(",", $v['coordinates']);
// get lat
$lat = $coordinatesArray[0];
// get lng
$lng = $coordinatesArray[1];
// check condition
if (is_numeric($lat) AND is_numeric($lng))
{
// get thread
$thread = array(
'thread_id' => $v['thread_id'],
'title' => $v['title']
);
// get router
$router = \XF::app()->router('public');
// get link
$link = $router->buildLink('threads', $thread);
// get info
$info = '<a href="' . $location . $link . '">' . $v['title'] . '</a>';
// get locations_array
$locations_array[] = [
'lat' => $lat + 0,
'lng' => $lng + 0,
'info' => $info,
'title' => $v['title']
];
// update results
$results[$k]['link'] = $link;
}
else
{
\XF::app()->error()->logError("Show map error. Map coordinate error in thread ID: " . $v['thread_id']);
}
}
// check condition
if (empty($locations_array))
{
// show error message
return $this->error(\XF::phrase('showmap_no_results_found_for_search_criteria'));
}
// get locationsJson
$locationsJson = json_encode($locations_array);
// prepare viewParams
$viewParams = [
'initialLat' => $initialLat,
'initialLng' => $initialLng,
'zoom' => $zoom,
'locationsJson' => $locationsJson,
'results' => $results,
'description' => $description
];
// send to template
return $this->view('Andy\ShowMap:ShowMap', 'andy_showmap', $viewParams);
}
public function actionAdmin()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// get db
$db = \XF::db();
// run query
$results = $db->fetchAll("
SELECT *
FROM xf_andy_show_map
ORDER BY category_id ASC
");
// prepare viewParams
$viewParams = [
'results' => $results
];
// send to template
return $this->view('Andy\ShowMap:ShowMap', 'andy_showmap_admin', $viewParams);
}
public function actionAdminAdd()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// send to template
return $this->view('Andy\ShowMap:ShowMap', 'andy_showmap_admin_add');
}
public function actionAdminSave()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// get categoryId
$categoryId = $this->filter('id', 'str');
// check condition
if ($categoryId == '')
{
return $this->error(\XF::phrase('showmap_category_id_missing'));
}
// get description
$description = $this->filter('description', 'str');
// check condition
if ($description == '')
{
return $this->error(\XF::phrase('showmap_description_missing'));
}
// get coordinates
$coordinates = $this->filter('coordinates', 'str');
// check condition
if ($coordinates == '')
{
return $this->error(\XF::phrase('showmap_coordinates_missing'));
}
// get zoom
$zoom = $this->filter('zoom', 'int');
// get db
$db = \XF::db();
// run query
$db->query("
INSERT INTO xf_andy_show_map
(category_id, description, coordinates, zoom)
VALUES
(?,?,?,?)
", array($categoryId, $description, $coordinates, $zoom));
// return redirect
return $this->redirect($this->buildLink('showmap/admin'));
}
public function actionAdminEdit()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// get showMapId
$showMapId = $this->filter('show_map_id', 'str');
// get db
$db = \XF::db();
// run query
$results = $db->fetchRow("
SELECT category_id,
description,
coordinates,
zoom
FROM xf_andy_show_map
WHERE show_map_id = ?
", $showMapId);
// prepare viewParams
$viewParams = [
'showMapId' => $showMapId,
'categoryId' => $results['category_id'],
'description' => $results['description'],
'coordinates' => $results['coordinates'],
'zoom' => $results['zoom']
];
// send to template
return $this->view('Andy\ShowMap:ShowMap', 'andy_showmap_admin_edit', $viewParams);
}
public function actionAdminEditSave()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// get showMapId
$showMapId = $this->filter('show_map_id', 'uint');
// verify showMapId
if ($showMapId == '')
{
return $this->error(\XF::phrase('error'));
}
// get categoryId
$categoryId = $this->filter('id', 'str');
// check condition
if ($categoryId == '')
{
return $this->error(\XF::phrase('showmap_category_id_missing'));
}
// get detail
$description = $this->filter('description', 'str');
// check condition
if ($description == '')
{
return $this->error(\XF::phrase('showmap_description_missing'));
}
// get coordinates
$coordinates = $this->filter('coordinates', 'str');
// check condition
if ($coordinates == '')
{
return $this->error(\XF::phrase('showmap_coordinates_missing'));
}
// get zoom
$zoom = $this->filter('zoom', 'uint');
// get db
$db = \XF::db();
// run query
$oldCategoryId = $db->fetchOne("
SELECT category_id
FROM xf_andy_show_map
WHERE show_map_id = ?
", $showMapId);
// run query
$db->query('
UPDATE xf_andy_show_map SET
category_id = ?,
description = ?,
coordinates = ?,
zoom = ?
WHERE show_map_id = ?
', array($categoryId, $description, $coordinates, $zoom, $showMapId));
// run query
$db->query('
UPDATE xf_andy_show_map_data SET
category_id = ?
WHERE category_id = ?
', array($categoryId, $oldCategoryId));
// return redirect
return $this->redirect($this->buildLink('showmap/admin'));
}
public function actionAdminDelete()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// get showMapId
$showMapId = $this->filter('show_map_id', 'str');
// get db
$db = \XF::db();
// delete row
$db->query("
DELETE FROM xf_andy_show_map
WHERE show_map_id = ?
", $showMapId);
// return redirect
return $this->redirect($this->buildLink('showmap/admin'));
}
public function actionUpdate()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// get db
$db = \XF::db();
// run query
$results = $db->fetchAll("
SELECT category_id
FROM xf_andy_show_map
");
// prepare viewParams
$viewParams = [
'results' => $results
];
// send to template
return $this->view('Andy\ShowMap:ShowMap', 'andy_showmap_update', $viewParams);
}
public function actionUpdateSave()
{
// get visitor
$visitor = \XF::visitor();
// check for user group permission
if (!$visitor->hasPermission('showMap', 'admin'))
{
return $this->noPermission();
}
// get forumId
$forumId = $this->filter('forum_id', 'uint');
// get categoryId
$categoryId = $this->filter('id', 'str');
// get insertCount
$insertCount = 0;
// get db
$db = \XF::db();
// run query
$db->query("
DELETE FROM xf_andy_show_map_data
WHERE category_id = ?
", $categoryId);
// run query
$results = $db->fetchAll("
SELECT first_post_id
FROM xf_thread
WHERE node_id = ?
", $forumId);
foreach ($results AS $k => $v)
{
// run query
$data = $db->fetchRow("
SELECT thread_id,
message
FROM xf_post
WHERE post_id = ?
", $v['first_post_id']);
// run query
$data2 = $db->fetchOne("
SELECT thread_id
FROM xf_andy_show_map_data
WHERE thread_id = ?
", $data['thread_id']);
// check condition
if (empty($data2))
{
// check for [map={coordinates}]View Map[/map]
if (preg_match_all("/\[map\=(.*?)\]View Map\[\/map\]/i", $data['message'], $out))
{
// run query
$db->query("
INSERT INTO xf_andy_show_map_data
(thread_id, category_id, coordinates)
VALUES
(?,?,?)
", array($data['thread_id'], $categoryId, $out[1][0]));
$insertCount = $insertCount + 1;
}
}
}
// prepare viewParams
$viewParams = [
'categoryId' => $categoryId,
'insertCount' => $insertCount
];
// send to template
return $this->view('Andy\ShowMap:ShowMap', 'andy_showmap_update_confirm', $viewParams);
}
}