From 72cb2ec0fe2fdd1ee895ce1ddb2f008a5f29610b Mon Sep 17 00:00:00 2001
From: Trey <treydock@tamu.edu>
Date: Mon, 29 Aug 2011 15:38:34 -0500
Subject: [PATCH] - Allow notifications to use different order for user's fullname IssueID #18

---
 inc/db.function.php                    |   13 +++++++++--
 inc/notificationmailsetting.class.php  |   10 ++++++++-
 inc/notificationtargetticket.class.php |   34 ++++++++++++++++----------------
 inc/user.class.php                     |   19 +++++++++++++----
 locales/en_GB.php                      |    1 +
 5 files changed, 51 insertions(+), 26 deletions(-)

diff --git a/inc/db.function.php b/inc/db.function.php
index 6f53c26..ce25574 100644
--- a/inc/db.function.php
+++ b/inc/db.function.php
@@ -1082,7 +1082,7 @@ function formatUserName($ID, $login, $realname, $firstname, $link=0, $cut=0, $fo
  *
  *@return string : username string (realname if not empty and name if realname is empty).
 **/
-function getUserName($ID, $link=0) {
+function getUserName($ID, $link=0, $for_notification=false) {
    global $DB, $CFG_GLPI, $LANG;
 
    $user = "";
@@ -1106,8 +1106,15 @@ function getUserName($ID, $link=0) {
 
       if ($DB->numrows($result)==1) {
          $data     = $DB->fetch_assoc($result);
-         $username = formatUserName($data["id"], $data["name"], $data["realname"],
-                                    $data["firstname"], $link);
+
+			//Allow notifications to use different name order
+         if ($for_notification && $CFG_GLPI["notif_names_format"]==FIRSTNAME_BEFORE) {
+		$username = formatUserName($data["id"], $data["name"], $data["firstname"],
+		                        $data["realname"], $link);
+			} else {
+		$username = formatUserName($data["id"], $data["name"], $data["realname"],
+		                        $data["firstname"], $link);
+			}
 
          if ($link==2) {
             $user["name"]    = $username;
diff --git a/inc/notificationmailsetting.class.php b/inc/notificationmailsetting.class.php
index a7623ca..683221b 100644
--- a/inc/notificationmailsetting.class.php
+++ b/inc/notificationmailsetting.class.php
@@ -159,6 +159,14 @@ class NotificationMailSetting extends CommonDBTM {
          echo "<td colspan='3'><textarea cols='60' rows='3' name='mailing_signature'>".
                                 $CFG_GLPI["mailing_signature"]."</textarea></td></tr>";
 
+
+         echo "<tr class='tab_bg_2'>";
+         echo "<td>" . $LANG['setup'][11] . "&nbsp;:</td><td>";
+			$values = array(REALNAME_BEFORE  => $LANG['common'][48]." ".$LANG['common'][43],
+								 FIRSTNAME_BEFORE => $LANG['common'][43]." ".$LANG['common'][48]);
+         Dropdown::showFromArray('notif_names_format', $values, array('value' => $CFG_GLPI["notif_names_format"]));
+			echo "</td></tr>";
+
          echo "<tr class='tab_bg_1'><th colspan='4'>".$LANG['setup'][660]."</th></tr>";
          echo "<tr class='tab_bg_2'><td>" . $LANG['setup'][231] . "&nbsp;:</td><td>";
          $mail_methods = array(MAIL_MAIL    => $LANG['setup'][650],
@@ -268,4 +276,4 @@ class NotificationMailSetting extends CommonDBTM {
    }
 
 }
-?>
\ No newline at end of file
+?>
diff --git a/inc/notificationtargetticket.class.php b/inc/notificationtargetticket.class.php
index e93b2c4..51fcb82 100644
--- a/inc/notificationtargetticket.class.php
+++ b/inc/notificationtargetticket.class.php
@@ -764,11 +764,11 @@ class NotificationTargetTicket extends NotificationTarget {
             foreach ($this->obj->getUsers(Ticket::REQUESTER) as $uid => $tmp) {
                $user_tmp = new User;
                $user_tmp->getFromDB($uid);
-               $users[$uid] = $user_tmp->getName();
+               $users[$uid] = $user_tmp->getName(0, true);
 
                $tmp = array();
                $tmp['##author.id##']   = $uid;
-               $tmp['##author.name##'] = $user_tmp->getName();
+               $tmp['##author.name##'] = $user_tmp->getName(0, true);
 
                if ($user_tmp->getField('locations_id')) {
                   $tmp['##author.location##']
@@ -791,7 +791,7 @@ class NotificationTargetTicket extends NotificationTarget {
          if ($this->obj->getField('users_id_recipient')) {
             $user_tmp = new User;
             $user_tmp->getFromDB($this->obj->getField('users_id_recipient'));
-            $this->datas['##ticket.openbyuser##'] = $user_tmp->getName();
+            $this->datas['##ticket.openbyuser##'] = $user_tmp->getName(0, true);
          } else {
             $this->datas['##ticket.openbyuser##'] = '';
          }
@@ -802,7 +802,7 @@ class NotificationTargetTicket extends NotificationTarget {
                $user_tmp = new User;
                $user_tmp->getFromDB($uid);
 
-               $users[$uid] = $user_tmp->getName();
+               $users[$uid] = $user_tmp->getName(0, true);
             }
             $this->datas['##ticket.assigntousers##'] = implode(', ',$users);
          } else {
@@ -815,7 +815,7 @@ class NotificationTargetTicket extends NotificationTarget {
             foreach ($this->obj->getUsers(Ticket::OBSERVER) as $uid => $tmp) {
                $user_tmp = new User();
                $user_tmp->getFromDB($uid);
-               $users[$uid] = $user_tmp->getName();
+               $users[$uid] = $user_tmp->getName(0, true);
             }
             $this->datas['##ticket.observerusers##'] = implode(', ',$users);
          } else {
@@ -912,7 +912,7 @@ class NotificationTargetTicket extends NotificationTarget {
             if ($this->obj->getField('users_id')) {
                $user_tmp = new User;
                if ($user_tmp->getFromDB($this->target_object->getField('users_id'))) {
-                  $this->datas['##ticket.item.user##'] = $user_tmp->getName();
+                  $this->datas['##ticket.item.user##'] = $user_tmp->getName(0, true);
                }
             }
 
@@ -990,7 +990,7 @@ class NotificationTargetTicket extends NotificationTarget {
          foreach ($tasks as $task) {
             $tmp = array();
             $tmp['##task.isprivate##']   = Dropdown::getYesNo($task['is_private']);
-            $tmp['##task.author##']      = html_clean(getUserName($task['users_id']));
+            $tmp['##task.author##']      = html_clean(getUserName($task['users_id'], 0, true));
             $tmp['##task.category##']    = Dropdown::getDropdownName('glpi_taskcategories',
                                                                      $task['taskcategories_id']);
             $tmp['##task.date##']        = convDateTime($task['date']);
@@ -999,7 +999,7 @@ class NotificationTargetTicket extends NotificationTarget {
 
             $plan = new TicketPlanning();
             if ($plan->getFromDBbyTask($task['id'])) {
-               $tmp['##task.planning.user##']   = html_clean(getUserName($plan->fields['users_id']));
+               $tmp['##task.planning.user##']   = html_clean(getUserName($plan->fields['users_id'], 0, true));
                $tmp['##task.planning.begin##']  = convDateTime($plan->fields['begin']);
                $tmp['##task.planning.end##']    = convDateTime($plan->fields['end']);
                $tmp['##task.planning.status##'] = Planning::getState($plan->fields['state']);
@@ -1024,7 +1024,7 @@ class NotificationTargetTicket extends NotificationTarget {
          foreach ($followups as $followup) {
             $tmp = array();
             $tmp['##followup.isprivate##']   = Dropdown::getYesNo($followup['is_private']);
-            $tmp['##followup.author##']      = html_clean(getUserName($followup['users_id']));
+            $tmp['##followup.author##']      = html_clean(getUserName($followup['users_id'], 0, true));
             $tmp['##followup.requesttype##']
                   = Dropdown::getDropdownName('glpi_requesttypes', $followup['requesttypes_id']);
             $tmp['##followup.date##']        = convDateTime($followup['date']);
@@ -1052,19 +1052,19 @@ class NotificationTargetTicket extends NotificationTarget {
             $tmp = array();
             $tmp['##validation.submission.title##']
                   = $LANG['validation'][27]." (".$LANG['job'][4]." ".
-                    html_clean(getUserName($validation['users_id'])).")";
+                    html_clean(getUserName($validation['users_id'], 0, true)).")";
             $tmp['##validation.answer.title##']
                   = $LANG['validation'][32]." (".$LANG['validation'][21]." ".
-                    html_clean(getUserName($validation['users_id_validate'])).")";
+                    html_clean(getUserName($validation['users_id_validate'], 0, true)).")";
 
-            $tmp['##validation.author##']       = html_clean(getUserName($validation['users_id']));
+            $tmp['##validation.author##']       = html_clean(getUserName($validation['users_id'], 0, true));
 
             $tmp['##validation.status##']       = TicketValidation::getStatus($validation['status']);
             $tmp['##validation.storestatus##']       = $validation['status'];
             $tmp['##validation.submissiondate##']    = convDateTime($validation['submission_date']);
             $tmp['##validation.commentsubmission##'] = $validation['comment_submission'];
             $tmp['##validation.validationdate##']    = convDateTime($validation['validation_date']);
-            $tmp['##validation.validator##']    =  html_clean(getUserName($validation['users_id_validate']));
+            $tmp['##validation.validator##']    =  html_clean(getUserName($validation['users_id_validate'], 0, true));
             $tmp['##validation.commentvalidation##'] = $validation['comment_validation'];
             $this->datas['validations'][] = $tmp;
          }
@@ -1163,12 +1163,12 @@ class NotificationTargetTicket extends NotificationTarget {
                      $user_tmp = new User();
                      $user_tmp->getFromDB($uid);
 
-                     $users[$uid] = $user_tmp->getName();
+                     $users[$uid] = $user_tmp->getName(0, true);
 
 
                      $tmp2 = array();
                      $tmp2['##author.id##']   = $uid;
-                     $tmp2['##author.name##'] = $user_tmp->getName();
+                     $tmp2['##author.name##'] = $user_tmp->getName(0, true);
 
                      if ($user_tmp->getField('locations_id')) {
                         $tmp2['##author.location##']
@@ -1195,7 +1195,7 @@ class NotificationTargetTicket extends NotificationTarget {
                      $user_tmp = new User();
                      $user_tmp->getFromDB($uid);
 
-                     $users[$uid] = $user_tmp->getName();
+                     $users[$uid] = $user_tmp->getName(0, true);
                   }
                   $tmp['##ticket.assigntousers##'] = implode(', ',$users);
                } else {
@@ -1227,7 +1227,7 @@ class NotificationTargetTicket extends NotificationTarget {
                   foreach ($t->getUsers(Ticket::OBSERVER) as $uid => $tmp) {
                      $user_tmp = new User();
                      $user_tmp->getFromDB($uid);
-                     $users[$uid] = $user_tmp->getName();
+                     $users[$uid] = $user_tmp->getName(0, true);
                   }
                   $tmp['##ticket.observerusers##'] = implode(', ',$users);
                } else {
diff --git a/inc/user.class.php b/inc/user.class.php
index 4f27705..d438545 100644
--- a/inc/user.class.php
+++ b/inc/user.class.php
@@ -738,13 +738,22 @@ class User extends CommonDBTM {
     *
     * @return string containing name of the user
    **/
-   function getName($with_comment=0) {
+   function getName($with_comment=0, $for_notification=false) {
+		global $CFG_GLPI;
 
       if (isset($this->fields["id"]) && $this->fields["id"]>0) {
-         return formatUserName($this->fields["id"],
-                               $this->fields["name"],
-                               (isset($this->fields["realname"]) ? $this->fields["realname"] : ''),
-                               (isset($this->fields["firstname"]) ? $this->fields["firstname"] : ''));
+			//Allow notifications to use different name format order
+         if ($for_notification && $CFG_GLPI["notif_names_format"]==FIRSTNAME_BEFORE) {
+		return formatUserName($this->fields["id"],
+		                   $this->fields["name"],
+		                (isset($this->fields["firstname"]) ? $this->fields["firstname"] : ''),
+			             (isset($this->fields["realname"]) ? $this->fields["realname"] : ''));
+			} else {
+		return formatUserName($this->fields["id"],
+		                   $this->fields["name"],
+		                (isset($this->fields["realname"]) ? $this->fields["realname"] : ''),
+			             (isset($this->fields["firstname"]) ? $this->fields["firstname"] : ''));
+			}
       }
       return NOT_AVAILABLE;
    }
diff --git a/locales/en_GB.php b/locales/en_GB.php
index 69dfd89..0d2fb45 100644
--- a/locales/en_GB.php
+++ b/locales/en_GB.php
@@ -1658,6 +1658,7 @@ $LANG['setup'][7]="CSV delimiter";
 $LANG['setup'][8]="Enable the financial and administrative informations by default"; 
 $LANG['setup'][9]="Network Interfaces"; 
 $LANG['setup'][10]="Display order of surnames firstnames"; 
+$LANG['setup'][11]="Display order of surnames firstnames for notifications";
 $LANG['setup'][14]="Email"; 
 $LANG['setup'][18]="Login"; 
 $LANG['setup'][19]="Password"; 
-- 
1.7.4.1

