Skip to content

Commit

Permalink
Merge pull request #71 from ctidigital/paragraphs-in-product-descript…
Browse files Browse the repository at this point in the history
…ions

Maintain layout of descriptions when importing products.
  • Loading branch information
paulpartington-cti authored Jan 19, 2018
2 parents 8c213f2 + b5daf7b commit 24ac2c8
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Component/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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, "</p>".PHP_EOL."<p>", $data);
$data = str_replace("<p></p>".PHP_EOL, "", $data);
$data = "<p>".$data."</p>";
}
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
*
Expand All @@ -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);
}

Expand Down

0 comments on commit 24ac2c8

Please sign in to comment.