Development

Changeset 7475

You must first sign up to be able to contribute.

Changeset 7475

Show
Ignore:
Timestamp:
02/13/08 14:06:14 (9 months ago)
Author:
xavier
Message:

sfPropelActAsTaggableBehaviorPlugin: added getTaggedWithCriteria, based on a patch of Nicolas Perriault.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelActAsTaggableBehaviorPlugin/trunk/lib/model/TagPeer.php

    r6442 r7475  
    22/* 
    33 * This file is part of the sfPropelActAsTaggableBehavior package. 
    4  *  
     4 * 
    55 * (c) 2007 Xavier Lacot <xavier@lacot.org> 
    6  *  
     6 * 
    77 * For the full copyright and license information, please view the LICENSE 
    88 * file that was distributed with this source code. 
     
    1111/** 
    1212 * Subclass for performing query and update operations on the 'tag' table. 
    13  *  
     13 * 
    1414 * @package plugins.sfPropelActAsTaggableBehaviorPlugin.lib.model 
    15  */  
     15 */ 
    1616class TagPeer extends BaseTagPeer 
    1717{ 
    1818  /** 
    1919   * Returns all tags, eventually with a limit option. 
    20    * The first optionnal parameter permits to add some restrictions on the  
     20   * The first optionnal parameter permits to add some restrictions on the 
    2121   * objects the selected tags are related to. 
    2222   * The second optionnal parameter permits to restrict the tag selection with 
    2323   * different criterias 
    24    *  
     24   * 
    2525   * @param      Criteria    $c 
    2626   * @param      array       $options 
     
    6969  /** 
    7070   * Returns all tags, sorted by name, with their number of occurencies. 
    71    * The first optionnal parameter permits to add some restrictions on the  
     71   * The first optionnal parameter permits to add some restrictions on the 
    7272   * objects the selected tags are related to. 
    7373   * The second optionnal parameter permits to restrict the tag selection with 
    7474   * different criterias 
    75    *  
     75   * 
    7676   * @param      Criteria    $c 
    7777   * @param      array       $options 
     
    135135 
    136136  /** 
    137    * Returns the names of the models that have instances tagged with one or  
    138    * several tags. The optionnal parameter might be a string, an array, or a  
     137   * Returns the names of the models that have instances tagged with one or 
     138   * several tags. The optionnal parameter might be a string, an array, or a 
    139139   * comma separated string 
    140    *  
     140   * 
    141141   * @param      mixed       $tags 
    142142   * @return     array 
     
    190190 
    191191  /** 
    192    * Returns the most popular tags with their associated weight. See  
     192   * Returns the most popular tags with their associated weight. See 
    193193   * sfPropelActAsTaggableToolkit::normalize for more details. 
    194    *  
    195    * The first optionnal parameter permits to add some restrictions on the  
     194   * 
     195   * The first optionnal parameter permits to add some restrictions on the 
    196196   * objects the selected tags are related to. 
    197197   * The second optionnal parameter permits to restrict the tag selection with 
    198198   * different criterias 
    199    *  
     199   * 
    200200   * @param      Criteria    $c 
    201201   * @param      array       $options 
     
    219219 
    220220  /** 
    221    * Returns the tags that are related to one or more other tags, with their  
    222    * associated weight (see sfPropelActAsTaggableToolkit::normalize for more  
     221   * Returns the tags that are related to one or more other tags, with their 
     222   * associated weight (see sfPropelActAsTaggableToolkit::normalize for more 
    223223   * details). 
    224    * The "related tags" of one tag are the ones which have at least one  
     224   * The "related tags" of one tag are the ones which have at least one 
    225225   * taggable object in common. 
    226    *  
    227    * The first optionnal parameter permits to add some restrictions on the  
     226   * 
     227   * The first optionnal parameter permits to add some restrictions on the 
    228228   * objects the selected tags are related to. 
    229229   * The second optionnal parameter permits to restrict the tag selection with 
    230230   * different criterias 
    231    *  
     231   * 
    232232   * @param      mixed       $tags 
    233233   * @param      array       $options 
     
    285285  /** 
    286286   * Retrieves the objects tagged with one or several tags. 
    287    *  
     287   * 
    288288   * The second optionnal parameter permits to restrict the tag selection with 
    289289   * different criterias 
    290    *  
     290   * 
    291291   * @param      mixed       $tags 
    292292   * @param      array       $options 
     
    314314 
    315315  /** 
     316   * Retrieve a Criteria instance for querying tagged model objects. 
     317   * 
     318   * Example: 
     319   * 
     320   * $c = TagPeer::getTaggedWithCriteria('Article', array('tag1', 'tag2')); 
     321   * $c->addDescendingOrderByColumn(ArticlePeer::POSTED_AT); 
     322   * $c->setLimit(10); 
     323   * $this->articles = ArticlePeer::doSelectJoinAuthor($c); 
     324   * 
     325   * @param  string    $model  Taggable model name 
     326   * @param  mixed     $tags   array of tags (can be a string where tags are 
     327   * comma separated) 
     328   * @param  Criteria  $c      Existing Criteria to hydrate 
     329   * @return Criteria 
     330   */ 
     331  public static function getTaggedWithCriteria($model, $tags = array(), Criteria $c = null) 
     332  { 
     333    if (!$c instanceof Criteria) 
     334    { 
     335      $c = new Criteria(); 
     336    } 
     337 
     338    if (!class_exists($model) || !is_callable(new $model, 'getPeer')) 
     339    { 
     340      throw new PropelException(sprintf('The class "%s" does not exist, or it is not a model class.', 
     341                                        $model)); 
     342    } 
     343 
     344    $taggings = self::getTaggings($tags, array('model' => $model)); 
     345    $tagging = isset($taggings[$model]) ? $taggings[$model] : array(); 
     346    $peer = get_class(call_user_func(array(new $model, 'getPeer'))); 
     347    $c->add(constant($peer.'::ID'), $tagging, Criteria::IN); 
     348 
     349    return $c; 
     350  } 
     351 
     352  /** 
    316353   * Returns the taggings associated to one tag or a set of tags. 
    317    *  
     354   * 
    318355   * The second optionnal parameter permits to restrict the results with 
    319356   * different criterias 
    320    *  
    321    * @param      mixed       $tags 
    322    * @param      array       $options 
    323    * @return     array 
    324    */ 
    325   private static function getTaggings($tags = array(), $options = array()) 
     357   * 
     358   * @param      mixed       $tags      Array of tag strings or string 
     359   * @param      array       $options   Array of options parameters 
     360   * @return     array 
     361   */ 
     362  protected static function getTaggings($tags = array(), $options = array()) 
    326363  { 
    327364    $tags = sfPropelActAsTaggableToolkit::explodeTagString($tags); 
     
    342379    $c->addSelectColumn(TaggingPeer::TAGGABLE_ID); 
    343380 
     381    // Taggable model class option 
    344382    if (isset($options['model'])) 
    345383    { 
     384      if (!class_exists($options['model']) || !is_callable(new $options['model'], 'getPeer')) 
     385      { 
     386        throw new PropelException(sprintf('The class "%s" does not exist, or it is not a model class.', 
     387                                          $options['model'])); 
     388      } 
     389 
    346390      $c->add(TaggingPeer::TAGGABLE_MODEL, $options['model']); 
    347391    } 
     
    409453  /** 
    410454   * Retrives a tag by his name. 
    411    *  
     455   * 
    412456   * @param      String      $tagname 
    413457   * @return     Tag 
     
    423467   * Retrieves a tag by his name. If it does not exist, creates it (but does not 
    424468   * save it) 
    425    *  
     469   * 
    426470   * @param      String      $tagname 
    427471   * @return     Tag 
  • plugins/sfPropelActAsTaggableBehaviorPlugin/trunk/lib/sfPropelActAsTaggableToolkit.class.php

    r7390 r7475  
    124124    if (!is_string($model)) 
    125125    { 
    126       throw new Exception('The param passed to the metod isTaggable must be a string.'); 
     126      throw new Exception('The param passed to the method isTaggable must be an object or a string.'); 
    127127    } 
    128128