modules/XC/MailChimp/src/Model/MailChimpSegment.php line 20

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. use XC\MailChimp\Core;
  9. use XC\MailChimp\Core\Request\Segment;
  10. /**
  11.  * MailChimp mail list
  12.  *
  13.  * @ORM\Entity (repositoryClass="\XC\MailChimp\Model\Repo\MailChimpSegment")
  14.  * @ORM\Table  (name="mailchimp_list_segments")
  15.  */
  16. class MailChimpSegment extends \XLite\Model\AEntity
  17. {
  18.     /**
  19.      * List ID
  20.      *
  21.      * @var string
  22.      *
  23.      * @ORM\Id
  24.      * @ORM\Column (type="string", length=32)
  25.      */
  26.     protected $id '';
  27.     /**
  28.      * List name
  29.      *
  30.      * @var string
  31.      *
  32.      * @ORM\Column (type="string", length=128)
  33.      */
  34.     protected $name '';
  35.     /**
  36.      * List creation date
  37.      *
  38.      * @var string
  39.      *
  40.      * @ORM\Column (type="string", length=128)
  41.      */
  42.     protected $created_date '';
  43.     /**
  44.      * Is static MailChimp segment
  45.      *
  46.      * @var boolean
  47.      *
  48.      * @ORM\Column (type="boolean")
  49.      */
  50.     protected $static false;
  51.     /**
  52.      * MailChimp parent list
  53.      *
  54.      * @var \XC\MailChimp\Model\MailChimpList
  55.      *
  56.      * @ORM\ManyToOne (targetEntity="XC\MailChimp\Model\MailChimpList", inversedBy="segments")
  57.      */
  58.     protected $list;
  59.     /**
  60.      * Use amount of orders as condition
  61.      *
  62.      * @var boolean
  63.      *
  64.      * @ORM\Column (type="boolean")
  65.      */
  66.     protected $useOrdersLastMonth false;
  67.     /**
  68.      * Amount of orders last month
  69.      *
  70.      * @var integer
  71.      *
  72.      * @ORM\Column (type="integer")
  73.      */
  74.     protected $ordersLastMonth 0;
  75.     /**
  76.      * Use total amount of all orders as condition
  77.      *
  78.      * @var boolean
  79.      *
  80.      * @ORM\Column (type="boolean")
  81.      */
  82.     protected $useOrderAmount false;
  83.     /**
  84.      * Total for all orders
  85.      *
  86.      * @var float
  87.      *
  88.      * @ORM\Column (type="decimal", precision=14, scale=4)
  89.      */
  90.     protected $orderAmount 0.0;
  91.     /**
  92.      * Use memberships as condition
  93.      *
  94.      * @var boolean
  95.      *
  96.      * @ORM\Column (type="boolean")
  97.      */
  98.     protected $useMemberships false;
  99.     /**
  100.      * Memberships
  101.      *
  102.      * @var \XLite\Model\Membership[]
  103.      *
  104.      * @ORM\ManyToMany (targetEntity="\XLite\Model\Membership")
  105.      * @ORM\JoinTable (name="segment_membership",
  106.      *      joinColumns={@ORM\JoinColumn(name="segment_id", referencedColumnName="id", onDelete="CASCADE")},
  107.      *      inverseJoinColumns={@ORM\JoinColumn(name="membership_id", referencedColumnName="membership_id", onDelete="CASCADE")}
  108.      * )
  109.      */
  110.     protected $memberships;
  111.     /**
  112.      * Use specified products as condition
  113.      *
  114.      * @var boolean
  115.      *
  116.      * @ORM\Column (type="boolean")
  117.      */
  118.     protected $useProducts false;
  119.     /**
  120.      * Products
  121.      *
  122.      * @var \XLite\Model\Product[]
  123.      *
  124.      * @ORM\ManyToMany (targetEntity="\XLite\Model\Product")
  125.      * @ORM\JoinTable (name="segment_products",
  126.      *      joinColumns={@ORM\JoinColumn(name="segment_id", referencedColumnName="id", onDelete="CASCADE")},
  127.      *      inverseJoinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="product_id", onDelete="CASCADE")}
  128.      * )
  129.      */
  130.     protected $products;
  131.     /**
  132.      * Profiles
  133.      *
  134.      * @var \XLite\Model\Profile[]
  135.      *
  136.      * @ORM\ManyToMany (targetEntity="XLite\Model\Profile", inversedBy="mail_chimp_segments")
  137.      * @ORM\JoinTable  (
  138.      *      name="mailchimp_segment_subscriptions",
  139.      *      joinColumns={@ORM\JoinColumn(name="segment_id", referencedColumnName="id", onDelete="CASCADE")},
  140.      *      inverseJoinColumns={@ORM\JoinColumn(name="profile_id", referencedColumnName="profile_id", onDelete="CASCADE")}
  141.      * )
  142.      */
  143.     protected $profiles;
  144.     /**
  145.      * Enabled
  146.      *
  147.      * @var boolean
  148.      *
  149.      * @ORM\Column (type="boolean")
  150.      */
  151.     protected $enabled true;
  152.     /**
  153.      * Constructor
  154.      *
  155.      * @param array $data Entity properties OPTIONAL
  156.      */
  157.     public function __construct(array $data = [])
  158.     {
  159.         $this->memberships = new \Doctrine\Common\Collections\ArrayCollection();
  160.         $this->products = new \Doctrine\Common\Collections\ArrayCollection();
  161.         $this->profiles = new \Doctrine\Common\Collections\ArrayCollection();
  162.         parent::__construct($data);
  163.     }
  164.     /**
  165.      * Check if provided profile is subscribes to this list
  166.      *
  167.      * @param \XLite\Model\Profile $profile Profile
  168.      *
  169.      * @return boolean
  170.      */
  171.     public function isProfileSubscribed(\XLite\Model\Profile $profile)
  172.     {
  173.         return isset($profile)
  174.             ? \XLite\Core\Database::getRepo('XC\MailChimp\Model\MailChimpSegment')
  175.                 ->isProfileSubscribed($this$profile)
  176.             : false;
  177.     }
  178.     /**
  179.      * Check if profile matches segment conditions
  180.      *
  181.      * @param \XLite\Model\Profile $profile Profile
  182.      *
  183.      * @return boolean
  184.      */
  185.     public function checkProfileConditions(\XLite\Model\Profile $profile)
  186.     {
  187.         $return true;
  188.         $conditionsEnabled false;
  189.         if ($this->getUseOrdersLastMonth()) {
  190.             $return $return && $this->checkOrdersLastMonthCondition($profile);
  191.             $conditionsEnabled true;
  192.         }
  193.         if ($this->getUseOrderAmount()) {
  194.             $return $return && $this->checkOrderAmountCondition($profile);
  195.             $conditionsEnabled true;
  196.         }
  197.         if ($this->getUseMemberships()) {
  198.             $return $return && $this->checkMembershipsCondition($profile);
  199.             $conditionsEnabled true;
  200.         }
  201.         if ($this->getUseProducts()) {
  202.             $return $return && $this->checkProductsCondition($profile);
  203.             $conditionsEnabled true;
  204.         }
  205.         return $conditionsEnabled && $return;
  206.     }
  207.     /**
  208.      * Check amount of orders for last 30 days
  209.      *
  210.      * @param \XLite\Model\Profile $profile Profile
  211.      *
  212.      * @return boolean
  213.      */
  214.     public function checkOrdersLastMonthCondition(\XLite\Model\Profile $profile)
  215.     {
  216.         return \XLite\Core\Database::getRepo('XLite\Model\Profile')->countOrdersLastMonth($profile)
  217.             >= $this->getOrdersLastMonth();
  218.     }
  219.     /**
  220.      * Check total amount for all orders
  221.      *
  222.      * @param \XLite\Model\Profile $profile Profile
  223.      *
  224.      * @return boolean
  225.      */
  226.     public function checkOrderAmountCondition(\XLite\Model\Profile $profile)
  227.     {
  228.         return \XLite\Core\Database::getRepo('XLite\Model\Profile')->getOrdersTotal($profile);
  229.     }
  230.     /**
  231.      * Check memberships condition
  232.      *
  233.      * @param \XLite\Model\Profile $profile Profile
  234.      *
  235.      * @return boolean
  236.      */
  237.     public function checkMembershipsCondition(\XLite\Model\Profile $profile)
  238.     {
  239.         return \XLite\Core\Database::getRepo('XLite\Model\Profile')->checkProfileMemberships($profile$this);
  240.     }
  241.     /**
  242.      * Check products condition
  243.      *
  244.      * @param \XLite\Model\Profile $profile Profile
  245.      *
  246.      * @return boolean
  247.      */
  248.     public function checkProductsCondition(\XLite\Model\Profile $profile)
  249.     {
  250.         return \XLite\Core\Database::getRepo('XLite\Model\Profile')->checkProductsPurchased($profile$this);
  251.     }
  252.     /**
  253.      * Subscribe profile to this segment
  254.      *
  255.      * @param \XLite\Model\Profile $profile Profile
  256.      *
  257.      * @return void
  258.      */
  259.     public function doProfileSubscribe(\XLite\Model\Profile $profile)
  260.     {
  261.         $this->addProfiles($profile);
  262.         \XLite\Core\Database::getEM()->persist($this);
  263.         \XLite\Core\Database::getEM()->flush();
  264.         try {
  265.             Segment\AddTo::executeAction($this->getList()->getId(), $this->getId(), [$profile->getLogin()]);
  266.         } catch (Core\MailChimpException $e) {
  267.             \XLite\Core\TopMessage::addError($e->getMessage());
  268.         }
  269.     }
  270.     /**
  271.      * Set id
  272.      *
  273.      * @param string $id
  274.      * @return MailChimpSegment
  275.      */
  276.     public function setId($id)
  277.     {
  278.         $this->id $id;
  279.         return $this;
  280.     }
  281.     /**
  282.      * Get id
  283.      *
  284.      * @return string
  285.      */
  286.     public function getId()
  287.     {
  288.         return $this->id;
  289.     }
  290.     /**
  291.      * Set name
  292.      *
  293.      * @param string $name
  294.      * @return MailChimpSegment
  295.      */
  296.     public function setName($name)
  297.     {
  298.         $this->name $name;
  299.         return $this;
  300.     }
  301.     /**
  302.      * Get name
  303.      *
  304.      * @return string
  305.      */
  306.     public function getName()
  307.     {
  308.         return $this->name;
  309.     }
  310.     /**
  311.      * Set created_date
  312.      *
  313.      * @param string $createdDate
  314.      * @return MailChimpSegment
  315.      */
  316.     public function setCreatedDate($createdDate)
  317.     {
  318.         $this->created_date $createdDate;
  319.         return $this;
  320.     }
  321.     /**
  322.      * Get created_date
  323.      *
  324.      * @return string
  325.      */
  326.     public function getCreatedDate()
  327.     {
  328.         return $this->created_date;
  329.     }
  330.     /**
  331.      * Set static
  332.      *
  333.      * @param boolean $static
  334.      * @return MailChimpSegment
  335.      */
  336.     public function setStatic($static)
  337.     {
  338.         $this->static $static;
  339.         return $this;
  340.     }
  341.     /**
  342.      * Get static
  343.      *
  344.      * @return boolean
  345.      */
  346.     public function getStatic()
  347.     {
  348.         return $this->static;
  349.     }
  350.     /**
  351.      * Set useOrdersLastMonth
  352.      *
  353.      * @param boolean $useOrdersLastMonth
  354.      * @return MailChimpSegment
  355.      */
  356.     public function setUseOrdersLastMonth($useOrdersLastMonth)
  357.     {
  358.         $this->useOrdersLastMonth $useOrdersLastMonth;
  359.         return $this;
  360.     }
  361.     /**
  362.      * Get useOrdersLastMonth
  363.      *
  364.      * @return boolean
  365.      */
  366.     public function getUseOrdersLastMonth()
  367.     {
  368.         return $this->useOrdersLastMonth;
  369.     }
  370.     /**
  371.      * Set ordersLastMonth
  372.      *
  373.      * @param integer $ordersLastMonth
  374.      * @return MailChimpSegment
  375.      */
  376.     public function setOrdersLastMonth($ordersLastMonth)
  377.     {
  378.         $this->ordersLastMonth $ordersLastMonth;
  379.         return $this;
  380.     }
  381.     /**
  382.      * Get ordersLastMonth
  383.      *
  384.      * @return integer
  385.      */
  386.     public function getOrdersLastMonth()
  387.     {
  388.         return $this->ordersLastMonth;
  389.     }
  390.     /**
  391.      * Set useOrderAmount
  392.      *
  393.      * @param boolean $useOrderAmount
  394.      * @return MailChimpSegment
  395.      */
  396.     public function setUseOrderAmount($useOrderAmount)
  397.     {
  398.         $this->useOrderAmount $useOrderAmount;
  399.         return $this;
  400.     }
  401.     /**
  402.      * Get useOrderAmount
  403.      *
  404.      * @return boolean
  405.      */
  406.     public function getUseOrderAmount()
  407.     {
  408.         return $this->useOrderAmount;
  409.     }
  410.     /**
  411.      * Set orderAmount
  412.      *
  413.      * @param float $orderAmount
  414.      * @return MailChimpSegment
  415.      */
  416.     public function setOrderAmount($orderAmount)
  417.     {
  418.         $this->orderAmount $orderAmount;
  419.         return $this;
  420.     }
  421.     /**
  422.      * Get orderAmount
  423.      *
  424.      * @return float
  425.      */
  426.     public function getOrderAmount()
  427.     {
  428.         return $this->orderAmount;
  429.     }
  430.     /**
  431.      * Set useMemberships
  432.      *
  433.      * @param boolean $useMemberships
  434.      * @return MailChimpSegment
  435.      */
  436.     public function setUseMemberships($useMemberships)
  437.     {
  438.         $this->useMemberships $useMemberships;
  439.         return $this;
  440.     }
  441.     /**
  442.      * Get useMemberships
  443.      *
  444.      * @return boolean
  445.      */
  446.     public function getUseMemberships()
  447.     {
  448.         return $this->useMemberships;
  449.     }
  450.     /**
  451.      * Set useProducts
  452.      *
  453.      * @param boolean $useProducts
  454.      * @return MailChimpSegment
  455.      */
  456.     public function setUseProducts($useProducts)
  457.     {
  458.         $this->useProducts $useProducts;
  459.         return $this;
  460.     }
  461.     /**
  462.      * Get useProducts
  463.      *
  464.      * @return boolean
  465.      */
  466.     public function getUseProducts()
  467.     {
  468.         return $this->useProducts;
  469.     }
  470.     /**
  471.      * Set enabled
  472.      *
  473.      * @param boolean $enabled
  474.      * @return MailChimpSegment
  475.      */
  476.     public function setEnabled($enabled)
  477.     {
  478.         $this->enabled = (bool)$enabled;
  479.         return $this;
  480.     }
  481.     /**
  482.      * Get enabled
  483.      *
  484.      * @return boolean
  485.      */
  486.     public function getEnabled()
  487.     {
  488.         return $this->enabled;
  489.     }
  490.     /**
  491.      * Set list
  492.      *
  493.      * @param \XC\MailChimp\Model\MailChimpList $list
  494.      * @return MailChimpSegment
  495.      */
  496.     public function setList(\XC\MailChimp\Model\MailChimpList $list null)
  497.     {
  498.         $this->list $list;
  499.         return $this;
  500.     }
  501.     /**
  502.      * Get list
  503.      *
  504.      * @return \XC\MailChimp\Model\MailChimpList
  505.      */
  506.     public function getList()
  507.     {
  508.         return $this->list;
  509.     }
  510.     /**
  511.      * Add memberships
  512.      *
  513.      * @param \XLite\Model\Membership $memberships
  514.      * @return MailChimpSegment
  515.      */
  516.     public function addMemberships(\XLite\Model\Membership $memberships)
  517.     {
  518.         $this->memberships[] = $memberships;
  519.         return $this;
  520.     }
  521.     /**
  522.      * Get memberships
  523.      *
  524.      * @return \Doctrine\Common\Collections\Collection
  525.      */
  526.     public function getMemberships()
  527.     {
  528.         return $this->memberships;
  529.     }
  530.     /**
  531.      * Add products
  532.      *
  533.      * @param \XLite\Model\Product $products
  534.      * @return MailChimpSegment
  535.      */
  536.     public function addProducts(\XLite\Model\Product $products)
  537.     {
  538.         $this->products[] = $products;
  539.         return $this;
  540.     }
  541.     /**
  542.      * Get products
  543.      *
  544.      * @return \Doctrine\Common\Collections\Collection
  545.      */
  546.     public function getProducts()
  547.     {
  548.         return $this->products;
  549.     }
  550.     /**
  551.      * Add profiles
  552.      *
  553.      * @param \XLite\Model\Profile $profiles
  554.      * @return MailChimpSegment
  555.      */
  556.     public function addProfiles(\XLite\Model\Profile $profiles)
  557.     {
  558.         $this->profiles[] = $profiles;
  559.         return $this;
  560.     }
  561.     /**
  562.      * Get profiles
  563.      *
  564.      * @return \Doctrine\Common\Collections\Collection
  565.      */
  566.     public function getProfiles()
  567.     {
  568.         return $this->profiles;
  569.     }
  570. }