/** * Delete old widget * * @param array $allWidget */ protected final function deleteOldWidget($allWidget = []) { if (count($allWidget)) { $oldWidget = CoreWidgets::find()->toArray(); $oldWidget = array_column($oldWidget, 'base_name'); $widgetMustDelete = array_diff($oldWidget, $allWidget); if (count($widgetMustDelete)) { foreach ($widgetMustDelete as $w) { $tmp = CoreWidgets::findFirst(['conditions' => 'base_name = ?0', 'bind' => [$w]]); if ($tmp) { $tmp->delete(); } } } } }
/** * Instance construct * * @param integer $id * @param array $widgetInfo * @param array $options */ public function __construct($id = null, $widgetInfo = [], $options = []) { $this->validation = $this->checkOptions($options); if ($this->validation) { $this->newOptions = $options; $id = (int) $id; if ($id > 0) { /** * @var CoreWidgetValues $widget */ $widget = CoreWidgetValues::findFirst(['conditions' => 'widget_value_id = ?0', 'bind' => [$id]]); if (!$widget) { $this->options = new \stdClass(); $this->options->_layout = 'default'; } else { $this->options = json_decode($widget->options); $this->_id = $id; } } $this->_widget_name = explode('_', get_class($this))[0]; $this->widget_class_name = strtolower(get_class($this)); /** * @var CoreWidgets $widgetInfoDb */ $widgetInfoDb = CoreWidgets::findFirst(['conditions' => 'lower(base_name) = ?0', 'bind' => [str_replace('_widget', '', $this->widget_class_name)]]); $widgetInfo['_layout'] = 'default'; if (isset($widgetInfo['title'])) { $this->_title = __($widgetInfo['title']); } else { $this->_title = __($widgetInfoDb->title); } if (isset($widgetInfo['description'])) { $this->_description = __($widgetInfo['description']); } else { $this->_description = __($widgetInfoDb->description); } $this->_layout = $this->_getLayout($this->_widget_name); } }
/** * Index action * List view all widget available and sidebar register in default frontend template * * @return void */ public function indexAction() { $displayIcon = ''; //Add widget frontend translation $allWidget = get_child_folder(APP_DIR . '/widgets/frontend/'); ZTranslate::getInstance()->addWidgetLang($allWidget, 'frontend'); //Get default frontend template $defaultTemplate = CoreTemplates::findFirst("published = 1 AND location = \"frontend\""); if ($defaultTemplate && isset($defaultTemplate->base_name)) { $defaultTemplate = $defaultTemplate->base_name; } else { $defaultTemplate = PDI::getDefault()->get("config")->frontendTemplate->defaultTemplate; } ZTranslate::getInstance()->addTemplateLang($defaultTemplate, 'frontend'); //Update sidebar with default frontend template $this->updateSidebarTemplate($defaultTemplate); $widget_html = ''; //Get widget published $_widget = CoreWidgets::find(['conditions' => 'published = 1']); foreach ($_widget as $w) { $class_name = $w->base_name . "_widget"; if (class_exists($class_name)) { /** * @var ZWidget $current_widget */ $current_widget = new $class_name(); $widget_html .= $current_widget->getWidgetHtmlBackend(); } } //Set view widget_html $this->view->setVar('widget_html', $widget_html); /** * Find all sidebar register in default frontend template * * @var CoreSidebars[] $sidebars */ $sidebars = CoreSidebars::find("theme_name = \"" . $defaultTemplate . "\""); //Create sidebar html $sidebar_html = ''; $sortable = []; $panelAfter = '</div> </div> </div> </div>'; $totalSidebar = count($sidebars); $half = round($totalSidebar / 2); foreach ($sidebars as $index => $sidebar) { if ($index == 0) { $sidebar_html .= '<div class="col-sm-6 col-sidebar-first no-padding-lr" style="padding-right: 8px">'; } if ($index == 0) { $class = 'box-sidebar box-sidebar-first'; } else { $class = 'box-sidebar'; } $panelBefore = '<div class="box ' . $class . '""> <div class="box box-default"> <div class="box-header with-border">' . $displayIcon . '<h3 class="box-title">' . __($sidebar->sidebar_name) . '</h3>' . '<div class="box-tools pull-right"> <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button> </div> </div> <div class="box-body"> <div class="box">'; $sidebar_html .= $panelBefore; $sortable[] = '#' . $sidebar->sidebar_base_name . '_sortable'; $sidebar_html .= "<div class=\"zsidebar zsidebar_" . $sidebar->sidebar_base_name . " \" data-content=\"" . $sidebar->sidebar_base_name . "\">\n <div id=\"" . $sidebar->sidebar_base_name . "_sortable\" class=\"zsidebar-content\">"; /** * @var CoreWidgetValues[] $widget_available_in_sidebar */ $widget_available_in_sidebar = CoreWidgetValues::find(['conditions' => 'theme_name = ?1 AND sidebar_base_name = ?2 AND published = 1', 'bind' => [1 => $defaultTemplate, 2 => $sidebar->sidebar_base_name], 'order' => 'ordering ASC']); foreach ($widget_available_in_sidebar as $widget_available) { if (class_exists($widget_available->class_name)) { $widget_object = new $widget_available->class_name($widget_available->widget_value_id); if (method_exists($widget_object, "getForm")) { $sidebar_html .= $widget_object->getForm(); } } } $sidebar_html .= '</div></div>' . $panelAfter; if ($index == $half - 1) { $sidebar_html .= '</div><div class="col-sm-6 col-sidebar-last no-padding-lr" style="padding-left: 7px">'; } if ($index == $totalSidebar - 1) { $sidebar_html .= '</div>'; } } //Set view sidebar_html $this->view->setVar("sidebar_html", $sidebar_html); $this->view->setVar('sortable', implode(', ', $sortable)); $this->assets->collection("css_header")->addJs('/templates/backend/' . $this->_defaultTemplate . "/zcms/css/sidebar-widget.css"); }