dynamic /
silverstripe-foxy
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Dynamic\Foxy\Model; |
||
| 4 | |||
| 5 | use SilverStripe\CMS\Model\SiteTree; |
||
| 6 | use SilverStripe\Core\ClassInfo; |
||
| 7 | use SilverStripe\Core\Config\Configurable; |
||
| 8 | use SilverStripe\Core\Extensible; |
||
| 9 | use SilverStripe\Core\Injector\Injectable; |
||
| 10 | use SilverStripe\ORM\ArrayList; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Class FoxyHelper |
||
| 14 | * @package Dynamic\Foxy\Model |
||
| 15 | */ |
||
| 16 | class FoxyHelper extends \FoxyCart_Helper |
||
| 17 | { |
||
| 18 | use Configurable; |
||
| 19 | use Injectable; |
||
| 20 | use Extensible; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var |
||
| 24 | */ |
||
| 25 | private static $secret; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 26 | |||
| 27 | /** |
||
| 28 | * @var |
||
| 29 | */ |
||
| 30 | protected static $cart_url; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var |
||
| 34 | */ |
||
| 35 | private static $custom_ssl; |
||
|
0 ignored issues
–
show
|
|||
| 36 | |||
| 37 | /** |
||
| 38 | * @var |
||
| 39 | */ |
||
| 40 | private static $max_quantity = 10; |
||
|
0 ignored issues
–
show
|
|||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private static $product_classes = []; |
||
|
0 ignored issues
–
show
|
|||
| 46 | |||
| 47 | /** |
||
| 48 | * @var |
||
| 49 | */ |
||
| 50 | private $foxy_secret; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var |
||
| 54 | */ |
||
| 55 | private $foxy_cart_url; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var |
||
| 59 | */ |
||
| 60 | private $foxy_custom_ssl; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var |
||
| 64 | */ |
||
| 65 | private $foxy_max_quantity; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var |
||
| 69 | */ |
||
| 70 | private $foxy_product_classes; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return mixed |
||
| 74 | */ |
||
| 75 | public function getStoreSecret() |
||
| 76 | { |
||
| 77 | if (!$this->foxy_secret) { |
||
| 78 | $this->setStoreSecret(); |
||
| 79 | } |
||
| 80 | |||
| 81 | return $this->foxy_secret; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function setStoreSecret() |
||
| 88 | { |
||
| 89 | $this->foxy_secret = $this->config()->get('secret'); |
||
| 90 | |||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | public function getStoreCartURL() |
||
| 98 | { |
||
| 99 | if (!$this->foxy_cart_url) { |
||
| 100 | $this->setStoreCartURL(); |
||
| 101 | } |
||
| 102 | |||
| 103 | return $this->foxy_cart_url; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function setStoreCartURL() |
||
| 110 | { |
||
| 111 | $this->foxy_cart_url = $this->config()->get('cart_url'); |
||
| 112 | |||
| 113 | return $this; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | public function getCustomSSL() |
||
| 120 | { |
||
| 121 | if (!$this->foxy_custom_ssl) { |
||
| 122 | $this->setCustomSSL(); |
||
| 123 | } |
||
| 124 | |||
| 125 | return $this->foxy_custom_ssl; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function setCustomSSL() |
||
| 132 | { |
||
| 133 | $this->foxy_custom_ssl = $this->config()->get('custom_ssl'); |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return mixed |
||
| 140 | */ |
||
| 141 | public function getMaxQuantity() |
||
| 142 | { |
||
| 143 | if (!$this->foxy_max_quantity) { |
||
| 144 | $this->setMaxQuantity(); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $this->foxy_max_quantity; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function setMaxQuantity() |
||
| 154 | { |
||
| 155 | $this->foxy_max_quantity = $this->config()->get('max_quantity'); |
||
| 156 | |||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public function getProuductClasses() |
||
| 164 | { |
||
| 165 | if (!$this->foxy_product_classes || !is_array($this->foxy_product_classes)) { |
||
| 166 | $this->setProductClasses(); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $this->foxy_product_classes; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function setProductClasses() |
||
| 176 | { |
||
| 177 | $productClasses = $this->config()->get('product_classes'); |
||
| 178 | |||
| 179 | if (empty($productClasses)) { |
||
| 180 | $this->foxy_product_classes = []; |
||
| 181 | |||
| 182 | return $this; |
||
| 183 | } elseif (!is_array($productClasses)) { |
||
| 184 | $productClasses = [$productClasses]; |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($this->config()->get('include_product_subclasses')) { |
||
| 188 | $productClasses = array_reduce($productClasses, function (array $list, $productClass) { |
||
| 189 | foreach (ClassInfo::subclassesFor($productClass) as $key => $class) { |
||
| 190 | $list[$key] = $class; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $list; |
||
| 194 | }, []); |
||
| 195 | } |
||
| 196 | |||
| 197 | $this->foxy_product_classes = $productClasses; |
||
| 198 | |||
| 199 | return $this; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return ArrayList|\SilverStripe\ORM\DataList |
||
| 204 | */ |
||
| 205 | public function getProducts() |
||
| 206 | { |
||
| 207 | $productClasses = $this->getProuductClasses(); |
||
| 208 | $products = ArrayList::create(); |
||
| 209 | |||
| 210 | if (!empty($productClasses)) { |
||
| 211 | $products = SiteTree::get()->filter('ClassName', array_values($productClasses)); |
||
| 212 | } |
||
| 213 | |||
| 214 | $this->extend('updateProducts', $products); |
||
| 215 | |||
| 216 | return $products; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * FoxyHelper constructor. |
||
| 221 | * |
||
| 222 | * Set the private statics $secret and $cart_url in FoxyCart_Helper |
||
| 223 | * |
||
| 224 | * @throws \SilverStripe\ORM\ValidationException |
||
| 225 | */ |
||
| 226 | public function __construct() |
||
| 227 | { |
||
| 228 | self::setCartURL($this->getStoreCartURL()); |
||
| 229 | self::setSecret($this->getStoreSecret()); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public static function StoreURL() |
||
| 236 | { |
||
| 237 | $helper = FoxyHelper::create(); |
||
| 238 | if ($helper->getCustomSSL()) { |
||
| 239 | return sprintf('https://%s/', $helper->getStoreCartURL()); |
||
| 240 | } else { |
||
| 241 | return sprintf('https://%s.foxycart.com/', $helper->getStoreCartURL()); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return string |
||
| 247 | * @throws \SilverStripe\ORM\ValidationException |
||
| 248 | */ |
||
| 249 | public static function FormActionURL() |
||
| 250 | { |
||
| 251 | return self::StoreURL() . 'cart'; |
||
| 252 | } |
||
| 253 | } |
||
| 254 |