Passed
Branch develop (e2dd44)
by
unknown
106:32
created

CommonSocialNetworks::showSocialNetwork()   C

Complexity

Conditions 14
Paths 180

Size

Total Lines 59
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 29
c 1
b 0
f 0
nc 180
nop 2
dl 0
loc 59
rs 5.6

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) 2012 Regis Houssin  <[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 <https://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *       \file       htdocs/core/class/commonincoterm.class.php
20
 *       \ingroup    core
21
 *       \brief      File of the superclass of object classes that support incoterm (customer and supplier)
22
 */
23
24
25
/**
26
 *      Superclass for social networks
27
 */
28
trait CommonSocialNetworks
29
{
30
	/**
31
	 * Show social network part if the module is enabled with hiding functionality
32
	 *
33
	 * @param	array	$socialnetworks		Array of social networks
34
	 * @param	int		$colspan			Colspan
35
	 * @return 	void
36
	 */
37
	public function showSocialNetwork($socialnetworks, $colspan = 4)
38
	{
39
		global $object, $form, $langs;
40
41
		$nbofnetworks = count($socialnetworks);
42
		$nbactive = 0;
43
		foreach ($socialnetworks as $key => $value) {
44
			if (!empty($object->socialnetworks[$key])) {
45
				$nbactive++;
46
			}
47
		}
48
49
		if ($nbofnetworks > 1) {
50
			print '<tr><td><br><a class="paddingtop paddingbottom socialnetworklnk" id="socialnetworklnk" href="javascript:toogleSocialNetwork(true)"></a></td>';
51
			print '<td'.($colspan ? ' colspan="'.($colspan-1).'"' : '').'>';
52
			print '<br><a class="paddingtop paddingbottom socialnetworklnk" href="javascript:toogleSocialNetwork(true)"><span class="badge badge-secondary socialnetworklnk">'.$nbactive.'</span></a>';
53
			print '</td>';
54
			print '</tr>';
55
		}
56
		foreach ($socialnetworks as $key => $value) {
57
			if ($value['active'] || $nbofnetworks == 1) {
58
				print '<tr class="soc_network">';
59
				print '<td><label for="'.$value['label'].'">'.$form->editfieldkey($value['label'], $key, '', $object, 0).'</label></td>';
60
				print '<td colspan="3">';
61
				if (!empty($value['icon'])) {
62
					print '<span class="fa '.$value['icon'].' pictofixedwidth"></span>';
63
				}
64
				print '<input type="text" name="'.$key.'" id="'.$key.'" class="minwidth100 maxwidth300 widthcentpercentminusx" maxlength="80" value="'.dol_escape_htmltag(GETPOSTISSET($key) ? GETPOST($key, 'alphanohtml') : (empty($object->socialnetworks[$key]) ? '' : $object->socialnetworks[$key])).'">';
65
				print '</td>';
66
				print '</tr>';
67
			} elseif (!empty($object->socialnetworks[$key])) {
68
				print '<input type="hidden" name="'.$key.'" value="'.$object->socialnetworks[$key].'">';
69
			}
70
		}
71
		print '<tr><td'.($colspan ? ' colspan="'.$colspan.'"' : '').'><hr></td></tr>';
72
73
		if ($nbofnetworks > 1) {
74
			print '<script type="text/javascript">
75
		$("document").ready(function() { toogleSocialNetwork(false); });
76
77
		jQuery(".socialnetworklnk").onClick(function() {
78
			console.log("Click on link");
79
			toogleSocialNetwork(true);
80
		});
81
82
		function toogleSocialNetwork(chgCookieState) {
83
			const lnk = $("#socialnetworklnk");
84
			const items = $(".soc_network");
85
			var cookieState = document.cookie.split(";").some((item) => item.trim().startsWith("DOLUSER_SOCIALNETWORKS_SHOW=true")) == true;
86
87
			if (!chgCookieState) cookieState = !cookieState ;
88
89
			if (cookieState) {
90
				items.hide();
91
				lnk.text("'.dol_escape_js($langs->transnoentitiesnoconv("ShowSocialNetworks")).'...");
92
				if (chgCookieState) { document.cookie = "DOLUSER_SOCIALNETWORKS_SHOW=false; SameSite=Strict"};
93
			} else {
94
				items.show();
95
				lnk.text("'.dol_escape_js($langs->transnoentitiesnoconv("HideSocialNetworks")).'...");
96
				if (chgCookieState) { document.cookie = "DOLUSER_SOCIALNETWORKS_SHOW=true; SameSite=Strict";}
97
			}
98
		}
99
		</script>';
100
		}
101
	}
102
}
103