| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 254 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 58 | public function __construct(Controller $controller, $name) |
||
| 59 | { |
||
| 60 | |||
| 61 | //set basics |
||
| 62 | $requiredFields = array(); |
||
| 63 | |||
| 64 | //requirements |
||
| 65 | Requirements::javascript('ecommerce/javascript/EcomOrderFormAddress.js'); // LEAVE HERE - NOT EASY TO INCLUDE VIA TEMPLATE |
||
| 66 | if (EcommerceConfig::get('OrderAddress', 'use_separate_shipping_address')) { |
||
| 67 | Requirements::javascript('ecommerce/javascript/EcomOrderFormShipping.js'); // LEAVE HERE - NOT EASY TO INCLUDE VIA TEMPLATE |
||
| 68 | } |
||
| 69 | |||
| 70 | // ________________ 1) Order + Member + Address fields |
||
| 71 | |||
| 72 | |||
| 73 | // define field lists ... |
||
| 74 | $addressFieldsMember = FieldList::create(); |
||
| 75 | $addressFieldsBilling = FieldList::create(); |
||
| 76 | $addressFieldsShipping = null; |
||
| 77 | $useShippingAddressField = null; |
||
| 78 | $shippingAddressFirst = EcommerceConfig::get('OrderFormAddress', 'shipping_address_first'); |
||
| 79 | |||
| 80 | $addressFieldsMember->push( |
||
| 81 | HeaderField::create( |
||
| 82 | 'AddressFieldsMemberHeading', |
||
| 83 | _t('OrderFormAddress.Address_Fields_Member_Heading', 'Your Personal Details'), |
||
| 84 | 3 |
||
| 85 | ) |
||
| 86 | ); |
||
| 87 | //find member |
||
| 88 | $this->order = ShoppingCart::current_order(); |
||
| 89 | $this->orderMember = $this->order->CreateOrReturnExistingMember(false); |
||
| 90 | $this->loggedInMember = Member::currentUser(); |
||
| 91 | |||
| 92 | //strange security situation... |
||
| 93 | if ($this->orderMember->exists() && $this->loggedInMember) { |
||
| 94 | if ($this->orderMember->ID != $this->loggedInMember->ID) { |
||
| 95 | if (!$this->loggedInMember->IsShopAdmin()) { |
||
| 96 | $this->loggedInMember->logOut(); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | //member fields |
||
| 102 | if ($this->orderMember) { |
||
| 103 | $memberFields = $this->orderMember->getEcommerceFields(); |
||
| 104 | $requiredFields = array_merge($requiredFields, $this->orderMember->getEcommerceRequiredFields()); |
||
| 105 | $addressFieldsMember->merge($memberFields); |
||
| 106 | } |
||
| 107 | |||
| 108 | //billing address field |
||
| 109 | $billingAddress = $this->order->CreateOrReturnExistingAddress('BillingAddress'); |
||
| 110 | $billingAddressFields = $billingAddress->getFields($this->orderMember); |
||
| 111 | $addressFieldsBilling->merge($billingAddressFields); |
||
| 112 | |||
| 113 | $requiredFields = array_merge($requiredFields, $billingAddress->getRequiredFields()); |
||
| 114 | |||
| 115 | //HACK: move phone to member fields .. |
||
| 116 | if ($addressFieldsMember) { |
||
| 117 | if ($addressFieldsBilling) { |
||
| 118 | if ($phoneField = $addressFieldsBilling->dataFieldByName('Phone')) { |
||
| 119 | $addressFieldsBilling->removeByName('Phone'); |
||
| 120 | $addressFieldsMember->insertAfter('Email', $phoneField); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | //shipping address field |
||
| 127 | |||
| 128 | if (EcommerceConfig::get('OrderAddress', 'use_separate_shipping_address')) { |
||
| 129 | //add the important CHECKBOX |
||
| 130 | $useShippingAddressField = FieldList::create( |
||
| 131 | HeaderField::create( |
||
| 132 | 'HasShippingAddressHeader', |
||
| 133 | _t('OrderFormAddress.HAS_SHIPPING_ADDRESS_HEADER', 'Delivery Option'), |
||
| 134 | 3 |
||
| 135 | ) |
||
| 136 | ); |
||
| 137 | $useShippingAddress = $this->order ? $this->order->UseShippingAddress : 0; |
||
|
|
|||
| 138 | if ($shippingAddressFirst) { |
||
| 139 | $useShippingAddressField->push( |
||
| 140 | CheckboxField::create( |
||
| 141 | 'UseDifferentShippingAddress', |
||
| 142 | _t('OrderForm.USE_DIFFERENT_SHIPPING_ADDRESS', 'I need to enter a separate billing address'), |
||
| 143 | $useShippingAddress |
||
| 144 | ) |
||
| 145 | ); |
||
| 146 | $useShippingAddressField->push( |
||
| 147 | HiddenField::create('UseShippingAddress', 'UseShippingAddress', $useShippingAddress) |
||
| 148 | ); |
||
| 149 | } else { |
||
| 150 | $useShippingAddressField->push( |
||
| 151 | CheckboxField::create( |
||
| 152 | 'UseShippingAddress', |
||
| 153 | _t('OrderForm.USESHIPPINGADDRESS', 'Use separate shipping address'), |
||
| 154 | $useShippingAddress |
||
| 155 | ) |
||
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | $addressFieldsShipping = FieldList::create(); |
||
| 160 | |||
| 161 | //$addressFieldsShipping->merge($useShippingAddressField); |
||
| 162 | //now we can add the shipping fields |
||
| 163 | $shippingAddress = $this->order->CreateOrReturnExistingAddress('ShippingAddress'); |
||
| 164 | $shippingAddressFields = $shippingAddress->getFields($this->orderMember); |
||
| 165 | $requiredFields = array_merge($requiredFields, $shippingAddress->getRequiredFields()); |
||
| 166 | $addressFieldsShipping->merge($shippingAddressFields); |
||
| 167 | } |
||
| 168 | |||
| 169 | //create holder |
||
| 170 | $allLeftFields = CompositeField::create(); |
||
| 171 | $allLeftFields->addExtraClass('leftOrder'); |
||
| 172 | |||
| 173 | //member fields holder |
||
| 174 | $leftFieldsMember = CompositeField::create($addressFieldsMember); |
||
| 175 | $leftFieldsMember->addExtraClass('leftOrderMember'); |
||
| 176 | |||
| 177 | //creating shipping fields holder |
||
| 178 | $leftFieldsShipping = CompositeField::create($addressFieldsShipping); |
||
| 179 | $leftFieldsShipping->addExtraClass('leftOrderShipping'); |
||
| 180 | |||
| 181 | //creating billing fields holder |
||
| 182 | $leftFieldsBilling = CompositeField::create($addressFieldsBilling); |
||
| 183 | $leftFieldsBilling->addExtraClass('leftOrderBilling'); |
||
| 184 | |||
| 185 | //adding member fields ... |
||
| 186 | $allLeftFields->push($leftFieldsMember); |
||
| 187 | if ($useShippingAddressField) { |
||
| 188 | $leftFieldsShippingOptions = CompositeField::create($useShippingAddressField); |
||
| 189 | $leftFieldsShippingOptions->addExtraClass('leftOrderShippingOptions'); |
||
| 190 | $allLeftFields->push($leftFieldsShippingOptions); |
||
| 191 | } |
||
| 192 | if ($shippingAddressFirst) { |
||
| 193 | if ($addressFieldsShipping) { |
||
| 194 | $allLeftFields->push($leftFieldsShipping); |
||
| 195 | } |
||
| 196 | $allLeftFields->push($leftFieldsBilling); |
||
| 197 | } else { |
||
| 198 | $allLeftFields->push($leftFieldsBilling); |
||
| 199 | if ($addressFieldsShipping) { |
||
| 200 | $allLeftFields->push($leftFieldsShipping); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | // ________________ 2) Log in / vs Create Account fields - RIGHT-HAND-SIDE fields |
||
| 205 | |||
| 206 | $rightFields = CompositeField::create(); |
||
| 207 | $rightFields->addExtraClass('rightOrder'); |
||
| 208 | //to do: simplify |
||
| 209 | if (EcommerceConfig::get('EcommerceRole', 'allow_customers_to_setup_accounts')) { |
||
| 210 | if ($this->orderDoesNotHaveFullyOperationalMember()) { |
||
| 211 | //general header |
||
| 212 | if (!$this->loggedInMember) { |
||
| 213 | $rightFields->push( |
||
| 214 | //TODO: check EXACT link!!! |
||
| 215 | new LiteralField('MemberInfo', '<p class="message good">'._t('OrderForm.MEMBERINFO', 'If you already have an account then please').' <a href="Security/login/?BackURL=/'.urlencode(implode('/', $controller->getURLParams())).'">'._t('OrderForm.LOGIN', 'log in').'</a>.</p>') |
||
| 216 | ); |
||
| 217 | } |
||
| 218 | } else { |
||
| 219 | if ($this->loggedInMember) { |
||
| 220 | $rightFields->push( |
||
| 221 | new LiteralField( |
||
| 222 | 'LoginNote', |
||
| 223 | '<p class="message good">'._t('Account.LOGGEDIN', 'You are logged in as '). |
||
| 224 | Convert::raw2xml($this->loggedInMember->FirstName).' '. |
||
| 225 | Convert::raw2xml($this->loggedInMember->Surname). |
||
| 226 | ' ('.Convert::raw2xml($this->loggedInMember->Email).').'. |
||
| 227 | ' <a href="/Security/logout/">'. |
||
| 228 | _t('Account.LOGOUTNOW', 'Log out?'). |
||
| 229 | '</a>'. |
||
| 230 | '</p>' |
||
| 231 | ) |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | if ($this->orderMember->exists()) { |
||
| 236 | if ($this->loggedInMember) { |
||
| 237 | if ($this->loggedInMember->ID != $this->orderMember->ID) { |
||
| 238 | $rightFields->push( |
||
| 239 | new LiteralField( |
||
| 240 | 'OrderAddedTo', |
||
| 241 | '<p class="message good">'. |
||
| 242 | _t('Account.ORDERADDEDTO', 'Order will be added to '). |
||
| 243 | Convert::raw2xml($this->orderMember->FirstName).' '. |
||
| 244 | Convert::raw2xml($this->orderMember->Surname).' ('. |
||
| 245 | Convert::raw2xml($this->orderMember->Email). |
||
| 246 | ').</p>' |
||
| 247 | ) |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | // ________________ 5) Put all the fields in one FieldList |
||
| 255 | |||
| 256 | $fields = FieldList::create($rightFields, $allLeftFields); |
||
| 257 | |||
| 258 | // ________________ 6) Actions and required fields creation + Final Form construction |
||
| 259 | |||
| 260 | $nextButton = new FormAction('saveAddress', _t('OrderForm.NEXT', 'Next')); |
||
| 261 | $nextButton->addExtraClass('next'); |
||
| 262 | $actions = FieldList::create($nextButton); |
||
| 263 | |||
| 264 | $validator = OrderFormAddress_Validator::create($requiredFields); |
||
| 265 | |||
| 266 | parent::__construct($controller, $name, $fields, $actions, $validator); |
||
| 267 | $this->setAttribute('autocomplete', 'off'); |
||
| 268 | //extensions need to be set after __construct |
||
| 269 | //extension point |
||
| 270 | $this->extend('updateFields', $fields); |
||
| 271 | $this->setFields($fields); |
||
| 272 | $this->extend('updateActions', $actions); |
||
| 273 | $this->setActions($actions); |
||
| 274 | $this->extend('updateValidator', $validator); |
||
| 275 | $this->setValidator($validator); |
||
| 276 | |||
| 277 | //this needs to come after the extension calls |
||
| 278 | foreach ($validator->getRequired() as $requiredField) { |
||
| 279 | $field = $fields->dataFieldByName($requiredField); |
||
| 280 | if ($field) { |
||
| 281 | $field->addExtraClass('required'); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | // ________________ 7) Load saved data |
||
| 286 | |||
| 287 | //we do this first so that Billing and Shipping Address can override this... |
||
| 288 | if ($this->orderMember) { |
||
| 289 | $this->loadDataFrom($this->orderMember); |
||
| 290 | } |
||
| 291 | |||
| 292 | if ($this->order) { |
||
| 293 | $this->loadDataFrom($this->order); |
||
| 294 | if ($billingAddress) { |
||
| 295 | $this->loadDataFrom($billingAddress); |
||
| 296 | } |
||
| 297 | if (EcommerceConfig::get('OrderAddress', 'use_separate_shipping_address')) { |
||
| 298 | if ($shippingAddress) { |
||
| 299 | $this->loadDataFrom($shippingAddress); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | //allow updating via decoration |
||
| 305 | $oldData = Session::get("FormInfo.{$this->FormName()}.data"); |
||
| 306 | if ($oldData && (is_array($oldData) || is_object($oldData))) { |
||
| 307 | $this->loadDataFrom($oldData); |
||
| 308 | } |
||
| 309 | |||
| 310 | $this->extend('updateOrderFormAddress', $this); |
||
| 311 | } |
||
| 312 | |||
| 802 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.