public function initialize(array $options) { parent::initialize($options); /* * Prepare some services for later access */ $this->user = $this->serviceContainer->getService('user'); $this->i18n = $this->serviceContainer->getService('i18n'); $this->widgetTypeManager = $this->serviceContainer->getService('widget_type_manager'); $this->moduleManager = $this->serviceContainer->getService('module_manager'); }
public function renderWidget(array $widget) { //it the widget is called programmatically, it has no id and can not be edited if (!isset($widget['id'])) { return parent::renderWidget($widget); } list($widgetWrapClass, $widgetInnerClass) = $this->getWidgetContainerClasses($widget); /* * Open widget wrap with wrapped user's classes */ $html = '<div class="' . $widgetWrapClass . '" id="dm_widget_' . $widget['id'] . '">'; /* * Add edit button if required */ if ($this->user->can('widget_edit')) { try { $widgetPublicName = $this->serviceContainer->getService('widget_type_manager')->getWidgetType($widget)->getPublicName(); } catch (Exception $e) { $widgetPublicName = $widget['module'] . '.' . $widget['action']; } $title = $this->i18n->__('Edit this %1%', array('%1%' => $this->i18n->__($widgetPublicName))); $html .= '<a class="dm dm_widget_edit" title="' . $title . '"></a>'; } /* * Add fast record edit button if required */ if ('show' === $widget['action'] && $this->user->can('widget_edit_fast') && $this->user->can('widget_edit_fast_record')) { if ($module = $this->moduleManager->getModuleOrNull($widget['module'])) { if ($module->hasModel()) { $html .= sprintf('<a class="dm dm_widget_record_edit" title="%s"></a>', $this->i18n->__('Edit this %1%', array('%1%' => $this->i18n->__($module->getName()))), $widget['id']); } } } elseif (!$this->user->can('widget_edit') && $this->user->can('widget_edit_fast')) { $fastEditPermission = 'widget_edit_fast_' . dmString::underscore(str_replace('dmWidget', '', $widget['module'])) . '_' . $widget['action']; if ($this->user->can($fastEditPermission)) { try { $widgetPublicName = $this->serviceContainer->getService('widget_type_manager')->getWidgetType($widget)->getPublicName(); } catch (Exception $e) { $widgetPublicName = $widget['module'] . '.' . $widget['action']; } $html .= sprintf('<a class="dm dm_widget_fast_edit" title="%s"></a>', $this->i18n->__('Edit this %1%', array('%1%' => $this->i18n->__(dmString::lcfirst($widgetPublicName)))), $widget['id']); } } /* * Open widget inner with user's classes */ $html .= '<div class="' . $widgetInnerClass . '">'; /* * get widget inner content */ $html .= $this->renderWidgetInner($widget); /* * Close widget inner */ $html .= '</div>'; /* * Close widget wrap */ $html .= '</div>'; return $html; }