Migrer un module vers une nouvelle version

Mettre à jour son module 5.0 en 5.1

Dernière mise à jour : 19/01/2018 à 00h32


Si aucun des éléments se trouvant dans cette documentation ne vous aide, référez-vous au forum et faites une demande de support.



Cette documentation permet de rendre votre module compatible avec la version 5.1 de PHPBoost sans trop de modifications dans le code. Elle n'a pas pour but de le convertir en utilisant le modèle MVC s'il n'est pas MVC. Effectuez les différentes étapes suivantes (peu importe l'ordre) pour mettre à jour votre module.



Le fichier de configuration - OBLIGATOIRE






Les seuls changements à effectuer dans le fichier /module/config.ini pour rendre votre module compatible avec la nouvelle version sont les paramètres "compatibility" et "version". Remplacez la valeur de "compatibility" par 5.1 et augmentez le numéro de version de votre module pour plus de cohérence.



Code TEXT :
version=5.1.0
compatibility=5.1




La modification du config.ini est obligatoire



Encodage de vos fichiers en UTF8 sans BOM - OBLIGATOIRE






Voici la démarche à suivre avec Notepad++ (c'est un exemple, d'autres éditeurs font la même chose) pour que tous les nouveaux documents créés soient en UTF8 sans Bom (UTF8 et BOM - Explications):



img_01_notepad_utf8



Si vous voulez convertir en UTF8 sans Bom tous vos documents ouverts, voici ce qu'il faut faire (toujours avec Notepad++) :



img_02_notepad_utf8



La modification des fichiers lang est obligatoire



Gestion des catégories et des éléments en front - OBLIGATOIRE






Dans le cas d'utilisation des catégories - Exemples en image :



En 5.0, la gestion des catégories se trouve en back :

categories_manage_admin_5_0



En 5.1, la gestion des catégories se trouve en front :



categories_manage_front_5_1



Il en est de même pour la gestion des éléments. Elle passe donc en front en 5.1 .



En 5.0 :

categories_add_admin_5_0



En 5.1 :

categories_add_front_5_1



Renommer le fichier AdminMonModuleManageController.class.php en MonModuleManageController.class.php.



Maintenant, il va falloir effectuer l'ensemble des modifications décrites dans ce lien. En rouge, le code à effacer et en vert le code à ajouter. Le module Articles a encore été pris en exemple.



Les fichiers concernés sont :

  • monmodule/controllers/AdminMonModuleManageController ->monmodule/controllers/MonModuleManageController.class.php
  • monmodule/controllers/AdminMonModuleConfigController.class.php
  • monmodule/controllers/categories/MonModuleCategoriesFormController.class.php
  • monmodules/controllers/categories/MonModuleCategoriesManageController.class.php
  • monmodule/controllers/categories/MonModuleDeleteCategoryController.class.php
  • monmodule/index.php
  • monmodule/phpboost/MonModuleTreeLinks.class.php
  • monmodule/services/MonModuleAuthorizationsService.class.php
  • monmodule/util/AdminMonModuleDisplayResponse.class.php
  • monmodule/util/MonModuleUrlBuilder.class.php



Gestion globale dans l'administration






En 5.1, la gestion du système de commentaire ainsi que celui de la notation a été entièrement transférée dans la rubrique contenu de l'administration générale.

Si vos modules utilisaient ces fonctionnalités en 5.0, veuillez suivre les modifications ci-dessous.



De plus, Phpboost 5.1 offre une nouvelle fonction New Content que vous allez pouvoir ajouter.



Les commentaires (facultatif)






Création du fichier NomModuleComments.class.php dans NomModule/phpboost/



NomModuleComments.class.php :
<?php
/*##################################################
 *                      NomModuleComments.class.php
 *                            -------------------
 *   begin            : *champ à renseigner
 *   copyright     : *champ à renseigner
 *   email            : *champ à renseigner
 *
 *
 ###################################################
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ###################################################*/
/**
 * @author Votre Nom  <votre e-mail>
 */
class NomModuleComments extends AbstractCommentsExtensionPoint
{
    public function __construct()
    {
        parent::__construct('nommodule');
    }
}
?>
 




Puisque l'activation des commentaires doit se faire à partir de l'administration générale et non dans la configuration de votre module, voici les quelques lignes de codes à supprimer (si bien-entendu elles existent dans votre module) :



NomModuleConfig.class.php :
 
// A supprimer dans la déclaration des constantes
const COMMENTS_ENABLED = 'comments_enable'; 
//
//
// A supprimer dans la déclaration des méthodes
    public function enable_comments()
    {
        $this->set_property(self::COMMENTS_ENABLED, true);
    }
 
    public function disable_comments()
    {
        $this->set_property(self::COMMENTS_ENABLED, false);
    }
 
    public function are_comments_enabled()
    {
        return $this->get_property(self::COMMENTS_ENABLED);
    }
//
//
// A supprimer dans public function get_default_values() {    return array (
self::COMMENTS_ENABLED => true,
 




Nous allons maintenant supprimer le champ d'activation des commentaires de la configuration de votre module :

config_commentaires_5_0



AdminNomModuleConfigController.class.php :
 
// A supprimer dans la déclaration des champs
$fieldset->add_field(new FormFieldCheckbox('comments_enabled', $this->admin_common_lang['config.comments_enabled'], $this->config->are_comments_enabled()));
//
//
// A supprimer dans private function save()
        if ($this->form->get_value('comments_enabled'))
        {
            $this->config->enable_comments();
        }
        else
        {
            $this->config->disable_comments();
        }
 




Le système de notation (facultatif)






Création du fichier NomModuleNotation.class.php dans NomModule/phpboost/



NomModuleNotation.class.php :
<?php
/*##################################################
 *                      NomModuleNotation.class.php
 *                            -------------------
 *   begin                :  *champ à renseigner
 *   copyright         :  *champ à renseigner
 *   email                :  *champ à renseigner
 *
 *
 ###################################################
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ###################################################*/
/**
 * @author Votre Nom  <votre e-mail>
 */
class NomModuleNotation extends AbstractNotationExtensionPoint
{
    public function __construct()
    {
        parent::__construct('nommodule');
    }
}
?>
 




Ensuite ajoutez dans NomModuleExtensionPointProvider.class.php dans NomModule/phpboost/



NomModuleExtensionPointProvider.class.php :
public function notation()
    {
        return new NomModuleNotation();
    }




Puisque l'activation de la notation doit se faire à partir de l'administration générale et non dans la configuration de votre module, voici les quelques lignes de codes à supprimer (si bien-entendu elles existent dans votre module) :



NomModuleConfig.class.php :
 
// A supprimer dans la déclaration des constantes
const NOTATION_ENABLED = 'notation_enabled';
const NOTATION_SCALE = 'notation_scale';
//
//
// A supprimer dans la déclaration des méthodes
    public function enable_notation()
    {
        $this->set_property(self::NOTATION_ENABLED, true);
    }
 
    public function disable_notation()
    {
        $this->set_property(self::NOTATION_ENABLED, false);
    }
 
    public function is_notation_enabled()
    {
        return $this->get_property(self::NOTATION_ENABLED);
    }
 
    public function get_notation_scale()
    {
        return $this->get_property(self::NOTATION_SCALE);
    }
 
    public function set_notation_scale($notation_scale) 
    {
        $this->set_property(self::NOTATION_SCALE, $notation_scale);
    }
//
// A supprimer dans public function get_default_values() {    return array (
self::NOTATION_ENABLED => true,
self::NOTATION_SCALE => 5,
 




Nous allons maintenant supprimer les champs de configuration de la notation de votre module :



config_notation_5_0



AdminNomModuleConfigController.class.php :
 
// A supprimer dans public function execute(HTTPRequestCustom $request)
$this->form->get_field_by_id('notation_scale')->set_hidden(!$this->config->is_notation_enabled());
//
//
// A supprimer dans la déclaration des champs
$fieldset->add_field(new FormFieldCheckbox('notation_enabled', $this->admin_common_lang['config.notation_enabled'], $this->config->is_notation_enabled(), array(
            'events' => array('click' => '
                if (HTMLForms.getField("notation_enabled").getValue()) {
                    HTMLForms.getField("notation_scale").enable();
                } else {
                    HTMLForms.getField("notation_scale").disable();
                }'
            )
)));
//
//
// A supprimer dans private function save()
if ($this->form->get_value('notation_enabled'))
{
$this->config->enable_notation();
$this->config->set_notation_scale($this->form->get_value('notation_scale'));
    if ($this->form->get_value('notation_scale') != $this->config->get_notation_scale())
    NotationService::update_notation_scale('nommodule', $this->config->get_notation_scale(), $this->form->get_value('notation_scale'));
}
else
$this->config->disable_notation();
 




Dans services/NomModule.class.php



NomModule.class.php :
 
// A supprimer dans la déclaration des attributs et des constantes
private $notation_enabled;
//
//
const NOTATION_DISABLED = 0;
const NOTATION_ENABLED = 1;
//
//
    public function get_notation_enabled()
    {
        return $this->notation_enabled;
    }
    public function set_notation_enabled($enabled) 
    {
        $this->notation_enabled = $enabled;
    }
// A supprimer dans public function get_properties()  { return array(
'notation_enabled' => $this->get_notation_enabled(),
//
//
// A supprimer dans public function set_properties(array $properties)    {
$this->set_notation_enabled($properties['notation_enabled']);
// Dans la même méthode, ajouter sous $notation = new Notation();
$notation_config = new NomModuleNotation();
//
//
//A supprimer dans public function init_default_properties($id_category = Category::ROOT_CATEGORY)
$this->notation_enabled = self::NOTATION_ENABLED;
//
//
// Dans public function get_array_tpl_vars() , ajouter :
$notation_config = new NomModuleNotation();
// Et dans return array( modifier :
'C_NOTATION_ENABLED' => $this->get_notation_enabled(),
// par
'C_NOTATION_ENABLED' => $notation_config->is_notation_enabled(),
 




Activation du New-Content (facultatif)






Création du fichier NomModuleNewContent.class.php dans NomModule/phpboost/



NomModuleNewContent.class.php :
<?php
/*##################################################
 *                      NomModuleNewContent.class.php
 *                            -------------------
 *   begin                :  *champ à renseigner
 *   copyright         :  *champ à renseigner
 *   email                :  *champ à renseigner
 *
 *
 ###################################################
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ###################################################*/
/**
 * @author Votre Nom  <votre e-mail>
 */
class NomModuleNewContent extends AbstractNewContentExtensionPoint
{
    public function __construct()
    {
        parent::__construct('nommodule');
    }
}
?>
 




Ensuite ajoutez dans NomModuleExtensionPointProvider.class.php dans NomModule/phpboost/



NomModuleExtensionPointProvider.class.php :
public function newcontent()
    {
        return new NomModuleNewContent();
    }




Dans la majorité des cas, il faudra modifier NomModule.class.php en ajoutant :



- pour créer l'objet :

Code PHP :
$new_content = new NomModuleContent();




- pour créer la variable template :

Code PHP :
'C_NEW_CONTENT'                   => $new_content->check_if_is_new_content($this->publishing_start_date != null ? $this->publishing_start_date->get_timestamp() : $this->get_date_created()->get_timestamp()) && $this->is_published(),




Exemple avec le module Articles (L 11 et L 36):



Articles.class.php :
  1.  
  2. public function get_array_tpl_vars()
  3. {
  4. $category = $this->get_category();
  5. $contents = FormatingHelper::second_parse($this->contents);
  6. $description = $this->get_real_description();
  7. $user = $this->get_author_user();
  8. $user_group_color = User::get_group_color($user->get_groups(), $user->get_level(), true);
  9. $sources = $this->get_sources();
  10. $nbr_sources = count($sources);
  11. $new_content = new ArticlesNewContent();
  12. $notation_config = new ArticlesNotation();
  13.  
  14. return array_merge(
  15. Date::get_array_tpl_vars($this->date_created, 'date'),
  16. Date::get_array_tpl_vars($this->date_updated, 'date_updated'),
  17. Date::get_array_tpl_vars($this->publishing_start_date, 'publishing_start_date'),
  18. Date::get_array_tpl_vars($this->publishing_end_date, 'publishing_end_date'),
  19. //Conditions
  20. 'C_EDIT' => $this->is_authorized_to_edit(),
  21. 'C_DELETE' => $this->is_authorized_to_delete(),
  22. 'C_HAS_PICTURE' => $this->has_picture(),
  23. 'C_USER_GROUP_COLOR' => !empty($user_group_color),
  24. 'C_PUBLISHED' => $this->is_published(),
  25. 'C_PUBLISHING_START_AND_END_DATE' => $this->publishing_start_date != null && $this->publishing_end_date != null,
  26. 'C_PUBLISHING_START_DATE' => $this->publishing_start_date != null,
  27. 'C_PUBLISHING_END_DATE' => $this->publishing_end_date != null,
  28. 'C_DATE_UPDATED' => $this->date_updated != null,
  29. 'C_AUTHOR_DISPLAYED' => $this->get_author_name_displayed(),
  30. 'C_AUTHOR_CUSTOM_NAME' => $this->is_author_custom_name_enabled(),
  31. 'C_NOTATION_ENABLED' => $notation_config->is_notation_enabled(),
  32. 'C_READ_MORE' => !$this->get_description_enabled() && TextHelper::strlen($contents) > ArticlesConfig::load()->get_number_character_to_cut() && $description != @strip_tags($contents, '<br><br/>'),
  33. 'C_SOURCES' => $nbr_sources > 0,
  34. 'C_DIFFERED' => $this->published == self::PUBLISHED_DATE,
  35. 'C_NEW_CONTENT' => $new_content->check_if_is_new_content($this->publishing_start_date != null ? $this->publishing_start_date->get_timestamp() : $this->get_date_created()->get_timestamp()) && $this->is_published(),
  36.  




La variable C_NEW_CONTENT peut être différente suivant les champs utilisés dans votre module. Si votre module utilise une date de publication :



Code PHP :
'C_NEW_CONTENT'   => $new_content->check_if_is_new_content($this->publishing_start_date != null ? $this->publishing_start_date->get_timestamp() : $this->get_date_created()->get_timestamp()) && $this->is_published(),




Sinon :



Code PHP :
'C_NEW_CONTENT' => $new_content->check_if_is_new_content($this->get_creation_date()->get_timestamp()),




Il ne reste maintenant qu'à intégrer notre variable C_NEW_CONTENT dans le template

Voici deux exemples avec le module Articles:



ArticlesDisplayArticlesController.tpl :
<div class="content">
        # INCLUDE  NOT_VISIBLE_MESSAGE #
<article itemscope="itemscope" itemtype="http://schema.org/Article" id="article-articles-{ID}" class="article-articles# IF C_NEW_CONTENT # new-content# ENDIF #">
 




ArticlesDisplaySeveralArticlesController.tpl :
 
<div class="content elements-container# IF C_SEVERAL_COLUMNS # columns-{NUMBER_COLUMNS}# ENDIF#">
            # START articles #
<article id="article-articles-{articles.ID}" class="article-articles article-several# IF C_MOSAIC # block# ENDIF ## IF articles.C_NEW_CONTENT # new-content# ENDIF #" itemscope="itemscope" itemtype="http://schema.org/CreativeWork">
 




Affichage des catégories d'un formulaire s'il existe au moins une catégorie (facultatif)






Ce formulaire ne sera pas affiché s'il n'y a pas de catégorie créée :

form_display_localisation



Utilisation de MonModuleService::get_categories_manager()->get_categories_cache()->has_categories().



Exemple avec le module Articles, on ajoute la condition :



Code PHP :
if (ArticlesService::get_categories_manager()->get_categories_cache()->has_categories())
        { ......}


à



Code PHP :
$search_category_children_options = new SearchCategoryChildrensOptions();
            $search_category_children_options->add_authorizations_bits(Category::CONTRIBUTION_AUTHORIZATIONS);
            $search_category_children_options->add_authorizations_bits(Category::WRITE_AUTHORIZATIONS);
            $fieldset->add_field(ArticlesService::get_categories_manager()->get_select_categories_form_field('id_category', $this->common_lang['form.category'], $this->get_article()->get_id_category(), $search_category_children_options));




Il faut faire de même pour la méthode save().



Voici les deux modifications en exemple :



ArticlesFormController.class.php :
    private function build_form(HTTPRequestCustom $request)
    {
.
.
.
if (ArticlesService::get_categories_manager()->get_categories_cache()->has_categories())
        {
            $search_category_children_options = new SearchCategoryChildrensOptions();
            $search_category_children_options->add_authorizations_bits(Category::CONTRIBUTION_AUTHORIZATIONS);
            $search_category_children_options->add_authorizations_bits(Category::WRITE_AUTHORIZATIONS);
            $fieldset->add_field(ArticlesService::get_categories_manager()->get_select_categories_form_field('id_category', $this->common_lang['form.category'], $this->get_article()->get_id_category(), $search_category_children_options));
        }
.
.
.
.
    private function save()
    {
        $article = $this->get_article();
 
        $article->set_title($this->form->get_value('title'));
 
        if (ArticlesService::get_categories_manager()->get_categories_cache()->has_categories())
            $article->set_id_category($this->form->get_value('id_category')->get_raw_value());
.
.
.

Cette page a été vue 1354 fois