function addImageToProduct($product_id, $image_path, $alt_text, $thumb_path = null)
 {
     global $application;
     $tables = $this->getTables();
     $imgs_table = $tables['pi_images']['columns'];
     // detailed image resizing
     $piSettings = modApiFunc('Product_Images', 'getSettings');
     if ($piSettings['RESIZE_DETAILED_LARGE_IMAGE'] == 'Y' && function_exists('gd_info')) {
         $image_path = modApiFunc('Product_Images', 'resizeImage', $image_path, $piSettings['DETAILED_LARGE_IMAGE_SIZE']);
     }
     $_is = getimagesize($image_path);
     $img_sizes = array('width' => $_is[0], 'height' => $_is[1], 'filesize' => filesize($image_path));
     $image_path = str_replace($application->getAppIni('PATH_IMAGES_DIR'), '', $image_path);
     $query = new DB_Insert('pi_images');
     $query->addInsertValue($product_id, $imgs_table['product_id']);
     $query->addInsertValue($image_path, $imgs_table['image_path']);
     $query->addInsertValue(serialize($img_sizes), $imgs_table['image_sizes']);
     $query->addInsertValue($_is['mime'], $imgs_table['image_mime_type']);
     $query->addMultiLangInsertValue($alt_text, $imgs_table['alt_text'], $imgs_table['image_id'], 'Product_Images');
     $query->addInsertValue($this->__getMaxSortOrderOfProductImages($product_id) + 1, $imgs_table['sort_order']);
     if ($thumb_path != null) {
         $_ts = getimagesize($thumb_path);
         $thumb_sizes = array('width' => $_ts[0], 'height' => $_ts[1], 'filesize' => filesize($thumb_path));
         $thumb_path = str_replace($application->getAppIni('PATH_IMAGES_DIR'), '', $thumb_path);
         $query->addInsertValue($thumb_path, $imgs_table['thumb_path']);
         $query->addInsertValue(serialize($thumb_sizes), $imgs_table['thumb_sizes']);
     }
     $application->db->getDB_Result($query);
     $file_id = $application->db->DB_Insert_Id();
     return $file_id;
 }
 function _cloneShippingMethod($method_id)
 {
     global $application;
     $_methods = $this->getShippingMethods();
     $method = -1;
     foreach ($_methods as $m) {
         if ($m["id"] == $method_id) {
             $method = $m;
             break;
         }
     }
     if ($method == -1) {
         return false;
     }
     $rates = $this->getShippingRates($method_id);
     $tables = $this->getTables();
     $tables = $this->getTables();
     $table = 'sm_dsr_methods';
     $columns = $tables[$table]['columns'];
     $query = new DB_Insert($table);
     $query->addMultiLangInsertValue($method["method_name"], $columns['method_name'], $columns['id'], 'Shipping_Module_DSR');
     $query->addInsertValue($method["method_code"], $columns['method_code']);
     $query->addInsertValue($method["destination"], $columns['destination']);
     $query->addInsertValue($method["available"], $columns['available']);
     $application->db->getDB_Result($query);
     $new_method_id = $application->db->DB_Insert_Id();
     foreach ($rates as $rate) {
         $rate_data = array("country_id" => $rate["dst_country"], "state_id" => $rate["dst_state"], "rate_data" => array("wrange_from" => $rate["wrange_from"], "wrange_to" => $rate["wrange_to"], "bcharge_abs" => $rate["bcharge_abs"], "bcharge_perc" => $rate["bcharge_perc"], "acharge_pi_abs" => $rate["acharge_pi_abs"], "acharge_pwu_abs" => $rate["acharge_pwu_abs"]));
         $this->_addShippingRate($new_method_id, $rate_data);
     }
     return $new_method_id;
 }
 function _addInputType($type_name, $type_values = array(), $it_id = '')
 {
     global $application;
     $result = array();
     // magic numbers: 9 is the maximal pre-defined input type
     if ($it_id > 0 && $it_id <= 9) {
         return;
     }
     // removing empty values
     if (!is_array($type_values)) {
         $type_values = array($type_values);
     }
     foreach ($type_values as $k => $v) {
         if (!trim($v)) {
             unset($type_values[$k]);
         }
     }
     $tables = modApiFunc('Catalog', 'getTables');
     $it = $tables['input_types']['columns'];
     $itv = $tables['input_type_values']['columns'];
     $i = new DB_Replace('input_types');
     if ($it_id) {
         $i->addReplaceValue($it_id, $it['ut_id']);
     }
     $i->addReplaceValue($type_name, $it['name']);
     $result['new_input_type'] = $application->db->getDB_Result($i);
     if ($it_id) {
         $result['insert_type_id'] = $it_id;
     } else {
         $result['insert_type_id'] = $application->db->DB_Insert_Id();
     }
     //adding values for new type
     $id = $result['insert_type_id'];
     if (is_array($type_values) && !empty($type_values)) {
         $saved_values = array();
         if ($it_id) {
             $query = new DB_Select();
             $query->addSelectField($itv['id'], 'id');
             $query->WhereValue($itv['it_id'], DB_EQ, $id);
             $query->SelectOrder($itv['id'], 'ASC');
             $saved_values = $application->db->getDB_Result($query);
             if (!$saved_values) {
                 $saved_values = array();
             } else {
                 foreach ($saved_values as $k => $v) {
                     $saved_values[$k] = $v['id'];
                 }
             }
         }
         $index = 1;
         foreach ($type_values as $i => $value) {
             $old_id = array_shift($saved_values);
             if ($old_id) {
                 $upd = new DB_Update('input_type_values');
                 $upd->addMultiLangUpdateValue($itv['value'], $value, $itv['id'], $old_id, 'Catalog');
                 $upd->WhereValue($itv['id'], DB_EQ, $old_id);
                 $application->db->getDB_Result($upd);
             } else {
                 $ins = new DB_Insert('input_type_values');
                 $ins->addInsertValue($id, $itv['it_id']);
                 $ins->addMultiLangInsertValue($value, $itv['value'], $itv['id'], 'Catalog');
                 $application->db->getDB_Result($ins);
             }
             $result['new_input_values'][] = $value;
         }
         if (!empty($saved_values)) {
             $d1 = new DB_Delete("input_type_values");
             $d1->deleteMultiLangField($itv['value'], $itv['id'], 'Catalog');
             $d1->WhereValue($itv['id'], DB_IN, '(\'' . implode('\', \'', $saved_values) . '\')');
             $application->db->getDB_Result($d1);
         }
     } elseif ($it_id) {
         $d1 = new DB_Delete("input_type_values");
         $d1->deleteMultiLangField($itv['value'], $itv['id'], 'Catalog');
         $d1->WhereValue($itv['it_id'], DB_EQ, $it_id);
         $application->db->getDB_Result($d1);
     }
     return $result;
 }
 /**
  * Adds the notification to the database.
  *
  *@param array $data - the array of features taken from the form
  *
  *@return
  */
 function addNotification($data)
 {
     global $application;
     $tables = $this->getTables();
     $n = $tables['notifications']['columns'];
     $query = new DB_Insert('notifications');
     $query->addInsertValue($data['Action'], $n['na_id']);
     $query->addMultiLangInsertValue($data['Name'], $n['name'], $n['id'], 'Notifications');
     $query->addMultiLangInsertValue($data['Subject'], $n['subject'], $n['id'], 'Notifications');
     $query->addMultiLangInsertValue($data['Body'], $n['body'], $n['id'], 'Notifications');
     if (key($data['SendFrom']) === 'EMAIL_ADMINISTRATOR') {
         $query->addInsertValue("", $n['from_email_custom_address']);
         $query->addInsertValue($data['SendFrom'][key($data['SendFrom'])], $n['from_email_admin_id']);
     } else {
         $query->addInsertValue($data['SendFrom'][key($data['SendFrom'])], $n['from_email_custom_address']);
         $query->addInsertValue(DB_NULL, $n['from_email_admin_id']);
     }
     $query->addInsertValue(key($data['SendFrom']), $n['from_email_code']);
     $query->addInsertValue($data['Active'], $n['active']);
     $application->db->getDB_Result($query);
     $n_id = $application->db->DB_Insert_Id();
     $nst = $tables['notification_send_to']['columns'];
     foreach ($data['SendTo'] as $email) {
         $query = new DB_Insert('notification_send_to');
         $query->addInsertValue($n_id, $nst['n_id']);
         $query->addInsertValue($email[key($email)], $nst['email']);
         $query->addInsertValue(key($email), $nst['code']);
         $application->db->getDB_Result($query);
     }
     if ($data['OptionsValues']) {
         $ov2n = $tables['option_values_to_notification']['columns'];
         foreach ($data['OptionsValues'] as $key => $val) {
             $query = new DB_Insert('option_values_to_notification');
             $query->addInsertValue($key, $ov2n['naov_id']);
             $query->addInsertValue($n_id, $ov2n['n_id']);
             $query->addInsertValue('true', $ov2n['value']);
             $application->db->getDB_Result($query);
         }
     }
     if ($data['BlockBodies']) {
         $nbb = $tables['notification_blocktag_bodies']['columns'];
         foreach ($data['BlockBodies'] as $block_id => $body) {
             $query = new DB_Insert('notification_blocktag_bodies');
             $query->addInsertValue($block_id, $nbb['nb_id']);
             $query->addInsertValue($n_id, $nbb['n_id']);
             $query->addMultiLangInsertValue($body, $nbb['body'], $nbb['id'], 'Notifications');
             $application->db->getDB_Result($query);
         }
     }
 }
 /**
  * Adds a value of the option.
  *
  * @descr $data = array(
  *  'option_id' => int  - ID of the option the value should be added to
  * ,'value_name' => string  - value name
  * ,'is_default' => enum('Y','N')  - if the added value is default for the option
  * ,'price_modifier' => float  - price modifier
  * ,'weight_modifier' => float  - weight modifier
  * ,'shipping_cost_modifier' => float  - shipping cost modifier
  * );
  *
  * for generation examples see action add_value_to_option.
  *
  * WARNING: the call of this function with the data that hasn't been checked
  * by the function checkDataFor() can result in the fatal error in the
  * class DB_MySQL.
  *
  * @param array $data - data to add ,checked by the function checkDataFor()
  * @return new value ID if it is added, FALSE if an error occurred
  * while adding
  */
 function addValueToOption($data)
 {
     global $application;
     $tables = $this->getTables();
     $values = $tables['po_options_values']['columns'];
     $options = $tables['po_options']['columns'];
     $max_sort_order = $this->__getMaxValuesSortOrder($data['option_id']);
     $data['sort_order'] = $max_sort_order + 1;
     $query = new DB_Insert('po_options_values');
     foreach ($data as $key => $value) {
         if ($key == 'value_name') {
             $query->addMultiLangInsertValue($value, $values[$key], $values['value_id'], 'Product_Options');
         } elseif ($key != '_ml') {
             $query->addInsertValue($value, $values[$key]);
         }
     }
     $application->db->getDB_Result($query);
     $res = $application->db->QueryResult;
     if ($res == false) {
         return false;
     }
     $vid = $application->db->DB_Insert_Id();
     if (isset($data['_ml'])) {
         modApiFunc('MultiLang', 'addMLTableData', 'Product_Options', 'po_options_values', $vid, $data['_ml']);
     }
     $query = new DB_Select();
     $query->addSelectTable('po_options');
     $query->addSelectField($options["option_type"], 'option_type');
     $query->WhereValue($options["option_id"], DB_EQ, $data["option_id"]);
     $res = $application->db->getDB_Result($query);
     $otype = $res[0]["option_type"];
     if ($otype == "SS") {
         $this->__chooseAndSetDefaultValueForOption($data["option_id"], $data["is_default"] == 'Y' ? $vid : 0);
     }
     return $vid;
 }
 /**
  * Adds Display Option.
  *
  * @param
  * @return
  */
 function addTaxDisplayOption($formula, $option_id, $display_view)
 {
     global $application;
     $tables = $this->getTables();
     $td = $tables['tax_display']['columns'];
     $query = new DB_Insert('tax_display');
     $query->addInsertValue($formula, $td['formula']);
     $query->addInsertValue($option_id, $td['tdo_id']);
     $query->addMultiLangInsertValue($display_view, $td['view'], $td['id'], 'Taxes');
     return $application->db->getDB_Result($query);
 }