<?php
	require_once( $GLOBALS["site_root"]."/includes/models/Cache.inc.php" );

	class ArticlecategoriesModel {

		protected function renderArticleCategoryOption( $data ) {
			$num = 0 + ($data["article_category_level"]-1)*3;
			return str_repeat( "&nbsp;" , $num ) . $data["article_category_name"];
		}

		public function getArticleCategoryOptions( $choose = true , $original = true ) {
			$mc = CacheModel::getCacheNameOnParams( "article_category_options" ,
				array( "lang" => $GLOBALS["language"], "choose" => $choose , "original" => $original ) );
			$retval = CacheModel::getCacheValueOnString( $mc );
			if (is_array( $retval )) return $retval;
			$retval = array();
			if ($choose) $retval[""]=__("Choose");
			if ($original) {
				$query = sprintf( "select * from article_categories order by article_category_meta_l" );
			} else {
				$query = sprintf( "select
					ac.article_category_id,
					ac.article_category_parentid,
					ac.article_category_level,
					ac.article_category_ordering,
					acl.article_category_title as article_category_name
				from article_categories as ac,article_category_langdefs as acl
				where
					ac.article_category_id=acl.article_category_id and
					acl.site_language_id=%d
				order by ac.article_category_meta_l",$GLOBALS["langcode"] );
			}
			$data = FNTH_db_get_resultset( $query );
			foreach ($data as $row)
				$retval[$row["article_category_id"]]=$this->renderArticleCategoryOption( $row );
			CacheModel::cacheValueOnString( $mc , $retval );
			return $retval;
		}

		public function getArticleCategoryIdFromName( $article_category_name ) {
			if (!is_array( $article_category_name )) {
				$query = sprintf( "select article_category_id from article_categories where article_category_name='%s'",
					FNTH_db_addslashes( $article_category_name ) );
				return FNTH_db_get_field( $query , "article_category_id" );
			}  else {
				$names = "";
				foreach ($article_category_name as $name) {
					$names.=(!empty( $names )) ? "," : "";
					$names.=sprintf( "'%s'" , FNTH_db_addslashes( $name ) );
				}
				$query = sprintf( "select article_category_name,article_category_id from article_categories where article_category_name in (%s)",$names );
				return FNTH_db_get_associative( $query , "article_category_name" , "article_category_id" );
			}
		}

		public function getArticleCategoryFromId( $article_category_id ) {
			$query = sprintf( "select * from article_categories where article_category_id=%d",$article_category_id);
			return FNTH_db_get_row( $query );
		}
		
		public function getArticleCategoryFromName( $article_category_name ) {
			$query = sprintf( "select * from article_categories where article_category_name='%s'",FNTH_db_addslashes( $article_category_name));
			return FNTH_db_get_row( $query );
		}

		public function getArticleCategoryLanguageDefsFromId( $article_category_id , $language_ids = array() ) {
			$instring = (empty( $language_ids )) ? "" : implode( "," , $language_ids );
			$query = sprintf( "select * from article_category_langdefs where article_category_id=%d %s",
				$article_category_id,
				(empty($instring)) ? "" : sprintf( " and site_language_id in (%s)",$instring ) );
			$ds = FNTH_db_get_resultset( $query );
			$retval = array();
			foreach ($ds as $row) {
				$retval[$row["site_language_id"]]=array(
					"article_category_title" => $row["article_category_title"],
					"article_category_aliasname" => $row["article_category_aliasname"]);
			}
			return $retval;
		}

		public function addArticleCategory( $parent_id , $datas = array() ) {
			$query = sprintf( "select sp_add_article_category( %s , '%s' ) as num",
				($parent_id) ? sprintf( "%d" , $parent_id ) : "null",
				FNTH_db_addslashes( $datas["article_category_name"] ) );
			$row = FNTH_db_get_row( $query );
			return $row["num"];
		}
		
		public function updateArticleCategory( $article_category_id , $data = array() ) {
			$d = array();
			$d["article_category_name"]=sprintf( "'%s'" , FNTH_db_addslashes( $data["article_category_name"] ) );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_update_query( "article_categories" , $d , sprintf( "article_category_id=%d" , $article_category_id ) )
				)
			);
		}
		
		public function setArticleCategoryLanguageDef( $article_category_id , $site_language_id , $data = array() ) {
			$query = sprintf( "select * from article_category_langdefs where site_language_id=%d and article_category_id=%d",
				$site_language_id , $article_category_id );
			$row = FNTH_db_get_row( $query );
			// prepare
			$d = array();
			$d["site_language_id"]=sprintf( "%d" , $site_language_id );
			$d["article_category_id"]=sprintf( "%d" , $article_category_id );
			$d["article_category_title"]=sprintf( "'%s'" , FNTH_db_addslashes( $data["article_category_title"] ) );
			$q = ($row) ?
				FNTH_db_generate_update_query( "article_category_langdefs" , $d ,
				sprintf( "site_language_id=%d and article_category_id=%d" , $site_language_id , $article_category_id ) ) :
				FNTH_db_generate_insert_query( "article_category_langdefs" , $d );
			FNTH_db_free_result( FNTH_db_query( $q ) );
		}
		
		public function setArticleCategoryLanguageDefs( $article_category_id , $arr = array() ) {
			if (!is_array( $arr )) return;
			if (empty( $arr)) return;
			foreach ($arr as $site_language_id => $lang) {
				$this->setArticleCategoryLanguageDef( $article_category_id , $site_language_id , $lang );
			}
		}

		public function deleteArticleCategory( $article_category_id ) {
			FNTH_db_free_result(
				FNTH_db_query( sprintf( "select sp_delete_article_category( %d )" , $article_category_id ) ) );
		}

		public function getPublicArticleCategoryOptions( ) {
			$query = sprintf( "select * from article_categories where is_public and is_active and not is_deleted order by article_category_meta_l" );
			$ds = FNTH_db_get_resultset( $query );
			$retval = array();
			foreach ($ds as $row)
				$retval[$row["article_category_id"]]=$row["article_category_name"];
			return $retval;
		}
	}

?>
