Published by Funky Dude on 8th July 2010
opencart has built in seo friendly url but that’s for products only as far as i can tell. and you have to actually type out the terms. so i took it upon myself to make it friendlier and more automagic.
this is for opencart v1.4.8b
the format of url we are trying to achieve is:
/product/:product_id/:product_title
so it would look like something like /product/1/girls-gone-wild
change catalog/model/tool/seo_url.php
public function rewrite($link)
{
if ($this->config->get('config_seo_url'))
{
$url_data = parse_url(str_replace('&', '&', $link));
$url = '';
$data = array();
parse_str($url_data['query'], $data);
switch($data['route'])
{
case 'product/product':
$this->load->model('catalog/product');
$product = $this->model_catalog_product->getProduct($data['product_id']);
$url = $this->config->get('config_url').'product/'.$data['product_id'].'/'.$this->toSlug($product['name']);
break;
case 'product/category':
$this->load->model('catalog/category');
$categoryId = explode('_',$data['path']);
$categoryId = $categoryId[(count($categoryId)-1)];
$category = $this->model_catalog_category->getCategory($categoryId);
$url = $this->config->get('config_url').'category/'.$data['path'].'/'.$this->toSlug($category['name']);
break;
case 'information/information':
$this->load->model('catalog/information');
$info = $this->model_catalog_information->getInformation($data['information_id']);
$url = $this->config->get('config_url').'information/'.$data['information_id'].'/'.$this->toSlug($info['title']);
break;
}
if(!empty($url))
{
unset($data['route'],$data['path'],$data['product_id'],$data['information_id']);
$query = '';
if ($data)
{
foreach ($data as $key => $value)
{
$query .= '&' . $key . '=' . $value;
}
if ($query)
{
$query = '?' . trim($query, '&');
}
}
return $url.$query;
}
return $link;
} else
{
return $link;
}
}
public function toSlug($name)
{
$name = str_replace("'", "", $name);
$name = str_replace('"', "", $name);
$name = strtolower($name);
$name = preg_replace("/&#?[a-z0-9]+;/i","",$name);
$name = preg_replace('/[^a-zA-Z0-9-]/', '-', $name);
$name = preg_replace('/-+/', "-", $name);
return $name;
}
then change catalog/controller/common/seo_url.php
public function index()
{
if (isset($this->request->get['_route_']))
{
$parts = explode('/', $this->request->get['_route_']);
if($parts[0] == 'category' || $parts[0] == 'product' || $parts[0] == 'information')
{
switch($parts[0])
{
case 'category':
$this->request->get['path'] = $parts[1];
break;
case 'product':
$this->request->get['product_id'] = $parts[1];
break;
case 'information':
$this->request->get['information_id'] = $parts[1];
break;
}
}
else
{
foreach ($parts as $part)
{
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
if ($query->num_rows)
{
$url = explode('=', $query->row['query']);
if ($url[0] == 'product_id')
{
$this->request->get['product_id'] = $url[1];
}
if ($url[0] == 'category_id')
{
if (!isset($this->request->get['path']))
{
$this->request->get['path'] = $url[1];
} else
{
$this->request->get['path'] .= '_' . $url[1];
}
}
if ($url[0] == 'manufacturer_id')
{
$this->request->get['manufacturer_id'] = $url[1];
}
if ($url[0] == 'information_id')
{
$this->request->get['information_id'] = $url[1];
}
} else
{
$this->request->get['route'] = 'error/not_found';
}
}
}
if (isset($this->request->get['product_id']))
{
$this->request->get['route'] = 'product/product';
} elseif (isset($this->request->get['path']))
{
$this->request->get['route'] = 'product/category';
} elseif (isset($this->request->get['manufacturer_id']))
{
$this->request->get['route'] = 'product/manufacturer';
} elseif (isset($this->request->get['information_id']))
{
$this->request->get['route'] = 'information/information';
}
if (isset($this->request->get['route']))
{
return $this->forward($this->request->get['route']);
}
}
}
Published by Funky Dude on 20th July 2009
If you need to sum up or average a column on a table via bindModel. this is what you are looking for.
The table looks like this
CREATE TABLE IF NOT EXISTS `gather_reports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`session_id` varchar(64) NOT NULL DEFAULT '',
`type` enum('Category','Store') NOT NULL DEFAULT 'Category',
`name` varchar(128) NOT NULL DEFAULT '',
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`importedId` text NOT NULL,
`total` int(11) NOT NULL DEFAULT '0',
`imported` int(11) NOT NULL DEFAULT '0',
`duration` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
in your controller
$this->GatherReport->bindModel(
array('hasOne'=>array(
'GatherTotal'=>array(
'className'=>'GatherReport',
'foreignKey'=>'id',
'fields'=>'SUM(GatherTotal.total) as total'
),
'GatherImported'=>array(
'className'=>'GatherReport',
'foreignKey'=>'id',
'fields'=>'SUM(GatherImported.imported) as imported'
),
'GatherTime'=>array(
'className'=>'GatherReport',
'foreignKey'=>'id',
'fields'=>'SUM(GatherTime.duration) as duration'
)
)),false);
$this->paginate['GatherReport']['group'] = 'GatherReport.session_id';
$data = $this->paginate('GatherReport');
Published by Funky Dude on 30th June 2009
So how do you combine your favorite blogging app with your favorite framework so that user can signin from your cakephp app and maintain his/her session in wordpress, assuming user can only sign up and log in from your cakephp site.
The idea is to create a wordpress user when a user signs up on your cakephp site.
1. We need wordpress password hash function to do this properly. You need to create a new component that would contain wp password hashing functions. The file that contains everything you need is at wp-include/class-phpass.php. Just turn this class into a cakephp component, so you can include it in your user controller.
2. In your user controller, include your recently created wp hash function component. And after the user is saved into your cakephp user table. You want to add it to your wp user table as well. And it would look something like this.
if($this->User->save($this->data))
{
$wp_hash = new $this->Wphash;
$wp_hash->PasswordHash(8,1);
$blog_user['ID'] = $this->User->id;
$blog_user['user_login'] = $this->sanitize_user($email);
$blog_user['user_pass'] = $wp_hash->HashPassword($this->data['User']['password']);
$blog_user['user_email'] = trim($email);
$blog_user['user_url'] = 'http:;
$blog_user['user_nicename'] = $this->data['User']['firstname'].' '.substr($this->data['User']['lastname'],0,1);
$blog_user['display_name'] = $this->data['User']['firstname'].' '.substr($this->data['User']['lastname'],0,1);
$blog_user['user_registered'] = date(configure::read('dateformat'));
$blogdata['Blog_user'] = $blog_user;
$this->Blog_user->save($blogdata);
$bid = $this->Blog_user->id;
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'first_name';
$user_meta['meta_value'] = $this->data['User']['firstname'];
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'last_name';
$user_meta['meta_value'] = $this->data['User']['lastname'];
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'nickname';
$user_meta['meta_value'] = $email;
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'rich_editing';
$user_meta['meta_value'] = 'true';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'comment_shortcuts';
$user_meta['meta_value'] = 'false';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'admin_color';
$user_meta['meta_value'] = 'fresh';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
$user_meta['user_id'] = $bid;
$user_meta['meta_key'] = 'blog_capabilities';
$user_meta['meta_value'] = 'a:1:{s:10:"subscriber";b:1;}';
$blogdata['BlogUserMeta']= $user_meta;
$this->BlogUserMeta->save($blogdata);
3. In your wordpress index.php file
session_name("mysite");
4. in your theme header.php(might not be the best place if you change your theme a lot)
$user = $_SESSION['Auth']['User'];
if(!empty($user))
{
wp_set_current_user($user['id']);
}
And there you have it.
Published by Funky Dude on 30th June 2009
One of my bigger project requires me to setup personal sites on the main site’s subdomains. So i did what everyone would do and googled that.
It turned out to be quite simple. You just have to set session.cookie_domain to the domain of your site without www.
Like this session.cookie_domain = .mysite.com
Of course, you can set it with php using
ini_set("session.cookie_domain", ".localhost/mysite");
AND
You probably need to set cakephp security setting to medium or low. You can do that in core.php
Some people say medium is enough, but i needed to change minie to low in order for it to work.
Published by Funky Dude on 30th June 2009
Here’s a cool function i found today while i was working on a project.
it prints the first n sentence.
function getLeadingSentences($data, $max)
{
//given string $data, will return the first $max sentences in that string
//in: $data = the string to parse, $max = maximum # of sentences to return
//returns: string containing the first $max sentences
//(If the # of sentences in the string is less than $max,
//then entire string will be returned.)
//a sentence is any charactors except ., !, and ?
//any number of times, plus one or more .s, ?s, or !s
//and any leading or trailing whitespace:
$re = "^s*[^.?!]+[.?!]+s*";
$out = "";
for($i = 0; $i < $max; $i++) {
if(ereg($re, $data, $match)) {
//if a sentence is found, take it out of $data and add it to $out
$out .= $match[0];
$data = ereg_replace($re, "", $data);
}
else {
$i = $max;
}
}
return $out;
}
//EXAMPLE:
$start = "Sentence one... Sentence two? Sentence three! Sentence four.";
$end = getLeadingSentences($start, 3);
echo("result: $end");
[source]
Published by Funky Dude on 28th June 2009
Setting up cakephp in local development environment can be tricky, escpecially in windows.
There are two setups to every cakephp application, development and production.
Using development setup is easy, just extra cakephp to, let’s say http://localhost/webapp. And you are done.
To change to production setup, just change your document root to ROOT.’/webapp/app/webroot/’.
Even when you are using development setup, every request is still redirected to app/webroot.
So all your css, js and images should be put in webroot directory.
Since cakephp uses pretty urls. You can’t use relative links in your application. To get around this problem, i set up a virtual host in apache.
to do this, please visit this site.
Published by Funky Dude on 26th June 2009
It’s really powerful and flexible. Use it