From 7f86797f7376a9fb3097091aef481db2b9b9c731 Mon Sep 17 00:00:00 2001
From: frederic34 <frederic.france@free.fr>
Date: Sun, 4 Oct 2015 17:25:46 +0200
Subject: [PATCH] Add contact photo

---
 htdocs/contact/card.php                       | 112 ++++++++++++++++--
 htdocs/contact/class/contact.class.php        |  13 +-
 htdocs/core/class/html.form.class.php         |   5 +
 htdocs/core/lib/files.lib.php                 |  10 ++
 .../install/mysql/migration/3.8.0-3.9.0.sql   |   3 +-
 htdocs/install/mysql/tables/llx_socpeople.sql |   1 +
 6 files changed, 129 insertions(+), 15 deletions(-)

diff --git a/htdocs/contact/card.php b/htdocs/contact/card.php
index 4174ad16a06..2db5523c5bd 100644
--- a/htdocs/contact/card.php
+++ b/htdocs/contact/card.php
@@ -35,6 +35,7 @@ require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/contact.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
+require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
@@ -277,12 +278,72 @@ if (empty($reshook))
             $action = 'edit';
         }
 
+
         if (! $error)
         {
         	$contactid=GETPOST("contactid",'int');
 
             $object->fetch($contactid);
 
+            // Photo save
+            $dir = $conf->societe->dir_output."/contact/".$object->id."/photos";
+            $file_OK = is_uploaded_file($_FILES['photo']['tmp_name']);
+            if ($file_OK)
+            {
+                if (GETPOST('deletephoto'))
+                {
+                    $fileimg=$dir.'/'.$object->photo;
+                    $dirthumbs=$dir.'/thumbs';
+                    dol_delete_file($fileimg);
+                    dol_delete_dir_recursive($dirthumbs);
+                    $object->photo = '';
+                }
+
+                if (image_format_supported($_FILES['photo']['name']) > 0)
+                {
+                    dol_mkdir($dir);
+
+                    if (@is_dir($dir))
+                    {
+                        $newfile=$dir.'/'.dol_sanitizeFileName($_FILES['photo']['name']);
+                        $result = dol_move_uploaded_file($_FILES['photo']['tmp_name'], $newfile, 1);
+
+                        if (! $result > 0)
+                        {
+                            $errors[] = "ErrorFailedToSaveFile";
+                        }
+                        else
+                        {
+                            $object->photo = dol_sanitizeFileName($_FILES['photo']['name']);
+                            // Create small thumbs for company (Ratio is near 16/9)
+                            // Used on logon for example
+                            $imgThumbSmall = vignette($newfile, $maxwidthsmall, $maxheightsmall, '_small', $quality);
+
+                            // Create mini thumbs for company (Ratio is near 16/9)
+                            // Used on menu or for setup page for example
+                            $imgThumbMini = vignette($newfile, $maxwidthmini, $maxheightmini, '_mini', $quality);
+                        }
+                    }
+                }
+                else
+                {
+                    $errors[] = "ErrorBadImageFormat";
+                }
+            }
+            else
+            {
+                switch($_FILES['photo']['error'])
+                {
+                    case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
+                    case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
+                        $errors[] = "ErrorFileSizeTooLarge";
+                        break;
+                    case 3: //uploaded file was only partially uploaded
+                        $errors[] = "ErrorFilePartiallyUploaded";
+                        break;
+                }
+            }
+
 			$object->oldcopy = clone$object;
 
             $object->old_lastname	= GETPOST("old_lastname");
@@ -699,7 +760,7 @@ else
 				print '</script>'."\n";
             }
 
-            print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" name="formsoc">';
+            print '<form enctype="multipart/form-data" method="post" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" name="formsoc">';
             print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
             print '<input type="hidden" name="id" value="'.$id.'">';
             print '<input type="hidden" name="action" value="update">';
@@ -906,6 +967,23 @@ else
             else print $langs->trans("NoDolibarrAccess");
             print '</td></tr>';
 
+            // Photo
+            print '<tr class="hideonsmartphone">';
+            print '<td>'.fieldLabel('Photo','photoinput').'</td>';
+            print '<td colspan="3">';
+            if ($object->photo) {
+                print $form->showphoto('contact',$object);
+                print "<br>\n";
+            }
+            print '<table class="nobordernopadding">';
+            if ($object->photo) print '<tr><td><input type="checkbox" class="flat" name="deletephoto" id="photodelete"> '.$langs->trans("Delete").'<br><br></td></tr>';
+            //print '<tr><td>'.$langs->trans("PhotoFile").'</td></tr>';
+            print '<tr><td><input type="file" class="flat" name="photo" id="photoinput"></td></tr>';
+            print '</table>';
+
+            print '</td>';
+            print '</tr>';
+
             print '</table>';
 
             print dol_fiche_end();
@@ -973,12 +1051,12 @@ else
 
         // Name
         print '<tr><td width="20%">'.$langs->trans("Lastname").' / '.$langs->trans("Label").'</td><td width="30%">'.$object->lastname.'</td>';
-        print '<td width="20%">'.$langs->trans("Firstname").'</td><td width="30%">'.$object->firstname.'</td></tr>';
+        print '<td width="20%">'.$langs->trans("Firstname").'</td><td width="30%">'.$object->firstname.'</td>';
 
         // Company
         if (empty($conf->global->SOCIETE_DISABLE_CONTACTS))
         {
-            print '<tr><td>'.$langs->trans("ThirdParty").'</td><td colspan="3">';
+            print '<tr><td>'.$langs->trans("ThirdParty").'</td><td colspan="2">';
             if ($object->socid > 0)
             {
                 $objsoc->fetch($object->socid);
@@ -988,30 +1066,44 @@ else
             {
                 print $langs->trans("ContactNotLinkedToCompany");
             }
-            print '</td></tr>';
+            print '</td>';
+        }
+
+        // Photo
+        if ($object->photo)
+        {
+            print '<td rowspan="6" style="text-align: center;" width="25%">';
+            print $form->showphoto('contact',$object);
+            print '</td>';
+        } else {
+            print '<td rowspan="6" style="text-align: center;" width="25%">';
+            print '&nbsp;';
+            print '</td>';
         }
 
+        print '</tr>';
+
         // Civility
-        print '<tr><td width="15%">'.$langs->trans("UserTitle").'</td><td colspan="3">';
+        print '<tr><td width="15%">'.$langs->trans("UserTitle").'</td><td colspan="2">';
         print $object->getCivilityLabel();
         print '</td></tr>';
 
         // Role
-        print '<tr><td>'.$langs->trans("PostOrFunction").'</td><td colspan="3">'.$object->poste.'</td>';
+        print '<tr><td>'.$langs->trans("PostOrFunction").'</td><td colspan="2">'.$object->poste.'</td>';
 
         // Address
-        print '<tr><td>'.$langs->trans("Address").'</td><td colspan="3">';
+        print '<tr><td>'.$langs->trans("Address").'</td><td colspan="2">';
         dol_print_address($object->address,'gmap','contact',$object->id);
         print '</td></tr>';
 
         // Zip/Town
-        print '<tr><td>'.$langs->trans("Zip").' / '.$langs->trans("Town").'</td><td colspan="3">';
+        print '<tr><td>'.$langs->trans("Zip").' / '.$langs->trans("Town").'</td><td colspan="2">';
         print $object->zip;
         if ($object->zip) print '&nbsp;';
         print $object->town.'</td></tr>';
 
         // Country
-        print '<tr><td>'.$langs->trans("Country").'</td><td colspan="3">';
+        print '<tr><td>'.$langs->trans("Country").'</td><td colspan="2">';
         $img=picto_from_langcode($object->country_code);
         if ($img) print $img.' ';
         print $object->country;
@@ -1020,7 +1112,7 @@ else
         // State
         if (empty($conf->global->SOCIETE_DISABLE_STATE))
         {
-            print '<tr><td>'.$langs->trans('State').'</td><td colspan="3">'.$object->state.'</td>';
+            print '<tr><td>'.$langs->trans('State').'</td><td colspan="2">'.$object->state.'</td>';
         }
 
         // Phone
diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php
index 0f9f74a6dbc..33864376ac3 100644
--- a/htdocs/contact/class/contact.class.php
+++ b/htdocs/contact/class/contact.class.php
@@ -73,6 +73,7 @@ class Contact extends CommonObject
 	var $code;
 	var $email;
 	var $skype;
+    var $photo;
     var $jabberid;
 	var $phone_pro;
 	var $phone_perso;
@@ -240,6 +241,7 @@ class Contact extends CommonObject
 		$this->phone_mobile=trim($this->phone_mobile);
 		$this->jabberid=trim($this->jabberid);
 		$this->skype=trim($this->skype);
+		$this->photo=trim($this->photo);
 		$this->fax=trim($this->fax);
 		$this->zip=(empty($this->zip)?'':$this->zip);
 		$this->town=(empty($this->town)?'':$this->town);
@@ -264,6 +266,7 @@ class Contact extends CommonObject
 		$sql .= ", fax='".$this->db->escape($this->fax)."'";
 		$sql .= ", email='".$this->db->escape($this->email)."'";
 		$sql .= ", skype='".$this->db->escape($this->skype)."'";
+		$sql .= ", photo='".$this->db->escape($this->photo)."'";
 		$sql .= ", note_private = ".(isset($this->note_private)?"'".$this->db->escape($this->note_private)."'":"null");
 		$sql .= ", note_public = ".(isset($this->note_public)?"'".$this->db->escape($this->note_public)."'":"null");
 		$sql .= ", phone = ".(isset($this->phone_pro)?"'".$this->db->escape($this->phone_pro)."'":"null");
@@ -392,7 +395,7 @@ class Contact extends CommonObject
 		if ($this->phone_perso && ! empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso;
 		if ($this->phone_mobile && ! empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile;
 		if ($this->fax && ! empty($conf->global->LDAP_CONTACT_FIELD_FAX))	    $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax;
-    if ($this->skype && ! empty($conf->global->LDAP_CONTACT_FIELD_SKYPE))	    $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype;
+        if ($this->skype && ! empty($conf->global->LDAP_CONTACT_FIELD_SKYPE))	    $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype;
 		if ($this->note_private && ! empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = $this->note_private;
 		if ($this->email && ! empty($conf->global->LDAP_CONTACT_FIELD_MAIL))     $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;
 
@@ -518,6 +521,7 @@ class Contact extends CommonObject
 		$sql.= " c.fk_departement,";
 		$sql.= " c.birthday,";
 		$sql.= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid, c.skype,";
+        $sql.= " c.photo,";
 		$sql.= " c.priv, c.note_private, c.note_public, c.default_lang, c.no_email, c.canvas,";
 		$sql.= " c.import_key,";
 		$sql.= " co.label as country, co.code as country_code,";
@@ -573,6 +577,7 @@ class Contact extends CommonObject
 				$this->email			= $obj->email;
 				$this->jabberid			= $obj->jabberid;
         		$this->skype			= $obj->skype;
+                $this->photo			= $obj->photo;
 				$this->priv				= $obj->priv;
 				$this->mail				= $obj->email;
 
@@ -909,12 +914,12 @@ class Contact extends CommonObject
         $label = '<u>' . $langs->trans("ShowContact") . '</u>';
         $label.= '<br><b>' . $langs->trans("Name") . ':</b> '.$this->getFullName($langs);
         //if ($this->civility_id) $label.= '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id;		// TODO Translate cibilty_id code
-        $label.= '<br><b>' . $langs->trans("Poste") . ':</b> '.$this->poste;
-        $label.= '<br><b>' . $langs->trans("EMail") . ':</b> '.$this->email;
+        if (! empty($this->poste)) $label.= '<br><b>' . $langs->trans("Poste") . ':</b> '.$this->poste;
+        if (! empty($this->email)) $label.= '<br><b>' . $langs->trans("EMail") . ':</b> '.$this->email;
         $phonelist=array();
         if ($this->phone_pro) $phonelist[]=$this->phone_pro;
         if ($this->phone_mobile) $phonelist[]=$this->phone_mobile;
-        if ($this->phone_pesro) $phonelist[]=$this->phone_perso;
+        if ($this->phone_perso) $phonelist[]=$this->phone_perso;
         $label.= '<br><b>' . $langs->trans("Phone") . ':</b> '.join(', ',$phonelist);
         $label.= '<br><b>' . $langs->trans("Address") . ':</b> '.dol_format_address($this, 1, ' ', $langs);
 
diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php
index 40d68bec667..8d69e58bad3 100644
--- a/htdocs/core/class/html.form.class.php
+++ b/htdocs/core/class/html.form.class.php
@@ -5155,6 +5155,11 @@ class Form
             $smallfile=preg_replace('/(\.png|\.gif|\.jpg|\.jpeg|\.bmp)/i','_small\\1',$smallfile);
             if ($object->logo) $file=$id.'/logos/thumbs/'.$smallfile;
         }
+        if ($modulepart=='contact')
+        {
+            $dir=$conf->societe->multidir_output[$entity].'/contact';
+            $file=$id.'/photos/'.$object->photo;
+        }
         else if ($modulepart=='userphoto')
         {
             $dir=$conf->user->dir_output;
diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php
index f9b6bbf8b61..2f49b37f716 100644
--- a/htdocs/core/lib/files.lib.php
+++ b/htdocs/core/lib/files.lib.php
@@ -1926,6 +1926,16 @@ function dol_check_secure_access_document($modulepart,$original_file,$entity,$fu
 		$sqlprotectagainstexternals = "SELECT rowid as fk_soc FROM ".MAIN_DB_PREFIX."societe WHERE rowid='".$db->escape($refname)."' AND entity IN (".getEntity('societe', 1).")";
 	}
 
+	// Wrapping for contact
+	else if ($modulepart == 'contact')
+	{
+		if ($fuser->rights->societe->lire)
+		{
+			$accessallowed=1;
+		}
+		$original_file=$conf->societe->multidir_output[$entity].'/contact/'.$original_file;
+	}
+
 	// Wrapping for invoices
 	else if ($modulepart == 'facture' || $modulepart == 'invoice')
 	{
diff --git a/htdocs/install/mysql/migration/3.8.0-3.9.0.sql b/htdocs/install/mysql/migration/3.8.0-3.9.0.sql
index 8bda380c0dd..d8b6ce12ea4 100755
--- a/htdocs/install/mysql/migration/3.8.0-3.9.0.sql
+++ b/htdocs/install/mysql/migration/3.8.0-3.9.0.sql
@@ -52,5 +52,6 @@ ALTER TABLE llx_payment_salary CHANGE COLUMN fk_user_creat fk_user_author intege
 
 ALTER TABLE llx_adherent ADD COLUMN pass_crypted varchar(128) after pass;
 
+ALTER TABLE llx_paiement ADD COLUMN ref varchar(30) NOT NULL AFTER rowid;
 
-ALTER TABLE llx_paiement ADD COLUMN ref varchar(30) NOT NULL AFTER rowid;
\ No newline at end of file
+ALTER TABLE llx_socpeople ADD COLUMN photo varchar(255) AFTER skype;
diff --git a/htdocs/install/mysql/tables/llx_socpeople.sql b/htdocs/install/mysql/tables/llx_socpeople.sql
index 4b99598a3b9..b9aad05b956 100644
--- a/htdocs/install/mysql/tables/llx_socpeople.sql
+++ b/htdocs/install/mysql/tables/llx_socpeople.sql
@@ -43,6 +43,7 @@ create table llx_socpeople
   email				varchar(255),
   jabberid			varchar(255),
   skype				varchar(255),
+  photo				varchar(255),
   no_email			smallint NOT NULL DEFAULT 0,
   priv				smallint NOT NULL DEFAULT 0,
   fk_user_creat		integer DEFAULT 0,							-- user qui a creel'enregistrement
-- 
GitLab