<?php
 
// table_edit2.inc.php, 0.2 2006/09/09 taru        plugin
 
define('PLUGIN_TABLE_EDIT_TEXT_SIZE',  68);
define('PLUGIN_TABLE_EDIT_PICTURE', TRUE);

function plugin_table_edit2_convert()
{
	global $script, $vars, $edit_auth_pages;
	static $number = array();
	$line_count = 1;
//	$table_find = FALSE;
//	if (empty($user)) return '';
	$page = isset($vars['page']) ? $vars['page'] : '';
	if (! isset($number[$page])) $number[$page] = 1;
	$count = $number[$page]++;
	$r_page = rawurlencode($page);
	$opt = array(
		'edit' => 'on',
		'auth' => '',
		'table' => '',
	);

	$auth_chk = auth::check_auth();
	foreach($edit_auth_pages as $key=>$val){
		if (preg_match($key, $page)){
			$opt['edit'] = 'off';
			if(! empty($auth_chk)){
				$opt['edit'] = 'on';
			}
		}
	}
//	if(is_editable($page)){	}

	$body = '';
//	$body2 = '';

	$args = func_get_args();
	$arg = array_pop($args);
	foreach ($args as $opt_key) {
		list($key, $val) = explode('=', strtolower($opt_key));
		$opt[$key] = htmlspecialchars($val);
	}

	// if (PKWK_READONLY) {
	if (auth::check_role('readonly') || is_freeze($page)) {
		$opt['edit'] = 'off';
	}

	$arg = preg_replace(array("[\\r|\\n]","[\\r]"), array("\n","\n"), $arg);
	$args = explode("\n", $arg);
//	$args_count = count($args);

	foreach ($args as $args_line) {
		if (preg_match('/^\|(.+)\|([hHfFcC]?)$/', $args_line, $matches) && $opt['edit'] != 'off'){
//			if ($matches[2] == "c") {	//C off : c on
//				$matches[2] = "h";
//				$body .= '|' . $matches[1] . "|RIGHT:|c\n";
//			}
			$body .= '|' . $matches[1] . "|";
			if(PLUGIN_TABLE_EDIT_PICTURE === TRUE ){
				$body .= "&table_edit2(edit_mod=show,table_num=$count,line_count=$line_count);"
					  . "&table_edit2(edit_mod=tr,table_num=$count,line_count=$line_count);";
			} else {
				$body .= "[[edit:" . $script
					 . "?plugin=table_edit2&refer=$r_page&edit_mod=show&table_num="
					 . $count . "&line_count=" . $line_count . "]][[add:" . $script
					 . "?plugin=table_edit2&refer=$r_page&edit_mod=tr&table_num="
					 . $count . "&line_count=" . $line_count . "]]";
			}
			$body .= "|" . $matches[2] . "\n";
		$line_count++;
		} else {
			$body .= $args_line . "\n";
		}
	}
 
	return convert_html($body);
}

function plugin_table_edit2_inline()
{
	global $script, $vars, $edit_auth_pages;
	$page = isset($vars['page']) ? $vars['page'] : '';
	$r_page = rawurlencode($page);
	$body = '';
	$opt = array(
		'edit_mod' => '',
		'table_num' => '',
		'line_count' => '',
	);
	$args = func_get_args();
	foreach ($args as $opt_key) {
		list($key, $val) = explode('=', strtolower($opt_key));
		$opt[$key] = htmlspecialchars($val);
	}
	if ($opt['edit_mod'] == 'show'){
		$image_png = 'paraedit.png" width="9" height="9" alt="" title="edit"';
	} else if ($opt['edit_mod'] == 'tr'){
		$image_png = 'mini_add.png" width="8" height="8" alt="" title="addition"';
	}
	$body .= '<a href="' . $script . "?plugin=table_edit2&amp;refer=$r_page&amp;edit_mod="
		 . $opt['edit_mod'] . "&amp;table_num=" . $opt['table_num'] . "&amp;line_count=" . $opt['line_count']
		 . '"><img src="' . IMAGE_URI . 'plus/' . $image_png . ' /></a>';
	return $body;
}
function plugin_table_edit2_action()
{
	global $vars, $post, $script;
	$line_count = 1;

	$table_num = 	$vars['table_num'];
	$page = 		$vars['refer'];

	if (auth::check_role('readonly')) die_message('PKWK_READONLY prohibits editing');
	if (! ereg("^($script)",$_SERVER['HTTP_REFERER'])) return;
	$args  = get_source($page);
	static $count = 0;
	$source_s = '';
	$body = '';
	$title = '';

	foreach ($args as $args_line) {
//		if (preg_match('/^#table_edit2([^\(\{]+)(?:\(([^\r]*)\))?(\{*)/', $args_line, $matches)){ //2
		if(preg_match('/^#([^\(\{]+)(?:\(([^\r]*)\))?(\{*)/', $args_line, $matches) && $matches[1] == 'table_edit2') {
			$table_find = 1;
			$count++;
			if( $line_count === 1 && $count == $table_num ){
				if(! preg_match('/(auth_check_off)/i',$matches[2])){
					check_editable($page, true, true);
				}
			}
			$end_line = strlen($matches[3]);
		}
		if (preg_match('/^\}{' . $end_line . '}/', $args_line)) {
			$table_find = 0;
		}
		if($table_find && $table_num == $count){
			if (preg_match('/^\|(.+)\|([hHfFcC]?)$/', $args_line, $match_line)){
				if($vars['edit_mod'] == 'show'){					//show
					if($line_count === 1){
						if( strtolower( $match_line[2] ) == 'c' ){
							$line_header = 2;
						} else {
							$line_header = 1;
						}
					}
					if($line_count === $line_header){
						$match_t = explode("|", $match_line[1]);
						$cell_count = 1;
						foreach ($match_t as $cell){
							$table_header[$cell_count] = $cell;
							$cell_count++;
						}
					} 
				}
				if($vars['line_count'] == $line_count){
					if($vars['edit_mod'] == 't_edit'){				//t_edit
						if(isset($vars['write'])){
							$source_s .= '|';
							for ($i = 1;$i < $vars['cell_count'];$i++){
								$source_s .= $vars['cell' . $i] . '|';
							}
							$source_s .= $match_line[2] . '
';
						} else if(isset($vars['cancel'])){
								return;
						} else if(isset($vars['delete'])){
						}
					} 
					if($vars['edit_mod'] == 'tr'){				//tr
						$source_s .= $args_line;
						$match_t = explode("|", $match_line[1]);
						$source_s .= '|';
						foreach ($match_t as $cell){
							$source_s .= '|';
						}
						$source_s .= $match_line[2] . '
';
					}
					if($vars['edit_mod'] == 'show'){				//show
						$match_t = explode("|", $match_line[1]);
						$title = 'table_edit2 ' . $page;
						$body = '<h3>' . 'table=' . $table_num . ' line=' . $line_count . '</h3>';
						$body .= '<form action="' . $script . '" method="post">';
						$body .= ' <table cellspacing="0" cellpadding="2" class="style_table">';
						$cell_count = 1;
						foreach ($match_t as $cell){
							$body .= '  <tr><td>' . $table_header[$cell_count] . '(' . $cell_count . ')</td><td class="style_td">';
							$body .= '   <input type=text name="cell' . $cell_count . '" value="' . $cell . '" size="' . PLUGIN_TABLE_EDIT_TEXT_SIZE . '" /></td></tr>';
							$cell_count++;
						}
						$body .= <<<EOD
  <tr><td  colspan="2">
   <input type="hidden" name="plugin"     value="table_edit2" />
   <input type="hidden" name="refer"      value="$page" />
   <input type="hidden" name="table_num"  value="$table_num" />
   <input type="hidden" name="line_count" value="$line_count" />
   <input type="hidden" name="cell_count" value="$cell_count" />
   <input type="hidden" name="edit_mod"   value="t_edit" />
   <input type="submit" name="write" value="edit" />
   <input type="submit" name="cancel" value="cancel" />
   <input type="submit" name="delete" value="delete" />
  </td></tr>
 </table>
</form>
EOD;
						break;
					}
				} else {
					$source_s .= $args_line;
				}
				$line_count++;
			} else {
				$source_s .= $args_line;
			}
		} else {
			$source_s .= $args_line;
		}
	}

	if($vars['edit_mod'] == 't_edit' || $vars['edit_mod'] == 'tr'){
		page_write($page, $source_s);
	}

	$post['refer'] = $page;

	return array('msg'=>$title, 'body'=>$body);
}

?>
