/**
  * Generate the checkout step
  * @return  string
  */
 public function generate()
 {
     $this->objForm = new Form($this->objModule->getFormId(), 'POST', function ($haste) {
         return \Input::post('FORM_SUBMIT') === $haste->getFormId();
     });
     if ($this->objModule->iso_order_conditions) {
         $objFormConfig = \FormModel::findByPk($this->objModule->iso_order_conditions);
         if (null === $objFormConfig) {
             throw new \InvalidArgumentException('Order condition form "' . $this->objModule->iso_order_conditions . '" not found.');
         }
         $this->objForm->setTableless($objFormConfig->tableless);
         $this->objForm->addFieldsFromFormGenerator($this->objModule->iso_order_conditions, function ($strName, &$arrDca) {
             $arrDca['value'] = $_SESSION['FORM_DATA'][$strName] ?: $arrDca['value'];
             return true;
         });
     }
     if (!empty($GLOBALS['ISO_HOOKS']['orderConditions']) && is_array($GLOBALS['ISO_HOOKS']['orderConditions'])) {
         foreach ($GLOBALS['ISO_HOOKS']['orderConditions'] as $callback) {
             \System::importStatic($callback[0])->{$callback[1]}($this->objForm, $this->objModule);
         }
     }
     if (!$this->objForm->hasFields()) {
         $this->blnError = false;
         return '';
     }
     // Change enctype if there are uploads
     if ($this->objForm->hasUploads()) {
         $this->objModule->Template->enctype = 'multipart/form-data';
     }
     if ($this->objForm->isSubmitted()) {
         $this->blnError = !$this->objForm->validate();
         $_SESSION['FORM_DATA'] = is_array($_SESSION['FORM_DATA']) ? $_SESSION['FORM_DATA'] : array();
         foreach (array_keys($this->objForm->getFormFields()) as $strField) {
             if ($this->objForm->getWidget($strField) instanceof \uploadable) {
                 $arrFile = $_SESSION['FILES'][$strField];
                 $varValue = str_replace(TL_ROOT . '/', '', dirname($arrFile['tmp_name'])) . '/' . rawurlencode($arrFile['name']);
             } else {
                 $varValue = $this->objForm->fetch($strField);
             }
             $_SESSION['FORM_DATA'][$strField] = $varValue;
         }
     } else {
         $blnError = false;
         foreach (array_keys($this->objForm->getFormFields()) as $strField) {
             // Clone widget because otherwise we add errors to the original widget instance
             $objClone = clone $this->objForm->getWidget($strField);
             $objClone->validate();
             if ($objClone->hasErrors()) {
                 $blnError = true;
                 break;
             }
         }
         $this->blnError = $blnError;
     }
     $objTemplate = new \Isotope\Template('iso_checkout_order_conditions');
     $this->objForm->addToTemplate($objTemplate);
     return $objTemplate->parse();
 }
Example #2
0
 public function testBindModelDefaultValues()
 {
     $objInstance = new Form('someid', 'POST', function () {
         return false;
     });
     $objModel = \PageModel::findByPk(13);
     $objInstance->bindModel($objModel);
     $objInstance->addFormField('id', array('inputType' => 'text'))->addFormField('pageTitle', array('inputType' => 'text'))->addFormField('jumpTo', array('inputType' => 'text'));
     $objInstance->createWidgets();
     $this->assertEquals(13, $objInstance->getWidget('id')->value);
     $this->assertEquals('My page', $objInstance->getWidget('pageTitle')->value);
     $this->assertEquals(11, $objInstance->getWidget('jumpTo')->value);
 }
Example #3
0
 /**
  * Validates a widget
  *
  * @param mixed   $varValue Widget value
  * @param \Widget $objWidget
  * @param Form    $objForm
  *
  * @return mixed Widget value
  *
  * @throws \Exception If value is not provided
  */
 public function validate($varValue, $objWidget, $objForm)
 {
     foreach ($this->arrMatches as $strFieldName => $arrValues) {
         $objTarget = $objForm->getWidget($strFieldName);
         // @todo if the target widget isn't validated, we have no value here
         // can we do anything about it?
         $varTargetValue = $objTarget->value;
         if (trim($varValue) == '' && $this->arrMatches[$objTarget->name] && in_array($varTargetValue, $this->arrMatches[$objTarget->name])) {
             throw new \Exception($GLOBALS['TL_LANG']['MSC']['mandatory']);
         }
     }
     return $varValue;
 }
 /**
  * Bind the form widget values to an MetaModels Item
  *
  * @param Form $hasteForm
  * @throws \Exception When an MetaModels Item is missing
  */
 public function bindFormToItem(Form $hasteForm)
 {
     if (is_null($this->mmItem)) {
         throw new \Exception('Trying to bind fields on non existing mmItem - aborting');
         return false;
     }
     $this->fields->reset();
     while ($this->fields->next()) {
         $field = $this->fields->current();
         $widget = $hasteForm->getWidget($field->getColName());
         if (!is_a($field, 'Teamsisu\\MetaModelsFrontendInput\\DataContainer\\Field\\FieldsetField')) {
             $saveHandler = $field->getSaveHandler();
             $saveHandler->setItem($this->mmItem);
             $value = $saveHandler->parseWidget($widget);
             if ($value) {
                 $saveHandler->setValue($value);
             }
         }
     }
 }
Example #5
0
 /**
  * Edit an address record. Based on the PersonalData core module
  * @param integer
  * @return void
  */
 protected function edit($intAddressId = 0)
 {
     $table = Address::getTable();
     \System::loadLanguageFile(\MemberModel::getTable());
     $this->Template = new \Isotope\Template($this->memberTpl);
     $this->Template->hasError = false;
     $this->Template->slabel = specialchars($GLOBALS['TL_LANG']['MSC']['saveData']);
     if ($intAddressId === 0) {
         $objAddress = Address::createForMember(\FrontendUser::getInstance()->id);
     } else {
         $objAddress = Address::findOneForMember($intAddressId, \FrontendUser::getInstance()->id);
     }
     if (null === $objAddress) {
         global $objPage;
         \Controller::redirect(\Controller::generateFrontendUrl($objPage->row()));
     }
     $objForm = new Form($table . '_' . $this->id, 'POST', function ($objForm) {
         return \Input::post('FORM_SUBMIT') === $objForm->getFormId();
     }, (bool) $this->tableless);
     $objForm->bindModel($objAddress);
     // Add form fields and modify for the address book
     $arrFields = $this->arrFields;
     $objForm->addFieldsFromDca($table, function ($strName, &$arrDca) use($arrFields) {
         if (!in_array($strName, $arrFields) || !$arrDca['eval']['feEditable']) {
             return false;
         }
         // Map checkboxWizard to regular checkbox widget
         if ($arrDca['inputType'] == 'checkboxWizard') {
             $arrDca['inputType'] = 'checkbox';
         }
         // Special field "country"
         if ($strName == 'country') {
             $arrCountries = array_merge(Isotope::getConfig()->getBillingCountries(), Isotope::getConfig()->getShippingCountries());
             $arrDca['reference'] = $arrDca['options'];
             $arrDca['options'] = array_values(array_intersect(array_keys($arrDca['options']), $arrCountries));
             $arrDca['default'] = Isotope::getConfig()->billing_country;
         }
         return true;
     });
     if ($objForm->isSubmitted()) {
         if ($objForm->validate()) {
             $objAddress->save();
             // Call onsubmit_callback
             if (is_array($GLOBALS['TL_DCA'][$table]['config']['onsubmit_callback'])) {
                 foreach ($GLOBALS['TL_DCA'][$table]['config']['onsubmit_callback'] as $callback) {
                     $objCallback = \System::importStatic($callback[0]);
                     $objCallback->{$callback}[1]($objAddress);
                 }
             }
             global $objPage;
             \Controller::redirect(\Controller::generateFrontendUrl($objPage->row()));
         } else {
             $this->Template->hasError = true;
         }
     }
     $objForm->addToTemplate($this->Template);
     // Add groups
     $arrGroups = array();
     foreach ($objForm->getFormFields() as $strName => $arrConfig) {
         if ($arrConfig['feGroup'] != '') {
             $arrGroups[$arrConfig['feGroup']][$strName] = $objForm->getWidget($strName)->parse();
         }
     }
     foreach ($arrGroups as $k => $v) {
         $this->Template->{$k} = $v;
     }
     $this->Template->addressDetails = $GLOBALS['TL_LANG'][$table]['addressDetails'];
     $this->Template->contactDetails = $GLOBALS['TL_LANG'][$table]['contactDetails'];
     $this->Template->personalData = $GLOBALS['TL_LANG'][$table]['personalData'];
     $this->Template->loginDetails = $GLOBALS['TL_LANG'][$table]['loginDetails'];
 }
 /**
  * Generate the module
  */
 protected function compile()
 {
     $this->Template->hasError = false;
     $this->Template->slabel = specialchars($GLOBALS['TL_LANG']['MSC']['saveAddressButton']);
     $table = Address::getTable();
     \System::loadLanguageFile($table);
     \Controller::loadDataContainer($table);
     // Call onload_callback (e.g. to check permissions)
     if (is_array($GLOBALS['TL_DCA'][$table]['config']['onload_callback'])) {
         foreach ($GLOBALS['TL_DCA'][$table]['config']['onload_callback'] as $callback) {
             $objCallback = \System::importStatic($callback[0]);
             $objCallback->{$callback}[1]();
         }
     }
     $arrFields = $this->iso_addressFields;
     $useBilling = in_array('billing', $this->iso_address);
     $objAddress = $this->getDefaultAddress($useBilling);
     $objForm = new Form('iso_cart_address_' . $this->id, 'POST', function ($objHaste) {
         /** @type Form $objHaste */
         return \Input::post('FORM_SUBMIT') === $objHaste->getFormId();
     }, (bool) $this->tableless);
     $objForm->bindModel($objAddress);
     // Add form fields
     $objForm->addFieldsFromDca($table, function ($strName, &$arrDca) use($arrFields, $useBilling) {
         if (!in_array($strName, $arrFields) || !$arrDca['eval']['feEditable'] || $arrDca['eval']['membersOnly'] && FE_USER_LOGGED_IN !== true) {
             return false;
         }
         // Special field "country"
         if ($strName == 'country') {
             if ($useBilling) {
                 $arrCountries = Isotope::getConfig()->getBillingCountries();
                 $arrDca['default'] = Isotope::getConfig()->billing_country;
             } else {
                 $arrCountries = Isotope::getConfig()->getShippingCountries();
                 $arrDca['default'] = Isotope::getConfig()->shipping_country;
             }
             $arrDca['reference'] = $arrDca['options'];
             $arrDca['options'] = array_values(array_intersect(array_keys($arrDca['options']), $arrCountries));
         }
         return true;
     });
     $objCart = Isotope::getCart();
     // Save the data
     if ($objForm->validate()) {
         if (!$objCart->id) {
             $objCart->save();
         }
         $objAddress->tstamp = time();
         $objAddress->pid = $objCart->id;
         $objAddress->save();
         // Call onsubmit_callback
         if (is_array($GLOBALS['TL_DCA'][$table]['config']['onsubmit_callback'])) {
             foreach ($GLOBALS['TL_DCA'][$table]['config']['onsubmit_callback'] as $callback) {
                 $objCallback = \System::importStatic($callback[0]);
                 $objCallback->{$callback}[1]($objAddress);
             }
         }
         // Set the billing address
         if ($useBilling) {
             $objCart->setBillingAddress($objAddress);
         }
         // Set the shipping address
         if (in_array('shipping', $this->iso_address)) {
             $objCart->setShippingAddress($objAddress);
         }
         $this->jumpToOrReload($this->jumpTo);
     }
     $objForm->addToTemplate($this->Template);
     // Predefine the group order (other groups will be appended automatically)
     $arrGroups = array();
     $categories = array('personal' => array(), 'address' => array(), 'contact' => array(), 'login' => array(), 'profile' => array());
     foreach ($objForm->getFormFields() as $strName => $arrConfig) {
         if ($arrConfig['feGroup'] != '') {
             $arrGroups[$arrConfig['feGroup']][$strName] = $objForm->getWidget($strName)->parse();
         }
     }
     foreach ($arrGroups as $k => $v) {
         $this->Template->{$k} = $v;
         // backwards compatibility
         $key = $k . ($k == 'personal' ? 'Data' : 'Details');
         $categories[$GLOBALS['TL_LANG']['tl_member'][$key]] = $v;
     }
     $this->Template->categories = $categories;
     $this->Template->addressDetails = $GLOBALS['TL_LANG'][$table]['addressDetails'];
     $this->Template->contactDetails = $GLOBALS['TL_LANG'][$table]['contactDetails'];
     $this->Template->personalData = $GLOBALS['TL_LANG'][$table]['personalData'];
     $this->Template->loginDetails = $GLOBALS['TL_LANG'][$table]['loginDetails'];
 }