Antwort #136 am: 17. Oktober 2018, 17:11:10
So, habe glaube fertig...
Da die Integration nicht so schwierig war, da abgesehen vom Aufruf der enhancer-Funktion alles beim alten blieb, habe ich mir die Modifikationen nochmals vorgenommen und etwas verfeinert - Erläuterungen dazu weiter unten.
Wer die von mir gemachten Änderungen nutzen will, kopiert am Besten einfach den kompletten nachfolgenden Code in die Datei
templates/CURRENT_TEMPLATE/smarty/modifier.contentenhancer.php - alles allem dort befindlichen Code markieren, löschen und mit dem nachfolgenden Code ersetzen - dann sind alle Änderungen sicher drin (es sind nämlich doche einige geworden, und ich habe glaube ich vergessen auch einige zu markieren...)
<?phpfunction smarty_modifier_contentenhancer
($html) { // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying setting template if(preg_match_all('/(\[([\w\d ]*)template[^\]]+\])/', $html, $matches)){ foreach($matches[1] AS $snippet){ if(preg_match('/template\=\'([^\']+)\'/', $snippet, $template_matches)){ if (!empty($template_matches[1])) { $template=$template_matches[1]; $template_usage=(!empty($matches[2][0])) ?
$matches[2][0] : "all"; $html = str_replace($snippet, '', $html); break; } } } } // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying setting template if(preg_match_all('/(\[products [^\]]+\])/', $html, $matches)){ foreach($matches[1] AS $snippet){ $products_models = null; $categories_ids = null; $keywords = null; // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for product_models $restrictions=setRestrictions
($snippet); // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for product_models if(preg_match('/products_models\=\'([^\']+)\'/', $snippet, $model_matches)){ $t_models = explode(',', $model_matches[1]); $products_models = array(); foreach($t_models AS $model){ $products_models[] = xtc_db_input
(trim($model)); } } // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for product_models $restrictions=setRestrictions
($snippet); // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for product_models if(preg_match('/categories_ids\=\'([^\']+)\'/', $snippet, $categories_ids_matches)){ $t_categories_ids = explode(',', $categories_ids_matches[1]); $categories_ids = array(); foreach($t_categories_ids AS $categories_id){ $categories_ids[] = (int
)$categories_id; } } // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for product_models $restrictions=setRestrictions
($snippet); // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for product_models if(preg_match('/keywords\=\'([^\']+)\'/', $snippet, $keyword_matches)){ $t_keywords = explode(',', $keyword_matches[1]); $keywords = array(); foreach($t_keywords AS $keyword){ $keywords[] = xtc_db_input
(trim($keyword)); } } // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - parse Products (with selected template) for keyword with $limit, $order_by, $exclusion (change function call) if (($template_usage == 'all') || ($template_usage == 'categories')) { $html = str_replace($snippet, parseProducts
($products_models, $categories_ids, $keywords, $restrictions, $template), $html); } else { $html = str_replace($snippet, parseProducts
($products_models, $categories_ids, $keywords, $restrictions), $html); } // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - parse Products (with selected template) for keyword with $limit, $order_by, $exclusion (change function call) } } if(preg_match_all('/(\[categories [^\]]+\])/', $html, $matches)){ foreach($matches[1] AS $snippet){ if(preg_match('/categories_ids\=\'([^\']+)\'/', $snippet, $categories_ids_matches)){ $t_categories_ids = explode(',', $categories_ids_matches[1]); $categories_ids = array(); foreach($t_categories_ids AS $categories_id){ $categories_ids[] = (int
)$categories_id; } } if(preg_match('/keywords\=\'([^\']+)\'/', $snippet, $keyword_matches)){ $t_keywords = explode(',', $keyword_matches[1]); $keywords = array(); foreach($t_keywords AS $keyword){ $keywords[] = xtc_db_input
(trim($keyword)); } } // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - parse Categories (with selected template) $restrictions=setRestrictions
($snippet); if (($template_usage == 'all') || ($template_usage == 'categories')) { $html = str_replace($snippet, parseCategories
($categories_ids, $keywords, $restrictions, $template), $html); } else { $html = str_replace($snippet, parseCategories
($categories_ids, $keywords, $restrictions), $html); } // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - parse Categories (with selected template) } } // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for keyword with $limit, $order_by, $exclusion (change function call) $html = parseSubstitutions
($html); // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for keyword with $limit, $order_by, $exclusion (change function call) return $html;}function /* parseCategories($categories_ids = NULL) { */ parseCategories
($categories_ids = null, $keywords = null, $restrictions = null, $template = '/module/modules/content_enhancer/listing_cats.html'){ // Whiteflash: Modul Content-Enhancer - mod modifying sql_settings for keyword with $limit if ((is_array($categories_ids)) || (is_array($keywords))) { if (GROUP_CHECK
== 'true') { $group_check = "c.group_permission_".$_SESSION['customers_status']['customers_status_id']."=1 AND"; } // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for keyword $keywords_sequence = generateKeywordsequence
($categories_ids, $keywords, 'cd', array('categories_name', 'categories_description')); $q = "SELECT
c.categories_id,
c.categories_image,
cd.categories_name,
cd.categories_description
FROM categories AS c
LEFT JOIN categories_description as cd
ON c.categories_id = cd.categories_id
WHERE c.categories_id IN ('".implode("', '", $categories_ids)."')
".$keywords_sequence." -- Whiteflash 2018-10-23: add keywords_sequence
AND ".$group_check." c.categories_status = '1'
AND cd.language_id = '" .(int
) $_SESSION['languages_id'] ."'
ORDER BY FIND_IN_SET(c.categories_id,'".implode(",", $categories_ids)."')"; // Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for restrictions $q = convertRestrictions
($q, $restrictions); $categories_query = xtDBquery
($q); while($categories = xtc_db_fetch_array
($categories_query, true)) { $category_link =xtc_category_link
($categories['categories_id'],$categories['categories_name']); $module_content[] = array ('CATEGORY_NAME' => $categories['categories_name'], 'CATEGORY_IMAGE_TRUE' => $categories['categories_image'], 'CATEGORY_IMAGE' => DIR_WS_IMAGES
.'categories/' . $categories['categories_image'], 'CATEGORY_LINK' => xtc_href_link
(FILENAME_DEFAULT
, xtc_get_all_get_params
(array(array('cat','page','filter_id','manufacturers_id'))) . $category_link), 'CATEGORY_DESCRIPTION' => $categories['categories_description'], 'CATEGORY_DESCRIPTION_INTRO' => shortenStringOnNextSpace
($categories['categories_description']) ); } //echo "<pre>"; //var_dump($module_content); //echo "</pre>"; $smarty = new Smarty
; $smarty->assign('categories', $module_content); $smarty->assign('language', $_SESSION["language"]); // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - check template file and set ready for use $template_chck = str_replace('//', '/', DIR_FS_CATALOG
) .'templates/'. CURRENT_TEMPLATE
. $template; $used_template = (file_exists($template_chck)) ? CURRENT_TEMPLATE
.$template : CURRENT_TEMPLATE
.'/module/modules/content_enhancer/listing_cats.html'; // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - check template file and set ready for use $html = $smarty->fetch($used_template); return $html; }}function /* parseProducts($products_models = null, $categories_ids = null, $keywords = null){ */ parseProducts
($products_models = null, $categories_ids = null, $keywords = null, $restrictions = null, $template = '/module/modules/content_enhancer/listing.html'){ // Whiteflash: Modul Content-Enhancer - mod modifying sql_settings for keyword with $limit if(is_array($products_models) || is_array($categories_ids) || is_array($keywords)){ $group_check = ''; $fsk_lock = ''; if ($_SESSION['customers_status']['customers_fsk18_display'] == '0') { $fsk_lock = ' AND p.products_fsk18!=1'; } if (GROUP_CHECK
== 'true') { $group_check = " AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']."=1 "; } $q = "SELECT * FROM
".TABLE_PRODUCTS
." p
JOIN ".TABLE_PRODUCTS_DESCRIPTION
." pd ON (p.products_id = pd.products_id AND pd.language_id = ".(int
)$_SESSION["languages_id"].")
WHERE
p.products_status = 1 ".$fsk_lock.$group_check; if(is_array($products_models)){ $q .= " AND p.products_model IN ('".implode("', '", $products_models)."') "; } if(is_array($categories_ids)){ $q .= " AND p.products_id IN (SELECT p2c.products_id FROM ".TABLE_PRODUCTS_TO_CATEGORIES
." p2c WHERE p2c.categories_id IN (".implode(',', $categories_ids).")) "; } // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for keyword $q .= generateKeywordsequence
($categories_ids, $keywords, 'pd', array('products_keywords', 'products_name', 'products_description'), 'AND'); // Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for keyword $q = convertRestrictions
($q, $restrictions); global $product; $module_content = array(); $listing_query = xtDBquery
($q); while ($listing = xtc_db_fetch_array
($listing_query, true)) { // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for keyword if (is_array($restrictions['excludes'])) { foreach ($restrictions['excludes'] as $column => $values) { if ((!empty($listing[$column])) && (in_array($listing[$column], $values))) continue 2; } } // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - mod modifying sql_settings for keyword $module_content[] = $product->buildDataArray($listing); } $smarty = new Smarty
; $smarty->assign('products', $module_content); $smarty->assign('language', $_SESSION["language"]); // BOF - Whiteflash 2018-10-23: Modul Content-Enhancer - check template file and set ready for use $template_chck = str_replace('//', '/', DIR_FS_CATALOG
) .'templates/'. CURRENT_TEMPLATE
. $template; $used_template = (file_exists($template_chck)) ? CURRENT_TEMPLATE
.$template : CURRENT_TEMPLATE
.'/module/modules/content_enhancer/listing.html'; // EOF - Whiteflash 2018-10-23: Modul Content-Enhancer - check template file and set ready for use $html = $smarty->fetch($used_template); return $html; } return '';}/**
*
* @param type $input
* @param type $target_length
* @return string
* @author Whiteflash, 2018-10-23
*/function shortenStringOnNextSpace
($input, $target_length = 125) { $shorty = substr($input, 0, strpos($input, " ", $target_length)); if ((!empty($shorty)) && (strlen($shorty) < strlen($input))) $shorty .= "..."; return $shorty;}/**
*
* @param type $q
* @param type $restrictions
* @return string
* @author Whiteflash, 2018-10-23
*/function convertRestrictions
($q, $restrictions) { if (!empty($restrictions['order_by'][0])) $q .= " ORDER BY ".$restrictions['order_by'][0]." ".$restrictions['order_by'][1]; if (!empty($restrictions['limit'])) $q .= " LIMIT ".$restrictions['limit']; if (!empty($restrictions['random'])) { if (($order_by_pos = strpos($q, 'ORDER BY')) !== false) $q = substr($q, 0, $order_by_pos); $q .= " ORDER BY RAND() ".((!empty($restrictions['random'])) ?
( (strtolower($restrictions['random']) == 'all') ?
"" : "LIMIT ".$restrictions['random']."") : "")." "; } return $q;}/**
*
* @param array $categories_ids
* @param array $keywords
* @param string $table_shortcut
* @param array $table_fields
* @param string $concatenate, optional (null)
* @return string keywordsequence
* @author Whiteflash, 2018-10-23
*/function generateKeywordsequence
($categories_ids, $keywords, $table_shortcut, $table_fields, $concatenate = null) { $keywords_sequence = (!empty($concatenate)) ?
" ".$concatenate : ""; if(is_array($keywords)) { $connector = (empty($categories_ids)) ?
" " : " OR"; $keywords_sequence .= $connector." (1!=1"; foreach($keywords AS $keyword){ for ($i=0;$i<count($table_fields);$i++) { $keywords_sequence .= " OR (".$table_shortcut.".".$table_fields[$i]." LIKE ('%".$keyword."%'))"; } } $keywords_sequence .= ")"; } return $keywords_sequence;}/**
*
* @param type $html
* @return type
* @author Whiteflash, 2018-10-23
*/function parseSubstitutions
($html) { if (strpos($html, 'substitutions') !== false) { if(preg_match_all('/(\[substitutions [^\]]+\])/', $html, $matches)){ foreach($matches[1] AS $match){ $quantifier = ''; $regex = '{(.[^{]*?)}'; $regex = '(?<={)(.[^{]*?)(?=})'; $left_brace = strpos($match, '{'); $right_brace = strrpos($match, '}'); $captured_raw_jsonstring = substr($match, $left_brace, (($right_brace - $left_brace) + 1)); $captured_jsonstring = str_replace('\'', '"', strip_tags($captured_raw_jsonstring)); $arguments = json_decode($captured_jsonstring); if ($arguments !== NULL) { foreach ($arguments as $search => $argument) { if (is_object($argument)) { $substitution = $limit = null; $validity_range = array(); foreach ($argument as $command => $c_argument) { if ($command == 'substitution') { $substitution = $c_argument; } elseif ($command == 'limit') { $limit = $c_argument; } elseif ($command == 'validity') { if (!is_object($c_argument)) { $validity_range = array($c_argument); } else { foreach ($c_argument as $cc_key => $cc_argument) { $validity_range[] = $cc_argument; } } } else { // unknown $command } } // check if substitution found if ($substitution !== null) { foreach ($validity_range as $identifyer) { $elements = array(); if (($identifyer == 'products_name') || ($identifyer == 'name')) { $tagname = 'div'; $elements = findElements
($html, $search, $tagname, array('class', 'nameWr')); } elseif ($identifyer == 'text') { $tagname = 'p'; $elements = findElements
($html, $search, $tagname, array()); } else { continue; } if(!empty($elements)) { $count_replaces = 0; foreach ($elements as $element) { if (($limit !== null) && ($count_replaces >= $limit)) { continue; } $new_replace = str_replace($search, $substitution, $element['inner_html']); $html = str_replace($element['inner_html'], $new_replace, $html); $count_replaces++; } } } } } else { $html = str_replace($search, $argument, $html); } } } // remove substitutions-instructions from output if (strpos($html, '[substitutions')!==false) $html = preg_replace('/(\[substitutions [^\]]+\])/', '', $html); } } } return $html;}/**
*
* @param string $string
* @param string $search
* @param string $tagname
* @param array $identifying, optional
* @return array | boolean false on error
* @author Whiteflash, 2018-10-23
*/function findElements
($string, $search, $tagname, $identifying = array()) { $elements = array(); $security_chars = 10; while (($pos = (strpos($string, $tagname, $offset))) !== false) { $end_of_tag_pos = strpos($string, '>', $pos); $start_of_next_tag_pos = null; if (!empty($identifying)) { $identifyer_length = strlen($identifying[0]) + strlen($identifying[1]) + $security_chars; $controllstring = substr($string, $pos, $identifyer_length); if (strpos($controllstring, $identifying[1]) === false) { $start_of_next_tag_pos = strpos($string, '</'.$tagname, $end_of_tag_pos); $start_of_next_tag_pos_sec = strpos($string, '</ '.$tagname, $end_of_tag_pos); } else { // nothing? - no, do nothing } } else { $start_of_next_tag_pos = strpos($string, '</'.$tagname, $end_of_tag_pos); $start_of_next_tag_pos_sec = strpos($string, '</ '.$tagname, $end_of_tag_pos); } // check for problems in setup of the html-tags if (($start_of_next_tag_pos_sec - $start_of_next_tag_pos) > 1) { return false; } // recompare start-pos of next tag if ($start_of_next_tag_pos_sec > $start_of_next_tag_pos) $start_of_next_tag_pos = $start_of_next_tag_pos_sec; if ($start_of_next_tag_pos !== null) { $inner_html = substr($string, ($end_of_tag_pos + 1), (($start_of_next_tag_pos - $end_of_tag_pos) - 1)); if (strpos($inner_html, $search) !== false) { $elements[] = array('inner_html'=>$inner_html, 'start'=>($end_of_tag_pos + 1), 'end'=>(($start_of_next_tag_pos - $end_of_tag_pos) - 1)); } } // upcount offset $offset = $pos + 1; } return $elements;}/**
* Function evaluates all restrictions from the given snippet-string.
* @param string $snippet
* @return array
* @author Whiteflash, 2018-10-23
*/function setRestrictions
($snippet) { // return already known restrictions if (key_exists($snippet, $_SESSION['snippets'])) return $_SESSION['snippets'][$snippet]; $restrictions = array(); if(preg_match('/limit\=\'([^\']+)\'/', $snippet, $modding_matches)) $restrictions['limit'] = intval($modding_matches[1]); if(preg_match('/order_by\=\'([^\']+)\'/', $snippet, $modding_matches)) $restrictions['order_by'] = (!empty($modding_matches[1]))?
explode(':', $modding_matches[1]):null; if(preg_match('/random\=\'([^\']+)\'/', $snippet, $modding_matches)) $restrictions['random'] = intval($modding_matches[1]); // check order_by-statement if (!strstr($restrictions['order_by'][0], '.')) getColumsTable
($restrictions['order_by'][0]); if (empty($restrictions['order_by'][1])) $restrictions['order_by'][1] = 'asc'; if(preg_match('/excludes\=\'([^\']+)\'/', $snippet, $modding_matches)) $excludes = (!empty($modding_matches[1]))?
explode(':', $modding_matches[1]):null; // check excludes-statement if (strstr($excludes[0], '.')) unsetColumsTable
($excludes[0]); $excludes[1]=explode(',', $excludes[1]); array_walk($excludes[1], 'trim'); $restrictions['exlcudes']=array($excludes[0] => $excludes[1]); // prepare (and save) restriction if (!is_array($_SESSION['snippets'])) $_SESSION['snippets'] = array(); $_SESSION['snippets'][$snippet] = $restrictions; return $restrictions;}/**
* Unsets all given table-prefixes of th given $input.
* @param string $input
* @author by Whiteflash, 2018-10-23
*/function unsetColumnsTable
(&$input) { $input=preg_replace('#.*\.#', '', $input);}/**
* Switches table-prefix for $input (column) or set it to null, if no match given.
* @param string $input
* @author Whiteflash, 2018-10-23
*/function getColumsTable
(&$input) { switch ($input) { case "products_id": $input='p.'.$input; break; case "products_model": $input='p.'.$input; break; case "products_status": $input='p.'.$input; break; case "products_name": $input='pd.'.$input; break; case "products_keywords": $input='pd.'.$input; break; default: $input=null; }}?> Zur Verwendung der Modifikationen:
1) Template:[template='/module/modules/content_enhancer/flow_listing.html']
[productstemplate='/module/modules/content_enhancer/flow_listing.html']
[categoriestemplate='/module/modules/content_enhancer/flow_listing.html']
Über den Tag template kann ein alternatives Template gewählt werden. Wird kein Tag gesetzt, dann bleibt es beim Standard-Template '
/module/modules/content_enhancer/listing.html' für die Produkte und bei '
/module/modules/content_enhancer/listing-cats.html' für die Kategorien. Wird dem Tag ein '
products' vorangestellt, gilt das gesetzte Template nur für die Produkte, wird ein '
categories' vorangestellt, analog nur für die Kategorien. Bei keinem gesetzten Präfix gilt das Template dann für beides (Vorsicht, das muss dann aber mit beidem umgehen können: Produkte und Kategorien!).
2) Tags für Produkte:[products keywords='keyword1,keyword2' Limit o. random='8 o. all' order_by='products_id:desc' excludes='products_id:314,313']
Bei allen 3 Tag-Arten (also keywords, categories_ids und products_models können frei und in loser Reihenfolge folgende Modifikatoren übergeben werden:
1.
limit: setzt in der SQL-Abfrage ein Limit (dann auf jeden Fall Anzahl angeben!) oder
random: wählt zufällig die gesetzte Anzahl an Artikeln oder Kategorien oder zeigt alle (all) in zufälliger Reihenfolge an.
2.
order_by: setzt in der SQL-Abfrage ein Order By. Schema
'Tabellenkürzel.Spaltenname:Sortierrichtung'
3.
excludes: sortiert alle Produkte mit der angegebenen products_id wieder aus dem Ergebnis-Array
... to be continued