Subscribe To RSS Feed


Mike D's SharpBlog

Just another Sharpdot weblog

Archive for the ‘PHP’ Category

Drupal: Complex View Layout, Getting Down and Dirty!

- Tuesday, April 6th, 2010 -

Drupal provides a ton of great functionality, but every now and then you get a designer, Amber :) , who just try’s to come up with a design for a Drupal site that challenges your knowledge of the system. Well thanks to the challenge I have found a nice bit of functionality to allow very custom views.

My challage was to have a block created by a View, pull the top 3 news nodes (basically like a blog) and display it on the homepage side-bar. Well that is not difficult, I even created a template for the block. Well now we wanted to show additional data for the first item in the view and not for the rest.

Question: Well, there is not a loop or counter to check for in the template file so how was I to get this done?
Answer: Using a Views Attachment Display.

Drupal Views have a display type called attachment. It is exactly as its name sounds,  a view that attaches to another. You can set it to display the additional data that you would like to display for the first item(or how ever many you want) then tell the Attachment Display to display before/after another View. Just change the offset of the main view to compensate for the items brought in by the Attachment.

Hope this helps others.

Tags: , ,
Posted in Drupal, PHP | No Comments »

Magento: Import Multiple Images or Remove Images durring Batch Import

- Tuesday, March 2nd, 2010 -

This article will detail how to adjust the Magento Batch Import (Dataflow) to allow uploading multiple gallery images, and also how to add a way to remove all images during Import/Update.

These are the accompaning files: (Please reference this file if you have any quetion about the code examples below. The WYSIWYG editor was messing things up. mainly “&’s” and “empty()’s” )
1. Full Code Example

First we are going to have to modify the the adapter used to save/update products during the import process. The file used is located here: “/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php“. Since we are going to be modifying this file we should extend it so we don’t change any core files. There are plenty of articles that detail how to extend/customize Magento files so I will not go into it.

First lets tackle adding multiple gallery images. These are images that appear on the products detail page below the main image. We will be making all our edits in the Product.php file mentioned in the previous paragraph. Find the SaveRow method. In this method find the code where Magento saves the products images(should be similar to the following code and around line 630).

        $imageData = array();
        foreach ($this->_imageFields as $field) {
            if (!empty($importData[$field]) && $importData[$field] != 'no_selection') {
                if (!isset($imageData[$importData[$field]])) {
                    $imageData[$importData[$field]] = array();
                }
                $imageData[$importData[$field]][] = $field;
            }
        }

        foreach ($imageData as $file => $fields) {
            try {
                $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields);
            }
            catch (Exception $e) {}
        }

First thing we will do is change the addImageToMediaGallery so that when it saves the image it is not excluded(saves you from having to re-edit the products after import to make the images appear.).


        foreach ($imageData as $file => $fields) {
            try {
            	// changed to not mark imported images as 'excluded' - see http://www.magentocommerce.com/boards/viewthread/6971/P15/ and http://www.magentocommerce.com/boards/viewthread/40007/
                //$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields);
                $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields, false, false);
            }
            catch (Exception $e) {}
        }

Now we can add the code to import multiple gallery images. Just below the code above add this new code:

            //Now add each gallery image.
        if (!empty($importData['gallimg'])) {
			//$importData['gallimg'] should be a list of images to import seperated by ";"
			$images = explode(";", $importData['gallimg']);
			foreach($images as $image){
				//Don't add field for gallimg's
				try {
					//made second param null so it is not added as the main image, small or thumb. just added to the gallery
					$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . trim($image), NULL, false, false);
				}
				catch (Exception $e) {}

			}
        }

In order to use this new code you need to add a new column to your import spreadsheet. Label the column “gallimg”. This needs to be a list of the images you want to import separated by a “;”.
Now that was not too hard was it?

Now we can address removing images during the import/update process. In order to remove all the images you will need to continue modifying the saveRow method. Add the following code just above the code we previously edited.

        //MRD added to remove all images for product before uploading new images
        if(isset($importData['remove_all_images']) && $importData['remove_all_images']=="yes" ){
	        //check if gallery attribute exists then remove all images if it exists
	        //Get products gallery attribute
	        $attributes = $product->getTypeInstance()->getSetAttributes();
        	if (isset($attributes['media_gallery'])) {
				$gallery = $attributes['media_gallery'];
				//Get the images
				$galleryData = $product->getMediaGallery();
				foreach($galleryData['images'] as $image){
				//If image exists
			    	if ($gallery->getBackend()->getImage($product, $image['file'])) {
			            $gallery->getBackend()->removeImage($product, $image['file']);
			        }
				}
			}
			#$gallery->clearMediaAttribute($product, array('image','small_image','thumbnail'));
        }
        //END Remove Images

To utilize the code we just created you will need to add the “remove_all_images” column to your spreadsheet and set the value to “yes”.

This was written using Magento version 1.2.1 so your files could be slightly different, but should still work if you follow the instruction. If you have any problems please leave a productive comment and I will see how I can help.

Posted in PHP, magento | 19 Comments »

Magento Error: You cannot define a correlation name … more than once.

- Thursday, February 4th, 2010 -

This will be a short post about an error I ran into when messing around with the Magento “Toolbar” that is displayed for search results and on category pages. I decided that i needed to extend the toolbar block in-order to achieve some custom functionality that a client needed. Then I noticed that when I sorted by price I got the following error “You cannot define a correlation name ‘_price_order_table’ more than once“.  In my case I had extended the setCollection function.

My problem(Error) began because I was calling the parent method then was setting the order on the collection again, and Magento did not like it.

     public function setCollection($collection)
    {
        parent::setCollection($collection);
        //The next three lines were what were causing the problem.
        //Because setOrder was called in the parent function it threw the error when it was called again.
        #if ($this->getCurrentOrder()) {
        #    $this->getCollection()->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        #}
        //Custom code went here
        return $this;
    }

Posted in PHP, magento | No Comments »

Magento: Problems/Errors after upgrade to 1.3.x with the Flat Catalog

- Friday, October 23rd, 2009 -

I recently decided to upgrade a Magento Site from version 1.2.1 to version 1.3.2.4 and the upgrade went smoothly, so I thought. When trying to enable the new caching features(Flat products and Categories) added in ver 1.3, I was unable to build the flat catalog for the categories and products. I kept getting “Flat Catalog Category rebuild error”. This was very disappointing as this was a major reason to do the upgrade.

Well not to fear, the problem was that the hosting company I used when installing Magento did not allow the “innodb” storage engine. This meant that the tables were using the  MyISAM  storage engine instead. This was the root of the problem. Keep reading to get the solution.

Luckily the Magento Team has released a Database Repair Tool. (Download here)
If you need to use the Database Repair Tool Through trial and error I found it is MUCH better to run it before updating versions. If you do it after updating(“to just fix the flat catalog error”) I have ran into other errors I believe to have been caused by updating a site with a corrupted database.
The instructions are easy to follow and worked perfectly, but here is the gist of things. You create a fresh install of Magento(same version as your site). You put the repair tool script in the web root and fill in the connection info for the damaged database and the fresh installation database. Then the script fixes broken foreign keys, storage engine types, etc.

After running the repair tool I was able to build the Flat catalog categories and products tables.
Problem Solved.

Posted in PHP, Sql, magento | No Comments »

Magento: Adding Image Uploads to TinyMCE

- Thursday, February 19th, 2009 -

If you are using Magento and want to add a WYSIWYG Editor I recomend the Fontis Plugin (Fontis WYSIWYG Editor).  This editor allows you to use TinyMCE or FCKeditor.  The FCKeditor had upload built in. If you want to use TinyMCE Keep reading.

First download ibrowser, this tutorial was written using 1.3.8. (Download Ibrowser)

Install the files to your tinyMCE plugins directory. Mine was: /js/fontis/tiny_mce/plugins/ibrowser.

Open the config.inc.php (tiny_mce\plugins\iBrowser\config\config.ini.php) and set your image path. I set it to: /media/uploads/. Be sure that the file path exists. I like to use dynamic folders so I uncomment: “$cfg['ilibs_inc']” and enter the base path for my folders in “$cfg['ilibs_dir']“.

Next copy the tinyMCE.editor_plugin.js file to the ibrowser plugin directory. This file is located here (ibrowser/ingerface/tinyMCE.editor_plugin.js). Rename the editor_plugin.js file ( in \tiny_mce\plugins\iBrowser) to editor_plugin_src.js. Now rename the tinyMCE.editor_plugin.js (in \tiny_mce\plugins\iBrowser\interface) to editor_plugin.js and move to \tiny_mce\plugins\iBrowser.

*Note: Depending on the version of tinyMCE you may need to use one of the other files labled with tinyMCE in the tiny_mce\plugins\ibrowser\interface directory.

Lastly  you need to add code to the javascript function that created the editor. My file was located in /app/design/adminhtml/default/default/template/fontis/wysiwyg/wysiwyg.phtml.

Find This javascript code: (yours could look slightly different)

tinyMCE.init({
mode : "exact",
elements : editable_areas,
theme : "advanced",
strict_loading_mode : true,
width: "640",
height: "400",
content_css: "<?php echo $this->getSkinUrl() ?>tinymce_content.css",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
relative_urls : false,
});

Insert the below code just under (theme_advanced_statusbar_location : “bottom”,).

plugins : "ibrowser",
theme_advanced_buttons3_add : "ibrowser",

Tags: , ,
Posted in PHP, magento | 16 Comments »

PHP extension Errors, pdo_mysql.dll

- Monday, November 17th, 2008 -

I had a problem getting the pdo_mysql extension to work properly.  I figured out that inorder to use extensions beginning with pdo you need to also have php_pdo.dll in the extensions as well. The php_pdo.dll extension must also be added to the php.ini file before any of the other extensions with pdo in the name. Hope This helps anyone else who might be stuck.

Posted in PHP | No Comments »