Issues (2811)

Dolibarr/Modules/Website.php (1 issue)

Labels
Severity
1
<?php
2
3
/* Copyright (C) 2015      Laurent Destailleur <[email protected]>
4
 * Copyright (C) 2024       Rafael San José             <[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace Dolibarr\Modules;
21
22
/**
23
 *  \defgroup   website     Module website
24
 *  \brief      website module descriptor.
25
 *  \file       htdocs/core/modules/modWebsite.class.php
26
 *  \ingroup    websites
27
 *  \brief      Description and activation file for the module Website
28
 */
29
30
use Dolibarr\Core\Base\DolibarrModules;
31
32
/**
33
 *  Class to describe Websites module
34
 */
35
class Website extends DolibarrModules
36
{
37
    /**
38
     *   Constructor. Define names, constants, directories, boxes, permissions
39
     *
40
     * @param DoliDB $db Database handler
0 ignored issues
show
The type Dolibarr\Modules\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
41
     */
42
    public function __construct($db)
43
    {
44
        $this->db = $db;
45
        $this->numero = 10000;
46
47
        // Family can be 'crm','financial','hr','projects','products','ecm','technic','other'
48
        // It is used to group modules in module setup page
49
        $this->family = "portal";
50
        $this->module_position = '50';
51
        // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
52
        $this->name = preg_replace('/^mod/i', '', get_only_class($this));
53
        $this->description = "Enable to build and serve public web sites with CMS features";
54
        // Possible values for version are: 'development', 'experimental', 'dolibarr' or version
55
        $this->version = 'dolibarr';
56
        // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
57
        $this->const_name = 'MAIN_MODULE_' . static::getNameOf($this->name); // strtoupper($this->name);
58
        // Name of image file used for this module.
59
        $this->picto = 'website';
60
61
        // Data directories to create when module is enabled
62
        $this->dirs = array("/website/temp");
63
64
        // Config pages
65
        $this->config_page_url = array('website.php');
66
67
        // Dependencies
68
        $this->hidden = getDolGlobalInt('MODULE_WEBSITE_DISABLED'); // A condition to disable module
69
        $this->depends = array('modFckeditor'); // List of modules id that must be enabled if this module is enabled
70
        $this->requiredby = array(); // List of modules id to disable if this one is disabled
71
        $this->conflictwith = array(); // List of modules id this module is in conflict with
72
        $this->phpmin = array(7, 0); // Minimum version of PHP required by module
73
        $this->langfiles = array("website");
74
75
        // Constants
76
        $this->const = array();
77
78
        // New pages on tabs
79
        //$this->tabs[] = array();                      // To add a new tab identified by code tabname1
80
81
        // Boxes
82
        $this->boxes = array();
83
84
        // Permissions
85
        $this->rights = array(); // Permission array used by this module
86
        $this->rights_class = 'website';
87
        $r = 0;
88
89
        $this->rights[$r][0] = 10001;
90
        $this->rights[$r][1] = 'Read website content';
91
        $this->rights[$r][3] = 0;
92
        $this->rights[$r][4] = 'read';
93
        $r++;
94
95
        $this->rights[$r][0] = 10002;
96
        $this->rights[$r][1] = 'Create/modify website content (html and javascript content)';
97
        $this->rights[$r][3] = 0;
98
        $this->rights[$r][4] = 'write';
99
        $r++;
100
101
        $this->rights[$r][0] = 10003;
102
        $this->rights[$r][1] = 'Create/modify website content (dynamic php code). Dangerous, must be reserved to restricted developers.';
103
        $this->rights[$r][3] = 0;
104
        $this->rights[$r][4] = 'writephp';
105
        $r++;
106
107
        $this->rights[$r][0] = 10005;
108
        $this->rights[$r][1] = 'Delete website content';
109
        $this->rights[$r][3] = 0;
110
        $this->rights[$r][4] = 'delete';
111
        $r++;
112
113
        $this->rights[$r][0] = 10008;
114
        $this->rights[$r][1] = 'Export website content';
115
        $this->rights[$r][3] = 0;
116
        $this->rights[$r][4] = 'export';
117
        $r++;
118
119
        // Main menu entries
120
        $r = 0;
121
        $this->menu[$r] = array('fk_menu' => '0', // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
122
            'type' => 'top', // This is a Left menu entry
123
            'titre' => 'WebSites',
124
            'prefix' => img_picto('', $this->picto, 'class="pictofixedwidth em092"'),
125
            'mainmenu' => 'website',
126
            'url' => '/website/index.php',
127
            'langs' => 'website', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory.
128
            'position' => 100,
129
            'enabled' => '$conf->website->enabled', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected.
130
            'perms' => '$user->rights->website->read', // Use 'perms'=>'$user->hasRight("mymodule","level1","level2")' if you want your menu with a permission rules
131
            'target' => '',
132
            'user' => 2); // 0=Menu for internal users, 1=external users, 2=both
133
        $r++;
134
135
        // Exports
136
        $r = 1;
137
138
        $this->export_code[$r] = $this->rights_class . '_' . $r;
139
        $this->export_label[$r] = 'MyWebsitePages'; // Translation key (used only if key ExportDataset_xxx_z not found)
140
        $this->export_permission[$r] = array(array("website", "export"));
141
        $this->export_icon[$r] = 'globe';
142
        $keyforclass = 'WebsitePage';
143
        $keyforclassfile = '/website/class/websitepage.class.php';
144
        $keyforelement = 'Website';
145
        include DOL_DOCUMENT_ROOT . '/core/commonfieldsinexport.inc.php';
146
        //$keyforselect='myobject'; $keyforelement='myobject'; $keyforaliasextra='extra';
147
        //include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
148
        $this->export_sql_start[$r] = 'SELECT DISTINCT ';
149
        $this->export_sql_end[$r] = ' FROM ' . MAIN_DB_PREFIX . 'website_page as t, ' . MAIN_DB_PREFIX . 'website as p';
150
        $this->export_sql_end[$r] .= ' WHERE t.fk_website = p.rowid';
151
        $this->export_sql_end[$r] .= ' AND p.entity IN (' . getEntity('website') . ')';
152
        $r++;
153
    }
154
155
156
    /**
157
     *  Function called when module is enabled.
158
     *  The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
159
     *  It also creates data directories
160
     *
161
     * @param string $options Options when enabling module ('', 'noboxes')
162
     * @return     int                 1 if OK, 0 if KO
163
     */
164
    public function init($options = '')
165
    {
166
        global $conf, $langs;
167
168
        $error = 0;
169
170
        $result = $this->_load_tables('/install/mysql/', 'website');
171
        if ($result < 0) {
172
            return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default')
173
        }
174
175
        // Remove permissions and default values
176
        $this->remove($options);
177
178
        // Copy flags and octicons directory
179
        $dirarray = array('common/flags' => 'flags', 'common/octicons/build/svg' => 'octicons');
180
        foreach ($dirarray as $dirfrom => $dirtarget) {
181
            $src = DOL_DOCUMENT_ROOT . '/theme/' . $dirfrom;
182
            $dest = DOL_DATA_ROOT . '/medias/image/' . $dirtarget;
183
184
            if (is_dir($src)) {
185
                require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/files.lib.php';
186
                dol_mkdir($dest);
187
                $result = dolCopyDir($src, $dest, 0, 0);
188
                if ($result < 0) {
189
                    $langs->load("errors");
190
                    $this->error = $langs->trans('ErrorFailToCopyDir', $src, $dest);
191
                    $this->errors[] = $langs->trans('ErrorFailToCopyDir', $src, $dest);
192
                    $error++;
193
                }
194
            }
195
        }
196
197
        if ($error) {
198
            return 0;
199
        }
200
201
        // Website templates
202
        $srcroot = DOL_DOCUMENT_ROOT . '/install/doctemplates/websites';
203
        $destroot = DOL_DATA_ROOT . '/doctemplates/websites';
204
205
        dol_mkdir($destroot);
206
207
        // Copy templates in zip format (old)
208
        $docs = dol_dir_list($srcroot, 'files', 0, 'website_.*(\.zip|\.jpg)$');
209
        foreach ($docs as $cursorfile) {
210
            $src = $srcroot . '/' . $cursorfile['name'];
211
            $dest = $destroot . '/' . $cursorfile['name'];
212
213
            $result = dol_copy($src, $dest, 0, 1); // For full zip templates, we overwrite old existing files
214
            if ($result < 0) {
215
                $langs->load("errors");
216
                $this->error = $langs->trans('ErrorFailToCopyFile', $src, $dest);
217
                $this->errors[] = $langs->trans('ErrorFailToCopyFile', $src, $dest);
218
                $error++;
219
            }
220
        }
221
222
        // Copy templates in dir format (recommended) into zip file
223
        $docs = dol_dir_list($srcroot, 'directories', 0, 'website_.*$');
224
        foreach ($docs as $cursorfile) {
225
            $src = $srcroot . '/' . $cursorfile['name'];
226
            $dest = $destroot . '/' . $cursorfile['name'];
227
228
            // Compress it
229
            global $errormsg;
230
            $errormsg = '';
231
            $result = dol_compress_dir($src, $dest . '.zip', 'zip');
232
            if ($result < 0) {
233
                $error++;
234
                $this->error = ($errormsg ? $errormsg : $langs->trans('ErrorFailToCreateZip', $dest));
235
                $this->errors[] = ($errormsg ? $errormsg : $langs->trans('ErrorFailToCreateZip', $dest));
236
            }
237
        }
238
239
        if ($error) {
240
            return 0;
241
        }
242
243
        $sql = array();
244
245
        return $this->_init($sql, $options);
246
    }
247
}
248