<?php
$sql_sub = "DELETE FROM ".$module."_links WHERE id_module = :id ";
$result_sub = $conn->prepare($sql_sub);
$result_sub->bindValue(':id', $id, PDO::PARAM_INT);
$result_sub->execute();
if (isset($links_data) && is_array($links_data)){
	$next = 0;
	foreach ($links_data AS $key => $value){
		if (isset($value['link']) && $value['link']){
	  	$linkstart = substr($value['link'], 0, 1);
	  	if ($linkstart != "/"){
				$linkstarts = substr($value['link'], 0, 8);
				$linkstart = substr($value['link'], 0, 7);
				if ($linkstarts != "https://"){
					if ($linkstart != "http://" AND $linkstart != "mailto:") $value['link'] = "http://".$value['link'];
				}
			}
			if ($value['link'] == "http://" OR $value['link'] == "https://") unset($value['link']);
			
			if (isset($value['link']) && $value['link']){
				$next++;
				$sql_tmp = "INSERT INTO ".$module."_links SET 
										id_module = :id, 
										sortorder = :sortorder, 
										link = :link ";
				if (isset($value['linktext']) && $value['linktext']) $sql_tmp .= ", linktext = :linktext ";
				else $sql_tmp .= ", linktext = NULL ";
				
				$result_tmp = $conn->prepare($sql_tmp);
				$result_tmp->bindValue(':id', $id, PDO::PARAM_INT);
				$result_tmp->bindValue(':sortorder', $next, PDO::PARAM_INT);
				$result_tmp->bindValue(':link', $value['link'], PDO::PARAM_STR);
				if (isset($value['linktext']) && $value['linktext']) $result_tmp->bindValue(':linktext', $value['linktext'], PDO::PARAM_STR);
				
				$result_tmp->execute();
			}
		}
	}
	$links_data = array();
}