public function insertBrick(HTML_QuickForm2_Container $form) { $fieldSet = $this->getConfig('hide_fieldset') ? $form : $form->addElement('fieldset', 'address', array('id' => 'row-address-0'))->setLabel($this->___('Address Information')); foreach ($this->getConfig('fields', array()) as $f => $required) { switch ($f) { case 'street': $street = $fieldSet->addText('street', array('size' => 30))->setLabel($this->___('Street')); if ($required) { $street->addRule('required', ___('Please enter %s', $this->___('Street'))); } break; case 'street2': $street = $fieldSet->addText('street2', array('size' => 30))->setLabel($this->___('Street (Second Line)')); if ($required) { $street->addRule('required', ___('Please enter %s', $this->___('Street (Second Line)'))); } break; case 'city': $city = $fieldSet->addText('city', array('size' => 30))->setLabel($this->___('City')); if ($required) { $city->addRule('required', ___('Please enter %s', $this->___('City'))); } break; case 'zip': $zip = $fieldSet->addText('zip')->setLabel($this->___('ZIP Code')); if ($required) { $zip->addRule('required', ___('Please enter %s', $this->___('ZIP Code'))); } break; case 'country': $country = $fieldSet->addSelect('country')->setLabel($this->___('Country'))->setId('f_country')->loadOptions(Am_Di::getInstance()->countryTable->getOptions(true)); if ($required) { $country->addRule('required', ___('Please enter %s', $this->___('Country'))); } break; case 'state': $group = $fieldSet->addGroup()->setLabel($this->___('State')); $stateSelect = $group->addSelect('state')->setId('f_state')->loadOptions($stateOptions = Am_Di::getInstance()->stateTable->getOptions(@$_REQUEST['country'], true)); $stateText = $group->addText('state')->setId('t_state'); $disableObj = $stateOptions ? $stateText : $stateSelect; $disableObj->setAttribute('disabled', 'disabled')->setAttribute('style', 'display: none'); if ($required) { $group->addRule('required', ___('Please enter %s', $this->___('State'))); } break; } } if ($this->getConfig('country_default')) { $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array('country' => $this->getConfig('country_default')))); } }
public function insertBrick(HTML_QuickForm2_Container $form) { $product_paysys = Am_Di::getInstance()->config->get('product_paysystem'); $base_name = 'product_id_' . $form->getId(); $name = self::$bricksAdded ? $base_name . '_' . self::$bricksAdded : $base_name; $products = $this->getProductsFiltered(); if (!$products) { if ($this->getConfig('require', self::REQUIRE_DEFAULT) == self::REQUIRE_NEVER) { return; } throw new Am_Exception_QuietError(___("There are no products available for purchase. Please come back later.")); } self::$bricksAdded++; if ($this->getConfig('require', self::REQUIRE_DEFAULT) != self::REQUIRE_NEVER) { self::$bricksWhichCanBeRequiredAdded++; } if ($this->getConfig('require', self::REQUIRE_DEFAULT) == self::REQUIRE_ALTERNATE) { self::$bricksAlternateAdded++; } $options = $shortOptions = $attrs = $dataOptions = array(); if ($this->getConfig('empty-option')) { $shortOptions[null] = $this->getConfig('empty-option-text', ___('Please select')); $options[null] = '<span class="am-product-title am-product-empty">' . $shortOptions[null] . '</span><span class="am-product-terms"></span><span class="am-product-desc"></span>'; $attrs[null] = array(); $dataOptions[null] = array('value' => null, 'label' => $options[null], 'selected' => false, 'variable_qty' => false, 'qty' => 1); } foreach ($this->getBillingPlans($products) as $plan) { $p = $plan->getProduct(); $pid = $p->product_id . '-' . $plan->plan_id; $options[$pid] = $this->renderProduct($p, $plan); $shortOptions[$pid] = $this->shortRender($p, $plan); $attrs[$pid] = array('data-first_price' => $plan->first_price, 'data-second_price' => $plan->second_price, 'data-paysys' => $product_paysys && $plan->paysys_id); $dataOptions[$pid] = array('label' => $options[$pid], 'value' => $pid, 'variable_qty' => $plan->variable_qty, 'qty' => $plan->qty, 'selected' => false); } $inputType = $this->getConfig('input-type', 'advradio'); if (count($options) == 1) { if ($this->getConfig('hide_if_one')) { $inputType = 'none'; } elseif ($inputType != 'checkbox') { $inputType = 'hidden'; } } $oel = null; //outer element switch ($inputType) { case 'none': list($pid, $label) = each($options); $oel = $el = $form->addHidden($name, $attrs[$pid]); $el->setValue($pid); $el->toggleFrozen(true); break; case 'checkbox': $data = array(); foreach ($this->getBillingPlans($products) as $plan) { $p = $plan->getProduct(); $data[$p->product_id . '-' . $plan->pk()] = array('data-first_price' => $plan->first_price, 'data-second_price' => $plan->second_price, 'data-paysys' => $product_paysys && $plan->paysys_id, 'options' => array('value' => $p->product_id . '-' . $plan->pk(), 'label' => $this->renderProduct($p, $plan), 'variable_qty' => $plan->variable_qty, 'qty' => $plan->qty, 'selected' => false)); } if ($this->getConfig('display-popup')) { $oel = $gr = $form->addGroup(); $gr->addStatic()->setContent(sprintf('<div id="%s-preview"></div>', $name)); $gr->addStatic()->setContent(sprintf('<div><a id="%s" class="local-link" href="javascript:;" data-title="%s">%s</a></div>', $name, $this->___('Membership Type'), $this->___('Add Membership'))); $gr->addStatic()->setContent(sprintf('<div id="%s-list" style="height:350px; overflow-y:scroll; display:none">', $name)); $el = $gr->addElement(new Am_Form_Element_SignupCheckboxGroup($name, $data, 'checkbox')); $gr->addStatic()->setContent('</div>'); $form->addScript()->setScript(<<<CUT \$(function(){ \$('#{$name}').click(function(){ \$('#{$name}-list').amPopup({ title : \$(this).data('title'), width : 450 }); return false; }) \$('#{$name}-list input[type=checkbox]').change(function(){ \$('#{$name}-preview').empty(); \$('#{$name}-list input[name^=product][type=checkbox]:checked').each(function(){ \$('#{$name}-preview'). append( \$('<div style="margin-bottom:0.2em" class="am-selected-product-row"></div>'). append('[<a href="javascript:;" class="local-link" onclick="\$(\\'#{$name}-list input[type=checkbox][value=' + \$(this).val() + ']\\').prop(\\'checked\\', \\'\\').change(); return false;">X</a>] '). append(\$(this).parent().html().replace(/<input.*>/, '')) ); }) }).change() }); CUT ); } else { $oel = $el = $form->addElement(new Am_Form_Element_SignupCheckboxGroup($name, $data, 'checkbox')); } break; case 'select': $oel = $el = $form->addSelect($name); foreach ($shortOptions as $pid => $label) { $el->addOption($label, $pid, empty($attrs[$pid]) ? null : $attrs[$pid]); } break; case 'hidden': case 'advradio': default: $data = array(); $first = 0; foreach ($options as $pid => $label) { $data[$pid] = $attrs[$pid]; $data[$pid]['options'] = $dataOptions[$pid]; if (!$first++ && Am_Di::getInstance()->request->isGet()) { // pre-check first option $data[$pid]['options']['selected'] = true; } } $oel = $el = $form->addElement(new Am_Form_Element_SignupCheckboxGroup($name, $data, $inputType == 'advradio' ? 'radio' : $inputType)); break; } $oel->setLabel($this->___('Membership Type')); if ($this->getConfig('no_label')) { $oel->setAttribute('class', 'no-label'); } switch ($this->getConfig('require', self::REQUIRE_DEFAULT)) { case self::REQUIRE_DEFAULT: if (self::$bricksWhichCanBeRequiredAdded == 1) { $el->addRule('required', $this->___('Please choose a membership type')); } break; case self::REQUIRE_ALWAYS: $el->addRule('required', $this->___('Please choose a membership type')); break; case self::REQUIRE_NEVER: break; case self::REQUIRE_ALTERNATE: if (self::$bricksAlternateAdded == 1) { $f = $form; while ($container = $f->getContainer()) { $f = $container; } $f->addRule('callback2', $this->___('Please choose a membership type'), array($this, 'formValidate')); } break; default: throw new Am_Exception_InternalError('Unknown require type [%s] for product brick', $this->getConfig('require', self::REQUIRE_DEFAULT)); } if (self::$bricksAdded == 1) { $script = <<<EOF jQuery(function(\$){ \$(":checkbox[name^='product_id'], select[name^='product_id'], :radio[name^='product_id']").change(function(){ var el = \$(this); var show = el.is(":checked") || el.is(":selected"); el.closest("label").find(".am-product-qty") .toggle(show).prop("disabled", !show); if (this.type == 'radio') { // in case of radio elements we must disable not-selected el.closest("form") .find("label:has(input[name='"+this.name+"']:not(:checked)) .am-product-qty") .hide().prop("disabled", true); } }); }); EOF; $form->addScript()->setScript($script); } if (($d = $this->getConfig('default')) && $inputType != 'none' && $inputType != 'hidden') { $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array($name => $inputType == 'checkbox' || $inputType == 'advradio' ? array($d) : $d))); } }