/** * Handle the onBeforeSaveCategory event. * * @param Event $event */ public function onBeforeSaveCategory(Event $event) { // Get category id to save $id = $event->params['category']->id; if (!$event->params['isNewCategory']) { // Get old category from db $category = CategoryModel::populateModel(CategoryRecord::model()->findById($id)); // Get fields $this->before = $this->fields($category); } else { // Get fields $this->before = $this->fields($event->params['category'], true); } }
/** * 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; }
/** * 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; }
/** * Initialize the category saving/deleting events. */ public function log() { // Get values before saving craft()->on('categories.onBeforeSaveCategory', function (Event $event) { // Get category id to save $id = $event->params['category']->id; if (!$event->params['isNewCategory']) { // Get old category from db $category = CategoryModel::populateModel(CategoryRecord::model()->findById($id)); // Get fields craft()->auditLog_category->before = craft()->auditLog_category->fields($category); } else { // Get fields craft()->auditLog_category->before = craft()->auditLog_category->fields($event->params['category'], true); } }); // Get values after saving craft()->on('categories.onSaveCategory', function (Event $event) { // Get saved category $category = $event->params['category']; // Get fields craft()->auditLog_category->after = craft()->auditLog_category->fields($category); // New row $log = new AuditLogRecord(); // Get user $user = craft()->userSession->getUser(); // Set user id $log->userId = $user ? $user->id : null; // Set element type $log->type = ElementType::Category; // Set origin $log->origin = craft()->request->isCpRequest() ? craft()->config->get('cpTrigger') . '/' . craft()->request->path : craft()->request->path; // Set before $log->before = craft()->auditLog_category->before; // Set after $log->after = craft()->auditLog_category->after; // Set status $log->status = $event->params['isNewCategory'] ? AuditLogModel::CREATED : AuditLogModel::MODIFIED; // Save row $log->save(false); // Callback craft()->auditLog->elementHasChanged(ElementType::Category, $category->id, craft()->auditLog_category->before, craft()->auditLog_category->after); }); // Get values before deleting craft()->on('categories.onBeforeDeleteCategory', function (Event $event) { // Get deleted category $category = $event->params['category']; // Get fields craft()->auditLog_category->before = craft()->auditLog_category->fields($category); craft()->auditLog_category->after = craft()->auditLog_category->fields($category, true); // New row $log = new AuditLogRecord(); // Set user id $log->userId = craft()->userSession->getUser()->id; // Set element type $log->type = ElementType::Category; // Set origin $log->origin = craft()->request->isCpRequest() ? craft()->config->get('cpTrigger') . '/' . craft()->request->path : craft()->request->path; // Set before $log->before = craft()->auditLog_category->before; // Set after $log->after = craft()->auditLog_category->after; // Set status $log->status = AuditLogModel::DELETED; // Save row $log->save(false); // Callback craft()->auditLog->elementHasChanged(ElementType::Category, $category->id, craft()->auditLog_category->before, craft()->auditLog_category->after); }); }
function saveCategoryAction() { $rebuild = false; $edit = !empty($this->request->categoryId); $imageSrc = false; try { $file = new UploadedFile("categoryImage"); $file->addFilter("extension", array("jpg", "gif", "png")); //check and save image if ($file->wasUploaded()) { $file->setSavePath(CODE_ROOT_DIR . 'uploads/images_categories/'); $file->save(); $imageSrc = $file->getSavedFileName(); } } catch (Exception $e) { } if (!$edit && $imageSrc == false) { $imageSrc = 'defaultCategoryImage.gif'; } //create and save new category $fields = $this->request->getArray(array("name", "urlName", "navigationName", "title", "headerDescription", "possibleTender", "description", "metaDescription", "parentCategoryId", "forbidden")); if ($edit) { $category = $this->category->findByPk($this->request->categoryId); //handle categoryParentId change if ($category->parentCategoryId != $this->request->parentCategoryId && $category->changeParent($this->request->parentCategoryId)) { $rebuild = true; } } else { $category = new CategoryRecord(); } $category->fromArray($fields); if ($imageSrc) { if ($edit) { $category->removeImage(); } $category->imageSrc = $imageSrc; } if (empty($category->urlName)) { $category->urlName = $category->name; } $excludeCategoryId = $edit ? $category->categoryId : false; $category->urlName = $this->category->getFreeUrlName($category->urlName, $excludeCategoryId); $category->save(); if (!$edit) { $this->categoryParent->addNode($this->request->parentCategoryId, $category->categoryId); } if ($rebuild) { $this->categoryParent->moveNode($category->categoryId, $category->parentCategoryId); $this->category->updateValidatedSitesCount(); } $redirect = AppRouter::getRewrittedUrl("/admin/category/index/" . $category->parentCategoryId); $this->redirect($redirect); }