• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/ZendX/JQuery/View/Helper/AjaxLink.php

00001 <?php
00026 include_once "Zend/View/Helper/HtmlElement.php";
00027 
00037 class ZendX_JQuery_View_Helper_AjaxLink extends Zend_View_Helper_HtmlElement
00038 {
00045     private static $currentLinkCallbackId = 1;
00046 
00086     public function ajaxLink($label, $url, $options=null, $params=null)
00087     {
00088         $jquery = $this->view->jQuery();
00089         $jquery->enable();
00090 
00091         $jqHandler = (ZendX_JQuery_View_Helper_JQuery::getNoConflictMode()==true)?'$j':'$';
00092 
00093         $attribs = array();
00094         if(isset($options['attribs']) && is_array($options['attribs'])) {
00095             $attribs = $options['attribs'];
00096         }
00097 
00098         //
00099         // The next following 4 conditions check for html attributes that the link might need
00100         //
00101         if(empty($options['noscript']) || $options['noscript'] == false) {
00102             $attribs['href'] = "#";
00103         } else {
00104             $attribs['href'] = $url;
00105         }
00106 
00107         if(!empty($options['title'])) {
00108             $attribs['title'] = $options['title'];
00109         }
00110 
00111         // class value is an array because the jQuery CSS selector
00112         // click event needs its own classname later on
00113         if(!isset($attribs['class'])) {
00114             $attribs['class'] = array();
00115         } elseif(is_string($attribs['class'])) {
00116             $attribs['class'] = explode(" ", $attribs['class']);
00117         }
00118         if(!empty($options['class'])) {
00119             $attribs['class'][] = $options['class'];
00120         }
00121 
00122         if(!empty($options['id'])) {
00123             $attribs['id'] = $options['id'];
00124         }
00125 
00126         //
00127         // Execute Javascript inline?
00128         //
00129         $inline = false;
00130         if(!empty($options['inline']) && $options['inline'] == true) {
00131             $inline = true;
00132         }
00133 
00134         //
00135         // Detect the callbacks:
00136         // Just those two callbacks, beforeSend and complete can be defined for the $.get and $.post options.
00137         // Pick all the defined callbacks and put them on their respective stacks.
00138         //
00139         $callbacks = array('beforeSend' => null, 'complete' => null);
00140         if(isset($options['beforeSend'])) {
00141             $callbacks['beforeSend'] = $options['beforeSend'];
00142         }
00143         if(isset($options['complete'])) {
00144             $callbacks['complete'] = $options['complete'];
00145         }
00146 
00147         $updateContainer = false;
00148         if(!empty($options['update']) && is_string($options['update'])) {
00149             $updateContainer = $options['update'];
00150 
00151             // Additionally check if there is a callback complete that is a shortcut to be executed
00152             // on the specified update container
00153             if(!empty($callbacks['complete'])) {
00154                 switch(strtolower($callbacks['complete'])) {
00155                     case 'show':
00156                         $callbacks['complete'] = sprintf("%s('%s').show();", $jqHandler, $updateContainer);
00157                         break;
00158                     case 'showslow':
00159                         $callbacks['complete'] = sprintf("%s('%s').show('slow');", $jqHandler, $updateContainer);
00160                         break;
00161                     case 'shownormal':
00162                         $callbacks['complete'] = sprintf("%s('%s').show('normal');", $jqHandler, $updateContainer);
00163                         break;
00164                     case 'showfast':
00165                         $callbacks['complete'] = sprintf("%s('%s').show('fast');", $jqHandler, $updateContainer);
00166                         break;
00167                     case 'fadein':
00168                         $callbacks['complete'] = sprintf("%s('%s').fadeIn('normal');", $jqHandler, $updateContainer);
00169                         break;
00170                     case 'fadeinslow':
00171                         $callbacks['complete'] = sprintf("%s('%s').fadeIn('slow');", $jqHandler, $updateContainer);
00172                         break;
00173                     case 'fadeinfast':
00174                         $callbacks['complete'] = sprintf("%s('%s').fadeIn('fast');", $jqHandler, $updateContainer);
00175                         break;
00176                     case 'slidedown':
00177                         $callbacks['complete'] = sprintf("%s('%s').slideDown('normal');", $jqHandler, $updateContainer);
00178                         break;
00179                     case 'slidedownslow':
00180                         $callbacks['complete'] = sprintf("%s('%s').slideDown('slow');", $jqHandler, $updateContainer);
00181                         break;
00182                     case 'slidedownfast':
00183                         $callbacks['complete'] = sprintf("%s('%s').slideDown('fast');", $jqHandler, $updateContainer);
00184                         break;
00185                 }
00186             }
00187         }
00188 
00189         if(empty($options['dataType'])) {
00190             $options['dataType'] = "html";
00191         }
00192 
00193         $requestHandler = $this->_determineRequestHandler($options, (count($params)>0)?true:false);
00194 
00195         $callbackCompleteJs = array();
00196         if($updateContainer != false) {
00197             if($options['dataType'] == "text") {
00198                 $callbackCompleteJs[] = sprintf("%s('%s').text(data);", $jqHandler, $updateContainer);
00199             } else {
00200                 $callbackCompleteJs[] = sprintf("%s('%s').html(data);", $jqHandler, $updateContainer);
00201             }
00202         }
00203         if($callbacks['complete'] != null) {
00204             $callbackCompleteJs[] = $callbacks['complete'];
00205         }
00206 
00207         if(isset($params) && count($params) > 0) {
00208             $params = ZendX_JQuery::encodeJson($params);
00209         } else {
00210             $params = '{}';
00211         }
00212 
00213         $js = array();
00214         if($callbacks['beforeSend'] != null) {
00215             switch(strtolower($callbacks['beforeSend'])) {
00216                 case 'fadeout':
00217                     $js[] = sprintf("%s(this).fadeOut();", $jqHandler);
00218                     break;
00219                 case 'fadeoutslow':
00220                     $js[] = sprintf("%s(this).fadeOut('slow');", $jqHandler);
00221                     break;
00222                 case 'fadeoutfast':
00223                     $js[] = sprintf("%s(this).fadeOut('fast');", $jqHandler);
00224                     break;
00225                 case 'hide':
00226                     $js[] = sprintf("%s(this).hide();", $jqHandler);
00227                     break;
00228                 case 'hideslow':
00229                     $js[] = sprintf("%s(this).hide('slow');", $jqHandler);
00230                     break;
00231                 case 'hidefast':
00232                     $js[] = sprintf("%s(this).hide('fast');", $jqHandler);
00233                     break;
00234                 case 'slideup':
00235                     $js[] = sprintf("%s(this).slideUp(1000);", $jqHandler);
00236                     break;
00237                 default:
00238                     $js[] = $callbacks['beforeSend'];
00239                     break;
00240             }
00241         }
00242 
00243         switch($requestHandler) {
00244             case 'GET':
00245                 $js[] = sprintf("%s.get('%s', %s, function(data, textStatus) { %s }, '%s');return false;",
00246                     $jqHandler, $url, $params, implode(" ", $callbackCompleteJs), $options['dataType']);
00247                 break;
00248             case 'POST':
00249                 $js[] = sprintf("%s.post('%s', %s, function(data, textStatus) { %s }, '%s');return false;",
00250                     $jqHandler, $url, $params, implode(" ", $callbackCompleteJs), $options['dataType']);
00251                 break;
00252         }
00253 
00254         $js = implode($js);
00255 
00256         if($inline == true) {
00257             $attribs['onclick'] = $js;
00258         } else {
00259             if(!isset($attribs['id'])) {
00260                 $clickClass = sprintf("ajaxLink%d", ZendX_JQuery_View_Helper_AjaxLink::$currentLinkCallbackId);
00261                 ZendX_JQuery_View_Helper_AjaxLink::$currentLinkCallbackId++;
00262 
00263                 $attribs['class'][] = $clickClass;
00264                 $onLoad = sprintf("%s('a.%s').click(function() { %s });", $jqHandler, $clickClass, $js);
00265             } else {
00266                 $onLoad = sprintf("%s('a#%s').click(function() { %s });", $jqHandler, $attribs['id'], $js);
00267             }
00268 
00269             $jquery->addOnLoad($onLoad);
00270         }
00271 
00272         if(count($attribs['class']) > 0) {
00273             $attribs['class'] = implode(" ", $attribs['class']);
00274         } else {
00275             unset($attribs['class']);
00276         }
00277 
00278         $html = '<a'
00279             . $this->_htmlAttribs($attribs)
00280             . '>'
00281             . $label
00282             . '</a>';
00283         return $html;
00284     }
00285 
00297     protected function _determineRequestHandler($options, $hasParams)
00298     {
00299         if(isset($options['method']) && in_array(strtoupper($options['method']), array('GET', 'POST'))) {
00300             return strtoupper($options['method']);
00301         }
00302         $requestHandler = "GET";
00303         if($hasParams == true) {
00304             $requestHandler = "POST";
00305         }
00306         return $requestHandler;
00307     }
00308 }

Generated on Thu Apr 19 2012 17:01:19 for openbiz by  doxygen 1.7.2