diff --git a/attachments_component/site/src/Controller/AttachmentsController.php b/attachments_component/site/src/Controller/AttachmentsController.php index f1eabee1..43e76d48 100644 --- a/attachments_component/site/src/Controller/AttachmentsController.php +++ b/attachments_component/site/src/Controller/AttachmentsController.php @@ -79,7 +79,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(); @@ -102,7 +102,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..1c0366b1 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 ) { @@ -387,7 +387,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 && 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 // do the query differently @@ -497,7 +499,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 +510,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/PlgAttachmentsFramework.php b/attachments_plugin_framework/src/PlgAttachmentsFramework.php index 3a032ccd..a2bcae73 100644 --- a/attachments_plugin_framework/src/PlgAttachmentsFramework.php +++ b/attachments_plugin_framework/src/PlgAttachmentsFramework.php @@ -911,25 +911,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 +948,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 +1020,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 +1038,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 +1054,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 +1066,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;