* @copyright Copyright © 2019 Delo Design & NorrNext. All rights reserved. * @license GNU General Public License version 3 or later; see license.txt * @link https://www.norrnext.com */ defined('_JEXEC') or die; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\Layout\FileLayout; use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Session\Session; class PlgButtonQuantummanagerbutton extends CMSPlugin { /** * Application object * * @var CMSApplication * @since 1.0.0 */ protected $app; /** * Load the language file on instantiation. * * @var boolean * * @since 1.1.0 */ protected $autoloadLanguage = true; protected $install_quantummanager = false; public function __construct(&$subject, $config = array()) { parent::__construct($subject, $config); if(file_exists(JPATH_SITE . '/administrator/components/com_quantummanager/quantummanager.php')) { $this->install_quantummanager = true; } } /** * Display the button. * * @param string $name The name of the button to add. * * @return CMSObject The button options as CMSObject. * * @throws Exception * * @since 1.1.0 */ public function onDisplay($name, $asset, $author) { if(!$this->install_quantummanager) { return; } if (!$this->accessCheck()) { return; } JLoader::register('QuantummanagerHelper', JPATH_ROOT . '/administrator/components/com_quantummanager/helpers/quantummanager.php'); $function = 'function(){}'; $isJoomla4 = QuantummanagerHelper::isJoomla4(); $link = 'index.php?option=com_ajax&plugin=quantummanagerbutton&group=editors-xtd&format=html&tmpl=component&plugin.task=getmodal&e_name=' . $name . '&asset=com_content&author=' . Session::getFormToken() . '=1&function=' . $function . '&isjoomla4=' . ($isJoomla4 ? '1' : '0'); $button = new CMSObject(); $button->modal = true; $button->class = 'btn'; $button->link = $link; $button->text = Text::_('PLG_BUTTON_QUANTUMMANAGERBUTTON_BUTTON'); if ($isJoomla4) { $button->name = $this->_type . '_' . $this->_name; $button->icon = 'pictures'; $button->iconSVG = ''; $button->options = [ 'height' => '400px', 'width' => '800px', 'bodyHeight' => '70', 'modalWidth' => '80', 'tinyPath' => $link, 'confirmCallback' => 'Joomla.getImage(Joomla.selectedMediaFile, \'' . $name . '\', this)', ]; return $button; } $button->name = 'file-add'; $button->options = "{handler: 'iframe', size: {x: 1450, y: 700}, classWindow: 'quantummanager-modal-sbox-window'}"; $label = Text::_('PLG_BUTTON_QUANTUMMANAGERBUTTON_BUTTON'); Factory::getDocument()->addStyleDeclaration(<<install_quantummanager) { return; } JLoader::register('QuantummanagerHelper', JPATH_ROOT . '/administrator/components/com_quantummanager/helpers/quantummanager.php'); JLoader::register('QuantummanagerbuttonHelper', JPATH_ROOT . '/plugins/editors-xtd/quantummanagerbutton/helper.php'); $app = Factory::getApplication(); $data = $app->input->getArray(); $task = $app->input->get('plugin_task'); $html = ''; if (!$this->accessCheck()) { return; } if ($task === 'getmodal') { QuantummanagerHelper::loadlang(); $layout = new FileLayout('default', JPATH_ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, [ 'plugins', 'editors-xtd', 'quantummanagerbutton', 'tmpl' ])); echo $layout->render(); } if ($task === 'prepareforcontent') { if (!isset($data['params'], $data['scope'])) { $app->close(); } $scope = $data['scope']; $params = json_decode($data['params'], JSON_OBJECT_AS_ARRAY); $file = QuantummanagerHelper::preparePath($data['path'], false, $scope, true); $name = explode('/', $file); $filename = end($name); $type = explode('.', $file); $filetype = end($type); $filesize = filesize(JPATH_ROOT . '/' . $file); $scopesTemplate = $this->params->get('scopes', QuantummanagerbuttonHelper::defaultValues()); $scopesCustom = $this->params->get('customscopes', []); $variables = []; $variablesParams = []; $html = ''; $shortCode = false; $template = '{name}'; if(is_array($scopesCustom)) { $scopesCustom = []; } foreach ($scopesCustom as $scopeCustom) { $nameTmp = 'scopes' . count($scopesTemplate); $scopesTemplate->$nameTmp = $scopeCustom; } foreach ($scopesTemplate as $scopesTemplateCurrent) { $scopesTemplateCurrent = (object) $scopesTemplateCurrent; if ($scopesTemplateCurrent->id === $scope) { if (empty($scopesTemplateCurrent->templatelist)) { foreach ($params['files'] as $item) { $file = QuantummanagerHelper::preparePath($data['path'], false, $scope, true) . DIRECTORY_SEPARATOR . $item['file']; $name = explode('/', $file); $filename = end($name); $type = explode('.', $file); $filetype = mb_strtolower(end($type)); $filesize = filesize(JPATH_ROOT . '/' . $file); $variables = [ '{file}' => $file, '{filename}' => $filename, '{type}' => $filetype, '{size}' => QuantummanagerHelper::formatFileSize($filesize), ]; if (file_exists(JPATH_ROOT . DIRECTORY_SEPARATOR . $file)) { if (in_array($filetype, ['jpg', 'jpeg', 'png'])) { list($width, $height, $type, $attr) = getimagesize(JPATH_ROOT . DIRECTORY_SEPARATOR . $file); $variables['{imagewidth}'] = $width; $variables['{imageheight}'] = $height; } } foreach ($item['fields'] as $key => $value) { if (preg_match("#^\{.*?\}$#isu", $key)) { $variables[$key] = trim($value); } } $template = '{name}'; $variablesFind = []; $variablesReplace = []; foreach ($variables as $key => $value) { $variablesFind[] = $key; $variablesReplace[] = $value; } $template = str_replace($variablesFind, $variablesReplace, $template); $html .= preg_replace("#[\s\040]?[a-zA-Z0-9]{1,}\=\"\"#isu", '', $template); } } else { foreach ($scopesTemplateCurrent->templatelist as $templateList) { $templateList = (object) $templateList; if (isset($params['template']) && $templateList->templatename === $params['template']) { //собираем по выбранному шаблону $templatebefore = ''; $templateitems = ''; $templateafter = ''; $shortCode = false; if (preg_match("#^\{\{.*?\}\}$#isu", trim($templateList->templatebefore))) { $templatebefore = '[before]' . $templateList->templatebefore . '[/before]'; $shortCode = true; } else { $templatebefore = $templateList->templatebefore; } $variablesForTemplate = []; foreach ($params['files'] as $item) { $file = QuantummanagerHelper::preparePath($data['path'], false, $scope, true) . DIRECTORY_SEPARATOR . $item['file']; $name = explode('/', $file); $filename = end($name); $type = explode('.', $file); $filetype = end($type); $filesize = filesize(JPATH_ROOT . '/' . $file); $variables = [ '{file}' => $file, '{filename}' => $filename, '{type}' => $filetype, '{size}' => QuantummanagerHelper::formatFileSize($filesize), ]; if (file_exists(JPATH_ROOT . DIRECTORY_SEPARATOR . $file)) { if (in_array($filetype, ['jpg', 'jpeg', 'png'])) { list($width, $height, $type, $attr) = getimagesize(JPATH_ROOT . DIRECTORY_SEPARATOR . $file); $variables['{imagewidth}'] = $width; $variables['{imageheight}'] = $height; } } foreach ($item['fields'] as $key => $value) { if (preg_match("#^\{.*?\}$#isu", $key)) { $variables[$key] = trim($value); } } $variablesFind = []; $variablesReplace = []; foreach ($variables as $key => $value) { $variablesFind[] = $key; $variablesReplace[] = $value; } foreach ($variables as $key => $value) { $variables[$key] = str_replace($variablesFind, $variablesReplace, $value); } $variablesFind = []; $variablesReplace = []; foreach ($variables as $key => $value) { $variablesFind[] = $key; $variablesReplace[] = $value; } if (preg_match("#^\{\{.*?\}\}$#isu", trim($templateList->template)) || $shortCode) { $shortCode = true; $variablesForTemplate[] = $variables; } else { $item = str_replace($variablesFind, $variablesReplace, $templateList->template); $item = preg_replace("#[\s\040]?[a-zA-Z0-9]{1,}\=\"\"#isu", '', $item); $templateitems .= $item; } } if ($shortCode) { $templateitems = '[item][variables]' . json_encode($variablesForTemplate) . '[/variables][template]' . $templateList->template . '[/template][/item]'; } if (preg_match("#^\{\{.*?\}\}$#isu", trim($templateList->templateafter))) { $templateafter = '[after]' . $templateList->templateafter . '[/after]'; $shortCode = true; } else { $templateafter = $templateList->templateafter; } if ($shortCode) { $html = '[qmcontent]' . $templatebefore . $templateitems . $templateafter . '[/qmcontent]'; } else { $html = $templatebefore . $templateitems . $templateafter; } } } } } } echo $html; $app->close(); } } protected function accessCheck() { if ($this->app->isClient('administrator')) { return true; } // проверяем на включенность параметра JLoader::register('QuantummanagerHelper', JPATH_ADMINISTRATOR . '/components/com_quantummanager/helpers/quantummanager.php'); if (!(int) QuantummanagerHelper::getParamsComponentValue('front', 0)) { return false; } // проверяем что пользователь авторизован if (Factory::getUser()->id === 0) { return false; } $actions = QuantummanagerHelper::getActions(); if (!$actions->get('core.create')) { return false; } return true; } }