/** * Saves a tag. * * @param TagModel $tag * @throws Exception * @return bool */ public function saveTag(TagModel $tag) { $isNewTag = !$tag->id; // Tag data if (!$isNewTag) { $tagRecord = TagRecord::model()->with('element')->findById($tag->id); if (!$tagRecord) { throw new Exception(Craft::t('No tag exists with the ID “{id}”', array('id' => $tag->id))); } $elementRecord = $tagRecord->element; // If tag->setId is null and there is an tagRecord setId, we assume this is a front-end edit. if ($tag->setId === null && $tagRecord->setId) { $tag->setId = $tagRecord->setId; } } else { $tagRecord = new TagRecord(); $elementRecord = new ElementRecord(); $elementRecord->type = ElementType::Tag; } $tagRecord->setId = $tag->setId; $tagRecord->name = $tag->name; $tagRecord->validate(); $tag->addErrors($tagRecord->getErrors()); $elementRecord->validate(); $tag->addErrors($elementRecord->getErrors()); if (!$tag->hasErrors()) { // Save the element record first $elementRecord->save(false); // Now that we have an element ID, save it on the other stuff if (!$tag->id) { $tag->id = $elementRecord->id; $tagRecord->id = $tag->id; } $tagRecord->save(false); // Update the search index craft()->search->indexElementAttributes($tag, $tag->locale); // Fire an 'onSaveTag' event $this->onSaveTag(new Event($this, array('tag' => $tag, 'isNewTag' => $isNewTag))); return true; } else { return false; } }
/** * Saves a tag. * * @param TagModel $tag * * @throws Exception|\Exception * @return bool */ public function saveTag(TagModel $tag) { $isNewTag = !$tag->id; // Tag data if (!$isNewTag) { $tagRecord = TagRecord::model()->findById($tag->id); if (!$tagRecord) { throw new Exception(Craft::t('No tag exists with the ID “{id}”.', array('id' => $tag->id))); } } else { $tagRecord = new TagRecord(); } $tagRecord->groupId = $tag->groupId; $tagRecord->validate(); $tag->addErrors($tagRecord->getErrors()); if ($tag->hasErrors()) { return false; } $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { // Fire an 'onBeforeSaveTag' event $event = new Event($this, array('tag' => $tag, 'isNewTag' => $isNewTag)); $this->onBeforeSaveTag($event); // Is the event giving us the go-ahead? if ($event->performAction) { $success = craft()->elements->saveElement($tag, false); // If it didn't work, rollback the transaction in case something changed in onBeforeSaveTag if (!$success) { if ($transaction !== null) { $transaction->rollback(); } return false; } // Now that we have an element ID, save it on the other stuff if ($isNewTag) { $tagRecord->id = $tag->id; } $tagRecord->save(false); } else { $success = false; } // Commit the transaction regardless of whether we saved the tag, in case something changed // in onBeforeSaveTag if ($transaction !== null) { $transaction->commit(); } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } if ($success) { // Fire an 'onSaveTag' event $this->onSaveTag(new Event($this, array('tag' => $tag, 'isNewTag' => $isNewTag))); if ($this->hasEventHandler('onSaveTagContent')) { // Fire an 'onSaveTagContent' event (deprecated) $this->onSaveTagContent(new Event($this, array('tag' => $tag))); } } return $success; }
/** * Saves a tag. * * @param TagModel $tag * * @throws Exception|\Exception * @return bool */ public function saveTag(TagModel $tag) { $isNewTag = !$tag->id; // Tag data if (!$isNewTag) { $tagRecord = TagRecord::model()->findById($tag->id); if (!$tagRecord) { throw new Exception(Craft::t('No tag exists with the ID “{id}”', array('id' => $tag->id))); } } else { $tagRecord = new TagRecord(); } $tagRecord->groupId = $tag->groupId; // See if we can find another tag with tha same name $criteria = craft()->elements->getCriteria(ElementType::Tag); $criteria->groupId = $tag->groupId; $criteria->search = 'name::"' . $tag->name . '"'; $criteria->id = $isNewTag ? null : 'not ' . $tag->id; $matchingTag = $criteria->first(); if ($matchingTag) { // The name needs to be 100% identical for validation to take care of this. $tagRecord->name = $matchingTag->name; } else { $tagRecord->name = $tag->name; } $tagRecord->validate(); $tag->addErrors($tagRecord->getErrors()); if ($tag->hasErrors()) { return false; } $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { // Fire an 'onBeforeSaveTag' event $this->onBeforeSaveTag(new Event($this, array('tag' => $tag, 'isNewTag' => $isNewTag))); if (craft()->elements->saveElement($tag, false)) { // Now that we have an element ID, save it on the other stuff if ($isNewTag) { $tagRecord->id = $tag->id; } $tagRecord->save(false); if ($transaction !== null) { $transaction->commit(); } } else { return false; } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } // If we've made it here, everything has been successful so far. // Fire an 'onSaveTag' event $this->onSaveTag(new Event($this, array('tag' => $tag, 'isNewTag' => $isNewTag))); if ($this->hasEventHandler('onSaveTagContent')) { // Fire an 'onSaveTagContent' event (deprecated) $this->onSaveTagContent(new Event($this, array('tag' => $tag))); } return true; }