Zend_Translate & TMX adapter – Source language strings as id

Thomas Weidner describes some additional functionality only available from Zend Framework 1.10.2+ and forward, for those of us that do *not* wish to use a separate message id/key but rather the source language string in the source code.

Set the “useId” option to false.
In this case the source language is used as message key and the source must be set as first translation.





Nachricht 1
message 1


Note the ‘useId’ => false below;

$translate = new Zend_Translate('tmx', $file, $locale, array('useId' => false));

Then “Nachricht 1” will be used as message key instead of “0001”.
Note that this works only as with 1.10.2 (or trunk as of today).

Jayson Minard: Yes, I Crashed the Site!

Jayson Minard wrote a very good article on upgrading a production site and what can go wrong and what we can learn from it.

Yesterday, I performed an upgrade to a third-party package used with Zend Developer Zone. It has an automated schema update system which silently performs actions on the database that had a large impact on ZDZ and related sites causing an outage. So, there are good lessons from my post-mortem that I would like to share with the community.

The Start of the Problem

First, let us look at the actual list of actions that started the issue:

1. The upgrade does a schema check on first load

2. The upgrade then corrects the schema to be valid for the new release (performing table changes via DDL)

3. The upgrade then may modify large amounts of existing data, or delete large amounts of old data

These schema and data updates can cause huge potential issues when the database and tables are used concurrently by the online site. First, the DDL changes will lock the affected tables. And for some storage engines (i.e. MyISAM in MySQL) the modifications or deletes will also cause table locks, and in other engines they could cause contention on locked rows, and in other engines cause things like rollback segments to overflow. The online site then waits behind the locks or contention causing threads in the web server to be held until no more threads are left to serve actual user requests. No more threads, no more site.

Learn from the problem; via Yes, I Crashed the Site!.

CSS Rounded Corners In All Browsers (With No Images)

n the past two years, increased browser support has transformed CSS3 from a fringe activity for Safari geeks to a viable option for enterprise level websites. While cross-browser support is often too weak for CSS3 to hold up a site’s main design, front-end developers commonly look to CSS3 solutions for progressive enhancement in their sites. For instance, a developer might add a drop-shadow in Firefox, Safari and Chrome using -moz-box-shadow and -webkit-box-shadow, and then be comfortable with this design enhancement falling off for IE users.

But wouldn’t it be great if IE users could see the better version of the page? Fortunately there are cross-browser hacks for the more common CSS3 attributes. These hacks not only allow CSS3 attributes to work in all browsers, but in turn allow designers and developers to use CSS3 in the central styling of their sites.

In this article we’ll walk through getting rounded corners working in all browsers. Firefox, Safari and Chrome are easy with the border-radius property, but we’ll have to jump through some extra hoops to get it working in IE and Opera.

After only a few compromises, we will have CSS rounded corners working in all browsers, and without using any images. Welcome to the ‘No Image UI Club’!

Read the entire article here; Jon Raasch’s Blog.

App_Controller_Helper_Params for JSON and XML POSTs

Matthew Weier O’Phinney shares a bit of very useful code to inject request params into a Zend Framework request object from a JSON or XML POST request.

“Below is a plugin I use to translate JSON or XML raw post request data to request user parameters.
Note that it expects a “Content-Type” header of either “application/json” or “application/xml”. If those are detected, it then does the translation and injection.
Once it has, you can then simply access the parameters from your request object like any others.
I actually use this with dojox.data.JsonRestStore already, so you should be set. :)”

class App_Controller_Helper_Params extends Zend_Controller_Action_Helper_Abstract { 
/**
 * @var array Parameters detected in raw content body
 */
 protected $_bodyParams = array(); 
/**
 * Do detection of content type, and retrieve parameters from raw body if
 * present
 *
 * @return void
 */
 public function init() {
 $request = $this->getRequest();
 $contentType = $request->getHeader('Content-Type');
 $rawBody = $request->getRawBody();
 if (!$rawBody) {
   return;
 }
switch (true) {
 case (strstr($contentType, 'application/json')):
   $this->setBodyParams(Zend_Json::decode($rawBody));
   break;
 case (strstr($contentType, 'application/xml')):
   $config = new Zend_Config_Xml($rawBody);
   $this->setBodyParams($config->toArray());
   break;
 default:
   if ($request->isPut()) {
    parse_str($rawBody, $params);
   $this->setBodyParams($params);
 }
 break;
 }
}

/**
* Set body params
*
* @param array $params
* @return Scrummer_Controller_Action
*/
public function setBodyParams(array $params)
{
  $this->_bodyParams = $params;
  return $this;
}

/**
* Retrieve body parameters
*
* @return array
*/
public function getBodyParams()
{
  return $this->_bodyParams;
}

/**
* Get body parameter
*
* @param string $name
* @return mixed
*/
public function getBodyParam($name)
{
  if ($this->hasBodyParam($name)) {
    return $this->_bodyParams[$name];
  }
  return null;
}

/**
* Is the given body parameter set?
*
* @param string $name
* @return bool
*/
public function hasBodyParam($name)
{
  if (isset($this->_bodyParams[$name])) {
    return true;
  }
  return false;
}

/**
 * Do we have any body parameters?
 *
 * @return bool
 */
 public function hasBodyParams()
 {
   if (!empty($this->_bodyParams)) {
     return true;
   }
   return false;
 }
 
 /**
 * Get submit parameters
 *
 * @return array
 */
 public function getSubmitParams()
 {
   if ($this->hasBodyParams()) {
     return $this->getBodyParams();
    }
   return $this->getRequest()->getPost();
 } 

 public function direct()
 {
   return $this->getSubmitParams();
 }
}

11 easy steps for installing Apache ActiveMQ for PHP

Take a peak at Web Developer Juice’s writeup on how to configure and use ActiveMQ (Message Queues), defenitely worth the read.

Apache ActiveMQ is one good option for implementing message queue in your PHP application. It can be easily installed on your server and it’s web accessible admin interface really makes administrator’s life easy. It can be easily connected with PHP via STOMP. I will suggesst to use MySql for Data persistance and start ActiveMQ as unix service.

Basic requirements: java, php, mysql.

via Web Developer Juice.

Introduction to Zend_Mail

A big thank you to Ryan Horn for putting together this great screencast about Zend_Mail. Ryan took the time to talk about how to setup a dev environment that uses a gmail account. This is perfect for folks like myself who work in a development environment that’s behind an annoying ISP firewall on SMTP (port 25). Ryan Horn is a web developer based out of Buffalo, feel free to reach him by email.

Grab a copy of the project or browse the repository.

via Free Zend Framework Screencasts – Zendcasts.

Keeping your HTML valid with Zend Framework, Tidy and Firebug

Ryan Mauger wrote a good article on tidying things behind the scenes, during development, definitely worth a look;

With Zend Framework there is an easy way to ensure that you always create valid HTML in your applications. This involves the use of a simple Front Controller Plugin, and the php Tidy component.

Valid HTML is important for a great many reasons, the most important of which is ensuring consistency across all of your visitors browsers. The first step to making sure that your site appears correctly on all the browsers is to ensure that your HTML is valid. Even if the goons at Microsoft continue to ignore the standards and do their own thing, if you at least ensure your html passes validation, then fixing things for Internet Explo(r|it)er of all its versions is a far easier task, and usually possible with a few simple extra styling rules in your CSS.

via Ryan’s Blog.

Zend_Form Translated Country & Currency Lists

A very common question is how do I get a localized / translated list of countries, currencies etc for a company registration form or similar.

Here is a easy to use sample; For your cut’n’paste pleasure 🙂


'HtmlTag'),
array('tag' => 'div', 'class' => 'element')),
'Label',
array(array('row' => 'HtmlTag'),
array('tag' => 'li')),
);

private $buttonDecorators = array(
'ViewHelper',
array(array('data' => 'HtmlTag'),
array('tag' => 'div', 'class' => 'button')),
array(array('row' => 'HtmlTag'),
array('tag' => 'li')),
);

public function init()
{
$this->setMethod('post');

$companyName = new Zend_Form_Element_Text('name', array(
'decorators' => $this->elementDecorators,
'label' => _('Company name'),
'description' => _('Enter the company name'),
'required' => true,
'filters' => array(
'StringTrim'
),
'validators' => array(
array('StringLength', false, array(6, 50))
),
'class' => 'input-text'
));

$accountNumber = new Zend_Form_Element_Text('accountno', array(
'decorators' => $this->elementDecorators,
'label' => _('Account number'),
'description' => _('Enter the ORG/VAT number.'),
'required' => true,
'filters' => array(
'StringTrim'
),
'validators' => array(
array('StringLength', false, array(12, 25))
),
'class' => 'input-text'
));
/**
* Generate a Country select box with the localized country
* names based upon the current application wide locale.
*/
$locale = Zend_Registry::getInstance()->get("Zend_Locale");

$countries = ($locale->getTranslationList('Territory',
$locale->getLanguage(),
2));

asort($countries, SORT_LOCALE_STRING);

$country = new Zend_Form_Element_Select('country', array(
'decorators' => $this->elementDecorators,
'label' => _('Country'),
'description' => _('Select the Country of Incorporation.'),
'required' => true,
'filters' => array(
'StringTrim'
),
'class' => 'input-select'
));

$country->addMultiOptions($countries)
->setValue($locale->getRegion());

/**
* Generate a Currency select box with the localizes currency
* names based upon the current application wide locale.
*/
$currencies= ($locale->getTranslationList('NameToCurrency',
$locale->getLanguage(),
2));

asort($currencies, SORT_LOCALE_STRING);

$currency = new Zend_Form_Element_Select('currency', array(
'decorators' => $this->elementDecorators,
'label' => _('Currency'),
'description' => _('Select the billing currency.'),
'required' => true,
'filters' => array(
'StringTrim'
),
'class' => 'input-select'
));

$currency->addMultiOptions($currencies)
->setValue('EUR');

$submit = new Zend_Form_Element_Submit('register', array(
'decorators' => $this->buttonDecorators,
'label' => _('Register'),
'class' => 'input-submit'
));
$this->addElements(Array($companyName,
$country,
$accountNumber,
$currency,
$submit));
}
}

For some more samples and references for these types of functions, check out;
http://framework.zend.com/manual/en/zend.locale.functions.html

Designing applications for cloud deployment

Gojko Adzic wrote a GREAT articles about a few very good points to take into consideration as you plan application or infrastructure deployment on to the Cloud;

During the last two years, I was involved in several projects deployed on the Amazon cloud. Being a relatively early adopter was a fantastic experience that provided lots of opportunities to burn my fingers and learn from mistakes. It also seriously challenged my view of scalable software architectures. I spoke about key lessons learned at CloudCamp London last week – here is the summary of that presentation.

Before I start, I’d like to point out that judging from this post it might seem that I have a negative view of cloud deployments, but nothing could be further from the truth. I have many nice things to say about the cloud, but lots of other presenters at CloudCamp do that all the time. I wanted to play the devil’s advocate a bit and expose some of the things that you won’t necessarily find in marketing materials.

via Gojko Adzic » Designing applications for cloud deployment.