Example #1
0
 /**
  * Get all the substitutions for this tag
  *
  * @param   integer  $tag_id    Tag ID
  * @param   boolean  $asString  Return results as string?
  * @param   integer  $offset    Record offset
  * @param   integer  $limit     Number of records to return (returns all if less than 1)
  * @return  mixed    Array by default, string if $asString is set to true
  */
 public function saveSubstitutions($tag_string = '', $tag_id = null)
 {
     $tag_id = $tag_id ?: $this->id;
     if (!$tag_id) {
         $this->setError(Lang::txt('Missing argument.'));
         return false;
     }
     require_once __DIR__ . DS . 'substitute.php';
     $ts = new Substitute($this->_db);
     $subs = $ts->getRecords($tag_id);
     if (!$subs) {
         $subs = array();
     }
     $raw_tags = explode(',', trim($tag_string));
     $tags = array();
     foreach ($raw_tags as $raw_tag) {
         $nrm = $this->normalize($raw_tag);
         $tags[] = $nrm;
         if (isset($subs[$nrm])) {
             continue;
             // Substitution already exists
         }
         $sub = new Substitute($this->_db);
         $sub->raw_tag = trim($raw_tag);
         $sub->tag_id = $tag_id;
         if ($sub->check()) {
             if (!$sub->store()) {
                 $this->setError($sub->getError());
             }
         }
     }
     // Build list of tags from old list not found in new list and delete them
     $remove = array();
     foreach ($subs as $key => $sub) {
         if (!in_array($key, $tags)) {
             $remove[] = $key;
         }
     }
     $ts = new Substitute($this->_db);
     if (count($remove) > 0) {
         if (!$ts->removeForTag($tag_id, $remove)) {
             $this->setError($this->_db->getErrorMsg());
             return false;
         }
     }
     // Get all possibly existing tags that are now aliases
     $sql = "SELECT t.id FROM {$this->_tbl} AS t WHERE t.tag IN ('" . implode("','", $tags) . "')";
     $this->_db->setQuery($sql);
     if ($ids = $this->_db->loadObjectList()) {
         require_once __DIR__ . DS . 'object.php';
         $to = new Object($this->_db);
         // Move associations on tag and delete tag
         foreach ($ids as $id) {
             if ($tag_id != $id->id) {
                 // Get all the associations to this tag
                 // Loop through the associations and link them to a different tag
                 $to->moveObjects($id->id, $tag_id);
                 // Get all the substitutions to this tag
                 // Loop through the records and link them to a different tag
                 $ts->moveSubstitutes($id->id, $tag_id);
                 // Delete the tag
                 $tag = new self($this->_db);
                 $tag->delete($id->id);
             }
         }
     }
     return true;
 }