1<?php
2/* Copyright (C) 2010-2012 Regis Houssin  <regis.houssin@inodbox.com>
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/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 */
28abstract 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)) {
75			$object->fetch($id);
76		}
77			$this->object = $object;
78		//}
79	}
80
81	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
82	/**
83	 *  Set content of ->tpl array, to use into template
84	 *
85	 *  @param	string		$action    Type of action
86	 *  @param	int			$id			Id
87	 *  @return	string					HTML output
88	 */
89	public function assign_values(&$action, $id)
90	{
91		// phpcs:enable
92		global $conf, $langs, $user, $canvas;
93		global $form, $formcompany, $objsoc;
94
95		if ($action == 'add' || $action == 'update') {
96			$this->assign_post();
97		}
98
99		foreach ($this->object as $key => $value) {
100			$this->tpl[$key] = $value;
101		}
102
103		$this->tpl['error'] = $this->error;
104		$this->tpl['errors'] = $this->errors;
105
106		if ($action == 'create' || $action == 'edit') {
107			if ($conf->use_javascript_ajax) {
108				$this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
109				jQuery(document).ready(function () {
110						jQuery("#selectcountry_id").change(function() {
111							document.formsoc.action.value="'.$action.'";
112							document.formsoc.canvas.value="'.$canvas.'";
113							document.formsoc.submit();
114						});
115					})
116				</script>'."\n";
117			}
118
119			if (is_object($objsoc) && $objsoc->id > 0) {
120				$this->tpl['company'] = $objsoc->getNomUrl(1);
121				$this->tpl['company_id'] = $objsoc->id;
122			} else {
123				$this->tpl['company'] = $form->select_company($this->object->socid, 'socid', '', 1);
124			}
125
126			// Civility
127			$this->tpl['select_civility'] = $formcompany->select_civility($this->object->civility_id);
128
129			// Predefined with third party
130			if ((isset($objsoc->typent_code) && $objsoc->typent_code == 'TE_PRIVATE') || !empty($conf->global->CONTACT_USE_COMPANY_ADDRESS)) {
131				if (dol_strlen(trim($this->object->address)) == 0) {
132					$this->tpl['address'] = $objsoc->address;
133				}
134				if (dol_strlen(trim($this->object->zip)) == 0) {
135					$this->object->zip = $objsoc->zip;
136				}
137				if (dol_strlen(trim($this->object->town)) == 0) {
138					$this->object->town = $objsoc->town;
139				}
140				if (dol_strlen(trim($this->object->phone_pro)) == 0) {
141					$this->object->phone_pro = $objsoc->phone;
142				}
143				if (dol_strlen(trim($this->object->fax)) == 0) {
144					$this->object->fax = $objsoc->fax;
145				}
146				if (dol_strlen(trim($this->object->email)) == 0) {
147					$this->object->email = $objsoc->email;
148				}
149			}
150
151			// Zip
152			$this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 6);
153
154			// Town
155			$this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
156
157			if (dol_strlen(trim($this->object->country_id)) == 0) {
158				$this->object->country_id = $objsoc->country_id;
159			}
160
161			// Country
162			$this->tpl['select_country'] = $form->select_country($this->object->country_id, 'country_id');
163			$countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
164
165			if ($user->admin) {
166				$this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
167			}
168
169			// State
170			if ($this->object->country_id) {
171				$this->tpl['select_state'] = $formcompany->select_state($this->object->state_id, $this->object->country_code);
172			} else {
173				$this->tpl['select_state'] = $countrynotdefined;
174			}
175
176			// Public or private
177			$selectarray = array('0'=>$langs->trans("ContactPublic"), '1'=>$langs->trans("ContactPrivate"));
178			$this->tpl['select_visibility'] = $form->selectarray('priv', $selectarray, $this->object->priv, 0);
179		}
180
181		if ($action == 'view' || $action == 'edit' || $action == 'delete') {
182			// Emailing
183			if (!empty($conf->mailing->enabled)) {
184				$langs->load("mails");
185				$this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
186			}
187
188			// Linked element
189			$this->tpl['contact_element'] = array();
190			$i = 0;
191
192			$this->object->load_ref_elements();
193
194			if (!empty($conf->commande->enabled)) {
195				$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
196				$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande ? $this->object->ref_commande : $langs->trans("NoContactForAnyOrder");
197				$i++;
198			}
199			if (!empty($conf->propal->enabled)) {
200				$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
201				$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal ? $this->object->ref_propal : $langs->trans("NoContactForAnyProposal");
202				$i++;
203			}
204			if (!empty($conf->contrat->enabled)) {
205				$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
206				$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat ? $this->object->ref_contrat : $langs->trans("NoContactForAnyContract");
207				$i++;
208			}
209			if (!empty($conf->facture->enabled)) {
210				$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
211				$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation ? $this->object->ref_facturation : $langs->trans("NoContactForAnyInvoice");
212				$i++;
213			}
214
215			// Dolibarr user
216			if ($this->object->user_id) {
217				$dolibarr_user = new User($this->db);
218				$result = $dolibarr_user->fetch($this->object->user_id);
219				$this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
220			} else {
221				$this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
222			}
223		}
224
225		if ($action == 'view' || $action == 'delete') {
226			$this->tpl['showrefnav'] = $form->showrefnav($this->object, 'id');
227
228			if ($this->object->socid > 0) {
229				$objsoc = new Societe($this->db);
230
231				$objsoc->fetch($this->object->socid);
232				$this->tpl['company'] = $objsoc->getNomUrl(1);
233			} else {
234				$this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
235			}
236
237			$this->tpl['civility'] = $this->object->getCivilityLabel();
238
239			$this->tpl['address'] = dol_nl2br($this->object->address);
240
241			$this->tpl['zip'] = ($this->object->zip ? $this->object->zip.'&nbsp;' : '');
242
243			$img = picto_from_langcode($this->object->country_code);
244			$this->tpl['country'] = ($img ? $img.' ' : '').$this->object->country;
245
246			$this->tpl['phone_pro'] = dol_print_phone($this->object->phone_pro, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
247			$this->tpl['phone_perso'] = dol_print_phone($this->object->phone_perso, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
248			$this->tpl['phone_mobile'] = dol_print_phone($this->object->phone_mobile, $this->object->country_code, 0, $this->object->id, 'AC_TEL');
249			$this->tpl['fax'] = dol_print_phone($this->object->fax, $this->object->country_code, 0, $this->object->id, 'AC_FAX');
250			$this->tpl['email'] = dol_print_email($this->object->email, 0, $this->object->id, 'AC_EMAIL');
251
252			$this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
253
254			$this->tpl['note'] = nl2br($this->object->note);
255		}
256
257		if ($action == 'create_user') {
258			// Full firstname and lastname separated with a dot : firstname.lastname
259			include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
260			require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
261			$login = dol_buildlogin($this->object->lastname, $this->object->firstname);
262
263			$generated_password = getRandomPassword(false);
264			$password = $generated_password;
265
266			// Create a form array
267			$formquestion = array(
268			array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
269			array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
270
271			$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');
272		}
273	}
274
275	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
276	/**
277	 *  Assign POST values into object
278	 *
279	 *  @return		string					HTML output
280	 */
281	private function assign_post()
282	{
283		// phpcs:enable
284		global $langs, $mysoc;
285
286		$this->object->socid = GETPOST("socid", 'int');
287		$this->object->lastname			= GETPOST("name");
288		$this->object->firstname		= GETPOST("firstname");
289		$this->object->civility_id = GETPOST("civility_id");
290		$this->object->poste			= GETPOST("poste");
291		$this->object->address = GETPOST("address");
292		$this->object->zip = GETPOST("zipcode");
293		$this->object->town				= GETPOST("town");
294		$this->object->country_id = GETPOST("country_id") ? GETPOST("country_id") : $mysoc->country_id;
295		$this->object->state_id = GETPOST("state_id");
296		$this->object->phone_pro = GETPOST("phone_pro");
297		$this->object->phone_perso = GETPOST("phone_perso");
298		$this->object->phone_mobile = GETPOST("phone_mobile");
299		$this->object->fax = GETPOST("fax");
300		$this->object->email			= GETPOST("email");
301		$this->object->priv				= GETPOST("priv");
302		$this->object->note				= GETPOST("note", "restricthtml");
303		$this->object->canvas = GETPOST("canvas");
304
305		// We set country_id, and country_code label of the chosen country
306		if ($this->object->country_id) {
307			$sql = "SELECT code, label FROM ".MAIN_DB_PREFIX."c_country WHERE rowid = ".((int) $this->object->country_id);
308			$resql = $this->db->query($sql);
309			if ($resql) {
310				$obj = $this->db->fetch_object($resql);
311			} else {
312				dol_print_error($this->db);
313			}
314			$this->object->country_id = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
315			$this->object->country_code = $obj->code;
316			$this->object->country = $langs->trans("Country".$obj->code) ? $langs->trans("Country".$obj->code) : $obj->label;
317		}
318	}
319}
320