/**
  * Handle the onBeforeDeleteCategory event.
  *
  * @param Event $event
  */
 public function onBeforeDeleteCategory(Event $event)
 {
     // Get deleted category
     $category = $event->params['category'];
     // Get fields
     $this->before = $this->fields($category);
     $this->after = $this->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 = $this->before;
     // Set after
     $log->after = $this->after;
     // Set status
     $log->status = AuditLogModel::DELETED;
     // Save row
     $log->save(false);
     // Callback
     craft()->auditLog->elementHasChanged(ElementType::Category, $category->id, $this->before, $this->after);
 }
 /**
  * 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);
     });
 }