| Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/ |
| Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/TInstall.php |
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library;
defined('_JEXEC') or die;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Factory;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
use Joomla\CMS\Language\LanguageHelper;
class TInstall
{
public $languages; // All known languages
public $available; // Available languages
public $extension;
public $ext_type;
public $ext_name;
public $plugin_group;
public $csv_file;
public $dest; // Destination file name
// Laguage translations
public $a_ini;
public $a_sys;
public $s_ini;
public $s_sys;
public $delete_folders;
public function __construct($extension = '', $plugin_group = '')
{
if ($extension)
{
$this->extension = $extension;
}
if ($plugin_group)
{
$this->plugin_group = $plugin_group;
}
if ($this->extension)
{
$parts = explode('_', $this->extension);
$this->ext_type = $parts[0]; // com, mod, plg, tpl, lib
$this->ext_name = $parts[1];
}
}
// Used By Components Dashboard
public function set_extension($extension)
{
$this->extension = $extension;
$parts = explode('_', $this->extension);
$this->ext_type = $parts[0]; // com, mod, plg, tpl, lib
$this->ext_name = $parts[1];
}
public function preflight($type, InstallerAdapter $adapter)
{
if ($type == 'update')
{
$this->remove_files();
}
return true;
}
public function postflight($type, InstallerAdapter $adapter)
{
$this->install_languages();
if ($this->ext_type == 'plg' && $this->plugin_group == 'fields')
{
$this->enable_plugin();
}
if ($type == 'update')
{
$this->remove_duplicate_update_sites();
}
return true;
}
public function install_languages()
{
$app = Factory::getApplication();
$this->languages = LanguageHelper::getKnownLanguages();
$this->available = array('en-GB', 'de-DE', 'fr-FR', 'it-IT', 'nl-NL', 'pl-PL', 'ru-RU');
// Step 1: Set CSV File Path
$function = 'install_lang_' . $this->ext_type;
$result = $this->$function();
// Step 2. Read CSV Files
$this->get_translations();
// Step 3: Install individual language
foreach ($this->languages as $language)
{
if (!\in_array($language['tag'], $this->available))
{
continue;
}
$success = false;
$language_tag = $language['tag'];
// Site - ini
$dest = JPATH_SITE . '/language/' . $language['tag'] . '/' . $this->dest . '.ini';
if (File::write($dest, $this->s_ini[$language_tag]))
{
$success = true;
}
// Site - sys
$dest = JPATH_SITE . '/language/' . $language['tag'] . '/' . $this->dest . '.sys.ini';
if (File::write($dest, $this->s_sys[$language_tag]))
{
$success = true;
}
// Admin - ini
$dest = JPATH_SITE . '/administrator/language/' . $language['tag'] . '/' . $this->dest . '.ini';
if (File::write($dest, $this->a_ini[$language_tag]))
{
$success = true;
}
// Admin - sys
$dest = JPATH_SITE . '/administrator/language/' . $language['tag'] . '/' . $this->dest . '.sys.ini';
if (File::write($dest, $this->a_sys[$language_tag]))
{
$success = true;
}
$message = 'Language ' . $language['tag'] . ' - ' . $language['name'] . ' ';
if ($success)
{
$app->enqueueMessage($message . 'Successfully Updated.');
}
else
{
$app->enqueueMessage($message . 'Not Updated.', 'danger');
}
}
return true;
}
public function install_lang_com()
{
$this->csv_file = JPATH_ADMINISTRATOR . '/components/' . $this->extension . '/csv/' . $this->ext_name . ' - lang.csv';
$this->dest = $this->extension;
}
public function install_lang_mod()
{
$this->csv_file = JPATH_SITE . '/modules/' . $this->extension . '/csv/' . $this->ext_name . ' - lang.csv';
$this->dest = $this->extension;
}
public function install_lang_plg()
{
$this->csv_file = JPATH_SITE . '/plugins/' . $this->plugin_group . '/' . $this->ext_name . '/csv/' . $this->ext_name . ' - lang.csv';
$this->dest = 'plg_' . $this->plugin_group . '_' . $this->ext_name;
}
public function install_lang_tpl()
{
$this->csv_file = JPATH_SITE . '/templates/' . $this->ext_name . '/csv/' . $this->ext_name . ' - lang.csv';
$this->dest = $this->extension;
}
public function remove_files()
{
// Step 1: Set Folders to Remove
$function = 'remove_files_' . $this->ext_type;
$result = $this->$function();
foreach ($this->delete_folders as $folder)
{
$subfolders = Folder::folders($folder, '.', false, true);
foreach ($subfolders as $subfolder)
{
Folder::delete($subfolder);
}
}
}
// Delete component files
public function remove_files_com()
{
$this->delete_folders = array(
JPATH_ADMINISTRATOR . '/components/' . $this->extension,
JPATH_SITE . '/components/' . $this->extension,
);
}
// Delete module files
public function remove_files_mod()
{
$this->delete_folders = array(
JPATH_SITE . '/modules/' . $this->extension,
);
}
// Delete template files
public function remove_files_tpl()
{
$this->delete_folders = array(
JPATH_SITE . '/templates/' . $this->ext_name,
);
}
// Delete plugin files
public function remove_files_plg()
{
$this->delete_folders = array(
JPATH_SITE . '/plugins/' . $this->plugin_group . '/' . $this->ext_name,
);
}
// Enable plugin after installation
public function enable_plugin()
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->update('#__extensions')
->set($db->quoteName('enabled') . ' = 1')
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'))
->where($db->quoteName('element') . ' = ' . $db->quote($this->ext_name))
->where($db->quoteName('folder') . ' = ' . $db->quote($this->plugin_group));
$db->setQuery($query);
$db->execute();
}
// Get language translations from CSV file
public function get_translations()
{
if (!file_exists($this->csv_file))
{
return;
}
$f = new TFile();
$data = $f->read_csv($this->csv_file); // 0 - admin/site; 1 - constant; 2 onwards language
// First row contains column headers or language type
$first = $data[0];
unset($data[0]);
$prefix = strtoupper($this->ext_type . '_' . $this->ext_name); // COM_EXAMPLE, MOD_EXAMPLE
foreach ($data as $row_num => $row)
{
// In row, first column has type, second column has constant
for ($i = 2; $i < count($row); $i++)
{
$lang = $first[$i];
// Add Extension Name
if ($row_num == 1)
{
$extension_name = $prefix . '="TF ' . ucwords(substr($this->ext_name, 2)) . '"' . "\n";
$this->s_ini[$lang] = $extension_name;
$this->s_sys[$lang] = $extension_name;
$this->a_ini[$lang] = $extension_name;
$this->a_sys[$lang] = $extension_name;
// Special case for custom fields plugin - add label
if ($this->ext_type == 'plg' && $this->plugin_group == 'fields')
{
$this->a_ini[$lang] .= 'PLG_FIELDS_' . strtoupper($this->ext_name) . '_LABEL="TF ' . ucwords(substr($this->ext_name, 2)) . '"' . "\n";
}
}
if ($row[0] == 'site_ini')
{
$this->s_ini[$lang] .= $prefix . '_' . $row[1] . '="' . $row[$i] . '"' . "\n";
}
elseif ($row[0] == 'site_sys')
{
$this->s_sys[$lang] .= $prefix . '_' . $row[1] . '="' . $row[$i] . '"' . "\n";
}
elseif ($row[0] == 'admin_ini')
{
$this->a_ini[$lang] .= $prefix . '_' . $row[1] . '="' . $row[$i] . '"' . "\n";
}
elseif ($row[0] == 'admin_sys')
{
$this->a_sys[$lang] .= $prefix . '_' . $row[1] . '="' . $row[$i] . '"' . "\n";
}
elseif ($row[0] == 'sitemodule') // Special case for module fieldset labels
{
$this->s_ini[$lang] .= 'COM_MODULES_' . $row[1] . '_FIELDSET_LABEL="' . $row[$i] . '"' . "\n";
}
}
}
// Common for all components
if ($this->ext_type == 'com')
{
$f = new TFile();
$data2 = $f->read_csv(JPATH_LIBRARIES . '/techfry/csv/tf - lang.csv');
foreach ($data2 as $row_num => $row)
{
for ($i = 2; $i < count($row); $i++)
{
$lang = $first[$i];
if ($row[0] == 'site_ini' || $row[0] == 'site')
{
$this->s_ini[$lang] .= 'COM_TF_' . $row[1] . '="' . $row[$i] . '"' . "\n";
}
elseif ($row[0] == 'admin_ini' || $row[0] == 'admin')
{
$this->a_ini[$lang] .= 'COM_TF_' . $row[1] . '="' . $row[$i] . '"' . "\n";
}
}
}
}
}
public function remove_duplicate_update_sites()
{
// $extension - com_example, mod_example, plg_example
// First, get extension id
$extension_id = $this->get_extension_id();
// Then, get update site IDs
$update_sites = $this->get_update_sites($extension_id);
// Remove duplicate update sites
if (\count($update_sites) > 1)
{
$conditions = array(
array('update_site_id', '=', $update_sites[1]),
);
$db = new TDb('update_sites');
$db->delete_items($conditions);
}
}
public function get_extension_id()
{
if ($this->ext_type == 'com' || $this->ext_type == 'mod')
{
$element = $this->extension;
}
elseif ($this->ext_type == 'plg' || $this->ext_type == 'tpl')
{
$element = $this->ext_name;
}
$db = new TDb('extensions');
$ext = $db->get_item(array('id' => $element, 'column' => 'element'));
return $ext->extension_id;
}
public function get_update_sites($extension_id)
{
$conditions = array(
array('extension_id', '=', $extension_id),
);
$db = new TDb('update_sites_extensions');
$update_sites = $db->get_column(array('select' => 'update_site_id', 'conditions' => $conditions, 'order' => 'update_site_id DESC'));
return $update_sites;
}
}