Пример #1
0
 /**
  * Check missing widgets and add them.
  *
  * @param array $modulesWidgets  Widget modules.
  * @param array $notFoundWidgets Widgets packages that must be created.
  *
  * @return void
  */
 protected function _checkMissingWidgets($modulesWidgets, $notFoundWidgets)
 {
     print ConsoleUtil::headLine('Checking widgets existence...');
     foreach ($modulesWidgets as $module => $widgets) {
         foreach ($widgets as $widgetObject) {
             $this->_info('Widget ' . $module . '.' . $widgetObject['name'] . ': ');
             $widget = Widget::getFirst('module = "%s" AND name = "%s"', [$module, $widgetObject['name']]);
             if (!$widget) {
                 $widget = new Widget();
                 if ($widget->save($widgetObject)) {
                     print ConsoleUtil::infoLine('Created.', false, 1);
                 } else {
                     print ConsoleUtil::infoLine('Failed.', false, 1, ConsoleUtil::FG_RED);
                     $messages = iterator_to_array($widget->getMessages());
                     $this->getDI()->getLogger()->error('Failed to created widget "' . $module . '"."' . $widgetObject . '": ' . implode(', ', $messages));
                 }
             } else {
                 print ConsoleUtil::infoLine('Exists.', false, 1, ConsoleUtil::FG_GREEN);
             }
         }
     }
     foreach ($notFoundWidgets as $widgetObject) {
         $this->_info('Widget ' . $widgetObject->name . ': ');
         $widget = Widget::findFirstByName($widgetObject->name);
         if ($widget) {
             print ConsoleUtil::infoLine('Exists.', false, 1, ConsoleUtil::FG_GREEN);
             continue;
         }
         $widget = new Widget();
         if ($widget->save($widgetObject->toArray())) {
             print ConsoleUtil::infoLine('Created.', false, 1);
         } else {
             print ConsoleUtil::infoLine('Failed.', false, 1, ConsoleUtil::FG_RED);
             $messages = iterator_to_array($widget->getMessages());
             $this->getDI()->getLogger()->error('Failed to created widget "' . $widgetObject . '": ' . implode(', ', $messages));
         }
     }
     print PHP_EOL;
 }
Пример #2
0
 /**
  * Enable package in config.
  *
  * @param Package $package Package object.
  *
  * @return void
  */
 protected function _enablePackageConfig(Package $package)
 {
     switch ($package->type) {
         case Manager::PACKAGE_TYPE_MODULE:
             $data = $package->getData();
             // Install widgets.
             if (!empty($data['widgets'])) {
                 $errors = [];
                 foreach ($data['widgets'] as $widgetData) {
                     $widget = new Widget();
                     try {
                         $widget->save($widgetData);
                     } catch (\PDOException $e) {
                         $this->flash->notice('Failed to install module widget... Check logs.');
                         PackageException::exception($e);
                     }
                     if ($widget->validationHasFailed()) {
                         $messages = $widget->getMessages();
                         foreach ($messages as $message) {
                             $errors[] = $message->getMessage();
                         }
                     }
                 }
                 if (!empty($errors)) {
                     $this->flash->notice('There was some errors during installation:' . implode('<br/> - ', $errors));
                 }
             }
             // Enable module widgets.
             $this->db->update(Widget::getTableName(), ['enabled'], [1], "module = '{$package->name}'");
             break;
         case Manager::PACKAGE_TYPE_WIDGET:
             if ($widget = $package->getWidget()) {
                 $widget->enabled = 1;
                 $widget->save();
             } else {
                 $widget = new Widget();
                 $widget->assign($package->toArray());
                 // Check widget data.
                 $data = $package->getData();
                 if (!empty($data['module'])) {
                     $widget->module = $data['module'];
                 }
                 $widget->save();
                 $package->addData('widget_id', $widget->id);
                 $package->save();
             }
             break;
     }
 }