From 2dba434dcbe7a939cb5c681cfd8e9bde166a623a Mon Sep 17 00:00:00 2001 From: JLTRY Date: Sat, 9 Nov 2024 21:01:45 +0100 Subject: [PATCH 1/6] Add plugin attachmentsid {attachmentsid id=xx,yy} where xx,yy = id of the attachment correct copyright do not use require_once inherit from PlgAttachmentsFramework --- .../src/Controller/AttachmentsController.php | 12 ++- .../site/src/Model/AttachmentsModel.php | 18 +++- .../src/AttachmentsPluginManager.php | 2 +- attachmentsid/attachmentsid.php | 98 +++++++++++++++++++ attachmentsid/attachmentsid.xml | 15 +++ 5 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 attachmentsid/attachmentsid.php create mode 100644 attachmentsid/attachmentsid.xml diff --git a/attachments_component/site/src/Controller/AttachmentsController.php b/attachments_component/site/src/Controller/AttachmentsController.php index f1eabee1..b40e8d24 100644 --- a/attachments_component/site/src/Controller/AttachmentsController.php +++ b/attachments_component/site/src/Controller/AttachmentsController.php @@ -21,6 +21,7 @@ use Joomla\CMS\Router\Route; use Joomla\CMS\Uri\Uri; use Joomla\Input\Input; +use JMCameron\Component\Attachments\Site\Model\AttachmentsModel; defined('_JEXEC') or die('Restricted access'); @@ -79,7 +80,7 @@ public function display($cachable = false, $urlparams = false) */ public function displayString($parent_id, $parent_type, $parent_entity, $title=null, $show_file_links=true, $allow_edit=true, - $echo=true, $from=null) + $echo=true, $from=null, $attachmentid=null) { $app = $this->app; $document = $app->getDocument(); @@ -89,9 +90,12 @@ public function displayString($parent_id, $parent_type, $parent_entity, $model = $this->getModel('Attachments', 'Site'); if ( !$model ) { $errmsg = Text::_('ATTACH_ERROR_UNABLE_TO_FIND_MODEL') . ' (ERR 60)'; - throw new \Exception($errmsg, 500); + //throw new \Exception($errmsg, 500); + return; } - + if ($attachmentsid) { + $parent_id = '?'; + } $model->setParentId($parent_id, $parent_type, $parent_entity); // Get the component parameters @@ -102,7 +106,7 @@ public function displayString($parent_id, $parent_type, $parent_entity, $model->setSortOrder($sort_order); // If none of the attachments should be visible, exit now - if ( ! $model->someVisible() ) { + if ( ! $model->someVisible($attachmentid) ) { return false; } diff --git a/attachments_component/site/src/Model/AttachmentsModel.php b/attachments_component/site/src/Model/AttachmentsModel.php index 6b5ba26b..6a1a8fc2 100644 --- a/attachments_component/site/src/Model/AttachmentsModel.php +++ b/attachments_component/site/src/Model/AttachmentsModel.php @@ -333,7 +333,7 @@ public function setSortOrder($new_sort_order) * * @return the list of attachments for this parent */ - public function &getAttachmentsList() + public function &getAttachmentsList($attachmentid=null) { // Just return it if it has already been created if ( $this->_list != null ) { @@ -349,6 +349,10 @@ public function &getAttachmentsList() $parent_id = $this->getParentId(); $parent_type = $this->getParentType(); $parent_entity = $this->getParentEntity(); + + if ($attachmentid) { + $parent_id = '%'; + } // Use parent entity corresponding to values saved in the attachments table $parent = $this->getParentClass(); @@ -387,7 +391,9 @@ public function &getAttachmentsList() $query = $db->getQuery(true); $query->select('a.*, u.name as creator_name')->from('#__attachments AS a'); $query->leftJoin('#__users AS u ON u.id = a.created_by'); - + if ($attachmentid != null) { + $query->where('a.id in (' . $attachmentid . ')' ); + } if ( $parent_id == 0 ) { // If the parent ID is zero, the parent is being created so we have // do the query differently @@ -395,7 +401,9 @@ public function &getAttachmentsList() $query->where('a.parent_id IS NULL AND u.id=' . (int)$user_id); } else { - $query->where('a.parent_id='.(int)$parent_id); + if ($parent_id != '%') { + $query->where('a.parent_id LIKE "'.(int)$parent_id . '"'); + } // Handle the state part of the query if ( $user->authorise('core.edit.state', 'com_attachments') ) { @@ -497,7 +505,7 @@ public function numAttachments() * * @return true if there are attachments and some should be visible */ - public function someVisible() + public function someVisible($attachmentid=null) { // See if the attachments list has been loaded if ( $this->_list == null ) { @@ -508,7 +516,7 @@ public function someVisible() } // Since the attachments have not been loaded, load them now - $this->getAttachmentsList(); + $this->getAttachmentsList($attachmentid); } return $this->_some_visible; diff --git a/attachments_plugin_framework/src/AttachmentsPluginManager.php b/attachments_plugin_framework/src/AttachmentsPluginManager.php index 4e0b9d20..6bf2636e 100644 --- a/attachments_plugin_framework/src/AttachmentsPluginManager.php +++ b/attachments_plugin_framework/src/AttachmentsPluginManager.php @@ -209,7 +209,7 @@ public function getAttachmentsPlugin($parent_type) // Make sure the parent type is valid if (!in_array($parent_type, $this->parent_types)) { - $errmsg = Text::sprintf('ATTACH_ERROR_UNKNOWN_PARENT_TYPE_S', $parent_type) . ' (ERR 303)'; + $errmsg = Text::sprintf('ATTACH_ERROR_UNKNOWN_PARENT_TYPE_S', $parent_type) . $parent_type . ' (ERR 303)'; throw new \Exception($errmsg, 406); } diff --git a/attachmentsid/attachmentsid.php b/attachmentsid/attachmentsid.php new file mode 100644 index 00000000..527cb461 --- /dev/null +++ b/attachmentsid/attachmentsid.php @@ -0,0 +1,98 @@ + 'onContentPrepare' + ]; + } + + public function onContentPrepare(Event $event) + { + if (version_compare(JVERSION, '5', 'lt')) { + [$context, $row, $params, $page] = $event->getArguments(); + } + else { + $context = $event['context']; + $row = $event['subject']; + $params = $event['params']; + } + // Set the parent info from the context + if (strpos($context, '.') === false) + { + // Assume the context is the parent_type + $parent_type = $context; + $parent_entity = ''; + } + else + { + list ($parent_type, $parent_entity) = explode('.', $context, 2); + } + return $this->OnPrepareRow($row, $parent_type, $parent_entity); + } + + public function OnPrepareRow(&$row, $parent_type, $parent_entity) { + static $load = null; + + $matches = array(); + preg_match_all(PF_REGEX_ATTACHIDI_PATTERN, $row->text, $matches); + if(count($matches[0])){ + $oldid = $row->id; + for($i = 0, $total = count($matches[0]); $i < $total; $i++){ + $base = $matches[0][$i]; + $attachment_ids = $matches[1][$i]; + JPluginHelper::importPlugin('attachments'); + $apm = AttachmentsPluginManager::getAttachmentsPluginManager(); + if ( !$apm->attachmentsPluginInstalled($parent_type) ) { + // Exit quietly if there is no Attachments plugin to handle this parent_type + $row->text = "no plugins"; + return false; + } + $parent = $apm->getAttachmentsPlugin($parent_type); + $mvc = Factory::getApplication() + ->bootComponent("com_attachments") + ->getMVCFactory(); + /** @var \JMCameron\Component\Attachments\Site\Controller\AttachmentsController $controller */ + $controller = $mvc->createController('Attachments', 'Site', [], $this->app, $this->app->getInput()); + // parent_id is set arbitrary to 1 + $attachments_list = $controller->displayString(1, $parent_type, 'article', null, true, true, false, 'article', $attachment_ids); + $row->text = str_replace($base, $attachments_list, $row->text); + } + } + } +} \ No newline at end of file diff --git a/attachmentsid/attachmentsid.xml b/attachmentsid/attachmentsid.xml new file mode 100644 index 00000000..06bf0e36 --- /dev/null +++ b/attachmentsid/attachmentsid.xml @@ -0,0 +1,15 @@ + + + plg_content_attachmentsid + 4.0.3 + October 28, 2024 + Jonathan M. Cameron + jmcameron@jmcameron.net + http://joomlacode.org/gf/project/attachments/ + (C) 2007-2018 Jonathan M. Cameron. All rights reserved. + http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL + ATTACH_ATTACHMENTS_PLUGIN_DESCRIPTION + + attachmentsid.php + + From 824bd2fb9fcad4a5483b33fd24c508bc778102ad Mon Sep 17 00:00:00 2001 From: Theofilos Intzoglou Date: Sun, 17 Nov 2024 14:31:37 +0200 Subject: [PATCH 2/6] Handle multiple {attachments} token with id(s) --- .../src/Controller/AttachmentsController.php | 8 +- .../site/src/Model/AttachmentsModel.php | 15 +- .../src/AttachmentsPluginManager.php | 2 +- .../src/PlgAttachmentsFramework.php | 147 ++++++++++-------- 4 files changed, 95 insertions(+), 77 deletions(-) diff --git a/attachments_component/site/src/Controller/AttachmentsController.php b/attachments_component/site/src/Controller/AttachmentsController.php index b40e8d24..43e76d48 100644 --- a/attachments_component/site/src/Controller/AttachmentsController.php +++ b/attachments_component/site/src/Controller/AttachmentsController.php @@ -21,7 +21,6 @@ use Joomla\CMS\Router\Route; use Joomla\CMS\Uri\Uri; use Joomla\Input\Input; -use JMCameron\Component\Attachments\Site\Model\AttachmentsModel; defined('_JEXEC') or die('Restricted access'); @@ -90,12 +89,9 @@ public function displayString($parent_id, $parent_type, $parent_entity, $model = $this->getModel('Attachments', 'Site'); if ( !$model ) { $errmsg = Text::_('ATTACH_ERROR_UNABLE_TO_FIND_MODEL') . ' (ERR 60)'; - //throw new \Exception($errmsg, 500); - return; + throw new \Exception($errmsg, 500); } - if ($attachmentsid) { - $parent_id = '?'; - } + $model->setParentId($parent_id, $parent_type, $parent_entity); // Get the component parameters diff --git a/attachments_component/site/src/Model/AttachmentsModel.php b/attachments_component/site/src/Model/AttachmentsModel.php index 6a1a8fc2..03fc0e51 100644 --- a/attachments_component/site/src/Model/AttachmentsModel.php +++ b/attachments_component/site/src/Model/AttachmentsModel.php @@ -17,6 +17,7 @@ use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; +use Joomla\CMS\Log\Log; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Uri\Uri; @@ -335,6 +336,7 @@ public function setSortOrder($new_sort_order) */ public function &getAttachmentsList($attachmentid=null) { + Log::add("Attachment id: ". join(',', $attachmentid ?? []) . print_r($attachmentid, true)); // Just return it if it has already been created if ( $this->_list != null ) { return $this->_list; @@ -349,10 +351,6 @@ public function &getAttachmentsList($attachmentid=null) $parent_id = $this->getParentId(); $parent_type = $this->getParentType(); $parent_entity = $this->getParentEntity(); - - if ($attachmentid) { - $parent_id = '%'; - } // Use parent entity corresponding to values saved in the attachments table $parent = $this->getParentClass(); @@ -391,8 +389,8 @@ public function &getAttachmentsList($attachmentid=null) $query = $db->getQuery(true); $query->select('a.*, u.name as creator_name')->from('#__attachments AS a'); $query->leftJoin('#__users AS u ON u.id = a.created_by'); - if ($attachmentid != null) { - $query->where('a.id in (' . $attachmentid . ')' ); + if ($attachmentid && join(',', $attachmentid)) { + $query->where('a.id in (' . join(',', $attachmentid) . ')' ); } if ( $parent_id == 0 ) { // If the parent ID is zero, the parent is being created so we have @@ -401,9 +399,7 @@ public function &getAttachmentsList($attachmentid=null) $query->where('a.parent_id IS NULL AND u.id=' . (int)$user_id); } else { - if ($parent_id != '%') { - $query->where('a.parent_id LIKE "'.(int)$parent_id . '"'); - } + $query->where('a.parent_id='.(int)$parent_id); // Handle the state part of the query if ( $user->authorise('core.edit.state', 'com_attachments') ) { @@ -440,6 +436,7 @@ public function &getAttachmentsList($attachmentid=null) $db->setQuery($query); $attachments = $db->loadObjectList(); } catch (\RuntimeException $e) { + Log::add("Query: $query"); $errmsg = $e->getMessage() . ' (ERR 58)'; throw new \Exception($errmsg, 500); } diff --git a/attachments_plugin_framework/src/AttachmentsPluginManager.php b/attachments_plugin_framework/src/AttachmentsPluginManager.php index 6bf2636e..4e0b9d20 100644 --- a/attachments_plugin_framework/src/AttachmentsPluginManager.php +++ b/attachments_plugin_framework/src/AttachmentsPluginManager.php @@ -209,7 +209,7 @@ public function getAttachmentsPlugin($parent_type) // Make sure the parent type is valid if (!in_array($parent_type, $this->parent_types)) { - $errmsg = Text::sprintf('ATTACH_ERROR_UNKNOWN_PARENT_TYPE_S', $parent_type) . $parent_type . ' (ERR 303)'; + $errmsg = Text::sprintf('ATTACH_ERROR_UNKNOWN_PARENT_TYPE_S', $parent_type) . ' (ERR 303)'; throw new \Exception($errmsg, 406); } diff --git a/attachments_plugin_framework/src/PlgAttachmentsFramework.php b/attachments_plugin_framework/src/PlgAttachmentsFramework.php index 3a032ccd..fe0b8e83 100644 --- a/attachments_plugin_framework/src/PlgAttachmentsFramework.php +++ b/attachments_plugin_framework/src/PlgAttachmentsFramework.php @@ -20,6 +20,7 @@ use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; +use Joomla\CMS\Log\Log; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Database\DatabaseDriver; use Joomla\Event\SubscriberInterface; @@ -911,25 +912,31 @@ public function insertAttachmentsList(&$content, $parent_id, $parent_entity) } // Get the attachments tag, if present - $attachments_tag = ''; + $attachments_tag = []; $attachments_tag_args = ''; $match = false; - if (StringHelper::strpos($content->$text_field_name, '{attachments')) + $attachment_id = null; + $offset = -1; + while (false != ($offset = StringHelper::strpos($content->$text_field_name, '{attachments', $offset + 1))) { - if (preg_match('@()?{attachments([ ]*:*[^}]+)?}()?@', $content->$text_field_name, $match)) + if (preg_match('@(?)?{attachments(?[ ]*:*[^}]+)?}(?)?@', substr($content->$text_field_name, $offset), $match)) { - $attachments_tag = true; + $attachments_tag[] = $match[0]; } - if (isset($match[1]) && $match[1]) + var_dump($match); + if (($attachments_placement === "custom") && isset($match["arguments"]) && $match["arguments"]) { - $attachments_tag_args_raw = $match[1]; + $attachments_tag_args_raw = $match["arguments"]; $attachments_tag_args = ltrim($attachments_tag_args_raw, ' :'); - } - if ($attachments_tag) - { - $attachments_tag = $match[0]; + preg_match('/id=([\d,]+)/', $attachments_tag_args, $match_id); + var_dump($match_id); + if ($match_id) { + $attachment_id[] = explode(",", $match_id[1]); + } + } else { + $attachment_id[] = null; } } @@ -942,58 +949,66 @@ public function insertAttachmentsList(&$content, $parent_id, $parent_entity) AttachmentsHelper::setup_upload_directory($attach_dir, $secure); } - // Construct the attachment list (if appropriate) - $html = ''; - $attachments_list = false; - $add_attachment_btn = false; - - // Get the html for the attachments list - /** @var \Joomla\CMS\MVC\Factory\MVCFactory $mvc */ - $mvc = Factory::getApplication() - ->bootComponent("com_attachments") - ->getMVCFactory(); - /** @var \JMCameron\Component\Attachments\Site\Controller\AttachmentsController $controller */ - $controller = $mvc->createController('Attachments', 'Site', [], $this->app, $this->app->getInput()); - $attachments_list = $controller->displayString($parent_id, $this->parent_type, $parent_entity, null, true, true, false, $from); - - // If the attachments list is empty, insert an empty div for it - if ($attachments_list == '') - { - $class_name = $aparams->get('attachments_table_style', 'attachmentsList'); - $div_id = 'attachmentsList' . '_' . $this->parent_type . '_' . $parent_entity . '_' . (string) $parent_id; - $attachments_list = "\n
\n"; - } + $i = 0; + $attachments_html = []; + // Run at least once and for every {attachments} tag + while (isset($attachments_tag[$i]) || $i === 0) { + + // Construct the attachment list (if appropriate) + $html = ''; + $attachments_list = false; + $add_attachment_btn = false; + + // Get the html for the attachments list + /** @var \Joomla\CMS\MVC\Factory\MVCFactory $mvc */ + $mvc = Factory::getApplication() + ->bootComponent("com_attachments") + ->getMVCFactory(); + /** @var \JMCameron\Component\Attachments\Site\Controller\AttachmentsController $controller */ + $controller = $mvc->createController('Attachments', 'Site', [], $this->app, $this->app->getInput()); + $attachments_list = $controller->displayString($parent_id, $this->parent_type, $parent_entity, null, true, true, false, $from, $attachment_id[$i]); + + // If the attachments list is empty, insert an empty div for it + if ($attachments_list == '') + { + $class_name = $aparams->get('attachments_table_style', 'attachmentsList'); + $div_id = 'attachmentsList' . '_' . $this->parent_type . '_' . $parent_entity . '_' . (string) $parent_id; + $attachments_list = "\n
\n"; + } - $html .= $attachments_list; + $html .= $attachments_list; - if ($html || $user_can_add) - { - // Add the style sheet - HTMLHelper::stylesheet('media/com_attachments/css/attachments_list.css'); - HTMLHelper::stylesheet('media/com_attachments/css/attachments_list_dark.css'); + if ($html || $user_can_add) + { + // Add the style sheet + HTMLHelper::stylesheet('media/com_attachments/css/attachments_list.css'); + HTMLHelper::stylesheet('media/com_attachments/css/attachments_list_dark.css'); - // Handle RTL styling (if necessary) - $lang = $this->app->getLanguage(); - if ($lang->isRTL()) + // Handle RTL styling (if necessary) + $lang = $this->app->getLanguage(); + if ($lang->isRTL()) + { + HTMLHelper::stylesheet('media/com_attachments/css/attachments_list_rtl.css'); + } + } + + // Construct the add-attachments button, if appropriate + $hide_add_attachments_link = $aparams->get('hide_add_attachments_link', 0); + if ($user_can_add && !$hide_add_attachments_link) { - HTMLHelper::stylesheet('media/com_attachments/css/attachments_list_rtl.css'); + $add_attachments_btn = AttachmentsHelper::attachmentButtonsHTML($this->parent_type, $parent_id, $parent_entity, $Itemid, $from); + $html .= $add_attachments_btn; } - } - // Construct the add-attachments button, if appropriate - $hide_add_attachments_link = $aparams->get('hide_add_attachments_link', 0); - if ($user_can_add && !$hide_add_attachments_link) - { - $add_attachments_btn = AttachmentsHelper::attachmentButtonsHTML($this->parent_type, $parent_id, $parent_entity, $Itemid, $from); - $html .= $add_attachments_btn; - } + // Wrap both list and the Add Attachments button in another div + if ($html) + { + $html = "
\n" . $html . "\n
"; + } - // Wrap both list and the Add Attachments button in another div - if ($html) - { - $html = "
\n" . $html . "\n
"; + $attachments_html[] = $html; + $i++; } - // Finally, add the attachments // NOTE: Hope str_replace() below is UTF8 safe (since the token being replaced is UTF8)... @@ -1006,11 +1021,14 @@ public function insertAttachmentsList(&$content, $parent_id, $parent_entity) { if ($attachments_tag) { - $content->$text_field_name = $html . $content->$text_field_name; + for ($i=0; $i < count($attachments_tag); $i++) { + $content->$text_field_name = str_replace($attachments_tag[$i], '', $content->$text_field_name); + } + $content->$text_field_name = $attachments_html[0] . $content->$text_field_name; } else { - $content->$text_field_name = $html . str_replace($attachments_tag, '', $content->$text_field_name); + $content->$text_field_name = $attachments_html[0] . $content->$text_field_name; } } break; @@ -1021,12 +1039,14 @@ public function insertAttachmentsList(&$content, $parent_id, $parent_entity) { if ($attachments_tag) { - $content->$text_field_name = str_replace($attachments_tag, $html, $content->$text_field_name); + for ($i=0; $i < count($attachments_tag); $i++) { + $content->$text_field_name = str_replace($attachments_tag[$i], $attachments_html[$i], $content->$text_field_name); + } } else { // If there is no tag, insert the attachments at the end - $content->$text_field_name .= $html; + $content->$text_field_name .= $attachments_html[0]; } } break; @@ -1035,7 +1055,9 @@ public function insertAttachmentsList(&$content, $parent_id, $parent_entity) // Disable and strip out any attachments tags if ($attachments_tag) { - $content->$text_field_name = str_replace($attachments_tag, '', $content->$text_field_name); + for ($i=0; $i < count($attachments_tag); $i++) { + $content->$text_field_name = str_replace($attachments_tag[$i], '', $content->$text_field_name); + } } break; @@ -1045,11 +1067,14 @@ public function insertAttachmentsList(&$content, $parent_id, $parent_entity) { if ($attachments_tag) { - $content->$text_field_name = str_replace($attachments_tag, '', $content->$text_field_name) . $html; + for ($i=0; $i < count($attachments_tag); $i++) { + $content->$text_field_name = str_replace($attachments_tag[$i], '', $content->$text_field_name); + } + $content->$text_field_name = $content->$text_field_name . $attachments_html[0]; } else { - $content->$text_field_name .= $html; + $content->$text_field_name .= $attachments_html[0]; } } break; From 4d6842299a34900c6df480809473f98dda4f70a7 Mon Sep 17 00:00:00 2001 From: JLTRY Date: Sun, 17 Nov 2024 14:45:53 +0100 Subject: [PATCH 3/6] set parent_id for attachamentsid $attachmentid is not a list --- attachments_component/site/src/Model/AttachmentsModel.php | 4 ++-- attachmentsid/attachmentsid.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/attachments_component/site/src/Model/AttachmentsModel.php b/attachments_component/site/src/Model/AttachmentsModel.php index 03fc0e51..77bbc079 100644 --- a/attachments_component/site/src/Model/AttachmentsModel.php +++ b/attachments_component/site/src/Model/AttachmentsModel.php @@ -389,8 +389,8 @@ public function &getAttachmentsList($attachmentid=null) $query = $db->getQuery(true); $query->select('a.*, u.name as creator_name')->from('#__attachments AS a'); $query->leftJoin('#__users AS u ON u.id = a.created_by'); - if ($attachmentid && join(',', $attachmentid)) { - $query->where('a.id in (' . join(',', $attachmentid) . ')' ); + if ($attachmentid) { + $query->where('a.id in (' . $attachmentid . ')' ); } if ( $parent_id == 0 ) { // If the parent ID is zero, the parent is being created so we have diff --git a/attachmentsid/attachmentsid.php b/attachmentsid/attachmentsid.php index 527cb461..2bb49ef7 100644 --- a/attachmentsid/attachmentsid.php +++ b/attachmentsid/attachmentsid.php @@ -63,10 +63,10 @@ public function onContentPrepare(Event $event) { list ($parent_type, $parent_entity) = explode('.', $context, 2); } - return $this->OnPrepareRow($row, $parent_type, $parent_entity); + return $this->OnPrepareRow($row, $parent_type, $parent_entity, $row->id); } - public function OnPrepareRow(&$row, $parent_type, $parent_entity) { + public function OnPrepareRow(&$row, $parent_type, $parent_entity, $parent_id) { static $load = null; $matches = array(); @@ -90,7 +90,7 @@ public function OnPrepareRow(&$row, $parent_type, $parent_entity) { /** @var \JMCameron\Component\Attachments\Site\Controller\AttachmentsController $controller */ $controller = $mvc->createController('Attachments', 'Site', [], $this->app, $this->app->getInput()); // parent_id is set arbitrary to 1 - $attachments_list = $controller->displayString(1, $parent_type, 'article', null, true, true, false, 'article', $attachment_ids); + $attachments_list = $controller->displayString($parent_id, $parent_type, 'article', null, true, true, false, 'article', $attachment_ids); $row->text = str_replace($base, $attachments_list, $row->text); } } From 3b14a011387e17d9c13ba20d5605bdb4a83e48b6 Mon Sep 17 00:00:00 2001 From: JLTRY Date: Mon, 18 Nov 2024 09:02:59 +0100 Subject: [PATCH 4/6] set back $attachmentid to an array remove logs --- attachments_component/site/src/Model/AttachmentsModel.php | 6 ++---- .../src/PlgAttachmentsFramework.php | 2 +- attachmentsid/attachmentsid.php | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/attachments_component/site/src/Model/AttachmentsModel.php b/attachments_component/site/src/Model/AttachmentsModel.php index 77bbc079..1adc6209 100644 --- a/attachments_component/site/src/Model/AttachmentsModel.php +++ b/attachments_component/site/src/Model/AttachmentsModel.php @@ -336,7 +336,6 @@ public function setSortOrder($new_sort_order) */ public function &getAttachmentsList($attachmentid=null) { - Log::add("Attachment id: ". join(',', $attachmentid ?? []) . print_r($attachmentid, true)); // Just return it if it has already been created if ( $this->_list != null ) { return $this->_list; @@ -389,8 +388,8 @@ public function &getAttachmentsList($attachmentid=null) $query = $db->getQuery(true); $query->select('a.*, u.name as creator_name')->from('#__attachments AS a'); $query->leftJoin('#__users AS u ON u.id = a.created_by'); - if ($attachmentid) { - $query->where('a.id in (' . $attachmentid . ')' ); + if ($attachmentid && join(',', $attachmentid)) { + $query->where('a.id in (' . join(',', $attachmentid) . ')' ); } if ( $parent_id == 0 ) { // If the parent ID is zero, the parent is being created so we have @@ -436,7 +435,6 @@ public function &getAttachmentsList($attachmentid=null) $db->setQuery($query); $attachments = $db->loadObjectList(); } catch (\RuntimeException $e) { - Log::add("Query: $query"); $errmsg = $e->getMessage() . ' (ERR 58)'; throw new \Exception($errmsg, 500); } diff --git a/attachments_plugin_framework/src/PlgAttachmentsFramework.php b/attachments_plugin_framework/src/PlgAttachmentsFramework.php index fe0b8e83..e11eb149 100644 --- a/attachments_plugin_framework/src/PlgAttachmentsFramework.php +++ b/attachments_plugin_framework/src/PlgAttachmentsFramework.php @@ -20,7 +20,7 @@ use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; -use Joomla\CMS\Log\Log; + use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Database\DatabaseDriver; use Joomla\Event\SubscriberInterface; diff --git a/attachmentsid/attachmentsid.php b/attachmentsid/attachmentsid.php index 2bb49ef7..e21b71ef 100644 --- a/attachmentsid/attachmentsid.php +++ b/attachmentsid/attachmentsid.php @@ -75,7 +75,7 @@ public function OnPrepareRow(&$row, $parent_type, $parent_entity, $parent_id) { $oldid = $row->id; for($i = 0, $total = count($matches[0]); $i < $total; $i++){ $base = $matches[0][$i]; - $attachment_ids = $matches[1][$i]; + $attachment_ids = explode(',', $matches[1][$i]); JPluginHelper::importPlugin('attachments'); $apm = AttachmentsPluginManager::getAttachmentsPluginManager(); if ( !$apm->attachmentsPluginInstalled($parent_type) ) { From 1e5920b5cc62b8ad3bdc93ea9dc8098422c8e3d4 Mon Sep 17 00:00:00 2001 From: Theofilos Intzoglou Date: Sun, 24 Nov 2024 13:58:47 +0200 Subject: [PATCH 5/6] Not needed any more --- attachmentsid/attachmentsid.php | 98 --------------------------------- attachmentsid/attachmentsid.xml | 15 ----- 2 files changed, 113 deletions(-) delete mode 100644 attachmentsid/attachmentsid.php delete mode 100644 attachmentsid/attachmentsid.xml diff --git a/attachmentsid/attachmentsid.php b/attachmentsid/attachmentsid.php deleted file mode 100644 index e21b71ef..00000000 --- a/attachmentsid/attachmentsid.php +++ /dev/null @@ -1,98 +0,0 @@ - 'onContentPrepare' - ]; - } - - public function onContentPrepare(Event $event) - { - if (version_compare(JVERSION, '5', 'lt')) { - [$context, $row, $params, $page] = $event->getArguments(); - } - else { - $context = $event['context']; - $row = $event['subject']; - $params = $event['params']; - } - // Set the parent info from the context - if (strpos($context, '.') === false) - { - // Assume the context is the parent_type - $parent_type = $context; - $parent_entity = ''; - } - else - { - list ($parent_type, $parent_entity) = explode('.', $context, 2); - } - return $this->OnPrepareRow($row, $parent_type, $parent_entity, $row->id); - } - - public function OnPrepareRow(&$row, $parent_type, $parent_entity, $parent_id) { - static $load = null; - - $matches = array(); - preg_match_all(PF_REGEX_ATTACHIDI_PATTERN, $row->text, $matches); - if(count($matches[0])){ - $oldid = $row->id; - for($i = 0, $total = count($matches[0]); $i < $total; $i++){ - $base = $matches[0][$i]; - $attachment_ids = explode(',', $matches[1][$i]); - JPluginHelper::importPlugin('attachments'); - $apm = AttachmentsPluginManager::getAttachmentsPluginManager(); - if ( !$apm->attachmentsPluginInstalled($parent_type) ) { - // Exit quietly if there is no Attachments plugin to handle this parent_type - $row->text = "no plugins"; - return false; - } - $parent = $apm->getAttachmentsPlugin($parent_type); - $mvc = Factory::getApplication() - ->bootComponent("com_attachments") - ->getMVCFactory(); - /** @var \JMCameron\Component\Attachments\Site\Controller\AttachmentsController $controller */ - $controller = $mvc->createController('Attachments', 'Site', [], $this->app, $this->app->getInput()); - // parent_id is set arbitrary to 1 - $attachments_list = $controller->displayString($parent_id, $parent_type, 'article', null, true, true, false, 'article', $attachment_ids); - $row->text = str_replace($base, $attachments_list, $row->text); - } - } - } -} \ No newline at end of file diff --git a/attachmentsid/attachmentsid.xml b/attachmentsid/attachmentsid.xml deleted file mode 100644 index 06bf0e36..00000000 --- a/attachmentsid/attachmentsid.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - plg_content_attachmentsid - 4.0.3 - October 28, 2024 - Jonathan M. Cameron - jmcameron@jmcameron.net - http://joomlacode.org/gf/project/attachments/ - (C) 2007-2018 Jonathan M. Cameron. All rights reserved. - http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL - ATTACH_ATTACHMENTS_PLUGIN_DESCRIPTION - - attachmentsid.php - - From 99116d71dab9d50687b02085c3a3bb7ecd178e27 Mon Sep 17 00:00:00 2001 From: Theofilos Intzoglou Date: Sun, 24 Nov 2024 13:59:12 +0200 Subject: [PATCH 6/6] Code cleanup --- attachments_component/site/src/Model/AttachmentsModel.php | 1 - attachments_plugin_framework/src/PlgAttachmentsFramework.php | 1 - 2 files changed, 2 deletions(-) diff --git a/attachments_component/site/src/Model/AttachmentsModel.php b/attachments_component/site/src/Model/AttachmentsModel.php index 1adc6209..1c0366b1 100644 --- a/attachments_component/site/src/Model/AttachmentsModel.php +++ b/attachments_component/site/src/Model/AttachmentsModel.php @@ -17,7 +17,6 @@ use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; -use Joomla\CMS\Log\Log; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Uri\Uri; diff --git a/attachments_plugin_framework/src/PlgAttachmentsFramework.php b/attachments_plugin_framework/src/PlgAttachmentsFramework.php index e11eb149..a2bcae73 100644 --- a/attachments_plugin_framework/src/PlgAttachmentsFramework.php +++ b/attachments_plugin_framework/src/PlgAttachmentsFramework.php @@ -20,7 +20,6 @@ use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; - use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Database\DatabaseDriver; use Joomla\Event\SubscriberInterface;