Friday, June 05, 2009

Gmail imap connect and seen/unseen

Some quick notes on imapping gmail (notice the novalidate-cert part... might save you some headaches), marking messages as read, and also about formatting PHP code for blogger. NOTE: code below has error checking removed for clarity.

$mbox = imap_open ("{}INBOX", "", "your");

$numMessages = imap_num_msg($mbox);
for ($jj = $numMessages; $jj > 0; $jj--) { //always have to go backwards, since message ID is dynamic
$header = imap_header ($mbox, $jj);
if ('U' == $header->Unseen) { //This message is unread
$body = '';
$struct = imap_fetchstructure($mbox, $jj);
if (!empty ($struct->parts)) {
$numParts = count ($struct->parts);
for ($kk = 0; $kk < $numParts; $kk++) {
$part = $struct->parts[$kk];
if ('PLAIN' == $part->subtype) {
$body = imap_fetchbody($mbox, $jj, $kk + 1);
} else {
$body = imap_body ($mbox, $jj);
imap_setflag_full($mbox, $jj, "\\Seen"); // mark as read
Formatting: I still don't have a good method of formatting for blogger. For this post, I used "Convert Special Characters into HTML Entities" by Stanley Shilov (Thanks!), switching to "Edit HTML" in Blogger, putting Stanley's optput between "pre" open and close tags, and then switching back to "compose" mode to clean up the output. There's GOT to be a better way... if anyone knows of any, please yell.

Thursday, June 04, 2009

Permissions for Magento Upgrade

Just updated my "live" and local servers to Magento ver. via the admin area, "System" menu, "Magento Connect", "Magento Connect Manager". I needed to set permissions on the live server, and used Magento: Magento Connect Manager - Save Settings - Permissions and How to resolve the file permissions error in Magento Connect Manager?

I got a lot of permissions errors when setting the permissions on the command line, and also a couple (for index.php and .htaccess) when installing (will have to talk to NuBlue about how to update them next time). However, since the only change in the two files was stuff for the super-new compiled stuff, I feel that I can address that problem next time.

For my local server, I simply downloaded the latest version, deleted my "magento" DB, created a new empty one, and installed (checking back with Max Berndt's "Getting Started with Magento Ecommerce!" to make sure I didn't miss anything). To install French, I downloaded the French pack, downloading the "Full Package", and copying the two fr_FR folders to the appropriate folders in Magento.

Wednesday, June 03, 2009

Zend Studio for Eclipse autocompletion hinting

I had problems getting auto-complete to work for my "product" member variable (of type Mage_Catalog_Model_Product) working in Zend Studio for Eclipse. I found the solution in this article by HanaDaddy. The below works for me... typing in $this->product-> now gives me auto complete.

* @var Mage_Catalog_Model_Product
var $product;

Debugging Magento: Vista, XAMPP, & Zend Studio for Eclipse

Here are some notes about what worked for me for being able to debug Magneto in Zend Studio for Eclipse using XAMPP on Vista.

For debugging with PDT & xDebug, use this excellent site. My php.ini for xDebug:
zend_extension_ts = D:\xampp\php\ext\php_xdebug-2.1.0-5.2-vc6.dll

Download the Window's Installer version of XAMPP. I have all 4 programs (MySQL, Apache, FileZilla FTP Server, and Mercury SMTP server) all running as services that boot on startup. Make sure you have at least XAMPP version 1.7.1. Afterwards, follow the EXCELLENT Magento installation instructions by Max Berndt (thanks Max!).

I don't have full notes here, but I do remember that I had a lot of problems, and in the end used the zend debugger from the PDT even though I own a licensed version of Zend Studio for Eclipse. I downloaded org.zend.php.debug.debugger.win32.x86_5.2.15.v20081217.jar from, and used 7zip to open the jar and extract ZendDebugger.dll from the resource/php5 directory, and copied it into C:\xampp\php\ext\. I then commented out all the lines from the [Zend], [XDebug], and [DEBUGGER] sections of the php.ini file. I then added the following 3 lines to the [DEBUGGER] section before rebooting Apache...
I don't remember where I got the exact allow_hosts, but I do know that the default values that I had caused Apache to crash, and the above finally worked for me.
Here you might also want to refer to the article PHP Debug with Zend Debugger And Eclipse PDT Tutorial Part 1.

The following assumes that Magento can be reached from
- From the "Run" menu, select "Debug Configurations"
- On the left, right-click on "PHP Web Page" and select "New"
- Name: Magento
- Server Debugger: Zend Debugger
- PHP Server: Click on the "New" link
- Name: Magento Server, URL (do NOT use localhost - I don't have notes on this, but I'm sure that I had trouble accessing the admin area using local host, and switching to fixed the problem).
- Hit next, going to the Server Path Mapping, and "Add"
- Path on Server: the windows path to your magento dir, e.g. C:\Users\Ed\workspace\magento
- Path in Workspace: /magento
- Click Finish
- Now back in the "Debug Configurations" dialog, set file to magento/
- uncheck "Auto Generate (URL)", and set the path (first part of which is auto-generated) to /magento
- In the "common" tab, click "run" and "debug" for showing in the favorite menus.

Hopefully, the above will work for you...

Tuesday, June 02, 2009

Programmatically Importing Product with Images in Magento

Although written in 2009, this article is still consistently one of the best read articles on the site. Use it at your own risk. If someone knows for sure that it still works (or doesn't!), please let me know.
It took me quite a while to figure out how to import products into Magento including images. The long version is below, the final results are here for those that need a quick fix, with thanks to articles from Darryl Adie (to show me 95% of the solution) and tza79 (who showed me how to set the visibility, a.k.a. mediaAttribute). Start with the code from Darryl, and then add the following...
//This call is needed since the media gallery is null for a newly created product.
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery ($fullImagePath, array ('image'), false, false); 
$product->addImageToMediaGallery ($fullSmallImagePath, array ('small_image'), false, false); 
$product->addImageToMediaGallery ($fullThumbnailPath, array ('thumbnail'), false, false);
Long, boring version:

I understood quickly with debugging (and with the help of Branko Ajzele's article) that the media gallery used in magento/app/code/core/Mage/Catalog/Model/Product.php was the object that I wanted to use, but for a newly created product, product->getMediaGalleryImages() returns NULL.

Product attribute "media_gallery_images" is only set in getMediaGalleryImages, when $product->getMediaGallery is already set, but $product->getMediaGallery is null for a newly created product - this is the cause of getMediaGalleryImages returning null.

product->setMediaGallery doesn't exist - but then again, neither does getMediaGallery (even though it's called in $product->getMediaGalleryImages)! What's going on here?!? This led me to several articles, which led to people talking about PHP5's Overloading. OK, maybe I should have known this already, but it was a cool discovery. However, it didn't help me solve my problem.

Here's where I lost a lot of time... I decided my best bet was to debug the admin area, to see how they create the media gallery. I was able to get Zend Studio for Eclipse to debug the admin area... a task which I will document shortly.

With the debugger, I could see that it was the ProductController that was creating the media gallery while loading the admin part, but EXACTLY where it was creating it, I was not able to find before it was time to go to dinner yesterday. In any case, if you want to debug this yourself, I can give you a hint: start in the "newAction" routine in /magento/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

Rather than going back to debugging the admin area today, I decided to take a good look at what an empty media gallery looks like, by creating a product in the Magento admin area, and then loading it with...
$mediaGallery = $product->getMediaGallery();
print_r ($mediaGallery);
Hmmm, it's nothing more than an array of arrays. Why not make it myself using setMediaGallery? Bingo!