/** Save and delete from database * all product custom_fields and xref @ var $table : the xref table(eg. product,category ...) @array $data : array of customfields @int $id : The concerned id (eg. product_id) */ public function storeProductCustomfields($table, $datas, $id) { vRequest::vmCheckToken('Invalid token in storeProductCustomfields'); //Sanitize id $id = (int) $id; //Table whitelist $tableWhiteList = array('product', 'category', 'manufacturer'); if (!in_array($table, $tableWhiteList)) { return false; } // Get old IDS $db = JFactory::getDBO(); $db->setQuery('SELECT `virtuemart_customfield_id` FROM `#__virtuemart_' . $table . '_customfields` as `PC` WHERE `PC`.virtuemart_' . $table . '_id =' . $id); $old_customfield_ids = $db->loadColumn(); if (array_key_exists('field', $datas)) { foreach ($datas['field'] as $key => $fields) { if (!empty($datas['field'][$key]['virtuemart_product_id']) and (int) $datas['field'][$key]['virtuemart_product_id'] != $id) { //aha the field is from the parent, what we do with it? $fields['override'] = (int) $fields['override']; $fields['disabler'] = (int) $fields['disabler']; if ($fields['override'] != 0 or $fields['disabler'] != 0) { //If it is set now as override, store it as clone, therefore set the virtuemart_customfield_id = 0 if ($fields['override'] != 0) { $fields['override'] = $fields['virtuemart_customfield_id']; } if ($fields['disabler'] != 0) { $fields['disabler'] = $fields['virtuemart_customfield_id']; } $fields['virtuemart_customfield_id'] = 0; } else { //we do not store customfields inherited by the parent, therefore $key = array_search($fields['virtuemart_customfield_id'], $old_customfield_ids); if ($key !== false) { unset($old_customfield_ids[$key]); } continue; } } if ($fields['field_type'] == 'C') { $cM = VmModel::getModel('custom'); $c = $cM->getCustom($fields['virtuemart_custom_id'], ''); if (!empty($c->sCustomId)) { $sCustId = $c->sCustomId; $labels = array(); foreach ($fields['selectoptions'] as $k => $option) { if ($option['voption'] == 'clabels' and !empty($option['clabel'])) { $labels[$k] = $option['clabel']; } } //for testing foreach ($fields['options'] as $prodId => $lvalue) { if ($prodId == $id) { continue; } $db->setQuery('SELECT `virtuemart_customfield_id` FROM `#__virtuemart_' . $table . '_customfields` as `PC` WHERE `PC`.virtuemart_' . $table . '_id ="' . $prodId . '" AND `virtuemart_custom_id`="' . $sCustId . '" '); $strIds = $db->loadColumn(); $i = 0; foreach ($lvalue as $k => $value) { if (!empty($labels[$k])) { $ts = array(); $ts['field_type'] = 'S'; $ts['virtuemart_product_id'] = $prodId; $ts['virtuemart_custom_id'] = $sCustId; if (isset($strIds[$i])) { $ts['virtuemart_customfield_id'] = $strIds[$i]; unset($strIds[$i++]); } $ts['customfield_value'] = $value; $tableCustomfields = $this->getTable($table . '_customfields'); $tableCustomfields->bindChecknStore($ts); } } if (count($strIds) > 0) { // delete old unused Customfields $db->setQuery('DELETE FROM `#__virtuemart_' . $table . '_customfields` WHERE `virtuemart_customfield_id` in ("' . implode('","', $strIds) . '") '); $db->execute(); } } } } $fields['virtuemart_' . $table . '_id'] = $id; $tableCustomfields = $this->getTable($table . '_customfields'); $tableCustomfields->setPrimaryKey('virtuemart_product_id'); if (!empty($datas['customfield_params'][$key]) and !isset($datas['clone'])) { if (array_key_exists($key, $datas['customfield_params'])) { $fields = array_merge((array) $fields, (array) $datas['customfield_params'][$key]); } } $tableCustomfields->_xParams = 'customfield_params'; if (!class_exists('VirtueMartModelCustom')) { require VMPATH_ADMIN . DS . 'models' . DS . 'custom.php'; } VirtueMartModelCustom::setParameterableByFieldType($tableCustomfields, $fields['field_type'], $fields['custom_element'], $fields['custom_jplugin_id']); $tableCustomfields->bindChecknStore($fields); $key = array_search($fields['virtuemart_customfield_id'], $old_customfield_ids); if ($key !== false) { unset($old_customfield_ids[$key]); } } } else { vmdebug('storeProductCustomfields nothing to store'); } vmdebug('Delete $old_customfield_ids', $old_customfield_ids); if (count($old_customfield_ids)) { // delete old unused Customfields $db->setQuery('DELETE FROM `#__virtuemart_' . $table . '_customfields` WHERE `virtuemart_customfield_id` in ("' . implode('","', $old_customfield_ids) . '") '); $db->execute(); vmdebug('Deleted $old_customfield_ids', $old_customfield_ids); } JPluginHelper::importPlugin('vmcustom'); $dispatcher = JDispatcher::getInstance(); if (isset($datas['customfield_params']) and is_array($datas['customfield_params'])) { foreach ($datas['customfield_params'] as $key => $plugin_param) { $dispatcher->trigger('plgVmOnStoreProduct', array($datas, $plugin_param)); } } }
public function storeProductCustomfields($table, $datas, $id) { //vmdebug('storeProductCustomfields',$datas); vRequest::vmCheckToken('Invalid token in storeProductCustomfields'); //Sanitize id $id = (int) $id; //Table whitelist $tableWhiteList = array('product', 'category', 'manufacturer'); if (!in_array($table, $tableWhiteList)) { return false; } // Get old IDS $db = JFactory::getDBO(); $db->setQuery('SELECT `virtuemart_customfield_id` FROM `#__virtuemart_' . $table . '_customfields` as `PC` WHERE `PC`.virtuemart_' . $table . '_id =' . $id); $old_customfield_ids = $db->loadColumn(); //vmdebug('storeProductCustomfields',$datas['field']); if (array_key_exists('field', $datas)) { foreach ($datas['field'] as $key => $fields) { if (!empty($datas['field'][$key]['virtuemart_product_id']) and (int) $datas['field'][$key]['virtuemart_product_id'] != $id) { //aha the field is from the parent, what we do with it? $fields['override'] = (int) $fields['override']; $fields['disabler'] = (int) $fields['disabler']; if ($fields['override'] != 0 or $fields['disabler'] != 0) { //If it is set now as override, store it as clone, therefore set the virtuemart_customfield_id = 0 if ($fields['override'] != 0) { $fields['override'] = $fields['virtuemart_customfield_id']; } if ($fields['disabler'] != 0) { $fields['disabler'] = $fields['virtuemart_customfield_id']; } $fields['virtuemart_customfield_id'] = 0; //unset($fields['virtuemart_product_id']); //why we unset the primary key? vmdebug('storeProductCustomfields I am in field from parent and create a clone'); } else { //we do not store customfields inherited by the parent, therefore vmdebug('storeProductCustomfields I am in field from parent => not storing'); $key = array_search($fields['virtuemart_customfield_id'], $old_customfield_ids); if ($key !== false) { //vmdebug('storeProductCustomfields unsetting from $old_customfild_ids',$key); unset($old_customfield_ids[$key]); } continue; } } $fields['virtuemart_' . $table . '_id'] = $id; $tableCustomfields = $this->getTable($table . '_customfields'); $tableCustomfields->setPrimaryKey('virtuemart_product_id'); if (!empty($datas['customfield_params'][$key]) and !isset($datas['clone'])) { if (array_key_exists($key, $datas['customfield_params'])) { $fields = array_merge((array) $fields, (array) $datas['customfield_params'][$key]); } } $tableCustomfields->_xParams = 'customfield_params'; if (!class_exists('VirtueMartModelCustom')) { require JPATH_VM_ADMINISTRATOR . DS . 'models' . DS . 'custom.php'; } VirtueMartModelCustom::setParameterableByFieldType($tableCustomfields, $fields['field_type'], $fields['custom_element'], $fields['custom_jplugin_id']); //vmdebug('Data to store $tableCustomfields->bindChecknStore',$fields,$tableCustomfields); $tableCustomfields->bindChecknStore($fields); $errors = $tableCustomfields->getErrors(); foreach ($errors as $error) { vmError($error); } $key = array_search($fields['virtuemart_customfield_id'], $old_customfield_ids); if ($key !== false) { unset($old_customfield_ids[$key]); } // vmdebug('datas clone',$old_customfield_ids,$fields); } } else { vmdebug('storeProductCustomfields nothing to store', $datas['field']); } vmdebug('Delete $old_customfield_ids', $old_customfield_ids); if (count($old_customfield_ids)) { // delete old unused Customfields $db->setQuery('DELETE FROM `#__virtuemart_' . $table . '_customfields` WHERE `virtuemart_customfield_id` in ("' . implode('","', $old_customfield_ids) . '") '); $db->execute(); vmdebug('Deleted $old_customfield_ids', $old_customfield_ids); } JPluginHelper::importPlugin('vmcustom'); $dispatcher = JDispatcher::getInstance(); if (isset($datas['customfield_params']) and is_array($datas['customfield_params'])) { foreach ($datas['customfield_params'] as $key => $plugin_param) { $dispatcher->trigger('plgVmOnStoreProduct', array($datas, $plugin_param)); } } }