Esempio n. 1
0
		/**
		* Validates POSTed address field + shipping method info and, if valid, populates the provided address object
		*
		* This was originally inside editOrderSaveSingleShippingAction but it's moved for use with both single and split-shipping addresses. -ge
		*
		* @param ISC_QUOTE_ADDRESS_SHIPPING $address address object to populate with validated details
		* @param array $errors by-reference array of errors
		* @param ISC_QUOTE_ADDRESS_SHIPPING $shippingMethodAddress an optional second address instance which carries cached shipping method information (this is a bit of a hack, but it's used by the split-shipping quotation functionality where a second temporary address is used to calculate shipping before actually saving in a different request)
		* @return bool true on success, otherwise false (with error information stored in $errors)
		*/
		protected function validateAndPopulatePostedShippingAddress(ISC_QUOTE_ADDRESS_SHIPPING $address, &$errors = null, ISC_QUOTE_ADDRESS_SHIPPING $shippingMethodAddress = null)
		{
			// this is tied to $_POST because of form fields

			if ($shippingMethodAddress === null) {
				$shippingMethodAddress = $address;
			}

			$shippingMethod = 'builtin:none';
			if (isset($_POST['shippingQuoteList'])) {
				$shippingMethod = $_POST['shippingQuoteList'];
			}

			$errors = array();

			$shippingCustomFields = array();
			$saveAddress = Interspire_Request::post('saveShippingAddress');

			$shippingFormFields = $GLOBALS['ISC_CLASS_FORM']->getFormFields(FORMFIELDS_FORM_SHIPPING, true);
			foreach($shippingFormFields as $formFieldId => $formField) {
				// All fields are optional on the order management page, so only validate
				// when there is a value.
				$error = '';
				if($formField->getValue() && !$formField->runValidation($error)) {
					$errors[] = $error;
				}

				if(!$formField->record['formfieldprivateid']) {
					$shippingCustomFields[$formFieldId] = $formField->getValue();
				}
			}

			if (!empty($errors)) {
				return false;
			}

			require ISC_BASE_PATH . '/lib/addressvalidation.php';
			$shippingAddressArray = convertAddressFieldsToArray($shippingFormFields);

			// Actually set the shipping address on the quote
			/** @var ISC_QUOTE_ADDRESS_SHIPPING */
			$address->setAddressByArray($shippingAddressArray)
				->setCustomFields($shippingCustomFields)
				->setSaveAddress($saveAddress);

			if ($shippingMethod == 'builtin:none') {
				$address->setShippingMethod(0, GetLang('xNone'), '', true);
				$address->setHandlingCost(0);
			} else if ($shippingMethod == 'builtin:custom') {
				if (empty($_POST['customShippingDescription'])) {
					$errors[] = GetLang('ErrorEnterShippingMethodName');
				} else {
					if (empty($_POST['customShippingPrice'])) {
						$customShippingPrice = 0;
					} else {
						$customShippingPrice = $_POST['customShippingPrice'];
					}

					$address->setShippingMethod(
						DefaultPriceFormat($customShippingPrice),
						$_POST['customShippingDescription'],
						'custom',
						true
					);

					$address->setHandlingCost(0);
				}
			} else if ($shippingMethod == 'builtin:current') {
				// restore previous shipping method
				$current = Interspire_Request::post('currentShipping', array());
				if (!empty($current)) {
					$address->setShippingMethod(
						$current['price'],
						$current['description'],
						$current['module'],
						(bool)$current['isCustom']
					);
				}
			} else {
				$method = $shippingMethodAddress->getCachedShippingMethod($shippingMethod);
				if (!$method) {
					$errors[] = GetLang('OldQuoteNotFound');
				} else {
					$address->setShippingMethod(
						$method['price'],
						$method['description'],
						$method['module'],
						true
					);

					$address->setHandlingCost($method['handling']);
				}
			}

			if(!empty($errors)) {
				return false;
			}

			return true;
		}