/** * Remove widgets if their is unused. * * @param array $widgets Widgets list. * * @return void */ protected function _removeUnusedWidgets($widgets) { // Get widgets from databases. print ConsoleUtil::headLine('Checking unused widgets...'); foreach (Widget::find() as $widget) { if (!in_array($widget->name, $widgets)) { $this->_info('Removing unused widget: ' . $widget->name . PHP_EOL); $widget->delete(); } } print PHP_EOL; }
/** * Prepare widgets metadata for Engine. * * @param DI $di Dependency injection. * * @return void */ protected function _initWidgets(DI $di) { if ($di->get('app')->isConsole()) { return; } $cache = $di->get('cacheData'); $widgets = $cache->get(System::CACHE_KEY_WIDGETS_METADATA); if ($widgets === null) { $widgets = []; foreach (Widget::find() as $object) { $widgets[] = [$object->id, $object->getKey(), $object]; } $cache->save(System::CACHE_KEY_WIDGETS_METADATA, $widgets, 0); // Unlimited. } $di->get('widgets')->addWidgets($widgets); }
/** * Get widget object. * * @return Widget||null */ public function getWidget() { if ($this->type !== Manager::PACKAGE_TYPE_WIDGET) { return null; } $data = $this->getData(); if (empty($data['widget_id'])) { return null; } return Widget::findFirstById($data['widget_id']); }
/** * Validates the form. * * @param array $data Data to validate. * @param bool $skipEntityCreation Skip entity creation. * * @return boolean */ public function isValid($data = null, $skipEntityCreation = true) { if (!$data) { $data = $this->getDI()->getRequest()->getPost(); } // Check package location. $packageManager = new Manager(); $path = $packageManager->getPackageLocation($data['type']); if (!is_writable($path)) { $this->addError('Can not create package. Package location isn\'t writable: ' . $path); $this->setValues($data); return false; } // Also check that config file is writable. if (!is_writable(ROOT_PATH . Config::CONFIG_PATH)) { $this->addError('Configuration file isn\'t writable...'); $this->setValues($data); return false; } if (isset($data['type']) && $data['type'] == 'widget' && !$this->hasEntity('widget')) { $this->addEntity(new WidgetModel(), 'widget'); } if (!parent::isValid($data, $skipEntityCreation)) { return false; } // Check package existence. $id = $this->getEntity()->id; $condition = "type='{$data['type']}' AND name='{$data['name']}'" . ($id ? " AND id!='{$id}'" : ''); if (Package::findFirst($condition)) { $this->addError('Package with that name already exist!'); return false; } // Check widget existence. if ($this->hasEntity('widget')) { $name = ucfirst($data['name']); $id = $this->getEntity('widget')->id; $condition = "module='{$data['module']}' AND name='{$name}'" . ($id ? " AND id!='{$id}'" : ''); if (WidgetModel::findFirst($condition)) { $this->addError('Widget with that name already exist!'); return false; } } return true; }
/** * Disable package in config. * * @param Package $package Package object. * * @return void */ protected function _disablePackageConfig(Package $package) { switch ($package->type) { case Manager::PACKAGE_TYPE_MODULE: // Disable module widgets. $this->db->update(Widget::getTableName(), ['enabled'], [0], "module = '{$package->name}'"); break; case Manager::PACKAGE_TYPE_WIDGET: if ($widget = $package->getWidget()) { $widget->enabled = 0; $widget->save(); } break; } }
/** * Widget options. * * @return void * * @Route("/widget-options", methods={"GET", "POST"}, name="admin-pages-widget-options") */ public function widgetOptionsAction() { $widgetIndex = $this->request->get('widget_index', 'int', -1); if ($widgetIndex != '0' && intval($widgetIndex) == 0) { $widgetIndex = -1; } $currentPageWidgets = $this->session->get('admin-pages-manage', []); if ($widgetIndex == -1) { $widgetIndex = $this->session->get('admin-pages-widget-index'); $currentPageWidgets[$widgetIndex] = ['widget_index' => $widgetIndex, 'id' => 0, 'layout' => $this->request->get('layout', 'string', 'middle'), 'widget_id' => $this->request->get('widget_id', 'int'), 'params' => []]; } if (empty($currentPageWidgets[$widgetIndex])) { return; } $widgetData = $currentPageWidgets[$widgetIndex]; $id = $widgetData['id']; $widgetParams = $widgetData['params']; $widgetParams['content_id'] = $id; $widget_id = $widgetData['widget_id']; $widgetMetadata = Widget::findFirstById($widget_id); $form = new CoreForm(); // building widget form $adminForm = $widgetMetadata->admin_form; if (empty($adminForm)) { $form->addText('title'); } elseif ($adminForm == 'action') { $widgetName = $widgetMetadata->name; if ($widgetMetadata->module !== null) { $widgetClass = '\\' . ucfirst($widgetMetadata->module) . '\\Widget\\' . $widgetName . '\\Controller'; } else { $widgetClass = '\\Widget\\' . $widgetName . '\\Controller'; } $widgetController = new $widgetClass(); $widgetController->setDefaults($widgetName, ucfirst($widgetMetadata->module), $widgetParams); $widgetController->prepare(); $form = $widgetController->adminAction(); } else { $form = new $adminForm(); } if ($widgetMetadata->is_paginated == 1) { $form->addText('count', 'Items count', null, 10); $form->setOrder('count', 1000); } if ($widgetMetadata->is_acl_controlled == 1) { $form->addMultiSelect('roles', 'Roles', null, Role::find(), null, ['using' => ['id', 'name']]); $form->setOrder('roles[]', 1001); } // set form values if (!empty($widgetParams)) { $form->setValues($widgetParams); } if (!$this->request->isPost() || !$form->isValid()) { $this->view->form = $form; $this->view->id = $id; $this->view->name = $widgetMetadata->name; return; } $currentPageWidgets[$widgetIndex]['params'] = $form->getValues(); $this->resolveModal(['hide' => true, 'customJs' => 'setEditedWidgetIndex(' . $widgetIndex . ');']); $this->session->set('admin-pages-manage', $currentPageWidgets); $this->session->set('admin-pages-widget-index', ++$widgetIndex); }