Passed
Branch develop (a7390e)
by
unknown
25:38
created

ActionsContactCardCommon::assign_values()   F

Complexity

Conditions 40
Paths > 20000

Size

Total Lines 180
Code Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 40
eloc 94
c 0
b 0
f 0
nc 48695400
nop 2
dl 0
loc 180
rs 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) 2010-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 <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *	\file       htdocs/contact/canvas/actions_contactcard_common.class.php
20
 *	\ingroup    thirdparty
21
 *	\brief      Fichier de la classe Thirdparty contact card controller (common)
22
 */
23
24
/**
25
 *	\class      ActionsContactCardCommon
26
 *	\brief      Classe permettant la gestion des contacts par defaut
27
 */
28
abstract class ActionsContactCardCommon
29
{
30
    /**
31
     * @var DoliDB Database handler.
32
     */
33
    public $db;
34
35
    public $dirmodule;
36
    public $targetmodule;
37
    public $canvas;
38
    public $card;
39
40
	//! Template container
41
	public $tpl = array();
42
	//! Object container
43
	public $object;
44
45
	/**
46
	 * @var string Error code (or message)
47
	 */
48
	public $error='';
49
50
51
	/**
52
	 * @var string[] Error codes (or messages)
53
	 */
54
	public $errors = array();
55
56
57
	/**
58
     *  Get object
59
	 *
60
     *  @param	int		$id		Object id
61
     *  @return	object			Object loaded
62
     */
63
    public function getObject($id)
64
    {
65
    	/*$ret = $this->getInstanceDao();
66
67
    	if (is_object($this->object) && method_exists($this->object,'fetch'))
68
    	{
69
    		if (! empty($id)) $this->object->fetch($id);
70
    	}
71
    	else
72
    	{*/
73
    		$object = new Contact($this->db);
74
    		if (! empty($id)) $object->fetch($id);
75
            $this->object = $object;
76
    	//}
77
    }
78
79
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
80
    /**
81
     *  Set content of ->tpl array, to use into template
82
     *
83
     *  @param	string		$action    Type of action
84
     *  @param	int			$id			Id
85
     *  @return	string					HTML output
86
     */
87
    public function assign_values(&$action, $id)
88
    {
89
        // phpcs:enable
90
        global $conf, $langs, $user, $canvas;
91
        global $form, $formcompany, $objsoc;
92
93
        if ($action == 'add' || $action == 'update') $this->assign_post();
94
95
        foreach($this->object as $key => $value)
96
        {
97
            $this->tpl[$key] = $value;
98
        }
99
100
        $this->tpl['error']=$this->error;
101
        $this->tpl['errors']=$this->errors;
102
103
        if ($action == 'create' || $action == 'edit')
104
        {
105
        	if ($conf->use_javascript_ajax)
106
			{
107
				$this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
108
				jQuery(document).ready(function () {
109
						jQuery("#selectcountry_id").change(function() {
110
							document.formsoc.action.value="'.$action.'";
111
							document.formsoc.canvas.value="'.$canvas.'";
112
							document.formsoc.submit();
113
						});
114
					})
115
				</script>'."\n";
116
			}
117
118
        	if (is_object($objsoc) && $objsoc->id > 0)
119
        	{
120
        		$this->tpl['company'] = $objsoc->getNomUrl(1);
121
        		$this->tpl['company_id'] = $objsoc->id;
122
        	}
123
        	else
124
        	{
125
        		$this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
126
        	}
127
128
        	// Civility
129
        	$this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
130
131
        	// Predefined with third party
132
        	if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || ! empty($conf->global->CONTACT_USE_COMPANY_ADDRESS))
133
        	{
134
        		if (dol_strlen(trim($this->object->address)) == 0) $this->tpl['address'] = $objsoc->address;
135
        		if (dol_strlen(trim($this->object->zip)) == 0) $this->object->zip = $objsoc->zip;
136
        		if (dol_strlen(trim($this->object->town)) == 0) $this->object->town = $objsoc->town;
137
        		if (dol_strlen(trim($this->object->phone_pro)) == 0) $this->object->phone_pro = $objsoc->phone;
138
        		if (dol_strlen(trim($this->object->fax)) == 0) $this->object->fax = $objsoc->fax;
139
        		if (dol_strlen(trim($this->object->email)) == 0) $this->object->email = $objsoc->email;
140
        	}
141
142
            // Zip
143
            $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town','selectcountry_id','state_id'), 6);
144
145
            // Town
146
            $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode','selectcountry_id','state_id'));
147
148
            if (dol_strlen(trim($this->object->country_id)) == 0) $this->object->country_id = $objsoc->country_id;
149
150
            // Country
151
            $this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
152
            $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
153
154
            if ($user->admin) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
155
156
            // State
157
            if ($this->object->country_id) $this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
158
            else $this->tpl['select_state'] = $countrynotdefined;
159
160
            // Public or private
161
            $selectarray=array('0'=>$langs->trans("ContactPublic"),'1'=>$langs->trans("ContactPrivate"));
162
            $this->tpl['select_visibility'] = $form->selectarray('priv', $selectarray, $this->object->priv, 0);
163
        }
164
165
        if ($action == 'view' || $action == 'edit' || $action == 'delete')
166
        {
167
        	// Emailing
168
        	if (! empty($conf->mailing->enabled))
169
			{
170
				$langs->load("mails");
171
				$this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
172
			}
173
174
        	// Linked element
175
        	$this->tpl['contact_element'] = array();
176
        	$i=0;
177
178
        	$this->object->load_ref_elements();
179
180
        	if (! empty($conf->commande->enabled))
181
        	{
182
        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
183
        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande?$this->object->ref_commande:$langs->trans("NoContactForAnyOrder");
184
        		$i++;
185
        	}
186
        	if (! empty($conf->propal->enabled))
187
        	{
188
        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
189
        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal?$this->object->ref_propal:$langs->trans("NoContactForAnyProposal");
190
        		$i++;
191
        	}
192
        	if (! empty($conf->contrat->enabled))
193
        	{
194
        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
195
        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat?$this->object->ref_contrat:$langs->trans("NoContactForAnyContract");
196
        		$i++;
197
        	}
198
        	if (! empty($conf->facture->enabled))
199
        	{
200
        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
201
        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation?$this->object->ref_facturation:$langs->trans("NoContactForAnyInvoice");
202
        		$i++;
203
        	}
204
205
        	// Dolibarr user
206
        	if ($this->object->user_id)
207
			{
208
				$dolibarr_user=new User($this->db);
209
				$result=$dolibarr_user->fetch($this->object->user_id);
210
				$this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
211
			}
212
			else $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
213
        }
214
215
        if ($action == 'view' || $action == 'delete')
216
        {
217
        	$this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
218
219
        	if ($this->object->socid > 0)
220
        	{
221
        		$objsoc = new Societe($this->db);
222
223
        		$objsoc->fetch($this->object->socid);
224
        		$this->tpl['company'] = $objsoc->getNomUrl(1);
225
        	}
226
        	else
227
        	{
228
        		$this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
229
        	}
230
231
        	$this->tpl['civility'] = $this->object->getCivilityLabel();
232
233
            $this->tpl['address'] = dol_nl2br($this->object->address);
234
235
            $this->tpl['zip'] = ($this->object->zip?$this->object->zip.'&nbsp;':'');
236
237
            $img=picto_from_langcode($this->object->country_code);
238
            $this->tpl['country'] = ($img?$img.' ':'').$this->object->country;
239
240
            $this->tpl['phone_pro'] 	= dol_print_phone($this->object->phone_pro, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
241
            $this->tpl['phone_perso'] 	= dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
242
            $this->tpl['phone_mobile'] 	= dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
243
            $this->tpl['fax'] 			= dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
244
            $this->tpl['email'] 		= dol_print_email($this->object->email, 0, $this->object->id, 'AC_EMAIL');
245
246
            $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
247
248
            $this->tpl['note'] = nl2br($this->object->note);
249
        }
250
251
        if ($action == 'create_user')
252
        {
253
        	// Full firstname and lastname separated with a dot : firstname.lastname
254
        	include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
255
            require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
256
        	$login=dol_buildlogin($this->object->lastname, $this->object->firstname);
257
258
       		$generated_password=getRandomPassword(false);
259
        	$password=$generated_password;
260
261
        	// Create a form array
262
        	$formquestion=array(
263
        	array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
264
        	array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
265
266
        	$this->tpl['action_create_user'] = $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$this->object->id, $langs->trans("CreateDolibarrLogin"), $langs->trans("ConfirmCreateContact"), "confirm_create_user", $formquestion, 'no');
267
        }
268
    }
269
270
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
271
    /**
272
     *  Assign POST values into object
273
     *
274
     *  @return		string					HTML output
275
     */
276
    private function assign_post()
277
    {
278
        // phpcs:enable
279
        global $langs, $mysoc;
280
281
        $this->object->old_name 		= $_POST["old_name"];
282
        $this->object->old_firstname 	= $_POST["old_firstname"];
283
284
        $this->object->socid			= $_POST["socid"];
285
        $this->object->lastname			= $_POST["name"];
286
        $this->object->firstname		= $_POST["firstname"];
287
        $this->object->civility_id		= $_POST["civility_id"];
288
        $this->object->poste			= $_POST["poste"];
289
        $this->object->address			= $_POST["address"];
290
        $this->object->zip				= $_POST["zipcode"];
291
        $this->object->town				= $_POST["town"];
292
        $this->object->country_id		= $_POST["country_id"]?$_POST["country_id"]:$mysoc->country_id;
293
        $this->object->state_id        	= $_POST["state_id"];
294
        $this->object->phone_pro		= $_POST["phone_pro"];
295
        $this->object->phone_perso		= $_POST["phone_perso"];
296
        $this->object->phone_mobile		= $_POST["phone_mobile"];
297
        $this->object->fax				= $_POST["fax"];
298
        $this->object->email			= $_POST["email"];
299
        $this->object->jabberid			= $_POST["jabberid"];
300
        $this->object->priv				= $_POST["priv"];
301
        $this->object->note				= $_POST["note"];
302
        $this->object->canvas			= $_POST["canvas"];
303
304
        // We set country_id, and country_code label of the chosen country
305
        if ($this->object->country_id)
306
        {
307
            $sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".$this->object->country_id;
308
            $resql=$this->db->query($sql);
309
            if ($resql)
310
            {
311
                $obj = $this->db->fetch_object($resql);
312
            }
313
            else
314
            {
315
                dol_print_error($this->db);
316
            }
317
            $this->object->country_id = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->label;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj does not seem to be defined for all execution paths leading up to this point.
Loading history...
318
            $this->object->country_code = $obj->code;
319
            $this->object->country = $langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->label;
320
        }
321
    }
322
}
323