From 4f02270747c1486ed885fa78c5f8a2c40413c3ea Mon Sep 17 00:00:00 2001 From: Andy Irvine Date: Fri, 19 Jan 2018 16:01:19 +0000 Subject: [PATCH 1/2] Maintain layout of descriptions when importing products. Uses newlines to define paragraphs. --- Component/Products.php | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Component/Products.php b/Component/Products.php index 9d3dbc5..1403e07 100644 --- a/Component/Products.php +++ b/Component/Products.php @@ -43,6 +43,17 @@ class Products extends CsvComponentAbstract 'store_view_code' ]; + /** + * Attributes that may have newlines defined. These will be split into + * paragraphs so text looks the same on frontend. + * + * @var array + */ + protected $attrDescription = [ + 'description', + 'short_description' + ]; + /** * @var ImporterFactory */ @@ -346,6 +357,39 @@ private function replaceSeparator($data, $column) return $data; } + /** + * Format description attribute values where newlines indicate + * the position of paragraphs. + * + * @param $data + * @param $column + * + * @return mixed|string + */ + private function insertParagraphs($data, $column) + { + if (in_array($column, $this->attrDescription) && !$this->spotHtmlTags($data, "p")) { + $data = str_replace(PHP_EOL, "

".PHP_EOL."

", $data); + $data = str_replace("

".PHP_EOL, "", $data); + $data = "

".$data."

"; + } + return $data; + } + + /** + * Find html tags in the given string + * + * @param $string + * @param $tagname + * + * @return int + */ + private function spotHtmlTags($string, $tagname) { + $pattern = "/<$tagname?.*>(.*)<\/$tagname>/"; + preg_match($pattern, $string, $matches); + return count($matches); + } + /** * Tidy up the value * @@ -357,6 +401,7 @@ private function replaceSeparator($data, $column) private function clean($value, $column) { $value = $this->replaceSeparator($value, $column); + $value = $this->insertParagraphs($value, $column); return trim($value); } From b5daf7bc02c8385390d6b4ed9e06cef6dd2bb395 Mon Sep 17 00:00:00 2001 From: Andy Irvine Date: Fri, 19 Jan 2018 16:08:05 +0000 Subject: [PATCH 2/2] Correcting position of brace. --- Component/Products.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Component/Products.php b/Component/Products.php index 1403e07..e3a357e 100644 --- a/Component/Products.php +++ b/Component/Products.php @@ -384,7 +384,8 @@ private function insertParagraphs($data, $column) * * @return int */ - private function spotHtmlTags($string, $tagname) { + private function spotHtmlTags($string, $tagname) + { $pattern = "/<$tagname?.*>(.*)<\/$tagname>/"; preg_match($pattern, $string, $matches); return count($matches);