Widget.php 3.26 KB
Newer Older
Ghislain Loas's avatar
Ghislain Loas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


23
class Class_Systeme_Widget_Widget extends Class_Systeme_Widget_Abstract {
24 25
  use Trait_Versionable;

Ghislain Loas's avatar
Ghislain Loas committed
26

27 28
  protected function _load() {
    return $this->_transmute($this->_findLeaf($this->getId()));
Ghislain Loas's avatar
Ghislain Loas committed
29 30 31 32
  }


  protected function _findLeaf($id) {
33 34 35
    if ($cfg = $this->getProfile()->getLocalModuleAccueilConfig($id))
      return $cfg;
    return [];
Ghislain Loas's avatar
Ghislain Loas committed
36 37 38
  }


39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  protected function _cleanRootAndPreferences($datas) {
    $root_properties = ['type_module' => '',
                        'division' => '',
                        'id_module' => '',
                        'preferences' => '',
                        'parent_id' => '',
                        'type_menu' => '',
                        'libelle' => '',
                        'picto' => '',
                        'sous_menus' => []];


    $cleaned_datas = array_intersect_key($datas, $root_properties);
    $cleaned_datas['preferences'] = array_diff_key($datas,
                                                   $root_properties);
    return $cleaned_datas;
  }


Ghislain Loas's avatar
Ghislain Loas committed
58
  protected function _update() {
59 60 61 62
    $cleaned_datas = $this->_cleanRootAndPreferences(
                                                     array_merge($this->getLocalSettings(),
                                                                 $this->getNewDatas()));

Ghislain Loas's avatar
Ghislain Loas committed
63

64 65
    $cleaned_datas['profile_id'] = $this->getProfileId();
    $datas = $this->_getWidgetResources()->updateHook($cleaned_datas);
Ghislain Loas's avatar
Ghislain Loas committed
66

Ghislain Loas's avatar
Ghislain Loas committed
67 68 69 70 71 72 73 74 75
    $sub_menus = null;
    if(isset($datas['children']))
      $sub_menus = $this->_convertChildren($datas['children']);

    return $this->_save($datas);
  }


  protected function _save($datas) {
76
    $profil = $this->getProfile();
Ghislain Loas's avatar
Ghislain Loas committed
77
    $cfg = $profil->getCfgAccueilAsArray();
78
    $cfg['modules'][$this->getId()] = $datas;
Ghislain Loas's avatar
Ghislain Loas committed
79 80
    return $profil->setCfgAccueil($cfg)->save();
  }
81 82 83 84 85 86 87 88


  public function getResourcesProvider() {
    if(!parent::getResourcesProvider())
      $this->setResourcesProvider(new Class_Systeme_ModulesAccueil());

    return parent::getResourcesProvider();
  }
89 90 91 92 93 94


  public function getSuccessSaveMessage() {
    return $this->_('La configuration de la boite %s a été sauvegardée',
                    $this->getLabel());
  }
95 96 97 98 99


  public function shouldVersion() {
    return Class_Systeme_ModulesAccueil_ConteneurDeuxColonnes::CODE != $this->getResourcesDefinition()->getCode();
  }
100 101 102 103 104 105 106


  public function moveToSection($section) {
    return $this
      ->setNewDatas(['division' => $section])
      ->_update();
  }
Ghislain Loas's avatar
Ghislain Loas committed
107
}