Thursday, December 30, 2010

New Firebug Autocomplete annoying with jQuery

Firebug is a great tool for web developers to debug webpages.

If you have installed the latest update from Firefox' Add-ons menu and tried to write jQuery commands into the console/command line of firebug you might have run across an annoying problem.

Firebug tries to autocomplete $, $(, $(', etc. adding $ and completing ' after the next character. Basically you have to keep hitting escape or delete to write your jQuery statement.

This problem is luckily fixed in the latest beta release. It is not available through the add-on menu in Firefox, but can be downloaded directly from the Firebug website. So head over to http://blog.getfirebug.com/2010/12/22/firebug-1-6-1b2/ and download it.

Source: http://code.google.com/p/fbug/issues/detail?id=3184.

Saturday, December 18, 2010

Windows 7 middle click mouse doesn't close tabs

In Firefox on Windows 7, closing tabs by clicking on them with the middle mouse button doesn't work. You only get the Alt+Tab 'window' to switch between programs.

To fix this:
- Go to Control Panel
- Select 'Hardware and Sound'
- Select 'Mouse' under 'Devices and Printers'
- In the drop-down box for 'Wheel button' change it from 'Flip' to 'Middle-click'
- Press OK

This should now enabling closing tabs with clicking the middle mouse button.

Sound cut-off in Deus Ex 1

Playing the old classic on Windows 7 gave me some trouble with speech being cut of about 1 second before it should have finished.

Read a few places about this problem and some suggestions about changing latency to 46 or 60 (in the deusex.ini file in the deusex/system folder), but this didn't work for me.

Changing from using Direct3D Support to OpenGL as the video device did.
Here is how:
- Open 'Deus Ex Safe Mode' from start menu
- Select 'Change your 3D video device'
- Select 'show all devices'
- Select 'OpenGL Support' and press next
- Accept settings suggestion (can be changed in-game later)
Enjoy!

If this doesn't work for you, here are some forums with suggestions for how to get it to work with Direct3D:
http://www.ttlg.com/forums/showthread.php?s=&threadid=50981
http://www.ttlg.com/forums/showthread.php?t=99140 (since DeusEx runs on the Unreal rendering engine)

Saturday, November 13, 2010

Workaround for Ubuntu install hangs on forward button

Have you tried installing Ubuntu using the official guide where you create a Live/Install USB using Universal USB Installer in Windows? Did the install not continue after pressing continue when selecting language or when selecting keyboard layout?

This seems to be because of gparted/parted not being able to start. It affects installing from Ubuntu 10.4-netbook 10.10-netbook, and 10.10-desktop.

A workaround is to create the Live/Install USB in Ubuntu.

You will need two USB drives (including the one you created in windows).

Start the Live session, get a copy of the iso you put on the USB onto the computer (either from a different machine or download it). Open the startup disk creator in you Ubuntu Live session (System->Administration->Startup Disk Creator). Select the iso file and the other USB drive.
Create the startup disk. Shut down, remove the USB created in windows. Start up using the USB created in Ubuntu to boot from. Install from the USB created in Ubuntu. The forward button should now work.

Sources: A Ubuntu bug report that I cannot find again. Please tell me if you have seen it so I can give credit.

Tuesday, October 26, 2010

Check if a Zend_Db_Table_Rowset is empty

When retrieving data from the database using Zend Framework (using find or fetchAll functions) you will be presented with a Zend_Db_Table_Rowset. To check if it is empty it would seem logical to use the empty function like when checking an array. This will not work.

To check if a Zend Rowset (Zend_Db_Table_Rowset) is empty you need to use the count function. e.g.:
if(count($result) > 0) {
//Do something
}

Or using the Rowsets own count function (which does not iterate through all the elements apparently):
if($result->count() > 0) {
//Do something
}


See http://www.mail-archive.com/fw-general@lists.zend.com/msg25411.html for a discussion on the topic.

Saturday, September 25, 2010

jQuery value and text of selected option in selectbox

You often need the value and text of the selected option of a select box. $("#selectboxid").val() returns the value of the selected option and $("#selectboxid :selected").text() returns the text.

Solution found at:
http://marcgrabanski.com/articles/jquery-select-list-values

Monday, August 23, 2010

Hilarious review of Windows 7

A bit late, but I ran across this hilarious review of 'Windows 7'.

http://www.cracked.com/blog/using-windows-7-may-lead-to-murder/

Check out what windows he has open in the task bar and also read the comments. Very funny. Enjoy! :)

Saturday, July 24, 2010

Sending images in MMS messages with Froyo on Nexus One

Upgraded to Android 2.2 Froyo and suddenly can't send images in MMS messages? (Mine just said Sending in the message view and never changed to Sent)
The problem could likely be that the images are too large to be sent (carriers usually have a max limit on image size or upload is so slow the it can't finish with a large image) and Froyo dosen't resize the image like 2.1 Eclair did.

To send images you need to crop them first. This will create a new image from a subsection of the original. (Not destroy the image being cropped tho). Do like this:

Open Gallery and open the image you want to send. Press the More button and choose Crop. Select the portion of the image you want and press Save.
You will now have the original plus a new smaller image.

Then, for the new image, do like usual, press Share and Messaging or choose Menu and Attach from inside a message.

Friday, July 16, 2010

Disabling spellchecker in NetBeans

I started using NetBeans and quickly encountered the annoying red underlining of all words in my code. Disabling the spellchecker feature in NetBeans 6.9 is NOT done by deselecting all files in the Options -> Miscellaneous -> 'Spellchecker' tab.

For NetBeans to really understand that you don't want spellchecker, do the following:
Go to Tools -> Plugins -> Installed tab -> select the spellchecker plugin -> press Deactivate -> restart NetBeans

Alternatively try adding the file type you are working on (if NetBeans doesn't recognize it) by following this post: http://www.herikstad.net/2010/07/filetypes-in-netbeans.html#links

Filetypes in NetBeans

Working with ex. .tpl files? Getting NetBeans to recognize most of the code (at least the HTML, JS, and CSS), and do code highlighting on it, is as easy as adding 'tpl' as an associated file type:

Follow http://www.waltervos.com/news/adding-filetypes-to-netbeans/.

UPDATE 24.03.2011: NetBeans have had a Smarty (.tpl) plugin for a while, but it hasn't been very good. Now it's getting much better. You can download it from withing NetBeans, go to Tools > Plugins > Available Plugins, search for smarty, check the checkbox beside the plugin and press Install.

(Make sure you have NetBeans Beta Update Center (See Settings tab of Plugins window URL: http://updates.netbeans.org/netbeans/updates/6.9/uc/final/beta/catalog.xml.gz for NetBeans 6.9))

NetBeans' blog posts about Smarty plugin: http://blogs.sun.com/netbeansphp/entry/initial_support_for_smarty_available and http://blogs.sun.com/netbeansphp/entry/smarty_update_custom_delimiters

Alternative to Eclipse IDE

So, getting tired of eclipse's infinite number of program/system halting tasks?

Maybe you should try Netbeans, it's not just for Java development. There are also plugins for PHP, Ruby, C/C++, and Groovy (whatever that is).

Check it out at:
http://netbeans.org/

Saturday, July 10, 2010

Opera icon in ubuntu gone

Did you have a shortcut to launching Opera and have it's icon disappear when upgrading your Opera installation? (e.g. to 10.60).

No worries, just open the shortcut properties (use http://www.herikstad.net/2010/05/setting-ubunutu-menu-item-icon.html if it is a menu or panel shortcut). The location you want to look for the icon is:
/usr/share/opera/styles/images.
Select the Opera_256x256.png file.

Voila. :)

Friday, June 25, 2010

Accessing elements cross-frame (HTML)

I know it is a hassle, but sometimes you need to maintain legacy code and come across someone using frames. To access elements in other frames you need to go to the top and 'back down into the frame you want':

<name of parent frame>.<name of sibling frame>.element
parent.mainframe.location.href = 'next.html'

Remember javascript variables that should be accessible cross-frame need to be global (not declared with var (var variablename)). Also for jQuery you use the document as a root to get elements from. e.g.:
parent.mainframe.document.myVar = 5;

Check out these pages for more information: http://forum.jquery.com/topic/jquery-getting-to-a-variable-in-the-parent-frame-stumped, http://anothertechnicalblog.blogspot.com/2009/08/seleccion-de-elementos-dentro-de-un.html

Sunday, May 23, 2010

How to get Flash on Froyo on the Nexus One

First of all you need to upgrade to Android 2.2 Froyo. Follow the guide at http://nexus404.com/Blog/2010/05/22/google-nexus-one-android-froyo-official-installation-instructions-android-froyo-almost-ready-for-your-nexus-one-you-can-install-it-alone-here’s-how/.

Next, you would think Flash was installed out of the box. It is not. To install it go to the Market, search for flash and select "Flash Player 10.1 BETA" (with an Adobe 'f' icon). Install this. Now you should be able to view flash content in your browser.

Solution found at: http://www.sizzledcore.com/2010/05/23/flash-player-10-1-beta-for-android-2-2/

Wednesday, May 5, 2010

Preventing web vulnerabilities

Two nice sites to check out when working with public web pages are:
http://jarlsberg.appspot.com/
and
http://ha.ckers.org/xss.html

Both are useful for learning about how your webpage can be exploited and how to prevent it.

Postscript delegate failed for ImageMagick PHP library

For ImageMagick in PHP through the library Imagick the following error may occur for versions ImageMagick-6.3.5 to ImageMagick-6.4.2:
Postscript delegate failed `': No such file or directory

This is a bug and the error appears when one is trying to use the readimageblob function to read a PDF file.

The fix has been posted and the bug is no longer present. See http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=11989. So to fix it get the latest release from http://www.imagemagick.org/.

Some installations (like server environments), haven't got the latest version and cannot install it. For these, it is possible to work around this bug, by using a temp file.

So instead of:
$im = new Imagick();
$im->readimageblob($data);


Use:
$im = new Imagick();
$temp = tmpfile();
fwrite($temp, $data);
fseek($temp, 0);
$im->readimagefile($temp);
fclose($temp);


(This would typically be followed by for example:
$im->setImageFormat("png");
$data = $im->getimageblob();
)

Sources: http://markmail.org/message/6mg3lxi26gd5fluv#query:readimageblob%20Postscript%20delegate%20failed+page:1+mid:6mg3lxi26gd5fluv+state:results

If this doesn't fix it for you, also check out: http://stackoverflow.com/questions/588918/imagemagick-imagick-convert-pdf-to-jpg-using-native-php-api

Saturday, May 1, 2010

Setting Ubuntu menu item icon

Every tried setting the icon for a menu item on the Ubuntu start menu and discover that ubuntu can't find your icon files?

First of all, to get to the place where you change the icon:
Right click 'Applications' menu on the taskbar -> select 'Edit Menus' -> find your menu item in the list -> click 'Properties' -> click the current icon

When you select Browse... and go to the directory where your icons are located it doesn't show any files there. The key is to press Open when you are in the directory where the files should be. A new window will open showing the files that can be used as icons.

"The image files must be one of the types: PNG, XPM, or SVG, and the extension must be ".png", ".xpm", or ".svg" (lower case)." Source: http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-0.6.html

Thursday, April 29, 2010

Undefined properties and showing all properties for Javascript/jQuery objects

To check if a Javascript/jQuery variable has been set use the following code:

if(typeof(myVar) !== 'undefined');

Solution source: http://quomon.com/question-how-to-check-if-a-javascript-variable-is-defined-891.aspx


To get all properties of a Javascript/jQuery object (to find what methods and values can be set for example) use the following function:

var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}


Or to list them in Firebug:
for(var key in obj){
console.log(key);
}


Solution source: http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object

Wednesday, April 21, 2010

Add InvalidHandler after jQuery validator initialization

If you have a problem where you need to add the option 'invalidHandler' to your jqueryValidate (jQuery Validation Plugin) after it has been initialized, this is how it can be done:
$(document).ready(function(){
$("#contactForm").bind('invalid-form.validate',
function(event, validator) {
alert('validation failed!');
}
);
});


Regularly you would add this on initialization:
$(document).ready(function(){
$('#contactForm').validate({
invalidHandler:
function(event, validator) {
alert('validation failed!');
},
rules: {}
});
});


Note: invalidHandler will be called when validation of form fails on submit (e.g. values for a field is missing or such).

This might work for other options of the jqueryValidate plugin, but I'm not sure which property to use. I found the property to bind to in the jquery.validate.js file, you might want to look there.

Thursday, April 8, 2010

Mount Nexus One SD card on PC with Ubuntu Linux

If you are trying to connect the Nexus One to Ubuntu via USB and it keeps disconnecting and you get the following messages when you run dmesg in a terminal:

[14082.844359] sd 23:0:0:0: [sdb] Sense Key : Not Ready [current]
[14082.844371] sd 23:0:0:0: [sdb] Add. Sense: Medium not present
[14082.844386] end_request: I/O error, dev sdb, sector 24
[14082.854502] sd 23:0:0:0: [sdb] Device not ready
[14082.854512] sd 23:0:0:0: [sdb] Result: hostbyte=DID_OK driverbyte=DRIVER_SENSE


The fix is turn on USB debugging for the Nexus One:

On the Nexus One, go to Settings -> Applications -> Development and check USB debugging and say OK to the warning. Connect the Nexus One via USB to the Ubuntu machine and it should connect fine.

Solution source: http://code.google.com/p/android/issues/detail?id=5880

Wednesday, April 7, 2010

IE javascript error "Invalid argument"

If you get one of the following errors in IE when running jQuery and/or jqGrid:

Message: Invalid argument.
Line: 112
Char: 247
Code: 0
URI: http://.../jquery-1.4.min.js


or

Message: 'p.cellLayout' is null or not an object
Line: 10
Char: 56008
Code: 0
URI: http://.../jquery.jqGrid.min.js


It is quite possibly that you are trying to set something to null. Check if any of the javascript variables in the page are null and being used as parameter to call a jqGrid or jQuery function.

NOTE: IE often doesn't specify the correct line number. So even though it may say line 10, the error could be somewhere else, this is why it is so hard to debug IE. The reason is dynamic pages where javascript libraries and content is added on the fly, IE can't keep up with this.

Tuesday, April 6, 2010

Imagemagick reading pdfs under Apache and Windows

Getting "Apache HTTP Server has stopped working" when trying to read a pdf file using the PHP imagick class when running Apache?:

If
<?php
$im = new imagick('file.pdf[0]');
?>

breaks your server. Try installing the latest Ghostscript windows binaries: http://pages.cs.wisc.edu/~ghost/

You should also have gotten an error if you tried to convert the pdf in commandline:
Unrecoverable error: undefinedresource in setcolorspace
Operand stack:
--nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- DefaultGray ColorSpace
START 0 2568152 1228867 1461800 167944 true 1134 5 <9>
END PROCS 6 2568152 1240166 1461800 169328 true 1133 5 <9>
gs_std_e.ps 13 2588248 1246221 1461800 170712 true 1133 5 <12>
gs_il1_e.ps 20 2588248 1248404 1461800 170712 true 1133 5 <15>
END FONTDIR/ENCS 25 2588248 1248582 1461800 170712 true 1133 5 <21>
END DEVS 31 2591744 1255606 1461800 170712 true 1133 5 <21>
END STATD 37 2591744 1262786 1461800 172336 true 1133 5 <45>
END GS_FONTS 44 2631936 1293632 1461800 172336 true 1134 5 <51>
END BASIC COLOR 50 2631936 1300193 1461800 172336 true 1132 5 <54>
END LEVEL 1 COLOR 55 2631936 1301921 1461800 172336 true 1132 5 <57>
END IMAGE 61 2652032 1308551 1461800 172336 true 1132 5 <60>
gs_btokn.ps 67 2652032 1311993 1461800 172336 true 991 4 <63>
gs_dps1.ps 73 2652032 1313338 1461800 172336 true 991 4 <63>
gs_dps2.ps 78 2652032 1315345 1461800 172336 true 991 4 <63>
gs_type1.ps 84 2652032 1316871 1461800 172336 true 991 4 <63>
gs_lev2.ps 93 2672128 1334882 1508160 219096 true 991 4 <63>
BEGIN RESOURCES 96 2672128 1337010 1508160 219096 true 991 5 <64>
END CATEGORY 100 2672128 1338270 1508160 219256 true 991 6 <64>
While reading gs_patrn.ps:
%%[ Error: typecheck; OffendingCommand: get ]%%
START 297633 2756688 1417603 1508160 221464 true 995 6 <67>
END PROCS 297638 2756688 1428886 1508160 222848 true 993 6 <67>
gs_std_e.ps 297645 2776784 1434941 1508160 224232 true 993 6 <70>
gs_il1_e.ps 297649 2776784 1437124 1508160 224232 true 993 6 <73>
END FONTDIR/ENCS 297653 2776784 1437302 1508160 224232 true 993 6 <79>
END DEVS 297657 2780280 1444326 1508160 224232 true 993 6 <79>
END STATD 297662 2800376 1454858 1508160 225856 true 993 6 <103>
END GS_FONTS 297667 2820472 1482354 1508160 225856 true 993 6 <109>
END BASIC COLOR 297672 2820472 1488915 1508160 225856 true 991 6 <112>
END LEVEL 1 COLOR 297677 2840568 1493995 1508160 225856 true 991 6 <115>
END IMAGE 297683 2840568 1497297 1508160 225856 true 991 6 <118>
gs_btokn.ps 297689 2840568 1500739 1508160 225856 true 991 6 <121>
gs_dps1.ps 297694 2840568 1502084 1508160 225856 true 991 6 <121>
gs_dps2.ps 297700 2840568 1504091 1508160 225856 true 991 6 <121>
gs_type1.ps 297705 2840568 1505617 1508160 225856 true 991 6 <121>

Friday, March 26, 2010

Custom message for Zend CSRF token

Ever see the following message when using Zends CSRF token?:

The token 'cea65eff4f464dsl57729a21fbd56aea' does not match the given token 'd2479b415baafe5791ase68f85122828a'

Wouldn't it be more useful to have an intelligible message? To set custom messages for the Zend_Form_Element_Hash do the following:

$this->addElement('hash', 'csrf_token', array(
'salt' => get_class($this) . '',
'timeout' => ,
'errorMessages' => array('Identical' => 'Form has timed out (probably), or you are attempting a cross-site request forgery. Please submit form again.'),
));


'Identical' is the validator that Zend_Form_Element_Hash uses. So adding the errorMessages line when creating the hash should do the trick.

There is probably a setErrorMessages() method also, but I haven't tried this.

Thursday, February 11, 2010

Zend PHPUnit check elements set by controller

When testing output from controllers in Zend using the Zend PHPUnit one might want to check the elements set by the controller, if they have the right values or exist at all.

Anything below top level form ($this->view->form = $form; in controller) one needs to use the getElement() function like one would in the controller. getSubForm() is also valid if there are subforms:

public function testRegisterViewObjectContainsProperties() {
$controller = new RegistrationController(
$this->request,
$this->response,
$this->request->getParams()
);
$controller->registerAction();

$this->assertTrue(isset($controller->view->form));

$this->assertTrue(isset($controller->view));
$this->assertTrue(isset($controller->view->form));
$username = $controller->view->form->getSubForm('userpass')->getElement('username');


http://www.contentwithstyle.co.uk/content/unit-testing-controllers-with-zend-framework seems to be useful.

Zend PHPUnit posting to controller

Sending data to the controller to check what you get back can be tricky. Especially if you have subforms or file uploads.

Here is how if you have subforms (in this case a select and a multiselect in separate subforms):
public function testPostMissingFilledFields() {
$this ->request ->setMethod('POST')
->setPost(array(
//Selected element with value '2'
'firstLayerSubform1' =>
array('secondLayerSubform1' =>
array('thirdLayerSubform1' =>
array('select' => array('2')))),
//Selected elements with values 1159 and 1161
'firstLayerSubform2' =>
array('secondLayerSubform2' =>
array('thirdLayerSubform2' =>
array('multiselect' => array('1159', '1161')))),
));


Useful functions to see what you submit look like are:
In Test file:
var_dump($this->request->getParams());
In controller:
var_dump($this->_request->getParams());

If you find a way to check for uploaded files please tell me. :)

PHPUnit with Zend View

When you want to check if important objects in Zend Views show up using PHPUnit testing can be used. In my case I have a registration form which repopulated the fields if one was missing or if anything was incorrectly filled in.

There seem to be multiple ways to select elements in the DOM:
- assertQuery - CSS selector
- assertXpath - Xpath selector
- strpos (!) - search through the html as a string

I first tried CSS selector:
$this->assertQuery('input[id="username"][value="test1"]'); //username
$this->assertQuery('input#username[value="test1"]'); //username
$this->assertQuery("input[value='test@asdf.com']"); //emailaddress


All should work, but there are different problems. The two first selections don't work, event though they work in firebug. The last line doesnt work either because of the period '.' which gives a "DOMXPath::query(): Invalid predicate" error.
Seems Zend PHPUnit converts from CSS to Xpath selectors and doesnt manage to do it right.

Therefore using Xpath directly is better. (Download the FireXpath extension to Firebug for Firefox and play around to learn it (also http://www.w3schools.com/XPath/default.asp is good)). So here's Xpath:
$this->assertXpath("//input[@id='username'][@value='test1']"); //username
$this->assertXpath("//input[@id='emailaddress'][@value='test@asdf.com']"); //emailaddress

Works out of the box.

Alternatively you can search through the html code linearly:
$this->assertTrue(strpos($this->_response->getBody(), 'test@asdf.com') !== false); //emailaddress
However, if anything changes in the html code, the test might fail.

Solution sources: http://framework.zend.com/manual/en/zend.test.phpunit.html, http://www.tig12.net/downloads/apidocs/zf/, others I cant remember.

Friday, February 5, 2010

Atomic transactions in Zend

If you have many database changes where all need to succeed or none at all do the following:

//Get a hold of the database adapter:
$model = new Model_DbTable_();
$adapter = $model->getAdapter();
$adapter->beginTransaction();
try {
//Do inserts, updates, deletes etc.
//Commit the changes and do rollback if it fails:
$adapter->commit();
}
catch (Exception $e) {
$user->getAdapter()->rollback();
//Print errors
$echo $e->getMessage();
}


or similarly:

$db->beginTransaction();
try {
$db->insert('server', array('key' => 'foo', 'value' => 'bar'));
$db->insert('server', array('key' => 'bar', 'value' => 'baz'));
$db->insert('server', array('key' => 'baz', 'value' => 'foo'));

$db->commit();
}
catch (Exception $e) {
$db->rollback();
$e->getMessage();
}


Solution found: http://devzone.zend.com/article/1367

'not found in haystack' error in Zend

If you get a '%value%' was not found in the haystack' error when using select boxes in Zend, add the following to the form element:

$element->setRegisterInArrayValidator(false);

or directly when creating form:

$form->addElement('multiselect', 'elementname', array('RegisterInArrayValidator' => false));

Solution found at: http://stackoverflow.com/questions/991001/zendvalidatebetween-strange-error-message