modules/XC/MailChimp/src/Model/MailChimpList.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XC\MailChimp\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * MailChimp mail list
  10.  *
  11.  * @ORM\Entity (repositoryClass="\XC\MailChimp\Model\Repo\MailChimpList")
  12.  * @ORM\Table  (name="mailchimp_lists")
  13.  */
  14. class MailChimpList extends \XLite\Model\AEntity
  15. {
  16.     /**
  17.      * List ID
  18.      *
  19.      * @var string
  20.      *
  21.      * @ORM\Id
  22.      * @ORM\Column (type="string", length=32)
  23.      */
  24.     protected $id '';
  25.     /**
  26.      * List name
  27.      *
  28.      * @var string
  29.      *
  30.      * @ORM\Column (type="string", length=128)
  31.      */
  32.     protected $name '';
  33.     /**
  34.      * List creation date
  35.      *
  36.      * @var string
  37.      *
  38.      * @ORM\Column (type="string", length=128)
  39.      */
  40.     protected $date_created '';
  41.     /**
  42.      * Last update date
  43.      *
  44.      * @var integer
  45.      *
  46.      * @ORM\Column (type="integer")
  47.      */
  48.     protected $date_updated '';
  49.     /**
  50.      * List rating
  51.      *
  52.      * @var float
  53.      *
  54.      * @ORM\Column (type="decimal", precision=3, scale=2)
  55.      */
  56.     protected $list_rating 0.0;
  57.     /**
  58.      * List subscribe URL short
  59.      *
  60.      * @var string
  61.      *
  62.      * @ORM\Column (type="string", length=128)
  63.      */
  64.     protected $subscribe_url_short '';
  65.     /**
  66.      * List subscribe URL long
  67.      *
  68.      * @var string
  69.      *
  70.      * @ORM\Column (type="string", length=128)
  71.      */
  72.     protected $subscribe_url_long '';
  73.     /**
  74.      * List member count
  75.      *
  76.      * @var integer
  77.      *
  78.      * @ORM\Column (type="integer")
  79.      */
  80.     protected $member_count 0;
  81.     /**
  82.      * List mail open rate
  83.      *
  84.      * @var float
  85.      *
  86.      * @ORM\Column (type="decimal", precision=5, scale=2)
  87.      */
  88.     protected $open_rate 0.0;
  89.     /**
  90.      * List mail click rate
  91.      *
  92.      * @var float
  93.      *
  94.      * @ORM\Column (type="decimal", precision=5, scale=2)
  95.      */
  96.     protected $click_rate 0.0;
  97.     /**
  98.      * Profiles
  99.      *
  100.      * @var \XLite\Model\Profile[]
  101.      *
  102.      * @ORM\ManyToMany (targetEntity="XLite\Model\Profile", inversedBy="mail_chimp_lists")
  103.      * @ORM\JoinTable  (
  104.      *      name="mailchimp_subscriptions",
  105.      *      joinColumns={@ORM\JoinColumn(name="list_id", referencedColumnName="id", onDelete="CASCADE")},
  106.      *      inverseJoinColumns={@ORM\JoinColumn(name="profile_id", referencedColumnName="profile_id", onDelete="CASCADE")}
  107.      * )
  108.      */
  109.     protected $profiles;
  110.     /**
  111.      * Segments
  112.      *
  113.      * @var \XC\MailChimp\Model\MailChimpSegment[]
  114.      *
  115.      * @ORM\OneToMany (targetEntity="XC\MailChimp\Model\MailChimpSegment", mappedBy="list", cascade={"all"})
  116.      */
  117.     protected $segments;
  118.     /**
  119.      * Groups
  120.      *
  121.      * @var \Doctrine\Common\Collections\Collection
  122.      *
  123.      * @ORM\OneToMany (targetEntity="XC\MailChimp\Model\MailChimpGroup", mappedBy="list", cascade={"all"})
  124.      */
  125.     protected $groups;
  126.     /**
  127.      * Enabled
  128.      *
  129.      * @var boolean
  130.      *
  131.      * @ORM\Column (type="boolean")
  132.      */
  133.     protected $enabled true;
  134.     /**
  135.      * Subscribe by default
  136.      *
  137.      * @var boolean
  138.      *
  139.      * @ORM\Column (type="boolean")
  140.      */
  141.     protected $subscribeByDefault true;
  142.     /**
  143.      * Defines if the list was removed from MailChimp
  144.      *
  145.      * @var boolean
  146.      *
  147.      * @ORM\Column (type="boolean")
  148.      */
  149.     protected $isRemoved false;
  150.     /**
  151.      * Qty in stock
  152.      *
  153.      * @var \XC\MailChimp\Model\Store
  154.      *
  155.      * @ORM\OneToOne (targetEntity="XC\MailChimp\Model\Store", mappedBy="list", fetch="LAZY", cascade={"all"})
  156.      */
  157.     protected $store;
  158.     /**
  159.      * Constructor
  160.      *
  161.      * @param array $data Entity properties OPTIONAL
  162.      */
  163.     public function __construct(array $data = [])
  164.     {
  165.         $this->profiles     = new \Doctrine\Common\Collections\ArrayCollection();
  166.         $this->segments     = new \Doctrine\Common\Collections\ArrayCollection();
  167.         $this->groups       = new \Doctrine\Common\Collections\ArrayCollection();
  168.         parent::__construct($data);
  169.     }
  170.     /**
  171.      * Check if provided profile is subscribes to this list
  172.      *
  173.      * @param \XLite\Model\Profile|null $profile Profile
  174.      *
  175.      * @return boolean
  176.      */
  177.     public function isProfileSubscribed($profile)
  178.     {
  179.         return isset($profile)
  180.             ? \XLite\Core\Database::getRepo('XC\MailChimp\Model\MailChimpList')
  181.                 ->isProfileSubscribed($this$profile)
  182.             : $this->getSubscribeByDefault();
  183.     }
  184.     /**
  185.      * Subscribe profile to list
  186.      *
  187.      * @param \XLite\Model\Profile $profile Profile
  188.      *
  189.      * @return void
  190.      *
  191.      * @throws \Mailchimp_Error
  192.      */
  193.     public function doProfileSubscribe(\XLite\Model\Profile $profile)
  194.     {
  195.         if (!$this->isProfileSubscribed($profile)) {
  196.             $firstName '';
  197.             $lastName '';
  198.             if ($profile->getFirstAddress()) {
  199.                 $firstName $profile->getFirstAddress()->getFirstname();
  200.                 $lastName $profile->getFirstAddress()->getLastname();
  201.             }
  202.             try {
  203.                 \XC\MailChimp\Core\MailChimp::getInstance()->doSubscribe(
  204.                     $this->getId(),
  205.                     $profile->getLogin(),
  206.                     $firstName,
  207.                     $lastName
  208.                 );
  209.             } catch (\Mailchimp_Error $e) {
  210.                 throw $e;
  211.             }
  212.             $profileComment $profile->getStatusComment();
  213.             if ($profileComment !== $profile::MAILCHIMP_TEMP_PROFILE_COMMENT) {
  214.                 $this->addProfiles($profile);
  215.             }
  216.             $profile->addMailChimpLists($this);
  217.             \XLite\Core\Database::getEM()->persist($this);
  218.             \XLite\Core\Database::getEM()->flush();
  219.         }
  220.     }
  221.     /**
  222.      * Unsubscribe profile to list
  223.      *
  224.      * @param \XLite\Model\Profile $profile Profile
  225.      *
  226.      * @return void
  227.      *
  228.      * @throws \Mailchimp_Error
  229.      */
  230.     public function doProfileUnsubscribe(\XLite\Model\Profile $profile)
  231.     {
  232.         if ($this->isProfileSubscribed($profile)) {
  233.             try {
  234.                 \XC\MailChimp\Core\MailChimp::getInstance()->doUnsubscribe(
  235.                     $this->getId(),
  236.                     $profile->getLogin()
  237.                 );
  238.             } catch (\Mailchimp_Error $e) {
  239.                 if ($e->getCode() != '215') {
  240.                     // Code '215' - email is not subscribed to the list, ignore error.
  241.                     // Otherwise throw an exception
  242.                     throw $e;
  243.                 }
  244.             }
  245.             $this->getProfiles()->removeElement($profile);
  246.             $profile->getMailChimpLists()->removeElement($profile);
  247.             \XLite\Core\Database::getEM()->persist($this);
  248.             \XLite\Core\Database::getEM()->flush();
  249.         }
  250.     }
  251.     /**
  252.      * Set id
  253.      *
  254.      * @param string $id
  255.      * @return MailChimpList
  256.      */
  257.     public function setId($id)
  258.     {
  259.         $this->id $id;
  260.         return $this;
  261.     }
  262.     /**
  263.      * Get id
  264.      *
  265.      * @return string
  266.      */
  267.     public function getId()
  268.     {
  269.         return $this->id;
  270.     }
  271.     /**
  272.      * Set name
  273.      *
  274.      * @param string $name
  275.      * @return MailChimpList
  276.      */
  277.     public function setName($name)
  278.     {
  279.         $this->name $name;
  280.         return $this;
  281.     }
  282.     /**
  283.      * Get name
  284.      *
  285.      * @return string
  286.      */
  287.     public function getName()
  288.     {
  289.         return $this->name;
  290.     }
  291.     /**
  292.      * Set date_created
  293.      *
  294.      * @param string $dateCreated
  295.      * @return MailChimpList
  296.      */
  297.     public function setDateCreated($dateCreated)
  298.     {
  299.         $this->date_created $dateCreated;
  300.         return $this;
  301.     }
  302.     /**
  303.      * Get date_created
  304.      *
  305.      * @return string
  306.      */
  307.     public function getDateCreated()
  308.     {
  309.         return $this->date_created;
  310.     }
  311.     /**
  312.      * Set date_updated
  313.      *
  314.      * @param integer $dateUpdated
  315.      * @return MailChimpList
  316.      */
  317.     public function setDateUpdated($dateUpdated)
  318.     {
  319.         $this->date_updated $dateUpdated;
  320.         return $this;
  321.     }
  322.     /**
  323.      * Get date_updated
  324.      *
  325.      * @return integer
  326.      */
  327.     public function getDateUpdated()
  328.     {
  329.         return $this->date_updated;
  330.     }
  331.     /**
  332.      * Set list_rating
  333.      *
  334.      * @param float $listRating
  335.      * @return MailChimpList
  336.      */
  337.     public function setListRating($listRating)
  338.     {
  339.         $this->list_rating $listRating;
  340.         return $this;
  341.     }
  342.     /**
  343.      * Get list_rating
  344.      *
  345.      * @return float
  346.      */
  347.     public function getListRating()
  348.     {
  349.         return $this->list_rating;
  350.     }
  351.     /**
  352.      * Set subscribe_url_short
  353.      *
  354.      * @param string $subscribeUrlShort
  355.      * @return MailChimpList
  356.      */
  357.     public function setSubscribeUrlShort($subscribeUrlShort)
  358.     {
  359.         $this->subscribe_url_short $subscribeUrlShort;
  360.         return $this;
  361.     }
  362.     /**
  363.      * Get subscribe_url_short
  364.      *
  365.      * @return string
  366.      */
  367.     public function getSubscribeUrlShort()
  368.     {
  369.         return $this->subscribe_url_short;
  370.     }
  371.     /**
  372.      * Set subscribe_url_long
  373.      *
  374.      * @param string $subscribeUrlLong
  375.      * @return MailChimpList
  376.      */
  377.     public function setSubscribeUrlLong($subscribeUrlLong)
  378.     {
  379.         $this->subscribe_url_long $subscribeUrlLong;
  380.         return $this;
  381.     }
  382.     /**
  383.      * Get subscribe_url_long
  384.      *
  385.      * @return string
  386.      */
  387.     public function getSubscribeUrlLong()
  388.     {
  389.         return $this->subscribe_url_long;
  390.     }
  391.     /**
  392.      * Set member_count
  393.      *
  394.      * @param integer $memberCount
  395.      * @return MailChimpList
  396.      */
  397.     public function setMemberCount($memberCount)
  398.     {
  399.         $this->member_count $memberCount;
  400.         return $this;
  401.     }
  402.     /**
  403.      * Get member_count
  404.      *
  405.      * @return integer
  406.      */
  407.     public function getMemberCount()
  408.     {
  409.         return $this->member_count;
  410.     }
  411.     /**
  412.      * Set open_rate
  413.      *
  414.      * @param float $openRate
  415.      * @return MailChimpList
  416.      */
  417.     public function setOpenRate($openRate)
  418.     {
  419.         $this->open_rate $openRate;
  420.         return $this;
  421.     }
  422.     /**
  423.      * Get open_rate
  424.      *
  425.      * @return float
  426.      */
  427.     public function getOpenRate()
  428.     {
  429.         return $this->open_rate;
  430.     }
  431.     /**
  432.      * Set click_rate
  433.      *
  434.      * @param float $clickRate
  435.      * @return MailChimpList
  436.      */
  437.     public function setClickRate($clickRate)
  438.     {
  439.         $this->click_rate $clickRate;
  440.         return $this;
  441.     }
  442.     /**
  443.      * Get click_rate
  444.      *
  445.      * @return float
  446.      */
  447.     public function getClickRate()
  448.     {
  449.         return $this->click_rate;
  450.     }
  451.     /**
  452.      * Set enabled
  453.      *
  454.      * @param boolean $enabled
  455.      * @return MailChimpList
  456.      */
  457.     public function setEnabled($enabled)
  458.     {
  459.         $this->enabled = (bool)$enabled;
  460.         return $this;
  461.     }
  462.     /**
  463.      * Get enabled
  464.      *
  465.      * @return boolean
  466.      */
  467.     public function getEnabled()
  468.     {
  469.         return $this->enabled;
  470.     }
  471.     /**
  472.      * Set subscribeByDefault
  473.      *
  474.      * @param boolean $subscribeByDefault
  475.      * @return MailChimpList
  476.      */
  477.     public function setSubscribeByDefault($subscribeByDefault)
  478.     {
  479.         $this->subscribeByDefault $subscribeByDefault;
  480.         return $this;
  481.     }
  482.     /**
  483.      * Get subscribeByDefault
  484.      *
  485.      * @return boolean
  486.      */
  487.     public function getSubscribeByDefault()
  488.     {
  489.         return $this->subscribeByDefault;
  490.     }
  491.     /**
  492.      * Set isRemoved
  493.      *
  494.      * @param boolean $isRemoved
  495.      * @return MailChimpList
  496.      */
  497.     public function setIsRemoved($isRemoved)
  498.     {
  499.         $this->isRemoved $isRemoved;
  500.         return $this;
  501.     }
  502.     /**
  503.      * Get isRemoved
  504.      *
  505.      * @return boolean
  506.      */
  507.     public function getIsRemoved()
  508.     {
  509.         return $this->isRemoved;
  510.     }
  511.     /**
  512.      * Add profiles
  513.      *
  514.      * @param \XLite\Model\Profile $profiles
  515.      * @return MailChimpList
  516.      */
  517.     public function addProfiles(\XLite\Model\Profile $profiles)
  518.     {
  519.         $this->profiles[] = $profiles;
  520.         return $this;
  521.     }
  522.     /**
  523.      * Get profiles
  524.      *
  525.      * @return \Doctrine\Common\Collections\Collection
  526.      */
  527.     public function getProfiles()
  528.     {
  529.         return $this->profiles;
  530.     }
  531.     /**
  532.      * Add segments
  533.      *
  534.      * @param \XC\MailChimp\Model\MailChimpSegment $segments
  535.      * @return MailChimpList
  536.      */
  537.     public function addSegments(\XC\MailChimp\Model\MailChimpSegment $segments)
  538.     {
  539.         $this->segments[] = $segments;
  540.         return $this;
  541.     }
  542.     /**
  543.      * Get segments
  544.      *
  545.      * @return \Doctrine\Common\Collections\Collection|\XC\MailChimp\Model\MailChimpSegment[]
  546.      */
  547.     public function getSegments()
  548.     {
  549.         return $this->segments;
  550.     }
  551.     /**
  552.      * Add groups
  553.      *
  554.      * @param \XC\MailChimp\Model\MailChimpGroup $groups
  555.      * @return MailChimpList
  556.      */
  557.     public function addGroups(\XC\MailChimp\Model\MailChimpGroup $groups)
  558.     {
  559.         $this->groups[] = $groups;
  560.         return $this;
  561.     }
  562.     /**
  563.      * Get groups
  564.      *
  565.      * @return \Doctrine\Common\Collections\Collection
  566.      */
  567.     public function getGroups()
  568.     {
  569.         return $this->groups;
  570.     }
  571.     /**
  572.      * Get enabled groups
  573.      *
  574.      * @return \Doctrine\Common\Collections\Collection
  575.      */
  576.     public function getEnabledGroups()
  577.     {
  578.         return $this->getGroups()->filter(static function ($group) {
  579.             return $group->getEnabled();
  580.         });
  581.     }
  582.     /**
  583.      * @return Store
  584.      */
  585.     public function getStore()
  586.     {
  587.         return $this->store;
  588.     }
  589.     /**
  590.      * @param Store $store
  591.      */
  592.     public function setStore($store)
  593.     {
  594.         $this->store $store;
  595.         return $this;
  596.     }
  597. }