From c14daa2790f9f383b650e244a96dfef699b141ae Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 19 Feb 2020 18:32:04 +0100 Subject: [PATCH] Can set main language and sublanguages of a website --- .../install/mysql/migration/11.0.0-12.0.0.sql | 3 + htdocs/install/mysql/tables/llx_website.sql | 2 + htdocs/langs/en_US/website.lang | 3 + htdocs/website/class/website.class.php | 55 ++++++++++++++++++- htdocs/website/index.php | 51 +++++++++++++++-- 5 files changed, 107 insertions(+), 7 deletions(-) diff --git a/htdocs/install/mysql/migration/11.0.0-12.0.0.sql b/htdocs/install/mysql/migration/11.0.0-12.0.0.sql index 447630e7eb2..9e718148794 100644 --- a/htdocs/install/mysql/migration/11.0.0-12.0.0.sql +++ b/htdocs/install/mysql/migration/11.0.0-12.0.0.sql @@ -44,6 +44,9 @@ ALTER TABLE llx_commande_fournisseur_dispatch_extrafields ADD INDEX idx_commande -- For v12 +ALTER TABLE llx_website ADD COLUMN lang varchar(8); +ALTER TABLE llx_website ADD COLUMN otherlang varchar(255); + ALTER TABLE llx_holiday_users DROP INDEX uk_holiday_users; ALTER TABLE llx_holiday_users ADD UNIQUE INDEX uk_holiday_users(fk_user, fk_type); diff --git a/htdocs/install/mysql/tables/llx_website.sql b/htdocs/install/mysql/tables/llx_website.sql index 645343544a4..743d4ec9ca8 100644 --- a/htdocs/install/mysql/tables/llx_website.sql +++ b/htdocs/install/mysql/tables/llx_website.sql @@ -26,6 +26,8 @@ CREATE TABLE llx_website description varchar(255), maincolor varchar(16), maincolorbis varchar(16), + lang varchar(8), + otherlang varchar(255), status integer DEFAULT 1, fk_default_home integer, use_manifest integer, diff --git a/htdocs/langs/en_US/website.lang b/htdocs/langs/en_US/website.lang index 1d31a763523..ee46c9f4954 100644 --- a/htdocs/langs/en_US/website.lang +++ b/htdocs/langs/en_US/website.lang @@ -121,3 +121,6 @@ BackToHomePage=Back to home page... TranslationLinks=Translation links YouTryToAccessToAFileThatIsNotAWebsitePage=You try to access to a page that is not a website page UseTextBetween5And70Chars=For good SEO practices, use a text between 5 and 70 characters +MainLanguage=Main language +OtherLanguages=Other languages +UseManifest=Provide a manifest.json file \ No newline at end of file diff --git a/htdocs/website/class/website.class.php b/htdocs/website/class/website.class.php index c722b7adc36..927d54f5b29 100644 --- a/htdocs/website/class/website.class.php +++ b/htdocs/website/class/website.class.php @@ -70,6 +70,16 @@ class Website extends CommonObject */ public $description; + /** + * @var string Main language of web site + */ + public $lang; + + /** + * @var string List of languages of web site ('fr', 'es_MX', ...) + */ + public $otherlang; + /** * @var int Status */ @@ -171,6 +181,8 @@ class Website extends CommonObject $sql .= 'entity,'; $sql .= 'ref,'; $sql .= 'description,'; + $sql .= 'lang,'; + $sql .= 'otherlang,'; $sql .= 'status,'; $sql .= 'fk_default_home,'; $sql .= 'virtualhost,'; @@ -181,6 +193,8 @@ class Website extends CommonObject $sql .= ' '.((empty($this->entity) && $this->entity != '0') ? 'NULL' : $this->entity).','; $sql .= ' '.(!isset($this->ref) ? 'NULL' : "'".$this->db->escape($this->ref)."'").','; $sql .= ' '.(!isset($this->description) ? 'NULL' : "'".$this->db->escape($this->description)."'").','; + $sql .= ' '.(!isset($this->lang) ? 'NULL' : "'".$this->db->escape($this->lang)."'").','; + $sql .= ' '.(!isset($this->otherlang) ? 'NULL' : "'".$this->db->escape($this->otherlang)."'").','; $sql .= ' '.(!isset($this->status) ? '1' : $this->status).','; $sql .= ' '.(!isset($this->fk_default_home) ? 'NULL' : $this->fk_default_home).','; $sql .= ' '.(!isset($this->virtualhost) ? 'NULL' : "'".$this->db->escape($this->virtualhost)."'").","; @@ -201,6 +215,16 @@ class Website extends CommonObject if (!$error) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element); + // Create subdirectory per language + $tmplangarray = explode(',', $this->otherlang); + if (is_array($tmplangarray)) { + dol_mkdir($conf->website->dir_output.'/'.$this->ref); + foreach($tmplangarray as $val) { + if (trim($val) == $this->lang) continue; + dol_mkdir($conf->website->dir_output.'/'.$this->ref.'/'.trim($val)); + } + } + // Uncomment this and change MYOBJECT to your own tag if you // want this action to call a trigger. // if (!$notrigger) { @@ -240,6 +264,8 @@ class Website extends CommonObject $sql .= " t.entity,"; $sql .= " t.ref,"; $sql .= " t.description,"; + $sql .= " t.lang,"; + $sql .= " t.otherlang,"; $sql .= " t.status,"; $sql .= " t.fk_default_home,"; $sql .= " t.use_manifest,"; @@ -267,6 +293,8 @@ class Website extends CommonObject $this->entity = $obj->entity; $this->ref = $obj->ref; $this->description = $obj->description; + $this->lang = $obj->lang; + $this->otherlang = $obj->otherlang; $this->status = $obj->status; $this->fk_default_home = $obj->fk_default_home; $this->virtualhost = $obj->virtualhost; @@ -332,6 +360,8 @@ class Website extends CommonObject $sql .= " t.entity,"; $sql .= " t.ref,"; $sql .= " t.description,"; + $sql .= " t.lang,"; + $sql .= " t.otherlang,"; $sql .= " t.status,"; $sql .= " t.fk_default_home,"; $sql .= " t.virtualhost,"; @@ -372,6 +402,8 @@ class Website extends CommonObject $line->entity = $obj->entity; $line->ref = $obj->ref; $line->description = $obj->description; + $line->lang = $obj->lang; + $line->otherlang = $obj->otherlang; $line->status = $obj->status; $line->fk_default_home = $obj->fk_default_home; $line->virtualhost = $obj->virtualhost; @@ -403,6 +435,8 @@ class Website extends CommonObject */ public function update(User $user, $notrigger = false) { + global $conf; + $error = 0; dol_syslog(__METHOD__, LOG_DEBUG); @@ -430,6 +464,8 @@ class Website extends CommonObject $sql .= ' entity = '.(isset($this->entity) ? $this->entity : "null").','; $sql .= ' ref = '.(isset($this->ref) ? "'".$this->db->escape($this->ref)."'" : "null").','; $sql .= ' description = '.(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "null").','; + $sql .= ' lang = '.(isset($this->lang) ? "'".$this->db->escape($this->lang)."'" : "null").','; + $sql .= ' otherlang = '.(isset($this->otherlang) ? "'".$this->db->escape($this->otherlang)."'" : "null").','; $sql .= ' status = '.(isset($this->status) ? $this->status : "null").','; $sql .= ' fk_default_home = '.(($this->fk_default_home > 0) ? $this->fk_default_home : "null").','; $sql .= ' use_manifest = '.((int) $this->use_manifest).','; @@ -452,6 +488,16 @@ class Website extends CommonObject // Uncomment this and change MYOBJECT to your own tag if you // want this action calls a trigger. + // Create subdirectory per language + $tmplangarray = explode(',', $this->otherlang); + if (is_array($tmplangarray)) { + dol_mkdir($conf->website->dir_output.'/'.$this->ref); + foreach($tmplangarray as $val) { + if (trim($val) == $this->lang) continue; + dol_mkdir($conf->website->dir_output.'/'.$this->ref.'/'.trim($val)); + } + } + //// Call triggers //$result=$this->call_trigger('MYOBJECT_MODIFY',$user); //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail} @@ -715,7 +761,8 @@ class Website extends CommonObject $label = ''.$langs->trans("WebSite").''; $label .= '
'; - $label .= ''.$langs->trans('Ref').': '.$this->ref; + $label .= ''.$langs->trans('Ref').': '.$this->ref.'
'; + $label .= ''.$langs->trans('MainLanguage').': '.$this->lang; $linkstart = 'entity = 1; $this->ref = 'myspecimenwebsite'; $this->description = 'A specimen website'; + $this->lang = 'en'; + $this->otherlang = 'fr,es_MX'; $this->status = ''; $this->fk_default_home = null; $this->virtualhost = 'http://myvirtualhost'; @@ -926,7 +975,7 @@ class Website extends CommonObject fputs($fp, $line); // Warning: We must keep llx_ here. It is a generic SQL. - $line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, aliasalt, title, description, image, keywords, status, date_creation, tms, lang, import_key, grabbed_from, type_container, htmlheader, content)'; + $line = 'INSERT INTO llx_website_page(rowid, fk_page, fk_website, pageurl, aliasalt, title, description, lang, otherlang, image, keywords, status, date_creation, tms, lang, import_key, grabbed_from, type_container, htmlheader, content)'; $line .= " VALUES("; $line .= $objectpageold->newid."__+MAX_llx_website_page__, "; @@ -936,6 +985,8 @@ class Website extends CommonObject $line .= "'".$this->db->escape($objectpageold->aliasalt)."', "; $line .= "'".$this->db->escape($objectpageold->title)."', "; $line .= "'".$this->db->escape($objectpageold->description)."', "; + $line .= "'".$this->db->escape($objectpageold->lang)."', "; + $line .= "'".$this->db->escape($objectpageold->otherlang)."', "; $line .= "'".$this->db->escape($objectpageold->image)."', "; $line .= "'".$this->db->escape($objectpageold->keywords)."', "; $line .= "'".$this->db->escape($objectpageold->status)."', "; diff --git a/htdocs/website/index.php b/htdocs/website/index.php index 85b50b3d13b..c44341e0361 100644 --- a/htdocs/website/index.php +++ b/htdocs/website/index.php @@ -398,7 +398,9 @@ if ($action == 'addsite') { $tmpobject = new Website($db); $tmpobject->ref = GETPOST('WEBSITE_REF', 'alpha'); - $tmpobject->description = GETPOST('WEBSITE_DESCRIPTION', 'alpha'); + $tmpobject->description = GETPOST('WEBSITE_DESCRIPTION', 'alphanohtml'); + $tmpobject->lang = GETPOST('WEBSITE_LANG', 'aZ09'); + $tmpobject->otherlang = GETPOST('WEBSITE_OTHERLANG', 'aZ09comma'); $tmpobject->virtualhost = GETPOST('virtualhost', 'alpha'); $result = $tmpobject->create($user); @@ -782,6 +784,8 @@ if ($action == 'addcontainer') $objectpage->pageurl = GETPOST('WEBSITE_PAGENAME', 'alpha'); $objectpage->aliasalt = GETPOST('WEBSITE_ALIASALT', 'alpha'); $objectpage->description = GETPOST('WEBSITE_DESCRIPTION', 'alpha'); + $objectpage->lang = GETPOST('WEBSITE_LANG', 'alpha'); + $objectpage->otherlang = GETPOST('WEBSITE_OTHERLANG', 'alpha'); $objectpage->image = GETPOST('WEBSITE_IMAGE', 'alpha'); $objectpage->keywords = GETPOST('WEBSITE_KEYWORDS', 'alpha'); $objectpage->lang = GETPOST('WEBSITE_LANG', 'aZ09'); @@ -1060,6 +1064,8 @@ if ($action == 'updatecss') if (!$error) { $object->virtualhost = GETPOST('virtualhost', 'alpha'); + $object->lang = GETPOST('WEBSITE_LANG', 'aZ09'); + $object->otherlang = GETPOST('WEBSITE_OTHERLANG', 'aZ09comma'); $object->use_manifest = GETPOST('use_manifest', 'alpha'); $result = $object->update($user); @@ -1403,11 +1409,13 @@ if ($action == 'updatemeta') { $objectpage->old_object = clone $objectpage; - $objectpage->title = GETPOST('WEBSITE_TITLE', 'alpha'); - $objectpage->type_container = GETPOST('WEBSITE_TYPE_CONTAINER', 'alpha'); + $objectpage->title = GETPOST('WEBSITE_TITLE', 'alphanohtml'); + $objectpage->type_container = GETPOST('WEBSITE_TYPE_CONTAINER', 'alphanohtml'); $objectpage->pageurl = GETPOST('WEBSITE_PAGENAME', 'alpha'); $objectpage->aliasalt = GETPOST('WEBSITE_ALIASALT', 'alpha'); - $objectpage->description = GETPOST('WEBSITE_DESCRIPTION', 'alpha'); + $objectpage->lang = GETPOST('WEBSITE_LANG', 'aZ09'); + $objectpage->otherlang = GETPOST('WEBSITE_OTHERLANG', 'aZ09comma'); + $objectpage->description = GETPOST('WEBSITE_DESCRIPTION', 'alphanohtml'); $objectpage->image = GETPOST('WEBSITE_IMAGE', 'alpha'); $objectpage->keywords = GETPOST('WEBSITE_KEYWORDS', 'alpha'); $objectpage->lang = GETPOST('WEBSITE_LANG', 'aZ09'); @@ -2691,6 +2699,24 @@ if ($action == 'editcss') print $websitekey; print ''; + // Main language + print ''; + $htmltext=''; + print $form->textwithpicto($langs->trans('MainLanguage'), $htmltext, 1, 'help', '', 0, 2, 'WEBSITE_LANG'); + print ''; + print $formadmin->select_language((GETPOSTISSET('WEBSITE_LANG') ? GETPOST('WEBSITE_LANG', 'alpha') : ($object->lang ? $object->lang : $langs->defaultlang)), 'WEBSITE_LANG', 0, 0, 0, 0, 0, 'minwidth300', 2); + print ''; + print ''; + + // Other languages + print ''; + $htmltext= ''; + print $form->textwithpicto($langs->trans('OtherLanguages'), $htmltext, 1, 'help', '', 0, 2, 'WEBSITE_OTHERLANG'); + print ''; + print ''; + print ''; + print ''; + // VirtualHost print ''; @@ -2816,8 +2842,11 @@ if ($action == 'createsite') print ''; + $siteref = $sitedesc = $sitelang = $siteotherlang = ''; if (GETPOST('WEBSITE_REF')) $siteref = GETPOST('WEBSITE_REF', 'alpha'); if (GETPOST('WEBSITE_DESCRIPTION')) $sitedesc = GETPOST('WEBSITE_DESCRIPTION', 'alpha'); + if (GETPOST('WEBSITE_LANG')) $sitelang = GETPOST('WEBSITE_LANG', 'aZ09'); + if (GETPOST('WEBSITE_OTHERLANG')) $siteotherlang = GETPOST('WEBSITE_OTHERLANG', 'aZ09comma'); print ''; + + print ''; + + print ''; print '
'; print $langs->trans('Ref'); @@ -2828,7 +2857,19 @@ if ($action == 'createsite') print '
'; print $langs->trans('Description'); print ''; - print ''; + print ''; + print '
'; + print $langs->trans('MainLanguage'); + print ''; + print $formadmin->select_language((GETPOSTISSET('WEBSITE_LANG') ? GETPOST('WEBSITE_LANG', 'alpha') : $langs->defaultlang), 'WEBSITE_LANG', 0, 0, 0, 0, 0, 'minwidth300', 2); + print '
'; + print $langs->trans('OtherLanguages'); + print ''; + print ''; print '
';