Ordering VirtueMart Child Items in Dropdowns

Ok, so a ton of people keep asking me about how to order child products in the dropdown list. I don’t develop with VirtueMart much anymore so the suggestions I give here are mere suggestions and have not been tested or put through any sort of trial. I’m just looking at the code and guessing at what to change.

This exploratory exercise uses VirtueMart 1.1.6 stable.

If it works for you let me know. If it kind of works for you but you fix something and it works better, let me know and I’ll update here.

  1. So let’s take a look at ps_product_attribute.php
  2. Somewhere in there is a bunch of functions that start with ‘list_attribute’. Let’s look at those.
  3. function list_attribute seems to be the main one that calls the other variations. It looks like by default it calls up the drop down menu for attributes. (line 221)
  4. Since that one is the default, let’s see if making modifications to any of its queries will give us the results we want. The function starts at line 241.
  5. At line 273ish you should see a SQL query: $q = “SELECT product_id,product_name FROM #__{vm}_product WHERE product_parent_id=$product_id AND product_parent_id<>0 AND product_publish=’Y'” ;
  6. This is the first query you’re going to play with. You can tack on any sort of ORDER BY directives that you wish:
    ex $q = “SELECT product_id,product_name FROM #__{vm}_product WHERE product_parent_id=$product_id AND product_parent_id<>0 AND product_publish=’Y’ ORDER BY product_name” ;
    I just ordered the SQL results by product name. You can put in whichever column or set of columns you wish to order by. It’s up to you.
  7. Now take this same principle and try it throughout the various ‘list_attribute’ functions and see if you can achieve the sorting results that you desire.

HTML5 Isn’t ready for primetime, W3C says

According to the W3c, HTML5 hasn’t yet reached a level of maturity that would make it useful to the whole of the internet. Some browsers are capable of supporting some of the features while others don’t.

According to Infoworld:

HTML5, which updates the HTML specification to accommodate modern Web applications, has gained a lot of adherents in vendors like Microsoft, Google, and Apple. But the specification is plain not ready yet for deployment to websites, an official with the World Wide Web Consortium (W3C), which oversees HTML5, stressed this week.

I agree. It’s awesome and has great potential, but let’s not get ahead of ourselves. If we start making major time investments in developing sites in HTML5 now before it is a final standard, we could end up wasting time developing in the wrong direction. We’ll get there soon, but be patient. It’s probably more than acceptable to have fun and give the new potential tags and features a whirl….just be careful. You might shoot your eye out.

Specify Stylesheets for Different Media Types with $html helper

I’m expanding my CakePHP experience and now I’ve delved into displaying the application on multiple devices and of course, print.

Print stylesheets can be tricky to design, but it took me a while to figure out how to have cake automatically add them to the template using the $html helper.

&lt;?php 
	//you specify the file name first, then the media types at the end.
	echo $html-&gt;css('simple', null, array('media'=&gt;'print'));
	//
	
	echo $html-&gt;css('regular', null, array('media'=&gt;'screen')); 
	echo $html-&gt;css('simple', null, array('media'=&gt;'print')); 
?&gt;

This will output something like this:

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/simple.css&quot; media=&quot;print&quot; /&gt;

CakePHP: Join results from multiple tables

Have you ever wanted to expand on your pre-set model queries with some table joins or other fancy footwork? You can join your model query to another table or tables like so:

				
$market = $this-&gt;Market-&gt;find('list',
	array(
		'joins' =&gt; array(
		array(
			'table' =&gt; 'companies_markets',
			'alias' =&gt; 'CompaniesMarkets',
			'type' =&gt; 'left',
			'foreignKey' =&gt; 'CompaniesMarkets.market_id',
			'conditions'=&gt; array('CompaniesMarkets.market_id = Market.market_id')
		)
	),
	'conditions' =&gt; array(
			'CompaniesMarkets.company_id' =&gt; $company
			),
			'fields'=&gt;array('Market.market_id','Market.market_name')
	)					
);

What’s we’re doing here is specifying the ‘joins’ option along with a slew of other pieces to designate what to join, how, and by which field. The ‘conditions’ simply sets which fields I want to check for a certain value and which fields I want to use for this list.

How To: CakePHP form input without labels

One of the great things about CakePHP is how easy it is to get very common tasks accomplished in a short amount of time. Form building is no exception, but there are many options to specify – and at times those options are not immediately apparent.

The standard input code syntax appears like this:

input(string $fieldName, array $options = array())

To have your form inputs not show labels, you can specify ‘label’=>false in the input options.

For example:

<?php
echo $form->create(‘User’, array(‘action’ => ‘login’, ‘class’=>’header-login’));
echo $form->input(‘username’, array(‘label’ => false));
echo $form->input(‘password’, array(‘label’ => false));
echo $form->end(‘Login’);
?>

This will show your form inputs without the label elements.

Demystifying Web Development: What It Means to Be a Modern Web Developer

The world of modern web development is not understood by anyone outside of it. The intricacies, the intense segmentation, and the true strength of the professional community is often overlooked by the layperson.

“So what do you do?” they ask. “I’m a web developer.” I pause for a moment. “I build and design websites and web applications.”

“Oh cool I used to do some of that at my old job with Frontpage.”

I’ve had about a million conversations with people that start off like this. For sake of brevity, I usually leave things at this – but secretly I’m steaming inside. Not necessarily because they’re not familiar with my realm, but because how the profession is written off as something almost trivial. The value is constantly being eroded and stereotyped, making educating clients and consumers that much more difficult. This is partly because to get your hands dirty in development the only thing you really need is a text editor and a web browser. That’s where many people think development ends.

If web development was an iceberg that would be the 18 feet sticking out of the ocean. What you don’t see is the remaining monstrosity that sinks the hive-mind Titanic as it approaches and blows people’s minds so much so that they nearly refuse to acknowledge it exists or is even nearly as complex as it truly is.

Being a web developer means three things:

  1. You program for the web – anything you program or develop is meant to be executed on a web server or presented to an end-user in a browser
  2. Most of the work you do is never seen or understood by those who use it. They just accept that it works and don’t ask questions as to why or how.
  3. You have a specialty. You are awesome using AJAX, web design, PHP, Haskell, ASP.net, Actionscript, Zend Framework or any of the other 149.54 million tools, methods, languages, and utilities that help make web development such an interesting field.

This escapes a lot of people even in 2010. Think of it in terms of construction. You want to build a house (or website) so you draw up plans that meet your needs, find a suitable plot of land for it, start estimating costs, and start assembling a team to complete the job. You’re going to need a general contractor or foreman, a drywall crew, framing crew, clearing crew, electricians, plumbers, masonry crew, cabinet crew, and the list goes on. And within each of those general fields there are also long lists of specialties. You have to be careful to select the right candidate for the right purpose and stay mindful of each crew’s specialties and core competencies. Don’t expect your master masons to be able to run electrical wiring…

The same is true in web development. Given the nearly endless array of specialties and qualities that development professionals possess, the world that consumes the fruits of our labor needs to know what it truly takes to make a monster like facebook, a twitter, a blog, or even a digg or reddit. This comes through on sites like craigslist where someone wants to create the next facebook or myspace – for a budget of $220 and a 24 pack of Miller Light. There is no concept to what it takes to actually create something from the ground up, make it function properly, design a great user interface, and get people to start using it.

As professionals we all develop specialties to serve a certain purpose – it’s about time we stop generalizing and start educating. The next time someone asks what you do for a living tell them as succinctly as possibile – any of the following are good examples:

  • “I’m a web developer. My main focus is developing applications that balance the servers for large sites like facebook and myspace so that they will run smoothly even though 18 million people are using them at one time”
  • “I’m a web developer specializing in designing highly usable web sites and applications. I make the web sites your love easier to use – so it just works the way you expect it to.”
  • “I’m a web developer and build the technology that powers web-based video streaming so you can watch the latest “Keeping up with the Kardashians” on hulu.”

Don’t cop out. Tell people what you do – don’t be mad at them for having no idea what it means to be a developer. Educate them.

Joomla! migration headaches

Recently I moved an installation of Joomla! with VirtueMart to a new web host. The previous web host had restricted most of the export functionality in cPanel so I had to FTP everything over to the new host manually.

This created a lot of extra headaches. The most persistent was not being able to consistently save/update/create users in the administrative back-end. Every so often the form would time out and give the error message ‘Error Loading ModulesMySQL server has gone away’.

It took around 24 hours of constant searching, but I finally found a solution that worked for me at the Joomla forums.

Basically it states that the configuration.php needs to be edited so that it is in UTF-8 encoding without BOM. You can open the file in Notepad++ and set the encoding to this, then upload it and VIOLA.

Hopefully this saves someone else a headache.

Find and Replace a letter/number and batch update with MySQL

I had the need to do a batch update of a large MySQL data table that held thousands of products and their information, including SKU numbers. Recently the company has changed how they manage the SKU numbers and needed to replace the leading ‘2’ with a ‘4’ for simple accounting tracking.

It was easy enough to use REGEX to find which ones started with ‘2’, but I was still unsure of how to replace the ‘2’ with a ‘4’ in all of the necessary records.

I knew that if the SKU started with a ‘2’, that it had to be changed to a ‘4’ so I started out using the IF() function to compare SKU’s as follows.

SELECT sku, IF(TRIM(LEADING '2' FROM sku) &lt;&gt; SUBSTRING(sku FROM 2),sku,INSERT(TRIM(LEADING '2' FROM sku),1,0,'4')
)
FROM product

So what this does is select all the SKU’s in the table and return the ones that need to be changed with what the change would look like. The first TRIM() function takes all of the SKUs and trims off the first 2, then it is compared to a substring of the original SKU starting at the second position. If there is no leading ‘2’ in the SKU, then it will not match the substring since nothing was trimmed.

The next argument is telling the function what to return if the TRIM() and SUBSTRING() values differ. It will return the original SKU since we don’t need to change it if it doesn’t start with ‘2’.

The last argument is telling it what to return if TRIM and SUBSTRING are equal. For this value I’m trimming the leading ‘2’ and INSERTing a ‘4’ at the first position of the SKU.

The above query lists the original SKU and what it should look like once I put it into an UPDATE query.

The UPDATE doesn’t look much different:

UPDATE product
SET sku =
IF(
  TRIM(LEADING '2' FROM sku) &lt;&gt; SUBSTRING(sku FROM 2),sku,INSERT(TRIM(LEADING '2' FROM sku),1,0,'4')
)

This actually updates the table with the changes that I wanted. There may have been a more elegant way to do this with REGEXP but this did the trick. I verified that only the SKUs with a leading 2 were changed to a leading 4 and the others were left as they were.

Ubuntu 10.04 LTS: First Thoughts

I have Ubuntu installed on my laptop as a dual-boot and on my work PC inside a VirtualBox. I use it for all kinds of things that Ubuntu just does better – especially heavy development.

After yesterday’s public release of Ubuntu 10.04 LTS I decided it was time to get my hands dirty and install the new version.

A few things stuck out during the first boot and installation process:

  • Slick Graphical Interface during installation (see screenshot)
    4-30-2010 1-39-49 PM
  • More emphasis on making the user aware of features: sliding panels explaining features and benefits during installation just like Windows OS install process
  • Seems more readily geared towards a wider user base – features like music store, built-in social networking and instant messaging platforms
  • It boots up in seriously just a few seconds. Really great benefit.
  • Default theme and desktop configuration are attractive…and surprisingly not orange/brown!4-30-2010 1-53-32 PM
  • Tweaked window layout: note the ‘close’, ‘minimize’ and ‘maximize’ buttons in the top left
    4-30-2010 1-56-47 PM
  • The feel of the OS seems much more familiar than it used to. I’m impressed and I think this is a great leap towards a wider user-base.

Well, those are some first-impressions. I’m really excited about the level of polish that has been reached with Ubuntu 10.04. Props to everyone involved in the project.

Dynamically Sync Your Files With Amazon S3

I’ve been getting used to using the cloud over the past week or so and I’ve got to say it’s much easier to manage than I had imagined. I know there are tons of great desktop clients to manage your S3 buckets and files, but I wanted something to do all of the management for me – to make things simple.

I’ve put together a script using Undesigned’s awesome PHP S3 class that will scan a directory, compare all of the files in the directory to those in a given directory on your bucket and then upload missing files or replaced unmatched files.

You could set this PHP script up to do a lot more than it does currently, as it was set up to be a quick and dirty solution. Here’s the basis of it. You can download the full script and classes below.

//check to see if the file exists and can return file info
if (($info = $s3-&gt;getObjectInfo($bucket, $uri)) !== false) {
//if the local hash doesnt match the remote hash, upload and replace the file
if($info['hash'] &lt;&gt; $local_md5){
print_r('Can\'t match file: '.$the_file.' -- Now uploading');
echo "Putting $the_file . . . at $uri ";
//this will upload files with private permissions - you need to set the permissions as
//needed by changing S3::PRIVATE to whichever level you need.
if($s3-&gt;putObject(S3::inputFile($object), $bucket, $uri,  S3::ACL_PRIVATE,
array(),
array( // Custom $requestHeaders
"Cache-Control" =&gt; "max-age=315360000",
"Expires" =&gt; gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
)))
echo "OK\n";
else
echo "ERROR!\n";
}
else{
echo "The hash values for $the_file are equal ";
}
}else{
//file doesn't exist in your bucket, so upload it
print_r('Can\'t find that file: '.$the_file.' -- Now uploading');
echo "Putting $the_file . . . at $uri ";
if($s3-&gt;putObject(S3::inputFile($object), $bucket, $uri,  S3::ACL_PUBLIC_READ,
array(),
array( // Custom $requestHeaders
"Cache-Control" =&gt; "max-age=315360000",
"Expires" =&gt; gmdate("D, d M Y H:i:s T", strtotime("+5 years"))
)))
echo "OK\n";
else
echo "ERROR!\n";
}
Get the Class Get the sync script