public function create(ViewEvent $event)
 {
     if (null === ($view = ViewQuery::create()->filterBySourceId($event->getSourceId())->findOneBySource($event->getSource()))) {
         $view = new View();
     }
     if ($event->hasDefinedViews()) {
         $view->setView($event->getViewName())->setSource($event->getSource())->setSourceId($event->getSourceId())->setSubtreeView($event->getSubtreeView())->setChildrenView($event->getChildrenView())->save();
     } else {
         $view->delete();
     }
 }
 public function createAction($source_id)
 {
     $form = new ViewForm($this->getRequest());
     try {
         $viewForm = $this->validateForm($form);
         $data = $viewForm->getData();
         $event = new ViewEvent($data['view'], $data['source'], $data['source_id']);
         if ($data['has_subtree'] != 0) {
             $event->setChildrenView($data['children_view'])->setSubtreeView($data['subtree_view']);
         }
         $this->dispatch('view.create', $event);
         return $this->generateSuccessRedirect($form);
     } catch (\Exception $ex) {
         $error_message = $ex->getMessage();
         Tlog::getInstance()->error("Failed to validate View form: {$error_message}");
     }
     $this->setupFormErrorContext('Failed to process View form data', $error_message, $form);
     $sourceType = $this->getRequest()->get('source_type');
     return $this->render($sourceType . '-edit', [$sourceType . '_id' => $source_id, 'current_tab' => 'modules']);
 }