addElement( 'grid', 'product', array( 'label' => 'Product Descriptions', 'columns' => array( 'specie' => array( 'type' => 'select', 'title' => 'Species', 'multiOptions' => $this->dataObj->getProductSpecies() ), 'class' => array( 'type' => 'select', 'title' => 'Class', 'multiOptions' => $this->dataObj->getDProductClass4Select() ), 'length' => array( 'type' => 'select', 'title' => 'Length', 'multiOptions' => $this->dataObj->getDProductLength4Select() ), 'preservative' => array( 'type' => 'select', 'title' => 'Preservative', 'multiOptions' => $this->dataObj->getDProductPreservative4Select() ), 'quantity' => array( 'type' => 'text', 'title' => 'Quantity', 'validators' => array(array('Int', true, array())) ), '_options' => array('type' => 'options', 'title' => '') ), 'required' => false ) ); $form->addFormRule(array($this, 'validateForm')); $form->setAjaxValidation(false); $this->doc->addScript('js/app/calculator.js'); $this->doc->addInitFunction('App_Calculator.init', array()); return $this; } public function validateForm(&$data) { $errors = array(); foreach ($data['product'] as $value) { if (empty($value['quantity'])) { $errors['product'][] = 'Quantity is required and can\'t be empty'; } else if (!is_numeric($value['quantity'])) { $errors['product'][] = $value['quantity'] . ' does not appear to be an integer'; } } return empty($errors) ? true : $errors; } protected function _bindFormButtons(Qs_Form $form) { $form->addElement('submit', 'btnSubmit', array( 'label' => 'Calculate', 'attribs' => array('class' => 'btn'), 'decorators' => array('ViewHelper') ) ); $form->btnSubmit->getDecorator('ViewHelper')->setAdditionalHtmlAfterElement('  '); $decorators = array('FormElements'); $decorators[] = array('decorator' => 'HtmlTag', 'options' => array('tag' => 'div')); $decorators[] = 'Fieldset'; $decorators[] = 'DtDdWrapper'; $form->addDisplayGroup(array('btnSubmit'), 'submitGroup', array('decorators' => $decorators)); return $this; } protected function _doInsert() { $form = $this->_getNewForm(); $item = array(); if ($form->validate()) { $data = $form->getValues(); $request = array(); $request['product'] = $data['product']; $product = array(); $species = $this->dataObj->getProduct4Select(array('id', 'specie'), null, '`type` = "' . App_Product_AbstractObj::UTILITY_POLE . '"'); $productsData = array(); foreach ($data['product'] as $value) { $key = $species[$value['specie']] . $value['class'] . $value['length']; if (key_exists($key, $product)) { $product[$key] += $value['quantity']; } else { $product[$key] = $value['quantity']; } $request['species'][$value['specie']] = $value['specie']; $request['preservative'][$value['preservative']] = $value['preservative']; $productsData[$key] = array(); $productsData[$key] = $value; } $productParams = $this->dataObj->getProductParams(array_keys($product)); foreach ($productParams as $productKey => $productValue) { $productSum = array( 'maxWeight' => 0, 'maxMeter' => 0, 'maxFoot' => 0, 'weight' => 0, 'meter' => 0, 'foot' => 0 ); $productSum['weight'] = $productValue['weight'] * $product[$productKey]; $productSum['meter'] = $productValue['meter'] * $product[$productKey]; $productSum['foot'] = $productValue['foot'] * $product[$productKey]; $productSum['maxWeight'] = max(Qs_Array::fetchCol($productParams, 'weight')); $productSum['maxMeter'] = max(Qs_Array::fetchCol($productParams, 'meter')); $productSum['maxFoot'] = max(Qs_Array::fetchCol($productParams, 'foot')); if (isset($productsData[$productKey])) { $productsData[$productKey]['productSum'] = $productSum; } } $item['truckParams'] = $this->dataObj->prepareTruckResult($productsData); $item['tpl'] = $this->getTemplate('view.tpl'); $item['requestAlias'] = Qs_SiteMap::find(null, array('type' => 'Form_PoleRequest_'), null, 'fullAlias'); $session = new Qs_Session_Namespace($item['requestAlias']); $session->calculatorData = $request; } $this->_renderMainForm($form); if (isset($item['tpl'])) { $this->doc->addItem($item); } return $this; } protected function _doGetProducts() { return $this->_doNew(); } protected function _doGetProductsAjax() { $productId = Qs_Request::getGetValue('productId'); $class = Qs_Request::getGetValue('class'); $rowNum = Qs_Request::getGetValue('rowNum'); return $this->_displayJson( json_encode(array_merge(array('rowNum' => $rowNum) + $this->dataObj->getProducts($productId, $class))) ); } }