/** Insert the attachments list into the content text (for front end)
  *
  * @param	object	&$content		the text of the content item (eg, article text)
  * @param	int		$parent_id		the ID for the parent object
  * @param	string	$parent_entity	the type of entity for this parent type
  *
  * @return	string	the modified content text (false for failure)
  */
 public function insertAttachmentsList(&$content, $parent_id, $parent_entity)
 {
     $aparams = $this->attachmentsParams();
     // Get the desired placement
     $attachments_placement = $aparams->get('attachments_placement', 'end');
     if ($attachments_placement == 'disabled_nofilter') {
         return false;
     }
     // Determine where we are
     $from = JRequest::getCmd('view', 'closeme');
     $Itemid = JRequest::getInt('Itemid', 1);
     // See whether we can display the links to add attachments
     $user_can_add = $this->userMayAddAttachment($parent_id, $parent_entity);
     // Get the field name for the content item's text
     $text_field_name = $this->getTextFieldName($content, $parent_entity);
     if ($text_field_name === null) {
         return false;
     }
     // Get the attachments tag, if present
     $attachments_tag = '';
     $attachments_tag_args = '';
     $match = false;
     if (JString::strpos($content->{$text_field_name}, '{attachments')) {
         if (preg_match('@(<span class="hide_attachments_token">)?{attachments([ ]*:*[^}]+)?}(</span>)?@', $content->{$text_field_name}, $match)) {
             $attachments_tag = true;
         }
         if (isset($match[1]) && $match[1]) {
             $attachments_tag_args_raw = $match[1];
             $attachments_tag_args = ltrim($attachments_tag_args_raw, ' :');
         }
         if ($attachments_tag) {
             $attachments_tag = $match[0];
         }
     }
     // Check the security status
     $attach_dir = JPATH_SITE . '/' . AttachmentsDefines::$ATTACHMENTS_SUBDIR;
     $secure = $aparams->get('secure', false);
     $hta_filename = $attach_dir . '/ . htaccess';
     if ($secure && !file_exists($hta_filename) || !$secure && file_exists($hta_filename)) {
         AttachmentsHelper::setup_upload_directory($attach_dir, $secure);
     }
     // Construct the attachment list (if appropriate)
     $html = '';
     $attachments_list = false;
     $add_attachement_btn = false;
     // Get the html for the attachments list
     require_once JPATH_SITE . '/components/com_attachments/controllers/attachments.php';
     $controller = new AttachmentsControllerAttachments();
     $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<div class=\"{$class_name}\" id=\"{$div_id}\"></div>\n";
     }
     $html .= $attachments_list;
     if ($html || $user_can_add) {
         // Add the style sheet
         JHtml::stylesheet('com_attachments/attachments_list.css', array(), true);
         // Handle RTL styling (if necessary)
         $lang = JFactory::getLanguage();
         if ($lang->isRTL()) {
             JHtml::stylesheet('com_attachments/attachments_list_rtl.css', array(), true);
         }
     }
     // 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 = "<div class=\"attachmentsContainer\">\n" . $html . "\n</div>";
     }
     // Finally, add the attachments
     // NOTE: Hope str_replace() below is UTF8 safe (since the token being replaced is UTF8)...
     switch ($attachments_placement) {
         case 'beginning':
             // Put the attachments list at the beginning
             if ($attachments_list || $user_can_add) {
                 if ($attachments_tag) {
                     $content->{$text_field_name} = $html . $content->{$text_field_name};
                 } else {
                     $content->{$text_field_name} = $html . str_replace($attachments_tag, '', $content->{$text_field_name});
                 }
             }
             break;
         case 'custom':
             // Insert the attachments at the desired location
             if ($attachments_list || $user_can_add) {
                 if ($attachments_tag) {
                     $content->{$text_field_name} = str_replace($attachments_tag, $html, $content->{$text_field_name});
                 } else {
                     // If there is no tag, insert the attachments at the end
                     $content->{$text_field_name} .= $html;
                 }
             }
             break;
         case 'disabled_filter':
             // Disable and strip out any attachments tags
             if ($attachments_tag) {
                 $content->{$text_field_name} = str_replace($attachments_tag, '', $content->{$text_field_name});
             }
             break;
         default:
             // Add the attachments to the end
             if ($attachments_list || $user_can_add) {
                 if ($attachments_tag) {
                     $content->{$text_field_name} = str_replace($attachments_tag, '', $content->{$text_field_name}) . $html;
                 } else {
                     $content->{$text_field_name} .= $html;
                 }
             }
             break;
     }
     return $content;
 }