Completed
Branch develop (59ab9a)
by
unknown
27:43
created

bookmarks.lib.php ➔ printBookmarksList()   C

Complexity

Conditions 10
Paths 24

Size

Total Lines 87
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 41
nc 24
nop 2
dl 0
loc 87
rs 5.2688
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
/* Copyright (C) 2009 Laurent Destailleur  <[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *	\file       htdocs/bookmarks/bookmarks.lib.php
20
 *	\ingroup	bookmarks
21
 *	\brief      File with library for bookmark module
22
 */
23
24
/**
25
 * Add area with bookmarks in menu
26
 *
27
 * @param 	DoliDb  	$aDb		Database handler
28
 * @param 	Translate	$aLangs		Object lang
29
 * @return	string
30
 */
31
function printBookmarksList($aDb, $aLangs)
32
{
33
	global $conf, $user;
34
35
	$db = $aDb;
36
	$langs = $aLangs;
37
38
	require_once DOL_DOCUMENT_ROOT.'/bookmarks/class/bookmark.class.php';
39
	if (! isset($conf->global->BOOKMARKS_SHOW_IN_MENU)) $conf->global->BOOKMARKS_SHOW_IN_MENU=5;
40
41
	$langs->load("bookmarks");
42
43
	$url= $_SERVER["PHP_SELF"].(! empty($_SERVER["QUERY_STRING"])?'?'.$_SERVER["QUERY_STRING"]:'');
44
    
45
	$ret = '';
46
	
47
	// Menu bookmark
48
	$ret.= '<div class="menu_top"></div>'."\n";
49
50
	$ret.= '<!-- form with POST method by default, will be replaced with GET for external link by js -->'."\n";
51
	$ret.= '<form id="actionbookmark" name="actionbookmark" method="POST" action="">';
52
	$ret.= '<select name="bookmark" id="boxbookmark" class="flat boxcombo vmenusearchselectcombo" alt="Bookmarks">';
53
	$ret.= '<option hidden value="listbookmarks" class="optiongrey" selected rel="'.DOL_URL_ROOT.'/bookmarks/list.php">'.$langs->trans('Bookmarks').'</option>';
54
    $ret.= '<option value="listbookmark" class="optionblue" rel="'.dol_escape_htmltag(DOL_URL_ROOT.'/bookmarks/list.php').'">'.dol_escape_htmltag($user->rights->bookmark->creer ? $langs->trans('EditBookmarks') : $langs->trans('ListOfBookmarks')).'...</option>';
55
	// Url to go on create new bookmark page
56
	if ($user->rights->bookmark->creer)
57
	{
58
    	$urltoadd=DOL_URL_ROOT.'/bookmarks/card.php?action=create&amp;urlsource='.urlencode($url).'&amp;url='.urlencode($url);
59
    	$ret.= '<option value="newbookmark" class="optionblue" rel="'.dol_escape_htmltag($urltoadd).'">'.dol_escape_htmltag($langs->trans('AddThisPageToBookmarks')).'...</option>';
60
	}
61
	// Menu with all bookmarks
62
	if (! empty($conf->global->BOOKMARKS_SHOW_IN_MENU))
63
	{
64
		$sql = "SELECT rowid, title, url, target FROM ".MAIN_DB_PREFIX."bookmark";
65
		$sql.= " WHERE (fk_user = ".$user->id." OR fk_user is NULL OR fk_user = 0)";
66
        $sql.= " AND entity IN (".getEntity('bookmarks',1).")";
67
		$sql.= " ORDER BY position";
68
		if ($resql = $db->query($sql) )
69
		{
70
			$i=0;
71
			while ($i < $conf->global->BOOKMARKS_SHOW_IN_MENU && $obj = $db->fetch_object($resql))
72
			{
73
			    $ret.='<option name="bookmark'.$obj->rowid.'" value="'.$obj->rowid.'" '.($obj->target == 1?' target="_blank"':'').' rel="'.dol_escape_htmltag($obj->url).'">';
74
			    //$ret.='<span class="fa fa-print">aa</span>';
75
			    $ret.=img_picto('','object_bookmark').' ';
76
			    $ret.=$obj->title;
77
			    $ret.='</option>';
78
				$i++;
79
			}
80
		}
81
		else
82
		{
83
			dol_print_error($db);
84
		}
85
	}
86
	
87
	$ret.= '</select>';
88
	$ret.= '</form>';
89
	
90
	$ret.=ajax_combobox('boxbookmark');
91
	
92
	$ret.='<script type="text/javascript">
93
        	$(document).ready(function () {';
94
	$ret.='    jQuery("#boxbookmark").change(function() {
95
	            var urlselected = jQuery("#boxbookmark option:selected").attr("rel");
96
	            var urltarget = jQuery("#boxbookmark option:selected").attr("target");
97
	            if (! urltarget) { urltarget=""; }
98
                jQuery("form#actionbookmark").attr("target",urltarget);
99
	            jQuery("form#actionbookmark").attr("action",urlselected);
100
	    
101
	            console.log("We change select bookmark. We choose urlselected="+urlselected+" with target="+urltarget);
102
	            
103
	            // Method is POST for internal link, GET for external
104
	            if (urlselected.startsWith(\'http\'))
105
	            {
106
	                var newmethod=\'GET\';
107
	                jQuery("form#actionbookmark").attr("method",newmethod);
108
	                console.log("We change method to newmethod="+newmethod);
109
	            }
110
	    
111
	            jQuery("#actionbookmark").submit();
112
	       });';
113
	$ret.='})</script>';
114
	$ret .= '<div class="menu_end"></div>';
115
116
	return $ret;
117
}
118
119