October 26th, 2010
For Magento 1.4.1.1.
It’s not obvious how to link to a page through the WYSIWYG editor. There doesn’t appear to be any valid documentation on it in the Magento website either. Thankfully I found this post: http://screencastworld.com/2010/03/magento/magento_tips_tricks_and_hacks/magento-tips-tricks-and-hacks-4-how-to-link-pages-using-store-url-short-code
Basically {{store url=”page-slug”}} will be replaced by Magento as if you called $this->getUrl(“page-slug”). It’s easiest to just swap to HTML edit mode and insert that code into the href manually.
Filed in Uncategorized | No Comments »
October 26th, 2010
Using Magento 1.4.1.1.
After reading a seemingly simple tutorial at http://www.exploremagento.com/magento/override-a-magento-core-block-class.php I ran into some problems. To override the catalog modules breadcrumbs you use the following XML:
<config>
<global>
<blocks>
<catalog>
<rewrite>
<breadcrumbs>Fido_Catalog_Block_Breadcrumbs</breadcrumbs>
</rewrite>
</catalog>
</blocks>
</global>
</config>
This is fine for the catalog module, but how do you replace the standard breadcrumbs in 1.4.1.1 which are in core/Mage/Page/Block/Html/Breadcrumbs.php? It took me quite a bit of trial and error, but the key here is the declaration of the block in page.xml:
<block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>
The type is what indicates how to override. Specifically “page” and “html_breadcrumbs”. To properly override the standard breadcrumbs you would use the following XML:
<config>
<global>
<blocks>
<page>
<rewrite>
<html_breadcrumbs>AIBooks_Page_Block_Html_Breadcrumbs</html_breadcrumbs>
</rewrite>
</page>
</blocks>
</global>
</config>
The rest of the article should apply just fine. In my package I mirrored the core code with app/code/local/AIBooks/Page/Block/Html/Breadcrumbs.php with your XML going into app/code/local/AIBooks/Page/etc/config.xml.
Tags: breadcrumbs, Magento, override, php
Filed in Magento, Uncategorized | 6 Comments »
October 25th, 2010
This is relevant for Magento 1.4.1.1.
I have been unable to find any decent documentation on how Magento handles the <action method=”…”> element and helpers within the arguments. After some debugging I found the method responsible for handling these elements. Take a look at app/code/core/Mage/Core/Model/Layout.php on line 289 at the _generateAction method. This method takes the <action> element as $node. It loops through all the child nodes (what would be the method arguments) and turns them into an associative array. If any child node has the “helper” attribute specified it will use that helper to get the value. You can pass arguments to the helper method with further child nodes. This is better illustrated through an example:
<reference name="breadcrumbs">
<action method="addLink">
<label>New Link</label>
<url helper="core/url/getUrl"><page>new-page</page></url>
</action>
</reference>
This has the effect of making the following calls:
Mage_Page_Block_Html_Breadcrumbs::addLink(
'Add Link',
Mage_Core_Helper_Url::getUrl('new-page')
);
The above is merely an example the addLink and getUrl methods do not exist. As far as I’ve found there’s no way to get a generic URL through any helper. In addition the element names for the arguments are only by convention. They are passed in order and the names do not matter.
So in summary:
- The <action> element calls the “method” attribute on the parent block’s class. Mage_Page_Block_Html_Breadcrumbs::addLink.
- The child elements are passed as arguments in order. <label> and <url>.
- Any child elements specifying the “helper” attribute will use that class and method to determine their value. Mage_Core_Helper_Url::getUrl.
- Any child elements of an element specifying the “helper”attribute will be passed as arguments to the helper method. <page>.
Unfortunately you cannot use helper methods any further down the chain. For example a call to the addCrumb method might look like this:
<reference name="breadcrumbs">
<action method="addCrumb">
<crumbName>newlink</crumbName>
<crumbInfo>
<label>New Link</label>
<link>new-link</link>
</crumbInfo>
</action>
</reference>
The crumbInfo argument is an associative array. While Magento will construct the array correctly you cannot use a helper on the <link> element where it would be quite useful.
Filed in Magento | 3 Comments »
October 13th, 2010
This is the first in a, hopefully, useful series of quick how-tos for Magento. All of them will be referencing Magento 1.4.1.1.
To add a new layout to Magento that is available in the CMS you’ll need to edit
app/etc/local.xml
To insert the <page> element like:
<config>
<global>
<page>
<layouts>
<three_box module="page" translate="label">
<label>3 box</label>
<template>page/3box.phtml</template>
<layout_handle>page_three_box</layout_handle>
</three_box>
</layouts>
</page>
</global>
</config>
To make the layout default you can also add
<is_default>1</is_default>
The layout can now be selected in the CMS as “3 box”. It will use the page/3box.phtml to render in whatever theme you are using. For example:
app/design/frontend/base/default/my_theme/template/page/3box.phtml
To reference the new layout in your layout XML files you use the <layout_handle> value. In our example, <page_three_box>.
Filed in Magento | No Comments »
May 14th, 2010
I recently tried installing the Canon EOS Utility on my new laptop. After checking Canon’s website I found that they only offer updates, no actual installer. For that you need the CD that came with your camera. Like most people I lost mine long ago. I’m not sure why Canon wants to make it so hard to use their software without the original CD. It’s not like you can use it without buying one of their cameras. The only conclusion I can come up with is that they are completely fucking retarded.
We can get around their retardedness (it’s a word, shut it) by editing their updater. This will only work with the older versions. As of this writing the 2.61 updater works. Download the updater here (I think all cameras have the same EOS Utility) http://www.usa.canon.com/consumer/controller?act=DownloadIndexAct and grab the latest version as well while you’re at it. Now extract the 261 updater and copy out UpdateInstaller to your Desktop. Then right click and go to Show Package Contents. Go to Contents/Resources/ and delete update.plist. Now run the installer. Once that is finished you can run the latest updater and it will recognize your previous install. The latest updater (281) doesn’t contain the update.plist file, so we have to install the old one first, then update.
I got most of this info from http://www.northlight-images.co.uk/article_pages/install_canon_software.html which includes instructions for doing the same thing in Windows.
P.S. Canon I love your hardware, but your software department is run by imbeciles.
Tags: Canon, eos utility, lost cd, updater
Filed in Canon, mac, Photography | 3 Comments »
March 29th, 2010
I like CakePHP’s error validation quite a lot. I use it in nearly every case because it’s easy and it works. There are times when the standard error validation scheme does not work. Like when you have a field that you want to validate manually in the controller, but still use CakePHP’s error display method. There is no method to simply inject an error into the existing validation system. After a bit of searching I finally found where the error messages are being stored. The error messages are stored on the model instance in the controller, such as $this->User->validationErrors. When the controller renders the view it copies each model’s validationErrors into the view. To add your error it’s as simple as adding another item to that array that matches your field.
For registration that requires a promotional code:
<?php echo $form->input('promo'); ?>
Then after checking the promo code you can set an error message like this:
$this->User->validationErrors['promo'] = 'Invalid promo code';
The promo code isn’t really part of our User model, it’s only used during registration. However, as far as I see you must have the field associated with a model to add that error message. I suppose an alternate option would be to make a Promo model and associate that with your controller, but that seems like too much work.
Tags: CakePHP, errors, validation
Filed in CakePHP | No Comments »
February 2nd, 2010
I recently tried using dompdf in a CakePHP project to easily generate PDFs from HTML. I ran into a rather obtuse error while trying to render a page and generate a PDF in the same controller action.
Fatal error: require_once() [function.require]: Failed opening required 'vendors/dompdf/include/htmlhelper.cls.php' in vendors/dompdf/dompdf_config.inc.php on line 194
That makes it seem a lot like dompdf is trying to include one of its files and failing. What’s really happening is dompdf is specifying an __autoload function which PHP is attempting to use to load the HtmlHelper class used by CakePHP. dompdf blindly includes the file if it exists or not thinking only its files would be loaded this way. To get around this and load the files normally, simply edit the DOMPDF_autoload function to make sure the file exists before including.
function DOMPDF_autoload($class) {
$filename = DOMPDF_INC_DIR . "/" . mb_strtolower($class) . ".cls.php";
if(is_file($filename)) {
require_once($filename);
}
}
I found this snippet after much searching over at http://www.dashinteractive.net/dompdf/index.php?v=3278826, hopefully this post will save someone else a few minutes of googling.
Filed in CakePHP | No Comments »
January 21st, 2010
I recently ran into a problem while writing a plugin in CakePHP. I wanted to do some authentication with one of the models in my app. I added this in the beforeFilter of my AppController. In there I was using $this->loadModel to load the model on the fly. This works fine inside the app. However, as soon as you move to a plugin things break down. It will try to load the model inside your plugin. Since that model doesn’t exist in the plugin, it will fail. You can specify a plugin in the loadModel call, like ‘Plugin.Model’, but you can’t specify no plugin. The way to get it to load correctly is the $uses variable. That will load the correct model when initializing the controller. Then you can use it as normal with $this->Model. This has the side effect of always loading the model on every controller. In my case that was perfectly fine since its used for auth on all actions. I can’t see any viable way to use loadModel to load a model outside of your plugin.
Filed in Uncategorized | 1 Comment »
October 15th, 2009
This no longer works in Snow Leopard
I was trying to do this again after upgrading to Snow Leopard and the OS no longer handles extended attributes in the same fashion. Attributes are now in a new style and the old style attributes you can only print out the hex. To get the value you have to do xattr -px com.apple.ResourceFork file_name | xxd -r -p > file_name.ttf. I tried this a few times and even though the file contents matches up exactly with the resource fork, the font does not appear to work. I also noticed that simply copying or moving the individual fonts on my Mac would cause them to stop working.
I just ran into a very weird issue today trying to transfer fonts from a Mac to a PC. I had several fonts on my mac that were working, installable, usable, and showed as having a size. When I tried sending them to a friend they would show up as 0k. I tried sending individual fonts, as a zip, via gmail, from a link, nothing worked. I decided to take a look at the files, opening them in a text editor showed nothing. Listing their file size in the terminal showed them as 0 bytes. The only place I could think of that data being stored was somewhere in the file attributes.
Back in OS 9 Mac fonts used to store their data in the resource fork. Normally this is used for storing metadata about a file. Someone at Apple decided that putting fonts in there was a good idea. Now in OS X the resource fork has been moved to the extended attribute com.apple.ResourceFork. Extended attributes are not used in file size calculations by terminal. If you want to see the extended attributes you can use xattr -l file_name. Running that on the font file showed a giant chunk of data in com.apple.ResourceFork that was definitely the font info. Mystery solved.
Converting the file is easy once you figure this out. You can simply dump the resource fork into a file with the extension .ttf and OS X will interpret it as a PC true type font. Use xattr -p com.apple.ResourceFork file_name > file_name.ttf to get the data. This will allow you to copy the file to a PC. I make absolutely no promises as to how well this works, but it worked for me. You can also try converting using transtype http://www.fontlab.com/font-converter/transtype/
Filed in mac | 3 Comments »
August 20th, 2009
I recently ran into the problem of having to redirect a user back to their requested url after logging in. In CakePHP you would normally use Auth::redirect() and that handles it for you. However, I had already written a custom authentication function in my app_controller. I simply wanted to figure out which url the user had requested, save that in the session, and send them back there after a successful login. This may seem like a very simple thing to do, but getting your current URL isn’t so easy in CakePHP.
The best solution I found was to use Router::getParams(). This will return you an array of info about the request. The currently request url, relative to you CakePHP install, will be in will be in ['url']['url']. The URL does not have a starting slash, unless the requested page was ‘/’. For example if you have CakePHP installed in http://www.mysite.com/cake_test/ and you request http:/www.mysite.com/cake_test/users/edit/15 the URL will be ‘users/edit/15′. So to add this into my auth controller I simply added:
$request_params = Router::getParams();
$this->Session->write('auth_redirect','/'.$request_params['url']['url']);
Then after a successful login you can simply:
$this->redirect($this->Session->read('auth_redirect'));
Tags: authentication, CakePHP, redirect
Filed in CakePHP | 1 Comment »