Modifing generated links from url library in opencart - php

First lemme tell you what i am trying to achieve here . Suppose there is a url like this http://www.example.com/?id=12345 now what i want is if there is an id parameter available in the url i want to append the same parameter to every url on that page . Opencart has a url library that generates url i am sure you all must be familiar with it too , i found a way to do what i want but it's working at just some random parts of the website like categories url's are generating with id parameter appended to it and other's dont .
here's what i tried so far
File : System/libray/url.php
here's the function
public function link($route, $args = '', $connection = 'NONSSL') {
if ($connection == 'NONSSL') {
$url = $this->url;
}else {
$url = $this->ssl;
}
$url .= 'index.php?route=' . $route;
if ($args) {
$url .= str_replace('&', '&', '&' . ltrim($args, '&'));
}
foreach ($this->rewrite as $rewrite) {
$url = $rewrite->rewrite($url);
}
if(isset($_GET['id']))
{
if(!empty($this->request->get['id']))
$url .= '&id='.$this->request->get['id'];
if(!empty($_GET['id']))
{
$url .= '&id='.$_GET['id'];
}
}
return $url;
}

The problem is that not everything uses this method to generate its URLs.
For example, anything to do with banners (e.g. the Carousel module) uses links that the admin sets manually in System->Design->Banners, so you would also need to edit the code for this too. The simplest and probably the correct way is to edit the data that the models spit out e.g.
model_design_banner->getBanner() becomes
public function getBanner($banner_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "banner_image bi LEFT JOIN " . DB_PREFIX . "banner_image_description bid ON (bi.banner_image_id = bid.banner_image_id) WHERE bi.banner_id = '" . (int)$banner_id . "' AND bid.language_id = '" . (int)$this->config->get('config_language_id') . "'");
if (isset($_GET['id'])) {
array_walk($query->rows, function(&$value) {
$value['link'] .= '&id=' . $_GET['id'];
});
}
return $query->rows;
}
It's either that, or edit the output in every single controller that uses this method.
That's just an example for banners, though. I don't recall off-hand which other modules will need to be edited, but if there's a particular one that's making you scratch your head, let me know and I'll give you another example to fix it.

Related

Dynamic URL in Codeigniter

I am having trouble to dynamic the url using codeigniter.
I need to display dynamic URL look like below.
http://example.com/[store-url]
I did that. But when i try to view non-dynamic url like
http://example.com/blog. It accesssing store page only.
So i have wrote in my routes like below and it is working.
$route['blog'] = "blog";
But here my problem is i am having lot pages such as /blog. all pages are pointing the stores pages only including admin control panel
There any solution for that without setting the routes
In the routes.php
First wirte the coding of Dynamic URL
like
include_once (APPPATH . 'helpers/inflector_helper.php');
$path = explode('/', $_SERVER['REQUEST_URI']);
if ($_SERVER['HTTP_HOST'] == 'www.expample.com') {
$controller = $path[1];
} else {
$controller = $path[2];
}
$route[$controller] = plural($controller) . "/view" . ucwords($controller);
$route[$controller . '/list'] = plural($controller) . "/view" . ucwords($controller);
$route[$controller . '/view/(:num)'] = plural($controller) . "/view" . ucwords($controller) . "/$1";
$route[$controller . '/view/(:num)/(:any)'] = plural($controller) . "/view" . ucwords($controller) . "/$1/$2";
I write my static url
$route['login'] = "authenticate_user/index";
$route['validate'] = "authenticate_user/validateUser";
$route['logout'] = "authenticate_user/logout";
I hope this should work.

Get English page title Joomla

I'm working with an custom back-end template in Joomla and now I'm come to a point that I need to support multiple languages.
In the begin of the development I did not think about multiple languages and that gives me some trouble now. That it is best to rewrite the code and make it "smarter" I know, but at this point there is not much time to rewrite this code.
In the backend I'm working with icons that are based on the page title. For example the page Article Manager requests the icon images/icon/article-manager.png. So you see what happens if the page title is for example German and is called Inhalt.
At this moment I use this code to generate the iconpath:
#Loading Joomla Document Module
$doc = JFactory::getDocument();
#Get the page title
$title = explode("-", $doc->title);
$title = trim(end($title));
#Generate icon path
$lastTitle = explode("-", $title);
if (strpos(end($lastTitle), ':')) {
$lastTitle = explode(":", end($lastTitle));
$lastTitle = $lastTitle[0];
$iconPath = trim($lastTitle);
$iconPath = 'templates/' . $this->template . '/images/icons/' . strtolower(str_replace(" ", "-", $iconPath)) . '.png';
} else {
$iconPath = trim(end($lastTitle));
$iconPath = 'templates/' . $this->template . '/images/icons/' . strtolower(str_replace(" ", "-", $iconPath)) . '.png';
}
Now I'm thinking of searching the database for the page/componetent/modul/plugin ID but is there a faster/easier way to edit it?
Work around
As earlier stated, icons generated by a page title is indeed a bad idea. But Lodder came with a pretty nice and easy work around. So I decided to follow his idea. I added an extra variable that checks if it is a 'subpage', when yes then it extends the icon file name with this subpage.
#Loading Joomla Application Module
$app = JFactory::getApplication();
#Genrate page icon
$comp = $app->input->get('option');
$view = $app->input->get('view');
if(!empty($view)) {
$iconFileName = $comp . '-' . $view;
} else {
$iconFileName = $comp;
}
$iconPath = 'templates/' . $this->template . '/images/icons/' . $iconFileName . '.png';
Basing the icons on the Page Title is a bad idea. Purely because as you have already mentioned, your site is multi-lingual, therefore different languages will result in different results.
I would personally base the icons on the component name. For example:
$jinput = JFactory::getApplication()->input;
$view = $jinput->get('option');
The above code will output com_content and will always be the same for all languages.
You can then simply name your icon com_content.png and call the icons like so:
$iconPath = 'templates/' . $this->template . '/images/icons/' . $view . '.png';
Hope this helps

function processing PHP

in following function
public static function ToDepartment($departmentId, $page = 1)
{
$link = self::CleanUrlText(Catalog::GetDepartmentName($departmentId)) .
'-d' . $departmentId . '/';
if ($page > 1)
$link .= 'page-' . $page . '/';
return self::Build($link);
}
there is a line
$link = self::CleanUrlText(Catalog::GetDepartmentName($departmentId)) .
'-d' . $departmentId . '/';
I want to know will self:CleanUrlText() will be evaluated first or Catalog:GetDepartmentName will be evaluated first
if Catalog:GetDepartmentName is evaluated first then, I have a confusion,
what purpose does URL cleaning solve,
if I am visiting a page such as http://localhost/tshirtshop/visit###-the-zoo-d2/
then .htaccess is handling the URL ReWriting part,
where d2 will get converted to DepartmentId=2 and internally in all code logic I will use DepartmentId which is an INT , then why CleanURL function is required
The code is given here
1st: Catalog::GetDepartmentName
2nd: self::CleanUrlText

Is there an easier/shorter way to get geographic coordinates from GoogleMaps?

I'm currently using the following method to get coordinates from GoogleMaps.
Can I possibly write this shorter/more efficient?
EDIT 21.06.2013
As of now the old Google Geocoding API is off. This is my modified code that works with the most recent version. I've updated this post, if someone stumbles over it and finds it useful.
public function getGeographicCoordinatesFromGoogle()
{
// create address string
$value = $this->address_line_1 . ' ' .
$this->postal_code . ' ' .
$this->city . ' ' .
$this->country;
$value = preg_replace('!\s+!', '+', $value);
// create request
$request = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' .
$value . '&sensor=false';
// get value from xml request
$xml = file_get_contents($request);
$doc = new \DOMDocument('1.0', 'UTF-8');
#$doc->loadXML($xml);
// fetch result
$result = $doc->getElementsByTagName('lat')->item(0)->nodeValue . ',' .
$doc->getElementsByTagName('lng')->item(0)->nodeValue;
// check result
if (!preg_match('/(-?\d+\.\d+),(-?\d+\.\d+)/', $result) ) {
$result = null;
}
// assign value
$this->setGeographicCoordinates($result);
}
You can use json instead of xml. json is newer, lightweight and object orientated.
I'll recommend you to use http_build_query() instead of trying to build the SearchQuery with Regex. There are other chars which need to be escaped.
This is a quite long method. Maybe it would make sense to have one Class which only handles the communication with GoogleMaps (Class GeoBackend with methods getGeoDataForAddress(), which returns a GeoData-Object, which could be asked for Cordinates etc.)

Stripping HTML from jQuery load() result

This is a followup to the solution for this question.
I am using jQuery's load() function to pull a headline within a div tag from one page to another within my site. This works wonderfully.
The problem is, load() also pulls the div tag itself, which I do not want, as it then gets formatted via CSS like the source page.
Here is the PHP:
function get_team_articles($team_id, $feat=0) {
.
.
.
while ($row = mysql_fetch_assoc($r)) {
$page = explode('_', $row['page_id']);
(is_numeric($page[1]))
? $pre = 'wk_'
: $pre = '';
$arr[] = $page[0] . " | " . $pre . $page[1] . ": " . "
<a linked_div='news_header'
linked_path='../news/" . $page[0] . "/" . $pre . $page[1] . "/" . $page[1] . "_" . $page[2] . ".html'
href='index.php?view=news&yr=" . $page[0] . "&wk=" . $page[1] . "&pg=" . $page[2] . "'></a>";
}
$articles = implode('<br/>', $arr);
return $articles;
}
Notice the linked_div and linked_path attributes within the anchor tag, which are used in my jQuery:
function set_team_headlines(){
$('#section-articles > a').each(function() {
var a = $(this);
a.load(a.attr('linked_path') + ' #' + a.attr('linked_div'));
});
}
Obviously I cannot strip the HTML tags within the anchor tags in PHP, because the server doesn't have the text within the anchor tags upon loading; so I assume I need to strip the HTML in jQuery after the load() call...and that is what I cannot figure out how to do. :)
The result I want is:
My headline
The result I'm getting is:
<div id="news_header">My headline</div>
Hopefully this makes sense. I think I provided more detail than I needed to. Thanks!
Navigate down one more level in your .load selector
a.load(a.attr('linked_path') + ' #news_header');
If your news_header id isn't unique, it isn't valid to select by that id (ID's must be unique!)
To get around that issue, use this:
a.load(a.attr('linked_path') + ' #' + a.attr('linked_div') + ' div');
Edit:
.load actually includes the targeted element when appending html instead of appending the target element's children. I would move to using $.get().
$.get(a.attr('linked_path')).done(function(html) {
a.text($(html).filter("#news_header").text());
});

Categories