<?php
// $Id: updateblock.inc.php 62 2006-08-23 14:34:37Z yosha_01 $

if( substr( XOOPS_VERSION , 6 , 3 ) < 2.1 ) {

	// Keep Block option values when update (by nobunobu)
	global $xoopsDB;
	$query = "SELECT mid FROM ".$xoopsDB->prefix('modules')." WHERE dirname=".$xoopsDB->quoteString($modversion['dirname'])." ";
	$result = $xoopsDB->query($query);
	$record= $xoopsDB->fetcharray($result);
	$local_msgs = array(); //Notice [PHP]: Undefined variable: local_msgs in file modules/discuss/include/updateblock.inc.php line 127
	if ($record) {
		$mid = $record['mid'];
		$count = count($modversion['blocks']);
		/* $sql = "SELECT * FROM ".$xoopsDB->prefix('newblocks')." WHERE mid=".$mid." AND block_type ='D'";
		$fresult = $xoopsDB->query($sql);
		$n_funcnum = $count;
		while ($fblock = $xoopsDB->fetchArray($fresult)) {
			$bnum = 0;
			for ($i = 1 ; $i <= $count ; $i++) {
				if (($modversion['blocks'][$i]['file'] == $fblock['func_file']) and ($modversion['blocks'][$i]['show_func'] == $fblock['show_func'])) {
					$bnum = $i;
					break;
				}
			}
			if($bnum) {
				$n_funcnum++;
				$modversion['blocks'][$n_funcnum]['file'] = $fblock['func_file'];
				$modversion['blocks'][$n_funcnum]['name'] = $fblock['name'];
				$modversion['blocks'][$n_funcnum]['description'] = $fblock['name'];
				$modversion['blocks'][$n_funcnum]['show_func'] = $fblock['show_func'];
				$modversion['blocks'][$n_funcnum]['edit_func'] = $fblock['edit_func'];
				$modversion['blocks'][$n_funcnum]['template'] = $fblock['template'];
				if ($fblock['options']) {
					$old_vals=explode("|",$fblock['options']);
					$def_vals=explode("|",$modversion['blocks'][$bnum]['options']);
					if (count($old_vals) == count($def_vals)) {
						// the number of parameters is not changed
						$modversion['blocks'][$n_funcnum]['options'] = $fblock['options'];
						$local_msgs[] = "Option's values of the cloned block <b>".$fblock['name']."</b> will be kept. (value = <b>".$fblock['options']."</b>)";
					} else if (count($old_vals) < count($def_vals)){
						// the number of parameters is increased
						for ($j=0; $j < count($old_vals); $j++) {
							$def_vals[$j] = $old_vals[$j];
						}
						$modversion['blocks'][$n_funcnum]['options'] = implode("|",$def_vals);
						$local_msgs[] = "Option's values of the cloned block <b>".$fblock['name']."</b> will be kept and new options are added. (value = <b>".$modversion['blocks'][$fblock['func_num']]['options']."</b>)";
					} else {
						$modversion['blocks'][$n_funcnum]['options'] = implode("|",$def_vals);
						$local_msgs[] = "Option's values of the cloned block <b>".$fblock['name']."</b> will be reset to the default, because of some decrease of options. (value = <b>".$modversion['blocks'][$n_funcnum]['options']."</b>)";
					}
				}
				$sql = "UPDATE ".$xoopsDB->prefix('newblocks')." SET func_num='$n_funcnum' WHERE mid=".$mid." AND bid='".$fblock['bid']."'";
				$iret = $xoopsDB->query($sql);

			}
		} */
		
		$sql = "SELECT * FROM ".$xoopsDB->prefix('newblocks')." WHERE mid=".$mid." AND block_type <>'D' AND func_num > $count";
		$fresult = $xoopsDB->query($sql);
		while ($fblock = $xoopsDB->fetchArray($fresult)) {
			$local_msgs[] = "Non Defined Block <b>".$fblock['name']."</b> will be deleted";
			$sql = "DELETE FROM ".$xoopsDB->prefix('newblocks')." WHERE bid='".$fblock['bid']."'";
			$iret = $xoopsDB->query($sql);
		}
		
		for ($i = 1 ; $i <= $count ; $i++) {
			$sql = "SELECT name,options,template FROM ".$xoopsDB->prefix('newblocks')." WHERE mid=".$mid." AND func_num=".$i." AND show_func='".addslashes($modversion['blocks'][$i]['show_func'])."' AND func_file='".addslashes($modversion['blocks'][$i]['file'])."'";
			$fresult = $xoopsDB->query($sql);
			$fblock = $xoopsDB->fetchArray($fresult);
			if ( isset( $fblock['options'] ) ) {
				$old_vals=explode("|",$fblock['options']);
				$def_vals=explode("|",$modversion['blocks'][$i]['options']);
				//update duplicatable modules name
				if( preg_match( '/^(\D+)(\d*)$/' , $old_vals[0]) ) {
	
					if (count($old_vals) == count($def_vals)) {
						$modversion['blocks'][$i]['options'] = $fblock['options'];
						$local_msgs[] = "Option's values of the block <b>".$fblock['name']."</b> will be kept. (value = <b>".$fblock['options']."</b>)";
					} else if (count($old_vals) < count($def_vals)){
						for ($j=0; $j < count($old_vals); $j++) {
							$def_vals[$j] = $old_vals[$j];
						}
						$modversion['blocks'][$i]['options'] = implode("|",$def_vals);
						$local_msgs[] = "Option's values of the block <b>".$fblock['name']."</b> will be kept and new option(s) are added. (value = <b>".$modversion['blocks'][$i]['options']."</b>)";
					} else {
						$local_msgs[] = "Option's values of the block <b>".$fblock['name']."</b> will be reset to the default, because of some decrease of options. (value = <b>".$modversion['blocks'][$i]['options']."</b>)";
					}
	
				}
			}
			//HACK by domifara force change template
			if ( isset( $fblock['template'] ) ) {
					//template update			
					$old_template_vals=$fblock['template'];
					$def_template_vals=$modversion['blocks'][$i]['template'];
				if ( $old_template_vals != $def_template_vals ) {
					$update_sql = "UPDATE ".$xoopsDB->prefix('newblocks')." SET template = ".$xoopsDB->quoteString($modversion['blocks'][$i]['template']) ." WHERE mid=".$mid." AND func_num=".$i." AND show_func='".addslashes($modversion['blocks'][$i]['show_func'])."' AND func_file='".addslashes($modversion['blocks'][$i]['file'])."'";
					$upret = $xoopsDB->query($update_sql);//nocheck force
					$local_msgs[] = "Block template values of the block <b>".$fblock['name']."</b> will be reset to this template. (value = <b>".htmlspecialchars($modversion['blocks'][$i]['template'], ENT_QUOTES)."</b>)";
				}			
			}			
		}
		for ($i = 1 ; $i <= $count ; $i++) {
			$sql = "SELECT * FROM ".$xoopsDB->prefix('newblocks')." WHERE mid=".$mid." AND func_num=".$i." AND (show_func<>".$xoopsDB->quoteString($modversion['blocks'][$i]['show_func'])." OR func_file<>".$xoopsDB->quoteString($modversion['blocks'][$i]['file']).")";
			$fresult = $xoopsDB->query($sql);
			while ($fblock = $xoopsDB->fetchArray($fresult)) {
				$local_msgs[] = "Non Defined Block function <b>".$fblock['name']."</b> will be deleted for upadte change";
				$sql = "DELETE FROM ".$xoopsDB->prefix('newblocks')." WHERE bid='".$fblock['bid']."'";
				$iret = $xoopsDB->query($sql);
			}
		}
		$sql = "SELECT tpf.* FROM ".$xoopsDB->prefix('tplfile')." tpf LEFT JOIN ".$xoopsDB->prefix('newblocks')." b ON tpf.tpl_refid=b.bid WHERE tpf.tpl_module=".$xoopsDB->quoteString($modversion['dirname'])." AND tpf.tpl_type = 'block' AND b.bid IS NULL";
		$tpresult = $xoopsDB->query($sql);
		while ($btemplate = $xoopsDB->fetchArray($tpresult)) {
			$local_msgs[] = "Overlap trash template <b> tpl_id".htmlspecialchars($btemplate['tpl_id'], ENT_QUOTES)." : ".htmlspecialchars($btemplate['tpl_file'], ENT_QUOTES)."</b> will be deleted for upadte change";
			$sql = "DELETE FROM ".$xoopsDB->prefix('tplsource')." WHERE tpl_id='".$btemplate['tpl_id']."'";
			$iret = $xoopsDB->query($sql);
			$sql = "DELETE FROM ".$xoopsDB->prefix('tplfile')." WHERE tpl_id='".$btemplate['tpl_id']."'";
			$iret = $xoopsDB->query($sql);
		}
	}

	global $msgs , $myblocksadmin_parsed_updateblock ;
	if( ! empty( $msgs ) && empty( $myblocksadmin_parsed_updateblock ) ) {
		$msgs = array_merge( $msgs , $local_msgs ) ;
		$myblocksadmin_parsed_updateblock = true ;
	}
}
?>