450 lines
21 KiB
PHP
450 lines
21 KiB
PHP
<?php
|
|
/**
|
|
* SOTESHOP/stNewsletterPlugin
|
|
*
|
|
* Ten plik należy do aplikacji stNewsletterPlugin opartej na licencji (Professional License SOTE).
|
|
* Nie zmieniaj tego pliku, jeśli chcesz korzystać z automatycznych aktualizacji oprogramowania.
|
|
* Jeśli chcesz wprowadzać swoje modyfikacje do programu, zapoznaj się z dokumentacją, jak zmieniać
|
|
* oprogramowanie bez zmiany kodu bazowego http://www.sote.pl/modifications
|
|
*
|
|
* @package stNewsletterPlugin
|
|
* @subpackage libs
|
|
* @copyright SOTE (www.sote.pl)
|
|
* @license http://www.sote.pl/license/sote (Professional License SOTE)
|
|
* @version $Id: stNewsletter.class.php 3208 2010-01-28 13:12:43Z bartek $
|
|
* @author Marcin Butlak <marcin.butlak@sote.pl>
|
|
*/
|
|
|
|
/**
|
|
* Klasa stNewsletter
|
|
*
|
|
* @package stNewsletterPlugin
|
|
* @subpackage libs
|
|
*/
|
|
class stNewsletter {
|
|
// public static function send($offset = 0)
|
|
// {
|
|
// return $offset+1;
|
|
// }
|
|
|
|
public static function getNewsletterGroups($object = null) {
|
|
$c = new Criteria();
|
|
$c -> add(NewsletterUserHasNewsletterGroupPeer::NEWSLETTER_USER_ID, $object -> getId());
|
|
$c -> add(NewsletterUserHasNewsletterGroupPeer::NEWSLETTER_GROUP_ID, NewsletterUserHasNewsletterGroupPeer::NEWSLETTER_GROUP_ID . '=' . NewsletterGroupPeer::ID, Criteria::CUSTOM);
|
|
|
|
$userGroups = NewsletterGroupPeer::doSelect($c);
|
|
$groups = array();
|
|
foreach ($userGroups as $group) {
|
|
$groups[] = $group -> getName();
|
|
}
|
|
|
|
return implode('|', $groups);
|
|
}
|
|
|
|
public static function setNewsletterGroups($object = null, $value = '') {
|
|
$c = new Criteria();
|
|
$c -> add(NewsletterUserHasNewsletterGroupPeer::NEWSLETTER_USER_ID, $object -> getId());
|
|
|
|
$remove = false;
|
|
if ($value{0} != '+' && $value{0} != '-') {
|
|
NewsletterUserHasNewsletterGroupPeer::doDelete($c);
|
|
$remove = true;
|
|
}
|
|
|
|
$userGroups = explode('|', $value);
|
|
if (is_array($userGroups)) {
|
|
foreach ($userGroups as $group) {
|
|
|
|
$modifier = null;
|
|
if ($group{0} == '+' || $group{0} == '-') {
|
|
$modifier = $group{0};
|
|
$group = substr($group, 1);
|
|
}
|
|
|
|
$c = new Criteria();
|
|
$c -> add(NewsletterGroupPeer::OPT_NAME, trim($group));
|
|
$dbGroup = NewsletterGroupPeer::doSelectOne($c);
|
|
if (!$dbGroup && strlen(trim($group)) && $modifier != '-') {
|
|
$dbGroup = new NewsletterGroup();
|
|
$dbGroup -> setCulture('pl_PL');
|
|
$dbGroup -> setName(trim($group));
|
|
$dbGroup -> save();
|
|
}
|
|
|
|
if ($dbGroup) {
|
|
if (($remove == true && $modifier == null) || ($remove == false && $modifier == '+')) {
|
|
$newslletterLink = new NewsletterUserHasNewsletterGroup();
|
|
$newslletterLink -> setNewsletterUser($object);
|
|
$newslletterLink -> setNewsletterGroup($dbGroup);
|
|
$newslletterLink -> save();
|
|
} elseif ($remove == false && $modifier == '-') {
|
|
$c = new Criteria();
|
|
$c -> add(NewsletterUserHasNewsletterGroupPeer::NEWSLETTER_USER_ID, $object -> getId());
|
|
$c -> add(NewsletterUserHasNewsletterGroupPeer::NEWSLETTER_GROUP_ID, $dbGroup -> getId());
|
|
NewsletterUserHasNewsletterGroupPeer::doDelete($c);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function addNewUserToNewsletterList($email, $user_id) {
|
|
$c = new Criteria();
|
|
$c -> add(NewsletterUserPeer::EMAIL, $email);
|
|
$newsletterUser = NewsletterUserPeer::doSelectOne($c);
|
|
|
|
if ($newsletterUser) {
|
|
$newsletterUser -> setUserId($user_id);
|
|
$newsletterUser -> save();
|
|
}
|
|
}
|
|
|
|
|
|
public static function getTemplatesMessage($message, $preview, $draft = null, $shop = null)
|
|
{
|
|
|
|
|
|
sfLoader::loadHelpers(array('Helper', 'stUrl'));
|
|
|
|
sfLoader::loadHelpers('stProduct','stProduct');
|
|
|
|
$config = stConfig::getInstance(sfContext::getInstance(), 'stNewsletterBackend');
|
|
|
|
$security_config = stConfig::getInstance(sfContext::getInstance(), 'stSecurityBackend');
|
|
|
|
if ($security_config->get('ssl', null, true) != '') {
|
|
$http_https = "https://";
|
|
}else{
|
|
$http_https = "http://";
|
|
}
|
|
|
|
$i18n = sfContext::getInstance()->getI18n();
|
|
|
|
$template_color_header = $config->get('template_color_header');
|
|
$template_color_background_a = $config->get('template_color_background_a');
|
|
$template_color_background_b = $config->get('template_color_background_b');
|
|
$template_color_background_font = $config->get('template_color_background_font');
|
|
$template_color_font_a = $config->get('template_color_font_a');
|
|
$template_color_font_b = $config->get('template_color_font_b');
|
|
$template_color_font_c = $config->get('template_color_font_c');
|
|
$template_cta_background = $config->get('template_cta_background');
|
|
$template_cta_font = $config->get('template_cta_font');
|
|
|
|
$newsletter_logo = $config->get('newsletter_logo');
|
|
|
|
if ($message == "config") {
|
|
|
|
$message = new NewsletterMessage();
|
|
$message->setSubject("Lorem Ipsum is simply dummy text!");
|
|
$message->setContent("Lorem Ipsum is simply dummy text of the printing and typesetting industry. <a href='' >Example link</a> lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. <a href='' >Example link</a> It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. <a href='' >Example link</a> it was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
|
|
|
|
$call_to_action = '<div style="text-align:center">';
|
|
$call_to_action .= '<a href="#" style="display: inline-block; background-color: #'.$template_cta_background.'; margin:10px 0px; padding: 15px 25px; font-family: Arial, sans-serif;font-size: 14px;color: #'.$template_cta_font.';font-weight: bold; text-decoration: none;" target="_blank">Call to action</a>';
|
|
$call_to_action .= '</div>';
|
|
|
|
$preview = true;
|
|
$draft = true;
|
|
$shop = true;
|
|
|
|
}
|
|
|
|
|
|
$view = new sfPartialView();
|
|
$view->initialize(sfContext::getInstance(), "stNewsletterBackend", "newsletterTemplateMail", false);
|
|
|
|
if ($draft) {
|
|
|
|
$c = new Criteria();
|
|
$c->add(NewsletterDraftMessageHasProductPeer::NEWSLETTER_DRAFT_MESSAGE_ID, $message->getId());
|
|
$c->addJoin(ProductPeer::ID, NewsletterDraftMessageHasProductPeer::PRODUCT_ID);
|
|
$c->addAscendingOrderByColumn(NewsletterDraftMessageHasProductPeer::ID);
|
|
$products = ProductPeer::doSelect($c);
|
|
|
|
$c = new Criteria();
|
|
$c->add(NewsletterDraftMessageHasBlogPeer::NEWSLETTER_DRAFT_MESSAGE_ID, $message->getId());
|
|
$c->addJoin(BlogPeer::ID, NewsletterDraftMessageHasBlogPeer::BLOG_ID);
|
|
$c->addAscendingOrderByColumn(NewsletterDraftMessageHasBlogPeer::ID);
|
|
$recommends = BlogPeer::doSelect($c);
|
|
|
|
}else{
|
|
|
|
$c = new Criteria();
|
|
$c->add(NewsletterMessageHasProductPeer::NEWSLETTER_MESSAGE_ID, $message->getId());
|
|
$c->addJoin(ProductPeer::ID, NewsletterMessageHasProductPeer::PRODUCT_ID);
|
|
$c->addAscendingOrderByColumn(NewsletterMessageHasProductPeer::ID);
|
|
$products = ProductPeer::doSelect($c);
|
|
|
|
$c = new Criteria();
|
|
$c->add(NewsletterMessageHasBlogPeer::NEWSLETTER_MESSAGE_ID, $message->getId());
|
|
$c->addJoin(BlogPeer::ID, NewsletterMessageHasBlogPeer::BLOG_ID);
|
|
$c->addAscendingOrderByColumn(NewsletterMessageHasBlogPeer::ID);
|
|
$recommends = BlogPeer::doSelect($c);
|
|
|
|
}
|
|
|
|
|
|
$results_products = array();
|
|
|
|
$row = array();
|
|
|
|
foreach ($products as $product) {
|
|
|
|
$product_url = st_url_for('stProduct/show?url='.$product->getFriendlyUrl(), true, 'frontend', null, sfContext::getInstance()->getUser()->getCulture());
|
|
|
|
if (!$preview && !$draft) {
|
|
$product_url = stNewsletter::convertToShopLink($product_url, $message->getHash());
|
|
}
|
|
|
|
$row['id'] = $product->getId();
|
|
|
|
$row['name'] = $product->getName();
|
|
|
|
$row['url'] = $product_url;
|
|
|
|
$row['image'] = $http_https.sfContext::getInstance()->getRequest()->getHost().st_product_image_path($product, 'large');
|
|
|
|
$row['link'] = st_link_to($product->getName(), $product_url);
|
|
|
|
$results_products[] = $row;
|
|
|
|
}
|
|
|
|
$results_recommends = array();
|
|
|
|
$row = array();
|
|
|
|
foreach ($recommends as $blog) {
|
|
|
|
$blog_url = st_url_for('stBlogFrontend/show?url='.$blog->getFriendlyUrl(), true, 'frontend', null, sfContext::getInstance()->getUser()->getCulture());
|
|
|
|
if (!$preview && !$draft) {
|
|
$blog_url = stNewsletter::convertToShopLink($blog_url, $message->getHash());
|
|
}
|
|
|
|
$row['id'] = $blog->getId();
|
|
|
|
$row['name'] = $blog->getName();
|
|
|
|
if ($blog->getAlternativeUrl()) {
|
|
$row['url'] = $blog->getAlternativeUrl();
|
|
} else {
|
|
$row['url'] = $blog_url;
|
|
}
|
|
|
|
$chart_limit = 200;
|
|
|
|
if (!$blog->getShortDescription() || $blog->getShortDescription()=="") {
|
|
$row['description'] = Blog::trim_post($blog->getLongDescription(), $chart_limit);
|
|
} else {
|
|
$row['description'] = Blog::trim_post($blog->getShortDescription(), $chart_limit);
|
|
}
|
|
$row['description'] = Blog::trim_post($blog->getLongDescription(), $chart_limit);
|
|
|
|
if ($config->get('date')==2) {
|
|
|
|
if ($blog->getCreatedAt()) {
|
|
$date = explode(" ", $blog->getCreatedAt());
|
|
|
|
$date = explode("-", $date[0]);
|
|
|
|
$row['date'] = $date[2]."-".$date[1]."-".$date[0];
|
|
} else {
|
|
$row['date'] = "";
|
|
}
|
|
|
|
|
|
} elseif ($config->get('date')==1) {
|
|
|
|
$date = explode(" ", $blog->getUpdatedAt());
|
|
|
|
$date = explode("-", $date[0]);
|
|
|
|
$row['date'] = $date[2]."-".$date[1]."-".$date[0];
|
|
|
|
} else {
|
|
|
|
$row['date'] = NULL;
|
|
}
|
|
|
|
$row['image'] = $http_https.sfContext::getInstance()->getRequest()->getHost().st_asset_image_path($blog->getImagePath(), 'thumb', 'blog');
|
|
|
|
|
|
$results_recommends[] = $row;
|
|
|
|
}
|
|
|
|
$params = array(
|
|
'config' => $config,
|
|
'products' => $results_products,
|
|
'recommends' => $results_recommends,
|
|
'message' => $message
|
|
);
|
|
|
|
$templates_full = $view->render($params);
|
|
|
|
$content = $message->getContent();
|
|
|
|
$content = stNewsletter::convertToShopContent($content,$config);
|
|
|
|
$templates = $templates_full;
|
|
|
|
if ($preview || $draft) {
|
|
|
|
$templates = explode('<!-- PREV -->', $templates_full);
|
|
$templates = $templates[1];
|
|
$hash_url = "/";
|
|
|
|
}else{
|
|
|
|
$hash_url = $http_https.sfContext::getInstance()->getRequest()->getHost()."/newsletter/countReadMessage?hash=".$message->getHash();
|
|
$content = stNewsletter::convertLink($content, $message->getHash(), $http_https);
|
|
|
|
}
|
|
|
|
if (!$newsletter_logo) {
|
|
$newsletter_logo = stTheme::getImagePath("logo.png");
|
|
}
|
|
|
|
if (!isset($call_to_action)) {
|
|
|
|
|
|
if ($preview || $draft) {
|
|
$cta_head_description = $message->getCtaDescriptionHead();
|
|
$cta_foot_description = $message->getCtaDescriptionFoot();
|
|
$cta_url = $message->getCtaButtonUrl();
|
|
}else{
|
|
$cta_head_description = stNewsletter::convertLink($message->getCtaDescriptionHead(), $message->getHash(), $http_https);
|
|
$cta_foot_description = stNewsletter::convertLink($message->getCtaDescriptionFoot(), $message->getHash(), $http_https);
|
|
$cta_url = stNewsletter::convertToShopLink($message->getCtaButtonUrl(), $message->getHash());
|
|
}
|
|
|
|
$cta_head_description = stNewsletter::convertToShopContent($cta_head_description,$config);
|
|
$cta_foot_description = stNewsletter::convertToShopContent($cta_foot_description,$config);
|
|
|
|
$call_to_action = '<div style="margin: 0;padding: 0;font-family: Arial, sans-serif;font-size: 14px;font-weight: normal;line-height: 1.6;">';
|
|
$call_to_action .= $cta_head_description;
|
|
$call_to_action .= '<div style="text-align:'.$message->getCtaButtonPosition().'">';
|
|
$call_to_action .= '<a href="'.$cta_url.'" style="display: inline-block; background-color: #'.$template_cta_background.'; margin-bottom:10px; padding: 12px 24px; font-family: Arial, sans-serif;font-size: 14px;color: #'.$template_cta_font.';font-weight: bold; text-decoration: none;" target="_blank">'.$message->getCtaButtonName().'</a>';
|
|
$call_to_action .= '</div>';
|
|
$call_to_action .= $cta_foot_description;
|
|
$call_to_action .= '</div>';
|
|
}
|
|
|
|
|
|
$templates = preg_replace('/{HOST}/', sfContext::getInstance()->getRequest()->getHost(), $templates);
|
|
$templates = preg_replace('/{HASH}/', $hash_url, $templates);
|
|
$templates = preg_replace('/{NEWSLETTER_CONTENT}/', $content, $templates);
|
|
$templates = preg_replace('/{SUBJECT}/', $message->getSubject(), $templates);
|
|
$templates = preg_replace('/{READ_MORE}/', $i18n->__('więcej'), $templates);
|
|
|
|
$templates = preg_replace('/{MAIN_IMAGE}/', $http_https.sfContext::getInstance()->getRequest()->getHost()."/".$message->getMainImage(), $templates);
|
|
|
|
if (!$preview && !$draft) {
|
|
$templates = preg_replace('/{MAIN_IMAGE_URL}/', stNewsletter::convertToShopLink($message->getMainImageUrl(), $message->getHash()), $templates);
|
|
}else{
|
|
$templates = preg_replace('/{MAIN_IMAGE_URL}/', $message->getMainImageUrl(), $templates);
|
|
}
|
|
|
|
if ($shop) {
|
|
$templates = preg_replace('/{LINK_TO_CANTREAD}/', "#", $templates);
|
|
$templates = preg_replace('/{LINK_TO_UNSUBSCRIBE}/', "#", $templates);
|
|
}
|
|
|
|
$templates = preg_replace('/{LINK_TO_UNSUBSCRIBE}/', $http_https.sfContext::getInstance()->getRequest()->getHost().'/newsletter/unsubscribe', $templates);
|
|
$templates = preg_replace('/{TEXT_TO_UNSUBSCRIBE}/', $i18n->__('Wypisz mnie z listy'), $templates);
|
|
$templates = preg_replace('/{UNSUBSCRIBE_MESSAGE}/', $i18n->__('Jeżeli chcą Państwo zrezygnować z otrzymywania wiadomości proszę kliknąć w poniższy link.'), $templates);
|
|
|
|
|
|
$templates = preg_replace('/{LINK_TO_CANTREAD}/', $http_https.sfContext::getInstance()->getRequest()->getHost().'/newsletter/showMessage?hash='.$message->getHash(), $templates);
|
|
$templates = preg_replace('/{TEXT_TO_CANTREAD}/', $i18n->__('Zobacz wiadomość w sklepie'), $templates);
|
|
|
|
$templates = preg_replace('/{CANTREAD_MESSAGE}/', $i18n->__('Jeśli nie możesz odczytać wiadomość.'), $templates);
|
|
|
|
$templates = preg_replace('/{DATE}/', date("d-m-Y"), $templates);
|
|
$templates = preg_replace('/{LOGO_SRC}/', $http_https.sfContext::getInstance()->getRequest()->getHost()."/".$newsletter_logo, $templates);
|
|
|
|
$templates = preg_replace('/{CALL_TO_ACTION}/', $call_to_action, $templates);
|
|
$templates = preg_replace('/{CTA}/', $call_to_action, $templates);
|
|
|
|
|
|
$templates = preg_replace('/{template_color_header}/', $template_color_header, $templates);
|
|
$templates = preg_replace('/{template_color_background_a}/', $template_color_background_a, $templates);
|
|
$templates = preg_replace('/{template_color_background_b}/', $template_color_background_b, $templates);
|
|
$templates = preg_replace('/{template_color_background_font}/', $template_color_background_font, $templates);
|
|
$templates = preg_replace('/{template_color_font_a}/', $template_color_font_a, $templates);
|
|
$templates = preg_replace('/{template_color_font_b}/', $template_color_font_b, $templates);
|
|
$templates = preg_replace('/{template_color_font_c}/', $template_color_font_c, $templates);
|
|
|
|
|
|
return $templates;
|
|
}
|
|
|
|
|
|
public static function convertLink($string, $message_hash, $http_https)
|
|
{
|
|
$new_href = $http_https.sfContext::getInstance()->getRequest()->getHost()."?nh=".$message_hash."&u=";
|
|
|
|
$doc = new DOMDocument;
|
|
$doc->loadHTML(mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8'));
|
|
|
|
foreach ($doc->getElementsByTagName('a') as $link) {
|
|
|
|
$old_link = $link->getAttribute('href');
|
|
|
|
$link->setAttribute('href', $new_href.$old_link);
|
|
|
|
}
|
|
|
|
return $doc->saveHTML();
|
|
}
|
|
|
|
|
|
public static function convertToShopLink($old_link, $message_hash)
|
|
{
|
|
$security_config = stConfig::getInstance(sfContext::getInstance(), 'stSecurityBackend');
|
|
|
|
if ($security_config->get('ssl', null, true) != '') {
|
|
$http_https = "https://";
|
|
} else {
|
|
$http_https = "http://";
|
|
}
|
|
|
|
$new_href = $http_https.sfContext::getInstance()->getRequest()->getHost()."?nh=".$message_hash."&u=";
|
|
|
|
return $new_href.$old_link;
|
|
}
|
|
|
|
public static function convertToShopContent($content, $config)
|
|
{
|
|
$template_color_font_b = $config->get('template_color_font_b');
|
|
$template_color_font_c = $config->get('template_color_font_c');
|
|
|
|
$link_pattern = '<a style="color:#'.$template_color_font_c.'"';
|
|
|
|
|
|
$p1 = '<p style="margin:0px 0px 10px 0px;padding: 0;';
|
|
$p2 = '<p style="margin:0px 0px 10px 0px;padding: 0;">';
|
|
|
|
$link_h1 = '<h1 style="margin: 0;padding: 0; font-family: Arial, sans-serif;line-height: 1.4; font-size: 24px; font-weight: 500; color:#'.$template_color_font_b.'"';
|
|
$link_h2 = '<h2 style="margin: 0;padding: 0; font-family: Arial, sans-serif;line-height: 1.4; font-size: 20px; font-weight: 500; color:#'.$template_color_font_b.'"';
|
|
$link_h3 = '<h3 style="margin: 0;padding: 0; font-family: Arial, sans-serif;line-height: 1.4; font-size: 18px; font-weight: 500; color:#'.$template_color_font_b.'"';
|
|
$link_h4 = '<h4 style="margin: 0;padding: 0; font-family: Arial, sans-serif;line-height: 1.4; font-size: 16px; font-weight: 500; color:#'.$template_color_font_b.'"';
|
|
$link_h5 = '<h5 style="margin: 0;padding: 0; font-family: Arial, sans-serif;line-height: 1.4; font-size: 16px; font-weight: 500; color:#'.$template_color_font_b.'"';
|
|
$link_h6 = '<h6 style="margin: 0;padding: 0; font-family: Arial, sans-serif;line-height: 1.4; font-size: 16px; font-weight: 500; color:#'.$template_color_font_b.'"';
|
|
|
|
$content = preg_replace('/<p style="/', $p1, $content);
|
|
$content = preg_replace('/<p>/', $p2, $content);
|
|
|
|
$content = preg_replace('/<a/', $link_pattern, $content);
|
|
$content = preg_replace('/<h1/', $link_h1, $content);
|
|
$content = preg_replace('/<h2/', $link_h2, $content);
|
|
$content = preg_replace('/<h3/', $link_h3, $content);
|
|
$content = preg_replace('/<h4/', $link_h4, $content);
|
|
$content = preg_replace('/<h5/', $link_h5, $content);
|
|
$content = preg_replace('/<h6/', $link_h6, $content);
|
|
|
|
return $content;
|
|
}
|
|
|
|
}
|