parser

Написать ответ на текущее сообщение

 

 
   команды управления поиском

Ответ

dRmx 27.08.2003 02:07

Значит, выполняю первый вариант, меняю
^что-либо:sql{ ... }
на
^MAIN:dbconnect{^что-либо:sql{ ... }}
. Ошибки. Ищу. Исправляю. Так несколько раз. При том, что в корневой auto.p написал:
@dbconnect[code] 
^connect[$connect_string]{$code}
Правильно ли я в нескольких случаях закрыл скобки и вообще вставил ^MAIN:dbconnect точно не уверен. Но ругаться браузер насчет connect-а перестал. Радует. Прохожу еще несколько ошибок. Вроде исправляю. Очередная непроходимая связана со string method not found status.
^if((def $params.request && $params.request is hash && def $params.request.$path_param) || $poll_info.status eq "closed")
Приведу все-таки код. Может кому-нибудь будет не лень просмотреть и помочь мне привести в рабочий вид.
##############################################
## Poll creating and supporting class
@CLASS
poll
##############################################

## main constructor
@create[pi_table_in;pa_table_in;extra][extra]
## shit class path
$cl_path[/classes/poll]
## defining poll_info table
$pi_table[$pi_table_in]
## defining poll_answers table
$pa_table[$pa_table_in]
## defining date used in methods
$date_now[^date::now[]]
## adding extra methods
^if(def $extra){
	^try{
		^if(-f $extra){
			$extra[^file::load[text;$extra]]
			^process{^taint[as-is][$extra.text]}
		}{
			^throw[parser.runtime;poll;Extra methods file not found]
		}
	}{
		^throw[parser.compile;poll;Extra methods compiling error. Check your extra methods code]
	}
}
######## end of @create[] constructor ########

##############################################
## creates and runs-in new poll
@run[params][params;poll_id;answer_keys;id]
## parameters checking and errors detecting
^check_params[$params;run]
## question checking
^if(!def $params.question){
	^throw[parser.runtime;poll;Poll question not defined]
}
## answers checking
^if(!def $params.answers || !($params.answers is hash)){
	^throw[parser.runtime;poll;Answers parameter contains illegal value]
}
## inserting poll into $pi_table
^MAIN:dbconnect{
^void:sql{
	INSERT INTO 
		$pi_table (
			question,
			type,
			default_value,
			button_text,
			status,
			dt_started
		)
	VALUES
		(
		'$params.question',
		'^if($params.type eq "checkbox"){$params.type}{radio}',
		'^params.default.int(1)',
		'^if(def $params.button_text){$params.button_text}{Ответить}',
		'^if($params.status eq "active"){$params.status}{closed}',
		'^date_now.sql-string[]'
		)
}
}
## extracting last_insert_id()
$poll_id[^last_insert_id[0]]
## sorting income answers
$answer_keys[^params.answers._keys[]]
^answer_keys.sort($answer_keys.key)
## inserting answers into $pa_table
^answer_keys.menu{
	$id[$answer_keys.key]
	^if(!($params.answers.$id is hash)){
		^throw[parser.runtime;poll;Answers must be defined as a hash]
	}
	^if(def $params.answers.$id.answer){
		^MAIN:dbconnect{
		^void:sql{
			INSERT INTO
				$pa_table (
					poll_id,
					answer,
					vote_count
				)
			VALUES
				(
				'$poll_id',
				'$params.answers.$id.answer',
				'^params.answers.$id.vote_count.int(0)'
				)
			}
		}
	}
}
## returns created poll's id and it's status
$result[$poll_id[$poll_id]$status[^MAIN:dbconnect{^string:sql{SELECT status FROM $pi_table WHERE poll_id = $poll_id}}[$.default{closed}]]]
######## end of @run[] method ########

######################################
## draws the poll body on your page
@show[params][params;poll_id;path_param;cookie;expires;poll;action;answers]
## parameters checking
^check_params[$params;show]
## defining poll_id
^if(def $params.poll_id && $params.poll_id eq "last_active"){
	$poll_id[^last_insert_id[0]]
}{
	$poll_id[^params.poll_id.int(0)]
}
## extracting poll from database
$poll_info[^extract[$poll_id;info]]
## path parameter defining
$path_param[^if(def $params.path_param){^taint[xml][$params.path_param]}{vote_for}]
## detecting vote
^if((def $params.request && $params.request is hash && def $params.request.$path_param) || $poll_info.status eq "closed"){
## detecting restriction
	^if(def $params.restrict && $params.restrict is hash){
## cookie name defining
		^if(def $params.restrict.cookie){
			$cookie[${poll_id}_$params.restrict.cookie]
		}{
			$cookie[${poll_id}_poll]
		}
## expire date defining
		^if(def $params.restrict.expires){
			$expires[^params.restrict.expires.int(1)]
		}{
			$expires[session]
		}
## setting cookie
		^if(!def $cookie:$cookie){
			^if($expires eq "session"){
				$cookie:$cookie[$expires]
			}{
				$cookie:$cookie($expires)
			}
		}{
## voting restricted
			$restricted(1)
		}
	}
## updating vote counts
	^if(!$restricted && def $params.request.$path_param && $params.request.$path_param is table && $poll_info.status ne "closed"){
		^params.request.$path_param.menu{
			^MAIN:dbconnect{
			^void:sql{
				UPDATE	$pa_table
				SET	vote_count = vote_count+1
				WHERE	answer_id = '^params.request.$path_param.field.int(0)'
				AND	poll_id = '$poll_id'
			}
		}
	}
}
## user voted or poll closed
	$action[result]
}{
## active poll, no vote submitted
	$action[answer]
}
## hashing poll
$poll[^poll_info.hash[poll_id]]
## extracting answers
$answers[^extract[$poll_id;answers]]
## hashing answers
$answers[^answers.hash[answer_id]]
## inserting answers into $poll hash
$poll.$poll_id.answers[$answers]
## adding path parameter
$poll.$poll_id.path_param[$path_param]
## extracting total votes for this poll
$poll.$poll_id.total[^MAIN:dbconnect{^string:sql{SELECT SUM(vote_count) FROM $pa_table WHERE poll_id = '$poll_id' GROUP BY poll_id}}[$.default{0}]]
	^if($poll.$poll_id.total eq "0"){$poll.$poll_id.total[100]}
## rectriction defining
$poll.$poll_id.restricted[^if($restricted){yes}{no}]
## getting poll view
^if(def $params.skin){
## processing poll skin
	^transform[$params.skin;^printPoll[$poll;$action]]
}{
## printing poll
	^printPoll[$poll;$action]
}
######## end of @show[] method ########

#######################################
## updates existing poll
@update[params][params;poll_id]
## parameters checking
^check_params[$params;update]
## poll_id detecting
^if(!def $params.poll_id){
	^throw[parser.runtime;poll;Nothing to update, check your ^$.poll_id[]]
}{
	$poll_id(^params.poll_id.int(0))
	^if($poll_id <= 0){
		^throw[parser.runtime;poll;Invalid ^$.poll_id[]]
	}
}
## updating poll info
^if(def $params.question || def $params.type || def $params.status || def $params.default){
	^MAIN:dbconnect{
	^void:sql{
		UPDATE	$pi_table
		SET	question = ^if(def $params.question){'$params.question'}{question},
			type = ^if(def $params.type){'^if($params.type eq "checkbox"){checkbox}{radio}'}{type},
			default_value = ^if(def $params.default){'^params.default.int(1)'}{default_value},
			button_text = ^if(def $params.button_text){'$params.button_text'}{button_text},
			status = ^if(def $params.status){^if($params.status eq "active"){'active', dt_ended = ''}{'closed', dt_ended = '^date_now.sql-string[]'}}{status}
		WHERE	poll_id = '$poll_id'
		}
	}
}
## updating answers
^if(def $params.answers && $params.answers is hash){
	^params.answers.foreach[key;value]{
		^if($value is hash){
			^if(^key.mid(0;3) eq "new"){
## adding new answer to poll
				^MAIN:dbconnect{
				^void:sql{
					INSERT INTO
						$pa_table
							(
							poll_id,
							answer,
							vote_count
							)
					VALUES
						(
						'$poll_id',
						'$value.answer',
						'^value.vote_count.int(0)'
						)
				}
			}{
## deleting answers
				^if(^key.mid(0;6) eq "delete"){
					^void:sql{
						UPDATE	$pa_table
						SET	poll_id = 0
						WHERE	answer_id = ^value.answer.int(0)
						AND	poll_id = '$poll_id'
					}
				
				}{
## updating exist answer
					^void:sql{
						UPDATE	$pa_table
						SET	answer = ^if(def $value.answer){'$value.answer'}{answer},
							vote_count = ^if(def $value.vote_count){'^value.vote_count.int(0)'}{vote_count}
						WHERE	answer_id = ^key.int(0)
						AND	poll_id = '$poll_id'
						}
					}
				}
			}
		}
	}
}
######## end of @update[] method ########

##############################################
## poll_info and poll_answers extractor
@extract[poll_id;type][poll_id;type]
^if(def $poll_id){
	^switch[$type]{
		^case[info]{$result[^MAIN:dbconnect{
					^table::sql{
						SELECT
							poll_id,
							question,
							type,
							default_value,
							button_text,
							status,
							dt_started,
							dt_ended
						FROM	$pi_table
					^if($poll_id ne "all"){
						WHERE	poll_id = '^poll_id.int(0)'
						}
					}
				}]
		}
		^case[answers]{$result[^MAIN:dbconnect{
					^table::sql{
						SELECT
							answer_id,
							poll_id,
							answer,
							vote_count
						FROM	$pa_table
					^if($poll_id ne "all"){
						WHERE	poll_id = '^poll_id.int(0)'
						}
					}
				}]
		}
		^case[DEFAULT]{$result[]}
	}
}{
	$result[]
}
######## end of @extract[] method ############

##############################################
@last_insert_id[default][default]
$result[^MAIN:dbconnect{^string:sql{SELECT MAX(poll_id) FROM $pi_table}[$.default{$default}]}]
######## end of @last_insert_id[] method ########

##############################################
## parameters hash checker
@check_params[params;method][params;method]
^if(!def $params || !($params is hash)){
	^throw[parser.runtime;poll;Defined none of parameters while calling <b>^#40$method[]</b> method]
}
######## end of @check_params[] method #######

#######################################
## poll xml code generator
@printPoll[params;action][params;action]
^try{
## code generating
	^params.foreach[p_id;poll]{
	     <poll id="$p_id" status="$poll.status" type="$poll.type" default="$poll.default_value" restricted="$poll.restricted">
		<question>$poll.question</question>
		<${action}s total="$poll.total">
		^poll.answers.foreach[a_id;answer]{
			<$action id="$a_id" vote_count="$answer.vote_count">
				$answer.answer
			</$action>
		}
		</${action}s>
		<date started="$poll.dt_started" closed="$poll.dt_ended" />
		<form path="$poll.path_param" action="$request:uri" button="$poll.button_text" />
	     </poll>
	}
}{
	^throw[parser.compile;poll;printPoll method compiling error.Check your parameters]
}
######## end of @printPoll[] method ###

#######################################
## poll xml xslt transformator
@transform[xslt_file;xml_code][xslt_file;xml_code;document;transformedDoc]
$document[^xdoc::create{<?xml version="1.0" encoding="$response:charset"?> 
<document>
	^untaint{$xml_code}
</document>}] 
$transformedDoc[^document.transform[$xslt_file]]
^transformedDoc.string[
		$.method[html]
		]
######## end of @transform[] method ###
Глупо заявлять, что класс писал не я.