All pastes #753914 Raw Edit

Mine

public php v1 · immutable
#753914 ·published 2007-10-29 17:20 UTC
rendered paste body
  /* tags are passed as a comma and/or space seperated string   * for exmaple:   * "Documentation Database, Other"  <=- this is 3 tags   */  public function setTags($tags)  {    $orig_tags = $this->getTags();    $new = explode(',', $tags);    $old = explode(',', $orig_tags);    $new = array_map('trim', $new);    $old = array_map('trim', $old);    $added = array_diff($new, $old);    $removed = array_diff($old, $new);    $message = 'New Tags: ';    foreach($added as $n)    {       $message .= '"'.$n.'","';    }    sfContext::getInstance()->getLogger()->log($message);    $message = 'Tags to remove: ';    foreach($removed as $n)    {       $message .= '"'.$n.'",';    }    sfContext::getInstance()->getLogger()->log($message);    // remove the old ones!    foreach($removed as $name)    {      foreach($this->Tag as $k => $t)      {        if ($t->name == $name)        {          $this->unlink('Tag', array($t->id));        }      }    }    // uses LIKE for case-insensitivity    $find_q = new Doctrine_Query();    $find_q->select('t.id')           ->from('Tag t')           ->where('t.name LIKE ?');    foreach($added as $name)    {      // first, lets see if a tag by this name already exists      $ret = $find_q->fetchOne(array($name));      if (!$ret)      {        $ret = new Tag();        $ret->name = $name;      }      $this->Tag[] = $ret;    }  }