Skip to content

Instantly share code, notes, and snippets.

@mmiliaus
Created August 15, 2012 14:48
Show Gist options
  • Save mmiliaus/3360746 to your computer and use it in GitHub Desktop.
Save mmiliaus/3360746 to your computer and use it in GitHub Desktop.
<?php
/**
*
* Generates anchor HTML markup, which can perform GET/POST requests
*
* Example usage in the view
* <code>
* $this->linkTo(
'new',
'http://action/url',
array('confirm'=>'Are you sure?', 'method'=>'post'),
array(
'class'=>'unshortlist_application_lnk',
'title'=>'Unshortlist this candidate'
));
* </code>
*
* @author Martynas Miliauskas <[email protected]>
* @copyright 2008-2012 GulfJobsMarket Ltd.
*/
class Zend_View_Helper_LinkTo extends Zend_View_Helper_Abstract{
/*
* Generates anchor HTML markup, which can perform GET/POST requests
*
* @param string $label text inside of the anchor
* @param string $url anchor href
* @param array $options all sorts of magic: confirm messsage, request method
* @param array $html additional anchor attributes
* @return string
*/
public function linkTo($label, $url, $options=array(), $html=array())
{
$on_click = array();
$html_attrs = $html;
if ( isset($options['method']) && $options['method'] != 'post' ) {
$html_attrs['onclick'] = 'if (!confirm("'.$options['confirm'].'"){ return false; }';
$html_attrs_str = '';
foreach ($html_attrs as $attr=>$val) {
$html_attrs_str.=' '.$attr.'="'.$val.'"';
}
return '<a href="'.$url.'" '.$html_attrs_str.'>'.$label.'</a>';
}
$on_click[] = "var f = document.createElement('form');";
$on_click[] = "f.style.display = 'none';";
$on_click[] = "this.parentNode.appendChild(f);";
$on_click[] = "f.method = 'POST';";
$on_click[] = "f.action = this.href;";
$on_click[] = "f.submit();";
$on_click[] = "";
if (isset( $options['confirm']) ) {
array_unshift( $on_click, "if ( confirm('".$options['confirm']."') ) {");
array_push( $on_click, '}; return false');
}
$html_attrs['onclick'] = implode(' ', $on_click);
$html_attrs_str = '';
foreach ($html_attrs as $attr=>$val) {
$html_attrs_str.=' '.$attr.'="'.$val.'"';
}
return '<a href="'.$url.'" '.$html_attrs_str.'>'.$label.'</a>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment