getEmpty(); $config->fields = array_merge($config->fields, Config::getConfigurationValues('butterfly')); self::$_cache = $config->fields; } if (isset(self::$_cache[$name]) && self::$_cache[$name] !== '') { return self::$_cache[$name]; } return $defaultValue; } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { switch (get_class($item)) { case 'Config': return [1 => t_butterfly('Butterfly')]; default: return ''; } } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { switch (get_class($item)) { case 'Config': $config = new self(); $config->showFormDisplay(); break; } return true; } protected static function checkPicture($name, $input, $old, $width = 0, $height = 0, $max_size = 500) { $blank = "_blank_$name"; $new = "_$name"; if (isset($input[$blank]) && $input[$blank]) { unset($input[$blank]); if (isset($old[$name]) && $old[$name]) { PluginButterflyToolbox::deletePicture($old[$name]); } $input[$name] = ''; } else if (isset($input[$new]) && !empty($input[$new]) && !empty($input[$new][0])) { $picName = array_shift($input[$new]); // Use GLPI temp directory if available, fallback to system temp $glpiTmpConstant = 'GLPI_TMP_DIR'; $tmpDir = (defined($glpiTmpConstant) && constant($glpiTmpConstant)) ? constant($glpiTmpConstant) : sys_get_temp_dir(); $picPath = $tmpDir . '/' . $picName; $picResizedPath = $tmpDir . '/resized_' . $picName; if ($width || $height) { if (PluginButterflyToolbox::resizePicture($picPath, $picResizedPath, $width, $height, 0, 0, 0, 0, $max_size)) { $picPath = $picResizedPath; } } if ($dest = PluginButterflyToolbox::savePicture($picPath)) { $input[$name] = $dest; } else { Session::addMessageAfterRedirect(__('Unable to save picture file.'), true, ERROR); } if (isset($old[$name]) && $old[$name]) { PluginButterflyToolbox::deletePicture($old[$name]); } } unset($input["_$name"]); unset($input["_prefix_$name"]); unset($input["_prefix_new_$name"]); unset($input["_tag_$name"]); unset($input["_tag_new_$name"]); unset($input["_uploader_$name"]); unset($input["new_$name"]); unset($input[$blank]); unset($input[$new]); return $input; } protected static function checkCSS($name, $label, $input, $old) { $fullName = "{$name}_css_custom"; $type = "{$name}_css_type"; if (!isset($input[$type])) { $input[$type] = 'css'; } if (isset($input[$fullName])) { $input[$fullName] = html_entity_decode($input[$fullName]); $input[$fullName] = preg_replace('/\\\\r\\\\n/', "\n", $input[$fullName]); $input[$fullName] = preg_replace('/\\\\n/', "\n", $input[$fullName]); if ($input[$type] === 'scss' && PluginButterflyScss::hasScssSuport()) { try { $variables = []; $variables['butterfly_timestamp'] = PluginButterflyToolbox::getTimestamp(); PluginButterflyScss::compileScss($input[$fullName], $variables); } catch (\Throwable $th) { $message = sprintf(t_butterfly('Unable to compile the SCSS (%1$s). Message: '), $label); Session::addMessageAfterRedirect($message . $th->getMessage(), true, ERROR); } } } return $input; } static function configUpdate($input) { $old = Config::getConfigurationValues('butterfly'); unset($input['_no_history']); $input = self::checkPicture('favicon_picture', $input, $old, 192, 192, 192); $input = self::checkPicture('login_picture', $input, $old, 145, 80, 300); $input = self::checkPicture('internal_picture', $input, $old, 100, 55, 300); $input = self::checkPicture('login_background_picture', $input, $old); $input = self::checkCSS('login', t_butterfly('Login Page'), $input, $old); $input = self::checkCSS('internal', t_butterfly('Internal Page'), $input, $old); foreach ($input as $key => $value) { if ($value && strpos($key, '_blank_') === 0) { $name = substr($key, 7); $input[$name] = ''; } } $input['timestamp'] = time(); PluginButterflyToolbox::setTimestamp($input['timestamp']); Session::addMessageAfterRedirect(__('Item successfully updated'), false, INFO); return $input; } function getEmpty() { $defaultCss = 'css'; if (PluginButterflyScss::hasScssSuport()) { $defaultCss = 'scss'; } $this->fields = [ 'favicon_picture' => '', 'page_title' => '', 'page_footer_display' => 'original', 'page_footer_text' => '', 'login_picture' => '', 'login_picture_max_width' => '240px', 'login_picture_max_height' => '130px', 'login_css_custom' => '', 'login_css_type' => $defaultCss, 'login_theme' => '', 'internal_picture' => '', 'internal_picture_width' => '100px', 'internal_picture_height' => '55px', 'internal_css_custom' => '', 'internal_css_type' => $defaultCss, ]; } protected function buildPictureLine($name, $recommendedSize = null) { if (!empty($this->fields[$name])) { echo ''; echo Html::image(PluginButterflyToolbox::getPictureUrl($this->fields[$name]), [ 'style' => ' max-width: 100px; max-height: 100px; background-image: linear-gradient(45deg, #b0b0b0 25%, transparent 25%), linear-gradient(-45deg, #b0b0b0 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #b0b0b0 75%), linear-gradient(-45deg, transparent 75%, #b0b0b0 75%); background-size: 10px 10px; background-position: 0 0, 0 5px, 5px -5px, -5px 0px;', 'class' => 'picture_square' ]); echo " "; echo Html::getCheckbox([ 'title' => t_butterfly('Reset'), 'name' => "_blank_$name" ]); echo " " . t_butterfly('Reset'); echo ''; echo ''; } else { echo ''; } Html::file([ 'name' => $name, 'onlyimages' => true, 'min_file_size' => 1, ]); if ($recommendedSize) { echo ''; echo sprintf(t_butterfly('Recommended size: %1$s'), $recommendedSize); echo ''; } echo ''; } protected function buildCssLine($name, $label) { $fullName = "{$name}_css_custom"; $type = "{$name}_css_type"; echo "" . sprintf(t_butterfly('Custom CSS for %1$s'), $label) . ""; echo ""; echo ""; echo t_butterfly('CSS Type') . ':'; echo ""; echo ""; $css_type = [ 'off' => __('Disabled'), 'css' => 'CSS', ]; if (PluginButterflyScss::hasScssSuport()) { $css_type['scss'] = 'SCSS'; } Dropdown::showFromArray($type, $css_type, ['value' => $this->fields[$type]]); echo ""; echo ""; echo ""; echo ""; $rand = mt_rand(); echo sprintf( ''; echo Html::scriptBlock(' $(function() { var textarea = document.getElementById("' . $fullName . '_' . $rand . '"); var editorCode = CodeMirror.fromTextArea(textarea, { mode: "text/x-scss", lineNumbers: true, viewportMargin: Infinity, extraKeys: { "Ctrl-Space": "autocomplete" }, foldGutter: true, gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"] }); $("#' . $fullName . '_' . $rand . '").data("CodeMirrorInstance", editorCode); // Fix bad display of gutter (see https://github.com/codemirror/CodeMirror/issues/3098 ) setTimeout(function () {editorCode.refresh();}, ' . (500 + self::$_i++ * 100) . '); }); '); echo ""; echo "\n"; } /** * Print the config form for display * * @return Nothing (display) * */ function showFormDisplay() { global $CFG_GLPI; if (!Config::canView()) { return false; } $this->getEmpty(); $this->fields = array_merge($this->fields, Config::getConfigurationValues('butterfly')); $canedit = Session::haveRight(Config::$rightname, UPDATE); if ($canedit) { echo "
"; } echo Html::hidden('config_context', ['value' => 'butterfly']); echo Html::hidden('config_class', ['value' => __CLASS__]); echo "
"; $rand = mt_rand(); echo ""; // echo "
"; // echo ""; // General echo "
"; echo ""; echo ""; echo ""; echo ""; $this->buildPictureLine('favicon_picture', '192px x 192px'); echo "\n"; echo ""; echo ""; echo ""; echo "\n"; echo ""; echo ""; echo ""; echo "\n"; echo ""; echo ""; echo "\n"; echo "
" . t_butterfly('Favicon and Title') . "
" . __('Picture') . "
" . __('Title') . ""; echo sprintf( '', Html::parseAttributes([ 'name' => 'page_title', 'value' => $this->fields['page_title'], 'style' => 'width: 98%', ]) ); echo "
" . t_butterfly('Footer') . ""; Dropdown::showFromArray('page_footer_display', [ 'original' => __('Original'), 'hide' => t_butterfly('Hide'), 'custom' => t_butterfly('Custom'), ], ['value' => $this->fields['page_footer_display']]); echo "
" . t_butterfly('Footer text') . ""; Html::textarea([ 'rand' => $rand, 'editor_id' => 'text' . $rand, 'name' => 'page_footer_text', 'value' => $this->fields['page_footer_text'], 'style' => 'width: 98%', 'rows' => 1, 'enable_richtext' => true, 'enable_fileupload' => false, 'enable_images' => false, ]); ?> "; echo "
"; echo "
"; // Login echo "
"; echo ""; echo ""; echo ""; echo ""; echo "\n"; $themeInfo = PluginButterflyTheme::getThemeInfo($this->fields['login_theme']); if ($themeInfo && isset($themeInfo['variables'])) { foreach ($themeInfo['variables'] as $k => $v) { $themeId = $themeInfo['id']; $fieldName = "login_theme-$themeId-$k"; $fieldValue = $v['default']; if (isset($this->fields[$fieldName]) && $this->fields[$fieldName]) { $fieldValue = $this->fields[$fieldName]; } echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; } } echo ""; echo ""; $this->buildPictureLine('login_picture', '145px x 80px'); echo "\n"; echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; echo ""; echo ""; $this->buildPictureLine('login_background_picture', '1920px x 1080px'); echo "\n"; // Custom CSS Login $this->buildCssLine('login', t_butterfly('Login Page')); echo "
" . t_butterfly('Login Page') . "
" . t_butterfly('Theme') . ""; echo sprintf( ''; ?> "; echo ""; echo '' . __('Preview') . ''; ?> "; echo "
" . t_butterfly($v['name']) . ""; Html::showColorField($fieldName, [ 'value' => $fieldValue ]); echo " "; echo Html::getCheckbox([ 'title' => t_butterfly('Reset'), 'name' => "_blank_$fieldName" ]); echo " " . t_butterfly('Reset'); echo "
" . __('Picture') . "
" . __('Width') . ""; echo sprintf( '', Html::parseAttributes([ 'name' => 'login_picture_max_width', 'value' => $this->fields['login_picture_max_width'], ]) ); echo "" . __('Height') . ""; echo sprintf( '', Html::parseAttributes([ 'name' => 'login_picture_max_height', 'value' => $this->fields['login_picture_max_height'], ]) ); echo "
" . t_butterfly('Background picture') . "
"; echo "
"; // Internal Page echo "
"; echo ""; echo ""; echo ""; echo ""; $this->buildPictureLine('internal_picture', '100px x 55px'); echo "\n"; echo ""; echo ""; echo ""; echo ""; echo ""; echo "\n"; // Custom CSS Internal $this->buildCssLine('internal', t_butterfly('Internal Page')); echo "
" . t_butterfly('Internal Page') . "
" . __('Picture') . "
" . __('Width') . ""; echo sprintf( '', Html::parseAttributes([ 'name' => 'internal_picture_width', 'value' => $this->fields['internal_picture_width'], ]) ); echo "" . __('Height') . ""; echo sprintf( '', Html::parseAttributes([ 'name' => 'internal_picture_height', 'value' => $this->fields['internal_picture_height'], ]) ); echo "
"; echo "
"; // echo "
"; echo Html::scriptBlock("$('#tabs$rand').tabs({ activate: function(event, ui) { localStorage['butterfly_last_tab'] = $('#tabs$rand').tabs('option', 'active'); var editor = ui.newPanel.find('.butterfly-codemirror').data('CodeMirrorInstance'); if (editor) { editor.refresh(); } }, active: localStorage['butterfly_last_tab'] });"); if ($canedit) { echo ""; echo ""; echo ""; echo "
"; echo ""; echo "
"; } Html::closeForm(); } }