<?php
	FNTH_import( "xmlprocess" );

	class ParseArticleToDb extends FNTHXmlProcess {
		protected $bInPageBreak = false;
		protected $iArticleNum = 0;

		public function parseHtmlTag( $tag ) {
			switch ($tag["tagtype"]) {
			case "a":
				if (!isset( $tag["href"] )) break;
				$href = $tag["href"];
				$href = FNTH_strip_quotes( $href , "'" );
				$href = FNTH_strip_quotes( $href , "\"" );

				$query = sprintf( "select article_id from articles where is_url_used and is_active and not is_deleted and article_url='%s'",
					FNTH_db_addslashes( $href ) );
				$article_id = FNTH_db_get_field( $query , "article_id" );
				if ($article_id) {
					$this->iArticleNum = $article_id;
					return false;
				}
				$query = sprintf( "select article_id from larticles where is_url_used and is_active and not is_deleted and larticle_url='%s'",
					FNTH_db_addslashes( $href ) );
				$article_id = FNTH_db_get_field( $query , "article_id" );
				if ($article_id) {
					$this->iArticleNum = $article_id;
					return false;
				}
				break;
			case "/a":
				if ($this->iArticleNum) {
					$this->iArticleNum = 0;
					return false;
				}
				$this->iArticleNum = 0;
				break;
			case "div":
				if (!isset( $tag["class"] )) {
					$this->bInPageBreak = false;
					break;
				}
				if ($tag["class"]!="\"divpagebreak\"") {
					$this->bInPageBreak = false;
					break;
				}
				$this->bInPageBreak = true;
				break;
			case "/div":
				$this->bInPageBreak = false;
				break;
			default:
				$this->bInPageBreak = false;
				break;
			}
			return $tag;
		}

		public function parseData( $string ) {
			if ($this->iArticleNum) {
				// article replacement
				$string = sprintf( "[{article id=%d}]%s[{/article}]" , $this->iArticleNum , trim( $string ) );
			} else if ($this->bInPageBreak) {
				// pagebreak trimming
				$string = trim( $string );
			}
			return $string;
		}

	}
?>
