コード例 #1
0
 /**
  * Saves a category.
  *
  * @param CategoryModel $category
  *
  * @throws Exception|\Exception
  * @return bool
  */
 public function saveCategory(CategoryModel $category)
 {
     $isNewCategory = !$category->id;
     $hasNewParent = $this->_checkForNewParent($category);
     if ($hasNewParent) {
         if ($category->newParentId) {
             $parentCategory = $this->getCategoryById($category->newParentId, $category->locale);
             if (!$parentCategory) {
                 throw new Exception(Craft::t('No category exists with the ID “{id}”.', array('id' => $category->newParentId)));
             }
         } else {
             $parentCategory = null;
         }
         $category->setParent($parentCategory);
     }
     // Category data
     if (!$isNewCategory) {
         $categoryRecord = CategoryRecord::model()->findById($category->id);
         if (!$categoryRecord) {
             throw new Exception(Craft::t('No category exists with the ID “{id}”.', array('id' => $category->id)));
         }
     } else {
         $categoryRecord = new CategoryRecord();
     }
     $categoryRecord->groupId = $category->groupId;
     $categoryRecord->validate();
     $category->addErrors($categoryRecord->getErrors());
     if ($category->hasErrors()) {
         return false;
     }
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         // Fire an 'onBeforeSaveCategory' event
         $event = new Event($this, array('category' => $category, 'isNewCategory' => $isNewCategory));
         $this->onBeforeSaveCategory($event);
         // Is the event giving us the go-ahead?
         if ($event->performAction) {
             $success = craft()->elements->saveElement($category);
             // If it didn't work, rollback the transaction in case something changed in onBeforeSaveCategory
             if (!$success) {
                 if ($transaction !== null) {
                     $transaction->rollback();
                 }
                 return false;
             }
             // Now that we have an element ID, save it on the other stuff
             if ($isNewCategory) {
                 $categoryRecord->id = $category->id;
             }
             $categoryRecord->save(false);
             // Has the parent changed?
             if ($hasNewParent) {
                 if (!$category->newParentId) {
                     craft()->structures->appendToRoot($category->getGroup()->structureId, $category);
                 } else {
                     craft()->structures->append($category->getGroup()->structureId, $category, $parentCategory);
                 }
             }
             // Update the category's descendants, who may be using this category's URI in their own URIs
             craft()->elements->updateDescendantSlugsAndUris($category, true, true);
         } else {
             $success = false;
         }
         // Commit the transaction regardless of whether we saved the category, in case something changed
         // in onBeforeSaveCategory
         if ($transaction !== null) {
             $transaction->commit();
         }
     } catch (\Exception $e) {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         throw $e;
     }
     if ($success) {
         // Fire an 'onSaveCategory' event
         $this->onSaveCategory(new Event($this, array('category' => $category, 'isNewCategory' => $isNewCategory)));
     }
     return $success;
 }
コード例 #2
0
 /**
  * Saves a category.
  *
  * @param CategoryModel $category
  *
  * @throws Exception|\Exception
  * @return bool
  */
 public function saveCategory(CategoryModel $category)
 {
     $isNewCategory = !$category->id;
     // Category data
     if (!$isNewCategory) {
         $categoryRecord = CategoryRecord::model()->findById($category->id);
         if (!$categoryRecord) {
             throw new Exception(Craft::t('No category exists with the ID “{id}”', array('id' => $category->id)));
         }
     } else {
         $categoryRecord = new CategoryRecord();
     }
     $categoryRecord->groupId = $category->groupId;
     $categoryRecord->validate();
     $category->addErrors($categoryRecord->getErrors());
     if ($category->hasErrors()) {
         return false;
     }
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         // Fire an 'onBeforeSaveCategory' event
         $this->onBeforeSaveCategory(new Event($this, array('category' => $category, 'isNewCategory' => $isNewCategory)));
         if (craft()->elements->saveElement($category, false)) {
             // Now that we have an element ID, save it on the other stuff
             if ($isNewCategory) {
                 $categoryRecord->id = $category->id;
             }
             $categoryRecord->save(false);
             if ($isNewCategory) {
                 // Add it to the group's structure
                 craft()->structures->appendToRoot($category->getGroup()->structureId, $category);
             }
             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 'onSaveCategory' event
     $this->onSaveCategory(new Event($this, array('category' => $category, 'isNewCategory' => $isNewCategory)));
     return true;
 }