/**
  * Replaces the metadata of extensions automatically from the given data
  *
  * @param array         $metadata
  * @param array         $head
  * @param JDocumentHTML $document
  *
  * @throws Exception
  */
 private function automaticReplacement($metadata, $head, $document)
 {
     // Extension: com_content - View: article
     $content = $this->params->get('com_content_enable');
     if (!empty($content)) {
         $option = $this->request->get('option');
         $view = $this->request->get('view');
         $article_id = $this->request->get('id', '', 'INT');
         if ($option == 'com_content' and $view == 'article' and !empty($article_id)) {
             $model = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request' => true));
             $model->setState('params', JFactory::getApplication()->getParams());
             $article = (array) $model->getItem($article_id);
             if (!empty($article)) {
                 // Get most often used keywords - do not replace keywords from EFSEO table
                 if (empty($metadata['keywords'])) {
                     $content_overwrite_keywords = $this->params->get('com_content_overwrite_keywords');
                     // Only set keywords automatically if no global keywords are entered or the overwrite option is enabled
                     if (empty($head['metaTags']['standard']['keywords']) or !empty($content_overwrite_keywords)) {
                         $content_number_keywords = $this->params->get('com_content_number_keywords');
                         $content_blacklist_keywords = array_map('mb_strtolower', array_map('trim', explode(',', $this->params->get('com_content_blacklist_keywords'))));
                         $content_keywords_whole_text = $article['introtext'] . ' ' . $article['fulltext'];
                         $content_min_length_keyword = $this->params->get('com_content_min_length_keywords', 3);
                         $document->setMetaData('keywords', $this->automaticReplacementKeywords($content_number_keywords, $content_blacklist_keywords, $content_keywords_whole_text, $content_min_length_keyword));
                     }
                 }
                 // Generate the description - do not replace description from EFSEO table
                 if (empty($metadata['description'])) {
                     $content_overwrite_description = $this->params->get('com_content_overwrite_description');
                     // Only set description automatically if no global description is entered or the overwrite option is enabled
                     if (empty($head['description']) or !empty($content_overwrite_description)) {
                         $content_description_select_text = $this->params->get('com_content_description_select_text');
                         $content_description_add_dots = $this->params->get('com_content_description_add_dots');
                         $clean_again = true;
                         if ($content_description_select_text == 0) {
                             if (!empty($article['fulltext'])) {
                                 $content_description_whole_text = $article['fulltext'];
                             } else {
                                 $content_description_whole_text = $article['introtext'];
                             }
                         } elseif ($content_description_select_text == 1) {
                             if (!empty($article['introtext'])) {
                                 $content_description_whole_text = $article['introtext'];
                             } else {
                                 $content_description_whole_text = $article['fulltext'];
                             }
                         } elseif ($content_description_select_text == 2) {
                             $content_description_whole_text = $article['introtext'] . ' ' . $article['fulltext'];
                             $clean_again = false;
                         }
                         $document->setMetaData('description', $this->automaticReplacementDescription($content_description_whole_text, $content_description_add_dots, $clean_again));
                     }
                 }
             }
             return;
         }
     }
     // Extension: com_k2 - View: item
     $k2 = $this->params->get('com_k2_enable');
     if (!empty($k2)) {
         $option = $this->request->get('option');
         $view = $this->request->get('view');
         $item_id = $this->request->get('id', '', 'INT');
         if ($option == 'com_k2' and $view == 'item' and !empty($item_id)) {
             $query = "SELECT * FROM #__k2_items WHERE id = " . $item_id;
             $this->db->setQuery($query);
             $item = $this->db->loadAssoc();
             if (!empty($item)) {
                 // Get most often used keywords - do not replace keywords from EFSEO table
                 if (empty($metadata['keywords'])) {
                     $k2_overwrite_keywords = $this->params->get('com_k2_overwrite_keywords');
                     // Only set keywords automatically if no global keywords are entered or the overwrite option is enabled
                     if (empty($head['metaTags']['standard']['keywords']) or !empty($k2_overwrite_keywords)) {
                         $k2_number_keywords = $this->params->get('com_k2_number_keywords');
                         $k2_blacklist_keywords = array_map('mb_strtolower', array_map('trim', explode(',', $this->params->get('com_k2_blacklist_keywords'))));
                         $k2_keywords_whole_text = $item['introtext'] . ' ' . $item['fulltext'];
                         $k2_min_length_keyword = $this->params->get('com_k2_min_length_keywords', 3);
                         $document->setMetaData('keywords', $this->automaticReplacementKeywords($k2_number_keywords, $k2_blacklist_keywords, $k2_keywords_whole_text, $k2_min_length_keyword));
                     }
                 }
                 // Generate the description - do not replace description from EFSEO table
                 if (empty($metadata['description'])) {
                     $k2_overwrite_description = $this->params->get('com_k2_overwrite_description');
                     // Only set description automatically if no global description is entered or the overwrite option is enabled
                     if (empty($head['description']) or !empty($k2_overwrite_description)) {
                         $k2_description_select_text = $this->params->get('com_k2_description_select_text');
                         $k2_description_add_dots = $this->params->get('com_k2_description_add_dots');
                         $clean_again = true;
                         if ($k2_description_select_text == 0) {
                             if (!empty($item['fulltext'])) {
                                 $k2_description_whole_text = $item['fulltext'];
                             } else {
                                 $k2_description_whole_text = $item['introtext'];
                             }
                         } elseif ($k2_description_select_text == 1) {
                             if (!empty($item['introtext'])) {
                                 $k2_description_whole_text = $item['introtext'];
                             } else {
                                 $k2_description_whole_text = $item['fulltext'];
                             }
                         } elseif ($k2_description_select_text == 2) {
                             $k2_description_whole_text = $item['introtext'] . ' ' . $item['fulltext'];
                             $clean_again = false;
                         }
                         $document->setMetaData('description', $this->automaticReplacementDescription($k2_description_whole_text, $k2_description_add_dots, $clean_again));
                     }
                 }
             }
             return;
         }
     }
 }