PageRenderTime 636ms CodeModel.GetById 323ms app.highlight 120ms RepoModel.GetById 74ms app.codeStats 1ms

/htdocs/contact/canvas/actions_contactcard_common.class.php

https://github.com/asterix14/dolibarr
PHP | 468 lines | 327 code | 66 blank | 75 comment | 84 complexity | 561df7b01029b457a89e6c961bce0674 MD5 | raw file
  1<?php
  2/* Copyright (C) 2010-2011 Regis Houssin  <regis@dolibarr.fr>
  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 2 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 */
 28abstract class ActionsContactCardCommon
 29{
 30    var $db;
 31    var $dirmodule;
 32    var $targetmodule;
 33    var $canvas;
 34    var $card;
 35
 36	//! Template container
 37	var $tpl = array();
 38	//! Object container
 39	var $object;
 40	//! Error string
 41	var $error;
 42	//! Error array
 43	var $errors=array();
 44
 45	
 46	/**
 47	 * 	Instantiation of DAO class
 48	 * 
 49	 * 	@return	void
 50	 */
 51	private function getInstanceDao()
 52	{
 53		if (! is_object($this->object))
 54		{
 55			$modelclassfile = dol_buildpath('/'.$this->dirmodule.'/canvas/'.$this->canvas.'/dao_'.$this->targetmodule.'_'.$this->canvas.'.class.php');
 56	        if (file_exists($modelclassfile))
 57	        {
 58	            // Include dataservice class (model)
 59	            $ret = require_once($modelclassfile);
 60	            if ($ret)
 61	            {
 62	            	// Instantiate dataservice class (model)
 63	            	$modelclassname = 'Dao'.ucfirst($this->targetmodule).ucfirst($this->canvas);
 64	            	$this->object = new $modelclassname($this->db);
 65	            }
 66	        }
 67		}
 68	}
 69	
 70	/**
 71     *  Get object
 72	 *
 73     *  @param		int			Object id
 74     *  @return		object		Object loaded
 75     */
 76    function getObject($id)
 77    {
 78    	$ret = $this->getInstanceDao();
 79    	
 80    	if (is_object($this->object) && method_exists($this->object,'fetch'))
 81    	{
 82    		if (! empty($id)) $this->object->fetch($id);
 83    	}
 84    	else
 85    	{
 86    		$object = new Contact($this->db);
 87    		if (! empty($id)) $object->fetch($id);
 88            $this->object = $object;
 89    	}
 90    }
 91
 92    /**
 93     *  Load data control
 94     *
 95     *	@param	int		$id		Id of object
 96     *	@return	void
 97     */
 98    function doActions(&$action)
 99    {
100        global $conf, $user, $langs;
101
102        // Creation utilisateur depuis contact
103        if ($action == 'confirm_create_user' && GETPOST("confirm") == 'yes')
104        {
105            // Recuperation contact actuel
106            $result = $this->object->fetch($id);
107
108            if ($result > 0)
109            {
110                $this->db->begin();
111
112                // Creation user
113                $nuser = new User($this->db);
114                $result=$nuser->create_from_contact($this->object,$_POST["login"]);
115
116                if ($result > 0)
117                {
118                    $result2=$nuser->setPassword($user,$_POST["password"],0,1,1);
119                    if ($result2)
120                    {
121                        $this->db->commit();
122                    }
123                    else
124                    {
125                        $this->db->rollback();
126                    }
127                }
128                else
129                {
130                    $this->errors=$nuser->error;
131
132                    $this->db->rollback();
133                }
134            }
135            else
136            {
137                $this->errors=$this->object->errors;
138            }
139        }
140
141        // Creation contact
142        if ($action == 'add')
143        {
144            $this->assign_post();
145
146            if (! $_POST["name"])
147            {
148                array_push($this->errors,$langs->trans("ErrorFieldRequired",$langs->transnoentities("Lastname").' / '.$langs->transnoentities("Label")));
149                $action = 'create';
150            }
151
152            if ($_POST["name"])
153            {
154                $id =  $this->object->create($user);
155                if ($id > 0)
156                {
157                    Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
158                    exit;
159                }
160                else
161                {
162                    $this->errors=$this->object->errors;
163                    $action = 'create';
164                }
165            }
166        }
167
168        if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes')
169        {
170            $result=$this->object->fetch($id);
171
172            $this->object->old_name = $_POST["old_name"];
173            $this->object->old_firstname = $_POST["old_firstname"];
174
175            $result = $this->object->delete();
176            if ($result > 0)
177            {
178                Header("Location: list.php");
179                exit;
180            }
181            else
182            {
183                $this->errors=$this->object->errors;
184            }
185        }
186
187        if ($action == 'update')
188        {
189        	if ($_POST["cancel"])
190        	{
191        		Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
192        		exit;
193        	}
194        	
195            if (empty($_POST["name"]))
196            {
197                $this->error=array($langs->trans("ErrorFieldRequired",$langs->transnoentities("Name").' / '.$langs->transnoentities("Label")));
198                $action = 'edit';
199            }
200
201            if (empty($this->error))
202            {
203                $this->object->fetch($_POST["contactid"]);
204
205                $this->object->oldcopy=dol_clone($this->object);
206
207                $this->assign_post();
208
209                $result = $this->object->update($_POST["contactid"], $user);
210
211                if ($result > 0)
212                {
213                    Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$this->object->id);
214                    exit;
215                }
216                else
217                {
218                    $this->errors=$this->object->errors;
219                    $action = 'edit';
220                }
221            }
222        }
223    }
224
225	/**
226     *  Set content of ->tpl array, to use into template
227     *
228     *  @param      string		$action     Type of action
229     *  @return		string					HTML output
230     */
231    function assign_values(&$action)
232    {
233        global $conf, $langs, $user, $canvas;
234        global $form, $formcompany, $objsoc;
235
236        if ($action == 'add' || $action == 'update') $this->assign_post();
237
238        foreach($this->object as $key => $value)
239        {
240            $this->tpl[$key] = $value;
241        }
242
243        $this->tpl['error']=$this->error;
244        $this->tpl['errors']=$this->errors;
245
246        if ($action == 'create' || $action == 'edit')
247        {
248        	if ($conf->use_javascript_ajax)
249			{
250				$this->tpl['ajax_selectcountry'] = "\n".'<script type="text/javascript" language="javascript">
251				jQuery(document).ready(function () {
252						jQuery("#selectpays_id").change(function() {
253							document.formsoc.action.value="'.$action.'";
254							document.formsoc.canvas.value="'.$canvas.'";
255							document.formsoc.submit();
256						});
257					})
258				</script>'."\n";
259			}
260
261        	if (is_object($objsoc) && $objsoc->id > 0)
262        	{
263        		$this->tpl['company'] = $objsoc->getNomUrl(1);
264        		$this->tpl['company_id'] = $objsoc->id;
265        	}
266        	else
267        	{
268        		$this->tpl['company'] = $form->select_company($this->object->socid,'socid','',1);
269        	}
270
271        	// Civility
272        	$this->tpl['select_civility'] = $formcompany->select_civility($this->object->civilite_id);
273
274        	// Predefined with third party
275        	if ($objsoc->typent_code == 'TE_PRIVATE' || ! empty($conf->global->CONTACT_USE_COMPANY_ADDRESS))
276        	{
277        		if (dol_strlen(trim($this->object->address)) == 0) $this->tpl['address'] = $objsoc->address;
278        		if (dol_strlen(trim($this->object->zip)) == 0) $this->object->zip = $objsoc->zip;
279        		if (dol_strlen(trim($this->object->town)) == 0) $this->object->town = $objsoc->town;
280        		if (dol_strlen(trim($this->object->phone_pro)) == 0) $this->object->phone_pro = $objsoc->phone;
281        		if (dol_strlen(trim($this->object->fax)) == 0) $this->object->fax = $objsoc->fax;
282        		if (dol_strlen(trim($this->object->email)) == 0) $this->object->email = $objsoc->email;
283        	}
284
285            // Zip
286            $this->tpl['select_zip'] = $formcompany->select_ziptown($this->object->zip,'zipcode',array('town','selectpays_id','departement_id'),6);
287
288            // Town
289            $this->tpl['select_town'] = $formcompany->select_ziptown($this->object->town,'town',array('zipcode','selectpays_id','departement_id'));
290
291            if (dol_strlen(trim($this->object->fk_pays)) == 0) $this->object->fk_pays = $objsoc->pays_id;
292
293            // Country
294            $this->tpl['select_country'] = $form->select_country($this->object->fk_pays,'pays_id');
295            $countrynotdefined = $langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')';
296
297            if ($user->admin) $this->tpl['info_admin'] = info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
298
299            // State
300            if ($this->object->fk_pays) $this->tpl['select_state'] = $formcompany->select_state($this->object->fk_departement,$this->object->pays_code);
301            else $this->tpl['select_state'] = $countrynotdefined;
302
303            // Public or private
304            $selectarray=array('0'=>$langs->trans("ContactPublic"),'1'=>$langs->trans("ContactPrivate"));
305            $this->tpl['select_visibility'] = $form->selectarray('priv',$selectarray,$this->object->priv,0);
306        }
307
308        if ($action == 'view' || $action == 'edit' || $action == 'delete')
309        {
310        	// Emailing
311        	if ($conf->mailing->enabled)
312			{
313				$langs->load("mails");
314				$this->tpl['nb_emailing'] = $this->object->getNbOfEMailings();
315			}
316
317        	// Linked element
318        	$this->tpl['contact_element'] = array();
319        	$i=0;
320
321        	$this->object->load_ref_elements();
322
323        	if ($conf->commande->enabled)
324        	{
325        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForOrders");
326        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_commande?$this->object->ref_commande:$langs->trans("NoContactForAnyOrder");
327        		$i++;
328        	}
329        	if ($conf->propal->enabled)
330        	{
331        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForProposals");
332        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_propal?$this->object->ref_propal:$langs->trans("NoContactForAnyProposal");
333        		$i++;
334        	}
335        	if ($conf->contrat->enabled)
336        	{
337        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForContracts");
338        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_contrat?$this->object->ref_contrat:$langs->trans("NoContactForAnyContract");
339        		$i++;
340        	}
341        	if ($conf->facture->enabled)
342        	{
343        		$this->tpl['contact_element'][$i]['linked_element_label'] = $langs->trans("ContactForInvoices");
344        		$this->tpl['contact_element'][$i]['linked_element_value'] = $this->object->ref_facturation?$this->object->ref_facturation:$langs->trans("NoContactForAnyInvoice");
345        		$i++;
346        	}
347
348        	// Dolibarr user
349        	if ($this->object->user_id)
350			{
351				$dolibarr_user=new User($this->db);
352				$result=$dolibarr_user->fetch($this->object->user_id);
353				$this->tpl['dolibarr_user'] = $dolibarr_user->getLoginUrl(1);
354			}
355			else $this->tpl['dolibarr_user'] = $langs->trans("NoDolibarrAccess");
356        }
357
358        if ($action == 'view' || $action == 'delete')
359        {
360        	$this->tpl['showrefnav'] = $form->showrefnav($this->object,'id');
361
362        	if ($this->object->socid > 0)
363        	{
364        		$objsoc = new Societe($this->db);
365
366        		$objsoc->fetch($this->object->socid);
367        		$this->tpl['company'] = $objsoc->getNomUrl(1);
368        	}
369        	else
370        	{
371        		$this->tpl['company'] = $langs->trans("ContactNotLinkedToCompany");
372        	}
373
374        	$this->tpl['civility'] = $this->object->getCivilityLabel();
375
376            $this->tpl['address'] = dol_nl2br($this->object->address);
377
378            $this->tpl['zip'] = ($this->object->zip?$this->object->zip.'&nbsp;':'');
379
380            $img=picto_from_langcode($this->object->pays_code);
381            $this->tpl['country'] = ($img?$img.' ':'').$this->object->pays;
382
383            $this->tpl['phone_pro'] 	= dol_print_phone($this->object->phone_pro,$this->object->pays_code,0,$this->object->id,'AC_TEL');
384            $this->tpl['phone_perso'] 	= dol_print_phone($this->object->phone_perso,$this->object->pays_code,0,$this->object->id,'AC_TEL');
385            $this->tpl['phone_mobile'] 	= dol_print_phone($this->object->phone_mobile,$this->object->pays_code,0,$this->object->id,'AC_TEL');
386            $this->tpl['fax'] 			= dol_print_phone($this->object->fax,$this->object->pays_code,0,$this->object->id,'AC_FAX');
387            $this->tpl['email'] 		= dol_print_email($this->object->email,0,$this->object->id,'AC_EMAIL');
388
389            $this->tpl['visibility'] = $this->object->LibPubPriv($this->object->priv);
390
391            $this->tpl['note'] = nl2br($this->object->note);
392        }
393        
394        if ($action == 'create_user')
395        {
396        	// Full firstname and name separated with a dot : firstname.name
397        	include_once(DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php');
398        	$login=dol_buildlogin($this->object->nom, $this->object->prenom);
399        	
400        	$generated_password='';
401        	if (! $ldap_sid)
402        	{
403        		$generated_password=getRandomPassword('');
404        	}
405        	$password=$generated_password;
406        	
407        	// Create a form array
408        	$formquestion=array(
409        	array('label' => $langs->trans("LoginToCreate"), 'type' => 'text', 'name' => 'login', 'value' => $login),
410        	array('label' => $langs->trans("Password"), 'type' => 'text', 'name' => 'password', 'value' => $password));
411        	
412        	$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');
413        }
414    }
415
416    /**
417     *  Assign POST values into object
418     *
419     *  @return		string					HTML output
420     */
421    private function assign_post()
422    {
423        global $langs, $mysoc;
424
425        $this->object->old_name 			= 	$_POST["old_name"];
426        $this->object->old_firstname 		= 	$_POST["old_firstname"];
427
428        $this->object->socid				=	$_POST["socid"];
429        $this->object->name					=	$_POST["name"];
430        $this->object->firstname			= 	$_POST["firstname"];
431        $this->object->civilite_id			= 	$_POST["civilite_id"];
432        $this->object->poste				= 	$_POST["poste"];
433        $this->object->address				=	$_POST["address"];
434        $this->object->zip					=	$_POST["zipcode"];
435        $this->object->town					=	$_POST["town"];
436        $this->object->fk_pays				=	$_POST["pays_id"]?$_POST["pays_id"]:$mysoc->pays_id;
437        $this->object->fk_departement		=	$_POST["departement_id"];
438        $this->object->phone_pro			= 	$_POST["phone_pro"];
439        $this->object->phone_perso			= 	$_POST["phone_perso"];
440        $this->object->phone_mobile			= 	$_POST["phone_mobile"];
441        $this->object->fax					=	$_POST["fax"];
442        $this->object->email				=	$_POST["email"];
443        $this->object->jabberid				= 	$_POST["jabberid"];
444        $this->object->priv					= 	$_POST["priv"];
445        $this->object->note					=	$_POST["note"];
446        $this->object->canvas				=	$_POST["canvas"];
447
448        // We set pays_id, and pays_code label of the chosen country
449        if ($this->object->fk_pays)
450        {
451            $sql = "SELECT code, libelle FROM ".MAIN_DB_PREFIX."c_pays WHERE rowid = ".$this->object->fk_pays;
452            $resql=$this->db->query($sql);
453            if ($resql)
454            {
455                $obj = $this->db->fetch_object($resql);
456            }
457            else
458            {
459                dol_print_error($this->db);
460            }
461            $this->object->pays_code	=	$obj->code;
462            $this->object->pays			=	$langs->trans("Country".$obj->code)?$langs->trans("Country".$obj->code):$obj->libelle;
463        }
464    }
465
466}
467
468?>