<?php
require('includes/application_top.php');
$action = (isset($_GET['action']) ? $_GET['action'] : '');
  if (tep_not_null($action)) {
    switch ($action) {
		case'getstate':
			$country_id = (int) $_GET['country_id'];
			$page = (isset($_GET['page']) && $_GET['page'] == 'createacc') ? 1 : 0;
			if($page){
				$inputname = 'state';
			}else{
				$inputname = 'entry_state';
			}
			ob_start();
			$zones_query = tep_db_query("select zone_name, zone_code from " . TABLE_ZONES . " where zone_country_id = '" . $country_id . "' and zone_status = 1  order by zone_name");
			if(tep_db_num_rows($zones_query)  > 0)
			{
				$process = true;
				$entry_state_has_zones = true;
				$country = $country_id;
				if ($process == true) {
				  if ($entry_state_has_zones == true) {
					$zones_array = array();
					$selected_zone = '';
					$zones_array[] = array('id' => '', 'text' => 'Select State');
					$zones_query = tep_db_query("select zone_name, zone_code from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' and zone_status = 1  order by zone_name");
					while ($zones_values = tep_db_fetch_array($zones_query)) {
					  $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
					  if (strtolower($state) == strtolower($zones_values['zone_code']) || strtolower($state) == strtolower($zones_values['zone_name'])) {
						$selected_zone = $zones_values['zone_name'];
					  }
					}
					echo  tep_draw_pull_down_menu($inputname, $zones_array, $selected_zone, 'class="form-control"');
				  } else {
					echo tep_draw_input_field($inputname, '', 'required class="form-control" placeholder="State/Province"' );
				  }
				} else {
				  echo tep_draw_input_field($inputname, '', 'required class="form-control" placeholder="State/Province"');
				}

		 } else {
			echo tep_draw_input_field($inputname, '', 'required class="form-control" placeholder="State/Province"'). '' . (tep_not_null(ENTRY_STATE_TEXT) ? '': '');
		}
			$data = ob_get_clean();
			echo $data;
		break;
		case 'delete_image':
		  //print_r($_GET);
			  $categories_id = $_GET['categories_id'];
			   if ( ($_GET['flag'] == '0') || ($_GET['flag'] == '1') ) {
					if (isset($categories_id)) {
			 echo $query = "UPDATE categories SET categories_image=". $_GET['flag'] ." WHERE categories_id = '".$_GET['categories_id']."'";
					  tep_db_query($query);
					}
			  }
		break;
		case 'add_ajax_special_product':
		//print_r($_POST);
		$data = '';
		$products_id = $_POST['pID'];
		$products_query = tep_db_query("select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'");
		//echo "select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'";
		$products_data = tep_db_fetch_array($products_query);
		if(tep_db_num_rows($products_query) > 0)
		{
			   $data .='
				 <div class="customer-badge" style="">
					<div class="details"><input type="hidden" name="products_id" value="'.$products_id.'">
					  <div id="search_section">
						 <input type="text" class="form-control custom-search cust-input ac_input" id="special_data_info" value="'.$products_data['products_name'].'" autocomplete="off" name="cname" value="" height="50px;" style="" placeholder="Search Product...." style="margin-top:10px;">
					  </div>
						  <input type="hidden" name="products_price" value="'.$products_price.'">
						  </div>
				 </div>';
		}
		echo $data;
				//<p class="cust-name no-margin" style="margin:0px;">'.$products_data['products_name'].'&nbsp;&nbsp;('.$products_data['products_price'].')</p>

		break;
		case 'add_ajax_featured_product':
		//print_r($_POST);
        $data = '';
		$products_id = $_POST['pID'];
		$products_query = tep_db_query("select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'");
		//echo "select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'";
        $products_data = tep_db_fetch_array($products_query);
        if(tep_db_num_rows($products_query) > 0)
        {
               $data .='
                 <div class="customer-badge" style="">
                    <div class="details"><input type="hidden" name="products_id" value="'.$products_id.'">
					  <div id="search_section">
					     <input type="text" class="form-control custom-search cust-input ac_input" id="product_data" value="'.$products_data['products_name'].'" autocomplete="off" name="cname" value="" height="50px;" style="width:500px;" placeholder="Search Product...." style="margin-top:10px;">
					  </div>
                          <input type="hidden" name="products_price" value="'.$products_price.'">
                          </div>
                 </div>';
        }
        echo $data;
                //<p class="cust-name no-margin" style="margin:0px;">'.$products_data['products_name'].'&nbsp;&nbsp;('.$products_data['products_price'].')</p>

		break;


		case 'add_ajax_product':
		//print_r($_POST);
        $data = '';
		$products_id = $_POST['pID'];
		$products_query = tep_db_query("select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'");
		//echo "select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'";
		//echo "select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'";
        $products_data = tep_db_fetch_array($products_query);
        if(tep_db_num_rows($products_query) > 0)
        {
               $data .='
                 <div class="customer-badge" style="">
                    <div class="details"><input type="hidden" name="products_id" value="'.$products_id.'">
					  <div id="search_section">
					     <input type="text" class="form-control custom-search cust-input ac_input" id="search_data" value="'.$products_data['products_name'].'" autocomplete="off" name="search_data" value="" height="50px;" style="width:300px;" placeholder="Search Product...." style="margin-top:10px;">
					  </div>
                          </div>
                 </div>';
        }
        echo $data;
                //<p class="cust-name no-margin" style="margin:0px;">'.$products_data['products_name'].'&nbsp;&nbsp;('.$products_data['products_price'].')</p>

		break;
		case 'ajax_assign_attributes':
			$options_id = $_POST['oID'];
			$products_id = $_POST['pID'];
			$options_values_id = $_POST['id'];
			$chk_query = tep_db_query("SELECT * from " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE options_values_id = '".$options_values_id."' AND options_id = '".$options_id."' AND products_id = '".$products_id."'");
			if(tep_db_num_rows($chk_query) > 0){
				echo 'Attribute Already Assigned!';
			}else{
			tep_db_query("DELETE FROM ". TABLE_PRODUCTS_ATTRIBUTES ." WHERE products_id = '".$products_id."' AND options_id = '".$options_id."' AND options_values_id = '0' ");
			tep_db_query("INSERT INTO  " . TABLE_PRODUCTS_ATTRIBUTES . " SET options_values_id = '".$options_values_id."'
						,price_prefix = '+', options_id = '".$options_id."' , products_id = '".$products_id."'") ;
				echo 'Successfully Assign the Attribute';
			}
		break;
		case 'ajax_delete_attributes':
			$options_values_id = $_POST['id'];
		    tep_db_query("DELETE from products_options_values where products_options_values_id = '".$options_values_id."' ");
		    tep_db_query("DELETE from products_options_values_to_products_options where products_options_values_id = '".$options_values_id."' ");
		break;
		case 'fetchalloptionvalues':
			$languages = tep_get_languages();
			$options_query = tep_db_query("SELECT options_type FROM " . TABLE_PRODUCTS_OPTIONS  . " WHERE products_options_id = '".$_POST['options_id']."' ");
			$row = tep_db_fetch_array($options_query);
			$options_type = $row['options_type'];
			$formstr = '';
			$formstr .= tep_draw_form('assign_attribute', FILENAME_ATTRIBUTES, 'action=assign_attribute', 'post', 'id="assign_attribute" onsubmit="return validate_assignment('.$options_type.');" enctype="multipart/form-data"');
			$formstr .='
				<div class="row" style="padding:5px">'.
						tep_draw_hidden_field('options_id', $_POST['options_id']).
						tep_draw_hidden_field('products_id', $_POST['products_id']);
						$values_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " p2p WHERE pov.products_options_values_id = p2p.products_options_values_id and p2p.products_options_id = '" .$_POST['options_id']. "' and pov.language_id = '" . $languages_id . "' order by pov.products_options_values_name");
							if(tep_db_num_rows($values_query) > 0){
							$showassignBtn = 1;
							while($values_values = tep_db_fetch_array($values_query)) {
								$value_id = $values_values['products_options_values_id'];
								$check_att_qry = tep_db_query("SELECT * FROM " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE products_id = '".$_POST['products_id']."' AND options_id = '".$_POST['options_id']."' AND options_values_id = '".$value_id."' ");
								if(tep_db_num_rows($check_att_qry) > 0){
									continue;
								}
								$formstr .= '<div class="col-md-6 text-for" style="padding:5px;border-bottom:1px solid #ccc">'.
										tep_draw_hidden_field('hiddenoID[' . $value_id . ']', $_POST['options_id']) .
										tep_draw_checkbox_field('values_status[]', $values_values['products_options_values_id'],'','','class="biggercheckbox"') . '&nbsp;' . htmlspecialchars($values_values['products_options_values_name']);
								$formstr .='</div>';
							}
						}else{
							$showassignBtn = 0;
							if($options_type != 4 && $options_type != 1){
								$formstr .=' <h4 style="padding:10px;"> No Options value Available!!</h4> ';
							}
						}

			$formstr .='<div class="col-md-12" style="text-align:center;margin-top:10px;">';
			if($showassignBtn && ($options_type != 4 || $options_type != 1)){
				$formstr .='<button type="submit" class="btn btn-primary m-r-5"><i class="fa fa-save"></i> Assign</button>';
			}
			if($options_type == 4 || $options_type == 1){
				$formstr .='<button type="submit" class="btn btn-primary m-r-5"><i class="fa fa-save"></i> Assign</button>';
			}
			if($options_type != 4 && $options_type != 1){
				$formstr .='<button type="button" onClick="addnewoptionsvalues()" class="btn btn-primary m-r-5"><i class="fa fa-plus"></i> Add New Values</button> <p id="ajaxoptresponse"></p>';
			}
			$formstr .='</div>';

			$formstr .='</div> </form>';
			$attr_arr_query  = tep_db_query("select pov.products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " p2p WHERE pov.products_options_values_id = p2p.products_options_values_id and p2p.products_options_id = '" .$_POST['options_id']. "' and pov.language_id = '" . $languages_id . "' order by pov.products_options_values_name");
			if(tep_db_num_rows($attr_arr_query) > 0){
				while($attr_arr = tep_db_fetch_array($attr_arr_query)){
					$mk_attr_arr[] = $attr_arr['products_options_values_name'];
				}
			}else{
				$mk_attr_arr[] = 'nodata-available';
			}
			$mk_js_attr_arr = json_encode($mk_attr_arr);
			if($_POST['open']){
				$thisstyle = '';
			}else{
				$thisstyle = 'display:none;';
			}
			$formstr .= '<div class="row col-md-12 showthisdivnow" style="text-align:center;margin-top:20px;'.$thisstyle.'">
							<label class="col-md-3 control-label"><strong>Option Value:</strong></label>
							<div class="col-md-6 serializeDiv">';
									for ($i = 0, $n = sizeof($languages); $i < $n; $i ++) {
										$formstr .='<input id="ajax_new_option_value" class="form-control m-b-5" placeholder="Option Value Name in ' . $languages[$i]['name'] . '" type="text" name="ajax_value_name[' . $languages[$i]['id'] . ']">';
									}
					$formstr .= '<p id="ajaxresponse" style="margin:0px;"></p>
								<input type="hidden" name="ajax_products_id" value="'.$_POST['products_id'].'">
								<input type="hidden" name="ajax_options_id" value="'.$_POST['options_id'].'">
								<textarea name="ajaxattributesname" style="display:none;">'.strtolower($mk_js_attr_arr).'</textarea>
							</div>
							<div class="col-md-3">
								<button type="button" onClick="addnewoptionsvaluesajax()"  class="btn btn-primary m-r-5" id="insert_attr"><i class="fa fa-save"></i> Save</button>
							</div>
						</div>';
			echo $formstr;
		break;
		case 'getoptionvalue':
			$attr_arr_query  = tep_db_query("select pov.products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " p2p WHERE pov.products_options_values_id = p2p.products_options_values_id and p2p.products_options_id = '" .$_POST['options_id']. "' and pov.language_id = '" . $languages_id . "' order by pov.products_options_values_name");
			if(tep_db_num_rows($attr_arr_query) > 0){
				while($attr_arr = tep_db_fetch_array($attr_arr_query)){
					$mk_attr_arr[] = $attr_arr['products_options_values_name'];
				}
			}else{
				$mk_attr_arr[] = 'nodata-available';
			}
			$mk_js_attr_arr = json_encode($mk_attr_arr);
			echo '<textarea name="attributesname" style="display:none;">'.strtolower($mk_js_attr_arr).'</textarea>';
		break;
		case 'getalloptionvaluesdropdown':
				$options_query = tep_db_query("SELECT options_type FROM " . TABLE_PRODUCTS_OPTIONS  . " WHERE products_options_id = '".$_POST['options_id']."' ");
				$row = tep_db_fetch_array($options_query);
				$options_type = $row['options_type'];
				if($options_type == 1 || $options_type == 4){
					echo 'noopt';
					exit;
				}
				if($options_type == 5 || $options_type == 6){
					echo 'noneed';
					exit;
				}
				if($options_type == 7){
					echo 'nooptnoneed';
					exit;
				}
				$values_query = tep_db_query("select pov.products_options_values_id, pov.products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov, " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " p2p WHERE pov.products_options_values_id = p2p.products_options_values_id and p2p.products_options_id = '" .$_POST['options_id']. "' and pov.language_id = '" . $languages_id . "' order by pov.products_options_values_name");
				if(tep_db_num_rows($values_query) > 0){
					//echo "\n" . '<option>--Select Option Value--</option>';
					while($values_values = tep_db_fetch_array($values_query)) {
						echo "\n" . '<option name="' . htmlspecialchars($values_values['products_options_values_name']) . '" value="' . $values_values['products_options_values_id'] . '">' . htmlspecialchars($values_values['products_options_values_name']) . '</option>';
					}
				}else{
					echo "\n" . '<option>No Option Values Available </option>';
				}
		break;
		case 'ajax_add_options_values':
				$languages = tep_get_languages();
				$pID = $_POST['ajax_products_id'];
				$value_name_array = $_POST['ajax_value_name'];
				$max_values_id_query = tep_db_query("select max(products_options_values_id) + 1 as next_id from " . TABLE_PRODUCTS_OPTIONS_VALUES);
				$max_values_id_values = tep_db_fetch_array($max_values_id_query);
				$next_id = $max_values_id_values['next_id'];
				$value_name = tep_db_prepare_input($value_name_array);
				for ($i = 0, $n = sizeof($languages); $i < $n; $i ++) {
					$value_name = tep_db_prepare_input($value_name_array[$languages[$i]['id']]);
					tep_db_query("insert into " . TABLE_PRODUCTS_OPTIONS_VALUES . " (products_options_values_id, language_id, products_options_values_name) values ('" . $next_id . "', '" . $languages[$i]['id'] . "', '" . tep_db_input($value_name) . "')");
				}
				//tep_db_query("insert into " . TABLE_PRODUCTS_OPTIONS_VALUES . " (products_options_values_id, language_id, products_options_values_name) values ('" . $next_id . "', '" . (int)$languages_id . "', '" . tep_db_input($value_name) . "')");
				tep_db_query("insert into " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . " (products_options_id, products_options_values_id) values ('" . $_POST['ajax_options_id'] . "', '" . $next_id . "')");
				//tep_db_query("insert into " . TABLE_PRODUCTS_ATTRIBUTES . " SET products_id = '".(int)$pID."', options_id = '".(int)$_POST['ajax_options_id']."', price_prefix = '+', options_values_id = '".$next_id."'");
		break;
		case 'add_ajax_pages_product':
		//print_r($_POST);
        $data = '';
		$products_id = $_POST['pID'];
		$products_query = tep_db_query("select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'");
		//echo "select p.*,pd.* from products p,products_description pd where p.products_id =pd.products_id and p.products_id = '".$products_id."'";
        $products_data = tep_db_fetch_array($products_query);
        if(tep_db_num_rows($products_query) > 0)
        {
               $data .='
                 <div class="customer-badge" style="">
                    <div class="details"><input type="hidden" name="pages_attach_product" value="'.$products_id.'">
					  <div id="search_section">
					     <input type="text" class="form-control custom-search cust-input ac_input" id="pages_data" value="'.$products_data['products_name'].'" autocomplete="off" name="cname" value="" height="50px;" style="" placeholder="Search Product...." style="margin-top:10px;">
					     <input type="hidden" name="pages_attach_product" value="'.$products_id.'">
					  </div>
                          <input type="hidden" name="products_price" value="'.$products_price.'">
                          </div>
                 </div>';
        }
        echo $data;
                //<p class="cust-name no-margin" style="margin:0px;">'.$products_data['products_name'].'&nbsp;&nbsp;('.$products_data['products_price'].')</p>
		break;
		case 'optionsmandetory':
			  if (tep_not_null($_POST['otype'])) {
				switch ($_POST['otype']) {
					case 0:
					case 5:
					case 6:
						//echo 'select, boolean, datepicker';
						$data = array();
						if($typerow == 0){
							$type = 'select';
						}elseif($typerow == 5){
							$type = 'boolean';
						}elseif($typerow == 6){
							$type = 'datepicker';
						}
						$required = 0;
						if($_POST['mandetory']){
							$required = 1;
						}
						$data = array("type" => $type, "required" => $required);
						$jsondata = json_encode($data);
						$chk = tep_db_query("select id from products_options_validation where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."'");
						if(tep_db_num_rows($chk) > 0){
							$update = tep_db_query("UPDATE products_options_validation set settings = '".$jsondata."' where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."' ");
						}else{
							$insert = tep_db_query("INSERT INTO products_options_validation SET `option_id` = '".(int)$_POST['oID']."', `products_id` = '".(int)$_POST['pID']."', settings = '".$jsondata."'");
						}
					break;
					case 1:
						//echo 'textbox';
						$data = array();
						$type = 'textbox';
						$required = 0;
						if($_POST['mandetory']){
							$required = 1;
						}
						$minchar = (int)$_POST['minchar'];
						$maxchar = (int)$_POST['maxchar'];
						$validatetype = $_POST['vtype'];
						$data = array("type" => $type, "required" => $required, "minchar" => $minchar, "maxchar" => $maxchar, "validatetype" => $validatetype);
						$jsondata = json_encode($data);
						$chk = tep_db_query("select id from products_options_validation where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."'");
						if(tep_db_num_rows($chk) > 0){
							$update = tep_db_query("UPDATE products_options_validation set settings = '".$jsondata."' where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."' ");
						}else{
							$insert = tep_db_query("INSERT INTO products_options_validation SET `option_id` = '".(int)$_POST['oID']."', `products_id` = '".(int)$_POST['pID']."', settings = '".$jsondata."'");
						}
					break;
					case 2:
						//echo 'radio';
						$data = array();
						$type = 'radio';
						$required = 0;
						if($_POST['mandetory']){
							$required = 1;
						}
						$data = array("type" => $type, "required" => $required);
						$jsondata = json_encode($data);
						$chk = tep_db_query("select id from products_options_validation where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."'");
						if(tep_db_num_rows($chk) > 0){
							$update = tep_db_query("UPDATE products_options_validation set settings = '".$jsondata."' where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."' ");
						}else{
							$insert = tep_db_query("INSERT INTO products_options_validation SET `option_id` = '".(int)$_POST['oID']."', `products_id` = '".(int)$_POST['pID']."', settings = '".$jsondata."'");
						}
					break;
					case 3:
						//echo 'checkbox';
						$data = array();
						$type = 'checkbox';
						$required = 0;
						if($_POST['mandetory']){
							$required = 1;
						}
						$data = array("type" => $type, "required" => $required);
						$jsondata = json_encode($data);
						$chk = tep_db_query("select id from products_options_validation where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."'");
						if(tep_db_num_rows($chk) > 0){
							$update = tep_db_query("UPDATE products_options_validation set settings = '".$jsondata."' where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."' ");
						}else{
							$insert = tep_db_query("INSERT INTO products_options_validation SET `option_id` = '".(int)$_POST['oID']."', `products_id` = '".(int)$_POST['pID']."', settings = '".$jsondata."'");
						}
					break;
					case 4:
						//echo 'textarea';
						$data = array();
						$type = 'textarea';
						$required = 0;
						if($_POST['mandetory']){
							$required = 1;
						}
						$minchar = (int)$_POST['minchar'];
						$maxchar = (int)$_POST['maxchar'];
						$validatetype = $_POST['vtype'];
						$data = array("type" => $type, "required" => $required, "minchar" => $minchar, "maxchar" => $maxchar, "validatetype" => $validatetype);
						$jsondata = json_encode($data);
						$chk = tep_db_query("select id from products_options_validation where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."'");
						if(tep_db_num_rows($chk) > 0){
							$update = tep_db_query("UPDATE products_options_validation set settings = '".$jsondata."' where `option_id` = '".(int)$_POST['oID']."' AND `products_id` = '".(int)$_POST['pID']."' ");
						}else{
							$insert = tep_db_query("INSERT INTO products_options_validation SET `option_id` = '".(int)$_POST['oID']."', `products_id` = '".(int)$_POST['pID']."', settings = '".$jsondata."'");
						}
					break;
				}
			  }
		break;
		/****lc Audio/Video****/
		case 'saveaudio':
			$html = '';
			$checkQry = tep_db_query("select media_source from products_to_media where media_file = '".$_POST['audiofilename']."' and products_id = '".(int)$_GET['pID']."' AND media_type = 'AUDIO' ");
			if(tep_db_num_rows($checkQry) > 0){
				echo 'exist';
			} else {
				$insert = tep_db_query("insert into products_to_media set products_id = '".(int)$_GET['pID']."', media_type = 'AUDIO', media_title = '".$_POST['audiotitle']."', media_file = '".$_POST['audiofilename']."' ");
				$id = tep_db_insert_id();
				$html .= '
				<tr class="mediarow'.$id.'">
					<td class="table-col text-left">'.$_POST['audiofilename'].'</td>
					<td class="table-col text-left">AUDIO</td>
					<td class="table-col text-left">'.$_POST['audiotitle'].'</td>
					<td class="table-col text-right"><button class="btn-xs btn btn-danger" type="button" onclick="deleteMedia('.$id.')">Delete</button></td>
				</tr>';
				echo $html;
			}
		break;
		case 'savevideo':
			$html = '';
			$checkQry = tep_db_query("select media_source from products_to_media where media_file = '".$_POST['videofilename']."' and products_id = '".(int)$_GET['pID']."' AND media_type = 'VIDEO' ");
			if(tep_db_num_rows($checkQry) > 0){
				echo 'exist';
			} else {
				$insert = tep_db_query("insert into products_to_media set media_source = '".$_POST['videosource']."', products_id = '".(int)$_GET['pID']."', media_type = 'VIDEO', media_title = '".$_POST['videotitle']."', media_file = '".$_POST['videofilename']."' ");
				$id = tep_db_insert_id();
				if($_POST['videosource'] == 'YOUTUBE'){
					$icon = '<i class="fa fa-youtube" style="font-size: 20px;" aria-hidden="true"></i>';
				}elseif($_POST['videosource'] == 'VIMEO'){
					$icon = '<i class="fa fa-vimeo-square" style="font-size: 20px;" aria-hidden="true"></i>';
				}else{
					$icon = '';
				}
				$html .= '
				<tr class="mediarow'.$id.'">
					<td class="table-col text-left">'.$_POST['videofilename'].'</td>
					<td class="table-col text-left">VIDEO '.$icon.'</td>
					<td class="table-col text-left">'.$_POST['videotitle'].'</td>
					<td class="table-col text-right"><button class="btn-xs btn btn-danger" type="button" onclick="deleteMedia('.$id.')">Delete</button></td>
				</tr>';
				echo $html;
			}
		break;
		case 'unassignMedia':
			$del = tep_db_query("delete from products_to_media where id = '".(int)$_GET['mID']."'");
		break;
		case 'setpaymentsorting':
			if(count($_POST['position']) > 0){
			  foreach($_POST['position'] as $sort_order => $configuration_key){
				tep_db_query("UPDATE " . TABLE_CONFIGURATION . " SET sort_order` = '".$sort_order."' WHERE configuration_key = '".$configuration_key."'");
			  }
			}
		break;
		case 'setpagescategorysorting':
			if(count($_POST['position']) > 0){
			  foreach($_POST['position'] as $sort_order => $categories_id){
				tep_db_query("UPDATE pages_categories SET `categories_sort_order` = '".$sort_order."' WHERE categories_id = '".$categories_id."'");
			  }
			}
		break;
		case 'setpagesorting':
			if(count($_POST['position']) > 0 && $_POST['category_id'] > 0){
			  foreach($_POST['position'] as $sort_order => $pages_id){
				tep_db_query("UPDATE pages_to_categories SET `page_sort_order` = '".$sort_order."' WHERE categories_id = '".$_POST['category_id']."' and pages_id = '".$pages_id."'");
			  }
			}
		break;
		case 'setspecialsorting':
			if(count($_POST['position']) > 0){
			  foreach($_POST['position'] as $sort_order => $specials_id){
				tep_db_query("UPDATE specials SET `specials_sort` = '".$sort_order."' WHERE specials_id = '".$specials_id."'");
			  }
			}
		break;
		case 'setfeaturedsorting':
			if(count($_POST['position']) > 0){
			  foreach($_POST['position'] as $sort_order => $products_id){
				tep_db_query("UPDATE products SET `featured_sort` = '".$sort_order."' WHERE products_id = '".$products_id."'");
			  }
			}
		break;
		case 'setcategorysorting':
			if(count($_POST['position']) > 0){
			  foreach($_POST['position'] as $sort_order => $categories_id){
				tep_db_query("UPDATE categories SET `sort_order` = '".$sort_order."' WHERE categories_id = '".$categories_id."'");
			  }
			}
		break;
		case 'setproductsorting':
			if(count($_POST['position']) > 0 && $_POST['category_id'] > 0){
			  foreach($_POST['position'] as $sort_order => $products_id){
				tep_db_query("UPDATE products_to_categories SET `sort_order` = '".$sort_order."' WHERE categories_id = '".$_POST['category_id']."' and products_id = '".$products_id."'");
			  }
			}
		break;

		case 'setgroupingsorting':
		    if(count($_POST['position']) > 0){
		      foreach($_POST['position'] as $sort_order => $admin_files_id){
			tep_db_query("UPDATE admin_files SET `sort` = '".$sort_order."' WHERE admin_files_id = '".$admin_files_id."'");

		      }
			//Refresh the left menu cache
			tep_db_query("DELETE FROM cache_data WHERE cache_code LIKE'LEFT_MENU_DATA_%'");
		    }
		break;
		case 'setfilesorting':
		    if(count($_POST['position']) > 0){
		      foreach($_POST['position'] as $sort_order => $admin_files_id){
			tep_db_query("UPDATE admin_files SET `sort` = '".$sort_order."' WHERE admin_files_id = '".$admin_files_id."'");

		      }
			//Clean the left menu cache to show the effect instantly
			lc_admin_cache::clean_leftmenu_cache();

		    }
		break;
		case 'changefilestatus':
		    if(isset($_POST['admin_files_id']) && $_POST['admin_files_id'] > 0){
		    	tep_db_query("UPDATE admin_files SET `status` = '".$_POST['status']."' WHERE admin_files_id = '".$_POST['admin_files_id']."'");


			//Clean the left menu cache to show the effect instantly
			lc_admin_cache::clean_leftmenu_cache();
			echo $_POST['status'];exit;
		   }
		break;
		case 'changefilevisible':
		    if(isset($_POST['admin_files_id']) && $_POST['admin_files_id'] > 0){
		    	tep_db_query("UPDATE admin_files SET `visible` = '".$_POST['visible']."' WHERE admin_files_id = '".$_POST['admin_files_id']."'");

			//Clean the left menu cache to show the effect instantly
			lc_admin_cache::clean_leftmenu_cache();
			echo $_POST['visible'];exit;
		   }
		break;
		/****lc Audio/Video****/


	}
}

$action  = (isset($_POST['action']) && $_POST['action'] != '') ? $_POST['action'] : '';
$language_id = (isset($_POST['lid']) && $_POST['lid'] != '') ? $_POST['lid'] : '';
$type = (isset($_POST['type']) && $_POST['type'] != '') ? $_POST['type'] : '';
$id = (isset($_POST['id']) && $_POST['id'] != '') ? $_POST['id'] : '';
$permalink_name = (isset($_POST['name']) && $_POST['name'] != '') ? $_POST['name'] : '';
$permalink_val = (isset($_POST['pname']) && $_POST['pname'] != '') ? $_POST['pname'] : '';
if($action == 'get_slug'){
	if(trim($permalink_val) == '')
		$slug =  sanitize($permalink_name);
	else
		$slug = $permalink_val;
	if (is_numeric($slug)){
		$slug = 'pr-'.$slug;
	}
	$add_query = '';
	if($type == 'category' && $id > 0)
	  $checktype = 'categories_id';
	 // $add_query = " and categories_id != '".(int)$id."'";
	if($type == 'product' && $id > 0)
	  $checktype = 'products_id';
	  //$add_query = " and products_id != '".(int)$id."'";
	if($type == 'page' && $id > 0)
	   $checktype = 'products_id';
	  // $add_query = " and pages_id != '".(int)$id."'";

	if($type == 'page_category' && $id > 0)
	   $checktype = 'pages_categories_id';

    if($type == 'manufacturer' && $id > 0)
	   $checktype = 'manufacturers_id';

	if($type == 'form' && $id > 0)
	  $checktype = 'form_id';
	  //$add_query = " and form_id != '".(int)$id."'";
	if($type == 'topics' && $id > 0)
	  $checktype = 'topics_id';
	if($type == 'articles' && $id > 0)
	  $checktype = 'articles_id';

	if($type == 'gallery' && $id > 0)
	  $checktype = 'albums_id';

	$slug = check_dup_permalink($checktype, $id, $slug,1);
	/*
    $query = "select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug . "' and language_id = '" . (int)$language_id . "' and web_id = '" . (int)$web_id . "' ".$add_query."";
    $permalink_query = tep_db_query($query);
    if(tep_db_num_rows ($permalink_query) > 0){
		$suffix = rand(10, 1000);
     $slug = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    }
	*/
    echo  $slug;
}
if($action == 'check_slug'){
	 $slug =  sanitize($permalink_name);

	$add_query = '';
	if($type == 'category' && $id > 0)
	  $add_query = " and categories_id != '".(int)$id."'";

	if($type == 'product' && $id > 0)
	  $add_query = " and products_id != '".(int)$id."'";

	if($type == 'page_category' && $id > 0)
	  $add_query = " and pages_categories_id != '".(int)$id."'";

	if($type == 'manufacturer' && $id > 0)
	  $add_query = " and manufacturers_id != '".(int)$id."'";

	if($type == 'page' && $id > 0)
	  $add_query = " and pages_id != '".(int)$id."'";
	if($type == 'form' && $id > 0)
	  $add_query = " and form_id != '".(int)$id."'";
	if($type == 'topics' && $id > 0)
	  $add_query = " and topics_id != '".(int)$id."'";
 	if($type == 'articles' && $id > 0)
	  $add_query = " and articles_id != '".(int)$id."'";
 	if($type == 'gallery' && $id > 0)
	  $add_query = " and albums_id != '".(int)$id."'";

    $permalink_query = tep_db_query("select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug. "' and language_id = '" . (int)$language_id . "'  ".$add_query);
    if(tep_db_num_rows ($permalink_query) > 0){
		echo 'error';
    }else{
	   echo  $slug;
	}
}
if($action == 'get_manuf_slug'){
	$slug =  sanitize($permalink_name);

	$add_query = '';
	if($type == 'manufacturer' && $id > 0)
	  $add_query = " and manufacturers_id != '".(int)$id."'";


    $permalink_query = tep_db_query("select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug . "' and language_id = '" . (int)$language_id . "'  ".$add_query);
    if(tep_db_num_rows ($permalink_query) > 0){
		$suffix = rand(10, 1000);
     $slug = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    }

    echo  $slug;
}

if($action == 'get_testimonial_slug'){
	 $slug =  sanitize($permalink_name);

	$add_query = '';

	if($type == 'testimonialcat' && $id > 0)
	  $add_query = " and testimonial_cat_id != '".(int)$id."'";

    $permalink_query = tep_db_query("select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug. "' and language_id = '" . (int)$language_id . "'  ".$add_query);
    if(tep_db_num_rows ($permalink_query) > 0){
		echo 'error';
    }else{
	   echo  $slug;
	}

}
//articles topics
if($action == 'get_topic_slug'){
	$slug =  sanitize($permalink_name);
	$language_id = '1';
	$add_query = '';
	if($type == 'topics' && $id > 0)
	  $add_query = " and topics_id != '".(int)$id."'";
    $permalink_query = tep_db_query("select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug . "' and language_id = '" . (int)$language_id . "'  ".$add_query);
    if(tep_db_num_rows ($permalink_query) > 0){
		$suffix = rand(10, 1000);
     $slug = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    }
    echo  $slug;
}
if($action == 'check_topic_slug'){
	$slug =  sanitize($permalink_name);
	$language_id = '1';
	$add_query = '';
	if($type == 'topics' && $id > 0)
	  $add_query = " and topics_id != '".(int)$id."'";
    $permalink_query = tep_db_query("select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug. "' and language_id = '" . (int)$language_id . "'  ".$add_query);
    if(tep_db_num_rows ($permalink_query) > 0){
	   echo 'error';
    }else{
	   echo  $slug;
	}

}
//articles
if($action == 'get_article_slug'){
	$slug =  sanitize($permalink_name);
	$language_id = '1';
	$add_query = '';
	if($type == 'articles' && $id > 0)
	  $add_query = " and topics_id != '".(int)$id."'";
    $permalink_query = tep_db_query("select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug . "' and language_id = '" . (int)$language_id . "' ".$add_query);
    if(tep_db_num_rows ($permalink_query) > 0){
		$suffix = rand(10, 1000);
     $slug = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
    }
    echo  $slug;
}
if($action == 'check_article_slug'){
	$slug =  sanitize($permalink_name);
	$language_id = '1';
	$add_query = '';
	if($type == 'articles' && $id > 0)
	  $add_query = " and topics_id != '".(int)$id."'";
    $permalink_query = tep_db_query("select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug. "' and language_id = '" . (int)$language_id . "' ".$add_query);
    if(tep_db_num_rows ($permalink_query) > 0){
	   echo 'error';
    }else{
	   echo  $slug;
	}

}
if($action == 'update_permalink'){
   $slug =  sanitize($permalink_name);
	$upd_query = '';
	$add_query = '';
	$insert_query = '';
	if($type == 'category' && $id > 0){
	  $add_query = " and categories_id != '".(int)$id."'";
	  $upd_query = " and categories_id = '".(int)$id."'";
	  $insert_query = ",categories_id = '".(int)$id."'";
	}
	if($type == 'product' && $id > 0){
	  $add_query = " and products_id != '".(int)$id."'";
	  $upd_query = " and products_id = '".(int)$id."'";
	  $insert_query = ", products_id = '".(int)$id."'";
	}
	if($type == 'page' && $id > 0){
	 $add_query = " and pages_id != '".(int)$id."'";
	 $upd_query = " and pages_id = '".(int)$id."'";
	 $insert_query = ", pages_id = '".(int)$id."'";
	}
	if($type == 'form' && $id > 0){
	  $add_query = " and form_id != '".(int)$id."'";
	  $upd_query = " and form_id = '".(int)$id."'";
	  $insert_query = ", form_id = '".(int)$id."'";
	}
	if($type == 'topics' && $id > 0){
	  $add_query = " and topics_id != '".(int)$id."'";
	  $upd_query = " and topics_id = '".(int)$id."'";
	  $insert_query = ", topics_id = '".(int)$id."'";
    }
	if($type == 'gallery' && $id > 0){
	  $add_query = " and albums_id != '".(int)$id."'";
	  $upd_query = " and albums_id = '".(int)$id."'";
	  $insert_query = ", albums_id = '".(int)$id."'";
    }
 	if($type == 'articles' && $id > 0){
	  $add_query = " and articles_id != '".(int)$id."'";
	  $upd_query = " and articles_id = '".(int)$id."'";
	  $insert_query = ", articles_id = '".(int)$id."'";
 	}
 	if($type == 'testimonialcat' && $id > 0){
	  $add_query = " and testimonial_cat_id != '".(int)$id."'";
	  $upd_query = " and testimonial_cat_id = '".(int)$id."'";
	  $insert_query = ", testimonial_cat_id = '".(int)$id."'";
 	}

    $query = "select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug . "' and language_id = '" . (int)$language_id . "' ".$add_query."";
    $permalink_query = tep_db_query($query);
    if(tep_db_num_rows ($permalink_query) > 0){
       echo 'MSG-DUPLICATE';
    }else{
    	$query_chk = tep_db_query("SELECT permalink_id from " . TABLE_PERMALINK . " where language_id = '" . (int)$language_id . "' ".$upd_query."");
		if(tep_db_num_rows($query_chk) > 0){
			$update_query = "UPDATE " . TABLE_PERMALINK . " SET permalink_name = '" .$slug . "' WHERE  language_id = '" . (int)$language_id . "' ".$upd_query."";
			tep_db_query($update_query);
		}else{
			$insert_query = "INSERT INTO  " . TABLE_PERMALINK . " SET permalink_name = '" .$slug . "' , language_id = '" . (int)$language_id . "' ".$insert_query."";
			tep_db_query($insert_query);
		}
    	//echo 'MSG-SUCCESS';
    	echo $slug;
    }
	  exit;
}
if($action == 'update_manuf_permalink'){
   $slug =  sanitize($permalink_name);
	$upd_query = '';
	$add_query = '';

 	if($type == 'manufacturer' && $id > 0){
	  $add_query = " and manufacturers_id != '".(int)$id."'";
	  $upd_query = "  manufacturers_id = '".(int)$id."'";
 	}

    $query = "select permalink_id from " . TABLE_PERMALINK . " where permalink_name = '" .$slug . "'  ".$add_query."";
    $permalink_query = tep_db_query($query);
    if(tep_db_num_rows ($permalink_query) > 0){
       echo 'MSG-DUPLICATE';
    }else{
    	echo $update_query = "UPDATE " . TABLE_PERMALINK . " SET permalink_name = '" .$slug . "' WHERE ".$upd_query."";
    	tep_db_query($update_query);
    	echo 'MSG-SUCCESS';
    }
	  exit;
}
?>
