/
home
/
assocoweys
/
avexp
/
plugins
/
content
/
relatednewsinarticle
/
Upload File
HOME
<?php /** * @copyright Copyright (C) 2011 Simplify Your Web, Inc. All rights reserved. * @license GNU General Public License version 3 or later; see LICENSE.txt */ // no direct access defined( '_JEXEC' ) or die; use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\Helper\ModuleHelper; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Registry\Registry; class plgContentRelatedNewsInArticle extends CMSPlugin { protected $autoloadLanguage = true; protected $app; function onContentPrepareForm($form, $data) { if (!$this->app) { $this->app = Factory::getApplication(); } $option = $this->app->input->get('option'); if ($option == 'com_modules' || $option == 'com_advancedmodules') { if (!in_array($form->getName(), array('com_modules.module', 'com_advancedmodules.module'))) { return true; } // If we are on the save command, no data is passed to $data variable, we need to get it directly from request $jformData = $this->app->input->get('jform', [], 'array'); if ($jformData && !$data) { $data = $jformData; } if (is_array($data)) { $data = (object) $data; } if ($data->module == 'mod_latestnewsenhancedextended' && $data->position == 'inside-article') { Form::addFormPath(dirname(__FILE__).'/forms'); $form->loadFile('module', false); } } return true; } public function onContentPrepare($context, &$row, &$params, $page = 0) { if (!$this->app) { $this->app = Factory::getApplication(); } $view = $this->app->input->get('view'); $canProceed = (($context == 'com_content.article' && $view == 'article') || ($context == 'com_k2.item' && $view == 'item')); if (!$canProceed) { return; } if (!isset($row->text)) { // does it exist in previous versions of k2 (prior to v2.6.8) ? return; } $position = 'inside-article'; // get the modules and their locations $modules_to_use = array(); $locations = array(); $fallbacks = array(); // use it to avoid checking params again later $modules = ModuleHelper::getModules($position); foreach ($modules as $module) { if ($module->module == 'mod_latestnewsenhancedextended') { // get module parameters $module_params = new Registry(); $module_params->loadString($module->params); if (($view == 'article' && $module_params->get('datasource') == 'articles') || ($view == 'item' && $module_params->get('datasource') == 'k2')) { $modules_to_use[] = $module; // get module location inside the article $location = $module_params->get('location_inside', 'paragraph'); if ($location == 'paragraph') { $location = $module_params->get('after_paragraph', 1); if ($location < 1) { $location = 1; } } $locations[$module->id] = $location; $fallbacks[$module->id] = $module_params->get('after_paragraph_fallback', 0); } } } if (empty($modules_to_use)) { return; } // iterate through each paragraph $delimiter = '</p>'; $renderer = Factory::getDocument()->loadRenderer('module'); // the output of the articles and the K2 items are handled differently $tmp_output_before = ''; // location 'before' foreach ($modules_to_use as $key => $module) { if ($locations[$module->id] == 'before') { $module_output = trim($renderer->render($module, array('style' => $module->style))); if ($module_output) { $tmp_output_before .= $module_output; } unset($modules_to_use[$key]); } } //if ($view == 'article') { // Articles: look through $row->text // location 'after paragraph' $paragraphs = array_map('trim', explode($delimiter, $row->text)); // array_map trims the strings $paragraphs = array_filter($paragraphs, 'strlen'); // array_filter eliminates empty strings if (!empty($paragraphs)) { foreach ($paragraphs as $index => $paragraph) { if ($paragraph) { $paragraphs[$index] .= $delimiter; } foreach ($modules_to_use as $key => $module) { if ($locations[$module->id] == $index + 1) { $module_output = trim($renderer->render($module, array('style' => $module->style))); if ($module_output) { $paragraphs[$index] .= $module_output; } unset($modules_to_use[$key]); } } } $row->text = $tmp_output_before.implode('', $paragraphs); } else { $row->text = $tmp_output_before.$row->text; } // location 'after paragraph' for missed location and fallback foreach ($modules_to_use as $key => $module) { // TODO would be nice to order the modules by the location (all but 'after') if ($locations[$module->id] != 'after' && $fallbacks[$module->id]) { $module_output = trim($renderer->render($module, array('style' => $module->style))); if ($module_output) { $row->text .= $module_output; } unset($modules_to_use[$key]); } } // location 'after' foreach ($modules_to_use as $module) { if ($locations[$module->id] == 'after') { $module_output = trim($renderer->render($module, array('style' => $module->style))); if ($module_output) { $row->text .= $module_output; } } } // } else { // // K2: look through $row->introtext then $row->fulltext // // intro and full text are handled separately in the output // // location 'after paragraph' // // intro text // // TODO if intro text is set to 'hide', prepend to full text // // TODO full text can also be set to 'hide' // // (check in $params) // $paragraphs_intro = array_map('trim', explode($delimiter, $row->introtext)); // array_map trims the strings // $paragraphs_intro = array_filter($paragraphs_intro, 'strlen'); // array_filter eliminates empty strings // if (!empty($paragraphs_intro)) { // foreach ($paragraphs_intro as $index => $paragraph) { // if ($paragraph) { // $paragraphs_intro[$index] .= $delimiter; // } // foreach ($modules_to_use as $key => $module) { // if ($locations[$module->id] == $index + 1) { // $module_output = trim($renderer->render($module, array('style' => $module->style))); // if ($module_output) { // $paragraphs_intro[$index] .= $module_output; // } // unset($modules_to_use[$key]); // } // } // } // $row->introtext = $tmp_output_before.implode('', $paragraphs_intro); // } else { // $row->introtext = $tmp_output_before.$row->introtext; // } // // full text // $paragraphs_full = array_map('trim', explode($delimiter, $row->fulltext)); // array_map trims the strings // $paragraphs_full = array_filter($paragraphs_full, 'strlen'); // array_filter eliminates empty strings // if (!empty($paragraphs_full)) { // foreach ($paragraphs_full as $index => $paragraph) { // if ($paragraph) { // $paragraphs_full[$index] .= $delimiter; // } // foreach ($modules_to_use as $key => $module) { // if ($locations[$module->id] == $index + 1 + count($paragraphs_intro)) { // $module_output = trim($renderer->render($module, array('style' => $module->style))); // if ($module_output) { // $paragraphs_full[$index] .= $module_output; // } // unset($modules_to_use[$key]); // } // } // } // $row->fulltext = implode('', $paragraphs_full); // } // // location 'after paragraph' for missed location and fallback // foreach ($modules_to_use as $key => $module) { // // TODO would be nice to order the modules by the location (all but 'after') // if ($locations[$module->id] != 'after' && $fallbacks[$module->id]) { // $module_output = trim($renderer->render($module, array('style' => $module->style))); // if ($module_output) { // $row->fulltext .= $module_output; // } // unset($modules_to_use[$key]); // } // } // // location 'after' // foreach ($modules_to_use as $module) { // if ($locations[$module->id] == 'after') { // $module_output = trim($renderer->render($module, array('style' => $module->style))); // if ($module_output) { // $row->fulltext .= $module_output; // } // } // } // } } } ?>