Magento: Origins

We’ve worked with Magento quite a bit in the past and created some top-notch websites for our clients. The developments we’ve done have mainly been new site builds, meaning we can start from a fresh install and give the admin backend straight to the client, and familiarise them with it really quickly.

Recently, we’ve been working with a couple of existing e-commerce sites that need to be migrated to Magento, so we’ve started putting together some scripts to automate importing categories, product information, attributes, options, cms page etc. I thought I’d put together a series of posts that go into a bit more detail about each of these scripts.

In this series of articles, I’ll be using the best tools available, putting together custom scripts and – to remain flexible – I’ll be looking on the forums to find out if someone’s coded it before!

Magento has a virtualised database relationship model which makes it very extensible, but this can also make it difficult to interact with the data directly (for example, stock levels aren’t kept with product information), so in order to help this data-integration process Magento provides Dataflow and a SOAP API.

So, as Magento stores all the sites information in the database such as content, categories and products, there’s a lot to cover, so in this first post I’ll take a look at CMS pages.

Magento contains some straight-forward CMS page management. It’s fairly basic, but this fact certainly helps – it lets you pretty much get away with anything – you can add any HTML you want and go to town on the layout. However, CMS pages aren’t currently included within the Dataflow / API, so we need another programmatic way to interact with the system.

Looking at the database structure for Magento, CMS controlled pages use only two tables – so it shouldn’t be too difficult to put something together.

The two tables in question, are:

m_cms_page
m_cms_page_store

Looking at the structure of these tables, all the columns should be obvious – the only ones you need to fill in to get a page to show are the following: title, root_template, meta_keywords, meta_description, identifier, content_heading, content, is_active

Knowing this, it’s just a matter of getting all of your data in the right order. In my example below, I’ve stored it all in a tidy CSV as it was the easiest way to extract it from the previous system, but you could easily amend this to read from a plain text file, a serious of form fields or some XML.

 
<?php
 
/**********************************************
// import CMS pages script
// for Magento 1.5.x
// input - CSV file
 
// this is doing some direct mysql queries as nothing is available via dataflow / api
**********************************************/
 
// database information
mysql_connect('localhost','root','');
mysql_select_db('');
 
if(mysql_error()){die(mysql_error());}
 
// up the memory depending about 
ini_set('memory_limit', '32M');
 
/**
 * Class to import CMS pages
 *
 * @category    Mage
 * @package     Magento Import Scripts
 * @author      Douglas Radburn @ Branded3 (http://www.branded3.com)
 */
class MagentoCMSImport {
 
	public function __construct($filename)
	{
		// set the filename of the CSV we're going to import
		$this->filename = $filename;
		// set the prefix for the database
		$this->prefix = 'dq_';
	}
 
	/**
	 * Parse CSV file
	 * @return none
	 */
	public function readInput()
	{
		if($file = fopen($this->filename, 'r'))
		{
			while (($line = fgetcsv($file)) !== FALSE)
			{
				$count++;
				list($title, $content, $meta_keywords, $meta_description) = $line;
 
				$data['title'] = $title;
				$data['root_template'] = 'one_column';
				$data['meta_keywords'] = $meta_keywords;
				$data['meta_description'] = $meta_description;
				$data['identifier'] = $this->seoSafe($title);
				$data['content'] = $content;
 
				$this->createCMS($data);
				sleep(0.5);
			}
		}
		else
		{
			die('A file was specified, but the file could not be opened.');
		}
	}
	// end of readInput
 
	/**
	 * Create a new CMS entry in the database
	 *
	 * @param array $data
	 * @return none
	 */
	private function createCMS($data)
	{		
 
		$sql = "INSERT INTO " . $this->prefix . "cms_page SET
		title = '" . addslashes($data['title']) . "',
		root_template = '" . $data['root_template'] . "',
		meta_keywords = '" . addslashes($data['meta_keywords']) . "',
		meta_description = '" . addslashes($data['meta_description']) . "',
		identifier = '" . $data['identifier'] . "',
		content = '" . addslashes($data['content']) . "',
		creation_time = NOW(),
		update_time = NOW()";
 
 
		try {
			mysql_query($sql) or die('Sorry, an error occured doing some sql. ' . mysql_error() . ' ' . $sql);
			$id = mysql_insert_id();
			mysql_query("INSERT INTO " . $this->prefix . "cms_page_store SET 
						page_id = '" . $id . "',
						store_id = '0'") or die(mysql_error());
			}
		catch (Exception $e) {
			echo '<p>Sorry, error occured while trying to save the attribute "' . $label . '" Error: '.$e->getMessage().'</p>';
		}
	}
 
	/**
	*
	* seoSafe() takes a string and returns a URL-safe version of it
	*
	* @param string $str The string to clean
	* return string Cleaned string
	*/
	function seoSafe($str) { 
 
		$str = strtolower($str);
 
		$allowed_chars = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
		"q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0","-","_", ".");
 
		$len = strlen($str);
 
		$new_str = "";
 
		// we'll allow spaces, but replace them with - so we know what we're doing
		$str = str_replace(" ", "-", $str);
 
		for($i=0; $i<=$len-1; $i++) { 
			if(in_array($str[$i], $allowed_chars)) {
				$new_str.= $str[$i];
			}
		}
 
		return $new_str;
 
	}
 
}
 
 
 
$filename = './import_cms.csv';
 
if($filename != '')
{
	// magento import categories
	$MagentoCMS = new MagentoCMSImport($filename);
	// go read the file, and import all the pages
	$MagentoCMS->readInput();
}
else
{
	echo 'No filename has been suggested.  Please enter a filename.';
}
?>

I’d recommend clearing out the existing pages that are in the database initially before import so that you don’t create any pages which clash. If you do import your pages, it’s a good idea to take a look over the database after the import so that you can ensure there aren’t any issues.

And there you have it. You should now have all your page data imported into Magento, and off you go!

By Douglas Radburn. at 9:48AM on Monday, 01 Aug 2011

Doug is our Senior Open Source Web Developer. Having gained some informative insight and technical experience at two major digital agencies after graduating; Doug brought his knowledge and skills to Branded3 in 2009, and has been solving our development dilemmas ever since. Follow Douglas Radburn on Twitter.

comments

Comments are closed.