[ Index ]

PHP Cross Reference of JPSpan 0.4 (beta)

title

Body

[close]

/examples/ -> sfsearch_server.php (source)

   1  <?php
   2  // $Id: sfsearch_server.php,v 1.4 2004/11/23 14:10:56 harryf Exp $
   3  /**
   4  * This is a remote script to call from Javascript
   5  */
   6  
   7  require_once  '../JPSpan.php';
   8  require_once JPSPAN . 'Serializer.php';
   9  require_once JPSPAN . 'Server/PostOffice.php';
  10  //-----------------------------------------------------------------------------------
  11  // Define a PHP class...
  12  class ProjectList {
  13      var $projects;
  14      function ProjectList($projects = array()) {
  15          $this->projects = $projects;
  16      }
  17  }
  18  //-----------------------------------------------------------------------------------
  19  // Define a class which generates Javascript when instances of ProjectList are
  20  // encountered...
  21  class ProjectListGenerator extends JPSpan_SerializedElement {
  22  
  23      function generate(& $code) {
  24          // Notice this generates client-side behaviour
  25          $func = "htmlElement.innerHTML = '';";
  26          $func .="var projects = new Array();";
  27          foreach ( $this->value->projects as $key => $project ) {
  28              $func .="projects[$key] = '$project';";
  29          }
  30          $func .="for (var i=0; i < projects.length; i++ ) {";
  31          $func .="htmlElement.innerHTML += '<a href=http://sf.net/projects/'+projects[i]+'>'+projects[i]+'</a><br>';";
  32          $func .="}";
  33          $code->append('var '.$this->tmpName.' = new Function("htmlElement","'.addslashes($func).'");');
  34      }
  35      
  36  }
  37  
  38  //-----------------------------------------------------------------------------------
  39  // Register the class and it's serializer with the JPSpan_Serializer
  40  JPSpan_Serializer::addType('ProjectList','ProjectListGenerator');
  41  
  42  //-----------------------------------------------------------------------------------
  43  // A class to publish for remote calls...
  44  class SfSearch {
  45  
  46      function getProjects($fragment='') {
  47          if ( !preg_match('/^[a-z].*$/',$fragment) ) {
  48              $fragment = '';
  49          }
  50          $base = '/home/groups/';
  51  
  52          $fraglen = strlen($fragment);
  53          
  54          if ( $fraglen >= 2 ) {
  55              $groupDir = $base . substr($fragment,0,1).'/'.substr($fragment,0,2).'/';
  56          } else if ( $fraglen == 1 ) {
  57              $groupDir = $base . $fragment . '/'. $fragment . 'a/';
  58          } else {
  59              $groupDir = $base . 'a/aa/';
  60          }
  61  
  62          $dh = opendir($groupDir);
  63          
  64          $projects = array();
  65          
  66          while (($node = readdir($dh)) !== false) {
  67              if ( $node == '.' || $node == '..' ) {
  68                  continue;
  69              }
  70              if ( is_dir($groupDir.$node) ) {
  71                  $projects[] = $node;
  72              }
  73          }
  74          
  75          for ( $i = $fraglen; $i > 0; $i-- ) {
  76              $matches = preg_grep('/^'.substr($fragment,0,$i).'/i',$projects);
  77              if ( count($matches) > 0 ) {
  78                  return new ProjectList(array_slice($matches,0,10));
  79              }
  80          }
  81  
  82          return new ProjectList();
  83      }
  84  
  85  }
  86  
  87  $S = & new JPSpan_Server_PostOffice();
  88  $S->encoding = 'php'; // Only needs ASCII
  89  $S->addHandler(new SfSearch());
  90  
  91  //-----------------------------------------------------------------------------------
  92  if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'], 'client')==0) {
  93  
  94      // Compress the Javascript
  95      define('JPSPAN_INCLUDE_COMPRESS',TRUE);
  96      $S->displayClient();
  97  
  98  } else {
  99  
 100      // Include error handler - PHP errors, warnings and notices serialized to JS
 101      require_once JPSPAN . 'ErrorHandler.php';
 102      $S->serve();
 103  
 104  }
 105  ?>


Generated: Fri Nov 26 11:42:46 2004 Cross-referenced by PHPXref 0.6