@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | { |
| 36 | 36 | $rules = Director::config()->get('rules'); |
| 37 | 37 | $rule = array_search(FoxyStripeController::class, $rules); |
| 38 | - $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; |
|
| 38 | + $myURL = Director::absoluteBaseURL().explode('//', $rule)[0]; |
|
| 39 | 39 | $myKey = FoxyCart::getStoreKey(); |
| 40 | 40 | |
| 41 | 41 | $this->updateConfig(); |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | $email = $emails[count($emails) - 1]; |
| 123 | 123 | return preg_replace_callback( |
| 124 | 124 | "|(\d+)|", |
| 125 | - function ($mathces) { |
|
| 125 | + function($mathces) { |
|
| 126 | 126 | return ++$mathces[1]; |
| 127 | 127 | }, |
| 128 | 128 | |
@@ -20,158 +20,158 @@ |
||
| 20 | 20 | class DataTestController extends Controller |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - private static $data = [ |
|
| 27 | - "TransactionDate" => "now", |
|
| 28 | - "OrderID" => "auto", |
|
| 29 | - "Email" => "auto", |
|
| 30 | - "Password" => "password", |
|
| 31 | - "OrderDetails" => [], |
|
| 32 | - ]; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 36 | - */ |
|
| 37 | - public function index() |
|
| 38 | - { |
|
| 39 | - $rules = Director::config()->get('rules'); |
|
| 40 | - $rule = array_search(FoxyStripeController::class, $rules); |
|
| 41 | - $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; |
|
| 42 | - $myKey = FoxyCart::getStoreKey(); |
|
| 43 | - |
|
| 44 | - $this->updateConfig(); |
|
| 45 | - $config = static::config()->get('data'); |
|
| 46 | - $config['OrderDetails'] = ArrayList::create($config['OrderDetails']); |
|
| 47 | - $xml = $this->renderWith('TestData', $config); |
|
| 48 | - $XMLOutput = $xml->RAW(); |
|
| 49 | - |
|
| 50 | - $XMLOutput_encrypted = \rc4crypt::encrypt($myKey, $XMLOutput); |
|
| 51 | - $XMLOutput_encrypted = urlencode($XMLOutput_encrypted); |
|
| 52 | - |
|
| 53 | - $ch = curl_init(); |
|
| 54 | - curl_setopt($ch, CURLOPT_URL, $myURL); |
|
| 55 | - curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted)); |
|
| 56 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 57 | - curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
|
| 58 | - |
|
| 59 | - $response = curl_exec($ch); |
|
| 60 | - $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 61 | - curl_close($ch); |
|
| 62 | - |
|
| 63 | - $configString = print_r(static::config()->get('data'), true); |
|
| 64 | - /** @var DebugView $view */ |
|
| 65 | - $view = Injector::inst()->create(DebugView::class); |
|
| 66 | - echo $view->renderHeader(); |
|
| 67 | - echo '<div class="info">'; |
|
| 68 | - echo "<h2>Config:</h2><pre>$configString</pre>"; |
|
| 69 | - if ($this->getRequest()->getVar('data')) { |
|
| 70 | - echo "<h2>Data:</h2><pre>{$xml->HTML()}</pre>"; |
|
| 71 | - } |
|
| 72 | - echo "<h2>Response:</h2><pre>$response</pre>"; |
|
| 73 | - echo '<p></p>'; |
|
| 74 | - echo '</div>'; |
|
| 75 | - echo $view->renderFooter(); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * |
|
| 80 | - */ |
|
| 81 | - private function updateConfig() |
|
| 82 | - { |
|
| 83 | - $data = static::config()->get('data'); |
|
| 84 | - $transactionDate = $data['TransactionDate']; |
|
| 85 | - static::config()->merge('data', [ |
|
| 86 | - 'TransactionDate' => strtotime($transactionDate), |
|
| 87 | - ]); |
|
| 88 | - |
|
| 89 | - $order_id = $data['OrderID']; |
|
| 90 | - if ($order_id === 'auto' || $order_id < 1) { |
|
| 91 | - $lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID; |
|
| 92 | - static::config()->merge('data', [ |
|
| 93 | - 'OrderID' => $lastOrderID + 1, |
|
| 94 | - ]); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $email = $data['Email']; |
|
| 98 | - if ($email === 'auto') { |
|
| 99 | - static::config()->merge('data', [ |
|
| 100 | - 'Email' => $this->generateEmail(), |
|
| 101 | - ]); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $orderDetails = $data['OrderDetails']; |
|
| 105 | - if (count($orderDetails) === 0) { |
|
| 106 | - static::config()->merge('data', [ |
|
| 107 | - 'OrderDetails' => [ |
|
| 108 | - $this->generateOrderDetail() |
|
| 109 | - ], |
|
| 110 | - ]); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if (!array_key_exists('Salt', $data)) { |
|
| 114 | - static::config()->merge('data', [ |
|
| 115 | - 'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt', |
|
| 116 | - ]); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - if (!array_key_exists('HashType', $data)) { |
|
| 120 | - static::config()->merge('data', [ |
|
| 121 | - 'HashType' => 'sha1_v2.4', |
|
| 122 | - ]); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $data = static::config()->get('data'); |
|
| 126 | - if (!array_key_exists('HashedPassword', $data)) { |
|
| 127 | - $encryptor = PasswordEncryptor::create_for_algorithm($data['HashType']); |
|
| 128 | - static::config()->merge('data', [ |
|
| 129 | - 'HashedPassword' => $encryptor->encrypt($data['Password'], $data['Salt']), |
|
| 130 | - ]); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @return string |
|
| 136 | - */ |
|
| 137 | - private function generateEmail() |
|
| 138 | - { |
|
| 139 | - $emails = Member::get()->filter([ |
|
| 140 | - 'Email:EndsWith' => '@example.com', |
|
| 141 | - ])->column('Email'); |
|
| 142 | - |
|
| 143 | - if ($emails && count($emails)) { |
|
| 144 | - $email = $emails[count($emails) - 1]; |
|
| 145 | - return preg_replace_callback( |
|
| 146 | - "|(\d+)|", |
|
| 147 | - function ($mathces) { |
|
| 148 | - return ++$mathces[1]; |
|
| 149 | - }, |
|
| 150 | ||
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - return '[email protected]'; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @return array |
|
| 158 | - */ |
|
| 159 | - private function generateOrderDetail() |
|
| 160 | - { |
|
| 161 | - return [ |
|
| 162 | - 'Title' => 'foo', |
|
| 163 | - 'Price' => 20.00, |
|
| 164 | - 'Quantity' => 1, |
|
| 165 | - 'Weight' => 0.1, |
|
| 166 | - 'DeliveryType' => 'shipped', |
|
| 167 | - 'CategoryDescription' => 'Default cateogry', |
|
| 168 | - 'CategoryCode' => 'DEFAULT', |
|
| 169 | - 'Options' => [ |
|
| 170 | - 'Name' => 'color', |
|
| 171 | - 'OptionValue' => 'blue', |
|
| 172 | - 'PriceMod' => '', |
|
| 173 | - 'WeightMod' => '', |
|
| 174 | - ], |
|
| 175 | - ]; |
|
| 176 | - } |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + private static $data = [ |
|
| 27 | + "TransactionDate" => "now", |
|
| 28 | + "OrderID" => "auto", |
|
| 29 | + "Email" => "auto", |
|
| 30 | + "Password" => "password", |
|
| 31 | + "OrderDetails" => [], |
|
| 32 | + ]; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 36 | + */ |
|
| 37 | + public function index() |
|
| 38 | + { |
|
| 39 | + $rules = Director::config()->get('rules'); |
|
| 40 | + $rule = array_search(FoxyStripeController::class, $rules); |
|
| 41 | + $myURL = Director::absoluteBaseURL() . explode('//', $rule)[0]; |
|
| 42 | + $myKey = FoxyCart::getStoreKey(); |
|
| 43 | + |
|
| 44 | + $this->updateConfig(); |
|
| 45 | + $config = static::config()->get('data'); |
|
| 46 | + $config['OrderDetails'] = ArrayList::create($config['OrderDetails']); |
|
| 47 | + $xml = $this->renderWith('TestData', $config); |
|
| 48 | + $XMLOutput = $xml->RAW(); |
|
| 49 | + |
|
| 50 | + $XMLOutput_encrypted = \rc4crypt::encrypt($myKey, $XMLOutput); |
|
| 51 | + $XMLOutput_encrypted = urlencode($XMLOutput_encrypted); |
|
| 52 | + |
|
| 53 | + $ch = curl_init(); |
|
| 54 | + curl_setopt($ch, CURLOPT_URL, $myURL); |
|
| 55 | + curl_setopt($ch, CURLOPT_POSTFIELDS, array("FoxyData" => $XMLOutput_encrypted)); |
|
| 56 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 57 | + curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
|
| 58 | + |
|
| 59 | + $response = curl_exec($ch); |
|
| 60 | + $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
| 61 | + curl_close($ch); |
|
| 62 | + |
|
| 63 | + $configString = print_r(static::config()->get('data'), true); |
|
| 64 | + /** @var DebugView $view */ |
|
| 65 | + $view = Injector::inst()->create(DebugView::class); |
|
| 66 | + echo $view->renderHeader(); |
|
| 67 | + echo '<div class="info">'; |
|
| 68 | + echo "<h2>Config:</h2><pre>$configString</pre>"; |
|
| 69 | + if ($this->getRequest()->getVar('data')) { |
|
| 70 | + echo "<h2>Data:</h2><pre>{$xml->HTML()}</pre>"; |
|
| 71 | + } |
|
| 72 | + echo "<h2>Response:</h2><pre>$response</pre>"; |
|
| 73 | + echo '<p></p>'; |
|
| 74 | + echo '</div>'; |
|
| 75 | + echo $view->renderFooter(); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * |
|
| 80 | + */ |
|
| 81 | + private function updateConfig() |
|
| 82 | + { |
|
| 83 | + $data = static::config()->get('data'); |
|
| 84 | + $transactionDate = $data['TransactionDate']; |
|
| 85 | + static::config()->merge('data', [ |
|
| 86 | + 'TransactionDate' => strtotime($transactionDate), |
|
| 87 | + ]); |
|
| 88 | + |
|
| 89 | + $order_id = $data['OrderID']; |
|
| 90 | + if ($order_id === 'auto' || $order_id < 1) { |
|
| 91 | + $lastOrderID = Order::get()->sort('Order_ID')->last()->Order_ID; |
|
| 92 | + static::config()->merge('data', [ |
|
| 93 | + 'OrderID' => $lastOrderID + 1, |
|
| 94 | + ]); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $email = $data['Email']; |
|
| 98 | + if ($email === 'auto') { |
|
| 99 | + static::config()->merge('data', [ |
|
| 100 | + 'Email' => $this->generateEmail(), |
|
| 101 | + ]); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $orderDetails = $data['OrderDetails']; |
|
| 105 | + if (count($orderDetails) === 0) { |
|
| 106 | + static::config()->merge('data', [ |
|
| 107 | + 'OrderDetails' => [ |
|
| 108 | + $this->generateOrderDetail() |
|
| 109 | + ], |
|
| 110 | + ]); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if (!array_key_exists('Salt', $data)) { |
|
| 114 | + static::config()->merge('data', [ |
|
| 115 | + 'Salt' => 'faGgWXUTdZ7i42lpA6cljzKeGBeUwShBSNHECwsJmt', |
|
| 116 | + ]); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + if (!array_key_exists('HashType', $data)) { |
|
| 120 | + static::config()->merge('data', [ |
|
| 121 | + 'HashType' => 'sha1_v2.4', |
|
| 122 | + ]); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $data = static::config()->get('data'); |
|
| 126 | + if (!array_key_exists('HashedPassword', $data)) { |
|
| 127 | + $encryptor = PasswordEncryptor::create_for_algorithm($data['HashType']); |
|
| 128 | + static::config()->merge('data', [ |
|
| 129 | + 'HashedPassword' => $encryptor->encrypt($data['Password'], $data['Salt']), |
|
| 130 | + ]); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @return string |
|
| 136 | + */ |
|
| 137 | + private function generateEmail() |
|
| 138 | + { |
|
| 139 | + $emails = Member::get()->filter([ |
|
| 140 | + 'Email:EndsWith' => '@example.com', |
|
| 141 | + ])->column('Email'); |
|
| 142 | + |
|
| 143 | + if ($emails && count($emails)) { |
|
| 144 | + $email = $emails[count($emails) - 1]; |
|
| 145 | + return preg_replace_callback( |
|
| 146 | + "|(\d+)|", |
|
| 147 | + function ($mathces) { |
|
| 148 | + return ++$mathces[1]; |
|
| 149 | + }, |
|
| 150 | ||
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + return '[email protected]'; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @return array |
|
| 158 | + */ |
|
| 159 | + private function generateOrderDetail() |
|
| 160 | + { |
|
| 161 | + return [ |
|
| 162 | + 'Title' => 'foo', |
|
| 163 | + 'Price' => 20.00, |
|
| 164 | + 'Quantity' => 1, |
|
| 165 | + 'Weight' => 0.1, |
|
| 166 | + 'DeliveryType' => 'shipped', |
|
| 167 | + 'CategoryDescription' => 'Default cateogry', |
|
| 168 | + 'CategoryCode' => 'DEFAULT', |
|
| 169 | + 'Options' => [ |
|
| 170 | + 'Name' => 'color', |
|
| 171 | + 'OptionValue' => 'blue', |
|
| 172 | + 'PriceMod' => '', |
|
| 173 | + 'WeightMod' => '', |
|
| 174 | + ], |
|
| 175 | + ]; |
|
| 176 | + } |
|
| 177 | 177 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function getCMSFields() |
| 71 | 71 | { |
| 72 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 72 | + $this->beforeUpdateCMSFields(function(FieldList $fields) { |
|
| 73 | 73 | if (FoxyStripeSetting::current_foxystripe_setting()->MultiGroup) { |
| 74 | 74 | $config = GridFieldConfig_RelationEditor::create(); |
| 75 | 75 | $config->addComponent(new GridFieldOrderableRows('SortOrder')); |
@@ -150,8 +150,8 @@ discard block |
||
| 150 | 150 | |
| 151 | 151 | if ($config->MultiGroup) { |
| 152 | 152 | $entries = $this->Products()->sort('SortOrder'); |
| 153 | - } else { |
|
| 154 | - $filter = '"ParentID" = ' . $this->ID; |
|
| 153 | + }else { |
|
| 154 | + $filter = '"ParentID" = '.$this->ID; |
|
| 155 | 155 | |
| 156 | 156 | // Build a list of all IDs for ProductGroups that are children |
| 157 | 157 | $holderIDs = $this->ProductGroupIDs(); |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | if ($filter) { |
| 163 | 163 | $filter .= ' OR '; |
| 164 | 164 | } |
| 165 | - $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ')'; |
|
| 165 | + $filter .= '"ParentID" IN ('.implode(',', $holderIDs).')'; |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | $order = '"SiteTree"."Title" ASC'; |
@@ -18,161 +18,161 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class ProductHolder extends \Page |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - private static $singular_name = 'Product Group'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private static $plural_name = 'Product Groups'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var string |
|
| 33 | - */ |
|
| 34 | - private static $description = 'Display a list of related products'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - private static $many_many = [ |
|
| 40 | - 'Products' => ProductPage::class, |
|
| 41 | - ]; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - private static $many_many_extraFields = [ |
|
| 47 | - 'Products' => [ |
|
| 48 | - 'SortOrder' => 'Int', |
|
| 49 | - ], |
|
| 50 | - ]; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - /* |
|
| 21 | + /** |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + private static $singular_name = 'Product Group'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private static $plural_name = 'Product Groups'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var string |
|
| 33 | + */ |
|
| 34 | + private static $description = 'Display a list of related products'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + private static $many_many = [ |
|
| 40 | + 'Products' => ProductPage::class, |
|
| 41 | + ]; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + private static $many_many_extraFields = [ |
|
| 47 | + 'Products' => [ |
|
| 48 | + 'SortOrder' => 'Int', |
|
| 49 | + ], |
|
| 50 | + ]; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + /* |
|
| 56 | 56 | private static $allowed_children = [ |
| 57 | 57 | ProductHolder::class, |
| 58 | 58 | ProductPage::class, |
| 59 | 59 | ]; |
| 60 | 60 | */ |
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @var string |
|
| 64 | - */ |
|
| 65 | - private static $table_name = 'ProductHolder'; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return FieldList |
|
| 69 | - */ |
|
| 70 | - public function getCMSFields() |
|
| 71 | - { |
|
| 72 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 73 | - if (FoxyStripeSetting::current_foxystripe_setting()->MultiGroup) { |
|
| 74 | - $config = GridFieldConfig_RelationEditor::create(); |
|
| 75 | - $config->addComponent(new GridFieldOrderableRows('SortOrder')); |
|
| 76 | - $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
| 77 | - $config->addComponent(new GridFieldAddExistingSearchButton()); |
|
| 78 | - |
|
| 79 | - $fields->addFieldToTab( |
|
| 80 | - 'Root.Products', |
|
| 81 | - GridField::create( |
|
| 82 | - 'Products', |
|
| 83 | - _t('ProductHolder.Products', 'Products'), |
|
| 84 | - $this->Products(), |
|
| 85 | - $config |
|
| 86 | - ) |
|
| 87 | - ); |
|
| 88 | - } |
|
| 89 | - }); |
|
| 90 | - |
|
| 91 | - return parent::getCMSFields(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @return DataList |
|
| 96 | - */ |
|
| 97 | - public function Products() |
|
| 98 | - { |
|
| 99 | - return $this->getManyManyComponents('Products')->sort('SortOrder'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * loadDescendantProductGroupIDListInto function. |
|
| 104 | - * |
|
| 105 | - * @param mixed &$idList |
|
| 106 | - */ |
|
| 107 | - public function loadDescendantProductGroupIDListInto(&$idList) |
|
| 108 | - { |
|
| 109 | - if ($children = $this->AllChildren()) { |
|
| 110 | - foreach ($children as $child) { |
|
| 111 | - if (in_array($child->ID, $idList)) { |
|
| 112 | - continue; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - if ($child instanceof self) { |
|
| 116 | - $idList[] = $child->ID; |
|
| 117 | - $child->loadDescendantProductGroupIDListInto($idList); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * ProductGroupIDs function. |
|
| 125 | - * |
|
| 126 | - * @return array |
|
| 127 | - */ |
|
| 128 | - public function ProductGroupIDs() |
|
| 129 | - { |
|
| 130 | - $holderIDs = []; |
|
| 131 | - $this->loadDescendantProductGroupIDListInto($holderIDs); |
|
| 132 | - |
|
| 133 | - return $holderIDs; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * @param int $limit |
|
| 138 | - * |
|
| 139 | - * @return PaginatedList |
|
| 140 | - * |
|
| 141 | - * @throws \Exception |
|
| 142 | - */ |
|
| 143 | - public function ProductList($limit = 10) |
|
| 144 | - { |
|
| 145 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 146 | - |
|
| 147 | - if ($config->ProductLimit > 0) { |
|
| 148 | - $limit = $config->ProductLimit; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if ($config->MultiGroup) { |
|
| 152 | - $entries = $this->Products()->sort('SortOrder'); |
|
| 153 | - } else { |
|
| 154 | - $filter = '"ParentID" = ' . $this->ID; |
|
| 155 | - |
|
| 156 | - // Build a list of all IDs for ProductGroups that are children |
|
| 157 | - $holderIDs = $this->ProductGroupIDs(); |
|
| 158 | - |
|
| 159 | - // If no ProductHolders, no ProductPages. So return false |
|
| 160 | - if ($holderIDs) { |
|
| 161 | - // Otherwise, do the actual query |
|
| 162 | - if ($filter) { |
|
| 163 | - $filter .= ' OR '; |
|
| 164 | - } |
|
| 165 | - $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ')'; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $order = '"SiteTree"."Title" ASC'; |
|
| 169 | - |
|
| 170 | - $entries = ProductPage::get()->where($filter); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - $list = new PaginatedList($entries, Controller::curr()->getRequest()); |
|
| 174 | - $list->setPageLength($limit); |
|
| 175 | - |
|
| 176 | - return $list; |
|
| 177 | - } |
|
| 62 | + /** |
|
| 63 | + * @var string |
|
| 64 | + */ |
|
| 65 | + private static $table_name = 'ProductHolder'; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return FieldList |
|
| 69 | + */ |
|
| 70 | + public function getCMSFields() |
|
| 71 | + { |
|
| 72 | + $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 73 | + if (FoxyStripeSetting::current_foxystripe_setting()->MultiGroup) { |
|
| 74 | + $config = GridFieldConfig_RelationEditor::create(); |
|
| 75 | + $config->addComponent(new GridFieldOrderableRows('SortOrder')); |
|
| 76 | + $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
| 77 | + $config->addComponent(new GridFieldAddExistingSearchButton()); |
|
| 78 | + |
|
| 79 | + $fields->addFieldToTab( |
|
| 80 | + 'Root.Products', |
|
| 81 | + GridField::create( |
|
| 82 | + 'Products', |
|
| 83 | + _t('ProductHolder.Products', 'Products'), |
|
| 84 | + $this->Products(), |
|
| 85 | + $config |
|
| 86 | + ) |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | + }); |
|
| 90 | + |
|
| 91 | + return parent::getCMSFields(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @return DataList |
|
| 96 | + */ |
|
| 97 | + public function Products() |
|
| 98 | + { |
|
| 99 | + return $this->getManyManyComponents('Products')->sort('SortOrder'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * loadDescendantProductGroupIDListInto function. |
|
| 104 | + * |
|
| 105 | + * @param mixed &$idList |
|
| 106 | + */ |
|
| 107 | + public function loadDescendantProductGroupIDListInto(&$idList) |
|
| 108 | + { |
|
| 109 | + if ($children = $this->AllChildren()) { |
|
| 110 | + foreach ($children as $child) { |
|
| 111 | + if (in_array($child->ID, $idList)) { |
|
| 112 | + continue; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + if ($child instanceof self) { |
|
| 116 | + $idList[] = $child->ID; |
|
| 117 | + $child->loadDescendantProductGroupIDListInto($idList); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * ProductGroupIDs function. |
|
| 125 | + * |
|
| 126 | + * @return array |
|
| 127 | + */ |
|
| 128 | + public function ProductGroupIDs() |
|
| 129 | + { |
|
| 130 | + $holderIDs = []; |
|
| 131 | + $this->loadDescendantProductGroupIDListInto($holderIDs); |
|
| 132 | + |
|
| 133 | + return $holderIDs; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * @param int $limit |
|
| 138 | + * |
|
| 139 | + * @return PaginatedList |
|
| 140 | + * |
|
| 141 | + * @throws \Exception |
|
| 142 | + */ |
|
| 143 | + public function ProductList($limit = 10) |
|
| 144 | + { |
|
| 145 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 146 | + |
|
| 147 | + if ($config->ProductLimit > 0) { |
|
| 148 | + $limit = $config->ProductLimit; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if ($config->MultiGroup) { |
|
| 152 | + $entries = $this->Products()->sort('SortOrder'); |
|
| 153 | + } else { |
|
| 154 | + $filter = '"ParentID" = ' . $this->ID; |
|
| 155 | + |
|
| 156 | + // Build a list of all IDs for ProductGroups that are children |
|
| 157 | + $holderIDs = $this->ProductGroupIDs(); |
|
| 158 | + |
|
| 159 | + // If no ProductHolders, no ProductPages. So return false |
|
| 160 | + if ($holderIDs) { |
|
| 161 | + // Otherwise, do the actual query |
|
| 162 | + if ($filter) { |
|
| 163 | + $filter .= ' OR '; |
|
| 164 | + } |
|
| 165 | + $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ')'; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $order = '"SiteTree"."Title" ASC'; |
|
| 169 | + |
|
| 170 | + $entries = ProductPage::get()->where($filter); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + $list = new PaginatedList($entries, Controller::curr()->getRequest()); |
|
| 174 | + $list->setPageLength($limit); |
|
| 175 | + |
|
| 176 | + return $list; |
|
| 177 | + } |
|
| 178 | 178 | } |
@@ -117,12 +117,10 @@ discard block |
||
| 117 | 117 | $this->setSiteConfig($siteConfig); |
| 118 | 118 | |
| 119 | 119 | $fields = ($fields != null && $fields->exists()) ? |
| 120 | - $this->getProductFields($fields) : |
|
| 121 | - $this->getProductFields(FieldList::create()); |
|
| 120 | + $this->getProductFields($fields) : $this->getProductFields(FieldList::create()); |
|
| 122 | 121 | |
| 123 | 122 | $actions = ($actions != null && $actions->exists()) ? |
| 124 | - $this->getProductActions($actions) : |
|
| 125 | - $this->getProductActions(FieldList::create()); |
|
| 123 | + $this->getProductActions($actions) : $this->getProductActions(FieldList::create()); |
|
| 126 | 124 | $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); |
| 127 | 125 | |
| 128 | 126 | parent::__construct($controller, $name, $fields, $actions, $validator); |
@@ -131,7 +129,7 @@ discard block |
||
| 131 | 129 | $this->setAttribute('action', FoxyCart::FormActionURL()); |
| 132 | 130 | $this->disableSecurityToken(); |
| 133 | 131 | |
| 134 | - $this->setHTMLID($this->getTemplateHelper()->generateFormID($this) . "_{$product->ID}"); |
|
| 132 | + $this->setHTMLID($this->getTemplateHelper()->generateFormID($this)."_{$product->ID}"); |
|
| 135 | 133 | } |
| 136 | 134 | |
| 137 | 135 | /** |
@@ -143,8 +141,7 @@ discard block |
||
| 143 | 141 | { |
| 144 | 142 | //Requirements::javascript('dynamic/foxystripe: client/dist/javascript/scripts.min.js'); |
| 145 | 143 | $hiddenTitle = ($this->product->ReceiptTitle) ? |
| 146 | - htmlspecialchars($this->product->ReceiptTitle) : |
|
| 147 | - htmlspecialchars($this->product->Title); |
|
| 144 | + htmlspecialchars($this->product->ReceiptTitle) : htmlspecialchars($this->product->Title); |
|
| 148 | 145 | $code = $this->product->Code; |
| 149 | 146 | |
| 150 | 147 | if ($this->getProduct()->getIsAvailable()) { |
@@ -178,7 +175,7 @@ discard block |
||
| 178 | 175 | ->setValue( |
| 179 | 176 | ProductPage::getGeneratedValue($code, 'price', $this->product->Price, 'value') |
| 180 | 177 | ) |
| 181 | - );//can't override id |
|
| 178 | + ); //can't override id |
|
| 182 | 179 | if ($this->product->Weight > 0) { |
| 183 | 180 | $fields->push( |
| 184 | 181 | HiddenField::create('weight') |
@@ -221,7 +218,7 @@ discard block |
||
| 221 | 218 | ); |
| 222 | 219 | |
| 223 | 220 | $fields->push( |
| 224 | - HeaderField::create('submitPrice', '$' . $this->product->Price, 4) |
|
| 221 | + HeaderField::create('submitPrice', '$'.$this->product->Price, 4) |
|
| 225 | 222 | ->addExtraClass('submit-price') |
| 226 | 223 | ); |
| 227 | 224 | $fields->push( |
@@ -234,7 +231,7 @@ discard block |
||
| 234 | 231 | } |
| 235 | 232 | |
| 236 | 233 | $this->extend('updatePurchaseFormFields', $fields); |
| 237 | - } else { |
|
| 234 | + }else { |
|
| 238 | 235 | $fields->push(HeaderField::create('unavailableText', 'Currently Out of Stock', 4)); |
| 239 | 236 | } |
| 240 | 237 | |
@@ -31,299 +31,299 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class FoxyStripePurchaseForm extends Form |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * @var |
|
| 36 | - */ |
|
| 37 | - protected $site_config; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @var |
|
| 41 | - */ |
|
| 42 | - private $product; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @param $siteConfig |
|
| 46 | - * |
|
| 47 | - * @return $this |
|
| 48 | - */ |
|
| 49 | - public function setSiteConfig($siteConfig) |
|
| 50 | - { |
|
| 51 | - $siteConfig = $siteConfig === null ? FoxyStripeSetting::current_foxystripe_setting() : $siteConfig; |
|
| 52 | - if ($siteConfig instanceof FoxyStripeSetting) { |
|
| 53 | - $this->site_config = $siteConfig; |
|
| 54 | - |
|
| 55 | - return $this; |
|
| 56 | - } |
|
| 57 | - throw new \InvalidArgumentException('$siteConfig needs to be an instance of FoxyStripeSetting.'); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return FoxyStripeSetting |
|
| 62 | - */ |
|
| 63 | - public function getSiteConfig() |
|
| 64 | - { |
|
| 65 | - if (!$this->site_config) { |
|
| 66 | - $this->setSiteConfig(FoxyStripeSetting::current_foxystripe_setting()); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return $this->site_config; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param $product |
|
| 74 | - * |
|
| 75 | - * @return $this |
|
| 76 | - */ |
|
| 77 | - public function setProduct($product) |
|
| 78 | - { |
|
| 79 | - if ($product instanceof ProductPage) { |
|
| 80 | - $this->product = $product; |
|
| 81 | - |
|
| 82 | - return $this; |
|
| 83 | - } |
|
| 84 | - throw new \InvalidArgumentException('$product needs to be an instance of ProductPage.'); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return ProductPage |
|
| 89 | - */ |
|
| 90 | - public function getProduct() |
|
| 91 | - { |
|
| 92 | - return $this->product; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * FoxyStripePurchaseForm constructor. |
|
| 97 | - * |
|
| 98 | - * @param ContentController $controller |
|
| 99 | - * @param string $name |
|
| 100 | - * @param FieldList|null $fields |
|
| 101 | - * @param FieldList|null $actions |
|
| 102 | - * @param null $validator |
|
| 103 | - * @param null $product |
|
| 104 | - * @param null $siteConfig |
|
| 105 | - * |
|
| 106 | - */ |
|
| 107 | - public function __construct( |
|
| 108 | - $controller, |
|
| 109 | - $name, |
|
| 110 | - FieldList $fields = null, |
|
| 111 | - FieldList $actions = null, |
|
| 112 | - $validator = null, |
|
| 113 | - $product = null, |
|
| 114 | - $siteConfig = null |
|
| 115 | - ) { |
|
| 116 | - $this->setProduct($product); |
|
| 117 | - $this->setSiteConfig($siteConfig); |
|
| 118 | - |
|
| 119 | - $fields = ($fields != null && $fields->exists()) ? |
|
| 120 | - $this->getProductFields($fields) : |
|
| 121 | - $this->getProductFields(FieldList::create()); |
|
| 122 | - |
|
| 123 | - $actions = ($actions != null && $actions->exists()) ? |
|
| 124 | - $this->getProductActions($actions) : |
|
| 125 | - $this->getProductActions(FieldList::create()); |
|
| 126 | - $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); |
|
| 127 | - |
|
| 128 | - parent::__construct($controller, $name, $fields, $actions, $validator); |
|
| 129 | - |
|
| 130 | - //have to call after parent::__construct() |
|
| 131 | - $this->setAttribute('action', FoxyCart::FormActionURL()); |
|
| 132 | - $this->disableSecurityToken(); |
|
| 133 | - |
|
| 134 | - $this->setHTMLID($this->getTemplateHelper()->generateFormID($this) . "_{$product->ID}"); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param FieldList $fields |
|
| 139 | - * |
|
| 140 | - * @return FieldList |
|
| 141 | - */ |
|
| 142 | - protected function getProductFields(FieldList $fields) |
|
| 143 | - { |
|
| 144 | - //Requirements::javascript('dynamic/foxystripe: client/dist/javascript/scripts.min.js'); |
|
| 145 | - $hiddenTitle = ($this->product->ReceiptTitle) ? |
|
| 146 | - htmlspecialchars($this->product->ReceiptTitle) : |
|
| 147 | - htmlspecialchars($this->product->Title); |
|
| 148 | - $code = $this->product->Code; |
|
| 149 | - |
|
| 150 | - if ($this->getProduct()->getIsAvailable()) { |
|
| 151 | - $fields->push( |
|
| 152 | - HiddenField::create('name') |
|
| 153 | - ->setValue( |
|
| 154 | - ProductPage::getGeneratedValue($code, 'name', $hiddenTitle, 'value') |
|
| 155 | - ) |
|
| 156 | - ); |
|
| 157 | - $fields->push( |
|
| 158 | - HiddenField::create('category') |
|
| 159 | - ->setValue( |
|
| 160 | - ProductPage::getGeneratedValue($code, 'category', $this->product->Category()->Code, 'value') |
|
| 161 | - ) |
|
| 162 | - ); |
|
| 163 | - $fields->push( |
|
| 164 | - HiddenField::create('code') |
|
| 165 | - ->setValue( |
|
| 166 | - ProductPage::getGeneratedValue($code, 'code', $this->product->Code, 'value') |
|
| 167 | - ) |
|
| 168 | - ); |
|
| 169 | - $fields->push( |
|
| 170 | - HiddenField::create( |
|
| 171 | - 'product_id' |
|
| 172 | - )->setValue( |
|
| 173 | - ProductPage::getGeneratedValue($code, 'product_id', $this->product->ID, 'value') |
|
| 174 | - ) |
|
| 175 | - ); |
|
| 176 | - $fields->push( |
|
| 177 | - HiddenField::create('price') |
|
| 178 | - ->setValue( |
|
| 179 | - ProductPage::getGeneratedValue($code, 'price', $this->product->Price, 'value') |
|
| 180 | - ) |
|
| 181 | - );//can't override id |
|
| 182 | - if ($this->product->Weight > 0) { |
|
| 183 | - $fields->push( |
|
| 184 | - HiddenField::create('weight') |
|
| 185 | - ->setValue( |
|
| 186 | - ProductPage::getGeneratedValue($code, 'weight', $this->product->Weight, 'value') |
|
| 187 | - ) |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $image = null; |
|
| 192 | - if ($this->product->Image() || ProductPage::has_extension(ProductPageLegacy::class)) { |
|
| 193 | - if ($this->product->Image()) { |
|
| 194 | - $image = $this->product->Image()->Pad(80, 80)->absoluteURL; |
|
| 195 | - } elseif (ProductPage::has_extension(ProductPageLegacy::class) && |
|
| 196 | - $this->product->PreviewImage()->exists()) { |
|
| 197 | - $image = $this->product->PreviewImage()->Pad(80, 80)->absoluteURL; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - if ($image) { |
|
| 201 | - $fields->push( |
|
| 202 | - HiddenField::create('image') |
|
| 203 | - ->setValue( |
|
| 204 | - ProductPage::getGeneratedValue($code, 'image', $image, 'value') |
|
| 205 | - ) |
|
| 206 | - ); |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - $optionsSet = $this->getProductOptionSet(); |
|
| 211 | - $fields->push($optionsSet); |
|
| 212 | - |
|
| 213 | - $quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10; |
|
| 214 | - |
|
| 215 | - $fields->push(QuantityField::create('x:visibleQuantity')->setTitle('Quantity')->setValue(1)); |
|
| 216 | - $fields->push( |
|
| 217 | - HiddenField::create('quantity') |
|
| 218 | - ->setValue( |
|
| 219 | - ProductPage::getGeneratedValue($code, 'quantity', 1, 'value') |
|
| 220 | - ) |
|
| 221 | - ); |
|
| 222 | - |
|
| 223 | - $fields->push( |
|
| 224 | - HeaderField::create('submitPrice', '$' . $this->product->Price, 4) |
|
| 225 | - ->addExtraClass('submit-price') |
|
| 226 | - ); |
|
| 227 | - $fields->push( |
|
| 228 | - $unavailable = HeaderField::create('unavailableText', 'Selection unavailable', 4) |
|
| 229 | - ->addExtraClass('unavailable-text') |
|
| 230 | - ); |
|
| 231 | - |
|
| 232 | - if (!empty(trim($this->getSiteConfig()->StoreName)) && $this->getProduct()->getIsAvailable()) { |
|
| 233 | - $unavailable->addExtraClass('hidden'); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $this->extend('updatePurchaseFormFields', $fields); |
|
| 237 | - } else { |
|
| 238 | - $fields->push(HeaderField::create('unavailableText', 'Currently Out of Stock', 4)); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $this->extend('updateFoxyStripePurchaseFormFields', $fields); |
|
| 242 | - |
|
| 243 | - return $fields; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param FieldList $actions |
|
| 248 | - * |
|
| 249 | - * @return FieldList |
|
| 250 | - */ |
|
| 251 | - protected function getProductActions(FieldList $actions) |
|
| 252 | - { |
|
| 253 | - if (!empty(trim($this->getSiteConfig()->StoreName)) && $this->getProduct()->getIsAvailable()) { |
|
| 254 | - $actions->push( |
|
| 255 | - $submit = FormAction::create( |
|
| 256 | - 'x:submit', |
|
| 257 | - _t('ProductForm.AddToCart', 'Add to Cart') |
|
| 258 | - )->addExtraClass('fs-add-to-cart-button') |
|
| 259 | - ); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - $this->extend('updateFoxyStripePurchaseFormActions', $actions); |
|
| 263 | - |
|
| 264 | - return $actions; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * @return CompositeField |
|
| 269 | - */ |
|
| 270 | - protected function getProductOptionSet() |
|
| 271 | - { |
|
| 272 | - $options = $this->product->ProductOptions(); |
|
| 273 | - $groupedOptions = new GroupedList($options); |
|
| 274 | - $groupedBy = $groupedOptions->groupBy('ProductOptionGroupID'); |
|
| 275 | - |
|
| 276 | - /** @var CompositeField $optionsSet */ |
|
| 277 | - $optionsSet = CompositeField::create(); |
|
| 278 | - |
|
| 279 | - /** @var DataList $set */ |
|
| 280 | - foreach ($groupedBy as $id => $set) { |
|
| 281 | - $group = OptionGroup::get()->byID($id); |
|
| 282 | - $title = $group->Title; |
|
| 283 | - $fieldName = preg_replace('/\s/', '_', $title); |
|
| 284 | - $disabled = []; |
|
| 285 | - $fullOptions = []; |
|
| 286 | - |
|
| 287 | - foreach ($set as $item) { |
|
| 288 | - $item = $this->setAvailability($item); |
|
| 289 | - |
|
| 290 | - $name = ProductPage::getGeneratedValue( |
|
| 291 | - $this->product->Code, |
|
| 292 | - $group->Title, |
|
| 293 | - $item->getGeneratedValue(), |
|
| 294 | - 'value' |
|
| 295 | - ); |
|
| 296 | - |
|
| 297 | - $fullOptions[$name] = $item->getGeneratedTitle(); |
|
| 298 | - if (!$item->Availability) { |
|
| 299 | - array_push($disabled, $name); |
|
| 300 | - } |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - $optionsSet->push( |
|
| 304 | - $dropdown = DropdownField::create($fieldName, $title, $fullOptions)->setTitle($title) |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - if (!empty($disabled)) { |
|
| 308 | - $dropdown->setDisabledItems($disabled); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $dropdown->addExtraClass("product-options"); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $optionsSet->addExtraClass('foxycartOptionsContainer'); |
|
| 315 | - |
|
| 316 | - return $optionsSet; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param OptionItem $option |
|
| 321 | - * @return OptionItem |
|
| 322 | - */ |
|
| 323 | - protected function setAvailability(OptionItem $option) |
|
| 324 | - { |
|
| 325 | - $option->Available = ($option->getAvailability()) ? true : false; |
|
| 326 | - |
|
| 327 | - return $option; |
|
| 328 | - } |
|
| 34 | + /** |
|
| 35 | + * @var |
|
| 36 | + */ |
|
| 37 | + protected $site_config; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @var |
|
| 41 | + */ |
|
| 42 | + private $product; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @param $siteConfig |
|
| 46 | + * |
|
| 47 | + * @return $this |
|
| 48 | + */ |
|
| 49 | + public function setSiteConfig($siteConfig) |
|
| 50 | + { |
|
| 51 | + $siteConfig = $siteConfig === null ? FoxyStripeSetting::current_foxystripe_setting() : $siteConfig; |
|
| 52 | + if ($siteConfig instanceof FoxyStripeSetting) { |
|
| 53 | + $this->site_config = $siteConfig; |
|
| 54 | + |
|
| 55 | + return $this; |
|
| 56 | + } |
|
| 57 | + throw new \InvalidArgumentException('$siteConfig needs to be an instance of FoxyStripeSetting.'); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return FoxyStripeSetting |
|
| 62 | + */ |
|
| 63 | + public function getSiteConfig() |
|
| 64 | + { |
|
| 65 | + if (!$this->site_config) { |
|
| 66 | + $this->setSiteConfig(FoxyStripeSetting::current_foxystripe_setting()); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return $this->site_config; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param $product |
|
| 74 | + * |
|
| 75 | + * @return $this |
|
| 76 | + */ |
|
| 77 | + public function setProduct($product) |
|
| 78 | + { |
|
| 79 | + if ($product instanceof ProductPage) { |
|
| 80 | + $this->product = $product; |
|
| 81 | + |
|
| 82 | + return $this; |
|
| 83 | + } |
|
| 84 | + throw new \InvalidArgumentException('$product needs to be an instance of ProductPage.'); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return ProductPage |
|
| 89 | + */ |
|
| 90 | + public function getProduct() |
|
| 91 | + { |
|
| 92 | + return $this->product; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * FoxyStripePurchaseForm constructor. |
|
| 97 | + * |
|
| 98 | + * @param ContentController $controller |
|
| 99 | + * @param string $name |
|
| 100 | + * @param FieldList|null $fields |
|
| 101 | + * @param FieldList|null $actions |
|
| 102 | + * @param null $validator |
|
| 103 | + * @param null $product |
|
| 104 | + * @param null $siteConfig |
|
| 105 | + * |
|
| 106 | + */ |
|
| 107 | + public function __construct( |
|
| 108 | + $controller, |
|
| 109 | + $name, |
|
| 110 | + FieldList $fields = null, |
|
| 111 | + FieldList $actions = null, |
|
| 112 | + $validator = null, |
|
| 113 | + $product = null, |
|
| 114 | + $siteConfig = null |
|
| 115 | + ) { |
|
| 116 | + $this->setProduct($product); |
|
| 117 | + $this->setSiteConfig($siteConfig); |
|
| 118 | + |
|
| 119 | + $fields = ($fields != null && $fields->exists()) ? |
|
| 120 | + $this->getProductFields($fields) : |
|
| 121 | + $this->getProductFields(FieldList::create()); |
|
| 122 | + |
|
| 123 | + $actions = ($actions != null && $actions->exists()) ? |
|
| 124 | + $this->getProductActions($actions) : |
|
| 125 | + $this->getProductActions(FieldList::create()); |
|
| 126 | + $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); |
|
| 127 | + |
|
| 128 | + parent::__construct($controller, $name, $fields, $actions, $validator); |
|
| 129 | + |
|
| 130 | + //have to call after parent::__construct() |
|
| 131 | + $this->setAttribute('action', FoxyCart::FormActionURL()); |
|
| 132 | + $this->disableSecurityToken(); |
|
| 133 | + |
|
| 134 | + $this->setHTMLID($this->getTemplateHelper()->generateFormID($this) . "_{$product->ID}"); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param FieldList $fields |
|
| 139 | + * |
|
| 140 | + * @return FieldList |
|
| 141 | + */ |
|
| 142 | + protected function getProductFields(FieldList $fields) |
|
| 143 | + { |
|
| 144 | + //Requirements::javascript('dynamic/foxystripe: client/dist/javascript/scripts.min.js'); |
|
| 145 | + $hiddenTitle = ($this->product->ReceiptTitle) ? |
|
| 146 | + htmlspecialchars($this->product->ReceiptTitle) : |
|
| 147 | + htmlspecialchars($this->product->Title); |
|
| 148 | + $code = $this->product->Code; |
|
| 149 | + |
|
| 150 | + if ($this->getProduct()->getIsAvailable()) { |
|
| 151 | + $fields->push( |
|
| 152 | + HiddenField::create('name') |
|
| 153 | + ->setValue( |
|
| 154 | + ProductPage::getGeneratedValue($code, 'name', $hiddenTitle, 'value') |
|
| 155 | + ) |
|
| 156 | + ); |
|
| 157 | + $fields->push( |
|
| 158 | + HiddenField::create('category') |
|
| 159 | + ->setValue( |
|
| 160 | + ProductPage::getGeneratedValue($code, 'category', $this->product->Category()->Code, 'value') |
|
| 161 | + ) |
|
| 162 | + ); |
|
| 163 | + $fields->push( |
|
| 164 | + HiddenField::create('code') |
|
| 165 | + ->setValue( |
|
| 166 | + ProductPage::getGeneratedValue($code, 'code', $this->product->Code, 'value') |
|
| 167 | + ) |
|
| 168 | + ); |
|
| 169 | + $fields->push( |
|
| 170 | + HiddenField::create( |
|
| 171 | + 'product_id' |
|
| 172 | + )->setValue( |
|
| 173 | + ProductPage::getGeneratedValue($code, 'product_id', $this->product->ID, 'value') |
|
| 174 | + ) |
|
| 175 | + ); |
|
| 176 | + $fields->push( |
|
| 177 | + HiddenField::create('price') |
|
| 178 | + ->setValue( |
|
| 179 | + ProductPage::getGeneratedValue($code, 'price', $this->product->Price, 'value') |
|
| 180 | + ) |
|
| 181 | + );//can't override id |
|
| 182 | + if ($this->product->Weight > 0) { |
|
| 183 | + $fields->push( |
|
| 184 | + HiddenField::create('weight') |
|
| 185 | + ->setValue( |
|
| 186 | + ProductPage::getGeneratedValue($code, 'weight', $this->product->Weight, 'value') |
|
| 187 | + ) |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $image = null; |
|
| 192 | + if ($this->product->Image() || ProductPage::has_extension(ProductPageLegacy::class)) { |
|
| 193 | + if ($this->product->Image()) { |
|
| 194 | + $image = $this->product->Image()->Pad(80, 80)->absoluteURL; |
|
| 195 | + } elseif (ProductPage::has_extension(ProductPageLegacy::class) && |
|
| 196 | + $this->product->PreviewImage()->exists()) { |
|
| 197 | + $image = $this->product->PreviewImage()->Pad(80, 80)->absoluteURL; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + if ($image) { |
|
| 201 | + $fields->push( |
|
| 202 | + HiddenField::create('image') |
|
| 203 | + ->setValue( |
|
| 204 | + ProductPage::getGeneratedValue($code, 'image', $image, 'value') |
|
| 205 | + ) |
|
| 206 | + ); |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + $optionsSet = $this->getProductOptionSet(); |
|
| 211 | + $fields->push($optionsSet); |
|
| 212 | + |
|
| 213 | + $quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10; |
|
| 214 | + |
|
| 215 | + $fields->push(QuantityField::create('x:visibleQuantity')->setTitle('Quantity')->setValue(1)); |
|
| 216 | + $fields->push( |
|
| 217 | + HiddenField::create('quantity') |
|
| 218 | + ->setValue( |
|
| 219 | + ProductPage::getGeneratedValue($code, 'quantity', 1, 'value') |
|
| 220 | + ) |
|
| 221 | + ); |
|
| 222 | + |
|
| 223 | + $fields->push( |
|
| 224 | + HeaderField::create('submitPrice', '$' . $this->product->Price, 4) |
|
| 225 | + ->addExtraClass('submit-price') |
|
| 226 | + ); |
|
| 227 | + $fields->push( |
|
| 228 | + $unavailable = HeaderField::create('unavailableText', 'Selection unavailable', 4) |
|
| 229 | + ->addExtraClass('unavailable-text') |
|
| 230 | + ); |
|
| 231 | + |
|
| 232 | + if (!empty(trim($this->getSiteConfig()->StoreName)) && $this->getProduct()->getIsAvailable()) { |
|
| 233 | + $unavailable->addExtraClass('hidden'); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $this->extend('updatePurchaseFormFields', $fields); |
|
| 237 | + } else { |
|
| 238 | + $fields->push(HeaderField::create('unavailableText', 'Currently Out of Stock', 4)); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $this->extend('updateFoxyStripePurchaseFormFields', $fields); |
|
| 242 | + |
|
| 243 | + return $fields; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param FieldList $actions |
|
| 248 | + * |
|
| 249 | + * @return FieldList |
|
| 250 | + */ |
|
| 251 | + protected function getProductActions(FieldList $actions) |
|
| 252 | + { |
|
| 253 | + if (!empty(trim($this->getSiteConfig()->StoreName)) && $this->getProduct()->getIsAvailable()) { |
|
| 254 | + $actions->push( |
|
| 255 | + $submit = FormAction::create( |
|
| 256 | + 'x:submit', |
|
| 257 | + _t('ProductForm.AddToCart', 'Add to Cart') |
|
| 258 | + )->addExtraClass('fs-add-to-cart-button') |
|
| 259 | + ); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + $this->extend('updateFoxyStripePurchaseFormActions', $actions); |
|
| 263 | + |
|
| 264 | + return $actions; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * @return CompositeField |
|
| 269 | + */ |
|
| 270 | + protected function getProductOptionSet() |
|
| 271 | + { |
|
| 272 | + $options = $this->product->ProductOptions(); |
|
| 273 | + $groupedOptions = new GroupedList($options); |
|
| 274 | + $groupedBy = $groupedOptions->groupBy('ProductOptionGroupID'); |
|
| 275 | + |
|
| 276 | + /** @var CompositeField $optionsSet */ |
|
| 277 | + $optionsSet = CompositeField::create(); |
|
| 278 | + |
|
| 279 | + /** @var DataList $set */ |
|
| 280 | + foreach ($groupedBy as $id => $set) { |
|
| 281 | + $group = OptionGroup::get()->byID($id); |
|
| 282 | + $title = $group->Title; |
|
| 283 | + $fieldName = preg_replace('/\s/', '_', $title); |
|
| 284 | + $disabled = []; |
|
| 285 | + $fullOptions = []; |
|
| 286 | + |
|
| 287 | + foreach ($set as $item) { |
|
| 288 | + $item = $this->setAvailability($item); |
|
| 289 | + |
|
| 290 | + $name = ProductPage::getGeneratedValue( |
|
| 291 | + $this->product->Code, |
|
| 292 | + $group->Title, |
|
| 293 | + $item->getGeneratedValue(), |
|
| 294 | + 'value' |
|
| 295 | + ); |
|
| 296 | + |
|
| 297 | + $fullOptions[$name] = $item->getGeneratedTitle(); |
|
| 298 | + if (!$item->Availability) { |
|
| 299 | + array_push($disabled, $name); |
|
| 300 | + } |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + $optionsSet->push( |
|
| 304 | + $dropdown = DropdownField::create($fieldName, $title, $fullOptions)->setTitle($title) |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + if (!empty($disabled)) { |
|
| 308 | + $dropdown->setDisabledItems($disabled); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $dropdown->addExtraClass("product-options"); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $optionsSet->addExtraClass('foxycartOptionsContainer'); |
|
| 315 | + |
|
| 316 | + return $optionsSet; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param OptionItem $option |
|
| 321 | + * @return OptionItem |
|
| 322 | + */ |
|
| 323 | + protected function setAvailability(OptionItem $option) |
|
| 324 | + { |
|
| 325 | + $option->Available = ($option->getAvailability()) ? true : false; |
|
| 326 | + |
|
| 327 | + return $option; |
|
| 328 | + } |
|
| 329 | 329 | } |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | if ($config->RemoteDomain) { |
| 76 | 76 | return $config->RemoteDomain; |
| 77 | 77 | } |
| 78 | - } else { |
|
| 78 | + }else { |
|
| 79 | 79 | if ($config->StoreName) { |
| 80 | 80 | return $config->StoreName; |
| 81 | 81 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $config = FoxyStripeSetting::current_foxystripe_setting(); |
| 94 | 94 | if ($config->CustomSSL) { |
| 95 | 95 | return sprintf('https://%s/cart', self::getFoxyCartStoreName()); |
| 96 | - } else { |
|
| 96 | + }else { |
|
| 97 | 97 | return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName()); |
| 98 | 98 | } |
| 99 | 99 | } |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | $config = FoxyStripeSetting::current_foxystripe_setting(); |
| 114 | 114 | if ($config->CustomSSL) { |
| 115 | 115 | $foxy_domain = self::getFoxyCartStoreName(); |
| 116 | - } else { |
|
| 116 | + }else { |
|
| 117 | 117 | $foxy_domain = self::getFoxyCartStoreName().'.foxycart.com'; |
| 118 | 118 | } |
| 119 | 119 | |
@@ -12,193 +12,193 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class FoxyCart |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - private static $keyPrefix = 'dYnm1c'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @param int $length |
|
| 22 | - * @param int $count |
|
| 23 | - * |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public static function setStoreKey($length = 54, $count = 0) |
|
| 27 | - { |
|
| 28 | - $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now'); |
|
| 29 | - $strLength = strlen($charset); |
|
| 30 | - $str = ''; |
|
| 31 | - while ($count < $length) { |
|
| 32 | - $str .= $charset[mt_rand(0, $strLength - 1)]; |
|
| 33 | - ++$count; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - return self::getKeyPrefix().substr(base64_encode($str), 0, $length); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @return mixed|null |
|
| 41 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 42 | - */ |
|
| 43 | - public static function getStoreKey() |
|
| 44 | - { |
|
| 45 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 46 | - if ($config->StoreKey) { |
|
| 47 | - return $config->StoreKey; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @return null|string |
|
| 55 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 56 | - */ |
|
| 57 | - public static function store_name_warning() |
|
| 58 | - { |
|
| 59 | - $warning = null; |
|
| 60 | - if (self::getFoxyCartStoreName() === null) { |
|
| 61 | - $warning = 'Must define FoxyCart Store Name or Store Remote Domain in your site settings in the cms'; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - return $warning; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return mixed|null |
|
| 69 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 70 | - */ |
|
| 71 | - public static function getFoxyCartStoreName() |
|
| 72 | - { |
|
| 73 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 74 | - if ($config->CustomSSL) { |
|
| 75 | - if ($config->RemoteDomain) { |
|
| 76 | - return $config->RemoteDomain; |
|
| 77 | - } |
|
| 78 | - } else { |
|
| 79 | - if ($config->StoreName) { |
|
| 80 | - return $config->StoreName; |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return string |
|
| 89 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 90 | - */ |
|
| 91 | - public static function FormActionURL() |
|
| 92 | - { |
|
| 93 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 94 | - if ($config->CustomSSL) { |
|
| 95 | - return sprintf('https://%s/cart', self::getFoxyCartStoreName()); |
|
| 96 | - } else { |
|
| 97 | - return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName()); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * FoxyCart API v1.1 functions. |
|
| 103 | - */ |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param array $foxyData |
|
| 107 | - * @return string |
|
| 108 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 109 | - */ |
|
| 110 | - private static function getAPIRequest($foxyData = array()) |
|
| 111 | - { |
|
| 112 | - if (self::getStoreKey() && self::getFoxyCartStoreName()) { |
|
| 113 | - $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 114 | - if ($config->CustomSSL) { |
|
| 115 | - $foxy_domain = self::getFoxyCartStoreName(); |
|
| 116 | - } else { |
|
| 117 | - $foxy_domain = self::getFoxyCartStoreName().'.foxycart.com'; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $foxyData['api_token'] = self::getStoreKey(); |
|
| 121 | - |
|
| 122 | - $ch = curl_init(); |
|
| 123 | - curl_setopt($ch, CURLOPT_URL, 'https://'.$foxy_domain.'/api'); |
|
| 124 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData); |
|
| 125 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 126 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
|
| 127 | - curl_setopt($ch, CURLOPT_TIMEOUT, 15); |
|
| 128 | - // If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle |
|
| 129 | - // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 130 | - $response = trim(curl_exec($ch)); |
|
| 131 | - |
|
| 132 | - // The following if block will print any CURL errors you might have |
|
| 133 | - if ($response == false) { |
|
| 134 | - //trigger_error("Could not connect to FoxyCart API", E_USER_ERROR); |
|
| 135 | - Injector::inst()->get(LoggerInterface::class)->error('Could not connect to FoxyCart API'); |
|
| 136 | - } |
|
| 137 | - curl_close($ch); |
|
| 138 | - |
|
| 139 | - return $response; |
|
| 140 | - } |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param null $Member |
|
| 146 | - * @return string |
|
| 147 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 148 | - */ |
|
| 149 | - public static function getCustomer(Member $Member = null) |
|
| 150 | - { |
|
| 151 | - |
|
| 152 | - // throw error if no $Member Object |
|
| 153 | - if (!isset($Member)) { |
|
| 154 | - trigger_error('No Member set', E_USER_ERROR); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // grab customer record from API |
|
| 158 | - |
|
| 159 | - $foxyData = array(); |
|
| 160 | - $foxyData['api_action'] = 'customer_get'; |
|
| 161 | - if ($Member->Customer_ID) { |
|
| 162 | - $foxyData['customer_id'] = $Member->Customer_ID; |
|
| 163 | - } |
|
| 164 | - $foxyData['customer_email'] = $Member->Email; |
|
| 165 | - |
|
| 166 | - return self::getAPIRequest($foxyData); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @param null $Member |
|
| 171 | - * @return string |
|
| 172 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 173 | - */ |
|
| 174 | - public static function putCustomer(Member $Member = null) |
|
| 175 | - { |
|
| 176 | - // throw error if no $Member Object |
|
| 177 | - if ($Member === null) { |
|
| 15 | + /** |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + private static $keyPrefix = 'dYnm1c'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @param int $length |
|
| 22 | + * @param int $count |
|
| 23 | + * |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public static function setStoreKey($length = 54, $count = 0) |
|
| 27 | + { |
|
| 28 | + $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now'); |
|
| 29 | + $strLength = strlen($charset); |
|
| 30 | + $str = ''; |
|
| 31 | + while ($count < $length) { |
|
| 32 | + $str .= $charset[mt_rand(0, $strLength - 1)]; |
|
| 33 | + ++$count; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + return self::getKeyPrefix().substr(base64_encode($str), 0, $length); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @return mixed|null |
|
| 41 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 42 | + */ |
|
| 43 | + public static function getStoreKey() |
|
| 44 | + { |
|
| 45 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 46 | + if ($config->StoreKey) { |
|
| 47 | + return $config->StoreKey; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @return null|string |
|
| 55 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 56 | + */ |
|
| 57 | + public static function store_name_warning() |
|
| 58 | + { |
|
| 59 | + $warning = null; |
|
| 60 | + if (self::getFoxyCartStoreName() === null) { |
|
| 61 | + $warning = 'Must define FoxyCart Store Name or Store Remote Domain in your site settings in the cms'; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + return $warning; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return mixed|null |
|
| 69 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 70 | + */ |
|
| 71 | + public static function getFoxyCartStoreName() |
|
| 72 | + { |
|
| 73 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 74 | + if ($config->CustomSSL) { |
|
| 75 | + if ($config->RemoteDomain) { |
|
| 76 | + return $config->RemoteDomain; |
|
| 77 | + } |
|
| 78 | + } else { |
|
| 79 | + if ($config->StoreName) { |
|
| 80 | + return $config->StoreName; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return string |
|
| 89 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 90 | + */ |
|
| 91 | + public static function FormActionURL() |
|
| 92 | + { |
|
| 93 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 94 | + if ($config->CustomSSL) { |
|
| 95 | + return sprintf('https://%s/cart', self::getFoxyCartStoreName()); |
|
| 96 | + } else { |
|
| 97 | + return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName()); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * FoxyCart API v1.1 functions. |
|
| 103 | + */ |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param array $foxyData |
|
| 107 | + * @return string |
|
| 108 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 109 | + */ |
|
| 110 | + private static function getAPIRequest($foxyData = array()) |
|
| 111 | + { |
|
| 112 | + if (self::getStoreKey() && self::getFoxyCartStoreName()) { |
|
| 113 | + $config = FoxyStripeSetting::current_foxystripe_setting(); |
|
| 114 | + if ($config->CustomSSL) { |
|
| 115 | + $foxy_domain = self::getFoxyCartStoreName(); |
|
| 116 | + } else { |
|
| 117 | + $foxy_domain = self::getFoxyCartStoreName().'.foxycart.com'; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $foxyData['api_token'] = self::getStoreKey(); |
|
| 121 | + |
|
| 122 | + $ch = curl_init(); |
|
| 123 | + curl_setopt($ch, CURLOPT_URL, 'https://'.$foxy_domain.'/api'); |
|
| 124 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData); |
|
| 125 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
| 126 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
|
| 127 | + curl_setopt($ch, CURLOPT_TIMEOUT, 15); |
|
| 128 | + // If you get SSL errors, you can uncomment the following, or ask your host to add the appropriate CA bundle |
|
| 129 | + // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
| 130 | + $response = trim(curl_exec($ch)); |
|
| 131 | + |
|
| 132 | + // The following if block will print any CURL errors you might have |
|
| 133 | + if ($response == false) { |
|
| 134 | + //trigger_error("Could not connect to FoxyCart API", E_USER_ERROR); |
|
| 135 | + Injector::inst()->get(LoggerInterface::class)->error('Could not connect to FoxyCart API'); |
|
| 136 | + } |
|
| 137 | + curl_close($ch); |
|
| 138 | + |
|
| 139 | + return $response; |
|
| 140 | + } |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param null $Member |
|
| 146 | + * @return string |
|
| 147 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 148 | + */ |
|
| 149 | + public static function getCustomer(Member $Member = null) |
|
| 150 | + { |
|
| 151 | + |
|
| 152 | + // throw error if no $Member Object |
|
| 153 | + if (!isset($Member)) { |
|
| 154 | + trigger_error('No Member set', E_USER_ERROR); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // grab customer record from API |
|
| 158 | + |
|
| 159 | + $foxyData = array(); |
|
| 160 | + $foxyData['api_action'] = 'customer_get'; |
|
| 161 | + if ($Member->Customer_ID) { |
|
| 162 | + $foxyData['customer_id'] = $Member->Customer_ID; |
|
| 163 | + } |
|
| 164 | + $foxyData['customer_email'] = $Member->Email; |
|
| 165 | + |
|
| 166 | + return self::getAPIRequest($foxyData); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @param null $Member |
|
| 171 | + * @return string |
|
| 172 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 173 | + */ |
|
| 174 | + public static function putCustomer(Member $Member = null) |
|
| 175 | + { |
|
| 176 | + // throw error if no $Member Object |
|
| 177 | + if ($Member === null) { |
|
| 178 | 178 | //trigger_error('No Member set', E_USER_ERROR); |
| 179 | - return false; |
|
| 180 | - } |
|
| 181 | - // send updated customer record from API |
|
| 182 | - $foxyData = array(); |
|
| 183 | - $foxyData['api_action'] = 'customer_save'; |
|
| 184 | - // customer_id will be 0 if created in SilverStripe. |
|
| 185 | - if ($Member->Customer_ID) { |
|
| 186 | - $foxyData['customer_id'] = $Member->Customer_ID; |
|
| 187 | - } |
|
| 188 | - $foxyData['customer_email'] = $Member->Email; |
|
| 189 | - $foxyData['customer_password_hash'] = $Member->Password; |
|
| 190 | - $foxyData['customer_password_salt'] = $Member->Salt; |
|
| 191 | - $foxyData['customer_first_name'] = $Member->FirstName; |
|
| 192 | - $foxyData['customer_last_name'] = $Member->Surname; |
|
| 193 | - |
|
| 194 | - return self::getAPIRequest($foxyData); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @return string |
|
| 199 | - */ |
|
| 200 | - public static function getKeyPrefix() |
|
| 201 | - { |
|
| 202 | - return self::$keyPrefix; |
|
| 203 | - } |
|
| 179 | + return false; |
|
| 180 | + } |
|
| 181 | + // send updated customer record from API |
|
| 182 | + $foxyData = array(); |
|
| 183 | + $foxyData['api_action'] = 'customer_save'; |
|
| 184 | + // customer_id will be 0 if created in SilverStripe. |
|
| 185 | + if ($Member->Customer_ID) { |
|
| 186 | + $foxyData['customer_id'] = $Member->Customer_ID; |
|
| 187 | + } |
|
| 188 | + $foxyData['customer_email'] = $Member->Email; |
|
| 189 | + $foxyData['customer_password_hash'] = $Member->Password; |
|
| 190 | + $foxyData['customer_password_salt'] = $Member->Salt; |
|
| 191 | + $foxyData['customer_first_name'] = $Member->FirstName; |
|
| 192 | + $foxyData['customer_last_name'] = $Member->Surname; |
|
| 193 | + |
|
| 194 | + return self::getAPIRequest($foxyData); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @return string |
|
| 199 | + */ |
|
| 200 | + public static function getKeyPrefix() |
|
| 201 | + { |
|
| 202 | + return self::$keyPrefix; |
|
| 203 | + } |
|
| 204 | 204 | } |
@@ -58,7 +58,7 @@ |
||
| 58 | 58 | */ |
| 59 | 59 | public function getCMSFields() |
| 60 | 60 | { |
| 61 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 61 | + $this->beforeUpdateCMSFields(function(FieldList $fields) { |
|
| 62 | 62 | $fields->removeByName('Options'); |
| 63 | 63 | |
| 64 | 64 | $fields->addFieldToTab( |
@@ -19,179 +19,179 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class OptionGroup extends DataObject |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - private static $db = array( |
|
| 26 | - 'Title' => 'Varchar(100)', |
|
| 27 | - ); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - private static $has_many = array( |
|
| 33 | - 'Options' => OptionItem::class, |
|
| 34 | - ); |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - private static $singular_name = 'Product Option Group'; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private static $plural_name = 'Product Option Groups'; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string |
|
| 48 | - */ |
|
| 49 | - private static $description = 'Groups of product options, e.g. size, color, etc'; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - private static $table_name = 'OptionGroup'; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @return FieldList |
|
| 58 | - */ |
|
| 59 | - public function getCMSFields() |
|
| 60 | - { |
|
| 61 | - $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 62 | - $fields->removeByName('Options'); |
|
| 63 | - |
|
| 64 | - $fields->addFieldToTab( |
|
| 65 | - 'Root.Options', |
|
| 66 | - HeaderField::create('OptionsHeader', 'Options can be added on Product Pages.', 3) |
|
| 67 | - ); |
|
| 68 | - }); |
|
| 69 | - |
|
| 70 | - return parent::getCMSFields(); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 75 | - */ |
|
| 76 | - public function requireDefaultRecords() |
|
| 77 | - { |
|
| 78 | - parent::requireDefaultRecords(); |
|
| 79 | - // create a catch-all group |
|
| 80 | - if (!self::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 81 | - $do = new self(); |
|
| 82 | - $do->Title = 'Options'; |
|
| 83 | - $do->write(); |
|
| 84 | - } |
|
| 85 | - if (!self::get()->filter(array('Title' => 'Size'))->first()) { |
|
| 86 | - $do = new self(); |
|
| 87 | - $do->Title = 'Size'; |
|
| 88 | - $do->write(); |
|
| 89 | - } |
|
| 90 | - if (!self::get()->filter(array('Title' => 'Color'))->first()) { |
|
| 91 | - $do = new self(); |
|
| 92 | - $do->Title = 'Color'; |
|
| 93 | - $do->write(); |
|
| 94 | - } |
|
| 95 | - if (!self::get()->filter(array('Title' => 'Type'))->first()) { |
|
| 96 | - $do = new self(); |
|
| 97 | - $do->Title = 'Type'; |
|
| 98 | - $do->write(); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @return RequiredFields |
|
| 104 | - */ |
|
| 105 | - public function getCMSValidator() |
|
| 106 | - { |
|
| 107 | - return new RequiredFields(array('Title')); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @return ValidationResult |
|
| 112 | - */ |
|
| 113 | - public function validate() |
|
| 114 | - { |
|
| 115 | - $result = parent::validate(); |
|
| 116 | - |
|
| 117 | - $title = $this->Title; |
|
| 118 | - $firstChar = substr($title, 0, 1); |
|
| 119 | - if (preg_match('/[^a-zA-Z]/', $firstChar)) { |
|
| 120 | - $result->addError('The first character of the Title can only be a letter', 'bad'); |
|
| 121 | - } |
|
| 122 | - if (preg_match('/[^a-zA-Z]\s/', $title)) { |
|
| 123 | - $result->addError('Please only use letters, numbers and spaces in the title', 'bad'); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $result; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @throws \SilverStripe\ORM\ValidationException |
|
| 131 | - */ |
|
| 132 | - public function onBeforeDelete() |
|
| 133 | - { |
|
| 134 | - parent::onBeforeDelete(); |
|
| 135 | - |
|
| 136 | - //make sure that if we delete this option group, we reassign the group's option items to the 'None' group. |
|
| 137 | - $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); |
|
| 138 | - |
|
| 139 | - if (isset($items)) { |
|
| 140 | - if ($noneGroup = self::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 141 | - /** @var OptionItem $item */ |
|
| 142 | - foreach ($items as $item) { |
|
| 143 | - $item->ProductOptionGroupID = $noneGroup->ID; |
|
| 144 | - $item->write(); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @param bool $member |
|
| 152 | - * |
|
| 153 | - * @return bool |
|
| 154 | - */ |
|
| 155 | - public function canView($member = null) |
|
| 156 | - { |
|
| 157 | - return true; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @param null $member |
|
| 162 | - * |
|
| 163 | - * @return bool|int |
|
| 164 | - */ |
|
| 165 | - public function canEdit($member = null) |
|
| 166 | - { |
|
| 167 | - switch ($this->Title) { |
|
| 168 | - case 'Options': |
|
| 169 | - return false; |
|
| 170 | - break; |
|
| 171 | - default: |
|
| 172 | - return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 173 | - break; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @param null $member |
|
| 179 | - * |
|
| 180 | - * @return bool|int |
|
| 181 | - */ |
|
| 182 | - public function canDelete($member = null) |
|
| 183 | - { |
|
| 184 | - return $this->canEdit($member); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @param null $member |
|
| 189 | - * @param array $context |
|
| 190 | - * |
|
| 191 | - * @return bool|int |
|
| 192 | - */ |
|
| 193 | - public function canCreate($member = null, $context = []) |
|
| 194 | - { |
|
| 195 | - return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 196 | - } |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + private static $db = array( |
|
| 26 | + 'Title' => 'Varchar(100)', |
|
| 27 | + ); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + private static $has_many = array( |
|
| 33 | + 'Options' => OptionItem::class, |
|
| 34 | + ); |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + private static $singular_name = 'Product Option Group'; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private static $plural_name = 'Product Option Groups'; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string |
|
| 48 | + */ |
|
| 49 | + private static $description = 'Groups of product options, e.g. size, color, etc'; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + private static $table_name = 'OptionGroup'; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @return FieldList |
|
| 58 | + */ |
|
| 59 | + public function getCMSFields() |
|
| 60 | + { |
|
| 61 | + $this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
| 62 | + $fields->removeByName('Options'); |
|
| 63 | + |
|
| 64 | + $fields->addFieldToTab( |
|
| 65 | + 'Root.Options', |
|
| 66 | + HeaderField::create('OptionsHeader', 'Options can be added on Product Pages.', 3) |
|
| 67 | + ); |
|
| 68 | + }); |
|
| 69 | + |
|
| 70 | + return parent::getCMSFields(); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 75 | + */ |
|
| 76 | + public function requireDefaultRecords() |
|
| 77 | + { |
|
| 78 | + parent::requireDefaultRecords(); |
|
| 79 | + // create a catch-all group |
|
| 80 | + if (!self::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 81 | + $do = new self(); |
|
| 82 | + $do->Title = 'Options'; |
|
| 83 | + $do->write(); |
|
| 84 | + } |
|
| 85 | + if (!self::get()->filter(array('Title' => 'Size'))->first()) { |
|
| 86 | + $do = new self(); |
|
| 87 | + $do->Title = 'Size'; |
|
| 88 | + $do->write(); |
|
| 89 | + } |
|
| 90 | + if (!self::get()->filter(array('Title' => 'Color'))->first()) { |
|
| 91 | + $do = new self(); |
|
| 92 | + $do->Title = 'Color'; |
|
| 93 | + $do->write(); |
|
| 94 | + } |
|
| 95 | + if (!self::get()->filter(array('Title' => 'Type'))->first()) { |
|
| 96 | + $do = new self(); |
|
| 97 | + $do->Title = 'Type'; |
|
| 98 | + $do->write(); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @return RequiredFields |
|
| 104 | + */ |
|
| 105 | + public function getCMSValidator() |
|
| 106 | + { |
|
| 107 | + return new RequiredFields(array('Title')); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @return ValidationResult |
|
| 112 | + */ |
|
| 113 | + public function validate() |
|
| 114 | + { |
|
| 115 | + $result = parent::validate(); |
|
| 116 | + |
|
| 117 | + $title = $this->Title; |
|
| 118 | + $firstChar = substr($title, 0, 1); |
|
| 119 | + if (preg_match('/[^a-zA-Z]/', $firstChar)) { |
|
| 120 | + $result->addError('The first character of the Title can only be a letter', 'bad'); |
|
| 121 | + } |
|
| 122 | + if (preg_match('/[^a-zA-Z]\s/', $title)) { |
|
| 123 | + $result->addError('Please only use letters, numbers and spaces in the title', 'bad'); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $result; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @throws \SilverStripe\ORM\ValidationException |
|
| 131 | + */ |
|
| 132 | + public function onBeforeDelete() |
|
| 133 | + { |
|
| 134 | + parent::onBeforeDelete(); |
|
| 135 | + |
|
| 136 | + //make sure that if we delete this option group, we reassign the group's option items to the 'None' group. |
|
| 137 | + $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); |
|
| 138 | + |
|
| 139 | + if (isset($items)) { |
|
| 140 | + if ($noneGroup = self::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 141 | + /** @var OptionItem $item */ |
|
| 142 | + foreach ($items as $item) { |
|
| 143 | + $item->ProductOptionGroupID = $noneGroup->ID; |
|
| 144 | + $item->write(); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @param bool $member |
|
| 152 | + * |
|
| 153 | + * @return bool |
|
| 154 | + */ |
|
| 155 | + public function canView($member = null) |
|
| 156 | + { |
|
| 157 | + return true; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @param null $member |
|
| 162 | + * |
|
| 163 | + * @return bool|int |
|
| 164 | + */ |
|
| 165 | + public function canEdit($member = null) |
|
| 166 | + { |
|
| 167 | + switch ($this->Title) { |
|
| 168 | + case 'Options': |
|
| 169 | + return false; |
|
| 170 | + break; |
|
| 171 | + default: |
|
| 172 | + return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 173 | + break; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @param null $member |
|
| 179 | + * |
|
| 180 | + * @return bool|int |
|
| 181 | + */ |
|
| 182 | + public function canDelete($member = null) |
|
| 183 | + { |
|
| 184 | + return $this->canEdit($member); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @param null $member |
|
| 189 | + * @param array $context |
|
| 190 | + * |
|
| 191 | + * @return bool|int |
|
| 192 | + */ |
|
| 193 | + public function canCreate($member = null, $context = []) |
|
| 194 | + { |
|
| 195 | + return Permission::check('Product_CANCRUD', 'any', $member); |
|
| 196 | + } |
|
| 197 | 197 | } |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | )->setEmptyString('') |
| 219 | 219 | ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')), |
| 220 | 220 | ]); |
| 221 | - } else { |
|
| 221 | + }else { |
|
| 222 | 222 | $fields->addFieldsToTab( |
| 223 | 223 | 'Root.Modifiers', |
| 224 | 224 | [ |
@@ -232,7 +232,7 @@ discard block |
||
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | // ProductOptionGroup Dropdown field w/ add new |
| 235 | - $groups = function () { |
|
| 235 | + $groups = function() { |
|
| 236 | 236 | return OptionGroup::get()->map()->toArray(); |
| 237 | 237 | }; |
| 238 | 238 | $groupFields = singleton(OptionGroup::class)->getCMSFields(); |
@@ -339,9 +339,9 @@ discard block |
||
| 339 | 339 | */ |
| 340 | 340 | public function getGeneratedValue() |
| 341 | 341 | { |
| 342 | - $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0'; |
|
| 342 | + $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
| 343 | 343 | $modPriceWithSymbol = self::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
| 344 | - $modWeight = ($this->WeightModifier) ? (string) $this->WeightModifier : '0'; |
|
| 344 | + $modWeight = ($this->WeightModifier) ? (string)$this->WeightModifier : '0'; |
|
| 345 | 345 | $modWeight = self::getOptionModifierActionSymbol($this->WeightModifierAction).$modWeight; |
| 346 | 346 | $modCode = self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
| 347 | 347 | |
@@ -353,14 +353,13 @@ discard block |
||
| 353 | 353 | */ |
| 354 | 354 | public function getGeneratedTitle() |
| 355 | 355 | { |
| 356 | - $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0'; |
|
| 356 | + $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
| 357 | 357 | $title = $this->Title; |
| 358 | 358 | $title .= ($this->PriceModifier != 0) ? |
| 359 | 359 | ': ('.self::getOptionModifierActionSymbol( |
| 360 | 360 | $this->PriceModifierAction, |
| 361 | 361 | $returnWithOnlyPlusMinus = true |
| 362 | - ).'$'.$modPrice.')' : |
|
| 363 | - ''; |
|
| 362 | + ).'$'.$modPrice.')' : ''; |
|
| 364 | 363 | |
| 365 | 364 | return $title; |
| 366 | 365 | } |
@@ -40,393 +40,393 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | class OptionItem extends DataObject |
| 42 | 42 | { |
| 43 | - /** |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - private static $db = array( |
|
| 47 | - 'Title' => 'Text', |
|
| 48 | - 'WeightModifier' => 'Decimal', |
|
| 49 | - 'CodeModifier' => 'Text', |
|
| 50 | - 'PriceModifier' => 'Currency', |
|
| 51 | - 'WeightModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 52 | - 'CodeModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 53 | - 'PriceModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 54 | - 'Available' => 'Boolean', |
|
| 55 | - 'SortOrder' => 'Int', |
|
| 56 | - ); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - private static $has_one = array( |
|
| 62 | - 'Product' => ProductPage::class, |
|
| 63 | - 'ProductOptionGroup' => OptionGroup::class, |
|
| 64 | - ); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - private static $belongs_many_many = array( |
|
| 70 | - 'OrderDetails' => OrderDetail::class, |
|
| 71 | - ); |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - private static $defaults = array( |
|
| 77 | - 'Available' => true, |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @var array |
|
| 82 | - */ |
|
| 83 | - private static $summary_fields = array( |
|
| 84 | - 'Title' => 'Title', |
|
| 85 | - 'ProductOptionGroup.Title' => 'Group', |
|
| 86 | - 'IsAvailable' => 'Available', |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @var array |
|
| 91 | - */ |
|
| 92 | - private static $searchable_fields = [ |
|
| 93 | - 'Title' => [ |
|
| 94 | - 'title' => 'Title', |
|
| 95 | - ], |
|
| 96 | - 'ProductOptionGroup.Title' => [ |
|
| 97 | - 'title' => 'Group', |
|
| 98 | - ], |
|
| 99 | - ]; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @var string |
|
| 103 | - */ |
|
| 104 | - private static $default_sort = 'SortOrder'; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @var string |
|
| 108 | - */ |
|
| 109 | - private static $table_name = 'OptionItem'; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @return FieldList |
|
| 113 | - */ |
|
| 114 | - public function getCMSFields() |
|
| 115 | - { |
|
| 116 | - $fields = parent::getCMSFields(); |
|
| 117 | - |
|
| 118 | - $fields->removeByName([ |
|
| 119 | - 'OrderDetails', |
|
| 120 | - 'SortOrder', |
|
| 121 | - 'ProductID', |
|
| 122 | - 'WeightModifier', |
|
| 123 | - 'CodeModifier', |
|
| 124 | - 'PriceModifier', |
|
| 125 | - 'WeightModifierAction', |
|
| 126 | - 'CodeModifierAction', |
|
| 127 | - 'PriceModifierAction', |
|
| 128 | - ]); |
|
| 129 | - |
|
| 130 | - if ($this->ProductID) { |
|
| 131 | - $product = ProductPage::get()->byID($this->ProductID); |
|
| 132 | - |
|
| 133 | - $parentPrice = $product->obj('Price')->Nice(); |
|
| 134 | - $parentWeight = $product->Weight; |
|
| 135 | - $parentCode = $product->Code; |
|
| 136 | - |
|
| 137 | - $fields->addFieldsToTab('Root.Modifiers', [ |
|
| 138 | - HeaderField::create('ModifyHD', _t( |
|
| 139 | - 'OptionItem.ModifyHD', |
|
| 140 | - 'Product Option Modifiers' |
|
| 141 | - ), 2), |
|
| 142 | - |
|
| 143 | - // Weight Modifier Fields |
|
| 144 | - HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 3), |
|
| 145 | - TextField::create('WeightModifier') |
|
| 146 | - ->setTitle(_t('OptionItem.WeightModifier', 'Weight')), |
|
| 147 | - DropdownField::create( |
|
| 148 | - 'WeightModifierAction', |
|
| 149 | - _t('OptionItem.WeightModifierAction', 'Weight Modification'), |
|
| 150 | - [ |
|
| 151 | - 'Add' => _t( |
|
| 152 | - 'OptionItem.WeightAdd', |
|
| 153 | - 'Add to Base Weight ({weight})', |
|
| 154 | - 'Add to weight', |
|
| 155 | - ['weight' => $parentWeight] |
|
| 156 | - ), |
|
| 157 | - 'Subtract' => _t( |
|
| 158 | - 'OptionItem.WeightSubtract', |
|
| 159 | - 'Subtract from Base Weight ({weight})', |
|
| 160 | - 'Subtract from weight', |
|
| 161 | - ['weight' => $parentWeight] |
|
| 162 | - ), |
|
| 163 | - 'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight'), |
|
| 164 | - ] |
|
| 165 | - )->setEmptyString('') |
|
| 166 | - ->setDescription(_t( |
|
| 167 | - 'OptionItem.WeightDescription', |
|
| 168 | - 'Does weight modify or replace base weight?' |
|
| 169 | - )), |
|
| 170 | - |
|
| 171 | - // Price Modifier FIelds |
|
| 172 | - HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 3), |
|
| 173 | - CurrencyField::create('PriceModifier') |
|
| 174 | - ->setTitle(_t('OptionItem.PriceModifier', 'Price')), |
|
| 175 | - DropdownField::create( |
|
| 176 | - 'PriceModifierAction', |
|
| 177 | - _t('OptionItem.PriceModifierAction', 'Price Modification'), |
|
| 178 | - [ |
|
| 179 | - 'Add' => _t( |
|
| 180 | - 'OptionItem.PriceAdd', |
|
| 181 | - 'Add to Base Price ({price})', |
|
| 182 | - 'Add to price', |
|
| 183 | - ['price' => $parentPrice] |
|
| 184 | - ), |
|
| 185 | - 'Subtract' => _t( |
|
| 186 | - 'OptionItem.PriceSubtract', |
|
| 187 | - 'Subtract from Base Price ({price})', |
|
| 188 | - 'Subtract from price', |
|
| 189 | - ['price' => $parentPrice] |
|
| 190 | - ), |
|
| 191 | - 'Set' => _t('OptionItem.PriceSet', 'Set as a new Price'), |
|
| 192 | - ] |
|
| 193 | - )->setEmptyString('') |
|
| 194 | - ->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')), |
|
| 195 | - |
|
| 196 | - // Code Modifier Fields |
|
| 197 | - HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 3), |
|
| 198 | - TextField::create('CodeModifier') |
|
| 199 | - ->setTitle(_t('OptionItem.CodeModifier', 'Code')), |
|
| 200 | - DropdownField::create( |
|
| 201 | - 'CodeModifierAction', |
|
| 202 | - _t('OptionItem.CodeModifierAction', 'Code Modification'), |
|
| 203 | - [ |
|
| 204 | - 'Add' => _t( |
|
| 205 | - 'OptionItem.CodeAdd', |
|
| 206 | - 'Add to Base Code ({code})', |
|
| 207 | - 'Add to code', |
|
| 208 | - ['code' => $parentCode] |
|
| 209 | - ), |
|
| 210 | - 'Subtract' => _t( |
|
| 211 | - 'OptionItem.CodeSubtract', |
|
| 212 | - 'Subtract from Base Code ({code})', |
|
| 213 | - 'Subtract from code', |
|
| 214 | - ['code' => $parentCode] |
|
| 215 | - ), |
|
| 216 | - 'Set' => _t('OptionItem.CodeSet', 'Set as a new Code'), |
|
| 217 | - ] |
|
| 218 | - )->setEmptyString('') |
|
| 219 | - ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')), |
|
| 220 | - ]); |
|
| 221 | - } else { |
|
| 222 | - $fields->addFieldsToTab( |
|
| 223 | - 'Root.Modifiers', |
|
| 224 | - [ |
|
| 225 | - HeaderField::create( |
|
| 226 | - 'ModifyHeader', |
|
| 227 | - 'Modifiers can be set when on a Product Page', |
|
| 228 | - 3 |
|
| 229 | - ), |
|
| 230 | - ] |
|
| 231 | - ); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // ProductOptionGroup Dropdown field w/ add new |
|
| 235 | - $groups = function () { |
|
| 236 | - return OptionGroup::get()->map()->toArray(); |
|
| 237 | - }; |
|
| 238 | - $groupFields = singleton(OptionGroup::class)->getCMSFields(); |
|
| 239 | - $groupField = DropdownField::create('ProductOptionGroupID', _t( |
|
| 240 | - 'OptionItem.Group', |
|
| 241 | - 'Group' |
|
| 242 | - ), $groups()) |
|
| 243 | - ->setEmptyString('') |
|
| 244 | - ->setDescription(_t( |
|
| 245 | - 'OptionItem.GroupDescription', |
|
| 246 | - 'Name of this group of options. Managed in <a href="admin/settings"> |
|
| 43 | + /** |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + private static $db = array( |
|
| 47 | + 'Title' => 'Text', |
|
| 48 | + 'WeightModifier' => 'Decimal', |
|
| 49 | + 'CodeModifier' => 'Text', |
|
| 50 | + 'PriceModifier' => 'Currency', |
|
| 51 | + 'WeightModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 52 | + 'CodeModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 53 | + 'PriceModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 54 | + 'Available' => 'Boolean', |
|
| 55 | + 'SortOrder' => 'Int', |
|
| 56 | + ); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + private static $has_one = array( |
|
| 62 | + 'Product' => ProductPage::class, |
|
| 63 | + 'ProductOptionGroup' => OptionGroup::class, |
|
| 64 | + ); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + private static $belongs_many_many = array( |
|
| 70 | + 'OrderDetails' => OrderDetail::class, |
|
| 71 | + ); |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + private static $defaults = array( |
|
| 77 | + 'Available' => true, |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @var array |
|
| 82 | + */ |
|
| 83 | + private static $summary_fields = array( |
|
| 84 | + 'Title' => 'Title', |
|
| 85 | + 'ProductOptionGroup.Title' => 'Group', |
|
| 86 | + 'IsAvailable' => 'Available', |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @var array |
|
| 91 | + */ |
|
| 92 | + private static $searchable_fields = [ |
|
| 93 | + 'Title' => [ |
|
| 94 | + 'title' => 'Title', |
|
| 95 | + ], |
|
| 96 | + 'ProductOptionGroup.Title' => [ |
|
| 97 | + 'title' => 'Group', |
|
| 98 | + ], |
|
| 99 | + ]; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @var string |
|
| 103 | + */ |
|
| 104 | + private static $default_sort = 'SortOrder'; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @var string |
|
| 108 | + */ |
|
| 109 | + private static $table_name = 'OptionItem'; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return FieldList |
|
| 113 | + */ |
|
| 114 | + public function getCMSFields() |
|
| 115 | + { |
|
| 116 | + $fields = parent::getCMSFields(); |
|
| 117 | + |
|
| 118 | + $fields->removeByName([ |
|
| 119 | + 'OrderDetails', |
|
| 120 | + 'SortOrder', |
|
| 121 | + 'ProductID', |
|
| 122 | + 'WeightModifier', |
|
| 123 | + 'CodeModifier', |
|
| 124 | + 'PriceModifier', |
|
| 125 | + 'WeightModifierAction', |
|
| 126 | + 'CodeModifierAction', |
|
| 127 | + 'PriceModifierAction', |
|
| 128 | + ]); |
|
| 129 | + |
|
| 130 | + if ($this->ProductID) { |
|
| 131 | + $product = ProductPage::get()->byID($this->ProductID); |
|
| 132 | + |
|
| 133 | + $parentPrice = $product->obj('Price')->Nice(); |
|
| 134 | + $parentWeight = $product->Weight; |
|
| 135 | + $parentCode = $product->Code; |
|
| 136 | + |
|
| 137 | + $fields->addFieldsToTab('Root.Modifiers', [ |
|
| 138 | + HeaderField::create('ModifyHD', _t( |
|
| 139 | + 'OptionItem.ModifyHD', |
|
| 140 | + 'Product Option Modifiers' |
|
| 141 | + ), 2), |
|
| 142 | + |
|
| 143 | + // Weight Modifier Fields |
|
| 144 | + HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 3), |
|
| 145 | + TextField::create('WeightModifier') |
|
| 146 | + ->setTitle(_t('OptionItem.WeightModifier', 'Weight')), |
|
| 147 | + DropdownField::create( |
|
| 148 | + 'WeightModifierAction', |
|
| 149 | + _t('OptionItem.WeightModifierAction', 'Weight Modification'), |
|
| 150 | + [ |
|
| 151 | + 'Add' => _t( |
|
| 152 | + 'OptionItem.WeightAdd', |
|
| 153 | + 'Add to Base Weight ({weight})', |
|
| 154 | + 'Add to weight', |
|
| 155 | + ['weight' => $parentWeight] |
|
| 156 | + ), |
|
| 157 | + 'Subtract' => _t( |
|
| 158 | + 'OptionItem.WeightSubtract', |
|
| 159 | + 'Subtract from Base Weight ({weight})', |
|
| 160 | + 'Subtract from weight', |
|
| 161 | + ['weight' => $parentWeight] |
|
| 162 | + ), |
|
| 163 | + 'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight'), |
|
| 164 | + ] |
|
| 165 | + )->setEmptyString('') |
|
| 166 | + ->setDescription(_t( |
|
| 167 | + 'OptionItem.WeightDescription', |
|
| 168 | + 'Does weight modify or replace base weight?' |
|
| 169 | + )), |
|
| 170 | + |
|
| 171 | + // Price Modifier FIelds |
|
| 172 | + HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 3), |
|
| 173 | + CurrencyField::create('PriceModifier') |
|
| 174 | + ->setTitle(_t('OptionItem.PriceModifier', 'Price')), |
|
| 175 | + DropdownField::create( |
|
| 176 | + 'PriceModifierAction', |
|
| 177 | + _t('OptionItem.PriceModifierAction', 'Price Modification'), |
|
| 178 | + [ |
|
| 179 | + 'Add' => _t( |
|
| 180 | + 'OptionItem.PriceAdd', |
|
| 181 | + 'Add to Base Price ({price})', |
|
| 182 | + 'Add to price', |
|
| 183 | + ['price' => $parentPrice] |
|
| 184 | + ), |
|
| 185 | + 'Subtract' => _t( |
|
| 186 | + 'OptionItem.PriceSubtract', |
|
| 187 | + 'Subtract from Base Price ({price})', |
|
| 188 | + 'Subtract from price', |
|
| 189 | + ['price' => $parentPrice] |
|
| 190 | + ), |
|
| 191 | + 'Set' => _t('OptionItem.PriceSet', 'Set as a new Price'), |
|
| 192 | + ] |
|
| 193 | + )->setEmptyString('') |
|
| 194 | + ->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')), |
|
| 195 | + |
|
| 196 | + // Code Modifier Fields |
|
| 197 | + HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 3), |
|
| 198 | + TextField::create('CodeModifier') |
|
| 199 | + ->setTitle(_t('OptionItem.CodeModifier', 'Code')), |
|
| 200 | + DropdownField::create( |
|
| 201 | + 'CodeModifierAction', |
|
| 202 | + _t('OptionItem.CodeModifierAction', 'Code Modification'), |
|
| 203 | + [ |
|
| 204 | + 'Add' => _t( |
|
| 205 | + 'OptionItem.CodeAdd', |
|
| 206 | + 'Add to Base Code ({code})', |
|
| 207 | + 'Add to code', |
|
| 208 | + ['code' => $parentCode] |
|
| 209 | + ), |
|
| 210 | + 'Subtract' => _t( |
|
| 211 | + 'OptionItem.CodeSubtract', |
|
| 212 | + 'Subtract from Base Code ({code})', |
|
| 213 | + 'Subtract from code', |
|
| 214 | + ['code' => $parentCode] |
|
| 215 | + ), |
|
| 216 | + 'Set' => _t('OptionItem.CodeSet', 'Set as a new Code'), |
|
| 217 | + ] |
|
| 218 | + )->setEmptyString('') |
|
| 219 | + ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')), |
|
| 220 | + ]); |
|
| 221 | + } else { |
|
| 222 | + $fields->addFieldsToTab( |
|
| 223 | + 'Root.Modifiers', |
|
| 224 | + [ |
|
| 225 | + HeaderField::create( |
|
| 226 | + 'ModifyHeader', |
|
| 227 | + 'Modifiers can be set when on a Product Page', |
|
| 228 | + 3 |
|
| 229 | + ), |
|
| 230 | + ] |
|
| 231 | + ); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // ProductOptionGroup Dropdown field w/ add new |
|
| 235 | + $groups = function () { |
|
| 236 | + return OptionGroup::get()->map()->toArray(); |
|
| 237 | + }; |
|
| 238 | + $groupFields = singleton(OptionGroup::class)->getCMSFields(); |
|
| 239 | + $groupField = DropdownField::create('ProductOptionGroupID', _t( |
|
| 240 | + 'OptionItem.Group', |
|
| 241 | + 'Group' |
|
| 242 | + ), $groups()) |
|
| 243 | + ->setEmptyString('') |
|
| 244 | + ->setDescription(_t( |
|
| 245 | + 'OptionItem.GroupDescription', |
|
| 246 | + 'Name of this group of options. Managed in <a href="admin/settings"> |
|
| 247 | 247 | Settings > FoxyStripe > Option Groups |
| 248 | 248 | </a>' |
| 249 | - )); |
|
| 250 | - if (class_exists('QuickAddNewExtension')) { |
|
| 251 | - $groupField->useAddNew('OptionGroup', $groups, $groupFields); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - $fields->addFieldsToTab('Root.Main', array( |
|
| 255 | - TextField::create('Title') |
|
| 256 | - ->setTitle(_t('OptionItem.Title', 'Product Option Name')), |
|
| 257 | - CheckboxField::create('Available') |
|
| 258 | - ->setTitle(_t('OptionItem.Available', 'Available for purchase')) |
|
| 259 | - ->setDescription(_t( |
|
| 260 | - 'OptionItem.AvailableDescription', |
|
| 261 | - 'If unchecked, will disable this option in the drop down menu' |
|
| 262 | - )), |
|
| 263 | - $groupField, |
|
| 264 | - )); |
|
| 265 | - |
|
| 266 | - return $fields; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * @return ValidationResult |
|
| 271 | - */ |
|
| 272 | - public function validate() |
|
| 273 | - { |
|
| 274 | - $result = parent::validate(); |
|
| 275 | - |
|
| 276 | - if ($this->ProductOptionGroupID == 0) { |
|
| 277 | - $result->addError('Must set a Group prior to saving'); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - return $result; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * @param $oma |
|
| 285 | - * @param bool $returnWithOnlyPlusMinus |
|
| 286 | - * |
|
| 287 | - * @return string |
|
| 288 | - */ |
|
| 289 | - public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false) |
|
| 290 | - { |
|
| 291 | - switch ($oma) { |
|
| 292 | - case 'Subtract': |
|
| 293 | - $symbol = '-'; |
|
| 294 | - break; |
|
| 295 | - case 'Set': |
|
| 296 | - $symbol = ($returnWithOnlyPlusMinus) ? '' : ':'; |
|
| 297 | - break; |
|
| 298 | - default: |
|
| 299 | - $symbol = '+'; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - return $symbol; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * @return string |
|
| 307 | - */ |
|
| 308 | - public function getWeightModifierWithSymbol() |
|
| 309 | - { |
|
| 310 | - return self::getOptionModifierActionSymbol($this->WeightModifierAction).$this->WeightModifier; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @return string |
|
| 315 | - */ |
|
| 316 | - public function getPriceModifierWithSymbol() |
|
| 317 | - { |
|
| 318 | - return self::getOptionModifierActionSymbol($this->PriceModifierAction).$this->PriceModifier; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * @return string |
|
| 323 | - */ |
|
| 324 | - public function getCodeModifierWithSymbol() |
|
| 325 | - { |
|
| 326 | - return self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * @return mixed |
|
| 331 | - */ |
|
| 332 | - public function getProductOptionGroupTitle() |
|
| 333 | - { |
|
| 334 | - return $this->ProductOptionGroup()->Title; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * @return string |
|
| 339 | - */ |
|
| 340 | - public function getGeneratedValue() |
|
| 341 | - { |
|
| 342 | - $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0'; |
|
| 343 | - $modPriceWithSymbol = self::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
|
| 344 | - $modWeight = ($this->WeightModifier) ? (string) $this->WeightModifier : '0'; |
|
| 345 | - $modWeight = self::getOptionModifierActionSymbol($this->WeightModifierAction).$modWeight; |
|
| 346 | - $modCode = self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 347 | - |
|
| 348 | - return $this->Title.'{p'.$modPriceWithSymbol.'|w'.$modWeight.'|c'.$modCode.'}'; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * @return mixed|string |
|
| 353 | - */ |
|
| 354 | - public function getGeneratedTitle() |
|
| 355 | - { |
|
| 356 | - $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0'; |
|
| 357 | - $title = $this->Title; |
|
| 358 | - $title .= ($this->PriceModifier != 0) ? |
|
| 359 | - ': ('.self::getOptionModifierActionSymbol( |
|
| 360 | - $this->PriceModifierAction, |
|
| 361 | - $returnWithOnlyPlusMinus = true |
|
| 362 | - ).'$'.$modPrice.')' : |
|
| 363 | - ''; |
|
| 364 | - |
|
| 365 | - return $title; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * @return bool |
|
| 370 | - */ |
|
| 371 | - public function getAvailability() |
|
| 372 | - { |
|
| 373 | - $available = ($this->Available == 1) ? true : false; |
|
| 374 | - |
|
| 375 | - $this->extend('updateOptionAvailability', $available); |
|
| 376 | - |
|
| 377 | - return $available; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * @return string |
|
| 382 | - */ |
|
| 383 | - public function getIsAvailable() |
|
| 384 | - { |
|
| 385 | - if ($this->getAvailability()) { |
|
| 386 | - return 'yes'; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - return 'no'; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * @param bool $member |
|
| 394 | - * |
|
| 395 | - * @return bool |
|
| 396 | - */ |
|
| 397 | - public function canView($member = false) |
|
| 398 | - { |
|
| 399 | - return true; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * @param null $member |
|
| 404 | - * |
|
| 405 | - * @return bool|int |
|
| 406 | - */ |
|
| 407 | - public function canEdit($member = null) |
|
| 408 | - { |
|
| 409 | - return Permission::check('Product_CANCRUD'); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * @param null $member |
|
| 414 | - * |
|
| 415 | - * @return bool|int |
|
| 416 | - */ |
|
| 417 | - public function canDelete($member = null) |
|
| 418 | - { |
|
| 419 | - return Permission::check('Product_CANCRUD'); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * @param null $member |
|
| 424 | - * @param array $context |
|
| 425 | - * |
|
| 426 | - * @return bool|int |
|
| 427 | - */ |
|
| 428 | - public function canCreate($member = null, $context = []) |
|
| 429 | - { |
|
| 430 | - return Permission::check('Product_CANCRUD'); |
|
| 431 | - } |
|
| 249 | + )); |
|
| 250 | + if (class_exists('QuickAddNewExtension')) { |
|
| 251 | + $groupField->useAddNew('OptionGroup', $groups, $groupFields); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + $fields->addFieldsToTab('Root.Main', array( |
|
| 255 | + TextField::create('Title') |
|
| 256 | + ->setTitle(_t('OptionItem.Title', 'Product Option Name')), |
|
| 257 | + CheckboxField::create('Available') |
|
| 258 | + ->setTitle(_t('OptionItem.Available', 'Available for purchase')) |
|
| 259 | + ->setDescription(_t( |
|
| 260 | + 'OptionItem.AvailableDescription', |
|
| 261 | + 'If unchecked, will disable this option in the drop down menu' |
|
| 262 | + )), |
|
| 263 | + $groupField, |
|
| 264 | + )); |
|
| 265 | + |
|
| 266 | + return $fields; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * @return ValidationResult |
|
| 271 | + */ |
|
| 272 | + public function validate() |
|
| 273 | + { |
|
| 274 | + $result = parent::validate(); |
|
| 275 | + |
|
| 276 | + if ($this->ProductOptionGroupID == 0) { |
|
| 277 | + $result->addError('Must set a Group prior to saving'); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + return $result; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * @param $oma |
|
| 285 | + * @param bool $returnWithOnlyPlusMinus |
|
| 286 | + * |
|
| 287 | + * @return string |
|
| 288 | + */ |
|
| 289 | + public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false) |
|
| 290 | + { |
|
| 291 | + switch ($oma) { |
|
| 292 | + case 'Subtract': |
|
| 293 | + $symbol = '-'; |
|
| 294 | + break; |
|
| 295 | + case 'Set': |
|
| 296 | + $symbol = ($returnWithOnlyPlusMinus) ? '' : ':'; |
|
| 297 | + break; |
|
| 298 | + default: |
|
| 299 | + $symbol = '+'; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + return $symbol; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * @return string |
|
| 307 | + */ |
|
| 308 | + public function getWeightModifierWithSymbol() |
|
| 309 | + { |
|
| 310 | + return self::getOptionModifierActionSymbol($this->WeightModifierAction).$this->WeightModifier; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @return string |
|
| 315 | + */ |
|
| 316 | + public function getPriceModifierWithSymbol() |
|
| 317 | + { |
|
| 318 | + return self::getOptionModifierActionSymbol($this->PriceModifierAction).$this->PriceModifier; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * @return string |
|
| 323 | + */ |
|
| 324 | + public function getCodeModifierWithSymbol() |
|
| 325 | + { |
|
| 326 | + return self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * @return mixed |
|
| 331 | + */ |
|
| 332 | + public function getProductOptionGroupTitle() |
|
| 333 | + { |
|
| 334 | + return $this->ProductOptionGroup()->Title; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * @return string |
|
| 339 | + */ |
|
| 340 | + public function getGeneratedValue() |
|
| 341 | + { |
|
| 342 | + $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0'; |
|
| 343 | + $modPriceWithSymbol = self::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
|
| 344 | + $modWeight = ($this->WeightModifier) ? (string) $this->WeightModifier : '0'; |
|
| 345 | + $modWeight = self::getOptionModifierActionSymbol($this->WeightModifierAction).$modWeight; |
|
| 346 | + $modCode = self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 347 | + |
|
| 348 | + return $this->Title.'{p'.$modPriceWithSymbol.'|w'.$modWeight.'|c'.$modCode.'}'; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * @return mixed|string |
|
| 353 | + */ |
|
| 354 | + public function getGeneratedTitle() |
|
| 355 | + { |
|
| 356 | + $modPrice = ($this->PriceModifier) ? (string) $this->PriceModifier : '0'; |
|
| 357 | + $title = $this->Title; |
|
| 358 | + $title .= ($this->PriceModifier != 0) ? |
|
| 359 | + ': ('.self::getOptionModifierActionSymbol( |
|
| 360 | + $this->PriceModifierAction, |
|
| 361 | + $returnWithOnlyPlusMinus = true |
|
| 362 | + ).'$'.$modPrice.')' : |
|
| 363 | + ''; |
|
| 364 | + |
|
| 365 | + return $title; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * @return bool |
|
| 370 | + */ |
|
| 371 | + public function getAvailability() |
|
| 372 | + { |
|
| 373 | + $available = ($this->Available == 1) ? true : false; |
|
| 374 | + |
|
| 375 | + $this->extend('updateOptionAvailability', $available); |
|
| 376 | + |
|
| 377 | + return $available; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * @return string |
|
| 382 | + */ |
|
| 383 | + public function getIsAvailable() |
|
| 384 | + { |
|
| 385 | + if ($this->getAvailability()) { |
|
| 386 | + return 'yes'; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + return 'no'; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * @param bool $member |
|
| 394 | + * |
|
| 395 | + * @return bool |
|
| 396 | + */ |
|
| 397 | + public function canView($member = false) |
|
| 398 | + { |
|
| 399 | + return true; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * @param null $member |
|
| 404 | + * |
|
| 405 | + * @return bool|int |
|
| 406 | + */ |
|
| 407 | + public function canEdit($member = null) |
|
| 408 | + { |
|
| 409 | + return Permission::check('Product_CANCRUD'); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * @param null $member |
|
| 414 | + * |
|
| 415 | + * @return bool|int |
|
| 416 | + */ |
|
| 417 | + public function canDelete($member = null) |
|
| 418 | + { |
|
| 419 | + return Permission::check('Product_CANCRUD'); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * @param null $member |
|
| 424 | + * @param array $context |
|
| 425 | + * |
|
| 426 | + * @return bool|int |
|
| 427 | + */ |
|
| 428 | + public function canCreate($member = null, $context = []) |
|
| 429 | + { |
|
| 430 | + return Permission::check('Product_CANCRUD'); |
|
| 431 | + } |
|
| 432 | 432 | } |
@@ -16,49 +16,49 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class CustomerExtension extends DataExtension |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - private static $db = [ |
|
| 23 | - 'Customer_ID' => 'Int', |
|
| 24 | - ]; |
|
| 19 | + /** |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + private static $db = [ |
|
| 23 | + 'Customer_ID' => 'Int', |
|
| 24 | + ]; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - private static $has_many = [ |
|
| 30 | - 'Orders' => Order::class, |
|
| 31 | - ]; |
|
| 26 | + /** |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + private static $has_many = [ |
|
| 30 | + 'Orders' => Order::class, |
|
| 31 | + ]; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @var array |
|
| 35 | - */ |
|
| 36 | - private static $indexes = [ |
|
| 37 | - 'Customer_ID' => true, // make unique |
|
| 38 | - ]; |
|
| 33 | + /** |
|
| 34 | + * @var array |
|
| 35 | + */ |
|
| 36 | + private static $indexes = [ |
|
| 37 | + 'Customer_ID' => true, // make unique |
|
| 38 | + ]; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @throws \Psr\Container\NotFoundExceptionInterface |
|
| 42 | - */ |
|
| 43 | - public function onBeforeWrite() |
|
| 44 | - { |
|
| 45 | - parent::onBeforeWrite(); |
|
| 40 | + /** |
|
| 41 | + * @throws \Psr\Container\NotFoundExceptionInterface |
|
| 42 | + */ |
|
| 43 | + public function onBeforeWrite() |
|
| 44 | + { |
|
| 45 | + parent::onBeforeWrite(); |
|
| 46 | 46 | |
| 47 | - // if Member data was imported from FoxyCart, PasswordEncryption will be set to 'none'. |
|
| 48 | - // Change to sh1_v2.4 to ensure SilverStripe is using the same hash as FoxyCart API 1.1 |
|
| 49 | - if (!$this->owner->PasswordEncryption && ( |
|
| 50 | - $this->owner->PasswordEncryption == null || $this->owner->PasswordEncryption == 'none' |
|
| 51 | - )) { |
|
| 52 | - $this->owner->PasswordEncryption = 'sha1_v2.4'; |
|
| 53 | - } |
|
| 47 | + // if Member data was imported from FoxyCart, PasswordEncryption will be set to 'none'. |
|
| 48 | + // Change to sh1_v2.4 to ensure SilverStripe is using the same hash as FoxyCart API 1.1 |
|
| 49 | + if (!$this->owner->PasswordEncryption && ( |
|
| 50 | + $this->owner->PasswordEncryption == null || $this->owner->PasswordEncryption == 'none' |
|
| 51 | + )) { |
|
| 52 | + $this->owner->PasswordEncryption = 'sha1_v2.4'; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - // Send updated customer data to Foxy Cart via API |
|
| 56 | - $response = FoxyCart::putCustomer($this->owner); |
|
| 55 | + // Send updated customer data to Foxy Cart via API |
|
| 56 | + $response = FoxyCart::putCustomer($this->owner); |
|
| 57 | 57 | |
| 58 | - // Grab customer_id record from FoxyCart response, store in Member |
|
| 59 | - if ($response) { |
|
| 60 | - $foxyResponse = new \SimpleXMLElement($response); |
|
| 61 | - $this->owner->Customer_ID = (int)$foxyResponse->customer_id; |
|
| 62 | - } |
|
| 63 | - } |
|
| 58 | + // Grab customer_id record from FoxyCart response, store in Member |
|
| 59 | + if ($response) { |
|
| 60 | + $foxyResponse = new \SimpleXMLElement($response); |
|
| 61 | + $this->owner->Customer_ID = (int)$foxyResponse->customer_id; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -11,18 +11,18 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class FoxyStripeProductFormFieldExtension extends DataExtension |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @param $attributes |
|
| 16 | - */ |
|
| 17 | - public function updateAttributes(&$attributes) |
|
| 18 | - { |
|
| 19 | - if (Controller::curr() instanceof ContentController && |
|
| 20 | - Controller::curr()->data()->Classname == 'DonationProduct' |
|
| 21 | - ) { |
|
| 22 | - if (preg_match('/^(product_id)/', $this->owner->getName())) { |
|
| 23 | - $attributes['h:name'] = $attributes['name']; |
|
| 24 | - unset($attributes['name']); |
|
| 25 | - } |
|
| 26 | - } |
|
| 27 | - } |
|
| 14 | + /** |
|
| 15 | + * @param $attributes |
|
| 16 | + */ |
|
| 17 | + public function updateAttributes(&$attributes) |
|
| 18 | + { |
|
| 19 | + if (Controller::curr() instanceof ContentController && |
|
| 20 | + Controller::curr()->data()->Classname == 'DonationProduct' |
|
| 21 | + ) { |
|
| 22 | + if (preg_match('/^(product_id)/', $this->owner->getName())) { |
|
| 23 | + $attributes['h:name'] = $attributes['name']; |
|
| 24 | + unset($attributes['name']); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -6,43 +6,43 @@ |
||
| 6 | 6 | |
| 7 | 7 | class DonationProduct extends ProductPage |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var string |
|
| 11 | - */ |
|
| 12 | - private static $singular_name = 'Donation'; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - private static $plural_name = 'Donations'; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - //private static $allowed_children = []; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @var bool |
|
| 26 | - */ |
|
| 27 | - private static $can_be_root = true; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - private static $table_name = 'DonationProduct'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @return FieldList |
|
| 36 | - */ |
|
| 37 | - public function getCMSFields() |
|
| 38 | - { |
|
| 39 | - $fields = parent::getCMSFields(); |
|
| 40 | - |
|
| 41 | - $fields->removeByName([ |
|
| 42 | - 'Weight', |
|
| 43 | - 'Price', |
|
| 44 | - ]); |
|
| 45 | - |
|
| 46 | - return $fields; |
|
| 47 | - } |
|
| 9 | + /** |
|
| 10 | + * @var string |
|
| 11 | + */ |
|
| 12 | + private static $singular_name = 'Donation'; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + private static $plural_name = 'Donations'; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + //private static $allowed_children = []; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @var bool |
|
| 26 | + */ |
|
| 27 | + private static $can_be_root = true; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + private static $table_name = 'DonationProduct'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @return FieldList |
|
| 36 | + */ |
|
| 37 | + public function getCMSFields() |
|
| 38 | + { |
|
| 39 | + $fields = parent::getCMSFields(); |
|
| 40 | + |
|
| 41 | + $fields->removeByName([ |
|
| 42 | + 'Weight', |
|
| 43 | + 'Price', |
|
| 44 | + ]); |
|
| 45 | + |
|
| 46 | + return $fields; |
|
| 47 | + } |
|
| 48 | 48 | } |