Thursday, December 27, 2012

Zend_File_Transfer_Adapter_Abstract bug when file not uploaded

I'm not sure where goes wrong, but some how if a file element does not contain file,
the check returns empty.
there is a bug open since may, but yet no patch for it.

file effected is
Zend_File_Transfer_Adapter_Abstract
line #617: public function isValid($files = null)

http://framework.zend.com/issues/browse/ZF-12189


Tuesday, December 25, 2012

Zend framework 2 FormRow Render element with ID differently

Not sure what goes up with their development team that there render element with id attribute differently, compared to those without it.
Took me sometime to figure this, sigh*!

And guess what, i cant file a zend framework 2 bug.. lol
Only Zend Framework 1 jira tracker is available, not sure if ive visited the wrong link

Zend AbstractTableGateway does not support table alias

Couldnt figure out how to do it,
finally, i created my own Sql and and Select on my own.

But ive to overwrite executeSelect to allow table select with alias
$resultSet = $this->selectWith($oSelect);

Zend framework 2 route example


router' => array(
    'routes' => array(
      'bannermgt' => array(
        'type' => 'segment',
        'options' => array(
          'route' => '/bannermgt[/:action][/:id][/page/:page][/searchname/:searchname][/searchpath:searchpath][/]',
          'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id' => '[0-9]+',
            'page' => '[0-9]+',
            'searchname' => '[^/]*',
            'searchpath' => '[^/]*',
          ),
          'defaults' => array(
            'controller' => 'BannerMgt',
            'action' => 'index',
            "page" => 1,
            "searchname" => "",
            "searchpath" => ""
          ),
        ),
      ),
    ),
  ),

Take note that ordering of URL in route is strict.
Therefore searchname must come first before searchpath.

Or else , you may ommit:
[/searchname/:searchname][/searchpath:searchpath]
And append as querystring
/bannermgt/list?searchpath=xxx&searchname=yyy

Zend Framework 2 redirect and forward

Both uses different path name to identify.
Redirect uses physical url on browser side, therefore the route name is to be used.

For forward wise, its using the factory service name to identify the controller to run.
example:
config:

'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\Login' => 'Application\Controller\LoginController'
        ),
    ),


'router' => array(
        'routes' => array(
            "login" => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/login[/:action][/]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Application\Controller\Login',
                        'action' => 'index',
                    ),
                ),
            ),



$this->forward()->dispatch("Application\Controller\Index", array("action" => "someaction");

for redirect:
$this->redirect()->toRoute("login", array("action" => "someaction");

to get current controller name:
$this->getEvent()->getRouteMatch()->getParam("controller");

to get current route name:
return $this->getEvent()->getRouteMatch()->getMatchedRouteName();


Friday, December 21, 2012

Zend framework 2 get other view helper instance

in view:

$oFormat = $this->plugin("dateFormat");
$oFormat->setLocale("en_GB");
echo $oFormat->__invoke(...);

to invoke directly:
echo $this->dateFormat(...);

Zend framework 2 Locale / i18n

In Application/config/module.config.php:

return array(
//...,
'translator' => array(
        'locale' => 'en_GB',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
//...


In Application/Module.php bootstrap:

//set your time zone here

date_default_timezone_set("Asia/Kuala_Lumpur");
$oViewHelperManager = $e->getApplication()->getServiceManager()->get("ViewHelperManager");
$oViewHelperManager->get("dateFormat")>setLocale($sLocale)->setTimezone(date_default_timezone_get());
$oViewHelperManager->get("numberFormat")->setLocale($sLocale);
$oViewHelperManager->get("currencyFormat")->setLocale($sLocale)->setCurrencyCode("MYR");

To output in view template:
echo $this->translate("My text here");
echo $this->dateFormat(new DateTime(), IntlDateFormatter::SHORT);
echo $this->numberFormat(12345.0123, NumberFormatter::DECIMAL);
//default currency
echo $this->numberFormat(12345.5226, NumberFormatter::CURRENCY);

//custom currency output:
echo $this->currencyFormat(1234.5226, "MYR");


Requires:
  • php5-intl

Thursday, December 20, 2012

Active directory login from Zend

After spending hours solving this issue,
and ive found that each different company have different active directory path setting.

For login purpose, the main concern are the baseDn.
Some uses this:
CN=Users,DC=EXAMPLE,DC=COM

Some uses this:
OU=Users,DC=EXAMPLE,DC=COM

And event this:
OU=Users,OU=.User Accounts,DC=EXAMPLE,DC=COM

Example of Zend 1 config in yaml:

ldap:
    server1:
      host: EXAMPLE.LOCAL
      accountDomainName: EXAMPLE.LOCAL
      accountDomainNameShort: EXAMPLE
      baseDn: OU=Users,OU=.User Accounts,DC=EXAMPLE,DC=LOCAL
      bindRequiresDn: false


Wednesday, December 19, 2012

Zend Framework 2 controller init

Example:

class MyController extends Controller {
protected $bInit = false;


public function onDispatch(\Zend\Mvc\MvcEvent $e) {
    if (! $this-> bInit) {
      $this->init();
      $this-> bInit = true;
    }
    return parent::onDispatch($e);
  }

Zend Framework 2 file upload

Example:
Note: Remember to set destination and filter to adaptor before running isValid or Receive,
Because setting filter or destination after running isvalid will cause the isvalid to add 2 times check on the file, one as tmp_name and another as destination name..
which will cause Count validator to fail!


    $sDestination .= getcwd() . "/public/assets";
 
    if (!is_dir($sDestination)) {
      if (!@mkdir($sDestination, 0777, true)) {
        throw new \Exception("Unable to create destination: " . $sDestination);
      }
    }
    $size = new Size(array('min' => 10, "max" => 10000000)); //minimum bytes filesize
    $count = new Count(array("min" => 0, "max" => 1));
    $extension = new Extension(array("extension" => array("jpg", "png", "swf")));

    $oFile = $this->params()->fromFiles('txtFile1');
 
    $adapter = new \Zend\File\Transfer\Adapter\Http();
    $adapter->setValidators(array($size, $count, $extension), $oFile['name']);
    $adapter->setDestination($sDestination);
 
    if (! $adapter->isValid()) {
      $dataError = $adapter->getMessages();
      $error = array();
      foreach ($dataError as $key => $row) {
        $error[] = $row;
      }
      $form->setMessages(array('txtFile1' => $error));
    } else {
   
      if ($adapter->receive($oFile['name'])) {
        $form->get("txtFile1")->setValue($adapter->getFileName());
      } else {
        $form->setMessages(array('txtFile1' => "Receive failed"));
      }
    }

Tuesday, December 18, 2012

Zend framework 2 and firebug and composer

To install firebug core lib via composer onto zend framework 2,

vendor/composer.json

#add:
 "firephp/firephp-core": "dev-master"

#example:

"require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "firephp/firephp-core": "dev-master"
    }

#run in console:
php composer.phar install

#for update on existing library
php composer.phar update

Kloxo dns template error

Ive came accross some long error on kloxo dns template,
and it turn out to be corrupted dns template.
I only realize this on creation of a domain or addition of a client.

running /script/fixdns seems to fix this issue,

if you have created a domain or client, just delete the client,
and recreate again.

yes, its still annoying that kloxo always have update issue which corrupt some of the files,
most obvious was roundcube update

Monday, December 17, 2012

Zend 2 Service Locator

Zend 2 Service Locator seems to be replacing Zend 1 Zend_Registry

Here are some reference:
http://framework.zend.com/manual/2.0/en/modules/zend.mvc.services.html