<?php

/**
 * Ahead WebSoft Technologies Pvt Ltd.
 * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
 * @license    https://www.socialengine.com/policies
 */
class Core_Form_Admin_Language_Autotranslate extends Engine_Form
{
	public function init()
	{
		$this->setTitle('Auto Translate')
			->setAttrib('class', 'global_form_popup global_form_ignore global_form_translate');

		$settings = Engine_Api::_()->getApi('settings', 'core');
		$googletranslatorapi = $settings->getSetting('core.googletranslatorapi', '');


		// Get all language from google for translation
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://translation.googleapis.com/language/translate/v2/languages?key=' . $googletranslatorapi . "&target=en");
		// receive server response
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$server_output = curl_exec($ch);
		$server_output = json_decode($server_output, true);
		curl_close($ch);

		$googleLanguages = $server_output['data']['languages'];
		$googleLanguagesArray = array();
		if ($googleLanguages) {
			foreach ($googleLanguages as $key => $value) {
				$googleLanguagesArray[$value['language']] = $value['name'] . "[" . $value['language'] . "]";
			}
		}

		$files = glob(APPLICATION_PATH . "/application/languages/en/*.csv");
		foreach ($files as $file) {
			$basefilename = basename($file);
			$explode = explode('.csv', $basefilename);
			if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled($explode[0])) {
				$allLanguageFiles[$basefilename] = $basefilename;
			}
		}

		if (engine_count($googleLanguagesArray) > 0) {
			$locale = Zend_Controller_Front::getInstance()->getRequest()->getParam('locale', null);

			$this->addElement('Select', 'phrases_translate', array(
				'label' => "Choose the phrases you want to translate automatically",
				'required' => true,
				'multiOptions' => array("all" => 'All Phrases', "missing" => "Missing Phrases Only"),
				'decorators' => array(
					'ViewHelper',
					array('Label', array('tag' => null, 'placement' => 'PREPEND')),
					array('HtmlTag', array('tag' => 'div'))
				),
				'onchange' => "hideShow(this.value);"
			));

			$files = glob(APPLICATION_PATH . "/application/languages/en/*.csv");
			$allLanguageFiles = array('all' => 'All');
			foreach ($files as $file) {
				$basefilename = basename($file);
				$explode = explode('.csv', $basefilename);
				if (Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled($explode[0])) {
					$allLanguageFiles[$basefilename] = $basefilename;
				}
			}

			$this->addElement('MultiCheckbox', 'main_languagefiles', array(
				'label' => 'CSV Files of Plugins to Translate',
				'description' => 'Choose from here the plugin csv files that you want to translate in other language.',
				'allowEmpty' => true,
				'required' => false,
				'multiOptions' => $allLanguageFiles,
				'class' => 'checkBoxClass',
			));

			$this->addElement('Hidden', 'convert_languages', array(
				'value' => $locale,
			));

			// Init submit
			$this->addElement('Button', 'submit', array(
				'label' => 'Submit',
				'type' => 'submit',
				'ignore' => true,
				'decorators' => array(
					'ViewHelper',
				),
			));

			$this->addElement('Cancel', 'cancel', array(
				'prependText' => ' or ',
				'link' => true,
				'label' => 'cancel',
				'onclick' => 'parent.Smoothbox.close();',
				'decorators' => array(
					'ViewHelper'
				)
			));

			$this->addDisplayGroup(array('submit', 'cancel'), 'buttons', array(
				'decorators' => array(
					'FormElements'
				)
			));
			$this->addDisplayGroup(array('submit', 'cancel'), 'buttons');
		} else {
			$this->addElement('Dummy', 'api_key_tip', array(
				'content' => '<div class="tip"><span>Google Translation API Key is not correct. Please make sure that you have created the correct API key from Google Console. <a href="' . Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'core', 'controller' => 'language', 'action' => 'api'), 'admin_default', true) . '">Click Here</a> to enter the API key.</span></div>',
				'decorators' => array(
					'ViewHelper',
					array('HtmlTag', array('tag' => 'div', 'class' => 'form-elements'))
				),
			));
			$this->addElement('Cancel', 'cancel', array(
				'link' => true,
				'label' => 'cancel',
				'onclick' => 'parent.Smoothbox.close();',
				'decorators' => array(
					'ViewHelper'
				)
			));
		}
	}
}
