• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/handlers/ajaxhandler.php

00001 <?php
00007 namespace Habari;
00008 
00013 class AjaxHandler extends ActionHandler
00014 {
00015 
00025   public function act_ajax()
00026   {
00033     Plugins::act( 'ajax_' . $this->handler_vars['context'], $this );
00034 
00035     // if this is the locale context, serve the locale javascript
00036     if ( $this->handler_vars['context'] == 'locale' ) {
00037       $this->locale_js();
00038     }
00039   }
00040 
00047   public function act_auth_ajax()
00048   {
00049     $user = User::identify();
00050     if ( $user->loggedin ) {
00051       Plugins::act( 'auth_ajax_verify',  $this );
00058       Plugins::act( 'auth_ajax_' . $this->handler_vars['context'], $this );
00059       exit;
00060     }
00061   }
00062 
00066   public function locale_js() {
00067     header('Expires: ' . gmdate('D, d M Y H:i:s ', time() + 432000) . 'GMT');
00068     header('content-type: text/javascript');
00069 
00070     $domain = Locale::get_messages();
00071     $domain_json = json_encode($domain);
00072 
00073     $js = <<<TEEHEE
00074 function _t() {
00075     var domain = {$domain_json};
00076     var s = arguments[0];
00077 
00078     if(domain[s] != undefined) {
00079         s = domain[s][1][0];
00080     }
00081 
00082     for(var i = 1; i <= arguments.length; i++) {
00083         r = new RegExp('%' + (i) + '\\\\\$s', 'g');
00084         if(!s.match(r)) {
00085             r = new RegExp('%s');
00086         }
00087         s = s.replace(r, arguments[i]);
00088     }
00089     return s;
00090 }
00091 TEEHEE;
00092     echo Plugins::filter('locale_js', $js);
00093   }
00094 
00100   public static function register_ajax($name, $fn)
00101   {
00102     Plugins::register($fn, 'action', 'ajax_' . $name);
00103   }
00104 
00110   public static function register_auth_ajax($name, $fn)
00111   {
00112     Plugins::register($fn, 'action', 'auth_ajax_' . $name);
00113   }
00114 
00115 
00120   public static function __static()
00121   {
00122     /*
00123      * These registration functions are here rather than the classes that directly provide
00124      * their data (like Tags for tag_list or Posts for post_list) because putting them in
00125      * their respective classes would require that they be autoloaded on every page load
00126      * so that these functions could register their ajax endpoints.
00127      */
00128     // Registers an auth_ajax endpoint for tag autocompletion
00129     self::register_auth_ajax('tag_list', function(){
00130       $tags = Tags::search($_GET['q']);
00131       $results = array();
00132       foreach($tags as $tag) {
00133         $results[] = array('id' => $tag->term_display, 'text' => $tag->term_display);
00134       }
00135       $ar = new AjaxResponse();
00136       $ar->data = array(
00137         'results' => $results,
00138         'more' => false,
00139         'context' => array()
00140       );
00141       $ar->out();
00142     });
00143 
00144     // Registers an auth_ajax endpoint for post autocompletion
00145     self::register_auth_ajax('post_list', function(){
00146       $posts = Posts::get(array('title_search' => $_GET['q']));
00147       $results = array();
00148       foreach($posts as $post) {
00149         $results[] = array('id' => $post->id, 'text' => $post->title);
00150       }
00151       $ar = new AjaxResponse();
00152       $ar->data = array(
00153         'results' => $results,
00154         'more' => false,
00155         'context' => array()
00156       );
00157       $ar->out();
00158     });
00159 
00160   }
00161 
00162 }
00163 
00164 ?>

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1