Development

Changeset 6525

You must first sign up to be able to contribute.

Changeset 6525

Show
Ignore:
Timestamp:
12/16/07 21:28:44 (1 year ago)
Author:
Carl.Vondrick
Message:

upgraded all forms to sfForm system + unit tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfLucenePlugin/trunk/CHANGELOG

    r6489 r6525  
    1212  * Refactored category support 
    1313  * Minor refactoring of sfLucene class to move to sfParameterHolder 
     14  * Upgraded forms to sfForm 
    1415 
    1516Version 0.1.1 Beta 
  • plugins/sfLucenePlugin/trunk/lib/category/sfLuceneCategory.class.php

    r6392 r6525  
    3232    $this->name = $name; 
    3333    $this->count = $count; 
     34  } 
     35 
     36  public function __toString() 
     37  { 
     38    return $this->name; 
    3439  } 
    3540 
  • plugins/sfLucenePlugin/trunk/lib/helper/sfLuceneHelper.php

    r6247 r6525  
    2020} 
    2121 
    22 function include_search_controls($query = null
     22function include_search_controls($form
    2323{ 
    24   include_component(sfContext::getInstance()->getModuleName(), 'controls', array('query' => $query)); 
     24  include_partial(sfContext::getInstance()->getModuleName() . '/controls', array('form' => $form)); 
    2525} 
    2626 
     
    2828{ 
    2929  include_component('sfLucene', 'pagerNavigation', array('pager' => $pager, 'radius' => $radius)); 
    30 } 
    31  
    32 function include_search_categories($multiple = false) 
    33 { 
    34   include_component('sfLucene', 'categories', array('multiple' => $multiple)); 
    35 } 
    36  
    37 function has_search_categories() 
    38 { 
    39   return (sfConfig::get('app_lucene_categories', true) ? true : false) && (count(sfLuceneToolkit::getApplicationInstance()->getCategories()) > 0); 
    4030} 
    4131 
  • plugins/sfLucenePlugin/trunk/modules/sfLucene/lib/BasesfLuceneActions.class.php

    r6247 r6525  
    2121   * For compatiability with default routing rules. 
    2222   */ 
    23   public function executeIndex(
     23  public function executeIndex($request
    2424  { 
    2525    $this->forward($this->getModuleName(), 'search'); 
     
    3131  * the search box is displayed to prompt the user to enter controls. 
    3232  */ 
    33   public function executeSearch(
     33  public function executeSearch($request
    3434  { 
    3535    // determine if the user pressed the "Advanced"  button 
    36     if ($this->getRequestParameter('commit') == $this->translate('Advanced')) 
     36    if ($request->getParameter('commit') == $this->translate('Advanced')) 
    3737    { 
    3838      // user did, so redirect to advanced search 
     
    4040    } 
    4141 
    42     $this->advanced_enabled = sfConfig::get('sf_lucene_interface_advanced', true); 
    43     $this->categories_enabled = sfConfig::get('sf_lucene_interface_categories', true); 
    44  
    45     $query = $this->getRequestParameter('query'); 
    46  
    47     // did user enter a query? 
    48     if ($query) 
    49     { 
    50       $category = $this->getRequestParameter('category', null); 
     42    $form = new sfLuceneSimpleForm(); 
     43    $this->configureCategories($form); 
     44    $form->bind($request->getParameter('form')); 
     45 
     46    // do we have a query? 
     47    if ($form->isValid()) 
     48    { 
     49      $values = $form->getValues(); 
    5150 
    5251      // get results 
    53       $pager = $this->getResults($query, $category); 
     52      $pager = $this->getResults($form); 
    5453 
    5554      $num = $pager->getNbResults(); 
     
    5958      { 
    6059        // display results 
    61         $this->configurePager($pager); 
     60        $pager = $this->configurePager($pager, $request); 
    6261 
    6362        $this->num = $num; 
    6463        $this->pager = $pager; 
    65         $this->query = $query; 
     64        $this->query = $values['query']; 
     65        $this->form = $form; 
    6666 
    6767        $this->setTitleNumResults($pager); 
     
    7272      { 
    7373        // display error 
     74        $this->form = $form; 
    7475        $this->setTitleI18n('No Results Found'); 
    7576 
     
    8081    { 
    8182      // display search controls 
     83      $this->form = $form; 
    8284      $this->setTitleI18n('Search'); 
    8385 
     
    9092  * user use a form to control some of the advanced query syntaxes. 
    9193  */ 
    92   public function executeAdvancedSearch(
     94  public function executeAdvancedSearch($request
    9395  { 
    9496    // disable this action if advanced searching is disabled. 
     
    9698 
    9799    // determine if the "Basic" button was clicked 
    98     if ($this->getRequestParameter('commit') == $this->translate('Basic')) 
     100    if ($request->getParameter('commit') == $this->translate('Basic')) 
    99101    { 
    100102      $this->redirect($this->getModuleName() . '/search'); 
    101103    } 
    102104 
    103     // did the user submit the form? 
    104     if ($this->getRequestParameter('mode') == 'search') 
    105     { 
    106       // base quey 
    107       $query = $this->getRequestParameter('keywords'); 
    108  
    109       // build the must have part 
    110       $musthave = preg_split('/ +/', $this->getRequestParameter('musthave'), -1, PREG_SPLIT_NO_EMPTY); 
    111  
    112       if (count($musthave)) 
    113       { 
    114         $query .= ' +' . implode($musthave, ' +'); 
    115       } 
    116  
    117       // build the must not have 
    118       $mustnothave = preg_split('/ +/', $this->getRequestParameter('mustnothave'), -1, PREG_SPLIT_NO_EMPTY); 
    119  
    120       if (count($mustnothave)) 
    121       { 
    122         $query .= ' -' . implode($mustnothave, ' -'); 
    123       } 
    124  
    125       // build the has pharse part 
    126       if ($this->getRequestParameter('hasphrase') != '') 
    127       { 
    128         $query .= ' "' . str_replace('"', '', $this->getRequestParameter('hasphrase')) . '"'; 
    129       } 
    130  
    131       $query = trim($query); 
    132  
    133       // is there a query? 
    134       if ($query) 
    135       { 
    136         // yes, so search 
    137  
    138         $this->getRequest()->setParameter('query', $query); 
    139  
    140         $this->forward($this->getModuleName(), 'search'); 
    141       } 
    142     } 
     105    $form = new sfLuceneAdvancedForm(); 
     106    $this->configureCategories($form); 
     107 
     108    // continue only if there was a submit 
     109    if ($request->getParameter('commit')) 
     110    { 
     111      $form->bind($request->getParameter('form')); 
     112 
     113        // is the form valid? 
     114      if ($form->isValid()) 
     115      { 
     116        $values = $form->getValues(); 
     117 
     118        // base quey 
     119        $query = $values['keywords']; 
     120 
     121        // build the must have part 
     122        $musthave = preg_split('/ +/', $values['musthave'], -1, PREG_SPLIT_NO_EMPTY); 
     123 
     124        if (count($musthave)) 
     125        { 
     126          $query .= ' +' . implode($musthave, ' +'); 
     127        } 
     128 
     129        // build the must not have 
     130        $mustnothave = preg_split('/ +/', $values['mustnothave'], -1, PREG_SPLIT_NO_EMPTY); 
     131 
     132        if (count($mustnothave)) 
     133        { 
     134          $query .= ' -' . implode($mustnothave, ' -'); 
     135        } 
     136 
     137        // build the has pharse part 
     138        if ($values['hasphrase'] != '') 
     139        { 
     140          $query .= ' "' . str_replace('"', '', $values['hasphrase']) . '"'; 
     141        } 
     142 
     143        $query = trim($query); 
     144 
     145        // is there a query? 
     146        if ($query) 
     147        { 
     148          // yes, so search 
     149 
     150          $requestParam = array('query' => $query); 
     151 
     152          if (isset($values['category'])) 
     153          { 
     154            $requestParam['category'] = $values['category']; 
     155          } 
     156 
     157          $request->setParameter('form', $requestParam); 
     158 
     159          $this->forward($this->getModuleName(), 'search'); 
     160        } 
     161      } 
     162    } 
     163 
     164    $this->form = $form; 
    143165 
    144166    $this->setTitleI18n('Advanced Search'); 
     
    150172  * Wrapper function for getting the results. 
    151173  */ 
    152   protected function getResults($querystring, $category = null) 
    153   { 
     174  protected function getResults($form) 
     175  { 
     176    $data = $form->getValues(); 
     177 
    154178    $query = new sfLuceneCriteria(); 
    155     $query->addSane($querystring); 
    156  
    157     if (sfConfig::get('app_lucene_categories', true) && $category
    158     { 
    159       $query->add('sfl_category: ' . $category); 
     179    $query->addSane($data['query']); 
     180 
     181    if (sfConfig::get('app_lucene_categories', true) && isset($data['category']) && $data['category'] != $this->translate('All')
     182    { 
     183      $query->add('sfl_category: ' . $data['category']); 
    160184    } 
    161185 
     
    172196 
    173197  /** 
     198   * Configures the form for categories 
     199   */ 
     200  protected function configureCategories($form) 
     201  { 
     202    $categories = $this->getLuceneInstance()->getCategories()->getAllCategories(); 
     203 
     204    if (!sfConfig::get('app_lucene_categories', true) || count($categories) == 0) 
     205    { 
     206      return; 
     207    } 
     208 
     209    $categories = array_merge(array($this->translate('All')), $categories); 
     210 
     211    $categories = array_combine($categories, $categories); 
     212 
     213    $form->setCategories($categories); 
     214 
     215    return $form; 
     216  } 
     217 
     218  /** 
    174219  * Configures the pager according to the request parameters. 
    175220  */ 
    176   protected function configurePager($pager
    177   { 
    178     $page = (int) ($this->getRequestParameter('page', 1)); 
     221  protected function configurePager($pager, $request
     222  { 
     223    $page = (int) ($request->getParameter('page', 1)); 
    179224 
    180225    $pager->setMaxPerPage(sfConfig::get('app_lucene_per_page', 10)); 
  • plugins/sfLucenePlugin/trunk/modules/sfLucene/lib/BasesfLuceneComponents.class.php

    r6489 r6525  
    1616abstract class BasesfLuceneComponents extends sfComponents 
    1717{ 
    18   public function executeControls() 
    19   { 
    20     $this->query = $this->getRequestParameter('query'); 
    21   } 
    22  
    2318  public function executePublicControls() 
    2419  { 
  • plugins/sfLucenePlugin/trunk/modules/sfLucene/templates/_controls.php

    r6247 r6525  
    88?> 
    99 
    10 <?php use_helper('sfLucene') ?> 
    11  
    12 <?php echo form_tag('sfLucene/search', 'method=get class=search-controls') ?> 
     10<form action="<?php echo url_for('sfLucene/search') ?>" method="get" class="search-controls"> 
    1311 
    1412  <label for="query"><?php echo __('What are you looking for?') ?></label> 
    15   <?php echo input_tag('query', $query, 'accesskey=q') ?> 
    16   <?php if (has_search_categories()): ?> 
    17     <?php include_search_categories() ?> 
     13  <?php echo $form['query'] ?> 
     14 
     15  <?php if ($form->hasCategories()): ?> 
     16    <?php echo $form['category'] ?> 
    1817  <?php endif ?> 
    19   <?php echo submit_tag(__('Search'), 'accesskey=s') ?> 
     18 
     19  <input type="submit" name="commit" accesskey="s" value="<?php echo __('Search') ?>" /> 
     20 
    2021  <?php if (sfConfig::get('app_lucene_advanced', true)): ?> 
    21     <?php echo submit_tag(__('Advanced'), 'accesskey=a') ?
     22    <input type="submit" name="commit" accesskey="a" value="<?php echo __('Advanced') ?>" /
    2223  <?php endif ?> 
    2324 
  • plugins/sfLucenePlugin/trunk/modules/sfLucene/templates/advancedSearchControls.php

    r6247 r6525  
    88?> 
    99 
    10 <?php use_helper('I18N', 'sfLucene') ?> 
     10<?php use_helper('I18N') ?> 
    1111 
    1212<h2><?php echo __('Advanced Search') ?></h2> 
    1313 
    14 <?php echo form_tag('sfLucene/advancedSearch', 'method=get') ?
     14<form action="<?php echo url_for('sfLucene/advancedSearch') ?>" method="get"
    1515  <fieldset> 
    1616    <legend><?php echo __('Search Terms') ?></legend> 
    1717 
    1818    <table> 
    19       <tbody> 
    20         <tr> 
    21           <td><label for="sfl_keywords"><?php echo __('May contain keywords') ?></label></td> 
    22           <td><?php echo input_tag('keywords', '', 'id=sfl_keywords') ?></td> 
    23         </tr> 
    24         <tr> 
    25           <td><label for="sfl_musthave"><?php echo __('Must contain keywords') ?></label></td> 
    26           <td><?php echo input_tag('musthave', '', 'id=sfl_musthave') ?></td> 
    27         </tr> 
    28         <tr> 
    29           <td><label for="sfl_mustnothave"><?php echo __('Must exclude keywords') ?></label></td> 
    30           <td><?php echo input_tag('mustnothave', '', 'id=sfl_mustnothave') ?></td> 
    31         </tr> 
    32         <tr> 
    33           <td><label for="sfl_hasphrase"><?php echo __('Contains exact phrase') ?></label></td> 
    34           <td><?php echo input_tag('hasphrase', '', 'id=sfl_hasphrase') ?></label></td> 
    35         </tr> 
    36         <?php if (has_search_categories()): ?> 
    37           <tr> 
    38             <td><label for="sfl_category"><?php echo __('Must be in category') ?></label></td> 
    39             <td><?php include_search_categories() ?></td> 
    40           </tr> 
    41         <?php endif ?> 
     19      <?php echo $form ?> 
    4220    </table> 
    4321  </fieldset> 
    4422 
    45   <?php echo submit_tag(__('Search'), 'accesskey=s') ?> 
    46   <?php echo submit_tag(__('Basic'), 'accesskey=b') ?> 
    47  
    48   <?php echo input_hidden_tag('mode', 'search') ?> 
     23  <input type="submit" value="<?php echo __('Search') ?>" name="commit" accesskey="s" /> 
     24  <input type="submit" value="<?php echo __('Basic') ?>" name="commit" accesskey="b" /> 
    4925</form> 
  • plugins/sfLucenePlugin/trunk/modules/sfLucene/templates/searchControls.php

    r6247 r6525  
    88?> 
    99 
    10 <?php use_helper('I18N') ?> 
     10<?php use_helper('sfLucene', 'I18N') ?> 
    1111 
    1212<h2><?php echo __('Search') ?></h2> 
    1313<p><?php echo __('Use our search engine to pinpoint exactly what you need on our site.') ?></p> 
    1414 
    15 <?php include_component($sf_context->getModuleName(), 'controls') ?> 
     15<?php include_search_controls($form) ?> 
  • plugins/sfLucenePlugin/trunk/modules/sfLucene/templates/searchNoResults.php

    r6247 r6525  
    88?> 
    99 
    10 <?php use_helper('I18N') ?> 
     10<?php use_helper('sfLucene', 'I18N') ?> 
    1111 
    1212<h2><?php echo __('No Results Found') ?></h2> 
    1313<p><?php echo __('We could not find any results with the search term you provided.') ?></p> 
    1414 
    15 <?php include_component($sf_context->getModuleName(), 'controls') ?> 
     15<?php include_search_controls($form) ?> 
  • plugins/sfLucenePlugin/trunk/modules/sfLucene/templates/searchResults.php

    r6247 r6525  
    2020</ol> 
    2121 
    22 <?php include_search_pager($pager, sfConfig::get('app_lucene_pager_radius')) ?> 
     22<?php include_search_pager($pager, sfConfig::get('app_lucene_pager_radius', 5)) ?> 
    2323 
    24 <?php include_search_controls($query) ?> 
     24<?php include_search_controls($form) ?> 
  • plugins/sfLucenePlugin/trunk/test/unit/category/sfLuceneCategoryTest.php

    r6489 r6525  
    1717require dirname(__FILE__) . '/../../bootstrap/unit.php'; 
    1818 
    19 $t = new lime_test(14, new lime_output_color()); 
     19$t = new lime_test(15, new lime_output_color()); 
    2020 
    2121$lucene = sfLucene::getInstance('testLucene', 'en'); 
     
    5959 
    6060$t->is($c->getPhp(), '$categories[\'bar\'] = 8;', '->getPhp() returns valid PHP to save'); 
     61 
     62$t->diag('testing magic methods'); 
     63$t->is($c->__toString(), 'bar', '__toString() returns the name');