/** * Verifies the fields in a submitted form. Uses the table's check() method. * Will often be overridden. Is expected to be called via Ajax * * @return unknown_type */ function validate() { $input = JFactory::getApplication()->input; Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = new CitruscartHelperBase(); $response = array(); $response['msg'] = ''; $response['error'] = ''; // get elements from post $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', $input->getString('elements'))); // validate it using table's ->check() method if (empty($elements)) { // if it fails check, return message $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_COULD_NOT_PROCESS_FORM')); echo json_encode($response); return; } if (!Citruscart::getInstance()->get('shop_enabled', '1')) { $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_SHOP_DISABLED')); $response['error'] = '1'; echo json_encode($response); return false; } // convert elements to array that can be binded $values = $helper->elementsToArray($elements); $product_id = !empty($values['product_id']) ? (int) $values['product_id'] : $input->getInt('product_id'); $product_qty = !empty($values['product_qty']) ? (int) $values['product_qty'] : '1'; $attributes = array(); foreach ($values as $key => $value) { if (substr($key, 0, 10) == 'attribute_') { $attributes[] = $value; if (!(int) $value) { $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_ALL_PRODUCT_ATTRIBUTES_REQUIRED')); $response['error'] = '1'; echo json_encode($response); return false; } } } sort($attributes); $attributes_csv = implode(',', $attributes); // Integrity checks on quantity being added if ($product_qty < 0) { $product_qty = '1'; } // using a helper file to determine the product's information related to inventory $availableQuantity = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) { $response['msg'] = $helper->generateMessage(JText::sprintf("COM_CITRUSCART_NOT_AVAILABLE_QUANTITY", $availableQuantity->product_name, $product_qty)); $response['error'] = '1'; echo json_encode($response); return false; } $product = JTable::getInstance('Products', 'CitruscartTable'); $product->load(array('product_id' => $product_id)); // if product notforsale, fail if ($product->product_notforsale) { $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_PRODUCT_NOT_FOR_SALE')); $response['error'] = '1'; echo json_encode($response); return false; } $user = JFactory::getUser(); $keynames = array(); $keynames['user_id'] = $user->id; if (empty($user->id)) { $session = JFactory::getSession(); $keynames['session_id'] = $session->getId(); } $keynames['product_id'] = $product_id; $cartitem = JTable::getInstance('Carts', 'CitruscartTable'); $cartitem->load($keynames); if ($product->quantity_restriction) { $error = false; $min = $product->quantity_min; $max = $product->quantity_max; if ($max) { $remaining = $max - $cartitem->product_qty; if ($product_qty > $remaining) { $error = true; $msg = $helper->generateMessage(JText::_('COM_CITRUSCART_YOU_HAVE_REACHED_THE_MAXIMUM_QUANTITY_YOU_CAN_ORDER_ANOTHER') . " " . $remaining); } } if ($min) { if ($product_qty < $min) { $error = true; $msg = $helper->generateMessage(JText::_('COM_CITRUSCART_YOU_HAVE_NOT_REACHED_THE_MIMINUM_QUANTITY_YOU_HAVE_TO_ORDER_AT_LEAST') . " " . $min); } } $remainder = 0; if (!empty($product->quantity_step)) { $remainder = $product_qty % $product->quantity_step; } if (!empty($product->quantity_step) && !empty($remainder)) { $error = true; $msg = $helper->generateMessage(JText::sprintf('COM_CITRUSCART_QUANTITY_MUST_BE_IN_INCREMENTS_OF_X_FOR_PRODUCT_Y', $product->quantity_step, $product->product_name)); } if ($error) { $response['msg'] = $msg; $response['error'] = '1'; echo json_encode($response); return false; } } // create cart object out of item properties $item = new JObject(); $item->user_id = JFactory::getUser()->id; $item->product_id = (int) $product_id; $item->product_qty = (int) $product_qty; $item->product_attributes = $attributes_csv; $item->vendor_id = '0'; // vendors only in enterprise version // no matter what, fire this validation plugin event for plugins that extend the checkout workflow $results = array(); $dispatcher = JDispatcher::getInstance(); $results = JFactory::getApplication()->triggerEvent("onValidateAddToCart", array($item, $values)); for ($i = 0; $i < count($results); $i++) { $result = $results[$i]; if (!empty($result->error)) { Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = CitruscartHelperBase::getInstance(); $response['msg'] = $helper->generateMessage($result->message); $response['error'] = '1'; echo json_encode($response); return; } else { // if here, all is OK $response['error'] = '0'; } } echo json_encode($response); return; }
function updatePaymentOptions() { $input = JFactory::getApplication()->input; $response = array(); $response['msg'] = ''; $response['error'] = ''; $error_message = ''; Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = CitruscartHelperBase::getInstance(); // get elements from post $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', $input->getString('elements'))); // Test if elements are empty // Return proper message to user if (empty($elements)) { // do form validation // if it fails check, return message $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_ERROR_WHILE_VALIDATING_THE_PARAMETERS')); echo json_encode($response); return; } // convert elements to array that can be binded Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = new CitruscartHelperBase(); $submitted_values = $helper->elementsToArray($elements); // Use AJAX to show plugins that are available JLoader::import('com_citruscart.library.json', JPATH_ADMINISTRATOR . '/components'); $this->setAddresses($submitted_values, false, true); if (!isset($submitted_values['billing_address_id'])) { $submitted_values['billing_address_id'] = ''; } if (!$this->validateAddress($submitted_values, $this->billing_input_prefix, $submitted_values['billing_address_id'], true)) { $error_message = $helper->generateMessage(JText::_('COM_CITRUSCART_BILLING_ADDRESS_ERROR') . " :: " . $this->getError()); $response['error'] = '1'; $response['msg'] = $error_message; echo json_encode($response); return; } $model = $this->getModel('Checkout', 'CitruscartModel'); $view = $this->getView('checkout', 'html'); $view->set('_controller', 'checkout'); $view->set('_view', 'checkout'); $view->set('_doTask', true); $view->set('hidemenu', true); $view->setModel($model, true); $view->setLayout('payment_options'); $payment_plugins = $this->getPaymentOptions($this->_order); $view->assign('payment_plugins', $payment_plugins); $guest = false; if ($this->defines->get('guest_checkout_enabled')) { if (!$this->onepage_checkout) { $guest = $this->user->id ? 0 : 1; } else { $guest = $input->get('guest', '0'); } $guest = $guest ? true : false; } $this->populateOrder($guest); $this->addCouponCodes($submitted_values); $this->_order->calculateTotals(); if (count($payment_plugins) == 1) { $payment_plugins[0]->checked = true; $dispatcher = JDispatcher::getInstance(); $results = JFactory::getApplication()->triggerEvent("onGetPaymentForm", array($payment_plugins[0]->element, '')); $text = ''; for ($i = 0; $i < count($results); $i++) { $text .= $results[$i]; } $view->assign('payment_form_div', $text); } else { if (isset($submitted_values['_checked']['payment_plugin'])) { foreach ($payment_plugins as $plugin) { if ($plugin->element == $submitted_values['_checked']['payment_plugin']) { $plugin->checked = true; } } } } $showPayment = true; if ((double) $this->_order->order_total == (double) '0.00') { $showPayment = false; } $view->assign('showPayment', $showPayment); ob_start(); $view->display(); $html = ob_get_contents(); ob_end_clean(); // set response array $response = array(); $response['msg'] = $error_message . $html; // encode and echo (need to echo to send back to browser) echo json_encode($response); return; }
function validateStep3($values) { Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = new CitruscartHelperBase(); $response = array(); $response['msg'] = ''; $response['error'] = ''; $msg = array(); $config = Citruscart::getInstance(); //check if we have billing address id if (empty($values['billing_input_address_id'])) { $responseBilling = $this->validateAddress($values); if (count($responseBilling)) { $msg = array_merge($msg, $responseBilling); $response['error'] = '1'; } } $session = JFactory::getSession(); $subtask = $session->get('subtask', 'shipping', 'citruscart_pos'); switch ($subtask) { case 'shipping': //check if we have billing address id if (empty($values['shipping_input_address_id'])) { if (empty($values['sameasbilling'])) { $responseShipping = $this->validateAddress($values, 'shipping'); if (count($responseShipping)) { $msg = array_merge($msg, $responseShipping); $response['error'] = '1'; } } } else { if (empty($values['shipping_name'])) { $response['error'] = '1'; $msg[] = JText::_('COM_CITRUSCART_PLEASE_SELECT_SHIPPING_METHOD'); } } break; case 'payment': default: if (!empty($values['billing_input_address_id'])) { if (empty($values['payment_plugin'])) { $response['error'] = '1'; $msg[] = JText::_('COM_CITRUSCART_PLEASE_SELECT_PAYMENT_METHOD'); } } break; } $response['msg'] = $helper->generateMessage("<li>" . implode("</li><li>", $msg) . "</li>", false); return $response; }