示例#1
0
 /**
  * Save tag substitutions
  *
  * @param   string   $tag_string
  * @return  boolean
  */
 public function saveSubstitutions($tag_string = '')
 {
     // Get the old list of substitutions
     $subs = array();
     foreach ($this->substitutes()->rows() as $sub) {
         $subs[$sub->get('tag')] = $sub;
     }
     // Add the specified tags as substitutes if not
     // already a substitute
     $raw_tags = trim($tag_string);
     $raw_tags = preg_split("/(,|;)/", $raw_tags);
     $tags = array();
     foreach ($raw_tags as $raw_tag) {
         $nrm = $this->normalize($raw_tag);
         $tags[] = $nrm;
         if (isset($subs[$nrm])) {
             continue;
             // Substitution already exists
         }
         $sub = Substitute::blank();
         $sub->set('raw_tag', trim($raw_tag));
         $sub->set('tag', trim($nrm));
         $sub->set('tag_id', $this->get('id'));
         if (!$sub->save()) {
             $this->addError($sub->getError());
         }
     }
     // Run through the old list of substitutions, finding any
     // not in the new list and delete them
     foreach ($subs as $key => $sub) {
         if (!in_array($key, $tags)) {
             if (!$sub->destroy()) {
                 $this->addError($sub->getError());
                 return false;
             }
         }
     }
     // Get all possibly existing tags that are now aliases
     $ids = self::all()->whereIn('tag', $tags)->rows();
     // Move associations on tag and delete tag
     foreach ($ids as $tag) {
         if ($tag->get('id') != $this->get('id')) {
             // Get all the associations to this tag
             // Loop through the associations and link them to a different tag
             Object::moveTo($tag->get('id'), $this->get('id'));
             // Get all the substitutions to this tag
             // Loop through the records and link them to a different tag
             Substitute::moveTo($tag->get('id'), $this->get('id'));
             // Delete the tag
             $tag->destroy();
         }
     }
     $this->set('objects', $this->objects()->total());
     $this->set('substitutes', $this->substitutes()->total());
     $this->save();
     return true;
 }