Exemple #1
0
 /**
  *
  * @param $session_id
  * @param $user_id
  * @return unknown_type
  */
 function mergeSessionCartWithUserCart($session_id, $user_id)
 {
     Citruscart::load('CitruscartHelperEav', 'helpers.eav');
     $date = JFactory::getDate();
     $session = JFactory::getSession();
     JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models');
     $model = JModelLegacy::getInstance('Carts', 'CitruscartModel');
     $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_citruscart/tables');
         $table = JTable::getInstance('Carts', 'CitruscartTable');
         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 = CitruscartHelperCarts::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 = CitruscartHelperEav::getAttributes('products', $session_cartitem->product_id);
                 foreach ($eavs as $eav) {
                     $table->{$eav->eavattribute_alias} = CitruscartHelperEav::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->toSql();
             if (!$table->save()) {
                 JError::raiseNotice('updateCart', $table->getError());
             }
             $table->cart_id = '0';
         }
     }
 }
Exemple #2
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();
     /* Get the application */
     $app = JFactory::getApplication();
     $editable_by = $app->isAdmin() ? 1 : 2;
     if ($app->isAdmin()) {
         $view = $app->input->get('view', '');
         //$view = JRequest::get('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_CITRUSCART_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) {
         Citruscart::load("CitruscartHelperBase", 'helpers._base');
         $eav_helper = CitruscartHelperBase::getInstance('Eav');
         $k = $this->_tbl_key;
         $id = $this->{$k};
         // Get the custom fields for this entities
         Citruscart::load('CitruscartHelperEav', 'helpers.eav');
         $eavs = CitruscartHelperEav::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', '#__citruscart_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->q($db->escape(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
                 Citruscart::load('CitruscartHelperEav', 'helpers.eav');
                 $eavs = CitruscartHelperEav::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;
                     }
                 }
             }
             $app->triggerEvent('onLoad' . $this->get('_suffix'), array(&$this));
         }
         return $result;
     } else {
         $this->setError($db->getErrorMsg());
         return false;
     }
 }
Exemple #3
0
    $subtotal = 0;
    ?>
            <?php 
    foreach ($items as $item) {
        ?>

            	<?php 
        $params = new DSCParameter(trim($item->cartitem_params));
        $default_url = "index.php?option=com_citruscart&view=products&task=view&id=" . $item->product_id;
        $attributes = CitruscartHelperProduct::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 = CitruscartHelperEav::getAttributes('products', $item->product_id, true, 2);
        for ($j = 0, $cj = count($eavs); $j < $cj; $j++) {
            $default_url .= '&' . urlencode($eavs[$j]->eavattribute_alias) . '=' . urlencode(CitruscartHelperEav::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;
        ?>
" name="cid[<?php 
        echo $item->cart_id;
Exemple #4
0
 /**
  * Saves an item and redirects based on task
  * @return void
  */
 function save()
 {
     $app = JFactory::getApplication();
     $task = $app->input->getString('task');
     $post = $app->input->getArray($_POST);
     //print_r($post); exit;
     $model = $this->getModel($this->get('suffix'));
     $isSaveAs = false;
     $row = $model->getTable();
     $row->load($model->getId());
     // to set the Product Id
     $row->product_id = isset($post['id']) ? $post['id'] : "";
     $row->bind($post);
     $row->product_description = $app->input->get('product_description', '', 'RAW');
     $row->product_description_short = $app->input->get('product_description_short', '', 'RAW');
     // set the id as 0 for new entry
     if ($task == "save_as") {
         unset($row);
         // load WITHOUT EAV! otherwise the save will fail
         $row = $model->getTable();
         $row->load($model->getId(), true, false);
         $row->bind($app->input->getArray($_POST));
         $row->product_description = $app->input->get('product_description', '', 'RAW');
         $row->product_description_short = $app->input->get('product_description_short', '', 'RAW');
         $isSaveAs = true;
         $oldProductImagePath = $row->getImagePath();
         $pk = $row->getKeyName();
         $oldPk = $row->{$pk};
         // these get reset
         $row->{$pk} = 0;
         $row->product_images_path = '';
         $row->product_rating = '';
         $row->product_comments = '';
     }
     $row->_isNew = empty($row->product_id);
     $fieldname = 'product_full_image_new';
     //$userfiles = JRequest::getVar( $fieldname, '', 'files', 'array' );
     $userfiles = $app->input->post->get($fieldname, '', 'files', 'array');
     // save the integrations
     $row = $this->prepareParameters($row);
     //check if normal price exists
     Citruscart::load("CitruscartHelperProduct", 'helpers.product');
     if ($isSaveAs) {
         // and the prices
         $prices = CitruscartHelperProduct::getPrices($oldPk);
     } else {
         $prices = CitruscartHelperProduct::getPrices($row->product_id);
     }
     if ($row->save()) {
         $row->product_id = $row->id;
         $model->setId($row->id);
         $this->messagetype = 'message';
         $this->message = JText::_('COM_CITRUSCART_SAVED');
         // check it's new entry or empty price but not save as
         if (($row->_isNew || empty($prices)) && !$isSaveAs) {
             // set price if new or no prices set
             $price = JTable::getInstance('Productprices', 'CitruscartTable');
             $price->product_id = $row->id;
             $price->product_price = $app->input->get('product_price');
             $price->group_id = Citruscart::getInstance()->get('default_user_group', '1');
             if (!$price->save()) {
                 $this->messagetype = 'notice';
                 $this->message .= " :: " . $price->getError();
             }
         }
         if ($row->_isNew && !$isSaveAs) {
             // set category
             $category = JTable::getInstance('Productcategories', 'CitruscartTable');
             $category->product_id = $row->id;
             $category->category_id = $app->input->getInt('category_id');
             if (!$category->save()) {
                 $this->messagetype = 'notice';
                 $this->message .= " :: " . $category->getError();
             }
             // save default quantity
             $quantity = JTable::getInstance('Productquantities', 'CitruscartTable');
             $quantity->product_id = $row->id;
             $quantity->quantity = $app->input->getInt('product_quantity');
             if (!$quantity->save()) {
                 $this->messagetype = 'notice';
                 $this->message .= " :: " . $quantity->getError();
             }
         }
         if ($isSaveAs) {
             // set price when cloning
             $priceTable = JTable::getInstance('Productprices', 'CitruscartTable');
             foreach ($prices as $price) {
                 $priceTable->product_id = $row->id;
                 $priceTable->product_price = $price->product_price;
                 $priceTable->product_price_startdate = $price->product_price_startdate;
                 $priceTable->product_price_enddate = $price->product_price_enddate;
                 $priceTable->created_date = $price->created_date;
                 $priceTable->modified_date = $price->modified_date;
                 $priceTable->group_id = $price->group_id;
                 $priceTable->price_quantity_start = $price->price_quantity_start;
                 $priceTable->price_quantity_end = $price->price_quantity_end;
                 if (!$priceTable->save()) {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $priceTable->getError();
                 }
             }
             // set category
             $categoryTable = JTable::getInstance('Productcategories', 'CitruscartTable');
             $categories = CitruscartHelperProduct::getCategories($oldPk);
             foreach ($categories as $category) {
                 $categoryTable->product_id = $row->id;
                 $categoryTable->category_id = $category;
                 if (!$categoryTable->save()) {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $categoryTable->getError();
                 }
             }
             // TODO Save Attributes
             // An array to map attribute id  old attribute id  are as key and new attribute id are as value
             $attrbuteMappingArray = array();
             $attrbuteParentMappingArray = array();
             $attributes = CitruscartHelperProduct::getAttributes($oldPk);
             foreach ($attributes as $attribute) {
                 $attributeTable = JTable::getInstance('ProductAttributes', 'CitruscartTable');
                 $attributeTable->productattribute_name = $attribute->productattribute_name;
                 $attributeTable->product_id = $row->id;
                 $attributeTable->ordering = $attribute->ordering;
                 if ($attributeTable->save()) {
                     $attrbuteMappingArray[$attribute->productattribute_id] = $attributeTable->productattribute_id;
                     $attrbuteParentMappingArray[$attributeTable->productattribute_id] = $attribute->parent_productattributeoption_id;
                 } else {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $attributeTable->getError();
                 }
             }
             // set Attribute options
             $attrbuteOptionsMappingArray = array();
             foreach ($attrbuteMappingArray as $oldAttrbuteId => $newAttributeId) {
                 // set Attribute options
                 $options = CitruscartHelperProduct::getAttributeOptionsObjects($oldAttrbuteId);
                 foreach ($options as $option) {
                     $attributeOptionsTable = JTable::getInstance('ProductAttributeOptions', 'CitruscartTable');
                     $attributeOptionsTable->productattribute_id = $newAttributeId;
                     $attributeOptionsTable->productattributeoption_name = $option->productattributeoption_name;
                     $attributeOptionsTable->productattributeoption_price = $option->productattributeoption_price;
                     $attributeOptionsTable->productattributeoption_prefix = $option->productattributeoption_prefix;
                     $attributeOptionsTable->productattributeoption_code = $option->productattributeoption_code;
                     $attributeOptionsTable->ordering = $option->ordering;
                     if ($attributeOptionsTable->save()) {
                         $attrbuteOptionsMappingArray[$option->productattributeoption_id] = $attributeOptionsTable->productattributeoption_id;
                     } else {
                         $this->messagetype = 'notice';
                         $this->message .= " :: " . $attributeOptionsTable->getError();
                     }
                 }
                 // save parent relationship
                 if ($attrbuteParentMappingArray[$newAttributeId]) {
                     $attributeTable = JTable::getInstance('ProductAttributes', 'CitruscartTable');
                     $attributeTable->load($newAttributeId);
                     $attributeTable->parent_productattributeoption_id = $attrbuteOptionsMappingArray[$attrbuteParentMappingArray[$newAttributeId]];
                     if (!$attributeTable->save()) {
                         $this->messagetype = 'notice';
                         $this->message .= " :: " . $attributeTable->getError();
                     }
                 }
             }
             // set quantity
             $quantityTable = JTable::getInstance('Productquantities', 'CitruscartTable');
             $quantities = CitruscartHelperProduct::getProductQuantitiesObjects($oldPk);
             foreach ($quantities as $quantity) {
                 $quantityTable->product_attributes = $quantity->product_attributes;
                 $quantityTable->product_id = $row->id;
                 $quantityTable->vendor_id = $quantity->vendor_id;
                 $quantityTable->quantity = $quantity->quantity;
                 $optionsCSV = $quantity->product_attributes;
                 $options = explode(",", $optionsCSV);
                 $newOptions = array();
                 foreach ($options as $option) {
                     $newOptions[] = $attrbuteOptionsMappingArray[$option];
                 }
                 $optionsCSV = implode(",", $newOptions);
                 $quantityTable->product_attributes = $optionsCSV;
                 if (!$quantityTable->save()) {
                     $this->messagetype = 'notice';
                     $this->message .= " :: " . $quantityTable->getError();
                 }
             }
             // copy all gallery files
             jimport('joomla.filesystem.folder');
             jimport('joomla.filesystem.file');
             $galleryFiles = JFolder::files($oldProductImagePath);
             // get all gallery images
             if (count($galleryFiles)) {
                 JFolder::create($row->getImagePath());
                 // create folder for images
                 JFolder::create($row->getImagePath() . 'thumbs');
                 // create folder for thumbnails images
                 for ($i = 0, $c = count($galleryFiles); $i < $c; $i++) {
                     // copy only images with both original file and a corresponding thumbnail
                     if (JFile::exists($oldProductImagePath . 'thumbs/' . $galleryFiles[$i]) && JFile::exists($oldProductImagePath . $galleryFiles[$i])) {
                         JFile::copy($oldProductImagePath . $galleryFiles[$i], $row->getImagePath() . DS . $galleryFiles[$i]);
                         JFile::copy($oldProductImagePath . 'thumbs/' . $galleryFiles[$i], $row->getImagePath() . '/thumbs/' . $galleryFiles[$i]);
                     }
                 }
             }
             // duplicate product files (only in db)
             $modelFiles = $this->getModel('productfiles');
             $modelFiles->setState('filter_product', $oldPk);
             $listFiles = $modelFiles->getList();
             if (count($listFiles)) {
                 $row_file = JTable::getInstance('Productfiles', 'CitruscartTable');
                 for ($i = 0, $c = count($listFiles); $i < $c; $i++) {
                     $row_file->bind($listFiles[$i]);
                     // bind old data
                     $row_file->productfile_id = 0;
                     // will be set
                     $row_file->product_id = $row->product_id;
                     // use clone's ID
                     $row_file->save();
                     // save the data
                 }
             }
             // create duplicate connections for EAV custom fields
             JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models');
             $model = JModelLegacy::getInstance('EavAttributes', 'CitruscartModel');
             $model->setState('filter_entitytype', 'products');
             $model->setState('filter_entityid', $oldPk);
             $listEAV = $model->getList();
             $teav = $model->getTable();
             if (is_array($listEAV)) {
                 for ($i = 0, $c = count($listEAV); $i < $c; $i++) {
                     $tblEAV = JTable::getInstance('EavAttributeEntities', 'CitruscartTable');
                     $tblEAV->eaventity_id = $row->product_id;
                     $tblEAV->eaventity_type = 'products';
                     $tblEAV->eavattribute_id = $listEAV[$i]->eavattribute_id;
                     $tblEAV->save();
                     // Clone the values too!
                     $teav->load($listEAV[$i]->eavattribute_id);
                     $value = CitruscartHelperEav::getAttributeValue($teav, 'products', $row->product_id);
                     $newValue = JTable::getInstance('EavValues', 'CitruscartTable');
                     $newValue->setType($teav->eavattribute_type);
                     $newValue->eavattribute_id = $listEAV[$i]->eavattribute_id;
                     $newValue->eaventity_id = $row->product_id;
                     // Store the value
                     $newValue->eavvalue_value = $value;
                     $newValue->eaventity_type = 'products';
                     $newValue->store();
                 }
             }
         }
         // Multiple images processing
         $i = 0;
         $error = false;
         while (!empty($userfiles['size'][$i])) {
             $dir = $row->getImagePath(true);
             if ($upload = $this->addimage($fieldname, $i, $dir)) {
                 // The first One is the default (if there is no default yet)
                 if ($i == 0 && (empty($row->product_full_image) || $row->product_full_image == '')) {
                     $row->product_full_image = $upload->getPhysicalName();
                     // need to re-save in this instance
                     // should we be storing or saving?
                     $row->save();
                 }
             } else {
                 $error = true;
             }
             $i++;
         }
         if ($error) {
             $this->messagetype = 'notice';
             $this->message .= " :: " . $this->getError();
         }
         $helper = new CitruscartHelperProduct();
         $helper->onAfterSaveProducts($row);
         $model->clearCache();
         JFactory::getApplication()->triggerEvent('onAfterSave' . $this->get('suffix'), array($row));
     } else {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_CITRUSCART_SAVE_FAILED') . " - " . $row->getError();
     }
     $redirect = "index.php?option=com_citruscart";
     switch ($task) {
         case "save_as":
             $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $row->product_id;
             $this->message .= " - " . JText::_('COM_CITRUSCART_YOU_ARE_NOW_EDITING_NEW_PRODUCT');
             break;
         case "saveprev":
             $redirect .= '&view=' . $this->get('suffix');
             // get prev in list
             Citruscart::load("CitruscartHelperProduct", 'helpers.product');
             $surrounding = CitruscartHelperProduct::getSurrounding($model->getId());
             if (!empty($surrounding['prev'])) {
                 $redirect .= '&task=edit&id=' . $surrounding['prev'];
             }
             break;
         case "savenext":
             $redirect .= '&view=' . $this->get('suffix');
             // get next in list
             Citruscart::load("CitruscartHelperProduct", 'helpers.product');
             $surrounding = CitruscartHelperProduct::getSurrounding($model->getId());
             if (!empty($surrounding['next'])) {
                 $redirect .= '&task=edit&id=' . $surrounding['next'];
             }
             break;
         case "savenew":
             $redirect .= '&view=' . $this->get('suffix') . '&task=add';
             break;
         case "apply":
             $redirect .= '&view=' . $this->get('suffix') . '&task=edit&id=' . $model->getId();
             break;
         case "save":
         default:
             $redirect .= "&view=" . $this->get('suffix');
             break;
     }
     $redirect = JRoute::_($redirect, false);
     $this->setRedirect($redirect, $this->message, $this->messagetype);
 }
Exemple #5
0
 /**
  * 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_citruscart/tables');
     $tbl_eav = JTable::getInstance('Eavvalues', 'CitruscartTable');
     $eavs = CitruscartHelperEav::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();
     }
 }
Exemple #6
0
 /**
  * Method to item to cart
  */
 function addToCart()
 {
     $app = JFactory::getApplication();
     $post = $app->input->getArray($_POST);
     $files = $app->input->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 = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $attributes_csv);
     if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) {
         $messagetype = 'notice';
         $message = JText::_(JText::sprintf('COM_CITRUSCART_NOT_AVAILABLE_QUANTITY_NOTICE', $availableQuantity->product_name, $product_qty));
         $this->setRedirect('index.php?option=com_citruscart&view=pos&task=addproducts&tmpl=component', $message, $messagetype);
         return;
     }
     // chec if product for sale
     $product = JTable::getInstance('Products', 'CitruscartTable');
     $product->load(array('product_id' => $product_id));
     // if product notforsale, fail
     if ($product->product_notforsale) {
         $messagetype = 'notice';
         $message = JText::_('COM_CITRUSCART_PRODUCT_NOT_FOR_SALE_NOTICE');
         $this->setRedirect('index.php?option=com_citruscart&view=pos&task=addproducts&tmpl=component', $message, $messagetype);
         return;
     }
     $session = JFactory::getSession();
     $cart_id = $session->get('user_id', '', 'citruscart_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 = Citruscart::getClass('CitruscartHelperCarts', '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
     $results = JFactory::getApplication()->triggerEvent("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();
     $results = JFactory::getApplication()->triggerEvent("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_CITRUSCART_NOT_AVAILABLE_QUANTITY_NOTICE', $availableQuantity->product_name, $product_qty));
             $this->setRedirect('index.php?option=com_citruscart&view=pos&task=addproducts&tmpl=component', $result->message, 'notice');
             return;
         }
     }
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables');
     $table = JTable::getInstance('Carts', 'CitruscartTable');
     // 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 = CitruscartHelperCarts::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 = CitruscartHelperEav::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->toSql();
     $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_citruscart&view=pos&task=addproducts&added=1&tmpl=component', JText::_('COM_CITRUSCART_SUCCESSFULLY_ADDED_ITEM_TO_CART'), 'success');
 }