![]() |
|
Snippets |
|
For ergonomic and graphism reasons, I sometimes prefer to use link rather than buttons and/or button rather than links...
To do so, I created this small function :
<?php # File : JsHelper.php // use of "normal" Javascript Helper use_helper('Javascript'); /*! * Create a link that submit the closest parent form * * @param $string string : text to display * @param $options mixed : options to be pass to the tag * @return string : HTML code for the link */ function link_to_submit( $string, $options = null ) { $func = ";var a=this.ancestors();for(var i in a){if(a[i].tagName=='FORM'){try{a[i].onsubmit();} catch(err){a[i].submit();}break;}}"; return link_to_function( $string, $func, $options ); } /*! * Create a button that act as a link * * @param $string string : text to display in the button * @param $url string : URL of the link * @param $options mixed : options to be pass to the tag * @return string : HTML code for the link */ function button_to_link( $string, $url, $options = null ) { $url = url_for($url); $func = ";document.location = '$url';return false;"; if ( is_string($options) ) $options.= " onclick=$func"; else $options['onclick'] = $func; return tag('button', $options ).htmlspecialchars($string).'</button>'; }
Then, you simply use it as a normal link_to function :
<? use_helper('Js') ?> <?=form_tag('module/action')?> <?=input_tag('text')?> <?=link_to_submit('Validate')?> <?=link_to('Cancel','module/index')?> </form>
Or :
<? use_helper('Js') ?> <?=form_tag('module/action')?> <?=input_tag('text')?> <?=submit_tag('Validate')?> <?=button_to_link('Cancel','module/index')?> </form>
In these two examples, links/buttons of the form are graphicly consistents. For a better user experience ;)
PS : works as well with AJAX forms form_remote_tag()
PPS : Don't forget that thanks to CSS, you can make a button looking like a link (et vice-versa).
PPPS : Don't forget that buttons and links doesn't have the same meaning for search engine bots.
PPPPS : Don't forget that this rely on the fact that user is using javascript!!!
Here is how I did this... Create a sfGuardAuth module in your application and edit the actions.class.php file as follow.
The trick is to not try to overwrite the sfGuardAuth/signin action, as it use validation. As well it allow you to use the "normal" signin way (form and etc).
require_once(sfConfig::get('sf_plugins_dir').'/sfGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php'); class sfGuardAuthActions extends BasesfGuardAuthActions { public function executeHTTPSignin() { // get somme interesting stuff! $request = $this->getRequest(); $response = $this->getResponse(); $user = $this->getUser(); // An HTTP authenticated user cannot logout (browser always send authentification datas) // So we must be sure that the user has seen the HTTP authentification box before if ( $user->getAttribute('request_authentification') ) { // If authentification datas has been sent if ( isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) { // If correct username given $guarduser = sfGuardUserPeer::retrieveByUserName( $_SERVER['PHP_AUTH_USER'] ); if ( $guarduser instanceof sfGuardUser ) { // If correct Password given if ( ($guarduser instanceof sfGuardUser) and ($guarduser->checkpassword( $_SERVER['PHP_AUTH_PW'] )) ) { // we can signin the user and redirect it $user->signin( $guarduser ); $user->setAttribute('request_authentification',false); $this->redirect( sfConfig::get('app_sf_guard_plugin_success_signin_url','@homepage') ); throw new sfStopException; } } } } // else, popup the authentification box $user->setAttribute('request_authentification',true); $response->setHttpHeader( 'WWW-Authenticate', 'Basic realm="Identification"' ); $response->setHttpHeader( 'HTTP/1.0', '401 Unauthorized' ); // This will be displayed if the user cancel the authentification process $this->forward( 'sfGuardAuth', 'password' ); throw new sfStopException; } public function executePasswowd() { # Implement this action as usual... } }
Enjoy... (I hope)