@@ -43,6 +43,9 @@ |
||
| 43 | 43 | self::setSecret(FoxyCart::getStoreKey()); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | + /** |
|
| 47 | + * @return string |
|
| 48 | + */ |
|
| 46 | 49 | public static function getSecret(){ |
| 47 | 50 | return FoxyCart::getStoreKey(); |
| 48 | 51 | } |
@@ -30,20 +30,20 @@ discard block |
||
| 30 | 30 | // protected static $cart_url = 'https://yourdomain.foxycart.com/cart'; |
| 31 | 31 | protected static $cart_url; |
| 32 | 32 | |
| 33 | - public static function setCartURL($storeName = null){ |
|
| 33 | + public static function setCartURL($storeName = null) { |
|
| 34 | 34 | self::$cart_url = 'https://'.$storeName.'.faxycart.com/cart'; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - public static function setSecret($secret = null){ |
|
| 37 | + public static function setSecret($secret = null) { |
|
| 38 | 38 | self::$secret = $secret; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - public function __construct(){ |
|
| 41 | + public function __construct() { |
|
| 42 | 42 | self::setCartURL(FoxyCart::getFoxyCartStoreName()); |
| 43 | 43 | self::setSecret(FoxyCart::getStoreKey()); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - public static function getSecret(){ |
|
| 46 | + public static function getSecret() { |
|
| 47 | 47 | return FoxyCart::getStoreKey(); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -107,7 +107,7 @@ discard block |
||
| 107 | 107 | $codes[$pair['prefix']] = $pair['value']; |
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | - if ( ! count($codes)) { |
|
| 110 | + if (!count($codes)) { |
|
| 111 | 111 | self::$log[] = '<strong style="color:#600;">No code found</strong> for the above link.'; |
| 112 | 112 | return $fail; |
| 113 | 113 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | |
| 132 | 132 | if ($output) { |
| 133 | 133 | echo self::$cart_url.'?'.$qs; |
| 134 | - } else { |
|
| 134 | + }else { |
|
| 135 | 135 | return self::$cart_url.'?'.$qs; |
| 136 | 136 | } |
| 137 | 137 | } |
@@ -149,18 +149,18 @@ discard block |
||
| 149 | 149 | if ($option_value == '--OPEN--') { |
| 150 | 150 | $hash = hash_hmac('sha256', $product_code.$option_name.$option_value, self::getSecret()); |
| 151 | 151 | $value = ($urlencode) ? urlencode($option_name).'||'.$hash.'||open' : $option_name.'||'.$hash.'||open'; |
| 152 | - } else { |
|
| 152 | + }else { |
|
| 153 | 153 | $hash = hash_hmac('sha256', $product_code.$option_name.$option_value, self::getSecret()); |
| 154 | 154 | if ($method == 'name') { |
| 155 | 155 | $value = ($urlencode) ? urlencode($option_name).'||'.$hash : $option_name.'||'.$hash; |
| 156 | - } else { |
|
| 156 | + }else { |
|
| 157 | 157 | $value = ($urlencode) ? urlencode($option_value).'||'.$hash : $option_value.'||'.$hash; |
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | if ($output) { |
| 162 | 162 | echo $value; |
| 163 | - } else { |
|
| 163 | + }else { |
|
| 164 | 164 | return $value; |
| 165 | 165 | } |
| 166 | 166 | } |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | $value[2] = ($value[2] == '') ? '--OPEN--' : $value[2]; |
| 249 | 249 | if ($type[2] == 'radio') { |
| 250 | 250 | $input_signed = preg_replace('%([\'"])'.preg_quote($value[2]).'\1%', '${1}'.self::fc_hash_value($code, $name[2], $value[2], 'value', FALSE)."$1", $input); |
| 251 | - } else { |
|
| 251 | + }else { |
|
| 252 | 252 | $input_signed = preg_replace('%([\'"])'.$prefix.preg_quote($name[2]).'\1%', '${1}'.$prefix.self::fc_hash_value($code, $name[2], $value[2], 'name', FALSE)."$1", $input); |
| 253 | 253 | } |
| 254 | 254 | self::$log[] = '<strong>INPUT:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
@@ -267,14 +267,14 @@ discard block |
||
| 267 | 267 | $count['lists']++; |
| 268 | 268 | preg_match_all('%<option [^>]*value=([\'"])(.+?)\1[^>]*>(?:.*?)</option>%i', $list[0], $options, PREG_SET_ORDER); |
| 269 | 269 | self::$log[] = '<strong>Options:</strong> <pre>'.htmlspecialchars(print_r($options, true)).'</pre>'; |
| 270 | - unset( $form_part_signed ); |
|
| 270 | + unset($form_part_signed); |
|
| 271 | 271 | foreach ($options as $option) { |
| 272 | - if( !isset($form_part_signed) ) $form_part_signed = $list[0]; |
|
| 272 | + if (!isset($form_part_signed)) $form_part_signed = $list[0]; |
|
| 273 | 273 | $option_signed = preg_replace( |
| 274 | 274 | '%'.preg_quote($option[1]).preg_quote($option[2]).preg_quote($option[1]).'%', |
| 275 | 275 | $option[1].self::fc_hash_value($code, $list[2], $option[2], 'value', FALSE).$option[1], |
| 276 | 276 | $option[0]); |
| 277 | - $form_part_signed = str_replace($option[0], $option_signed, $form_part_signed ); |
|
| 277 | + $form_part_signed = str_replace($option[0], $option_signed, $form_part_signed); |
|
| 278 | 278 | self::$log[] = '<strong>OPTION:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
| 279 | 279 | '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$list[2]). |
| 280 | 280 | '</strong> :: Value: <strong>'.htmlspecialchars($option[2]). |
@@ -14,315 +14,315 @@ |
||
| 14 | 14 | * - Empty textareas are assumed to be "open" |
| 15 | 15 | */ |
| 16 | 16 | class FoxyCart_Helper { |
| 17 | - /** |
|
| 18 | - * API Key (Secret) |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - **/ |
|
| 22 | - private static $secret; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Cart URL |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - * Notes: Could be 'https://yourdomain.foxycart.com/cart' or 'https://secure.yourdomain.com/cart' |
|
| 29 | - **/ |
|
| 30 | - // protected static $cart_url = 'https://yourdomain.foxycart.com/cart'; |
|
| 31 | - protected static $cart_url; |
|
| 32 | - |
|
| 33 | - public static function setCartURL($storeName = null){ |
|
| 34 | - self::$cart_url = 'https://'.$storeName.'.faxycart.com/cart'; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public static function setSecret($secret = null){ |
|
| 38 | - self::$secret = $secret; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function __construct(){ |
|
| 42 | - self::setCartURL(FoxyCart::getFoxyCartStoreName()); |
|
| 43 | - self::setSecret(FoxyCart::getStoreKey()); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public static function getSecret(){ |
|
| 47 | - return FoxyCart::getStoreKey(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Cart Excludes |
|
| 53 | - * |
|
| 54 | - * Arrays of values and prefixes that should be ignored when signing links and forms. |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected static $cart_excludes = array( |
|
| 58 | - // Cart values |
|
| 59 | - 'cart', 'fcsid', 'empty', 'coupon', 'output', 'sub_token', 'redirect', 'callback', '_', |
|
| 60 | - // Checkout pre-population values |
|
| 61 | - 'customer_email', 'customer_first_name', 'customer_last_name', 'customer_address1', 'customer_address2', |
|
| 62 | - 'customer_city', 'customer_state', 'customer_postal_code', 'customer_country', 'customer_phone', 'customer_company', |
|
| 63 | - 'shipping_first_name', 'shipping_last_name', 'shipping_address1', 'shipping_address2', |
|
| 64 | - 'shipping_city', 'shipping_state', 'shipping_postal_code', 'shipping_country', 'shipping_phone', 'shipping_company', |
|
| 65 | - ); |
|
| 66 | - protected static $cart_excludes_prefixes = array( |
|
| 67 | - 'h:', 'x:', '__', |
|
| 68 | - ); |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Debugging |
|
| 72 | - * |
|
| 73 | - * Set to $debug to TRUE to enable debug logging. |
|
| 74 | - * |
|
| 75 | - */ |
|
| 76 | - protected static $debug = FALSE; |
|
| 77 | - protected static $log = array(); |
|
| 78 | - |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * "Link Method": Generate HMAC SHA256 for GET Query Strings |
|
| 82 | - * |
|
| 83 | - * Notes: Can't parse_str because PHP doesn't support non-alphanumeric characters as array keys. |
|
| 84 | - * @return string |
|
| 85 | - **/ |
|
| 86 | - public static function fc_hash_querystring($qs, $output = TRUE) { |
|
| 87 | - self::$log[] = '<strong>Signing link</strong> with data: '.htmlspecialchars(substr($qs, 0, 150)).'...'; |
|
| 88 | - $fail = self::$cart_url.'?'.$qs; |
|
| 89 | - |
|
| 90 | - // If the link appears to be hashed already, don't bother |
|
| 91 | - if (strpos($qs, '||')) { |
|
| 92 | - self::$log[] = '<strong>Link appears to be signed already</strong>: '.htmlspecialchars($code[0]); |
|
| 93 | - return $fail; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // Stick an ampersand on the beginning of the querystring to make matching the first element a little easier |
|
| 97 | - $qs = '&'.urldecode($qs); |
|
| 98 | - |
|
| 99 | - // Get all the prefixes, codes, and name=value pairs |
|
| 100 | - preg_match_all('%(?P<amp>&(?:amp;)?)(?P<prefix>[a-z0-9]{1,3}:)?(?P<name>[^=]+)=(?P<value>[^&]+)%', $qs, $pairs, PREG_SET_ORDER); |
|
| 101 | - self::$log[] = 'Found the following pairs to sign:<pre>'.htmlspecialchars(print_r($pairs, true)).'</pre>'; |
|
| 102 | - |
|
| 103 | - // Get all the "code" values, set the matches in $codes |
|
| 104 | - $codes = array(); |
|
| 105 | - foreach ($pairs as $pair) { |
|
| 106 | - if ($pair['name'] == 'code') { |
|
| 107 | - $codes[$pair['prefix']] = $pair['value']; |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - if ( ! count($codes)) { |
|
| 111 | - self::$log[] = '<strong style="color:#600;">No code found</strong> for the above link.'; |
|
| 112 | - return $fail; |
|
| 113 | - } |
|
| 114 | - self::$log[] = '<strong style="color:orange;">CODES found:</strong> '.htmlspecialchars(print_r($codes, true)); |
|
| 115 | - |
|
| 116 | - // Sign the name/value pairs |
|
| 117 | - foreach ($pairs as $pair) { |
|
| 118 | - // Skip the cart excludes |
|
| 119 | - if (in_array($pair['name'], self::$cart_excludes) || in_array($pair['prefix'], self::$cart_excludes_prefixes)) { |
|
| 120 | - self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$pair['prefix'].$pair['name'].'" = '.$pair['value']; |
|
| 121 | - continue; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - // Continue to sign the value and replace the name=value in the querystring with name=value||hash |
|
| 125 | - $value = self::fc_hash_value($codes[$pair['prefix']], $pair['name'], $pair['value'], 'value', FALSE, 'urlencode'); |
|
| 126 | - $replacement = $pair['amp'].$pair['prefix'].urlencode($pair['name']).'='.$value; |
|
| 127 | - $qs = str_replace($pair[0], $replacement, $qs); |
|
| 128 | - self::$log[] = 'Signed <strong>'.$pair['name'].'</strong> = <strong>'.$pair['value'].'</strong> with '.$replacement.'.<br />Replacing: '.$pair[0].'<br />With... '.$replacement; |
|
| 129 | - } |
|
| 130 | - $qs = ltrim($qs, '&'); // Get rid of that leading ampersand we added earlier |
|
| 131 | - |
|
| 132 | - if ($output) { |
|
| 133 | - echo self::$cart_url.'?'.$qs; |
|
| 134 | - } else { |
|
| 135 | - return self::$cart_url.'?'.$qs; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * "Form Method": Generate HMAC SHA256 for form elements or individual <input />s |
|
| 142 | - * |
|
| 143 | - * @return string |
|
| 144 | - **/ |
|
| 145 | - public static function fc_hash_value($product_code, $option_name, $option_value = '', $method = 'name', $output = TRUE, $urlencode = false) { |
|
| 146 | - if (!$product_code || !$option_name) { |
|
| 147 | - return FALSE; |
|
| 148 | - } |
|
| 149 | - if ($option_value == '--OPEN--') { |
|
| 150 | - $hash = hash_hmac('sha256', $product_code.$option_name.$option_value, self::getSecret()); |
|
| 151 | - $value = ($urlencode) ? urlencode($option_name).'||'.$hash.'||open' : $option_name.'||'.$hash.'||open'; |
|
| 152 | - } else { |
|
| 153 | - $hash = hash_hmac('sha256', $product_code.$option_name.$option_value, self::getSecret()); |
|
| 154 | - if ($method == 'name') { |
|
| 155 | - $value = ($urlencode) ? urlencode($option_name).'||'.$hash : $option_name.'||'.$hash; |
|
| 156 | - } else { |
|
| 157 | - $value = ($urlencode) ? urlencode($option_value).'||'.$hash : $option_value.'||'.$hash; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ($output) { |
|
| 162 | - echo $value; |
|
| 163 | - } else { |
|
| 164 | - return $value; |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Raw HTML Signing: Sign all links and form elements in a block of HTML |
|
| 170 | - * |
|
| 171 | - * Accepts a string of HTML and signs all links and forms. |
|
| 172 | - * Requires link 'href' and form 'action' attributes to use 'https' and not 'http'. |
|
| 173 | - * Requires a 'code' to be set in every form. |
|
| 174 | - * |
|
| 175 | - * @return string |
|
| 176 | - **/ |
|
| 177 | - public static function fc_hash_html($html) { |
|
| 178 | - // Initialize some counting |
|
| 179 | - $count['temp'] = 0; // temp counter |
|
| 180 | - $count['links'] = 0; |
|
| 181 | - $count['forms'] = 0; |
|
| 182 | - $count['inputs'] = 0; |
|
| 183 | - $count['lists'] = 0; |
|
| 184 | - $count['textareas'] = 0; |
|
| 185 | - |
|
| 186 | - // Find and sign all the links |
|
| 187 | - preg_match_all('%<a .*?href=[\'"]'.preg_quote(self::$cart_url).'(?:\.php)?\?(.+?)[\'"].*?>%i', $html, $querystrings); |
|
| 188 | - // print_r($querystrings); |
|
| 189 | - foreach ($querystrings[1] as $querystring) { |
|
| 190 | - // If it's already signed, skip it. |
|
| 191 | - if (preg_match('%&(?:amp;)?hash=%i', $querystring)) { |
|
| 192 | - continue; |
|
| 193 | - } |
|
| 194 | - $pattern = '%(href=[\'"])'.preg_quote(self::$cart_url, '%').'(?:\.php)?\?'.preg_quote($querystring, '%').'([\'"])%i'; |
|
| 195 | - $signed = self::fc_hash_querystring($querystring, FALSE); |
|
| 196 | - $html = preg_replace($pattern, '$1'.$signed.'$2', $html, -1, $count['temp']); |
|
| 197 | - $count['links'] += $count['temp']; |
|
| 198 | - } |
|
| 199 | - unset($querystrings); |
|
| 200 | - |
|
| 201 | - // Find and sign all form values |
|
| 202 | - preg_match_all('%<form [^>]*?action=[\'"]'.preg_quote(self::$cart_url).'?[\'"].*?>(.+?)</form>%is', $html, $forms); |
|
| 203 | - foreach ($forms[1] as $form) { |
|
| 204 | - $count['forms']++; |
|
| 205 | - self::$log[] = '<strong>Signing form</strong> with data: '.htmlspecialchars(substr($form, 0, 150)).'...'; |
|
| 206 | - |
|
| 207 | - // Store the original form so we can replace it when we're done |
|
| 208 | - $form_original = $form; |
|
| 209 | - |
|
| 210 | - // Check for the "code" input, set the matches in $codes |
|
| 211 | - if (!preg_match_all('%<[^>]*?name=([\'"])([0-9]{1,3}:)?code\1[^>]*?>%i', $form, $codes, PREG_SET_ORDER)) { |
|
| 212 | - self::$log[] = '<strong style="color:#600;">No code found</strong> for the above form.'; |
|
| 213 | - continue; |
|
| 214 | - } |
|
| 215 | - // For each code found, sign the appropriate inputs |
|
| 216 | - foreach ($codes as $code) { |
|
| 217 | - // If the form appears to be hashed already, don't bother |
|
| 218 | - if (strpos($code[0], '||')) { |
|
| 219 | - self::$log[] = '<strong>Form appears to be signed already</strong>: '.htmlspecialchars($code[0]); |
|
| 220 | - continue; |
|
| 221 | - } |
|
| 222 | - // Get the code and the prefix |
|
| 223 | - $prefix = (isset($code[2])) ? $code[2] : ''; |
|
| 224 | - preg_match('%<[^>]*?value=([\'"])(.+?)\1[^>]*?>%i', $code[0], $code); |
|
| 225 | - $code = trim($code[2]); |
|
| 226 | - self::$log[] = '<strong>Prefix for '.htmlspecialchars($code).'</strong>: '.htmlspecialchars($prefix); |
|
| 227 | - if (!$code) { // If the code is empty, skip this form or specific prefixed elements |
|
| 228 | - continue; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // Sign all <input /> elements with matching prefix |
|
| 232 | - preg_match_all('%<input [^>]*?name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(?:.+?)\1[^>]*>%i', $form, $inputs); |
|
| 233 | - foreach ($inputs[0] as $input) { |
|
| 234 | - $count['inputs']++; |
|
| 235 | - // Test to make sure both name and value attributes are found |
|
| 236 | - if (preg_match('%name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1%i', $input, $name) > 0) { |
|
| 237 | - preg_match('%value=([\'"])(.*?)\1%i', $input, $value); |
|
| 238 | - $value = (count($value) > 0) ? $value : array('', '', ''); |
|
| 239 | - preg_match('%type=([\'"])(.*?)\1%i', $input, $type); |
|
| 240 | - $type = (count($type) > 0) ? $type : array('', '', ''); |
|
| 241 | - // Skip the cart excludes |
|
| 242 | - if (in_array($prefix.$name[2], self::$cart_excludes) || in_array(substr($prefix.$name[2], 0, 2), self::$cart_excludes_prefixes)) { |
|
| 243 | - self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$prefix.$name[2].'" = '.$value[2]; |
|
| 244 | - continue; |
|
| 245 | - } |
|
| 246 | - self::$log[] = '<strong>INPUT['.$type[2].']:</strong> Name: <strong>'.$prefix.htmlspecialchars(preg_quote($name[2])).'</strong>'; |
|
| 247 | - self::$log[] = '<strong>Replacement Pattern:</strong> ([\'"])'.$prefix.preg_quote($name[2]).'\1'; |
|
| 248 | - $value[2] = ($value[2] == '') ? '--OPEN--' : $value[2]; |
|
| 249 | - if ($type[2] == 'radio') { |
|
| 250 | - $input_signed = preg_replace('%([\'"])'.preg_quote($value[2]).'\1%', '${1}'.self::fc_hash_value($code, $name[2], $value[2], 'value', FALSE)."$1", $input); |
|
| 251 | - } else { |
|
| 252 | - $input_signed = preg_replace('%([\'"])'.$prefix.preg_quote($name[2]).'\1%', '${1}'.$prefix.self::fc_hash_value($code, $name[2], $value[2], 'name', FALSE)."$1", $input); |
|
| 253 | - } |
|
| 254 | - self::$log[] = '<strong>INPUT:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
|
| 255 | - '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$name[2]). |
|
| 256 | - '</strong> :: Value: <strong>'.htmlspecialchars($value[2]). |
|
| 257 | - '</strong><br />Initial input: '.htmlspecialchars($input). |
|
| 258 | - '<br />Signed: <span style="color:#060;">'.htmlspecialchars($input_signed).'</span>'; |
|
| 259 | - $form = str_replace($input, $input_signed, $form); |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - self::$log[] = '<strong>FORM after INPUTS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
|
| 263 | - |
|
| 264 | - // Sign all <option /> elements |
|
| 265 | - preg_match_all('%<select [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.+?)</select>%is', $form, $lists, PREG_SET_ORDER); |
|
| 266 | - foreach ($lists as $list) { |
|
| 267 | - $count['lists']++; |
|
| 268 | - preg_match_all('%<option [^>]*value=([\'"])(.+?)\1[^>]*>(?:.*?)</option>%i', $list[0], $options, PREG_SET_ORDER); |
|
| 269 | - self::$log[] = '<strong>Options:</strong> <pre>'.htmlspecialchars(print_r($options, true)).'</pre>'; |
|
| 270 | - unset( $form_part_signed ); |
|
| 271 | - foreach ($options as $option) { |
|
| 272 | - if( !isset($form_part_signed) ) $form_part_signed = $list[0]; |
|
| 273 | - $option_signed = preg_replace( |
|
| 274 | - '%'.preg_quote($option[1]).preg_quote($option[2]).preg_quote($option[1]).'%', |
|
| 275 | - $option[1].self::fc_hash_value($code, $list[2], $option[2], 'value', FALSE).$option[1], |
|
| 276 | - $option[0]); |
|
| 277 | - $form_part_signed = str_replace($option[0], $option_signed, $form_part_signed ); |
|
| 278 | - self::$log[] = '<strong>OPTION:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
|
| 279 | - '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$list[2]). |
|
| 280 | - '</strong> :: Value: <strong>'.htmlspecialchars($option[2]). |
|
| 281 | - '</strong><br />Initial option: '.htmlspecialchars($option[0]). |
|
| 282 | - '<br />Signed: <span style="color:#060;">'.htmlspecialchars($option_signed).'</span>'; |
|
| 283 | - } |
|
| 284 | - $form = str_replace($list[0], $form_part_signed, $form); |
|
| 285 | - } |
|
| 286 | - self::$log[] = '<strong>FORM after OPTIONS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
|
| 287 | - |
|
| 288 | - // Sign all <textarea /> elements |
|
| 289 | - preg_match_all('%<textarea [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.*?)</textarea>%is', $form, $textareas, PREG_SET_ORDER); |
|
| 290 | - // echo "\n\nTextareas: ".print_r($textareas, true); |
|
| 291 | - foreach ($textareas as $textarea) { |
|
| 292 | - $count['textareas']++; |
|
| 293 | - // Tackle implied "--OPEN--" first, if textarea is empty |
|
| 294 | - $textarea[3] = ($textarea[3] == '') ? '--OPEN--' : $textarea[3]; |
|
| 295 | - $textarea_signed = preg_replace('%([\'"])'.preg_quote($prefix.$textarea[2]).'\1%', "$1".self::fc_hash_value($code, $textarea[2], $textarea[3], 'name', FALSE)."$1", $textarea[0]); |
|
| 296 | - $form = str_replace($textarea[0], $textarea_signed, $form); |
|
| 297 | - self::$log[] = '<strong>TEXTAREA:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
|
| 298 | - '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$textarea[2]). |
|
| 299 | - '</strong> :: Value: <strong>'.htmlspecialchars($textarea[3]). |
|
| 300 | - '</strong><br />Initial textarea: '.htmlspecialchars($textarea[0]). |
|
| 301 | - '<br />Signed: <span style="color:#060;">'.htmlspecialchars($textarea_signed).'</span>'; |
|
| 302 | - } |
|
| 303 | - self::$log[] = '<strong>FORM after TEXTAREAS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
|
| 304 | - |
|
| 305 | - // Exclude all <button> elements |
|
| 306 | - $form = preg_replace('%<button ([^>]*)name=([\'"])(.*?)\1([^>]*>.*?</button>)%i', "<button $1name=$2x:$3$4", $form); |
|
| 307 | - |
|
| 308 | - } |
|
| 309 | - // Replace the entire form |
|
| 310 | - self::$log[] = '<strong>FORM after ALL:</strong> <pre>'.htmlspecialchars($form).'</pre>'.'replacing <pre>'.htmlspecialchars($form_original).'</pre>'; |
|
| 311 | - $html = str_replace($form_original, $form, $html); |
|
| 312 | - self::$log[] = '<strong>FORM end</strong><hr />'; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - // Return the signed output |
|
| 316 | - $output = ''; |
|
| 317 | - if (self::$debug) { |
|
| 318 | - self::$log['Summary'] = $count['links'].' links signed. '.$count['forms'].' forms signed. '.$count['inputs'].' inputs signed. '.$count['lists'].' lists signed. '.$count['textareas'].' textareas signed.'; |
|
| 319 | - $output .= '<h3>FoxyCart HMAC Debugging:</h3><ul>'; |
|
| 320 | - foreach (self::$log as $name => $value) { |
|
| 321 | - $output .= '<li><strong>'.$name.':</strong> '.$value.'</li>'; |
|
| 322 | - } |
|
| 323 | - $output .= '</ul><hr />'; |
|
| 324 | - } |
|
| 325 | - return $output.$html; |
|
| 326 | - } |
|
| 17 | + /** |
|
| 18 | + * API Key (Secret) |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + **/ |
|
| 22 | + private static $secret; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Cart URL |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + * Notes: Could be 'https://yourdomain.foxycart.com/cart' or 'https://secure.yourdomain.com/cart' |
|
| 29 | + **/ |
|
| 30 | + // protected static $cart_url = 'https://yourdomain.foxycart.com/cart'; |
|
| 31 | + protected static $cart_url; |
|
| 32 | + |
|
| 33 | + public static function setCartURL($storeName = null){ |
|
| 34 | + self::$cart_url = 'https://'.$storeName.'.faxycart.com/cart'; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public static function setSecret($secret = null){ |
|
| 38 | + self::$secret = $secret; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function __construct(){ |
|
| 42 | + self::setCartURL(FoxyCart::getFoxyCartStoreName()); |
|
| 43 | + self::setSecret(FoxyCart::getStoreKey()); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public static function getSecret(){ |
|
| 47 | + return FoxyCart::getStoreKey(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Cart Excludes |
|
| 53 | + * |
|
| 54 | + * Arrays of values and prefixes that should be ignored when signing links and forms. |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected static $cart_excludes = array( |
|
| 58 | + // Cart values |
|
| 59 | + 'cart', 'fcsid', 'empty', 'coupon', 'output', 'sub_token', 'redirect', 'callback', '_', |
|
| 60 | + // Checkout pre-population values |
|
| 61 | + 'customer_email', 'customer_first_name', 'customer_last_name', 'customer_address1', 'customer_address2', |
|
| 62 | + 'customer_city', 'customer_state', 'customer_postal_code', 'customer_country', 'customer_phone', 'customer_company', |
|
| 63 | + 'shipping_first_name', 'shipping_last_name', 'shipping_address1', 'shipping_address2', |
|
| 64 | + 'shipping_city', 'shipping_state', 'shipping_postal_code', 'shipping_country', 'shipping_phone', 'shipping_company', |
|
| 65 | + ); |
|
| 66 | + protected static $cart_excludes_prefixes = array( |
|
| 67 | + 'h:', 'x:', '__', |
|
| 68 | + ); |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Debugging |
|
| 72 | + * |
|
| 73 | + * Set to $debug to TRUE to enable debug logging. |
|
| 74 | + * |
|
| 75 | + */ |
|
| 76 | + protected static $debug = FALSE; |
|
| 77 | + protected static $log = array(); |
|
| 78 | + |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * "Link Method": Generate HMAC SHA256 for GET Query Strings |
|
| 82 | + * |
|
| 83 | + * Notes: Can't parse_str because PHP doesn't support non-alphanumeric characters as array keys. |
|
| 84 | + * @return string |
|
| 85 | + **/ |
|
| 86 | + public static function fc_hash_querystring($qs, $output = TRUE) { |
|
| 87 | + self::$log[] = '<strong>Signing link</strong> with data: '.htmlspecialchars(substr($qs, 0, 150)).'...'; |
|
| 88 | + $fail = self::$cart_url.'?'.$qs; |
|
| 89 | + |
|
| 90 | + // If the link appears to be hashed already, don't bother |
|
| 91 | + if (strpos($qs, '||')) { |
|
| 92 | + self::$log[] = '<strong>Link appears to be signed already</strong>: '.htmlspecialchars($code[0]); |
|
| 93 | + return $fail; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // Stick an ampersand on the beginning of the querystring to make matching the first element a little easier |
|
| 97 | + $qs = '&'.urldecode($qs); |
|
| 98 | + |
|
| 99 | + // Get all the prefixes, codes, and name=value pairs |
|
| 100 | + preg_match_all('%(?P<amp>&(?:amp;)?)(?P<prefix>[a-z0-9]{1,3}:)?(?P<name>[^=]+)=(?P<value>[^&]+)%', $qs, $pairs, PREG_SET_ORDER); |
|
| 101 | + self::$log[] = 'Found the following pairs to sign:<pre>'.htmlspecialchars(print_r($pairs, true)).'</pre>'; |
|
| 102 | + |
|
| 103 | + // Get all the "code" values, set the matches in $codes |
|
| 104 | + $codes = array(); |
|
| 105 | + foreach ($pairs as $pair) { |
|
| 106 | + if ($pair['name'] == 'code') { |
|
| 107 | + $codes[$pair['prefix']] = $pair['value']; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + if ( ! count($codes)) { |
|
| 111 | + self::$log[] = '<strong style="color:#600;">No code found</strong> for the above link.'; |
|
| 112 | + return $fail; |
|
| 113 | + } |
|
| 114 | + self::$log[] = '<strong style="color:orange;">CODES found:</strong> '.htmlspecialchars(print_r($codes, true)); |
|
| 115 | + |
|
| 116 | + // Sign the name/value pairs |
|
| 117 | + foreach ($pairs as $pair) { |
|
| 118 | + // Skip the cart excludes |
|
| 119 | + if (in_array($pair['name'], self::$cart_excludes) || in_array($pair['prefix'], self::$cart_excludes_prefixes)) { |
|
| 120 | + self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$pair['prefix'].$pair['name'].'" = '.$pair['value']; |
|
| 121 | + continue; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + // Continue to sign the value and replace the name=value in the querystring with name=value||hash |
|
| 125 | + $value = self::fc_hash_value($codes[$pair['prefix']], $pair['name'], $pair['value'], 'value', FALSE, 'urlencode'); |
|
| 126 | + $replacement = $pair['amp'].$pair['prefix'].urlencode($pair['name']).'='.$value; |
|
| 127 | + $qs = str_replace($pair[0], $replacement, $qs); |
|
| 128 | + self::$log[] = 'Signed <strong>'.$pair['name'].'</strong> = <strong>'.$pair['value'].'</strong> with '.$replacement.'.<br />Replacing: '.$pair[0].'<br />With... '.$replacement; |
|
| 129 | + } |
|
| 130 | + $qs = ltrim($qs, '&'); // Get rid of that leading ampersand we added earlier |
|
| 131 | + |
|
| 132 | + if ($output) { |
|
| 133 | + echo self::$cart_url.'?'.$qs; |
|
| 134 | + } else { |
|
| 135 | + return self::$cart_url.'?'.$qs; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * "Form Method": Generate HMAC SHA256 for form elements or individual <input />s |
|
| 142 | + * |
|
| 143 | + * @return string |
|
| 144 | + **/ |
|
| 145 | + public static function fc_hash_value($product_code, $option_name, $option_value = '', $method = 'name', $output = TRUE, $urlencode = false) { |
|
| 146 | + if (!$product_code || !$option_name) { |
|
| 147 | + return FALSE; |
|
| 148 | + } |
|
| 149 | + if ($option_value == '--OPEN--') { |
|
| 150 | + $hash = hash_hmac('sha256', $product_code.$option_name.$option_value, self::getSecret()); |
|
| 151 | + $value = ($urlencode) ? urlencode($option_name).'||'.$hash.'||open' : $option_name.'||'.$hash.'||open'; |
|
| 152 | + } else { |
|
| 153 | + $hash = hash_hmac('sha256', $product_code.$option_name.$option_value, self::getSecret()); |
|
| 154 | + if ($method == 'name') { |
|
| 155 | + $value = ($urlencode) ? urlencode($option_name).'||'.$hash : $option_name.'||'.$hash; |
|
| 156 | + } else { |
|
| 157 | + $value = ($urlencode) ? urlencode($option_value).'||'.$hash : $option_value.'||'.$hash; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ($output) { |
|
| 162 | + echo $value; |
|
| 163 | + } else { |
|
| 164 | + return $value; |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Raw HTML Signing: Sign all links and form elements in a block of HTML |
|
| 170 | + * |
|
| 171 | + * Accepts a string of HTML and signs all links and forms. |
|
| 172 | + * Requires link 'href' and form 'action' attributes to use 'https' and not 'http'. |
|
| 173 | + * Requires a 'code' to be set in every form. |
|
| 174 | + * |
|
| 175 | + * @return string |
|
| 176 | + **/ |
|
| 177 | + public static function fc_hash_html($html) { |
|
| 178 | + // Initialize some counting |
|
| 179 | + $count['temp'] = 0; // temp counter |
|
| 180 | + $count['links'] = 0; |
|
| 181 | + $count['forms'] = 0; |
|
| 182 | + $count['inputs'] = 0; |
|
| 183 | + $count['lists'] = 0; |
|
| 184 | + $count['textareas'] = 0; |
|
| 185 | + |
|
| 186 | + // Find and sign all the links |
|
| 187 | + preg_match_all('%<a .*?href=[\'"]'.preg_quote(self::$cart_url).'(?:\.php)?\?(.+?)[\'"].*?>%i', $html, $querystrings); |
|
| 188 | + // print_r($querystrings); |
|
| 189 | + foreach ($querystrings[1] as $querystring) { |
|
| 190 | + // If it's already signed, skip it. |
|
| 191 | + if (preg_match('%&(?:amp;)?hash=%i', $querystring)) { |
|
| 192 | + continue; |
|
| 193 | + } |
|
| 194 | + $pattern = '%(href=[\'"])'.preg_quote(self::$cart_url, '%').'(?:\.php)?\?'.preg_quote($querystring, '%').'([\'"])%i'; |
|
| 195 | + $signed = self::fc_hash_querystring($querystring, FALSE); |
|
| 196 | + $html = preg_replace($pattern, '$1'.$signed.'$2', $html, -1, $count['temp']); |
|
| 197 | + $count['links'] += $count['temp']; |
|
| 198 | + } |
|
| 199 | + unset($querystrings); |
|
| 200 | + |
|
| 201 | + // Find and sign all form values |
|
| 202 | + preg_match_all('%<form [^>]*?action=[\'"]'.preg_quote(self::$cart_url).'?[\'"].*?>(.+?)</form>%is', $html, $forms); |
|
| 203 | + foreach ($forms[1] as $form) { |
|
| 204 | + $count['forms']++; |
|
| 205 | + self::$log[] = '<strong>Signing form</strong> with data: '.htmlspecialchars(substr($form, 0, 150)).'...'; |
|
| 206 | + |
|
| 207 | + // Store the original form so we can replace it when we're done |
|
| 208 | + $form_original = $form; |
|
| 209 | + |
|
| 210 | + // Check for the "code" input, set the matches in $codes |
|
| 211 | + if (!preg_match_all('%<[^>]*?name=([\'"])([0-9]{1,3}:)?code\1[^>]*?>%i', $form, $codes, PREG_SET_ORDER)) { |
|
| 212 | + self::$log[] = '<strong style="color:#600;">No code found</strong> for the above form.'; |
|
| 213 | + continue; |
|
| 214 | + } |
|
| 215 | + // For each code found, sign the appropriate inputs |
|
| 216 | + foreach ($codes as $code) { |
|
| 217 | + // If the form appears to be hashed already, don't bother |
|
| 218 | + if (strpos($code[0], '||')) { |
|
| 219 | + self::$log[] = '<strong>Form appears to be signed already</strong>: '.htmlspecialchars($code[0]); |
|
| 220 | + continue; |
|
| 221 | + } |
|
| 222 | + // Get the code and the prefix |
|
| 223 | + $prefix = (isset($code[2])) ? $code[2] : ''; |
|
| 224 | + preg_match('%<[^>]*?value=([\'"])(.+?)\1[^>]*?>%i', $code[0], $code); |
|
| 225 | + $code = trim($code[2]); |
|
| 226 | + self::$log[] = '<strong>Prefix for '.htmlspecialchars($code).'</strong>: '.htmlspecialchars($prefix); |
|
| 227 | + if (!$code) { // If the code is empty, skip this form or specific prefixed elements |
|
| 228 | + continue; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // Sign all <input /> elements with matching prefix |
|
| 232 | + preg_match_all('%<input [^>]*?name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(?:.+?)\1[^>]*>%i', $form, $inputs); |
|
| 233 | + foreach ($inputs[0] as $input) { |
|
| 234 | + $count['inputs']++; |
|
| 235 | + // Test to make sure both name and value attributes are found |
|
| 236 | + if (preg_match('%name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1%i', $input, $name) > 0) { |
|
| 237 | + preg_match('%value=([\'"])(.*?)\1%i', $input, $value); |
|
| 238 | + $value = (count($value) > 0) ? $value : array('', '', ''); |
|
| 239 | + preg_match('%type=([\'"])(.*?)\1%i', $input, $type); |
|
| 240 | + $type = (count($type) > 0) ? $type : array('', '', ''); |
|
| 241 | + // Skip the cart excludes |
|
| 242 | + if (in_array($prefix.$name[2], self::$cart_excludes) || in_array(substr($prefix.$name[2], 0, 2), self::$cart_excludes_prefixes)) { |
|
| 243 | + self::$log[] = '<strong style="color:purple;">Skipping</strong> the reserved parameter or prefix "'.$prefix.$name[2].'" = '.$value[2]; |
|
| 244 | + continue; |
|
| 245 | + } |
|
| 246 | + self::$log[] = '<strong>INPUT['.$type[2].']:</strong> Name: <strong>'.$prefix.htmlspecialchars(preg_quote($name[2])).'</strong>'; |
|
| 247 | + self::$log[] = '<strong>Replacement Pattern:</strong> ([\'"])'.$prefix.preg_quote($name[2]).'\1'; |
|
| 248 | + $value[2] = ($value[2] == '') ? '--OPEN--' : $value[2]; |
|
| 249 | + if ($type[2] == 'radio') { |
|
| 250 | + $input_signed = preg_replace('%([\'"])'.preg_quote($value[2]).'\1%', '${1}'.self::fc_hash_value($code, $name[2], $value[2], 'value', FALSE)."$1", $input); |
|
| 251 | + } else { |
|
| 252 | + $input_signed = preg_replace('%([\'"])'.$prefix.preg_quote($name[2]).'\1%', '${1}'.$prefix.self::fc_hash_value($code, $name[2], $value[2], 'name', FALSE)."$1", $input); |
|
| 253 | + } |
|
| 254 | + self::$log[] = '<strong>INPUT:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
|
| 255 | + '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$name[2]). |
|
| 256 | + '</strong> :: Value: <strong>'.htmlspecialchars($value[2]). |
|
| 257 | + '</strong><br />Initial input: '.htmlspecialchars($input). |
|
| 258 | + '<br />Signed: <span style="color:#060;">'.htmlspecialchars($input_signed).'</span>'; |
|
| 259 | + $form = str_replace($input, $input_signed, $form); |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + self::$log[] = '<strong>FORM after INPUTS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
|
| 263 | + |
|
| 264 | + // Sign all <option /> elements |
|
| 265 | + preg_match_all('%<select [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.+?)</select>%is', $form, $lists, PREG_SET_ORDER); |
|
| 266 | + foreach ($lists as $list) { |
|
| 267 | + $count['lists']++; |
|
| 268 | + preg_match_all('%<option [^>]*value=([\'"])(.+?)\1[^>]*>(?:.*?)</option>%i', $list[0], $options, PREG_SET_ORDER); |
|
| 269 | + self::$log[] = '<strong>Options:</strong> <pre>'.htmlspecialchars(print_r($options, true)).'</pre>'; |
|
| 270 | + unset( $form_part_signed ); |
|
| 271 | + foreach ($options as $option) { |
|
| 272 | + if( !isset($form_part_signed) ) $form_part_signed = $list[0]; |
|
| 273 | + $option_signed = preg_replace( |
|
| 274 | + '%'.preg_quote($option[1]).preg_quote($option[2]).preg_quote($option[1]).'%', |
|
| 275 | + $option[1].self::fc_hash_value($code, $list[2], $option[2], 'value', FALSE).$option[1], |
|
| 276 | + $option[0]); |
|
| 277 | + $form_part_signed = str_replace($option[0], $option_signed, $form_part_signed ); |
|
| 278 | + self::$log[] = '<strong>OPTION:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
|
| 279 | + '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$list[2]). |
|
| 280 | + '</strong> :: Value: <strong>'.htmlspecialchars($option[2]). |
|
| 281 | + '</strong><br />Initial option: '.htmlspecialchars($option[0]). |
|
| 282 | + '<br />Signed: <span style="color:#060;">'.htmlspecialchars($option_signed).'</span>'; |
|
| 283 | + } |
|
| 284 | + $form = str_replace($list[0], $form_part_signed, $form); |
|
| 285 | + } |
|
| 286 | + self::$log[] = '<strong>FORM after OPTIONS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
|
| 287 | + |
|
| 288 | + // Sign all <textarea /> elements |
|
| 289 | + preg_match_all('%<textarea [^>]*name=([\'"])'.preg_quote($prefix).'(?![0-9]{1,3})(.+?)\1[^>]*>(.*?)</textarea>%is', $form, $textareas, PREG_SET_ORDER); |
|
| 290 | + // echo "\n\nTextareas: ".print_r($textareas, true); |
|
| 291 | + foreach ($textareas as $textarea) { |
|
| 292 | + $count['textareas']++; |
|
| 293 | + // Tackle implied "--OPEN--" first, if textarea is empty |
|
| 294 | + $textarea[3] = ($textarea[3] == '') ? '--OPEN--' : $textarea[3]; |
|
| 295 | + $textarea_signed = preg_replace('%([\'"])'.preg_quote($prefix.$textarea[2]).'\1%', "$1".self::fc_hash_value($code, $textarea[2], $textarea[3], 'name', FALSE)."$1", $textarea[0]); |
|
| 296 | + $form = str_replace($textarea[0], $textarea_signed, $form); |
|
| 297 | + self::$log[] = '<strong>TEXTAREA:</strong> Code: <strong>'.htmlspecialchars($prefix.$code). |
|
| 298 | + '</strong> :: Name: <strong>'.htmlspecialchars($prefix.$textarea[2]). |
|
| 299 | + '</strong> :: Value: <strong>'.htmlspecialchars($textarea[3]). |
|
| 300 | + '</strong><br />Initial textarea: '.htmlspecialchars($textarea[0]). |
|
| 301 | + '<br />Signed: <span style="color:#060;">'.htmlspecialchars($textarea_signed).'</span>'; |
|
| 302 | + } |
|
| 303 | + self::$log[] = '<strong>FORM after TEXTAREAS:</strong> <pre>'.htmlspecialchars($form).'</pre>'; |
|
| 304 | + |
|
| 305 | + // Exclude all <button> elements |
|
| 306 | + $form = preg_replace('%<button ([^>]*)name=([\'"])(.*?)\1([^>]*>.*?</button>)%i', "<button $1name=$2x:$3$4", $form); |
|
| 307 | + |
|
| 308 | + } |
|
| 309 | + // Replace the entire form |
|
| 310 | + self::$log[] = '<strong>FORM after ALL:</strong> <pre>'.htmlspecialchars($form).'</pre>'.'replacing <pre>'.htmlspecialchars($form_original).'</pre>'; |
|
| 311 | + $html = str_replace($form_original, $form, $html); |
|
| 312 | + self::$log[] = '<strong>FORM end</strong><hr />'; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + // Return the signed output |
|
| 316 | + $output = ''; |
|
| 317 | + if (self::$debug) { |
|
| 318 | + self::$log['Summary'] = $count['links'].' links signed. '.$count['forms'].' forms signed. '.$count['inputs'].' inputs signed. '.$count['lists'].' lists signed. '.$count['textareas'].' textareas signed.'; |
|
| 319 | + $output .= '<h3>FoxyCart HMAC Debugging:</h3><ul>'; |
|
| 320 | + foreach (self::$log as $name => $value) { |
|
| 321 | + $output .= '<li><strong>'.$name.':</strong> '.$value.'</li>'; |
|
| 322 | + } |
|
| 323 | + $output .= '</ul><hr />'; |
|
| 324 | + } |
|
| 325 | + return $output.$html; |
|
| 326 | + } |
|
| 327 | 327 | |
| 328 | 328 | } |
| 329 | 329 | \ No newline at end of file |
@@ -269,7 +269,9 @@ |
||
| 269 | 269 | self::$log[] = '<strong>Options:</strong> <pre>'.htmlspecialchars(print_r($options, true)).'</pre>'; |
| 270 | 270 | unset( $form_part_signed ); |
| 271 | 271 | foreach ($options as $option) { |
| 272 | - if( !isset($form_part_signed) ) $form_part_signed = $list[0]; |
|
| 272 | + if( !isset($form_part_signed) ) { |
|
| 273 | + $form_part_signed = $list[0]; |
|
| 274 | + } |
|
| 273 | 275 | $option_signed = preg_replace( |
| 274 | 276 | '%'.preg_quote($option[1]).preg_quote($option[2]).preg_quote($option[1]).'%', |
| 275 | 277 | $option[1].self::fc_hash_value($code, $list[2], $option[2], 'value', FALSE).$option[1], |
@@ -110,7 +110,7 @@ |
||
| 110 | 110 | * Products function. |
| 111 | 111 | * |
| 112 | 112 | * @access public |
| 113 | - * @return array |
|
| 113 | + * @return PaginatedList |
|
| 114 | 114 | */ |
| 115 | 115 | public function ProductList($limit = 10) { |
| 116 | 116 | |
@@ -7,144 +7,144 @@ |
||
| 7 | 7 | |
| 8 | 8 | class ProductHolder extends Page { |
| 9 | 9 | |
| 10 | - private static $allowed_children = array('ProductHolder', 'ProductPage'); |
|
| 10 | + private static $allowed_children = array('ProductHolder', 'ProductPage'); |
|
| 11 | 11 | |
| 12 | - private static $db = array( |
|
| 12 | + private static $db = array( |
|
| 13 | 13 | |
| 14 | - ); |
|
| 14 | + ); |
|
| 15 | 15 | |
| 16 | - private static $has_one = array( |
|
| 16 | + private static $has_one = array( |
|
| 17 | 17 | |
| 18 | - ); |
|
| 18 | + ); |
|
| 19 | 19 | |
| 20 | - private static $many_many = array( |
|
| 21 | - 'Products' => 'ProductPage' |
|
| 22 | - ); |
|
| 20 | + private static $many_many = array( |
|
| 21 | + 'Products' => 'ProductPage' |
|
| 22 | + ); |
|
| 23 | 23 | |
| 24 | - private static $many_many_extraFields = array( |
|
| 25 | - 'Products' => array( |
|
| 26 | - 'SortOrder' => 'Int' |
|
| 27 | - ) |
|
| 28 | - ); |
|
| 24 | + private static $many_many_extraFields = array( |
|
| 25 | + 'Products' => array( |
|
| 26 | + 'SortOrder' => 'Int' |
|
| 27 | + ) |
|
| 28 | + ); |
|
| 29 | 29 | |
| 30 | 30 | private static $singular_name = 'Product Group'; |
| 31 | 31 | private static $plural_name = 'Product Groups'; |
| 32 | 32 | private static $description = 'Display a list of related products'; |
| 33 | 33 | |
| 34 | - private static $defaults = array( |
|
| 34 | + private static $defaults = array( |
|
| 35 | 35 | |
| 36 | - ); |
|
| 36 | + ); |
|
| 37 | 37 | |
| 38 | - public function getCMSFields(){ |
|
| 39 | - $fields = parent::getCMSFields(); |
|
| 40 | - |
|
| 41 | - if(SiteConfig::current_site_config()->MultiGroup){ |
|
| 42 | - $config = GridFieldConfig_RelationEditor::create(); |
|
| 43 | - if(class_exists('GridFieldSortableRows')){ |
|
| 44 | - $config->addComponent(new GridFieldSortableRows('SortOrder')); |
|
| 45 | - } |
|
| 46 | - if(class_exists('GridFieldManyRelationHandler')){ |
|
| 47 | - $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
| 48 | - $config->addComponent(new GridFieldManyRelationHandler()); |
|
| 49 | - } |
|
| 50 | - $fields->addFieldToTab( |
|
| 51 | - 'Root.Products', |
|
| 52 | - GridField::create( |
|
| 53 | - 'Products', |
|
| 54 | - _t('ProductHolder.Products', 'Products'), |
|
| 55 | - $this->Products(), |
|
| 56 | - $config |
|
| 57 | - ) |
|
| 58 | - ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $this->extend('updateCMSFields', $fields); |
|
| 62 | - |
|
| 63 | - return $fields; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function Products() { |
|
| 67 | - return $this->getManyManyComponents('Products')->sort('SortOrder'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * loadDescendantProductGroupIDListInto function. |
|
| 72 | - * |
|
| 73 | - * @access public |
|
| 74 | - * @param mixed &$idList |
|
| 75 | - * @return void |
|
| 76 | - */ |
|
| 77 | - public function loadDescendantProductGroupIDListInto(&$idList) { |
|
| 78 | - if ($children = $this->AllChildren()) { |
|
| 79 | - foreach($children as $child) { |
|
| 80 | - if(in_array($child->ID, $idList)) continue; |
|
| 38 | + public function getCMSFields(){ |
|
| 39 | + $fields = parent::getCMSFields(); |
|
| 40 | + |
|
| 41 | + if(SiteConfig::current_site_config()->MultiGroup){ |
|
| 42 | + $config = GridFieldConfig_RelationEditor::create(); |
|
| 43 | + if(class_exists('GridFieldSortableRows')){ |
|
| 44 | + $config->addComponent(new GridFieldSortableRows('SortOrder')); |
|
| 45 | + } |
|
| 46 | + if(class_exists('GridFieldManyRelationHandler')){ |
|
| 47 | + $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
| 48 | + $config->addComponent(new GridFieldManyRelationHandler()); |
|
| 49 | + } |
|
| 50 | + $fields->addFieldToTab( |
|
| 51 | + 'Root.Products', |
|
| 52 | + GridField::create( |
|
| 53 | + 'Products', |
|
| 54 | + _t('ProductHolder.Products', 'Products'), |
|
| 55 | + $this->Products(), |
|
| 56 | + $config |
|
| 57 | + ) |
|
| 58 | + ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $this->extend('updateCMSFields', $fields); |
|
| 62 | + |
|
| 63 | + return $fields; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function Products() { |
|
| 67 | + return $this->getManyManyComponents('Products')->sort('SortOrder'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * loadDescendantProductGroupIDListInto function. |
|
| 72 | + * |
|
| 73 | + * @access public |
|
| 74 | + * @param mixed &$idList |
|
| 75 | + * @return void |
|
| 76 | + */ |
|
| 77 | + public function loadDescendantProductGroupIDListInto(&$idList) { |
|
| 78 | + if ($children = $this->AllChildren()) { |
|
| 79 | + foreach($children as $child) { |
|
| 80 | + if(in_array($child->ID, $idList)) continue; |
|
| 81 | 81 | |
| 82 | - if($child instanceof ProductHolder) { |
|
| 83 | - $idList[] = $child->ID; |
|
| 84 | - $child->loadDescendantProductGroupIDListInto($idList); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - } |
|
| 82 | + if($child instanceof ProductHolder) { |
|
| 83 | + $idList[] = $child->ID; |
|
| 84 | + $child->loadDescendantProductGroupIDListInto($idList); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * ProductGroupIDs function. |
|
| 92 | - * |
|
| 93 | - * @access public |
|
| 94 | - * @return array |
|
| 95 | - */ |
|
| 96 | - public function ProductGroupIDs() { |
|
| 97 | - $holderIDs = array(); |
|
| 98 | - $this->loadDescendantProductGroupIDListInto($holderIDs); |
|
| 99 | - return $holderIDs; |
|
| 100 | - } |
|
| 90 | + /** |
|
| 91 | + * ProductGroupIDs function. |
|
| 92 | + * |
|
| 93 | + * @access public |
|
| 94 | + * @return array |
|
| 95 | + */ |
|
| 96 | + public function ProductGroupIDs() { |
|
| 97 | + $holderIDs = array(); |
|
| 98 | + $this->loadDescendantProductGroupIDListInto($holderIDs); |
|
| 99 | + return $holderIDs; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Products function. |
|
| 104 | - * |
|
| 105 | - * @access public |
|
| 106 | - * @return array |
|
| 107 | - */ |
|
| 108 | - public function ProductList($limit = 10) { |
|
| 102 | + /** |
|
| 103 | + * Products function. |
|
| 104 | + * |
|
| 105 | + * @access public |
|
| 106 | + * @return array |
|
| 107 | + */ |
|
| 108 | + public function ProductList($limit = 10) { |
|
| 109 | 109 | |
| 110 | - $config = SiteConfig::current_site_config(); |
|
| 110 | + $config = SiteConfig::current_site_config(); |
|
| 111 | 111 | |
| 112 | - if($config->ProductLimit>0){ |
|
| 113 | - $limit = $config->ProductLimit; |
|
| 114 | - } |
|
| 112 | + if($config->ProductLimit>0){ |
|
| 113 | + $limit = $config->ProductLimit; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - if($config->MultiGroup){ |
|
| 117 | - $entries = $this->Products()->sort('SortOrder'); |
|
| 118 | - }else{ |
|
| 119 | - $filter = '"ParentID" = ' . $this->ID; |
|
| 116 | + if($config->MultiGroup){ |
|
| 117 | + $entries = $this->Products()->sort('SortOrder'); |
|
| 118 | + }else{ |
|
| 119 | + $filter = '"ParentID" = ' . $this->ID; |
|
| 120 | 120 | |
| 121 | - // Build a list of all IDs for ProductGroups that are children |
|
| 122 | - $holderIDs = $this->ProductGroupIDs(); |
|
| 121 | + // Build a list of all IDs for ProductGroups that are children |
|
| 122 | + $holderIDs = $this->ProductGroupIDs(); |
|
| 123 | 123 | |
| 124 | - // If no ProductHolders, no ProductPages. So return false |
|
| 125 | - if($holderIDs) { |
|
| 126 | - // Otherwise, do the actual query |
|
| 127 | - if($filter) $filter .= ' OR '; |
|
| 128 | - $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ")"; |
|
| 129 | - } |
|
| 124 | + // If no ProductHolders, no ProductPages. So return false |
|
| 125 | + if($holderIDs) { |
|
| 126 | + // Otherwise, do the actual query |
|
| 127 | + if($filter) $filter .= ' OR '; |
|
| 128 | + $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ")"; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - $order = '"SiteTree"."Title" ASC'; |
|
| 131 | + $order = '"SiteTree"."Title" ASC'; |
|
| 132 | 132 | |
| 133 | - $entries = ProductPage::get()->where($filter); |
|
| 134 | - } |
|
| 133 | + $entries = ProductPage::get()->where($filter); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | 136 | |
| 137 | - $list = new PaginatedList($entries, Controller::curr()->request); |
|
| 138 | - $list->setPageLength($limit); |
|
| 139 | - return $list; |
|
| 137 | + $list = new PaginatedList($entries, Controller::curr()->request); |
|
| 138 | + $list->setPageLength($limit); |
|
| 139 | + return $list; |
|
| 140 | 140 | |
| 141 | - } |
|
| 141 | + } |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | class ProductHolder_Controller extends Page_Controller { |
| 145 | 145 | |
| 146 | - public function init(){ |
|
| 147 | - parent::init(); |
|
| 146 | + public function init(){ |
|
| 147 | + parent::init(); |
|
| 148 | 148 | |
| 149 | - } |
|
| 149 | + } |
|
| 150 | 150 | } |
| 151 | 151 | \ No newline at end of file |
@@ -35,15 +35,15 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | ); |
| 37 | 37 | |
| 38 | - public function getCMSFields(){ |
|
| 38 | + public function getCMSFields() { |
|
| 39 | 39 | $fields = parent::getCMSFields(); |
| 40 | 40 | |
| 41 | - if(SiteConfig::current_site_config()->MultiGroup){ |
|
| 41 | + if (SiteConfig::current_site_config()->MultiGroup) { |
|
| 42 | 42 | $config = GridFieldConfig_RelationEditor::create(); |
| 43 | - if(class_exists('GridFieldSortableRows')){ |
|
| 43 | + if (class_exists('GridFieldSortableRows')) { |
|
| 44 | 44 | $config->addComponent(new GridFieldSortableRows('SortOrder')); |
| 45 | 45 | } |
| 46 | - if(class_exists('GridFieldManyRelationHandler')){ |
|
| 46 | + if (class_exists('GridFieldManyRelationHandler')) { |
|
| 47 | 47 | $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
| 48 | 48 | $config->addComponent(new GridFieldManyRelationHandler()); |
| 49 | 49 | } |
@@ -76,10 +76,10 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | public function loadDescendantProductGroupIDListInto(&$idList) { |
| 78 | 78 | if ($children = $this->AllChildren()) { |
| 79 | - foreach($children as $child) { |
|
| 80 | - if(in_array($child->ID, $idList)) continue; |
|
| 79 | + foreach ($children as $child) { |
|
| 80 | + if (in_array($child->ID, $idList)) continue; |
|
| 81 | 81 | |
| 82 | - if($child instanceof ProductHolder) { |
|
| 82 | + if ($child instanceof ProductHolder) { |
|
| 83 | 83 | $idList[] = $child->ID; |
| 84 | 84 | $child->loadDescendantProductGroupIDListInto($idList); |
| 85 | 85 | } |
@@ -109,23 +109,23 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | $config = SiteConfig::current_site_config(); |
| 111 | 111 | |
| 112 | - if($config->ProductLimit>0){ |
|
| 112 | + if ($config->ProductLimit > 0) { |
|
| 113 | 113 | $limit = $config->ProductLimit; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - if($config->MultiGroup){ |
|
| 116 | + if ($config->MultiGroup) { |
|
| 117 | 117 | $entries = $this->Products()->sort('SortOrder'); |
| 118 | - }else{ |
|
| 119 | - $filter = '"ParentID" = ' . $this->ID; |
|
| 118 | + }else { |
|
| 119 | + $filter = '"ParentID" = '.$this->ID; |
|
| 120 | 120 | |
| 121 | 121 | // Build a list of all IDs for ProductGroups that are children |
| 122 | 122 | $holderIDs = $this->ProductGroupIDs(); |
| 123 | 123 | |
| 124 | 124 | // If no ProductHolders, no ProductPages. So return false |
| 125 | - if($holderIDs) { |
|
| 125 | + if ($holderIDs) { |
|
| 126 | 126 | // Otherwise, do the actual query |
| 127 | - if($filter) $filter .= ' OR '; |
|
| 128 | - $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ")"; |
|
| 127 | + if ($filter) $filter .= ' OR '; |
|
| 128 | + $filter .= '"ParentID" IN ('.implode(',', $holderIDs).")"; |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | $order = '"SiteTree"."Title" ASC'; |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | |
| 144 | 144 | class ProductHolder_Controller extends Page_Controller { |
| 145 | 145 | |
| 146 | - public function init(){ |
|
| 146 | + public function init() { |
|
| 147 | 147 | parent::init(); |
| 148 | 148 | |
| 149 | 149 | } |
@@ -77,7 +77,9 @@ discard block |
||
| 77 | 77 | public function loadDescendantProductGroupIDListInto(&$idList) { |
| 78 | 78 | if ($children = $this->AllChildren()) { |
| 79 | 79 | foreach($children as $child) { |
| 80 | - if(in_array($child->ID, $idList)) continue; |
|
| 80 | + if(in_array($child->ID, $idList)) { |
|
| 81 | + continue; |
|
| 82 | + } |
|
| 81 | 83 | |
| 82 | 84 | if($child instanceof ProductHolder) { |
| 83 | 85 | $idList[] = $child->ID; |
@@ -115,7 +117,7 @@ discard block |
||
| 115 | 117 | |
| 116 | 118 | if($config->MultiGroup){ |
| 117 | 119 | $entries = $this->Products()->sort('SortOrder'); |
| 118 | - }else{ |
|
| 120 | + } else{ |
|
| 119 | 121 | $filter = '"ParentID" = ' . $this->ID; |
| 120 | 122 | |
| 121 | 123 | // Build a list of all IDs for ProductGroups that are children |
@@ -124,7 +126,9 @@ discard block |
||
| 124 | 126 | // If no ProductHolders, no ProductPages. So return false |
| 125 | 127 | if($holderIDs) { |
| 126 | 128 | // Otherwise, do the actual query |
| 127 | - if($filter) $filter .= ' OR '; |
|
| 129 | + if($filter) { |
|
| 130 | + $filter .= ' OR '; |
|
| 131 | + } |
|
| 128 | 132 | $filter .= '"ParentID" IN (' . implode(',', $holderIDs) . ")"; |
| 129 | 133 | } |
| 130 | 134 | |
@@ -9,43 +9,43 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | private static $keyPrefix = 'dYnm1c'; |
| 11 | 11 | |
| 12 | - public static function setStoreKey($length = 54, $count = 0){ |
|
| 12 | + public static function setStoreKey($length = 54, $count = 0) { |
|
| 13 | 13 | $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now'); |
| 14 | 14 | $strLength = strlen($charset); |
| 15 | 15 | $str = ''; |
| 16 | - while($count < $length){ |
|
| 17 | - $str .= $charset[mt_rand(0, $strLength-1)]; |
|
| 16 | + while ($count < $length) { |
|
| 17 | + $str .= $charset[mt_rand(0, $strLength - 1)]; |
|
| 18 | 18 | $count++; |
| 19 | 19 | } |
| 20 | - return self::getKeyPrefix().substr(base64_encode($str),0,$length); |
|
| 20 | + return self::getKeyPrefix().substr(base64_encode($str), 0, $length); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | - public static function getStoreKey(){ |
|
| 23 | + public static function getStoreKey() { |
|
| 24 | 24 | $config = SiteConfig::current_site_config(); |
| 25 | - if($config->StoreKey){ |
|
| 25 | + if ($config->StoreKey) { |
|
| 26 | 26 | return $config->StoreKey; |
| 27 | 27 | } |
| 28 | 28 | return null; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - public static function store_name_warning(){ |
|
| 31 | + public static function store_name_warning() { |
|
| 32 | 32 | $warning = null; |
| 33 | - if(self::getFoxyCartStoreName()===null){ |
|
| 33 | + if (self::getFoxyCartStoreName() === null) { |
|
| 34 | 34 | $warning = 'Must define FoxyCart Store Name in your site settings in the cms'; |
| 35 | 35 | } |
| 36 | 36 | return $warning; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - public static function getFoxyCartStoreName(){ |
|
| 39 | + public static function getFoxyCartStoreName() { |
|
| 40 | 40 | $config = SiteConfig::current_site_config(); |
| 41 | - if($config->StoreName){ |
|
| 41 | + if ($config->StoreName) { |
|
| 42 | 42 | return $config->StoreName; |
| 43 | 43 | } |
| 44 | 44 | return null; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | public static function FormActionURL() { |
| 48 | - return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName() ); |
|
| 48 | + return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName()); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | $foxyData["api_token"] = FoxyCart::getStoreKey(); |
| 60 | 60 | |
| 61 | 61 | $ch = curl_init(); |
| 62 | - curl_setopt($ch, CURLOPT_URL, "https://" . $foxy_domain . "/api"); |
|
| 62 | + curl_setopt($ch, CURLOPT_URL, "https://".$foxy_domain."/api"); |
|
| 63 | 63 | curl_setopt($ch, CURLOPT_POSTFIELDS, $foxyData); |
| 64 | 64 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 65 | 65 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | // The following if block will print any CURL errors you might have |
| 72 | 72 | if ($response == false) { |
| 73 | 73 | //trigger_error("Could not connect to FoxyCart API", E_USER_ERROR); |
| 74 | - SS_Log::log("Could not connect to FoxyCart API: " . $response, SS_Log::ERR); |
|
| 74 | + SS_Log::log("Could not connect to FoxyCart API: ".$response, SS_Log::ERR); |
|
| 75 | 75 | } |
| 76 | 76 | curl_close($ch); |
| 77 | 77 | |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | public static function putCustomer($Member = null) { |
| 98 | 98 | // throw error if no $Member Object |
| 99 | - if (!isset($Member)) ;//trigger_error('No Member set', E_USER_ERROR); |
|
| 99 | + if (!isset($Member)); //trigger_error('No Member set', E_USER_ERROR); |
|
| 100 | 100 | |
| 101 | 101 | // send updated customer record from API |
| 102 | 102 | $foxyData = array(); |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | return self::getAPIRequest($foxyData); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - public static function getKeyPrefix(){ |
|
| 115 | + public static function getKeyPrefix() { |
|
| 116 | 116 | return self::$keyPrefix; |
| 117 | 117 | } |
| 118 | 118 | |
@@ -7,46 +7,46 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | class FoxyCart extends Object { |
| 9 | 9 | |
| 10 | - private static $keyPrefix = 'dYnm1c'; |
|
| 11 | - |
|
| 12 | - public static function setStoreKey($length = 54, $count = 0){ |
|
| 13 | - $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now'); |
|
| 14 | - $strLength = strlen($charset); |
|
| 15 | - $str = ''; |
|
| 16 | - while($count < $length){ |
|
| 17 | - $str .= $charset[mt_rand(0, $strLength-1)]; |
|
| 18 | - $count++; |
|
| 19 | - } |
|
| 20 | - return self::getKeyPrefix().substr(base64_encode($str),0,$length); |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public static function getStoreKey(){ |
|
| 24 | - $config = SiteConfig::current_site_config(); |
|
| 25 | - if($config->StoreKey){ |
|
| 26 | - return $config->StoreKey; |
|
| 27 | - } |
|
| 28 | - return null; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public static function store_name_warning(){ |
|
| 32 | - $warning = null; |
|
| 33 | - if(self::getFoxyCartStoreName()===null){ |
|
| 34 | - $warning = 'Must define FoxyCart Store Name in your site settings in the cms'; |
|
| 35 | - } |
|
| 36 | - return $warning; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - public static function getFoxyCartStoreName(){ |
|
| 40 | - $config = SiteConfig::current_site_config(); |
|
| 41 | - if($config->StoreName){ |
|
| 42 | - return $config->StoreName; |
|
| 43 | - } |
|
| 44 | - return null; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public static function FormActionURL() { |
|
| 48 | - return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName() ); |
|
| 49 | - } |
|
| 10 | + private static $keyPrefix = 'dYnm1c'; |
|
| 11 | + |
|
| 12 | + public static function setStoreKey($length = 54, $count = 0){ |
|
| 13 | + $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.strtotime('now'); |
|
| 14 | + $strLength = strlen($charset); |
|
| 15 | + $str = ''; |
|
| 16 | + while($count < $length){ |
|
| 17 | + $str .= $charset[mt_rand(0, $strLength-1)]; |
|
| 18 | + $count++; |
|
| 19 | + } |
|
| 20 | + return self::getKeyPrefix().substr(base64_encode($str),0,$length); |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public static function getStoreKey(){ |
|
| 24 | + $config = SiteConfig::current_site_config(); |
|
| 25 | + if($config->StoreKey){ |
|
| 26 | + return $config->StoreKey; |
|
| 27 | + } |
|
| 28 | + return null; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public static function store_name_warning(){ |
|
| 32 | + $warning = null; |
|
| 33 | + if(self::getFoxyCartStoreName()===null){ |
|
| 34 | + $warning = 'Must define FoxyCart Store Name in your site settings in the cms'; |
|
| 35 | + } |
|
| 36 | + return $warning; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + public static function getFoxyCartStoreName(){ |
|
| 40 | + $config = SiteConfig::current_site_config(); |
|
| 41 | + if($config->StoreName){ |
|
| 42 | + return $config->StoreName; |
|
| 43 | + } |
|
| 44 | + return null; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public static function FormActionURL() { |
|
| 48 | + return sprintf('https://%s.foxycart.com/cart', self::getFoxyCartStoreName() ); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | /** |
| 52 | 52 | * FoxyCart API v1.1 functions |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | return self::getAPIRequest($foxyData); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - public static function getKeyPrefix(){ |
|
| 116 | - return self::$keyPrefix; |
|
| 117 | - } |
|
| 115 | + public static function getKeyPrefix(){ |
|
| 116 | + return self::$keyPrefix; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | } |
@@ -81,13 +81,17 @@ discard block |
||
| 81 | 81 | public static function getCustomer($Member = null) { |
| 82 | 82 | |
| 83 | 83 | // throw error if no $Member Object |
| 84 | - if (!isset($Member)) trigger_error('No Member set', E_USER_ERROR); |
|
| 84 | + if (!isset($Member)) { |
|
| 85 | + trigger_error('No Member set', E_USER_ERROR); |
|
| 86 | + } |
|
| 85 | 87 | |
| 86 | 88 | // grab customer record from API |
| 87 | 89 | |
| 88 | 90 | $foxyData = array(); |
| 89 | 91 | $foxyData["api_action"] = "customer_get"; |
| 90 | - if ($Member->Customer_ID) $foxyData["customer_id"] = $Member->Customer_ID; |
|
| 92 | + if ($Member->Customer_ID) { |
|
| 93 | + $foxyData["customer_id"] = $Member->Customer_ID; |
|
| 94 | + } |
|
| 91 | 95 | $foxyData["customer_email"] = $Member->Email; |
| 92 | 96 | |
| 93 | 97 | return self::getAPIRequest($foxyData); |
@@ -102,7 +106,9 @@ discard block |
||
| 102 | 106 | $foxyData = array(); |
| 103 | 107 | $foxyData["api_action"] = "customer_save"; |
| 104 | 108 | // customer_id will be 0 if created in SilverStripe. |
| 105 | - if ($Member->Customer_ID) $foxyData["customer_id"] = $Member->Customer_ID; |
|
| 109 | + if ($Member->Customer_ID) { |
|
| 110 | + $foxyData["customer_id"] = $Member->Customer_ID; |
|
| 111 | + } |
|
| 106 | 112 | $foxyData["customer_email"] = $Member->Email; |
| 107 | 113 | $foxyData["customer_password_hash"] = $Member->Password; |
| 108 | 114 | $foxyData["customer_password_salt"] = $Member->Salt; |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | 'Price.Nice' |
| 27 | 27 | ); |
| 28 | 28 | |
| 29 | - public function getCMSFields(){ |
|
| 29 | + public function getCMSFields() { |
|
| 30 | 30 | $fields = parent::getCMSFields(); |
| 31 | 31 | |
| 32 | 32 | $fields->addFieldsToTab('Root.Options', array( |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | return $fields; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - public function validate(){ |
|
| 40 | + public function validate() { |
|
| 41 | 41 | $result = parent::validate(); |
| 42 | 42 | |
| 43 | 43 | /*if($this->Country == 'DE' && $this->Postcode && strlen($this->Postcode) != 5) { |
@@ -2,65 +2,65 @@ |
||
| 2 | 2 | |
| 3 | 3 | class OrderDetail extends DataObject { |
| 4 | 4 | |
| 5 | - private static $singular_name = 'Order Detail'; |
|
| 6 | - private static $plural_name = 'Order Details'; |
|
| 7 | - private static $description = ''; |
|
| 5 | + private static $singular_name = 'Order Detail'; |
|
| 6 | + private static $plural_name = 'Order Details'; |
|
| 7 | + private static $description = ''; |
|
| 8 | 8 | |
| 9 | - private static $db = array( |
|
| 9 | + private static $db = array( |
|
| 10 | 10 | 'Quantity' => 'Int', |
| 11 | 11 | 'Price' => 'Currency' |
| 12 | 12 | ); |
| 13 | 13 | |
| 14 | - private static $has_one = array( |
|
| 14 | + private static $has_one = array( |
|
| 15 | 15 | 'Product' => 'ProductPage', |
| 16 | 16 | 'Order' => 'Order' |
| 17 | 17 | ); |
| 18 | 18 | |
| 19 | - private static $many_many = array( |
|
| 19 | + private static $many_many = array( |
|
| 20 | 20 | 'Options' => 'OptionItem' |
| 21 | 21 | ); |
| 22 | 22 | |
| 23 | - private static $summary_fields = array( |
|
| 23 | + private static $summary_fields = array( |
|
| 24 | 24 | 'Product.Title', |
| 25 | 25 | 'Quantity', |
| 26 | 26 | 'Price.Nice' |
| 27 | 27 | ); |
| 28 | 28 | |
| 29 | - public function getCMSFields(){ |
|
| 30 | - $fields = parent::getCMSFields(); |
|
| 29 | + public function getCMSFields(){ |
|
| 30 | + $fields = parent::getCMSFields(); |
|
| 31 | 31 | |
| 32 | 32 | $fields->addFieldsToTab('Root.Options', array( |
| 33 | 33 | GridField::create('Options', 'Product Options', $this->Options(), GridFieldConfig_RecordViewer::create()) |
| 34 | 34 | )); |
| 35 | 35 | |
| 36 | - $this->extend('updateCMSFields', $fields); |
|
| 37 | - return $fields; |
|
| 38 | - } |
|
| 36 | + $this->extend('updateCMSFields', $fields); |
|
| 37 | + return $fields; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public function validate(){ |
|
| 41 | - $result = parent::validate(); |
|
| 40 | + public function validate(){ |
|
| 41 | + $result = parent::validate(); |
|
| 42 | 42 | |
| 43 | - /*if($this->Country == 'DE' && $this->Postcode && strlen($this->Postcode) != 5) { |
|
| 43 | + /*if($this->Country == 'DE' && $this->Postcode && strlen($this->Postcode) != 5) { |
|
| 44 | 44 | $result->error('Need five digits for German postcodes'); |
| 45 | 45 | }*/ |
| 46 | 46 | |
| 47 | - return $result; |
|
| 48 | - } |
|
| 47 | + return $result; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function canView($member = false) { |
|
| 51 | - return Permission::check('Product_ORDERS'); |
|
| 52 | - } |
|
| 50 | + public function canView($member = false) { |
|
| 51 | + return Permission::check('Product_ORDERS'); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function canEdit($member = null) { |
|
| 54 | + public function canEdit($member = null) { |
|
| 55 | 55 | return false; |
| 56 | - } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function canDelete($member = null) { |
|
| 59 | - return Permission::check('Product_ORDERS'); |
|
| 60 | - } |
|
| 58 | + public function canDelete($member = null) { |
|
| 59 | + return Permission::check('Product_ORDERS'); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - public function canCreate($member = null) { |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 62 | + public function canCreate($member = null) { |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | 66 | } |
| 67 | 67 | \ No newline at end of file |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -class ProductImage extends DataObject{ |
|
| 8 | +class ProductImage extends DataObject { |
|
| 9 | 9 | |
| 10 | 10 | private static $db = array( |
| 11 | 11 | 'Title' => 'Text', |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | 'Title' => 'Caption' |
| 25 | 25 | ); |
| 26 | 26 | |
| 27 | - public function getCMSFields(){ |
|
| 27 | + public function getCMSFields() { |
|
| 28 | 28 | $fields = FieldList::create( |
| 29 | 29 | TextField::create('Title') |
| 30 | 30 | ->setTitle(_t('ProductImage.Title', 'Product Image Title')), |
@@ -7,25 +7,25 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | class ProductImage extends DataObject{ |
| 9 | 9 | |
| 10 | - private static $db = array( |
|
| 11 | - 'Title' => 'Text', |
|
| 12 | - 'SortOrder' => 'Int' |
|
| 13 | - ); |
|
| 10 | + private static $db = array( |
|
| 11 | + 'Title' => 'Text', |
|
| 12 | + 'SortOrder' => 'Int' |
|
| 13 | + ); |
|
| 14 | 14 | |
| 15 | - private static $has_one = array( |
|
| 16 | - 'Image' => 'Image', |
|
| 17 | - 'Parent' => 'SiteTree' |
|
| 18 | - ); |
|
| 15 | + private static $has_one = array( |
|
| 16 | + 'Image' => 'Image', |
|
| 17 | + 'Parent' => 'SiteTree' |
|
| 18 | + ); |
|
| 19 | 19 | |
| 20 | - private static $default_sort = 'SortOrder'; |
|
| 20 | + private static $default_sort = 'SortOrder'; |
|
| 21 | 21 | |
| 22 | - private static $summary_fields = array( |
|
| 23 | - 'Image.CMSThumbnail' => 'Image', |
|
| 24 | - 'Title' => 'Caption' |
|
| 25 | - ); |
|
| 22 | + private static $summary_fields = array( |
|
| 23 | + 'Image.CMSThumbnail' => 'Image', |
|
| 24 | + 'Title' => 'Caption' |
|
| 25 | + ); |
|
| 26 | 26 | |
| 27 | - public function getCMSFields(){ |
|
| 28 | - $fields = FieldList::create( |
|
| 27 | + public function getCMSFields(){ |
|
| 28 | + $fields = FieldList::create( |
|
| 29 | 29 | TextField::create('Title') |
| 30 | 30 | ->setTitle(_t('ProductImage.Title', 'Product Image Title')), |
| 31 | 31 | UploadField::create('Image') |
@@ -34,25 +34,25 @@ discard block |
||
| 34 | 34 | ->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png')) |
| 35 | 35 | ); |
| 36 | 36 | |
| 37 | - $this->extend('updateCMSFields', $fields); |
|
| 37 | + $this->extend('updateCMSFields', $fields); |
|
| 38 | 38 | |
| 39 | 39 | return $fields; |
| 40 | - } |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function canView($member = false) { |
|
| 43 | - return true; |
|
| 44 | - } |
|
| 42 | + public function canView($member = false) { |
|
| 43 | + return true; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function canEdit($member = null) { |
|
| 47 | - return Permission::check('Product_CANCRUD'); |
|
| 48 | - } |
|
| 46 | + public function canEdit($member = null) { |
|
| 47 | + return Permission::check('Product_CANCRUD'); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function canDelete($member = null) { |
|
| 51 | - return Permission::check('Product_CANCRUD'); |
|
| 52 | - } |
|
| 50 | + public function canDelete($member = null) { |
|
| 51 | + return Permission::check('Product_CANCRUD'); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function canCreate($member = null) { |
|
| 55 | - return Permission::check('Product_CANCRUD'); |
|
| 56 | - } |
|
| 54 | + public function canCreate($member = null) { |
|
| 55 | + return Permission::check('Product_CANCRUD'); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class Order extends DataObject implements PermissionProvider{ |
|
| 3 | +class Order extends DataObject implements PermissionProvider { |
|
| 4 | 4 | |
| 5 | 5 | private static $db = array( |
| 6 | 6 | 'Order_ID' => 'Int', |
@@ -91,9 +91,9 @@ discard block |
||
| 91 | 91 | return $this->getReceiptLink(); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - function getReceiptLink(){ |
|
| 95 | - $obj= HTMLVarchar::create(); |
|
| 96 | - $obj->setValue('<a href="' . $this->ReceiptURL . '" target="_blank" class="cms-panel-link action external-link">view</a>'); |
|
| 94 | + function getReceiptLink() { |
|
| 95 | + $obj = HTMLVarchar::create(); |
|
| 96 | + $obj->setValue('<a href="'.$this->ReceiptURL.'" target="_blank" class="cms-panel-link action external-link">view</a>'); |
|
| 97 | 97 | return $obj; |
| 98 | 98 | } |
| 99 | 99 | |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | class Order extends DataObject implements PermissionProvider{ |
| 4 | 4 | |
| 5 | - private static $db = array( |
|
| 5 | + private static $db = array( |
|
| 6 | 6 | 'Order_ID' => 'Int', |
| 7 | 7 | 'TransactionDate' => 'SS_Datetime', |
| 8 | 8 | 'ProductTotal' => 'Currency', |
@@ -14,11 +14,11 @@ discard block |
||
| 14 | 14 | 'Response' => 'Text' |
| 15 | 15 | ); |
| 16 | 16 | |
| 17 | - private static $has_one = array( |
|
| 17 | + private static $has_one = array( |
|
| 18 | 18 | 'Member' => 'Member' |
| 19 | 19 | ); |
| 20 | 20 | |
| 21 | - private static $has_many = array( |
|
| 21 | + private static $has_many = array( |
|
| 22 | 22 | 'Details' => 'OrderDetail' |
| 23 | 23 | ); |
| 24 | 24 | |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | 'ReceiptLink' |
| 39 | 39 | ); |
| 40 | 40 | |
| 41 | - private static $searchable_fields = array( |
|
| 41 | + private static $searchable_fields = array( |
|
| 42 | 42 | 'Order_ID', |
| 43 | 43 | 'TransactionDate' => array( |
| 44 | 44 | "field" => "DateField", |
@@ -86,28 +86,28 @@ discard block |
||
| 86 | 86 | return $obj; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - public function canView($member = false) { |
|
| 90 | - return Permission::check('Product_ORDERS'); |
|
| 91 | - } |
|
| 89 | + public function canView($member = false) { |
|
| 90 | + return Permission::check('Product_ORDERS'); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - public function canEdit($member = null) { |
|
| 93 | + public function canEdit($member = null) { |
|
| 94 | 94 | //return Permission::check('Product_ORDERS'); |
| 95 | 95 | return false; |
| 96 | - } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - public function canDelete($member = null) { |
|
| 98 | + public function canDelete($member = null) { |
|
| 99 | 99 | return false; |
| 100 | 100 | //return Permission::check('Product_ORDERS'); |
| 101 | - } |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - public function canCreate($member = null) { |
|
| 104 | - return false; |
|
| 105 | - } |
|
| 103 | + public function canCreate($member = null) { |
|
| 104 | + return false; |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - public function providePermissions() { |
|
| 108 | - return array( |
|
| 109 | - 'Product_ORDERS' => 'Allow user to manage Orders and related objects' |
|
| 110 | - ); |
|
| 111 | - } |
|
| 107 | + public function providePermissions() { |
|
| 108 | + return array( |
|
| 109 | + 'Product_ORDERS' => 'Allow user to manage Orders and related objects' |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | 113 | } |
| 114 | 114 | \ No newline at end of file |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -class OptionGroup extends DataObject{ |
|
| 8 | +class OptionGroup extends DataObject { |
|
| 9 | 9 | |
| 10 | 10 | private static $db = array( |
| 11 | 11 | 'Title' => 'Varchar(100)' |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | private static $plural_name = 'Product Option Groups'; |
| 16 | 16 | private static $description = 'Groups of product options, e.g. size, color, etc'; |
| 17 | 17 | |
| 18 | - function getCMSFields(){ |
|
| 18 | + function getCMSFields() { |
|
| 19 | 19 | |
| 20 | 20 | $fields = parent::getCMSFields(); |
| 21 | 21 | |
@@ -27,22 +27,22 @@ discard block |
||
| 27 | 27 | public function requireDefaultRecords() { |
| 28 | 28 | parent::requireDefaultRecords(); |
| 29 | 29 | // create a catch-all group |
| 30 | - if(!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 30 | + if (!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 31 | 31 | $do = new OptionGroup(); |
| 32 | 32 | $do->Title = "Options"; |
| 33 | 33 | $do->write(); |
| 34 | 34 | } |
| 35 | - if(!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) { |
|
| 35 | + if (!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) { |
|
| 36 | 36 | $do = new OptionGroup(); |
| 37 | 37 | $do->Title = "Size"; |
| 38 | 38 | $do->write(); |
| 39 | 39 | } |
| 40 | - if(!OptionGroup::get()->filter(array('Title' => 'Color'))->first()) { |
|
| 40 | + if (!OptionGroup::get()->filter(array('Title' => 'Color'))->first()) { |
|
| 41 | 41 | $do = new OptionGroup(); |
| 42 | 42 | $do->Title = "Color"; |
| 43 | 43 | $do->write(); |
| 44 | 44 | } |
| 45 | - if(!OptionGroup::get()->filter(array('Title' => 'Type'))->first()) { |
|
| 45 | + if (!OptionGroup::get()->filter(array('Title' => 'Type'))->first()) { |
|
| 46 | 46 | $do = new OptionGroup(); |
| 47 | 47 | $do->Title = "Type"; |
| 48 | 48 | $do->write(); |
@@ -53,30 +53,30 @@ discard block |
||
| 53 | 53 | return new RequiredFields(array('Title')); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - public function validate(){ |
|
| 56 | + public function validate() { |
|
| 57 | 57 | $result = parent::validate(); |
| 58 | 58 | |
| 59 | 59 | $title = $this->Title; |
| 60 | 60 | $firstChar = substr($title, 0, 1); |
| 61 | - if(preg_match('/[^a-zA-Z]/', $firstChar)){ |
|
| 61 | + if (preg_match('/[^a-zA-Z]/', $firstChar)) { |
|
| 62 | 62 | $result->error('The first character of the Title can only be a letter', 'bad'); |
| 63 | 63 | } |
| 64 | - if(preg_match('/[^a-zA-Z]\s/', $title)){ |
|
| 64 | + if (preg_match('/[^a-zA-Z]\s/', $title)) { |
|
| 65 | 65 | $result->error('Please only use letters, numbers and spaces in the title', 'bad'); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | return $result; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function onBeforeDelete(){ |
|
| 71 | + public function onBeforeDelete() { |
|
| 72 | 72 | parent::onBeforeDelete(); |
| 73 | 73 | |
| 74 | 74 | //make sure that if we delete this option group, we reassign the group's option items to the 'None' group. |
| 75 | 75 | $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); |
| 76 | 76 | |
| 77 | - if(isset($items)){ |
|
| 77 | + if (isset($items)) { |
|
| 78 | 78 | $noneGroup = OptionGroup::get()->filter(array('Title' => 'Options'))->first(); |
| 79 | - foreach($items as $item){ |
|
| 79 | + foreach ($items as $item) { |
|
| 80 | 80 | $item->ProductOptionGroupID = $noneGroup->ID; |
| 81 | 81 | $item->write(); |
| 82 | 82 | } |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | public function canEdit($member = null) { |
| 91 | - switch($this->Title){ |
|
| 91 | + switch ($this->Title) { |
|
| 92 | 92 | case 'Options': |
| 93 | 93 | return false; |
| 94 | 94 | break; |
@@ -7,32 +7,32 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | class OptionGroup extends DataObject{ |
| 9 | 9 | |
| 10 | - private static $db = array( |
|
| 11 | - 'Title' => 'Varchar(100)' |
|
| 12 | - ); |
|
| 10 | + private static $db = array( |
|
| 11 | + 'Title' => 'Varchar(100)' |
|
| 12 | + ); |
|
| 13 | 13 | |
| 14 | 14 | private static $singular_name = 'Product Option Group'; |
| 15 | - private static $plural_name = 'Product Option Groups'; |
|
| 16 | - private static $description = 'Groups of product options, e.g. size, color, etc'; |
|
| 15 | + private static $plural_name = 'Product Option Groups'; |
|
| 16 | + private static $description = 'Groups of product options, e.g. size, color, etc'; |
|
| 17 | 17 | |
| 18 | - function getCMSFields(){ |
|
| 18 | + function getCMSFields(){ |
|
| 19 | 19 | |
| 20 | - $fields = parent::getCMSFields(); |
|
| 20 | + $fields = parent::getCMSFields(); |
|
| 21 | 21 | |
| 22 | - $this->extend('getCMSFields', $fields); |
|
| 22 | + $this->extend('getCMSFields', $fields); |
|
| 23 | 23 | |
| 24 | - return $fields; |
|
| 25 | - } |
|
| 24 | + return $fields; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - public function requireDefaultRecords() { |
|
| 28 | - parent::requireDefaultRecords(); |
|
| 29 | - // create a catch-all group |
|
| 30 | - if(!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 31 | - $do = new OptionGroup(); |
|
| 32 | - $do->Title = "Options"; |
|
| 33 | - $do->write(); |
|
| 34 | - } |
|
| 35 | - if(!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) { |
|
| 27 | + public function requireDefaultRecords() { |
|
| 28 | + parent::requireDefaultRecords(); |
|
| 29 | + // create a catch-all group |
|
| 30 | + if(!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) { |
|
| 31 | + $do = new OptionGroup(); |
|
| 32 | + $do->Title = "Options"; |
|
| 33 | + $do->write(); |
|
| 34 | + } |
|
| 35 | + if(!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) { |
|
| 36 | 36 | $do = new OptionGroup(); |
| 37 | 37 | $do->Title = "Size"; |
| 38 | 38 | $do->write(); |
@@ -47,64 +47,64 @@ discard block |
||
| 47 | 47 | $do->Title = "Type"; |
| 48 | 48 | $do->write(); |
| 49 | 49 | } |
| 50 | - } |
|
| 51 | - |
|
| 52 | - public function getCMSValidator() { |
|
| 53 | - return new RequiredFields(array('Title')); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public function validate(){ |
|
| 57 | - $result = parent::validate(); |
|
| 58 | - |
|
| 59 | - $title = $this->Title; |
|
| 60 | - $firstChar = substr($title, 0, 1); |
|
| 61 | - if(preg_match('/[^a-zA-Z]/', $firstChar)){ |
|
| 62 | - $result->error('The first character of the Title can only be a letter', 'bad'); |
|
| 63 | - } |
|
| 64 | - if(preg_match('/[^a-zA-Z]\s/', $title)){ |
|
| 65 | - $result->error('Please only use letters, numbers and spaces in the title', 'bad'); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return $result; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function onBeforeDelete(){ |
|
| 72 | - parent::onBeforeDelete(); |
|
| 73 | - |
|
| 74 | - //make sure that if we delete this option group, we reassign the group's option items to the 'None' group. |
|
| 75 | - $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); |
|
| 76 | - |
|
| 77 | - if(isset($items)){ |
|
| 78 | - $noneGroup = OptionGroup::get()->filter(array('Title' => 'Options'))->first(); |
|
| 79 | - foreach($items as $item){ |
|
| 80 | - $item->ProductOptionGroupID = $noneGroup->ID; |
|
| 81 | - $item->write(); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function canView($member = false) { |
|
| 87 | - return true; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function canEdit($member = null) { |
|
| 91 | - switch($this->Title){ |
|
| 92 | - case 'Options': |
|
| 93 | - return false; |
|
| 94 | - break; |
|
| 95 | - default: |
|
| 96 | - return Permission::check('Product_CANCRUD'); |
|
| 97 | - break; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function canDelete($member = null) { |
|
| 103 | - return $this->canEdit(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function canCreate($member = null) { |
|
| 107 | - return Permission::check('Product_CANCRUD'); |
|
| 108 | - } |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + public function getCMSValidator() { |
|
| 53 | + return new RequiredFields(array('Title')); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public function validate(){ |
|
| 57 | + $result = parent::validate(); |
|
| 58 | + |
|
| 59 | + $title = $this->Title; |
|
| 60 | + $firstChar = substr($title, 0, 1); |
|
| 61 | + if(preg_match('/[^a-zA-Z]/', $firstChar)){ |
|
| 62 | + $result->error('The first character of the Title can only be a letter', 'bad'); |
|
| 63 | + } |
|
| 64 | + if(preg_match('/[^a-zA-Z]\s/', $title)){ |
|
| 65 | + $result->error('Please only use letters, numbers and spaces in the title', 'bad'); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return $result; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function onBeforeDelete(){ |
|
| 72 | + parent::onBeforeDelete(); |
|
| 73 | + |
|
| 74 | + //make sure that if we delete this option group, we reassign the group's option items to the 'None' group. |
|
| 75 | + $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); |
|
| 76 | + |
|
| 77 | + if(isset($items)){ |
|
| 78 | + $noneGroup = OptionGroup::get()->filter(array('Title' => 'Options'))->first(); |
|
| 79 | + foreach($items as $item){ |
|
| 80 | + $item->ProductOptionGroupID = $noneGroup->ID; |
|
| 81 | + $item->write(); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function canView($member = false) { |
|
| 87 | + return true; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function canEdit($member = null) { |
|
| 91 | + switch($this->Title){ |
|
| 92 | + case 'Options': |
|
| 93 | + return false; |
|
| 94 | + break; |
|
| 95 | + default: |
|
| 96 | + return Permission::check('Product_CANCRUD'); |
|
| 97 | + break; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function canDelete($member = null) { |
|
| 103 | + return $this->canEdit(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function canCreate($member = null) { |
|
| 107 | + return Permission::check('Product_CANCRUD'); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -class OptionItem extends DataObject{ |
|
| 8 | +class OptionItem extends DataObject { |
|
| 9 | 9 | |
| 10 | 10 | private static $db = array( |
| 11 | 11 | 'Title' => 'Text', |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | |
| 42 | 42 | private static $default_sort = 'SortOrder'; |
| 43 | 43 | |
| 44 | - public function getCMSFields(){ |
|
| 44 | + public function getCMSFields() { |
|
| 45 | 45 | $fields = FieldList::create( |
| 46 | 46 | new Tabset('Root', |
| 47 | 47 | new Tab('Main'), |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | $parentCode = $product->Code; |
| 59 | 59 | |
| 60 | 60 | // ProductOptionGroup Dropdown field w/ add new |
| 61 | - $groups = function(){ |
|
| 61 | + $groups = function() { |
|
| 62 | 62 | return OptionGroup::get()->map()->toArray(); |
| 63 | 63 | }; |
| 64 | 64 | $groupFields = singleton('OptionGroup')->getCMSFields(); |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | Textfield::create('Title') |
| 73 | 73 | ->setTitle(_t("OptionItem.Title", "Product Option Name")), |
| 74 | 74 | CheckboxField::create('Available') |
| 75 | - ->setTitle( _t("OptionItem.Available", "Available for purchase")) |
|
| 75 | + ->setTitle(_t("OptionItem.Available", "Available for purchase")) |
|
| 76 | 76 | ->setDescription(_t('OptionItem.AvailableDescription', "If unchecked, will disable this option in the drop down menu")), |
| 77 | 77 | $groupField |
| 78 | 78 | )); |
@@ -172,18 +172,18 @@ discard block |
||
| 172 | 172 | return $fields; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - public function validate(){ |
|
| 175 | + public function validate() { |
|
| 176 | 176 | $result = parent::validate(); |
| 177 | 177 | |
| 178 | - if($this->ProductOptionGroupID == 0){ |
|
| 178 | + if ($this->ProductOptionGroupID == 0) { |
|
| 179 | 179 | $result->error('Must set a Group prior to saving'); |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | return $result; |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus=false){ |
|
| 186 | - switch($oma){ |
|
| 185 | + public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false) { |
|
| 186 | + switch ($oma) { |
|
| 187 | 187 | case 'Subtract': |
| 188 | 188 | $symbol = '-'; |
| 189 | 189 | break; |
@@ -196,23 +196,23 @@ discard block |
||
| 196 | 196 | return $symbol; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - public function getWeightModifierWithSymbol(){ |
|
| 199 | + public function getWeightModifierWithSymbol() { |
|
| 200 | 200 | return self::getOptionModifierActionSymbol($this->WeightModifierAction).$this->WeightModifier; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - public function getPriceModifierWithSymbol(){ |
|
| 203 | + public function getPriceModifierWithSymbol() { |
|
| 204 | 204 | return self::getOptionModifierActionSymbol($this->PriceModifierAction).$this->PriceModifier; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - public function getCodeModifierWithSymbol(){ |
|
| 207 | + public function getCodeModifierWithSymbol() { |
|
| 208 | 208 | return self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - public function getProductOptionGroupTitle(){ |
|
| 211 | + public function getProductOptionGroupTitle() { |
|
| 212 | 212 | return $this->ProductOptionGroup()->Title; |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - public function getGeneratedValue(){ |
|
| 215 | + public function getGeneratedValue() { |
|
| 216 | 216 | $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
| 217 | 217 | $modPriceWithSymbol = OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
| 218 | 218 | $modWeight = ($this->WeightModifier) ? (string)$this->WeightModifier : '0'; |
@@ -221,15 +221,15 @@ discard block |
||
| 221 | 221 | return $this->Title.'{p'.$modPriceWithSymbol.'|w'.$modWeight.'|c'.$modCode.'}'; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - public function getGeneratedTitle(){ |
|
| 224 | + public function getGeneratedTitle() { |
|
| 225 | 225 | $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
| 226 | 226 | $title = $this->Title; |
| 227 | - $title .= ($this->PriceModifier != 0) ? ': ('.OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction, $returnWithOnlyPlusMinus=true).'$'.$modPrice.')' : ''; |
|
| 227 | + $title .= ($this->PriceModifier != 0) ? ': ('.OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction, $returnWithOnlyPlusMinus = true).'$'.$modPrice.')' : ''; |
|
| 228 | 228 | return $title; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | - public function getAvailability(){ |
|
| 232 | - return ($this->Available == 0) ? true : false ; |
|
| 231 | + public function getAvailability() { |
|
| 232 | + return ($this->Available == 0) ? true : false; |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | public function canView($member = false) { |
@@ -7,85 +7,85 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | class OptionItem extends DataObject{ |
| 9 | 9 | |
| 10 | - private static $db = array( |
|
| 11 | - 'Title' => 'Text', |
|
| 12 | - 'WeightModifier' => 'Int', |
|
| 13 | - 'CodeModifier' => 'Text', |
|
| 14 | - 'PriceModifier' => 'Currency', |
|
| 15 | - 'WeightModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 16 | - 'CodeModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 17 | - 'PriceModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 18 | - 'Available' => 'Boolean', |
|
| 19 | - 'SortOrder' => 'Int' |
|
| 20 | - ); |
|
| 21 | - |
|
| 22 | - private static $has_one = array( |
|
| 23 | - 'Product' => 'ProductPage', |
|
| 24 | - 'ProductOptionGroup' => 'OptionGroup', |
|
| 25 | - //'Category' => 'ProductCategory' |
|
| 26 | - ); |
|
| 10 | + private static $db = array( |
|
| 11 | + 'Title' => 'Text', |
|
| 12 | + 'WeightModifier' => 'Int', |
|
| 13 | + 'CodeModifier' => 'Text', |
|
| 14 | + 'PriceModifier' => 'Currency', |
|
| 15 | + 'WeightModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 16 | + 'CodeModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 17 | + 'PriceModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
| 18 | + 'Available' => 'Boolean', |
|
| 19 | + 'SortOrder' => 'Int' |
|
| 20 | + ); |
|
| 21 | + |
|
| 22 | + private static $has_one = array( |
|
| 23 | + 'Product' => 'ProductPage', |
|
| 24 | + 'ProductOptionGroup' => 'OptionGroup', |
|
| 25 | + //'Category' => 'ProductCategory' |
|
| 26 | + ); |
|
| 27 | 27 | |
| 28 | 28 | private static $belongs_many_many = array( |
| 29 | 29 | 'OrderDetails' => 'OrderDetail' |
| 30 | 30 | ); |
| 31 | 31 | |
| 32 | 32 | private static $defaults = array( |
| 33 | - 'Available' => true |
|
| 34 | - ); |
|
| 33 | + 'Available' => true |
|
| 34 | + ); |
|
| 35 | 35 | |
| 36 | - private static $summary_fields = array( |
|
| 37 | - 'Title' => 'Title', |
|
| 38 | - 'ProductOptionGroup.Title' => 'Group', |
|
| 36 | + private static $summary_fields = array( |
|
| 37 | + 'Title' => 'Title', |
|
| 38 | + 'ProductOptionGroup.Title' => 'Group', |
|
| 39 | 39 | 'Available.Nice' => 'Available' |
| 40 | - ); |
|
| 40 | + ); |
|
| 41 | 41 | |
| 42 | - private static $default_sort = 'SortOrder'; |
|
| 42 | + private static $default_sort = 'SortOrder'; |
|
| 43 | 43 | |
| 44 | - public function getCMSFields(){ |
|
| 45 | - $fields = FieldList::create( |
|
| 46 | - new Tabset('Root', |
|
| 47 | - new Tab('Main'), |
|
| 48 | - new Tab('Modifiers') |
|
| 49 | - ) |
|
| 50 | - ); |
|
| 44 | + public function getCMSFields(){ |
|
| 45 | + $fields = FieldList::create( |
|
| 46 | + new Tabset('Root', |
|
| 47 | + new Tab('Main'), |
|
| 48 | + new Tab('Modifiers') |
|
| 49 | + ) |
|
| 50 | + ); |
|
| 51 | 51 | |
| 52 | - // set variables from Product |
|
| 52 | + // set variables from Product |
|
| 53 | 53 | $productID = ($this->ProductID != 0) ? $this->ProductID : Session::get('CMSMain.currentPage'); |
| 54 | 54 | $product = ProductPage::get()->byID($productID); |
| 55 | 55 | |
| 56 | - $parentPrice = $product->obj('Price')->Nice(); |
|
| 57 | - $parentWeight = $product->Weight; |
|
| 58 | - $parentCode = $product->Code; |
|
| 59 | - |
|
| 60 | - // ProductOptionGroup Dropdown field w/ add new |
|
| 61 | - $groups = function(){ |
|
| 62 | - return OptionGroup::get()->map()->toArray(); |
|
| 63 | - }; |
|
| 64 | - $groupFields = singleton('OptionGroup')->getCMSFields(); |
|
| 65 | - $groupField = DropdownField::create('ProductOptionGroupID', _t("OptionItem.Group", "Group"), $groups()) |
|
| 66 | - ->setEmptyString('') |
|
| 56 | + $parentPrice = $product->obj('Price')->Nice(); |
|
| 57 | + $parentWeight = $product->Weight; |
|
| 58 | + $parentCode = $product->Code; |
|
| 59 | + |
|
| 60 | + // ProductOptionGroup Dropdown field w/ add new |
|
| 61 | + $groups = function(){ |
|
| 62 | + return OptionGroup::get()->map()->toArray(); |
|
| 63 | + }; |
|
| 64 | + $groupFields = singleton('OptionGroup')->getCMSFields(); |
|
| 65 | + $groupField = DropdownField::create('ProductOptionGroupID', _t("OptionItem.Group", "Group"), $groups()) |
|
| 66 | + ->setEmptyString('') |
|
| 67 | 67 | ->setDescription(_t('OptionItem.GroupDescription', 'Name of this group of options. Managed in <a href="admin/settings">Settings > FoxyStripe > Option Groups</a>')); |
| 68 | - if (class_exists('QuickAddNewExtension')) $groupField->useAddNew('OptionGroup', $groups, $groupFields); |
|
| 68 | + if (class_exists('QuickAddNewExtension')) $groupField->useAddNew('OptionGroup', $groups, $groupFields); |
|
| 69 | 69 | |
| 70 | 70 | $fields->addFieldsToTab('Root.Main', array( |
| 71 | - HeaderField::create('DetailsHD', _t("OptionItem.DetailsHD", "Product Option Details"), 2), |
|
| 72 | - Textfield::create('Title') |
|
| 71 | + HeaderField::create('DetailsHD', _t("OptionItem.DetailsHD", "Product Option Details"), 2), |
|
| 72 | + Textfield::create('Title') |
|
| 73 | 73 | ->setTitle(_t("OptionItem.Title", "Product Option Name")), |
| 74 | - CheckboxField::create('Available') |
|
| 75 | - ->setTitle( _t("OptionItem.Available", "Available for purchase")) |
|
| 74 | + CheckboxField::create('Available') |
|
| 75 | + ->setTitle( _t("OptionItem.Available", "Available for purchase")) |
|
| 76 | 76 | ->setDescription(_t('OptionItem.AvailableDescription', "If unchecked, will disable this option in the drop down menu")), |
| 77 | - $groupField |
|
| 78 | - )); |
|
| 77 | + $groupField |
|
| 78 | + )); |
|
| 79 | 79 | |
| 80 | - $fields->addFieldsToTab('Root.Modifiers', array( |
|
| 81 | - HeaderField::create('ModifyHD', _t('OptionItem.ModifyHD', 'Product Option Modifiers'), 2), |
|
| 80 | + $fields->addFieldsToTab('Root.Modifiers', array( |
|
| 81 | + HeaderField::create('ModifyHD', _t('OptionItem.ModifyHD', 'Product Option Modifiers'), 2), |
|
| 82 | 82 | |
| 83 | - // Weight Modifier Fields |
|
| 84 | - HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 3), |
|
| 85 | - NumericField::create('WeightModifier') |
|
| 83 | + // Weight Modifier Fields |
|
| 84 | + HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 3), |
|
| 85 | + NumericField::create('WeightModifier') |
|
| 86 | 86 | ->setTitle(_t('OptionItem.WeightModifier', 'Weight')), |
| 87 | - DropdownField::create('WeightModifierAction', _t('OptionItem.WeightModifierAction', 'Weight Modification'), |
|
| 88 | - array( |
|
| 87 | + DropdownField::create('WeightModifierAction', _t('OptionItem.WeightModifierAction', 'Weight Modification'), |
|
| 88 | + array( |
|
| 89 | 89 | 'Add' => _t( |
| 90 | 90 | 'OptionItem.WeightAdd', |
| 91 | 91 | "Add to Base Weight ({weight})", |
@@ -99,56 +99,56 @@ discard block |
||
| 99 | 99 | array('weight' => $parentWeight) |
| 100 | 100 | ), |
| 101 | 101 | 'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight') |
| 102 | - ) |
|
| 103 | - )->setEmptyString('') |
|
| 104 | - ->setDescription(_t('OptionItem.WeightDescription', 'Does weight modify or replace base weight?')), |
|
| 102 | + ) |
|
| 103 | + )->setEmptyString('') |
|
| 104 | + ->setDescription(_t('OptionItem.WeightDescription', 'Does weight modify or replace base weight?')), |
|
| 105 | 105 | |
| 106 | - // Price Modifier FIelds |
|
| 107 | - HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 3), |
|
| 108 | - CurrencyField::create('PriceModifier') |
|
| 106 | + // Price Modifier FIelds |
|
| 107 | + HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 3), |
|
| 108 | + CurrencyField::create('PriceModifier') |
|
| 109 | 109 | ->setTitle(_t('OptionItem.PriceModifier', 'Price')), |
| 110 | - DropdownField::create('PriceModifierAction', _t('OptionItem.PriceModifierAction', 'Price Modification'), |
|
| 111 | - array( |
|
| 112 | - 'Add' => _t( |
|
| 110 | + DropdownField::create('PriceModifierAction', _t('OptionItem.PriceModifierAction', 'Price Modification'), |
|
| 111 | + array( |
|
| 112 | + 'Add' => _t( |
|
| 113 | 113 | 'OptionItem.PriceAdd', |
| 114 | 114 | "Add to Base Price ({price})", |
| 115 | 115 | 'Add to price', |
| 116 | 116 | array('price' => $parentPrice) |
| 117 | 117 | ), |
| 118 | - 'Subtract' => _t( |
|
| 118 | + 'Subtract' => _t( |
|
| 119 | 119 | 'OptionItem.PriceSubtract', |
| 120 | 120 | "Subtract from Base Price ({price})", |
| 121 | 121 | 'Subtract from price', |
| 122 | 122 | array('price' => $parentPrice) |
| 123 | 123 | ), |
| 124 | - 'Set' => _t('OptionItem.PriceSet', 'Set as a new Price') |
|
| 125 | - ) |
|
| 126 | - )->setEmptyString('') |
|
| 127 | - ->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')), |
|
| 128 | - |
|
| 129 | - // Code Modifier Fields |
|
| 130 | - HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 3), |
|
| 131 | - TextField::create('CodeModifier') |
|
| 124 | + 'Set' => _t('OptionItem.PriceSet', 'Set as a new Price') |
|
| 125 | + ) |
|
| 126 | + )->setEmptyString('') |
|
| 127 | + ->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')), |
|
| 128 | + |
|
| 129 | + // Code Modifier Fields |
|
| 130 | + HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 3), |
|
| 131 | + TextField::create('CodeModifier') |
|
| 132 | 132 | ->setTitle(_t('OptionItem.CodeModifier', 'Code')), |
| 133 | - DropdownField::create('CodeModifierAction', _t('OptionItem.CodeModifierAction', 'Code Modification'), |
|
| 134 | - array( |
|
| 135 | - 'Add' => _t( |
|
| 133 | + DropdownField::create('CodeModifierAction', _t('OptionItem.CodeModifierAction', 'Code Modification'), |
|
| 134 | + array( |
|
| 135 | + 'Add' => _t( |
|
| 136 | 136 | 'OptionItem.CodeAdd', |
| 137 | 137 | "Add to Base Code ({code})", |
| 138 | 138 | 'Add to code', |
| 139 | 139 | array('code' => $parentCode) |
| 140 | 140 | ), |
| 141 | - 'Subtract' => _t( |
|
| 141 | + 'Subtract' => _t( |
|
| 142 | 142 | 'OptionItem.CodeSubtract', |
| 143 | 143 | 'Subtract from Base Code ({code})', |
| 144 | 144 | 'Subtract from code', |
| 145 | 145 | array('code' => $parentCode) |
| 146 | 146 | ), |
| 147 | - 'Set' => _t('OptionItem.CodeSet', 'Set as a new Code') |
|
| 148 | - ) |
|
| 149 | - )->setEmptyString('') |
|
| 150 | - ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')) |
|
| 151 | - )); |
|
| 147 | + 'Set' => _t('OptionItem.CodeSet', 'Set as a new Code') |
|
| 148 | + ) |
|
| 149 | + )->setEmptyString('') |
|
| 150 | + ->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')) |
|
| 151 | + )); |
|
| 152 | 152 | |
| 153 | 153 | /* |
| 154 | 154 | // Cateogry Dropdown field w/ add new |
@@ -167,85 +167,85 @@ discard block |
||
| 167 | 167 | */ |
| 168 | 168 | |
| 169 | 169 | // allow CMS fields to be extended |
| 170 | - $this->extend('getCMSFields', $fields); |
|
| 171 | - |
|
| 172 | - return $fields; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - public function validate(){ |
|
| 176 | - $result = parent::validate(); |
|
| 177 | - |
|
| 178 | - if($this->ProductOptionGroupID == 0){ |
|
| 179 | - $result->error('Must set a Group prior to saving'); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $result; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus=false){ |
|
| 186 | - switch($oma){ |
|
| 187 | - case 'Subtract': |
|
| 188 | - $symbol = '-'; |
|
| 189 | - break; |
|
| 190 | - case 'Set': |
|
| 191 | - $symbol = ($returnWithOnlyPlusMinus) ? '' : ':'; |
|
| 192 | - break; |
|
| 193 | - default: |
|
| 194 | - $symbol = '+'; |
|
| 195 | - } |
|
| 196 | - return $symbol; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - public function getWeightModifierWithSymbol(){ |
|
| 200 | - return self::getOptionModifierActionSymbol($this->WeightModifierAction).$this->WeightModifier; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - public function getPriceModifierWithSymbol(){ |
|
| 204 | - return self::getOptionModifierActionSymbol($this->PriceModifierAction).$this->PriceModifier; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - public function getCodeModifierWithSymbol(){ |
|
| 208 | - return self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - public function getProductOptionGroupTitle(){ |
|
| 212 | - return $this->ProductOptionGroup()->Title; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - public function getGeneratedValue(){ |
|
| 216 | - $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
| 217 | - $modPriceWithSymbol = OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
|
| 218 | - $modWeight = ($this->WeightModifier) ? (string)$this->WeightModifier : '0'; |
|
| 219 | - $modWeight = OptionItem::getOptionModifierActionSymbol($this->WeightModifierAction).$modWeight; |
|
| 220 | - $modCode = OptionItem::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 221 | - return $this->Title.'{p'.$modPriceWithSymbol.'|w'.$modWeight.'|c'.$modCode.'}'; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - public function getGeneratedTitle(){ |
|
| 225 | - $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
| 226 | - $title = $this->Title; |
|
| 227 | - $title .= ($this->PriceModifier != 0) ? ': ('.OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction, $returnWithOnlyPlusMinus=true).'$'.$modPrice.')' : ''; |
|
| 228 | - return $title; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - public function getAvailability(){ |
|
| 232 | - return ($this->Available == 0) ? true : false ; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - public function canView($member = false) { |
|
| 236 | - return true; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - public function canEdit($member = null) { |
|
| 240 | - return Permission::check('Product_CANCRUD'); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - public function canDelete($member = null) { |
|
| 244 | - return Permission::check('Product_CANCRUD'); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - public function canCreate($member = null) { |
|
| 248 | - return Permission::check('Product_CANCRUD'); |
|
| 249 | - } |
|
| 170 | + $this->extend('getCMSFields', $fields); |
|
| 171 | + |
|
| 172 | + return $fields; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + public function validate(){ |
|
| 176 | + $result = parent::validate(); |
|
| 177 | + |
|
| 178 | + if($this->ProductOptionGroupID == 0){ |
|
| 179 | + $result->error('Must set a Group prior to saving'); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $result; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus=false){ |
|
| 186 | + switch($oma){ |
|
| 187 | + case 'Subtract': |
|
| 188 | + $symbol = '-'; |
|
| 189 | + break; |
|
| 190 | + case 'Set': |
|
| 191 | + $symbol = ($returnWithOnlyPlusMinus) ? '' : ':'; |
|
| 192 | + break; |
|
| 193 | + default: |
|
| 194 | + $symbol = '+'; |
|
| 195 | + } |
|
| 196 | + return $symbol; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + public function getWeightModifierWithSymbol(){ |
|
| 200 | + return self::getOptionModifierActionSymbol($this->WeightModifierAction).$this->WeightModifier; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + public function getPriceModifierWithSymbol(){ |
|
| 204 | + return self::getOptionModifierActionSymbol($this->PriceModifierAction).$this->PriceModifier; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + public function getCodeModifierWithSymbol(){ |
|
| 208 | + return self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + public function getProductOptionGroupTitle(){ |
|
| 212 | + return $this->ProductOptionGroup()->Title; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + public function getGeneratedValue(){ |
|
| 216 | + $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
| 217 | + $modPriceWithSymbol = OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
|
| 218 | + $modWeight = ($this->WeightModifier) ? (string)$this->WeightModifier : '0'; |
|
| 219 | + $modWeight = OptionItem::getOptionModifierActionSymbol($this->WeightModifierAction).$modWeight; |
|
| 220 | + $modCode = OptionItem::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
| 221 | + return $this->Title.'{p'.$modPriceWithSymbol.'|w'.$modWeight.'|c'.$modCode.'}'; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + public function getGeneratedTitle(){ |
|
| 225 | + $modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
| 226 | + $title = $this->Title; |
|
| 227 | + $title .= ($this->PriceModifier != 0) ? ': ('.OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction, $returnWithOnlyPlusMinus=true).'$'.$modPrice.')' : ''; |
|
| 228 | + return $title; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + public function getAvailability(){ |
|
| 232 | + return ($this->Available == 0) ? true : false ; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + public function canView($member = false) { |
|
| 236 | + return true; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + public function canEdit($member = null) { |
|
| 240 | + return Permission::check('Product_CANCRUD'); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + public function canDelete($member = null) { |
|
| 244 | + return Permission::check('Product_CANCRUD'); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + public function canCreate($member = null) { |
|
| 248 | + return Permission::check('Product_CANCRUD'); |
|
| 249 | + } |
|
| 250 | 250 | |
| 251 | 251 | } |
@@ -65,7 +65,9 @@ |
||
| 65 | 65 | $groupField = DropdownField::create('ProductOptionGroupID', _t("OptionItem.Group", "Group"), $groups()) |
| 66 | 66 | ->setEmptyString('') |
| 67 | 67 | ->setDescription(_t('OptionItem.GroupDescription', 'Name of this group of options. Managed in <a href="admin/settings">Settings > FoxyStripe > Option Groups</a>')); |
| 68 | - if (class_exists('QuickAddNewExtension')) $groupField->useAddNew('OptionGroup', $groups, $groupFields); |
|
| 68 | + if (class_exists('QuickAddNewExtension')) { |
|
| 69 | + $groupField->useAddNew('OptionGroup', $groups, $groupFields); |
|
| 70 | + } |
|
| 69 | 71 | |
| 70 | 72 | $fields->addFieldsToTab('Root.Main', array( |
| 71 | 73 | HeaderField::create('DetailsHD', _t("OptionItem.DetailsHD", "Product Option Details"), 2), |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | ); |
| 37 | 37 | private static $indexes = array(); |
| 38 | 38 | |
| 39 | - public function getCMSFields(){ |
|
| 39 | + public function getCMSFields() { |
|
| 40 | 40 | $fields = parent::getCMSFields(); |
| 41 | 41 | |
| 42 | 42 | $fields->removeByName('ProductPageID'); |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * TODO implement validation to ensure values aren't duplicated in multiple tiers |
| 61 | 61 | */ |
| 62 | - public function validate(){ |
|
| 62 | + public function validate() { |
|
| 63 | 63 | $result = parent::validate(); |
| 64 | 64 | |
| 65 | 65 | /*$tierQuantity = ProductDiscountTier::get() |
@@ -88,11 +88,11 @@ discard block |
||
| 88 | 88 | return $result; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - public function getTitle(){ |
|
| 91 | + public function getTitle() { |
|
| 92 | 92 | return "{$this->Quantity} at {$this->Percentage}%"; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - public function getDiscountPercentage(){ |
|
| 95 | + public function getDiscountPercentage() { |
|
| 96 | 96 | return "{$this->Percentage}%"; |
| 97 | 97 | } |
| 98 | 98 | |
@@ -2,67 +2,67 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | class ProductDiscountTier extends DataObject { |
| 4 | 4 | |
| 5 | - private static $singular_name = 'Discount Tier'; |
|
| 6 | - private static $plural_name = 'Discount Tiers'; |
|
| 7 | - private static $description = 'A discount tier for a Product Discount'; |
|
| 8 | - |
|
| 9 | - private static $db = array( |
|
| 10 | - 'Quantity' => 'Int', |
|
| 11 | - 'Percentage' => 'Int' |
|
| 12 | - ); |
|
| 13 | - private static $has_one = array( |
|
| 14 | - 'ProductPage' => 'ProductPage' |
|
| 15 | - ); |
|
| 16 | - private static $has_many = array(); |
|
| 17 | - private static $many_many = array(); |
|
| 18 | - private static $many_many_extraFields = array(); |
|
| 19 | - private static $belongs_many_many = array(); |
|
| 20 | - |
|
| 21 | - private static $casting = array(); |
|
| 22 | - private static $defaults = array(); |
|
| 23 | - private static $default_sort = array( |
|
| 24 | - 'Quantity' |
|
| 25 | - ); |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - private static $summary_fields = array( |
|
| 29 | - 'Quantity', |
|
| 30 | - 'DiscountPercentage' |
|
| 31 | - ); |
|
| 32 | - private static $searchable_fields = array(); |
|
| 33 | - private static $field_labels = array( |
|
| 34 | - 'Quantity' => 'Quantity', |
|
| 35 | - 'DiscountPercentage' => 'Discount' |
|
| 36 | - ); |
|
| 37 | - private static $indexes = array(); |
|
| 38 | - |
|
| 39 | - public function getCMSFields(){ |
|
| 40 | - $fields = parent::getCMSFields(); |
|
| 41 | - |
|
| 42 | - $fields->removeByName('ProductPageID'); |
|
| 43 | - |
|
| 44 | - $quantity = $fields->dataFieldByName('Quantity'); |
|
| 45 | - $quantity->setTitle('Quantity to trigger discount'); |
|
| 46 | - $percentage = $fields->dataFieldByName('Percentage'); |
|
| 47 | - $percentage->setTitle('Percent discount'); |
|
| 48 | - |
|
| 49 | - $this->extend('updateCMSFields', $fields); |
|
| 50 | - return $fields; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function getCMSValidator() { |
|
| 54 | - return new RequiredFields(array('Quantity', 'Percentage')); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @return ValidationResult |
|
| 59 | - * |
|
| 60 | - * TODO implement validation to ensure values aren't duplicated in multiple tiers |
|
| 61 | - */ |
|
| 62 | - public function validate(){ |
|
| 63 | - $result = parent::validate(); |
|
| 64 | - |
|
| 65 | - /*$tierQuantity = ProductDiscountTier::get() |
|
| 5 | + private static $singular_name = 'Discount Tier'; |
|
| 6 | + private static $plural_name = 'Discount Tiers'; |
|
| 7 | + private static $description = 'A discount tier for a Product Discount'; |
|
| 8 | + |
|
| 9 | + private static $db = array( |
|
| 10 | + 'Quantity' => 'Int', |
|
| 11 | + 'Percentage' => 'Int' |
|
| 12 | + ); |
|
| 13 | + private static $has_one = array( |
|
| 14 | + 'ProductPage' => 'ProductPage' |
|
| 15 | + ); |
|
| 16 | + private static $has_many = array(); |
|
| 17 | + private static $many_many = array(); |
|
| 18 | + private static $many_many_extraFields = array(); |
|
| 19 | + private static $belongs_many_many = array(); |
|
| 20 | + |
|
| 21 | + private static $casting = array(); |
|
| 22 | + private static $defaults = array(); |
|
| 23 | + private static $default_sort = array( |
|
| 24 | + 'Quantity' |
|
| 25 | + ); |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + private static $summary_fields = array( |
|
| 29 | + 'Quantity', |
|
| 30 | + 'DiscountPercentage' |
|
| 31 | + ); |
|
| 32 | + private static $searchable_fields = array(); |
|
| 33 | + private static $field_labels = array( |
|
| 34 | + 'Quantity' => 'Quantity', |
|
| 35 | + 'DiscountPercentage' => 'Discount' |
|
| 36 | + ); |
|
| 37 | + private static $indexes = array(); |
|
| 38 | + |
|
| 39 | + public function getCMSFields(){ |
|
| 40 | + $fields = parent::getCMSFields(); |
|
| 41 | + |
|
| 42 | + $fields->removeByName('ProductPageID'); |
|
| 43 | + |
|
| 44 | + $quantity = $fields->dataFieldByName('Quantity'); |
|
| 45 | + $quantity->setTitle('Quantity to trigger discount'); |
|
| 46 | + $percentage = $fields->dataFieldByName('Percentage'); |
|
| 47 | + $percentage->setTitle('Percent discount'); |
|
| 48 | + |
|
| 49 | + $this->extend('updateCMSFields', $fields); |
|
| 50 | + return $fields; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function getCMSValidator() { |
|
| 54 | + return new RequiredFields(array('Quantity', 'Percentage')); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @return ValidationResult |
|
| 59 | + * |
|
| 60 | + * TODO implement validation to ensure values aren't duplicated in multiple tiers |
|
| 61 | + */ |
|
| 62 | + public function validate(){ |
|
| 63 | + $result = parent::validate(); |
|
| 64 | + |
|
| 65 | + /*$tierQuantity = ProductDiscountTier::get() |
|
| 66 | 66 | ->filter( |
| 67 | 67 | array( |
| 68 | 68 | 'ProductDiscountID' => $this->ProductDiscountID, |
@@ -85,31 +85,31 @@ discard block |
||
| 85 | 85 | $result->error($this->Percentage." is already used in another discount tier. Please use a different percentage"); |
| 86 | 86 | }*/ |
| 87 | 87 | |
| 88 | - return $result; |
|
| 89 | - } |
|
| 88 | + return $result; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - public function getTitle(){ |
|
| 92 | - return "{$this->Quantity} at {$this->Percentage}%"; |
|
| 93 | - } |
|
| 91 | + public function getTitle(){ |
|
| 92 | + return "{$this->Quantity} at {$this->Percentage}%"; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - public function getDiscountPercentage(){ |
|
| 96 | - return "{$this->Percentage}%"; |
|
| 97 | - } |
|
| 95 | + public function getDiscountPercentage(){ |
|
| 96 | + return "{$this->Percentage}%"; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - public function canView($member = false) { |
|
| 100 | - return true; |
|
| 101 | - } |
|
| 99 | + public function canView($member = false) { |
|
| 100 | + return true; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - public function canEdit($member = null) { |
|
| 104 | - return Permission::check('Product_CANCRUD'); |
|
| 105 | - } |
|
| 103 | + public function canEdit($member = null) { |
|
| 104 | + return Permission::check('Product_CANCRUD'); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - public function canDelete($member = null) { |
|
| 108 | - return Permission::check('Product_CANCRUD'); |
|
| 109 | - } |
|
| 107 | + public function canDelete($member = null) { |
|
| 108 | + return Permission::check('Product_CANCRUD'); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - public function canCreate($member = null) { |
|
| 112 | - return Permission::check('Product_CANCRUD'); |
|
| 113 | - } |
|
| 111 | + public function canCreate($member = null) { |
|
| 112 | + return Permission::check('Product_CANCRUD'); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | 115 | } |