Tuesday, February 19, 2013

[PgSQL] Updating environment variable for PostgreSQL 9 update

Yes, it's been a long time, but I haven't discovered anything major lately.

However, there is one thing that did bother me. A few months back, we updated one of our servers, especially with PostgeSQL, to version 9.

The problem is that some automatic backups and database dumps were not performing as they should, rendering many errors that took us a while to see. Some of these errors were:
undefined symbol: PQconnectdbParams

The other main problem was the fact that cPanel was thinking that the PostgreSQL server was offline. I knew this was not true because, well, the website was running fine.

After some research, I discovered that this was due to some kind of linking (tied to environment variables) not being updated as it should, and the PostgreSQL 9 library was not loaded. Therefore, some referenced functions were not found.

Now... there are two ways to achieve this. There is a slightly more complicated way that didn't really work for me (but might for you), and a much simpler way. It all depends on which access you have on your server.

If you DO NOT have root access to your server, but have the PostgreSQL root password, you can directly set the environment variable:

  • Make sure psql is running
  • Run this command: LD_LIBRARY_PATH=/usr/pgsql-9.0/lib psql
  • Enter the password

This is supposed to work (use DYLD_LIBRARY_PATH if on Mac OS X), but I was missing the PostgreSQL root password. I can't be sure if the change is permanent, or if it will be reverted on reboot.

The other way to do this is to automatically update the links/environment variables, but I believe you need root access to do this. It all goes through a simple command:
/sbin/ldconfig /usr/pgsql-9.0/lib

Wait a few seconds and you're done. You can refresh your cPanel PostgreSQL databases page or something if you want to see your result.

Had the documentation been updated with the latest links or had the problem occurred more widely, I would have found this way faster.

Hope this helps!

Friday, June 8, 2012

[ZF] Using usort() in a controller

Yes, I'm still alive.
... but very busy these days. Here goes!
Some of you probably tried using PHP's usort() method to... sort stuff. Well, it might seem unclear how it works with the Zend Framework.
As you know, this is how it should be used1:
usort ( array &$array , callable $cmp_function )

However, in a Zend Framework controller, you have to change things just a little bit to make it work:

usort($to_sort, array($this, 'sorting_method'));

I shouldn't have to explain that $to_sort is your array of data you want to sort and 'sorting_method' is your sorting method (duh).


1: http://www.php.net/usort

Wednesday, January 25, 2012

[ZF] Translating a page's title

So it's been a while, eh? Happy New Year (and Chinese New Year, while we're at it)!

For a project, we're finally in testing phase and all of a sudden, I point something out that looks wrong: the translator works perfectly... except for the page titles. By that, I mean the following tag:
<title>Title</title>
 
What gives? Well, my understanding is that the translator is not yet set in the Bootstrap when the
$this->view->headTitle($label);
is called.

The first thing I tried? Adding the translate() method to it:1
this->view->headTitle($this->view->translate($label));

... that didn't work at all. Simply no change. Great.

Then I put my thinking cap on (it's still early in the morning), and thougt this through. First of all, here is how the main parts of my Bootstrap file are laid out:
- Register plugins
- Register routes
- Prepare navigation (menus, prepare breadcrumbs, and set page title)
- Prepare translation
- Configure e-mail settings for sending stuff
- Prepare the view

Now this little brain of mine decided to kick in this morning, and made me realize: what if we prepared the translation before setting some text on the output page?

So there I am, moving the _initTranslate() method up a little, right over the _initNavigation(). Just leave the line of code that includes the translate() method ([1] above) when setting the page's title, and as long as $label is defined in your language file(s) and the translator is set correctly, shazam, it'll work like a beauty!

Final trick: you can prepend (or append, I guess) anything to your head title! In your _initView() method, be sure to do the following:
$view = new Zend_View();
$view->doctype('XHTML1_STRICT'); // Or whatever
$view->headTitle('Client - ');

// Add more settings here if needed

$view_renderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$view_renderer->setView($view);

return $view;
 
By doing this, all your pages' titles will look like "Client - Translated page title". Awesome, isn't it?

Friday, November 25, 2011

[ZF] Adding timepickers to forms

Validating forms is important to preserve data integrity throughout a project. In a user could simply enter any kind of data, this would generate many bugs and be a big nuisance security-wise.

One of the tricky input types (or let's say Zend_Form_Element types) is anything that has to do with dates. There are many ways to write the date and the time... here's a simple difference example:
USA: mm/dd/yyyy
Europe: dd/mm/yyyy

So if a user enters 05/11/2011, how can we know if we're using the 5th of november or the 11th of may as the date?

To make it easier for everyone, a simple datetimepicker is available on Binpress (← click me and follow instructions). If you're here, I'm guessing you know how to deal with PHP, Zend Framework, and you know how to RTFM (and know what that means).

The picker is based on jQuery. It's beautiful and awesome. You can configure it anyway you want and it'll work.

Right. So now your user can just click on a calendar and drag nifty little knobs to graciously select the time.

However, there is no easy way to actually have a simple timepicker as a Zend_Form_Element object.

This time is now over, and it's actually quite simple to have only the timepicker.

First of all, let's go to where the DateTimePicker is. If you've followed the simple instructions, the folder should be: \project\library\Core\Form\Element. Now duplicate the DateTimePicker.php file and rename the new one to TimePicker.php.

In this file, do the obvious: change Core_Form_Element_DateTimePicker for Core_Form_Element_TimePicker and 'dateTimePicker' for 'timePicker'.

Do the same thing for this folder: \project\library\Core\View\Helper... duplicate the DateTimePicker.php file and rename the new one to TimePicker.php, and in this file change Core_View_Helper_dateTimePicker to Core_View_Helper_timePicker, dateTimePicker to timePicker, and '%s("#%s").datetimepicker(%s);' to '%s("#%s").timepicker(%s);'.

If you're still smart, basically copy everything and remove the "date" part of it to only keep the time. Genius.

Next, well, you're done!

If you had this for a datetimepicker:
$element = new Core_Form_Element_DateTimePicker(
 $name,
 array(
  'jQueryParams' => array(
   'dateFormat' => 'dd/mm/yy',
   'defaultDate' => date('d/m/Y'),
   'timeFormat' => 'hh:mm'
  )
 )
);
Now you can simply have this for a timepicker:
$element = new Core_Form_Element_TimePicker(
 $name,
 array(
  'jQueryParams' => array(
   'dateFormat' => '',
   'defaultDate' => date('hh:mm'),
   'timeFormat' => 'hh:mm'
  )
 )
);
Oh, and just to make sure everything stays safe, let's add a little validator:
$element->addValidator('Date', true, array('format' => 'H:m'));
There you have it, a shiny little timepicker! If not, then try again or ask for help.

Now you can easily create sliders in a form so people can enter the time it took them to do something. Eat a donut, make coffee, take a bathroom break, go to work, sleep, anything! Awesome!

Friday, November 11, 2011

[ZF] Containing form elements in DIVs (or other HTML tags) with classes

Long time, no see.

So, let's say you're building a dynamic form and would like each dt (label) and dd (input field) pair to be grouped in a div,with one or more specific classes added to this div.

It's quite easy, actually!
// Wrap element in div with classes
$element->addDecorator(
array('openDiv' =>'HtmlTag'),
array(
'tag' => 'div',
'openOnly' => true,
'class' => $attrib_class
)
);
$element->addDecorator(
array('closeDiv' =>'HtmlTag'),
array('tag' => 'div', 'closeOnly' => true)
);

Considering your custom class name(s) are in the $attrib_class variable, this will output something similar to this:
<div class="class-1 class-2">
<dt id="item-id-label">
<label for="item-id">label</label>
</dt>
<dd id="item-id-element">
<input type="text" name="item-id" id="item-id" value="">
</dd>
</div>

And there you have it, customizable form elements!

Tuesday, August 23, 2011

[ZF] Adding links to form elements

Clicks, there's always too many of them.

Now imagine a form with a hundred checkboxes. Sure, you can pre-check all of them or leave them empty, but it would be much easier for the user to have a "select all" and "select none" buttons.

For more efficiency, it's better to have one for each set of checkboxes, and there's nothing better than to put these links as a decorator to the form element containing the checkboxes.


$form_element = new Zend_Form_Element_MultiCheckbox(

'name',
array(
'description' => '<a href="javascript: void();">Select all</a> <a href="javascript: void();">Select none</a>'
)
);



Now that's nice, but instead of having links, the HTML code has been escaped and that's of no use at all.

There's an easy trick to this: just set the decorators!


'decorators'	=> array(

'ViewHelper',
array(
'Description',
array('escape' => FALSE)
),
'FormElements',
'Form'
)



The important trick is to set the escape value to false, so that the HTML characters won't be converted.

And there we have it, two beautiful links that will same users a lot of hassle.

Friday, August 5, 2011

[ZF] Adding section titles in a Zend_Form

Do you have a Zend_Form with many elements and need to add section titles for it to be more user-friendly?

Nothing is as simple as this!

// Add section title
$personal_section = new Zend_Form_Element_Hidden('personal_section');
$personal_section->setLabel('Personal information');
$personal_section->setDecorators(
    array(
        'ViewHelper',
        'Description',
        'Errors',
        array('Label', array('tag' => 'h2'))
    )
);
$this->addElement($personal_section);


This basically creates a hidden input with a label, and we're just modifying this to use an H2 tag instead of the regular one. Not very nice technically, but very effective!