summaryrefslogtreecommitdiff
path: root/LibertyTranslations.php
blob: 5f8735c79925312ea830f68c4c8b0c0039a30512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
class LibertyTranslations extends LibertyBase {
	function LibertyTranslations( $pContentId = NULL ) {
		$this->mContentId = $pContentId;
		LibertyBase::LibertyBase();
	}

	function getContentTranslations() {
		global $gBitSystem;
		$ret = array();
		if( @BitBase::verifyId( $this->mContentId ) ) {
			$query = "SELECT lc.`content_id`, lc.`title`, lc.`lang_code`, ictm.`translation_id`
				FROM `".BIT_DB_PREFIX."liberty_content` lc
				LEFT OUTER JOIN `".BIT_DB_PREFIX."i18n_content_trans_map` ictm ON( lc.`content_id`=ictm.`content_id` )
				WHERE ictm.`content_id`=?";
			$result = $this->mDb->query( $query, array( $this->mContentId ) );
			while( $aux = $result->fetchRow() ) {
				if( !empty( $contentTypes[$aux['content_type_guid']] ) ) {
					$ret[$aux['lang_code']] = $aux;
				}
			}
		}
		return $ret;
	}

	function storeTranslation( $pParamHash ) {
		if( $this->verify( $pParamHash ) ) {
			$table = BIT_DB_PREFIX."i18n_content_trans_map";
			if( !@BitBase::verifyId( $pParamHash['translation_store']['translation_id'] ) && is_array( $pParamHash['translation_store'] ) ) {
				// using the from_id as translation_id
				//$translationId = $this->mDb->GenID( 'i18n_content_trans_id_seq' );

				// --- i can't work out why this isn't storing! driving me nuts ----
				foreach( $pParamHash['translation_store'] as $store ) {
					//vd($store);
					$result = $this->mDb->associateInsert( $table, $store );
				}
			} else {
				$result = $this->mDb->associateInsert( $table, $pParamHash['translation_store'] );
			}
		}
	}

	function verify( &$pParamHash ) {
		$i = 0;
		// we should make sure that we don't have an entry for this content_id / lang_code combo yet
		if( !$this->mDb->getOne( "SELECT `content_id` FROM `".BIT_DB_PREFIX."liberty_content` WHERE `content_id`=? AND `lang_code`=?", array( $pParamHash['content_id'], $pParamHash['lang_code'] ) ) ) {
			//return FALSE;
		}

		// make sure we don't have a translation_id for this content yet
		if( @BitBase::verifyId( $pParamHash['from_id'] ) ) {
			$pParamHash['translation_id'] = $this->mDb->getOne( "SELECT `translation_id` FROM `".BIT_DB_PREFIX."i18n_content_trans_map` WHERE `content_id`=?", array( $pParamHash['content_id'] ) );
		}

		// we have a from_id but no translation_id, this is a new entry in the translation map and we need both, the original and the new content_id entered
		if( @BitBase::verifyId( $pParamHash['translation_id'] ) ) {
			$pParamHash['translation_store']['translation_id'] = $pParamHash['translation_id'];
			$pParamHash['translation_store']['content_id'] = $pParamHash['content_id'];
		} elseif( @BitBase::verifyId( $pParamHash['from_id'] ) ) {
			// we can simply use the from_id as the translation_id
			$pParamHash['translation_store'][$i]['translation_id'] = $pParamHash['from_id'];
			$pParamHash['translation_store'][$i]['content_id'] = $pParamHash['from_id'];
			$i++;
			$pParamHash['translation_store'][$i]['translation_id'] = $pParamHash['from_id'];
			$pParamHash['translation_store'][$i]['content_id'] = $pParamHash['content_id'];
		}
		return( count( $this->mErrors ) == 0 );
	}

	function expunge() {
		if( @BitBase::verifyId( $this->mContentId ) ) {
			$result = $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."i18n_content_trans_map` WHERE `content_id`=?", $this->mContentId );
		}
	}
}

// ================== service functions ==================

function translation_content_edit( &$pObject, &$pParamHash ) {
	global $gBitLanguage, $gBitSmarty, $gBitUser;
	$trans = new LibertyTranslations( $pObject->mContentId );
	$translationId = NULL;
	$translations = $trans->getContentTranslations();
	foreach( $gBitLanguage->mLanguageList as $lang_code => $language ) {
		$translationsList[$lang_code] = $language;
		if( !empty( $translations[$lang_code]['content_id'] ) ) {
			$translationsList[$lang_code]['content_id'] = $translations[$lang_code]['content_id'];
			$translationsList[$lang_code]['title'] = $translations[$lang_code]['title'];
			$translationId = $translations[$lang_code]['translation_id'];
		}
	}
	$gBitSmarty->assign( 'translationsList', $translationsList );
	$gBitSmarty->assign( 'translationId', $translationId );

	if( @BitBase::verifyId( $_REQUEST['i18n']['from_id'] ) || @BitBase::verifyId( $_REQUEST['i18n']['translation_id'] ) ) {
		if( @BitBase::verifyId( $_REQUEST['i18n']['from_id'] ) ) {
			// load the content we're translating from
			$transObject = $trans->getLibertyObject( $_REQUEST['i18n']['from_id'] );
			$gBitSmarty->assign_by_ref( "translateFrom", $transObject );
		}
	}
}

// store the content
function translation_content_store( $pObject, $pParamHash ) {
	// if we are creating this content and we have a from_id, we know that we're translating a page
	// how do we check if this is the first install??? $pObject already contains stuff...
	if( @BitBase::verifyId( $_REQUEST['i18n']['from_id'] ) ) {
		$trans = new LibertyTranslations();
		$storeHash = $_REQUEST['i18n'];
		$storeHash['content_id'] = $pParamHash['content_id'];
		if( !$trans->storeTranslation( $storeHash ) ) {
			// error
		}
	}
	die;
}

function translation_content_exunge( $pObject, $pParamHash ) {
	$trans = new LibertyTranslations( $pObject->mContentId );
	$trans->expunge();
}
?>