linuxit

The linux way to do things

Display new products in random order on your homepage with Magento 1.8

By default, Magento displays new products the way it retrieves it which may be based on their id or their start date.  No matter which one it is, a simple problem quickly arise: what if you want do display more products than permitted by your theme or your own preferences?  For example, let’s say that I want to show 4 new products at once on my homepage but I have 20 in total I would like to display in a random rotary fashion.  With the default magento way of handling things, you are stuck, there is now way to do such thing.

There are 2-3 ways to achieve this not using the admin but I found all of them overcomplicated (to be fair, one of them is really simple: all you have to do is change the order by clause of the sql query that returns the new products by adding RAND() to it but I hate modifying magento’s core because even when done the proper way, which is to copy the file to modify in app/code/local, there always is a chance that a future upgrade will cause you troubles by let’s say modifying that precise core file thus, making your own local copy incompatible with the new installation forcing you to adapt your code to the new version anyway).  The following way of achieving it your best choice if like me, you are using a custom theme since upgrading Magento will never affect any of these files (upgrading your theme will do but you usually never have to do so).
The most easy method I could think of was to simply edit the template file in charge of rendering the new products which in my case was: app/design/frontend/default/theme569/template/catalog/product/new.phtml

Now change the line $i=0; foreach ($_products->getItems() as $_product): for $productList = $_products->getItems(); shuffle($productList); $i=0; foreach ($productList as $_product)

And voilà!


Wait until an jquery ajax request is completed before continuing

Sometimes, you need an ajax request to be completed to allow an application to continue as the rest of the process depends on it.  An example of this could be for instance when you are updating via ajax some database entries upon an user’s action.  Before allowing the web application to continue, you want to be sure that the row has been properly updated.

The typical way to achieve this is to add a delay using setTimeout or some obscure function (see http://stackoverflow.com/questions/1183872/put-a-delay-in-javascript) but of course, this method has a major drawback: you can never be absolutely sure that the delay will be long enough to let the operation finish.

A better way would be to simply specify that we want our ajax request to be sync instead of async (the default) by using {async: false} in our request :

$.ajax({
   type: "POST",
   async: false,
   url: "/ajax/newMemberGroup.php",
   data:  'name=' + $("#newMemberGroupInput").val() +'&selectOption=' + $("#memberGroup").html()
   }).done(function( msg ) {
   $("#memberGroup").html(msg);
});

That way, the rest of the code will only be executed once the ajax request is completed.  The only drawback of this method is that if your ajax request takes a long time, the browser can be locked for a long time (since we are waiting for our ajax request to be finished before continuing) but for a majority of cases where all you need is a quick  but crucial, operation, this method will do it!


Samba: permission denied when mounting a share from linux

Recently, I’ve been facing a weird issue with my samba server.  I tried mounting a share as I always did using :

[hudony@fedh mnt]$ sudo mount -t cifs -o username=raall //10.11.1.15/commun /mnt/commun/
Password for raall@//10.11.1.15/commun:  ********

and for some obscure reasons, I kept getting the following error message:

mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

However, the issue was not affecting windows and mac workstations.

Since the samba log files were not helpful about my issue, I searched and searched again the web for a solution and I finally got the solution while looking at some forum posts.  It seems that a recent change in the linux kernel has been released involving that from now on, the default authentication for cifs mounts needs to be at least ntlmv2 security.

The solution is to simply add the security parameter to the mount command like this:

[hudony@fedh mnt]$ sudo mount -t cifs -o username=raall,sec=ntlmv2 //10.11.1.15/commun /mnt/commun/

That’s it, problem solved!


Organize your pictures according to the date they were taken

When it comes to manage my pictures library, I must admit I am very undisciplined. I usually let pass many months between the time I take a picture and the time I transfer it to my computer so it is very hard to organize them by the date they were taken. Moreover, I am sometimes in a hurry so I just transfer them in a “temporary” place telling myself that I will organize them later (sure…).

In short, for years, my pictures library has been a total mess. Lately, I wanted to see my child evolution through years and months to finally quickly realize that my neglicence has made this simple task now impossible. In the end, all I wanted was to organize them by the date they were taken, not by the event they were attached to.

I recently solved my problem by writing a short bash script that does exactly that. First the script scans a source folder looking for jpg or jpeg files. Now for each file found, the script will look at his EXIF information (if it is not possible, the creation date of the file will then be used) to determine the date it was taken and copy it in a destination folder in an organized way using a Year, Month and Date format.  Files with the same name will be automatically renamed and you can choose to copy (default) or  instead move the pictures found.

The basic usage is the following one:

[hudony@fedh ~]$ ./organizePictures.sh -h
Usage: ./organizePictures.sh [-moqh] SOURCE DESTINATION
-m Use move instead of copy
-o Overwrite duplicate files instead of renaming them
-q Quiet, do not display informations on the console
-h This help message

Continue reading “Organize your pictures according to the date they were taken” »


Quickly analyze multiple log files

If just like me, you are using logrotate to manage your logs, you are probably also using its “compress” options (if not, then you should) which gzip (by default) the log after rotating it in order to save up some space.  Recently, a customer called me complaining that he didn’t receive an email sent to him few days ago.  I then proceeded to ask him which exact day the email was sent but he wasn’t sure since the sender had emptied its “sent items” list (???).

In short, I had to find it through all my postfix log files (we keep a week long and daily log files are compressed).  The usual way would be to unzip and analyze each log file until we find what we are looking for.  Doing that for 7 files isn’t that bad but what If we had 30 files to analyse, there must be a better way…

Indeed, to quickly solve this issue, you can use a for loop and the cat command :

[hudony@smtp log]# for i in $(ls maillog*.gz);do gunzip -c $i >> /tmp/logfile; done

The result is only one log file (which could be very big so be careful) containing all the useful data.