diff --git a/Component/Products.php b/Component/Products.php index 9d3dbc5..e3a357e 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,40 @@ 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 +402,7 @@ private function replaceSeparator($data, $column) private function clean($value, $column) { $value = $this->replaceSeparator($value, $column); + $value = $this->insertParagraphs($value, $column); return trim($value); }