/** * Get the custom fields for the given entity * @param string $entity * @param int $id * @param bool $cache_values If the values should be cached in RAV helper */ function getCustomFields($entity, $id, $cache_values = true, $editable_by = '') { Tienda::load('TiendaModelEavAttributes', 'models.eavattributes'); Tienda::load('TiendaHelperEav', 'helpers.eav'); $eavs = TiendaHelperEav::getAttributes($entity, $id, false, $editable_by); $fields = array(); foreach (@$eavs as $eav) { $key = $eav->eavattribute_alias; $value = TiendaHelperEav::getAttributeValue($eav, $entity, $id, false, $cache_values); $fields[] = array('attribute' => $eav, 'value' => $value); } return $fields; }
/** * This method removes all eav values from an entity with a specified ID * * @params $entity_type Type of the entity * @params $entity-id Entity ID */ public static function deleteEavValuesFromEntity($entity_type, $entity_id, $entity_type_mirror = null, $entity_id_mirror = null) { if (!$entity_type_mirror) { $entity_type_mirror = $entity_type; } if (!$entity_id_mirror) { $entity_id_mirror = $entity_id; } JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $tbl_eav = JTable::getInstance('Eavvalues', 'TiendaTable'); $eavs = TiendaHelperEav::getAttributes($entity_type, $entity_id); // get list of EAV fields for ($i = 0, $c = count($eavs); $i < $c; $i++) { $tbl_eav->setType($eavs[$i]->eavattribute_type); $tbl_eav->load(array('eaventity_type' => $entity_type_mirror, 'eaventity_id' => $entity_id_mirror, 'eavattribute_id' => $eavs[$i]->eavattribute_id)); $tbl_eav->delete(); } }
/** * Method to item to cart */ function addToCart() { $post = JRequest::get('post'); $files = JRequest::get('files'); $product_id = $post['product_id']; // get attributes $attributes = array(); foreach ($post as $key => $value) { if (substr($key, 0, 10) == 'attribute_') { $attributes[] = $value; } } sort($attributes); $attributes_csv = implode(',', $attributes); $product_qty = $post['quantity']; // Integrity checks on quantity being added if ($product_qty < 0) { $product_qty = '1'; } // check product if available $availableQuantity = Tienda::getClass('TiendaHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $attributes_csv); if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) { $messagetype = 'notice'; $message = JText::_(JText::sprintf('COM_TIENDA_NOT_AVAILABLE_QUANTITY_NOTICE', $availableQuantity->product_name, $product_qty)); $this->setRedirect('index.php?option=com_tienda&view=pos&task=addproducts&tmpl=component', $message, $messagetype); return; } // chec if product for sale $product = JTable::getInstance('Products', 'TiendaTable'); $product->load(array('product_id' => $product_id)); // if product notforsale, fail if ($product->product_notforsale) { $messagetype = 'notice'; $message = JText::_('COM_TIENDA_PRODUCT_NOT_FOR_SALE_NOTICE'); $this->setRedirect('index.php?option=com_tienda&view=pos&task=addproducts&tmpl=component', $message, $messagetype); return; } $session = JFactory::getSession(); $cart_id = $session->get('user_id', '', 'tienda_pos'); // userid from session $id_type = "user_id"; // create cart object out of item properties $item = new JObject(); $item->user_id = $cart_id; //TODO: need to determine what user $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 $canAddToCart = Tienda::getClass('TiendaHelperCarts', 'helpers.carts')->canAddItem($item, $cart_id, $id_type); // onAfterCreateItemForAddToCart: plugin can add values to the item before it is being validated /added // once the extra field(s) have been set, they will get automatically saved $dispatcher = JDispatcher::getInstance(); $results = $dispatcher->trigger("onAfterCreateItemForAddToCart", array($item, $post, $files)); foreach ($results as $result) { foreach ($result as $key => $value) { $item->set($key, $value); } } // no matter what, fire this validation plugin event for plugins that extend the checkout workflow $results = array(); $dispatcher = JDispatcher::getInstance(); $results = $dispatcher->trigger("onBeforeAddToCart", array($item, $post)); for ($i = 0; $i < count($results); $i++) { $result = $results[$i]; if (!empty($result->error)) { $messagetype = 'notice'; $message = JText::_(JText::sprintf('COM_TIENDA_NOT_AVAILABLE_QUANTITY_NOTICE', $availableQuantity->product_name, $product_qty)); $this->setRedirect('index.php?option=com_tienda&view=pos&task=addproducts&tmpl=component', $result->message, 'notice'); return; } } JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $table = JTable::getInstance('Carts', 'TiendaTable'); // first, determine if this product+attribute+vendor(+additonal_keys) exists in the cart // if so, update quantity // otherwise, add as new item // return the cart object with cart_id (to be used by plugins, etc) $keynames = array(); $keynames['user_id'] = $item->user_id; if (empty($item->user_id)) { $keynames['session_id'] = $session->getId(); } $keynames['product_id'] = $item->product_id; $keynames['product_attributes'] = $item->product_attributes; // fire plugin event: onGetAdditionalCartKeyValues // this event allows plugins to extend the multiple-column primary key of the carts table $additionalKeyValues = TiendaHelperCarts::getAdditionalKeyValues($item, $post, null); if (!empty($additionalKeyValues)) { $keynames = array_merge($keynames, $additionalKeyValues); } if ($table->load($keynames)) { $table->product_qty = $table->product_qty + $item->product_qty; } else { foreach ($item as $key => $value) { if (property_exists($table, $key)) { $table->set($key, $value); } } } // Now for Eavs!! $eavs = TiendaHelperEav::getAttributes('products', $item->product_id, false, array(1, 2)); if (count($eavs)) { foreach ($eavs as $eav) { // Search for user edtable fields & user submitted value if (in_array($eav->editable_by, array(1, 2)) !== false && array_key_exists($eav->eavattribute_alias, $item)) { $key = $eav->eavattribute_alias; $table->set($key, $item->{$key}); } } } $date = JFactory::getDate(); $table->last_updated = $date->toMysql(); $table->session_id = $session->getId(); if (!$table->save()) { JError::raiseNotice('updateCart', $table->getError()); } else { $this->fixQuantities($item->user_id); } $this->setRedirect('index.php?option=com_tienda&view=pos&task=addproducts&added=1&tmpl=component', JText::_('COM_TIENDA_SUCCESSFULLY_ADDED_ITEM_TO_CART'), 'success'); }
/** * * @param $session_id * @param $user_id * @return unknown_type */ function mergeSessionCartWithUserCart($session_id, $user_id) { Tienda::load('TiendaHelperEav', 'helpers.eav'); $date = JFactory::getDate(); $session = JFactory::getSession(); JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/models'); $model = JModel::getInstance('Carts', 'TiendaModel'); $model->setState('filter_user_leq', '0'); $model->setState('filter_session', $session_id); $session_cartitems = $model->getList(); $this->deleteSessionCartItems($session_id); if (!empty($session_cartitems)) { JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/tables'); $table = JTable::getInstance('Carts', 'TiendaTable'); foreach ($session_cartitems as $session_cartitem) { $keynames = array(); $keynames['user_id'] = $user_id; $keynames['product_id'] = $session_cartitem->product_id; $keynames['product_attributes'] = $session_cartitem->product_attributes; // fire plugin event: onGetAdditionalCartKeyValues //this event allows plugins to extend the multiple-column primary key of the carts table // load EAVs from the previous cart $additionalKeyValues = TiendaHelperCarts::getAdditionalKeyValues($session_cartitem, null, null); if (!empty($additionalKeyValues)) { $keynames = array_merge($keynames, $additionalKeyValues); } $table->product_id = $session_cartitem->product_id; if ($table->load($keynames)) { // the quantity as set in the session takes precedence $table->product_qty = $session_cartitem->product_qty; } else { $eavs = TiendaHelperEav::getAttributes('products', $session_cartitem->product_id); foreach (@$eavs as $eav) { $table->{$eav->eavattribute_alias} = TiendaHelperEav::getAttributeValue($eav, 'carts', $session_cartitem->cart_id, true, false); } foreach ($session_cartitem as $key => $value) { if (property_exists($table, $key)) { $table->set($key, $value); } } // this is a new cartitem, so set cart_id = 0 $table->cart_id = '0'; } $table->user_id = $user_id; $table->session_id = $session->getId(); $table->last_updated = $date->toMysql(); if (!$table->save()) { JError::raiseNotice('updateCart', $table->getError()); } $table->cart_id = '0'; } } }
/** * Loads a row from the database and binds the fields to the object properties * If $load_eav is true, binds also the eav fields linked to this entity * * @access public * @param mixed Optional primary key. If not specifed, the value of current key is used * @param bool reset the object values? * @param bool load the eav values for this object * * @return boolean True if successful */ function load($oid = null, $reset = true, $load_eav = true) { $eavs = array(); $app = JFactory::getApplication(); $editable_by = $app->isAdmin() ? 1 : 2; if ($app->isAdmin()) { $view = JRequest::getCmd('view', ''); if ($view == 'pos') { // display all for POS $editable_by = array(1, 2); } } if (!is_array($oid)) { // load by primary key if not array $keyName = $this->getKeyName(); $oid = array($keyName => $oid); } if (empty($oid)) { // if empty, use the value of the current key $keyName = $this->getKeyName(); $oid = $this->{$keyName}; if (empty($oid)) { // if still empty, fail $this->setError(JText::_('COM_TIENDA_CANNOT_LOAD_WITH_EMPTY_KEY')); return false; } } // allow $oid to be an array of key=>values to use when loading $oid = (array) $oid; $this->_linked_table_key = isset($this->_linked_table_key) ? $this->_linked_table_key : (isset($oid[$this->_linked_table_key_name]) ? $oid[$this->_linked_table_key_name] : ''); if (!empty($reset)) { $this->reset(); } $db = $this->getDBO(); // initialize the query $query = new DSCQuery(); $query->select('*'); $query->from($this->getTableName()); if ($load_eav) { Tienda::load("TiendaHelperBase", 'helpers._base'); $eav_helper = TiendaHelperBase::getInstance('Eav'); $k = $this->_tbl_key; $id = $this->{$k}; // Get the custom fields for this entities Tienda::load('TiendaHelperEav', 'helpers.eav'); $eavs = TiendaHelperEav::getAttributes($this->get('_suffix'), $id, true, $editable_by); // Is this a mirrored table (see decription at the beginning of this file) if (strlen($this->_linked_table) && $this->_linked_table_key) { // Copy the custom field value to this table $mirrored_eavs = $eav_helper->getAttributes($this->_linked_table, $this->_linked_table_key, true, $editable_by); $eavs = array_merge($eavs, $mirrored_eavs); } } foreach ($oid as $key => $value) { // Check that $key is field in table if (!in_array($key, array_keys($this->getProperties()))) { // Check if it is a eav field if ($load_eav) { // loop through until the key is found or the eav are finished $found = false; $i = 0; while (!$found && $i < count($eavs)) { // Does the key exists? if ($key == $eavs[$i]->eavattribute_alias) { $found = true; } else { $i++; } } // Was the key found? if (!$found) { // IF not return an error $this->setError(get_class($this) . ' does not have the field ' . $key); return false; } // key was found -> add this EAV field $value_tbl_name = 'value_' . $eavs[$i]->eavattribute_alias; // for some reason MySQL makes spaces around '-' charachter // (which is often charachter in aliases) that's why we replace it with '_' $value_tbl_name = str_replace("-", "_", $value_tbl_name); // Join the table based on the type of the value $table_type = $eav_helper->getType($eavs[$i]->eavattribute_alias); // Join the tables $query->join('LEFT', '#__tienda_eavvalues' . $table_type . ' AS ' . $value_tbl_name . ' ON ( ' . $value_tbl_name . '.eavattribute_id = ' . $eavs[$i]->eavattribute_id . ' AND ' . $value_tbl_name . '.eaventity_id = ' . $this->_tbl_key . ' )'); // Filter using '=' $query->where($value_tbl_name . ".eavvalue_value = '" . $value . "'"); // else let the store() method worry over this } else { $this->setError(get_class($this) . ' does not have the field ' . $key); return false; } } else { // add the key=>value pair to the query $value = $db->Quote($db->getEscaped(trim(strtolower($value)))); $query->where($key . ' = ' . $value); } } $db->setQuery((string) $query); if ($result = $db->loadAssoc()) { $result = $this->bind($result); if ($result) { // Only now load the eav, in necessary // Check if it is a eav field if ($load_eav) { $k = $this->_tbl_key; $id = $this->{$k}; // Get the custom fields for this entities Tienda::load('TiendaHelperEav', 'helpers.eav'); $eavs = TiendaHelperEav::getAttributes($this->get('_suffix'), $id, false, $editable_by); // Is this a mirrored table (see decription at the beginning of this file) if (strlen($this->_linked_table) && $this->_linked_table_key) { // Copy the custom field value to this table $mirrored_eavs = $eav_helper->getAttributes($this->_linked_table, $this->_linked_table_key); $eavs = array_merge($eavs, $mirrored_eavs); } if (count($eavs)) { foreach ($eavs as $eav) { $key = $eav->eavattribute_alias; $value = $eav_helper->getAttributeValue($eav, $this->get('_suffix'), $id); $this->{$key} = $value; } } } $dispatcher = JDispatcher::getInstance(); $dispatcher->trigger('onLoad' . $this->get('_suffix'), array(&$this)); } return $result; } else { $this->setError($db->getErrorMsg()); return false; } }
$k = 0; $subtotal = 0; ?> <?php foreach ($items as $item) { ?> <?php $params = new DSCParameter(trim(@$item->cartitem_params)); $default_url = "index.php?option=com_tienda&view=products&task=view&id=" . $item->product_id; $attributes = TiendaHelperProduct::convertAttributesToArray($item->product_id, $item->product_attributes); for ($j = 0, $c = count($attributes); $j < $c; $j++) { $default_url .= '&attribute_' . $attributes[$j][0] . '=' . $attributes[$j][1]; } $eavs = TiendaHelperEav::getAttributes('products', $item->product_id, true, 2); for ($j = 0, $cj = count($eavs); $j < $cj; $j++) { $default_url .= '&' . urlencode($eavs[$j]->eavattribute_alias) . '=' . urlencode(TiendaHelperEav::getAttributeValue($eavs[$j], 'carts', $item->cart_id, false, true)); } $link = $params->get('product_url', $default_url); $link = JRoute::_($link); ?> <tr class="row<?php echo $k; ?> "> <td style="width: 20px; text-align: center;"> <input type="checkbox" id="cb<?php echo $i; ?>