Passed
Push — EXTRACT_CLASSES ( 231cec...0382f2 )
by Rafael
65:54 queued 05:18
created

Bookmark::getNomUrl()   F

Complexity

Conditions 21
Paths 864

Size

Total Lines 64
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 41
nc 864
nop 5
dl 0
loc 64
rs 0.1888
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2005       Laurent Destailleur         <[email protected]>
4
 * Copyright (C) 2015       Marcos García               <[email protected]>
5
 * Copyright (C) 2024       Frédéric France             <[email protected]>
6
 * Copyright (C) 2024       Rafael San José             <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Dolibarr\Code\BookMarks\Classes;
23
24
use Dolibarr\Core\Base\CommonObject;
25
26
/**
27
 *      \file       htdocs/bookmarks/class/bookmark.class.php
28
 *      \ingroup    bookmark
29
 *      \brief      File of class to manage bookmarks
30
 */
31
32
/**
33
 *      Class to manage bookmarks
34
 */
35
class Bookmark extends CommonObject
36
{
37
    /**
38
     * @var string ID to identify managed object
39
     */
40
    public $element = 'bookmark';
41
42
    /**
43
     * @var string Name of table without prefix where object is stored
44
     */
45
    public $table_element = 'bookmark';
46
47
    /**
48
     * @var string  String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
49
     */
50
    public $picto = 'bookmark';
51
52
    /**
53
     * @var string  Last error number. For example: 'DB_ERROR_RECORD_ALREADY_EXISTS', '12345', ...
54
     */
55
    public $errno;
56
57
    /**
58
     * @var int ID
59
     */
60
    public $id;
61
62
    /**
63
     * @var int   User ID. If > 0, bookmark of one user. If == 0, bookmark public (for everybody)
64
     */
65
    public $fk_user;
66
67
    /**
68
     * Date creation record (datec)
69
     *
70
     * @var integer
71
     */
72
    public $datec;
73
74
    /**
75
     * @var string url
76
     */
77
    public $url;
78
79
    public $target; // 0=replace, 1=new window
80
81
    /**
82
     * @var string title
83
     */
84
    public $title;
85
86
    /**
87
     * @var int position of bookmark
88
     */
89
    public $position;
90
91
    /**
92
     * @var string favicon
93
     */
94
    public $favicon;
95
96
97
    /**
98
     *  Constructor
99
     *
100
     *  @param      DoliDB      $db      Database handler
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\BookMarks\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
101
     */
102
    public function __construct($db)
103
    {
104
        $this->db = $db;
105
106
        $this->ismultientitymanaged = 1;
107
    }
108
109
    /**
110
     *    Directs the bookmark
111
     *
112
     *    @param    int     $id     Bookmark Id Loader
113
     *    @return   int             Return integer <0 if KO, >0 if OK
114
     */
115
    public function fetch($id)
116
    {
117
        global $conf;
118
119
        $sql = "SELECT rowid, fk_user, dateb as datec, url, target,";
120
        $sql .= " title, position, favicon";
121
        $sql .= " FROM " . MAIN_DB_PREFIX . "bookmark";
122
        $sql .= " WHERE rowid = " . ((int) $id);
123
        $sql .= " AND entity = " . $conf->entity;
124
125
        dol_syslog("Bookmark::fetch", LOG_DEBUG);
126
        $resql = $this->db->query($sql);
127
        if ($resql) {
128
            $obj = $this->db->fetch_object($resql);
129
130
            $this->id = $obj->rowid;
131
            $this->ref = $obj->rowid;
132
133
            $this->fk_user = $obj->fk_user;
134
            $this->datec   = $this->db->jdate($obj->datec);
135
            $this->url     = $obj->url;
136
            $this->target  = $obj->target;
137
            $this->title   = $obj->title;
138
            $this->position = $obj->position;
139
            $this->favicon = $obj->favicon;
140
141
            $this->db->free($resql);
142
            return $this->id;
143
        } else {
144
            dol_print_error($this->db);
145
            return -1;
146
        }
147
    }
148
149
    /**
150
     *      Insert bookmark into database
151
     *
152
     *      @return     int     Return integer <0 si ko, rowid du bookmark cree si ok
153
     */
154
    public function create()
155
    {
156
        global $conf;
157
158
        // Clean parameters
159
        $this->url = trim($this->url);
160
        $this->title = trim($this->title);
161
        if (empty($this->position)) {
162
            $this->position = 0;
163
        }
164
165
        $now = dol_now();
166
167
        $this->db->begin();
168
169
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "bookmark (fk_user,dateb,url,target";
170
        $sql .= ",title,favicon,position";
171
        $sql .= ",entity";
172
        $sql .= ") VALUES (";
173
        $sql .= ($this->fk_user > 0 ? $this->fk_user : "0") . ",";
174
        $sql .= " '" . $this->db->idate($now) . "',";
175
        $sql .= " '" . $this->db->escape($this->url) . "', '" . $this->db->escape($this->target) . "',";
176
        $sql .= " '" . $this->db->escape($this->title) . "', '" . $this->db->escape($this->favicon) . "', " . (int) $this->position;
177
        $sql .= ", " . (int) $conf->entity;
178
        $sql .= ")";
179
180
        dol_syslog("Bookmark::create", LOG_DEBUG);
181
        $resql = $this->db->query($sql);
182
        if ($resql) {
183
            $id = $this->db->last_insert_id(MAIN_DB_PREFIX . "bookmark");
184
            if ($id > 0) {
185
                $this->id = $id;
186
                $this->db->commit();
187
                return $id;
188
            } else {
189
                $this->error = $this->db->lasterror();
190
                $this->errno = $this->db->lasterrno();
191
                $this->db->rollback();
192
                return -2;
193
            }
194
        } else {
195
            $this->error = $this->db->lasterror();
196
            $this->errno = $this->db->lasterrno();
197
            $this->db->rollback();
198
            return -1;
199
        }
200
    }
201
202
    /**
203
     *      Update bookmark record
204
     *
205
     *      @return     int         Return integer <0 if KO, > if OK
206
     */
207
    public function update()
208
    {
209
        // Clean parameters
210
        $this->url = trim($this->url);
211
        $this->title = trim($this->title);
212
        if (empty($this->position)) {
213
            $this->position = 0;
214
        }
215
216
        $sql = "UPDATE " . MAIN_DB_PREFIX . "bookmark";
217
        $sql .= " SET fk_user = " . ($this->fk_user > 0 ? $this->fk_user : "0");
218
        $sql .= " ,dateb = '" . $this->db->idate($this->datec) . "'";
219
        $sql .= " ,url = '" . $this->db->escape($this->url) . "'";
220
        $sql .= " ,target = '" . $this->db->escape($this->target) . "'";
221
        $sql .= " ,title = '" . $this->db->escape($this->title) . "'";
222
        $sql .= " ,favicon = '" . $this->db->escape($this->favicon) . "'";
223
        $sql .= " ,position = " . (int) $this->position;
224
        $sql .= " WHERE rowid = " . ((int) $this->id);
225
226
        dol_syslog("Bookmark::update", LOG_DEBUG);
227
        if ($this->db->query($sql)) {
228
            return 1;
229
        } else {
230
            $this->error = $this->db->lasterror();
231
            return -1;
232
        }
233
    }
234
235
    /**
236
     *      Removes the bookmark
237
     *
238
     *      @param      User    $user       User deleting
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\BookMarks\Classes\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
239
     *      @return     int                 Return integer <0 if KO, >0 if OK
240
     */
241
    public function delete($user)
242
    {
243
        $sql  = "DELETE FROM " . MAIN_DB_PREFIX . "bookmark";
244
        $sql .= " WHERE rowid = " . ((int) $this->id);
245
246
        $resql = $this->db->query($sql);
247
        if ($resql) {
248
            return 1;
249
        } else {
250
            $this->error = $this->db->lasterror();
251
            return -1;
252
        }
253
    }
254
255
    /**
256
     * Function used to replace a thirdparty id with another one.
257
     *
258
     * @param   DoliDB  $dbs        Database handler, because function is static we name it $dbs not $db to avoid breaking coding test
259
     * @param   int     $origin_id  Old thirdparty id
260
     * @param   int     $dest_id    New thirdparty id
261
     * @return  bool
262
     */
263
    public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
264
    {
265
        $tables = array(
266
            'bookmark'
267
        );
268
269
        return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
270
    }
271
272
    /**
273
     *  Return the label of the status
274
     *
275
     *  @param  int     $mode          0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
276
     *  @return string                 Label of status
277
     */
278
    public function getLibStatut($mode)
279
    {
280
        return '';
281
    }
282
283
    /**
284
     *  Return a link to the object card (with optionally the picto)
285
     *
286
     *  @param  int     $withpicto                  Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
287
     *  @param  string  $option                     On what the link point to ('nolink', ...)
288
     *  @param  int     $notooltip                  1=Disable tooltip
289
     *  @param  string  $morecss                    Add more css on link
290
     *  @param  int     $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
291
     *  @return string                              String with URL
292
     */
293
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
294
    {
295
        global $conf, $langs, $hookmanager;
296
297
        if (!empty($conf->dol_no_mouse_hover)) {
298
            $notooltip = 1; // Force disable tooltips
299
        }
300
301
        $result = '';
302
303
        $label = '<u>' . $langs->trans("Bookmark") . '</u>';
304
        $label .= '<br>';
305
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
306
307
        $url = constant('BASE_URL') . '/bookmarks/card.php?id=' . $this->id;
308
309
        if ($option != 'nolink') {
310
            // Add param to save lastsearch_values or not
311
            $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
312
            if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
313
                $add_save_lastsearch_values = 1;
314
            }
315
            if ($add_save_lastsearch_values) {
316
                $url .= '&save_lastsearch_values=1';
317
            }
318
        }
319
320
        $linkclose = '';
321
        if (empty($notooltip)) {
322
            if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
323
                $label = $langs->trans("ShowBookmark");
324
                $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
325
            }
326
            $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
327
            $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"';
328
        } else {
329
            $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
330
        }
331
332
        $linkstart = '<a href="' . $url . '"';
333
        $linkstart .= $linkclose . '>';
334
        $linkend = '</a>';
335
336
        $result .= $linkstart;
337
        if ($withpicto) {
338
            $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
339
        }
340
        if ($withpicto != 2) {
341
            $result .= $this->ref;
342
        }
343
        $result .= $linkend;
344
        //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
345
346
        global $action, $hookmanager;
347
        $hookmanager->initHooks(array('mybookmarkdao'));
348
        $parameters = array('id' => $this->id, 'getnomurl' => &$result);
349
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
350
        if ($reshook > 0) {
351
            $result = $hookmanager->resPrint;
352
        } else {
353
            $result .= $hookmanager->resPrint;
354
        }
355
356
        return $result;
357
    }
358
}
359